wldap32: Map Unix error codes to Windows ones for all functions.
[wine] / dlls / wldap32 / compare.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 /***********************************************************************
42  *      ldap_compareA     (WLDAP32.@)
43  *
44  * See ldap_compareW.
45  */
46 ULONG CDECL ldap_compareA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR attr, PCHAR value )
47 {
48     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
49 #ifdef HAVE_LDAP
50     WCHAR *dnW = NULL, *attrW = NULL, *valueW = NULL;
51
52     ret = ~0UL;
53
54     TRACE( "(%p, %s, %s, %s)\n", ld, debugstr_a(dn), debugstr_a(attr),
55            debugstr_a(value) );
56
57     if (!ld || !attr) return ~0UL;
58
59     if (dn) {
60         dnW = strAtoW( dn );
61         if (!dnW) goto exit;
62     }
63
64     attrW = strAtoW( attr );
65     if (!attrW) goto exit;
66
67     if (value) {
68         valueW = strAtoW( value );
69         if (!valueW) goto exit;
70     }
71
72     ret = ldap_compareW( ld, dnW, attrW, valueW );
73
74 exit:
75     strfreeW( dnW );
76     strfreeW( attrW );
77     strfreeW( valueW );
78
79 #endif
80     return ret;
81 }
82
83 /***********************************************************************
84  *      ldap_compareW     (WLDAP32.@)
85  *
86  * Check if an attribute has a certain value (asynchronous operation).
87  *
88  * PARAMS
89  *  ld      [I] Pointer to an LDAP context.
90  *  dn      [I] DN of entry to compare value for.
91  *  attr    [I] Attribute to compare value for.
92  *  value   [I] Value to compare.
93  *
94  * RETURNS
95  *  Success: Message ID of the compare operation.
96  *  Failure: An LDAP error code.
97  */
98 ULONG CDECL ldap_compareW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR attr, PWCHAR value )
99 {
100     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
101 #ifdef HAVE_LDAP
102     char *dnU = NULL, *attrU = NULL, *valueU = NULL;
103     struct berval val = { 0, NULL };
104     int msg;
105
106     ret = ~0UL;
107
108     TRACE( "(%p, %s, %s, %s)\n", ld, debugstr_w(dn), debugstr_w(attr),
109            debugstr_w(value) );
110
111     if (!ld || !attr) return ~0UL;
112
113     if (dn) {
114         dnU = strWtoU( dn );
115         if (!dnU) goto exit;
116     }
117
118     attrU = strWtoU( attr );
119     if (!attrU) goto exit;
120
121     if (value) {
122         valueU = strWtoU( value );
123         if (!valueU) goto exit;
124
125         val.bv_len = strlen( valueU );
126         val.bv_val = valueU;
127     }
128
129     ret = ldap_compare_ext( ld, dn ? dnU : "", attrU, &val, NULL, NULL, &msg );
130
131     if (ret == LDAP_SUCCESS)
132         ret = msg;
133     else
134         ret = ~0UL;
135
136 exit:
137     strfreeU( dnU );
138     strfreeU( attrU );
139     strfreeU( valueU );
140
141 #endif
142     return ret;
143 }
144
145 /***********************************************************************
146  *      ldap_compare_extA     (WLDAP32.@)
147  *
148  * See ldap_compare_extW.
149  */
150 ULONG CDECL ldap_compare_extA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR attr, PCHAR value,
151     struct WLDAP32_berval *data, PLDAPControlA *serverctrls, PLDAPControlA *clientctrls,
152     ULONG *message )
153 {
154     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
155 #ifdef HAVE_LDAP
156     WCHAR *dnW = NULL, *attrW = NULL, *valueW = NULL;
157     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
158
159     ret = WLDAP32_LDAP_NO_MEMORY;
160
161     TRACE( "(%p, %s, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
162            debugstr_a(attr), debugstr_a(value), data, serverctrls,
163            clientctrls, message );
164
165     if (!ld || !message) return WLDAP32_LDAP_PARAM_ERROR;
166
167     if (dn) {
168         dnW = strAtoW( dn );
169         if (!dnW) goto exit;
170     }
171     if (attr) {
172         attrW = strAtoW( attr );
173         if (!attrW) goto exit;
174     }
175     if (value) {
176         valueW = strAtoW( value );
177         if (!valueW) goto exit;
178     }
179     if (serverctrls) {
180         serverctrlsW = controlarrayAtoW( serverctrls );
181         if (!serverctrlsW) goto exit;
182     }
183     if (clientctrls) {
184         clientctrlsW = controlarrayAtoW( clientctrls );
185         if (!clientctrlsW) goto exit;
186     }
187
188     ret = ldap_compare_extW( ld, dnW, attrW, valueW, data,
189                              serverctrlsW, clientctrlsW, message );
190
191 exit:
192     strfreeW( dnW );
193     strfreeW( attrW );
194     strfreeW( valueW );
195     controlarrayfreeW( serverctrlsW );
196     controlarrayfreeW( clientctrlsW );
197
198 #endif
199     return ret;
200 }
201
202 /***********************************************************************
203  *      ldap_compare_extW     (WLDAP32.@)
204  *
205  * Check if an attribute has a certain value (asynchronous operation).
206  *
207  * PARAMS
208  *  ld          [I] Pointer to an LDAP context.
209  *  dn          [I] DN of entry to compare value for.
210  *  attr        [I] Attribute to compare value for.
211  *  value       [I] string encoded value to compare.
212  *  data        [I] berval encoded value to compare.
213  *  serverctrls [I] Array of LDAP server controls.
214  *  clientctrls [I] Array of LDAP client controls.
215  *  message     [O] Message ID of the compare operation.
216  *
217  * RETURNS
218  *  Success: LDAP_SUCCESS
219  *  Failure: An LDAP error code.
220  *
221  * NOTES
222  *  Set value to compare strings or data to compare binary values. If
223  *  both are non-NULL, data will be used. The serverctrls and clientctrls
224  *  parameters are optional and should be set to NULL if not used.
225  */
226 ULONG CDECL ldap_compare_extW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR attr, PWCHAR value,
227     struct WLDAP32_berval *data, PLDAPControlW *serverctrls, PLDAPControlW *clientctrls,
228     ULONG *message )
229 {
230     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
231 #ifdef HAVE_LDAP
232     char *dnU = NULL, *attrU = NULL, *valueU = NULL;
233     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
234     struct berval val = { 0, NULL };
235
236     ret = WLDAP32_LDAP_NO_MEMORY;
237
238     TRACE( "(%p, %s, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
239            debugstr_w(attr), debugstr_w(value), data, serverctrls,
240            clientctrls, message );
241
242     if (!ld || !message) return WLDAP32_LDAP_PARAM_ERROR;
243     if (!attr) return WLDAP32_LDAP_NO_MEMORY;
244
245     if (dn) {
246         dnU = strWtoU( dn );
247         if (!dnU) goto exit;
248     }
249
250     attrU = strWtoU( attr );
251     if (!attrU) goto exit;
252
253     if (!data) {
254         if (value) {
255             valueU = strWtoU( value );
256             if (!valueU) goto exit;
257
258             val.bv_len = strlen( valueU );
259             val.bv_val = valueU;
260         }
261     }
262     if (serverctrls) {
263         serverctrlsU = controlarrayWtoU( serverctrls );
264         if (!serverctrlsU) goto exit;
265     }
266     if (clientctrls) {
267         clientctrlsU = controlarrayWtoU( clientctrls );
268         if (!clientctrlsU) goto exit;
269     }
270
271     ret = map_error( ldap_compare_ext( ld, dn ? dnU : "", attrU, data ? (struct berval *)data : &val,
272                                        serverctrlsU, clientctrlsU, (int *)message ));
273
274 exit:
275     strfreeU( dnU );
276     strfreeU( attrU );
277     strfreeU( valueU );
278     controlarrayfreeU( serverctrlsU );
279     controlarrayfreeU( clientctrlsU );
280
281 #endif
282     return ret;
283 }
284
285 /***********************************************************************
286  *      ldap_compare_ext_sA     (WLDAP32.@)
287  *
288  * See ldap_compare_ext_sW.
289  */
290 ULONG CDECL ldap_compare_ext_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR attr, PCHAR value,
291     struct WLDAP32_berval *data, PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
292 {
293     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
294 #ifdef HAVE_LDAP
295     WCHAR *dnW = NULL, *attrW = NULL, *valueW = NULL;
296     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
297
298     ret = WLDAP32_LDAP_NO_MEMORY;
299
300     TRACE( "(%p, %s, %s, %s, %p, %p, %p)\n", ld, debugstr_a(dn),
301            debugstr_a(attr), debugstr_a(value), data, serverctrls,
302            clientctrls );
303
304     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
305
306     if (dn) {
307         dnW = strAtoW( dn );
308         if (!dnW) goto exit;
309     }
310     if (attr) {
311         attrW = strAtoW( attr );
312         if (!attrW) goto exit;
313     }
314     if (value) {
315         valueW = strAtoW( value );
316         if (!valueW) goto exit;
317     }
318     if (serverctrls) {
319         serverctrlsW = controlarrayAtoW( serverctrls );
320         if (!serverctrlsW) goto exit;
321     }
322     if (clientctrls) {
323         clientctrlsW = controlarrayAtoW( clientctrls );
324         if (!clientctrlsW) goto exit;
325     }
326
327     ret = ldap_compare_ext_sW( ld, dnW, attrW, valueW, data, serverctrlsW,
328                                clientctrlsW );
329
330 exit:
331     strfreeW( dnW );
332     strfreeW( attrW );
333     strfreeW( valueW );
334     controlarrayfreeW( serverctrlsW );
335     controlarrayfreeW( clientctrlsW );
336
337 #endif
338     return ret;
339 }
340
341 /***********************************************************************
342  *      ldap_compare_ext_sW     (WLDAP32.@)
343  *
344  * Check if an attribute has a certain value (synchronous operation).
345  *
346  * PARAMS
347  *  ld          [I] Pointer to an LDAP context.
348  *  dn          [I] DN of entry to compare value for.
349  *  attr        [I] Attribute to compare value for.
350  *  value       [I] string encoded value to compare.
351  *  data        [I] berval encoded value to compare.
352  *  serverctrls [I] Array of LDAP server controls.
353  *  clientctrls [I] Array of LDAP client controls.
354  *
355  * RETURNS
356  *  Success: LDAP_SUCCESS
357  *  Failure: An LDAP error code.
358  *
359  * NOTES
360  *  Set value to compare strings or data to compare binary values. If
361  *  both are non-NULL, data will be used. The serverctrls and clientctrls
362  *  parameters are optional and should be set to NULL if not used.
363  */
364 ULONG CDECL ldap_compare_ext_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR attr, PWCHAR value,
365     struct WLDAP32_berval *data, PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
366 {
367     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
368 #ifdef HAVE_LDAP
369     char *dnU = NULL, *attrU = NULL, *valueU = NULL;
370     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
371     struct berval val = { 0, NULL };
372
373     ret = WLDAP32_LDAP_NO_MEMORY;
374
375     TRACE( "(%p, %s, %s, %s, %p, %p, %p)\n", ld, debugstr_w(dn),
376            debugstr_w(attr), debugstr_w(value), data, serverctrls,
377            clientctrls );
378
379     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
380
381     if (dn) {
382         dnU = strWtoU( dn );
383         if (!dnU) goto exit;
384     }
385     if (attr) {
386         attrU = strWtoU( attr );
387         if (!attrU) goto exit;
388     }
389     if (!data) {
390         if (value) {
391             valueU = strWtoU( value );
392             if (!valueU) goto exit;
393
394             val.bv_len = strlen( valueU );
395             val.bv_val = valueU;
396         }
397     }
398     if (serverctrls) {
399         serverctrlsU = controlarrayWtoU( serverctrls );
400         if (!serverctrlsU) goto exit;
401     }
402     if (clientctrls) {
403         clientctrlsU = controlarrayWtoU( clientctrls );
404         if (!clientctrlsU) goto exit;
405     }
406
407     ret = map_error( ldap_compare_ext_s( ld, dn ? dnU : "", attr ? attrU : "",
408                                          data ? (struct berval *)data : &val,
409                                          serverctrlsU, clientctrlsU ));
410
411 exit:
412     strfreeU( dnU );
413     strfreeU( attrU );
414     strfreeU( valueU );
415     controlarrayfreeU( serverctrlsU );
416     controlarrayfreeU( clientctrlsU );
417
418 #endif
419     return ret;
420 }
421
422 /***********************************************************************
423  *      ldap_compare_sA     (WLDAP32.@)
424  *
425  * See ldap_compare_sW.
426  */
427 ULONG CDECL ldap_compare_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR attr, PCHAR value )
428 {
429     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
430 #ifdef HAVE_LDAP
431     WCHAR *dnW = NULL, *attrW = NULL, *valueW = NULL;
432
433     ret = WLDAP32_LDAP_NO_MEMORY;
434
435     TRACE( "(%p, %s, %s, %s)\n", ld, debugstr_a(dn), debugstr_a(attr),
436            debugstr_a(value) );
437
438     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
439
440     if (dn) {
441         dnW = strAtoW( dn );
442         if (!dnW) goto exit;
443     }
444     if (attr) {
445         attrW = strAtoW( attr );
446         if (!attrW) goto exit;
447     }
448     if (value) {
449         valueW = strAtoW( value );
450         if (!valueW) goto exit;
451     }
452
453     ret = ldap_compare_sW( ld, dnW, attrW, valueW );
454
455 exit:
456     strfreeW( dnW );
457     strfreeW( attrW );
458     strfreeW( valueW );
459
460 #endif
461     return ret;
462 }
463
464 /***********************************************************************
465  *      ldap_compare_sW     (WLDAP32.@)
466  *
467  * Check if an attribute has a certain value (synchronous operation).
468  *
469  * PARAMS
470  *  ld      [I] Pointer to an LDAP context.
471  *  dn      [I] DN of entry to compare value for.
472  *  attr    [I] Attribute to compare value for.
473  *  value   [I] Value to compare.
474  *
475  * RETURNS
476  *  Success: LDAP_SUCCESS
477  *  Failure: An LDAP error code.
478  */
479 ULONG CDECL ldap_compare_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR attr, PWCHAR value )
480 {
481     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
482 #ifdef HAVE_LDAP
483     char *dnU = NULL, *attrU = NULL, *valueU = NULL;
484     struct berval val = { 0, NULL };
485
486     ret = WLDAP32_LDAP_NO_MEMORY;
487
488     TRACE( "(%p, %s, %s, %s)\n", ld, debugstr_w(dn), debugstr_w(attr),
489            debugstr_w(value) );
490
491     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
492
493     if (dn) {
494         dnU = strWtoU( dn );
495         if (!dnU) goto exit;
496     }
497     if (attr) {
498         attrU = strWtoU( attr );
499         if (!attrU) goto exit;
500     }
501     if (value) {
502         valueU = strWtoU( value );
503         if (!valueU) goto exit;
504
505         val.bv_len = strlen( valueU );
506         val.bv_val = valueU;
507     }
508
509     ret = map_error( ldap_compare_ext_s( ld, dn ? dnU : "", attr ? attrU : "", &val, NULL, NULL ));
510
511 exit:
512     strfreeU( dnU );
513     strfreeU( attrU );
514     strfreeU( valueU );
515
516 #endif
517     return ret;
518 }