wldap32: Always return WLDAP32 error values instead of redefining the ldap.h ones.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #endif
35
36 #include "winldap_private.h"
37 #include "wldap32.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
40
41 #ifdef HAVE_LDAP
42 static LDAPMod *nullattrs[] = { NULL };
43 #endif
44
45 /***********************************************************************
46  *      ldap_addA     (WLDAP32.@)
47  *
48  * See ldap_addW.
49  */
50 ULONG CDECL ldap_addA( WLDAP32_LDAP *ld, PCHAR dn, LDAPModA *attrs[] )
51 {
52     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
53 #ifdef HAVE_LDAP
54     WCHAR *dnW = NULL;
55     LDAPModW **attrsW = NULL;
56
57     ret = WLDAP32_LDAP_NO_MEMORY;
58
59     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), attrs );
60
61     if (!ld) return ~0UL;
62
63     if (dn) {
64         dnW = strAtoW( dn );
65         if (!dnW) goto exit;
66     }
67     if (attrs) {
68         attrsW = modarrayAtoW( attrs );
69         if (!attrsW) goto exit;
70     }
71
72     ret = ldap_addW( ld, dnW, attrsW );
73
74 exit:
75     strfreeW( dnW );
76     modarrayfreeW( attrsW );
77
78 #endif
79     return ret;
80 }
81
82 /***********************************************************************
83  *      ldap_addW     (WLDAP32.@)
84  *
85  * Add an entry to a directory tree (asynchronous operation).
86  *
87  * PARAMS
88  *  ld      [I] Pointer to an LDAP context.
89  *  dn      [I] DN of the entry to add.
90  *  attrs   [I] Pointer to an array of LDAPModW structures, each
91  *              specifying an attribute and its values to add.
92  *
93  * RETURNS
94  *  Success: Message ID of the add operation.
95  *  Failure: An LDAP error code.
96  *
97  * NOTES
98  *  Call ldap_result with the message ID to get the result of
99  *  the operation. Cancel the operation by calling ldap_abandon
100  *  with the message ID.
101  */
102 ULONG CDECL ldap_addW( WLDAP32_LDAP *ld, PWCHAR dn, LDAPModW *attrs[] )
103 {
104     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
105 #ifdef HAVE_LDAP
106     char *dnU = NULL;
107     LDAPMod **attrsU = NULL;
108     int msg;
109  
110     ret = WLDAP32_LDAP_NO_MEMORY;
111
112     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), attrs );
113
114     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
115
116     if (dn) {
117         dnU = strWtoU( dn );
118         if (!dnU) goto exit;
119     }
120     if (attrs) {
121         attrsU = modarrayWtoU( attrs );
122         if (!attrsU) goto exit;
123     }
124
125     ret = ldap_add_ext( ld, dn ? dnU : "", attrs ? attrsU : nullattrs, NULL, NULL, &msg );
126
127     if (ret == LDAP_SUCCESS)
128         ret = msg;
129     else
130         ret = ~0UL;
131
132 exit:
133     strfreeU( dnU );
134     modarrayfreeU( attrsU );
135
136 #endif
137     return ret;
138 }
139
140 /***********************************************************************
141  *      ldap_add_extA     (WLDAP32.@)
142  *
143  * See ldap_add_extW.
144  */
145 ULONG CDECL ldap_add_extA( WLDAP32_LDAP *ld, PCHAR dn, LDAPModA *attrs[],
146     PLDAPControlA *serverctrls, PLDAPControlA *clientctrls, ULONG *message )
147 {
148     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
149 #ifdef HAVE_LDAP
150     WCHAR *dnW = NULL;
151     LDAPModW **attrsW = NULL;
152     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
153
154     ret = WLDAP32_LDAP_NO_MEMORY;
155
156     TRACE( "(%p, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn), attrs,
157            serverctrls, clientctrls, message );
158
159     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
160
161     if (dn) {
162         dnW = strAtoW( dn );
163         if (!dnW) goto exit;
164     }
165     if (attrs) {
166         attrsW = modarrayAtoW( attrs );
167         if (!attrsW) goto exit;
168     }
169     if (serverctrls) {
170         serverctrlsW = controlarrayAtoW( serverctrls );
171         if (!serverctrlsW) goto exit;
172     }
173     if (clientctrls) {
174         clientctrlsW = controlarrayAtoW( clientctrls );
175         if (!clientctrlsW) goto exit;
176     }
177
178     ret = ldap_add_extW( ld, dnW, attrsW, serverctrlsW, clientctrlsW, message );
179
180 exit:
181     strfreeW( dnW );
182     modarrayfreeW( attrsW );
183     controlarrayfreeW( serverctrlsW );
184     controlarrayfreeW( clientctrlsW );
185
186 #endif
187     return ret;
188 }
189
190 /***********************************************************************
191  *      ldap_add_extW     (WLDAP32.@)
192  *
193  * Add an entry to a directory tree (asynchronous operation).
194  *
195  * PARAMS
196  *  ld          [I] Pointer to an LDAP context.
197  *  dn          [I] DN of the entry to add.
198  *  attrs       [I] Pointer to an array of LDAPModW structures, each
199  *                  specifying an attribute and its values to add.
200  *  serverctrls [I] Array of LDAP server controls.
201  *  clientctrls [I] Array of LDAP client controls.
202  *  message     [O] Message ID of the add operation.
203  *
204  * RETURNS
205  *  Success: LDAP_SUCCESS
206  *  Failure: An LDAP error code.
207  *
208  * NOTES
209  *  Call ldap_result with the message ID to get the result of
210  *  the operation. The serverctrls and clientctrls parameters are
211  *  optional and should be set to NULL if not used.
212  */
213 ULONG CDECL ldap_add_extW( WLDAP32_LDAP *ld, PWCHAR dn, LDAPModW *attrs[],
214     PLDAPControlW *serverctrls, PLDAPControlW *clientctrls, ULONG *message )
215 {
216     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
217 #ifdef HAVE_LDAP
218     char *dnU = NULL;
219     LDAPMod **attrsU = NULL;
220     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
221     int dummy;
222
223     ret = WLDAP32_LDAP_NO_MEMORY;
224
225     TRACE( "(%p, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn), attrs,
226            serverctrls, clientctrls, message );
227
228     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
229
230     if (dn) {
231         dnU = strWtoU( dn );
232         if (!dnU) goto exit;
233     }
234     if (attrs) {
235         attrsU = modarrayWtoU( attrs );
236         if (!attrsU) goto exit;
237     }
238     if (serverctrls) {
239         serverctrlsU = controlarrayWtoU( serverctrls );
240         if (!serverctrlsU) goto exit;
241     }
242     if (clientctrls) {
243         clientctrlsU = controlarrayWtoU( clientctrls );
244         if (!clientctrlsU) goto exit;
245     }
246
247     ret = ldap_add_ext( ld, dn ? dnU : "", attrs ? attrsU : nullattrs, serverctrlsU,
248                         clientctrlsU, message ? (int *)message : &dummy );
249
250 exit:
251     strfreeU( dnU );
252     modarrayfreeU( attrsU );
253     controlarrayfreeU( serverctrlsU );
254     controlarrayfreeU( clientctrlsU );
255
256 #endif
257     return ret;
258 }
259
260 /***********************************************************************
261  *      ldap_add_ext_sA     (WLDAP32.@)
262  *
263  * See ldap_add_ext_sW.
264  */
265 ULONG CDECL ldap_add_ext_sA( WLDAP32_LDAP *ld, PCHAR dn, LDAPModA *attrs[],
266     PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
267 {
268     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
269 #ifdef HAVE_LDAP
270     WCHAR *dnW = NULL;
271     LDAPModW **attrsW = NULL;
272     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
273
274     ret = WLDAP32_LDAP_NO_MEMORY;
275
276     TRACE( "(%p, %s, %p, %p, %p)\n", ld, debugstr_a(dn), attrs,
277            serverctrls, clientctrls );
278
279     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
280
281     if (dn) {
282         dnW = strAtoW( dn );
283         if (!dnW) goto exit;
284     }
285     if (attrs) {
286         attrsW = modarrayAtoW( attrs );
287         if (!attrsW) goto exit;
288     }
289     if (serverctrls) {
290         serverctrlsW = controlarrayAtoW( serverctrls );
291         if (!serverctrlsW) goto exit;
292     }
293     if (clientctrls) {
294         clientctrlsW = controlarrayAtoW( clientctrls );
295         if (!clientctrlsW) goto exit;
296     }
297
298     ret = ldap_add_ext_sW( ld, dnW, attrsW, serverctrlsW, clientctrlsW );
299
300 exit:
301     strfreeW( dnW );
302     modarrayfreeW( attrsW );
303     controlarrayfreeW( serverctrlsW );
304     controlarrayfreeW( clientctrlsW );
305
306 #endif
307     return ret;
308 }
309
310 /***********************************************************************
311  *      ldap_add_ext_sW     (WLDAP32.@)
312  *
313  * Add an entry to a directory tree (synchronous operation).
314  *
315  * PARAMS
316  *  ld          [I] Pointer to an LDAP context.
317  *  dn          [I] DN of the entry to add.
318  *  attrs       [I] Pointer to an array of LDAPModW structures, each
319  *                  specifying an attribute and its values to add.
320  *  serverctrls [I] Array of LDAP server controls.
321  *  clientctrls [I] Array of LDAP client controls.
322  *
323  * RETURNS
324  *  Success: LDAP_SUCCESS
325  *  Failure: An LDAP error code.
326  *
327  * NOTES
328  *  The serverctrls and clientctrls parameters are optional and
329  *  should be set to NULL if not used.
330  */
331 ULONG CDECL ldap_add_ext_sW( WLDAP32_LDAP *ld, PWCHAR dn, LDAPModW *attrs[],
332     PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
333 {
334     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
335 #ifdef HAVE_LDAP
336     char *dnU = NULL;
337     LDAPMod **attrsU = NULL;
338     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
339
340     ret = WLDAP32_LDAP_NO_MEMORY;
341
342     TRACE( "(%p, %s, %p, %p, %p)\n", ld, debugstr_w(dn), attrs,
343            serverctrls, clientctrls );
344
345     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
346
347     if (dn) {
348         dnU = strWtoU( dn );
349         if (!dnU) goto exit;
350     }
351     if (attrs) {
352         attrsU = modarrayWtoU( attrs );
353         if (!attrsU) goto exit;
354     }
355     if (serverctrls) {
356         serverctrlsU = controlarrayWtoU( serverctrls );
357         if (!serverctrlsU) goto exit;
358     }
359     if (clientctrls) {
360         clientctrlsU = controlarrayWtoU( clientctrls );
361         if (!clientctrlsU) goto exit;
362     }
363
364     ret = ldap_add_ext_s( ld, dn ? dnU : "", attrs ? attrsU : nullattrs,
365                           serverctrlsU, clientctrlsU );
366
367 exit:
368     strfreeU( dnU );
369     modarrayfreeU( attrsU );
370     controlarrayfreeU( serverctrlsU );
371     controlarrayfreeU( clientctrlsU );
372
373 #endif
374     return ret;
375 }
376
377 /***********************************************************************
378  *      ldap_add_sA     (WLDAP32.@)
379  *
380  * See ldap_add_sW.
381  */
382 ULONG CDECL ldap_add_sA( WLDAP32_LDAP *ld, PCHAR dn, LDAPModA *attrs[] )
383 {
384     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
385 #ifdef HAVE_LDAP
386     WCHAR *dnW = NULL;
387     LDAPModW **attrsW = NULL;
388
389     ret = WLDAP32_LDAP_NO_MEMORY;
390
391     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), attrs );
392
393     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
394
395     if (dn) {
396         dnW = strAtoW( dn );
397         if (!dnW) goto exit;
398     }
399     if (attrs) {
400         attrsW = modarrayAtoW( attrs );
401         if (!attrsW) goto exit;
402     }
403
404     ret = ldap_add_sW( ld, dnW, attrsW );
405
406 exit:
407     strfreeW( dnW );
408     modarrayfreeW( attrsW );
409
410 #endif
411     return ret;
412 }
413
414 /***********************************************************************
415  *      ldap_add_sW     (WLDAP32.@)
416  *
417  * Add an entry to a directory tree (synchronous operation).
418  *
419  * PARAMS
420  *  ld      [I] Pointer to an LDAP context.
421  *  dn      [I] DN of the entry to add.
422  *  attrs   [I] Pointer to an array of LDAPModW structures, each
423  *              specifying an attribute and its values to add.
424  *
425  * RETURNS
426  *  Success: LDAP_SUCCESS
427  *  Failure: An LDAP error code.
428  */
429 ULONG CDECL ldap_add_sW( WLDAP32_LDAP *ld, PWCHAR dn, LDAPModW *attrs[] )
430 {
431     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
432 #ifdef HAVE_LDAP
433     char *dnU = NULL;
434     LDAPMod **attrsU = NULL;
435
436     ret = WLDAP32_LDAP_NO_MEMORY;
437
438     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), attrs );
439
440     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
441
442     if (dn) {
443         dnU = strWtoU( dn );
444         if (!dnU) goto exit;
445     }
446     if (attrs) {
447         attrsU = modarrayWtoU( attrs );
448         if (!attrsU) goto exit;
449     }
450
451     ret = ldap_add_ext_s( ld, dn ? dnU : "", attrs ? attrsU : nullattrs, NULL, NULL );
452
453 exit:
454     strfreeU( dnU );
455     modarrayfreeU( attrsU );
456
457 #endif
458     return ret;
459 }