Avoid using deprecated openldap functions.
[wine] / dlls / wldap32 / add.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 static LDAPMod *nullattrs[] = { NULL };
35 #else
36 #define LDAP_NOT_SUPPORTED  0x5c
37 #endif
38
39 #include "winldap_private.h"
40 #include "wldap32.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
43
44 ULONG ldap_addA( WLDAP32_LDAP *ld, PCHAR dn, LDAPModA *attrs[] )
45 {
46     ULONG ret = LDAP_NOT_SUPPORTED;
47 #ifdef HAVE_LDAP
48     WCHAR *dnW = NULL;
49     LDAPModW **attrsW = NULL;
50
51     ret = WLDAP32_LDAP_NO_MEMORY;
52
53     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), attrs );
54
55     if (!ld) return ~0UL;
56
57     if (dn) {
58         dnW = strAtoW( dn );
59         if (!dnW) goto exit;
60     }
61     if (attrs) {
62         attrsW = modarrayAtoW( attrs );
63         if (!attrsW) goto exit;
64     }
65
66     ret = ldap_addW( ld, dnW, attrsW );
67
68 exit:
69     strfreeW( dnW );
70     modarrayfreeW( attrsW );
71
72 #endif
73     return ret;
74 }
75
76 ULONG ldap_addW( WLDAP32_LDAP *ld, PWCHAR dn, LDAPModW *attrs[] )
77 {
78     ULONG ret = LDAP_NOT_SUPPORTED;
79 #ifdef HAVE_LDAP
80     char *dnU = NULL;
81     LDAPMod **attrsU = NULL;
82     int msg;
83  
84     ret = WLDAP32_LDAP_NO_MEMORY;
85
86     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), attrs );
87
88     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
89
90     if (dn) {
91         dnU = strWtoU( dn );
92         if (!dnU) goto exit;
93     }
94     if (attrs) {
95         attrsU = modarrayWtoU( attrs );
96         if (!attrsU) goto exit;
97     }
98
99     ret = ldap_add_ext( ld, dn ? dnU : "", attrs ? attrsU : nullattrs, NULL, NULL, &msg );
100
101     if (ret == LDAP_SUCCESS)
102         ret = msg;
103     else
104         ret = ~0UL;
105
106 exit:
107     strfreeU( dnU );
108     modarrayfreeU( attrsU );
109
110 #endif
111     return ret;
112 }
113
114 ULONG ldap_add_extA( WLDAP32_LDAP *ld, PCHAR dn, LDAPModA *attrs[],
115     PLDAPControlA *serverctrls, PLDAPControlA *clientctrls, ULONG *message )
116 {
117     ULONG ret = LDAP_NOT_SUPPORTED;
118 #ifdef HAVE_LDAP
119     WCHAR *dnW = NULL;
120     LDAPModW **attrsW = NULL;
121     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
122
123     ret = WLDAP32_LDAP_NO_MEMORY;
124
125     TRACE( "(%p, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn), attrs,
126            serverctrls, clientctrls, message );
127
128     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
129
130     if (dn) {
131         dnW = strAtoW( dn );
132         if (!dnW) goto exit;
133     }
134     if (attrs) {
135         attrsW = modarrayAtoW( attrs );
136         if (!attrsW) goto exit;
137     }
138     if (serverctrls) {
139         serverctrlsW = controlarrayAtoW( serverctrls );
140         if (!serverctrlsW) goto exit;
141     }
142     if (clientctrls) {
143         clientctrlsW = controlarrayAtoW( clientctrls );
144         if (!clientctrlsW) goto exit;
145     }
146
147     ret = ldap_add_extW( ld, dnW, attrsW, serverctrlsW, clientctrlsW, message );
148
149 exit:
150     strfreeW( dnW );
151     modarrayfreeW( attrsW );
152     controlarrayfreeW( serverctrlsW );
153     controlarrayfreeW( clientctrlsW );
154
155 #endif
156     return ret;
157 }
158
159 ULONG ldap_add_extW( WLDAP32_LDAP *ld, PWCHAR dn, LDAPModW *attrs[],
160     PLDAPControlW *serverctrls, PLDAPControlW *clientctrls, ULONG *message )
161 {
162     ULONG ret = LDAP_NOT_SUPPORTED;
163 #ifdef HAVE_LDAP
164     char *dnU = NULL;
165     LDAPMod **attrsU = NULL;
166     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
167     int dummy;
168
169     ret = WLDAP32_LDAP_NO_MEMORY;
170
171     TRACE( "(%p, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn), attrs,
172            serverctrls, clientctrls, message );
173
174     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
175
176     if (dn) {
177         dnU = strWtoU( dn );
178         if (!dnU) goto exit;
179     }
180     if (attrs) {
181         attrsU = modarrayWtoU( attrs );
182         if (!attrsU) goto exit;
183     }
184     if (serverctrls) {
185         serverctrlsU = controlarrayWtoU( serverctrls );
186         if (!serverctrlsU) goto exit;
187     }
188     if (clientctrls) {
189         clientctrlsU = controlarrayWtoU( clientctrls );
190         if (!clientctrlsU) goto exit;
191     }
192
193     ret = ldap_add_ext( ld, dn ? dnU : "", attrs ? attrsU : nullattrs, serverctrlsU,
194                         clientctrlsU, message ? (int *)message : &dummy );
195
196 exit:
197     strfreeU( dnU );
198     modarrayfreeU( attrsU );
199     controlarrayfreeU( serverctrlsU );
200     controlarrayfreeU( clientctrlsU );
201
202 #endif
203     return ret;
204 }
205
206 ULONG ldap_add_ext_sA( WLDAP32_LDAP *ld, PCHAR dn, LDAPModA *attrs[],
207     PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
208 {
209     ULONG ret = LDAP_NOT_SUPPORTED;
210 #ifdef HAVE_LDAP
211     WCHAR *dnW = NULL;
212     LDAPModW **attrsW = NULL;
213     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
214
215     ret = WLDAP32_LDAP_NO_MEMORY;
216
217     TRACE( "(%p, %s, %p, %p, %p)\n", ld, debugstr_a(dn), attrs,
218            serverctrls, clientctrls );
219
220     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
221
222     if (dn) {
223         dnW = strAtoW( dn );
224         if (!dnW) goto exit;
225     }
226     if (attrs) {
227         attrsW = modarrayAtoW( attrs );
228         if (!attrsW) goto exit;
229     }
230     if (serverctrls) {
231         serverctrlsW = controlarrayAtoW( serverctrls );
232         if (!serverctrlsW) goto exit;
233     }
234     if (clientctrls) {
235         clientctrlsW = controlarrayAtoW( clientctrls );
236         if (!clientctrlsW) goto exit;
237     }
238
239     ret = ldap_add_ext_sW( ld, dnW, attrsW, serverctrlsW, clientctrlsW );
240
241 exit:
242     strfreeW( dnW );
243     modarrayfreeW( attrsW );
244     controlarrayfreeW( serverctrlsW );
245     controlarrayfreeW( clientctrlsW );
246
247 #endif
248     return ret;
249 }
250
251 ULONG ldap_add_ext_sW( WLDAP32_LDAP *ld, PWCHAR dn, LDAPModW *attrs[],
252     PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
253 {
254     ULONG ret = LDAP_NOT_SUPPORTED;
255 #ifdef HAVE_LDAP
256     char *dnU = NULL;
257     LDAPMod **attrsU = NULL;
258     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
259
260     ret = WLDAP32_LDAP_NO_MEMORY;
261
262     TRACE( "(%p, %s, %p, %p, %p)\n", ld, debugstr_w(dn), attrs,
263            serverctrls, clientctrls );
264
265     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
266     if (!attrs) return LDAP_PROTOCOL_ERROR;
267
268     if (dn) {
269         dnU = strWtoU( dn );
270         if (!dnU) goto exit;
271     }
272     if (attrs) {
273         attrsU = modarrayWtoU( attrs );
274         if (!attrsU) goto exit;
275     }
276     if (serverctrls) {
277         serverctrlsU = controlarrayWtoU( serverctrls );
278         if (!serverctrlsU) goto exit;
279     }
280     if (clientctrls) {
281         clientctrlsU = controlarrayWtoU( clientctrls );
282         if (!clientctrlsU) goto exit;
283     }
284
285     ret = ldap_add_ext_s( ld, dn ? dnU : "", attrs ? attrsU : nullattrs,
286                           serverctrlsU, clientctrlsU );
287
288 exit:
289     strfreeU( dnU );
290     modarrayfreeU( attrsU );
291     controlarrayfreeU( serverctrlsU );
292     controlarrayfreeU( clientctrlsU );
293
294 #endif
295     return ret;
296 }
297
298 ULONG ldap_add_sA( WLDAP32_LDAP *ld, PCHAR dn, LDAPModA *attrs[] )
299 {
300     ULONG ret = LDAP_NOT_SUPPORTED;
301 #ifdef HAVE_LDAP
302     WCHAR *dnW = NULL;
303     LDAPModW **attrsW = NULL;
304
305     ret = WLDAP32_LDAP_NO_MEMORY;
306
307     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), attrs );
308
309     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
310
311     if (dn) {
312         dnW = strAtoW( dn );
313         if (!dnW) goto exit;
314     }
315     if (attrs) {
316         attrsW = modarrayAtoW( attrs );
317         if (!attrsW) goto exit;
318     }
319
320     ret = ldap_add_sW( ld, dnW, attrsW );
321
322 exit:
323     strfreeW( dnW );
324     modarrayfreeW( attrsW );
325
326 #endif
327     return ret;
328 }
329
330 ULONG ldap_add_sW( WLDAP32_LDAP *ld, PWCHAR dn, LDAPModW *attrs[] )
331 {
332     ULONG ret = LDAP_NOT_SUPPORTED;
333 #ifdef HAVE_LDAP
334     char *dnU = NULL;
335     LDAPMod **attrsU = NULL;
336
337     ret = WLDAP32_LDAP_NO_MEMORY;
338
339     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), attrs );
340
341     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
342
343     if (dn) {
344         dnU = strWtoU( dn );
345         if (!dnU) goto exit;
346     }
347     if (attrs) {
348         attrsU = modarrayWtoU( attrs );
349         if (!attrsU) goto exit;
350     }
351
352     ret = ldap_add_ext_s( ld, dn ? dnU : "", attrs ? attrsU : nullattrs, NULL, NULL );
353
354 exit:
355     strfreeU( dnU );
356     modarrayfreeU( attrsU );
357
358 #endif
359     return ret;
360 }