xref: /reactos/dll/win32/wldap32/bind.c (revision c2c66aff)
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 "winldap_private.h"
22 
23 /***********************************************************************
24  *      ldap_bindA     (WLDAP32.@)
25  *
26  * See ldap_bindW.
27  */
28 ULONG CDECL ldap_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
29 {
30     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
31 #ifdef HAVE_LDAP
32     WCHAR *dnW = NULL, *credW = NULL;
33 
34     ret = WLDAP32_LDAP_NO_MEMORY;
35 
36     TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_a(dn), cred, method );
37 
38     if (!ld) return ~0u;
39 
40     if (dn) {
41         dnW = strAtoW( dn );
42         if (!dnW) goto exit;
43     }
44     if (cred) {
45         credW = strAtoW( cred );
46         if (!credW) goto exit;
47     }
48 
49     ret = ldap_bindW( ld, dnW, credW, method );
50 
51 exit:
52     strfreeW( dnW );
53     strfreeW( credW );
54 
55 #endif
56     return ret;
57 }
58 
59 /***********************************************************************
60  *      ldap_bindW     (WLDAP32.@)
61  *
62  * Authenticate with an LDAP server (asynchronous operation).
63  *
64  * PARAMS
65  *  ld      [I] Pointer to an LDAP context.
66  *  dn      [I] DN of entry to bind as.
67  *  cred    [I] Credentials (e.g. password string).
68  *  method  [I] Authentication method.
69  *
70  * RETURNS
71  *  Success: Message ID of the bind operation.
72  *  Failure: An LDAP error code.
73  *
74  * NOTES
75  *  Only LDAP_AUTH_SIMPLE is supported (just like native).
76  */
77 ULONG CDECL ldap_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
78 {
79     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
80 #ifdef HAVE_LDAP
81     char *dnU = NULL, *credU = NULL;
82     struct berval pwd = { 0, NULL };
83     int msg;
84 
85     ret = WLDAP32_LDAP_NO_MEMORY;
86 
87     TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_w(dn), cred, method );
88 
89     if (!ld) return ~0u;
90     if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
91 
92     if (dn) {
93         dnU = strWtoU( dn );
94         if (!dnU) goto exit;
95     }
96     if (cred) {
97         credU = strWtoU( cred );
98         if (!credU) goto exit;
99 
100         pwd.bv_len = strlen( credU );
101         pwd.bv_val = credU;
102     }
103 
104     ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
105 
106     if (ret == LDAP_SUCCESS)
107         ret = msg;
108     else
109         ret = ~0u;
110 
111 exit:
112     strfreeU( dnU );
113     strfreeU( credU );
114 
115 #endif
116     return ret;
117 }
118 
119 /***********************************************************************
120  *      ldap_bind_sA     (WLDAP32.@)
121  *
122  * See ldap_bind_sW.
123  */
124 ULONG CDECL ldap_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
125 {
126     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
127 #ifdef HAVE_LDAP
128     WCHAR *dnW = NULL, *credW = NULL;
129 
130     ret = WLDAP32_LDAP_NO_MEMORY;
131 
132     TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_a(dn), cred, method );
133 
134     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
135 
136     if (dn) {
137         dnW = strAtoW( dn );
138         if (!dnW) goto exit;
139     }
140     if (cred) {
141         credW = strAtoW( cred );
142         if (!credW) goto exit;
143     }
144 
145     ret = ldap_bind_sW( ld, dnW, credW, method );
146 
147 exit:
148     strfreeW( dnW );
149     strfreeW( credW );
150 
151 #endif
152     return ret;
153 }
154 
155 /***********************************************************************
156  *      ldap_bind_sW     (WLDAP32.@)
157  *
158  * Authenticate with an LDAP server (synchronous operation).
159  *
160  * PARAMS
161  *  ld      [I] Pointer to an LDAP context.
162  *  dn      [I] DN of entry to bind as.
163  *  cred    [I] Credentials (e.g. password string).
164  *  method  [I] Authentication method.
165  *
166  * RETURNS
167  *  Success: LDAP_SUCCESS
168  *  Failure: An LDAP error code.
169  */
170 ULONG CDECL ldap_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
171 {
172     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
173 #ifdef HAVE_LDAP
174     char *dnU = NULL, *credU = NULL;
175     struct berval pwd = { 0, NULL };
176 
177     ret = WLDAP32_LDAP_NO_MEMORY;
178 
179     TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_w(dn), cred, method );
180 
181     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
182     if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
183 
184     if (dn) {
185         dnU = strWtoU( dn );
186         if (!dnU) goto exit;
187     }
188     if (cred) {
189         credU = strWtoU( cred );
190         if (!credU) goto exit;
191 
192         pwd.bv_len = strlen( credU );
193         pwd.bv_val = credU;
194     }
195 
196     ret = map_error( ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL ));
197 
198 exit:
199     strfreeU( dnU );
200     strfreeU( credU );
201 
202 #endif
203     return ret;
204 }
205 
206 /***********************************************************************
207  *      ldap_sasl_bindA     (WLDAP32.@)
208  *
209  * See ldap_sasl_bindW.
210  */
211 ULONG CDECL ldap_sasl_bindA( WLDAP32_LDAP *ld, const PCHAR dn,
212     const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
213     PLDAPControlA *clientctrls, int *message )
214 {
215     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
216 #ifdef HAVE_LDAP
217     WCHAR *dnW, *mechanismW = NULL;
218     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
219 
220     ret = WLDAP32_LDAP_NO_MEMORY;
221 
222     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
223            debugstr_a(mechanism), cred, serverctrls, clientctrls, message );
224 
225     if (!ld || !dn || !mechanism || !cred || !message)
226         return WLDAP32_LDAP_PARAM_ERROR;
227 
228     dnW = strAtoW( dn );
229     if (!dnW) goto exit;
230 
231     mechanismW = strAtoW( mechanism );
232     if (!mechanismW) goto exit;
233 
234     if (serverctrls) {
235         serverctrlsW = controlarrayAtoW( serverctrls );
236         if (!serverctrlsW) goto exit;
237     }
238     if (clientctrls) {
239         clientctrlsW = controlarrayAtoW( clientctrls );
240         if (!clientctrlsW) goto exit;
241     }
242 
243     ret = ldap_sasl_bindW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, message );
244 
245 exit:
246     strfreeW( dnW );
247     strfreeW( mechanismW );
248     controlarrayfreeW( serverctrlsW );
249     controlarrayfreeW( clientctrlsW );
250 
251 #endif
252     return ret;
253 }
254 
255 /***********************************************************************
256  *      ldap_sasl_bindW     (WLDAP32.@)
257  *
258  * Authenticate with an LDAP server using SASL (asynchronous operation).
259  *
260  * PARAMS
261  *  ld          [I] Pointer to an LDAP context.
262  *  dn          [I] DN of entry to bind as.
263  *  mechanism   [I] Authentication method.
264  *  cred        [I] Credentials.
265  *  serverctrls [I] Array of LDAP server controls.
266  *  clientctrls [I] Array of LDAP client controls.
267  *  message     [O] Message ID of the bind operation.
268  *
269  * RETURNS
270  *  Success: LDAP_SUCCESS
271  *  Failure: An LDAP error code.
272  *
273  * NOTES
274  *  The serverctrls and clientctrls parameters are optional and should
275  *  be set to NULL if not used.
276  */
277 ULONG CDECL ldap_sasl_bindW( WLDAP32_LDAP *ld, const PWCHAR dn,
278     const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
279     PLDAPControlW *clientctrls, int *message )
280 {
281     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
282 #ifdef HAVE_LDAP
283     char *dnU, *mechanismU = NULL;
284     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
285     struct berval credU;
286 
287     ret = WLDAP32_LDAP_NO_MEMORY;
288 
289     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
290            debugstr_w(mechanism), cred, serverctrls, clientctrls, message );
291 
292     if (!ld || !dn || !mechanism || !cred || !message)
293         return WLDAP32_LDAP_PARAM_ERROR;
294 
295     dnU = strWtoU( dn );
296     if (!dnU) goto exit;
297 
298     mechanismU = strWtoU( mechanism );
299     if (!mechanismU) goto exit;
300 
301     if (serverctrls) {
302         serverctrlsU = controlarrayWtoU( serverctrls );
303         if (!serverctrlsU) goto exit;
304     }
305     if (clientctrls) {
306         clientctrlsU = controlarrayWtoU( clientctrls );
307         if (!clientctrlsU) goto exit;
308     }
309 
310     credU.bv_len = cred->bv_len;
311     credU.bv_val = cred->bv_val;
312 
313     ret = map_error( ldap_sasl_bind( ld, dnU, mechanismU, &credU,
314                                      serverctrlsU, clientctrlsU, message ));
315 
316 exit:
317     strfreeU( dnU );
318     strfreeU( mechanismU );
319     controlarrayfreeU( serverctrlsU );
320     controlarrayfreeU( clientctrlsU );
321 
322 #endif
323     return ret;
324 }
325 
326 /***********************************************************************
327  *      ldap_sasl_bind_sA     (WLDAP32.@)
328  *
329  * See ldap_sasl_bind_sW.
330  */
331 ULONG CDECL ldap_sasl_bind_sA( WLDAP32_LDAP *ld, const PCHAR dn,
332     const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
333     PLDAPControlA *clientctrls, PBERVAL *serverdata )
334 {
335     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
336 #ifdef HAVE_LDAP
337     WCHAR *dnW, *mechanismW = NULL;
338     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
339 
340     ret = WLDAP32_LDAP_NO_MEMORY;
341 
342     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
343            debugstr_a(mechanism), cred, serverctrls, clientctrls, serverdata );
344 
345     if (!ld || !dn || !mechanism || !cred || !serverdata)
346         return WLDAP32_LDAP_PARAM_ERROR;
347 
348     dnW = strAtoW( dn );
349     if (!dnW) goto exit;
350 
351     mechanismW = strAtoW( mechanism );
352     if (!mechanismW) goto exit;
353 
354     if (serverctrls) {
355         serverctrlsW = controlarrayAtoW( serverctrls );
356         if (!serverctrlsW) goto exit;
357     }
358     if (clientctrls) {
359         clientctrlsW = controlarrayAtoW( clientctrls );
360         if (!clientctrlsW) goto exit;
361     }
362 
363     ret = ldap_sasl_bind_sW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, serverdata );
364 
365 exit:
366     strfreeW( dnW );
367     strfreeW( mechanismW );
368     controlarrayfreeW( serverctrlsW );
369     controlarrayfreeW( clientctrlsW );
370 
371 #endif
372     return ret;
373 }
374 
375 /***********************************************************************
376  *      ldap_sasl_bind_sW     (WLDAP32.@)
377  *
378  * Authenticate with an LDAP server using SASL (synchronous operation).
379  *
380  * PARAMS
381  *  ld          [I] Pointer to an LDAP context.
382  *  dn          [I] DN of entry to bind as.
383  *  mechanism   [I] Authentication method.
384  *  cred        [I] Credentials.
385  *  serverctrls [I] Array of LDAP server controls.
386  *  clientctrls [I] Array of LDAP client controls.
387  *  serverdata  [O] Authentication response from the server.
388  *
389  * RETURNS
390  *  Success: LDAP_SUCCESS
391  *  Failure: An LDAP error code.
392  *
393  * NOTES
394  *  The serverctrls and clientctrls parameters are optional and should
395  *  be set to NULL if not used.
396  */
397 ULONG CDECL ldap_sasl_bind_sW( WLDAP32_LDAP *ld, const PWCHAR dn,
398     const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
399     PLDAPControlW *clientctrls, PBERVAL *serverdata )
400 {
401     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
402 #ifdef HAVE_LDAP
403     char *dnU, *mechanismU = NULL;
404     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
405     struct berval credU;
406 
407     ret = WLDAP32_LDAP_NO_MEMORY;
408 
409     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
410            debugstr_w(mechanism), cred, serverctrls, clientctrls, serverdata );
411 
412     if (!ld || !dn || !mechanism || !cred || !serverdata)
413         return WLDAP32_LDAP_PARAM_ERROR;
414 
415     dnU = strWtoU( dn );
416     if (!dnU) goto exit;
417 
418     mechanismU = strWtoU( mechanism );
419     if (!mechanismU) goto exit;
420 
421     if (serverctrls) {
422         serverctrlsU = controlarrayWtoU( serverctrls );
423         if (!serverctrlsU) goto exit;
424     }
425     if (clientctrls) {
426         clientctrlsU = controlarrayWtoU( clientctrls );
427         if (!clientctrlsU) goto exit;
428     }
429 
430     credU.bv_len = cred->bv_len;
431     credU.bv_val = cred->bv_val;
432 
433     ret = map_error( ldap_sasl_bind_s( ld, dnU, mechanismU, &credU,
434                                        serverctrlsU, clientctrlsU, (struct berval **)serverdata ));
435 
436 exit:
437     strfreeU( dnU );
438     strfreeU( mechanismU );
439     controlarrayfreeU( serverctrlsU );
440     controlarrayfreeU( clientctrlsU );
441 
442 #endif
443     return ret;
444 }
445 
446 /***********************************************************************
447  *      ldap_simple_bindA     (WLDAP32.@)
448  *
449  * See ldap_simple_bindW.
450  */
451 ULONG CDECL ldap_simple_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
452 {
453     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
454 #ifdef HAVE_LDAP
455     WCHAR *dnW = NULL, *passwdW = NULL;
456 
457     ret = WLDAP32_LDAP_NO_MEMORY;
458 
459     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
460 
461     if (!ld) return ~0u;
462 
463     if (dn) {
464         dnW = strAtoW( dn );
465         if (!dnW) goto exit;
466     }
467     if (passwd) {
468         passwdW = strAtoW( passwd );
469         if (!passwdW) goto exit;
470     }
471 
472     ret = ldap_simple_bindW( ld, dnW, passwdW );
473 
474 exit:
475     strfreeW( dnW );
476     strfreeW( passwdW );
477 
478 #endif
479     return ret;
480 }
481 
482 /***********************************************************************
483  *      ldap_simple_bindW     (WLDAP32.@)
484  *
485  * Authenticate with an LDAP server (asynchronous operation).
486  *
487  * PARAMS
488  *  ld      [I] Pointer to an LDAP context.
489  *  dn      [I] DN of entry to bind as.
490  *  passwd  [I] Password string.
491  *
492  * RETURNS
493  *  Success: Message ID of the bind operation.
494  *  Failure: An LDAP error code.
495  *
496  * NOTES
497  *  Set dn and passwd to NULL to bind as an anonymous user.
498  */
499 ULONG CDECL ldap_simple_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
500 {
501     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
502 #ifdef HAVE_LDAP
503     char *dnU = NULL, *passwdU = NULL;
504     struct berval pwd = { 0, NULL };
505     int msg;
506 
507     ret = WLDAP32_LDAP_NO_MEMORY;
508 
509     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
510 
511     if (!ld) return ~0u;
512 
513     if (dn) {
514         dnU = strWtoU( dn );
515         if (!dnU) goto exit;
516     }
517     if (passwd) {
518         passwdU = strWtoU( passwd );
519         if (!passwdU) goto exit;
520 
521         pwd.bv_len = strlen( passwdU );
522         pwd.bv_val = passwdU;
523     }
524 
525     ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
526 
527     if (ret == LDAP_SUCCESS)
528         ret = msg;
529     else
530         ret = ~0u;
531 
532 exit:
533     strfreeU( dnU );
534     strfreeU( passwdU );
535 
536 #endif
537     return ret;
538 }
539 
540 /***********************************************************************
541  *      ldap_simple_bind_sA     (WLDAP32.@)
542  *
543  * See ldap_simple_bind_sW.
544  */
545 ULONG CDECL ldap_simple_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
546 {
547     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
548 #ifdef HAVE_LDAP
549     WCHAR *dnW = NULL, *passwdW = NULL;
550 
551     ret = WLDAP32_LDAP_NO_MEMORY;
552 
553     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
554 
555     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
556 
557     if (dn) {
558         dnW = strAtoW( dn );
559         if (!dnW) goto exit;
560     }
561     if (passwd) {
562         passwdW = strAtoW( passwd );
563         if (!passwdW) goto exit;
564     }
565 
566     ret = ldap_simple_bind_sW( ld, dnW, passwdW );
567 
568 exit:
569     strfreeW( dnW );
570     strfreeW( passwdW );
571 
572 #endif
573     return ret;
574 }
575 
576 /***********************************************************************
577  *      ldap_simple_bind_sW     (WLDAP32.@)
578  *
579  * Authenticate with an LDAP server (synchronous operation).
580  *
581  * PARAMS
582  *  ld      [I] Pointer to an LDAP context.
583  *  dn      [I] DN of entry to bind as.
584  *  passwd  [I] Password string.
585  *
586  * RETURNS
587  *  Success: LDAP_SUCCESS
588  *  Failure: An LDAP error code.
589  *
590  * NOTES
591  *  Set dn and passwd to NULL to bind as an anonymous user.
592  */
593 ULONG CDECL ldap_simple_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
594 {
595     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
596 #ifdef HAVE_LDAP
597     char *dnU = NULL, *passwdU = NULL;
598     struct berval pwd = { 0, NULL };
599 
600     ret = WLDAP32_LDAP_NO_MEMORY;
601 
602     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
603 
604     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
605 
606     if (dn) {
607         dnU = strWtoU( dn );
608         if (!dnU) goto exit;
609     }
610     if (passwd) {
611         passwdU = strWtoU( passwd );
612         if (!passwdU) goto exit;
613 
614         pwd.bv_len = strlen( passwdU );
615         pwd.bv_val = passwdU;
616     }
617 
618     ret = map_error( ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL ));
619 
620 exit:
621     strfreeU( dnU );
622     strfreeU( passwdU );
623 
624 #endif
625     return ret;
626 }
627 
628 /***********************************************************************
629  *      ldap_unbind     (WLDAP32.@)
630  *
631  * Close LDAP connection and free resources (asynchronous operation).
632  *
633  * PARAMS
634  *  ld  [I] Pointer to an LDAP context.
635  *
636  * RETURNS
637  *  Success: LDAP_SUCCESS
638  *  Failure: An LDAP error code.
639  */
640 ULONG CDECL WLDAP32_ldap_unbind( WLDAP32_LDAP *ld )
641 {
642     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
643 #ifdef HAVE_LDAP
644 
645     TRACE( "(%p)\n", ld );
646 
647     if (ld)
648         ret = map_error( ldap_unbind_ext( ld, NULL, NULL ));
649     else
650         ret = WLDAP32_LDAP_PARAM_ERROR;
651 
652 #endif
653     return ret;
654 }
655 
656 /***********************************************************************
657  *      ldap_unbind_s     (WLDAP32.@)
658  *
659  * Close LDAP connection and free resources (synchronous operation).
660  *
661  * PARAMS
662  *  ld  [I] Pointer to an LDAP context.
663  *
664  * RETURNS
665  *  Success: LDAP_SUCCESS
666  *  Failure: An LDAP error code.
667  */
668 ULONG CDECL WLDAP32_ldap_unbind_s( WLDAP32_LDAP *ld )
669 {
670     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
671 #ifdef HAVE_LDAP
672 
673     TRACE( "(%p)\n", ld );
674 
675     if (ld)
676         ret = map_error( ldap_unbind_ext_s( ld, NULL, NULL ));
677     else
678         ret = WLDAP32_LDAP_PARAM_ERROR;
679 
680 #endif
681     return ret;
682 }
683