Avoid using deprecated openldap functions.
[wine] / dlls / wldap32 / dn.c
1 /*
2  * WLDAP32 - LDAP support for Wine
3  *
4  * Copyright 2005 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
31
32 #ifdef HAVE_LDAP_H
33 #include <ldap.h>
34 #else
35 #define LDAP_SUCCESS        0x00
36 #endif
37
38 #include "winldap_private.h"
39 #include "wldap32.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
42
43 PCHAR ldap_dn2ufnA( PCHAR dn )
44 {
45     PCHAR ret = NULL;
46 #ifdef HAVE_LDAP
47     WCHAR *dnW, *retW;
48
49     TRACE( "(%s)\n", debugstr_a(dn) );
50
51     dnW = strAtoW( dn );
52     if (!dnW) return NULL;
53
54     retW = ldap_dn2ufnW( dnW );
55     ret = strWtoA( retW );
56
57     strfreeW( dnW );
58     ldap_memfreeW( retW );
59
60 #endif
61     return ret;
62 }
63
64 PWCHAR ldap_dn2ufnW( PWCHAR dn )
65 {
66     PWCHAR ret = NULL;
67 #ifdef HAVE_LDAP
68     char *dnU, *retU;
69
70     TRACE( "(%s)\n", debugstr_w(dn) );
71
72     dnU = strWtoU( dn );
73     if (!dnU) return NULL;
74
75     retU = ldap_dn2ufn( dnU );
76     ret = strUtoW( retU );
77
78     strfreeU( dnU );
79     ldap_memfree( retU );
80
81 #endif
82     return ret;
83 }
84
85 PCHAR *ldap_explode_dnA( PCHAR dn, ULONG notypes )
86 {
87     PCHAR *ret = NULL;
88 #ifdef HAVE_LDAP
89     WCHAR *dnW, **retW;
90
91     TRACE( "(%s, 0x%08lx)\n", debugstr_a(dn), notypes );
92
93     dnW = strAtoW( dn );
94     if (!dnW) return NULL;
95
96     retW = ldap_explode_dnW( dnW, notypes );
97     ret = strarrayWtoA( retW );
98
99     strfreeW( dnW );
100     ldap_value_freeW( retW );
101
102 #endif
103     return ret;
104 }
105
106 PWCHAR *ldap_explode_dnW( PWCHAR dn, ULONG notypes )
107 {
108     PWCHAR *ret = NULL;
109 #ifdef HAVE_LDAP
110     char *dnU, **retU;
111
112     TRACE( "(%s, 0x%08lx)\n", debugstr_w(dn), notypes );
113
114     dnU = strWtoU( dn );
115     if (!dnU) return NULL;
116
117     retU = ldap_explode_dn( dnU, notypes );
118     ret = strarrayUtoW( retU );
119
120     strfreeU( dnU );
121     ldap_memvfree( (void **)retU );
122
123 #endif
124     return ret;
125 }
126
127 PCHAR ldap_get_dnA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
128 {
129     PCHAR ret = NULL;
130 #ifdef HAVE_LDAP
131     PWCHAR retW;
132
133     TRACE( "(%p, %p)\n", ld, entry );
134
135     if (!ld || !entry) return NULL;
136
137     retW = ldap_get_dnW( ld, entry );
138
139     ret = strWtoA( retW );
140     ldap_memfreeW( retW );
141
142 #endif
143     return ret;
144 }
145
146 PWCHAR ldap_get_dnW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
147 {
148     PWCHAR ret = NULL;
149 #ifdef HAVE_LDAP
150     char *retU;
151
152     TRACE( "(%p, %p)\n", ld, entry );
153
154     if (!ld || !entry) return NULL;
155
156     retU = ldap_get_dn( ld, entry );
157
158     ret = strUtoW( retU );
159     ldap_memfree( retU );
160
161 #endif
162     return ret;
163 }
164
165 ULONG ldap_ufn2dnA( PCHAR ufn, PCHAR *dn )
166 {
167     ULONG ret = LDAP_SUCCESS;
168 #ifdef HAVE_LDAP
169     PWCHAR ufnW = NULL, dnW = NULL;
170
171     TRACE( "(%s, %p)\n", debugstr_a(ufn), dn );
172
173     if (!dn) return WLDAP32_LDAP_PARAM_ERROR;
174
175     *dn = NULL;
176
177     if (ufn) {
178         ufnW = strAtoW( ufn );
179         if (!ufnW) return WLDAP32_LDAP_NO_MEMORY;
180     }
181
182     ret = ldap_ufn2dnW( ufnW, &dnW );
183
184     if (dnW) {
185         *dn = strWtoA( dnW );
186         if (!*dn) ret = WLDAP32_LDAP_NO_MEMORY;
187     }
188
189     strfreeW( ufnW );
190     ldap_memfreeW( dnW );
191
192 #endif
193     return ret;
194 }
195
196 ULONG ldap_ufn2dnW( PWCHAR ufn, PWCHAR *dn )
197 {
198     ULONG ret = LDAP_SUCCESS;
199 #ifdef HAVE_LDAP
200     char *ufnU = NULL;
201
202     TRACE( "(%s, %p)\n", debugstr_w(ufn), dn );
203
204     if (!dn) return WLDAP32_LDAP_PARAM_ERROR;
205
206     *dn = NULL;
207
208     if (ufn) {
209         ufnU = strWtoU( ufn );
210         if (!ufnU) return WLDAP32_LDAP_NO_MEMORY;
211
212         /* FIXME: do more than just a copy */
213         *dn = strUtoW( ufnU );
214         if (!*dn) ret = WLDAP32_LDAP_NO_MEMORY;
215     }
216
217     strfreeU( ufnU );
218
219 #endif
220     return ret;
221 }