advpack: Fix buffer sizes for possibly quoted strings.
[wine.git] / dlls / wldap32 / bind.c
blob790aafca3fb7720442160af2381a820be75afaf1
1 /*
2 * WLDAP32 - LDAP support for Wine
4 * Copyright 2005 Hans Leidekker
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.
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.
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
21 #include "config.h"
23 #include "wine/port.h"
24 #include "wine/debug.h"
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
32 #ifdef HAVE_LDAP_H
33 #include <ldap.h>
34 #endif
36 #include "winldap_private.h"
37 #include "wldap32.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
41 /***********************************************************************
42 * ldap_bindA (WLDAP32.@)
44 * See ldap_bindW.
46 ULONG CDECL ldap_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
48 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
49 #ifdef HAVE_LDAP
50 WCHAR *dnW = NULL, *credW = NULL;
52 ret = WLDAP32_LDAP_NO_MEMORY;
54 TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_a(dn), cred, method );
56 if (!ld) return ~0UL;
58 if (dn) {
59 dnW = strAtoW( dn );
60 if (!dnW) goto exit;
62 if (cred) {
63 credW = strAtoW( cred );
64 if (!credW) goto exit;
67 ret = ldap_bindW( ld, dnW, credW, method );
69 exit:
70 strfreeW( dnW );
71 strfreeW( credW );
73 #endif
74 return ret;
77 /***********************************************************************
78 * ldap_bindW (WLDAP32.@)
80 * Authenticate with an LDAP server (asynchronous operation).
82 * PARAMS
83 * ld [I] Pointer to an LDAP context.
84 * dn [I] DN of entry to bind as.
85 * cred [I] Credentials (e.g. password string).
86 * method [I] Authentication method.
88 * RETURNS
89 * Success: Message ID of the bind operation.
90 * Failure: An LDAP error code.
92 * NOTES
93 * Only LDAP_AUTH_SIMPLE is supported (just like native).
95 ULONG CDECL ldap_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
97 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
98 #ifdef HAVE_LDAP
99 char *dnU = NULL, *credU = NULL;
100 struct berval pwd = { 0, NULL };
101 int msg;
103 ret = WLDAP32_LDAP_NO_MEMORY;
105 TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_w(dn), cred, method );
107 if (!ld) return ~0UL;
108 if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
110 if (dn) {
111 dnU = strWtoU( dn );
112 if (!dnU) goto exit;
114 if (cred) {
115 credU = strWtoU( cred );
116 if (!credU) goto exit;
118 pwd.bv_len = strlen( credU );
119 pwd.bv_val = credU;
122 ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
124 if (ret == LDAP_SUCCESS)
125 ret = msg;
126 else
127 ret = ~0UL;
129 exit:
130 strfreeU( dnU );
131 strfreeU( credU );
133 #endif
134 return ret;
137 /***********************************************************************
138 * ldap_bind_sA (WLDAP32.@)
140 * See ldap_bind_sW.
142 ULONG CDECL ldap_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
144 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
145 #ifdef HAVE_LDAP
146 WCHAR *dnW = NULL, *credW = NULL;
148 ret = WLDAP32_LDAP_NO_MEMORY;
150 TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_a(dn), cred, method );
152 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
154 if (dn) {
155 dnW = strAtoW( dn );
156 if (!dnW) goto exit;
158 if (cred) {
159 credW = strAtoW( cred );
160 if (!credW) goto exit;
163 ret = ldap_bind_sW( ld, dnW, credW, method );
165 exit:
166 strfreeW( dnW );
167 strfreeW( credW );
169 #endif
170 return ret;
173 /***********************************************************************
174 * ldap_bind_sW (WLDAP32.@)
176 * Authenticate with an LDAP server (synchronous operation).
178 * PARAMS
179 * ld [I] Pointer to an LDAP context.
180 * dn [I] DN of entry to bind as.
181 * cred [I] Credentials (e.g. password string).
182 * method [I] Authentication method.
184 * RETURNS
185 * Success: LDAP_SUCCESS
186 * Failure: An LDAP error code.
188 ULONG CDECL ldap_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
190 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
191 #ifdef HAVE_LDAP
192 char *dnU = NULL, *credU = NULL;
193 struct berval pwd = { 0, NULL };
195 ret = WLDAP32_LDAP_NO_MEMORY;
197 TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_w(dn), cred, method );
199 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
200 if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
202 if (dn) {
203 dnU = strWtoU( dn );
204 if (!dnU) goto exit;
206 if (cred) {
207 credU = strWtoU( cred );
208 if (!credU) goto exit;
210 pwd.bv_len = strlen( credU );
211 pwd.bv_val = credU;
214 ret = ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL );
216 exit:
217 strfreeU( dnU );
218 strfreeU( credU );
220 #endif
221 return ret;
224 /***********************************************************************
225 * ldap_sasl_bindA (WLDAP32.@)
227 * See ldap_sasl_bindW.
229 ULONG CDECL ldap_sasl_bindA( WLDAP32_LDAP *ld, const PCHAR dn,
230 const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
231 PLDAPControlA *clientctrls, int *message )
233 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
234 #ifdef HAVE_LDAP
235 WCHAR *dnW, *mechanismW = NULL;
236 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
238 ret = WLDAP32_LDAP_NO_MEMORY;
240 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
241 debugstr_a(mechanism), cred, serverctrls, clientctrls, message );
243 if (!ld || !dn || !mechanism || !cred || !message)
244 return WLDAP32_LDAP_PARAM_ERROR;
246 dnW = strAtoW( dn );
247 if (!dnW) goto exit;
249 mechanismW = strAtoW( mechanism );
250 if (!mechanismW) goto exit;
252 if (serverctrls) {
253 serverctrlsW = controlarrayAtoW( serverctrls );
254 if (!serverctrlsW) goto exit;
256 if (clientctrls) {
257 clientctrlsW = controlarrayAtoW( clientctrls );
258 if (!clientctrlsW) goto exit;
261 ret = ldap_sasl_bindW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, message );
263 exit:
264 strfreeW( dnW );
265 strfreeW( mechanismW );
266 controlarrayfreeW( serverctrlsW );
267 controlarrayfreeW( clientctrlsW );
269 #endif
270 return ret;
273 /***********************************************************************
274 * ldap_sasl_bindW (WLDAP32.@)
276 * Authenticate with an LDAP server using SASL (asynchronous operation).
278 * PARAMS
279 * ld [I] Pointer to an LDAP context.
280 * dn [I] DN of entry to bind as.
281 * mechanism [I] Authentication method.
282 * cred [I] Credentials.
283 * serverctrls [I] Array of LDAP server controls.
284 * clientctrls [I] Array of LDAP client controls.
285 * message [O] Message ID of the bind operation.
287 * RETURNS
288 * Success: LDAP_SUCCESS
289 * Failure: An LDAP error code.
291 * NOTES
292 * The serverctrls and clientctrls parameters are optional and should
293 * be set to NULL if not used.
295 ULONG CDECL ldap_sasl_bindW( WLDAP32_LDAP *ld, const PWCHAR dn,
296 const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
297 PLDAPControlW *clientctrls, int *message )
299 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
300 #ifdef HAVE_LDAP
301 char *dnU, *mechanismU = NULL;
302 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
303 struct berval credU;
305 ret = WLDAP32_LDAP_NO_MEMORY;
307 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
308 debugstr_w(mechanism), cred, serverctrls, clientctrls, message );
310 if (!ld || !dn || !mechanism || !cred || !message)
311 return WLDAP32_LDAP_PARAM_ERROR;
313 dnU = strWtoU( dn );
314 if (!dnU) goto exit;
316 mechanismU = strWtoU( mechanism );
317 if (!mechanismU) goto exit;
319 if (serverctrls) {
320 serverctrlsU = controlarrayWtoU( serverctrls );
321 if (!serverctrlsU) goto exit;
323 if (clientctrls) {
324 clientctrlsU = controlarrayWtoU( clientctrls );
325 if (!clientctrlsU) goto exit;
328 credU.bv_len = cred->bv_len;
329 credU.bv_val = cred->bv_val;
331 ret = ldap_sasl_bind( ld, dnU, mechanismU, &credU,
332 serverctrlsU, clientctrlsU, message );
334 exit:
335 strfreeU( dnU );
336 strfreeU( mechanismU );
337 controlarrayfreeU( serverctrlsU );
338 controlarrayfreeU( clientctrlsU );
340 #endif
341 return ret;
344 /***********************************************************************
345 * ldap_sasl_bind_sA (WLDAP32.@)
347 * See ldap_sasl_bind_sW.
349 ULONG CDECL ldap_sasl_bind_sA( WLDAP32_LDAP *ld, const PCHAR dn,
350 const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
351 PLDAPControlA *clientctrls, PBERVAL *serverdata )
353 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
354 #ifdef HAVE_LDAP
355 WCHAR *dnW, *mechanismW = NULL;
356 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
358 ret = WLDAP32_LDAP_NO_MEMORY;
360 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
361 debugstr_a(mechanism), cred, serverctrls, clientctrls, serverdata );
363 if (!ld || !dn || !mechanism || !cred || !serverdata)
364 return WLDAP32_LDAP_PARAM_ERROR;
366 dnW = strAtoW( dn );
367 if (!dnW) goto exit;
369 mechanismW = strAtoW( mechanism );
370 if (!mechanismW) goto exit;
372 if (serverctrls) {
373 serverctrlsW = controlarrayAtoW( serverctrls );
374 if (!serverctrlsW) goto exit;
376 if (clientctrls) {
377 clientctrlsW = controlarrayAtoW( clientctrls );
378 if (!clientctrlsW) goto exit;
381 ret = ldap_sasl_bind_sW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, serverdata );
383 exit:
384 strfreeW( dnW );
385 strfreeW( mechanismW );
386 controlarrayfreeW( serverctrlsW );
387 controlarrayfreeW( clientctrlsW );
389 #endif
390 return ret;
393 /***********************************************************************
394 * ldap_sasl_bind_sW (WLDAP32.@)
396 * Authenticate with an LDAP server using SASL (synchronous operation).
398 * PARAMS
399 * ld [I] Pointer to an LDAP context.
400 * dn [I] DN of entry to bind as.
401 * mechanism [I] Authentication method.
402 * cred [I] Credentials.
403 * serverctrls [I] Array of LDAP server controls.
404 * clientctrls [I] Array of LDAP client controls.
405 * serverdata [O] Authentication response from the server.
407 * RETURNS
408 * Success: LDAP_SUCCESS
409 * Failure: An LDAP error code.
411 * NOTES
412 * The serverctrls and clientctrls parameters are optional and should
413 * be set to NULL if not used.
415 ULONG CDECL ldap_sasl_bind_sW( WLDAP32_LDAP *ld, const PWCHAR dn,
416 const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
417 PLDAPControlW *clientctrls, PBERVAL *serverdata )
419 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
420 #ifdef HAVE_LDAP
421 char *dnU, *mechanismU = NULL;
422 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
423 struct berval credU;
425 ret = WLDAP32_LDAP_NO_MEMORY;
427 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
428 debugstr_w(mechanism), cred, serverctrls, clientctrls, serverdata );
430 if (!ld || !dn || !mechanism || !cred || !serverdata)
431 return WLDAP32_LDAP_PARAM_ERROR;
433 dnU = strWtoU( dn );
434 if (!dnU) goto exit;
436 mechanismU = strWtoU( mechanism );
437 if (!mechanismU) goto exit;
439 if (serverctrls) {
440 serverctrlsU = controlarrayWtoU( serverctrls );
441 if (!serverctrlsU) goto exit;
443 if (clientctrls) {
444 clientctrlsU = controlarrayWtoU( clientctrls );
445 if (!clientctrlsU) goto exit;
448 credU.bv_len = cred->bv_len;
449 credU.bv_val = cred->bv_val;
451 ret = ldap_sasl_bind_s( ld, dnU, mechanismU, &credU,
452 serverctrlsU, clientctrlsU, (struct berval **)serverdata );
454 exit:
455 strfreeU( dnU );
456 strfreeU( mechanismU );
457 controlarrayfreeU( serverctrlsU );
458 controlarrayfreeU( clientctrlsU );
460 #endif
461 return ret;
464 /***********************************************************************
465 * ldap_simple_bindA (WLDAP32.@)
467 * See ldap_simple_bindW.
469 ULONG CDECL ldap_simple_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
471 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
472 #ifdef HAVE_LDAP
473 WCHAR *dnW = NULL, *passwdW = NULL;
475 ret = WLDAP32_LDAP_NO_MEMORY;
477 TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
479 if (!ld) return ~0UL;
481 if (dn) {
482 dnW = strAtoW( dn );
483 if (!dnW) goto exit;
485 if (passwd) {
486 passwdW = strAtoW( passwd );
487 if (!passwdW) goto exit;
490 ret = ldap_simple_bindW( ld, dnW, passwdW );
492 exit:
493 strfreeW( dnW );
494 strfreeW( passwdW );
496 #endif
497 return ret;
500 /***********************************************************************
501 * ldap_simple_bindW (WLDAP32.@)
503 * Authenticate with an LDAP server (asynchronous operation).
505 * PARAMS
506 * ld [I] Pointer to an LDAP context.
507 * dn [I] DN of entry to bind as.
508 * passwd [I] Password string.
510 * RETURNS
511 * Success: Message ID of the bind operation.
512 * Failure: An LDAP error code.
514 * NOTES
515 * Set dn and passwd to NULL to bind as an anonymous user.
517 ULONG CDECL ldap_simple_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
519 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
520 #ifdef HAVE_LDAP
521 char *dnU = NULL, *passwdU = NULL;
522 struct berval pwd = { 0, NULL };
523 int msg;
525 ret = WLDAP32_LDAP_NO_MEMORY;
527 TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
529 if (!ld) return ~0UL;
531 if (dn) {
532 dnU = strWtoU( dn );
533 if (!dnU) goto exit;
535 if (passwd) {
536 passwdU = strWtoU( passwd );
537 if (!passwdU) goto exit;
539 pwd.bv_len = strlen( passwdU );
540 pwd.bv_val = passwdU;
543 ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
545 if (ret == LDAP_SUCCESS)
546 ret = msg;
547 else
548 ret = ~0UL;
550 exit:
551 strfreeU( dnU );
552 strfreeU( passwdU );
554 #endif
555 return ret;
558 /***********************************************************************
559 * ldap_simple_bind_sA (WLDAP32.@)
561 * See ldap_simple_bind_sW.
563 ULONG CDECL ldap_simple_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
565 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
566 #ifdef HAVE_LDAP
567 WCHAR *dnW = NULL, *passwdW = NULL;
569 ret = WLDAP32_LDAP_NO_MEMORY;
571 TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
573 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
575 if (dn) {
576 dnW = strAtoW( dn );
577 if (!dnW) goto exit;
579 if (passwd) {
580 passwdW = strAtoW( passwd );
581 if (!passwdW) goto exit;
584 ret = ldap_simple_bind_sW( ld, dnW, passwdW );
586 exit:
587 strfreeW( dnW );
588 strfreeW( passwdW );
590 #endif
591 return ret;
594 /***********************************************************************
595 * ldap_simple_bind_sW (WLDAP32.@)
597 * Authenticate with an LDAP server (synchronous operation).
599 * PARAMS
600 * ld [I] Pointer to an LDAP context.
601 * dn [I] DN of entry to bind as.
602 * passwd [I] Password string.
604 * RETURNS
605 * Success: LDAP_SUCCESS
606 * Failure: An LDAP error code.
608 * NOTES
609 * Set dn and passwd to NULL to bind as an anonymous user.
611 ULONG CDECL ldap_simple_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
613 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
614 #ifdef HAVE_LDAP
615 char *dnU = NULL, *passwdU = NULL;
616 struct berval pwd = { 0, NULL };
618 ret = WLDAP32_LDAP_NO_MEMORY;
620 TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
622 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
624 if (dn) {
625 dnU = strWtoU( dn );
626 if (!dnU) goto exit;
628 if (passwd) {
629 passwdU = strWtoU( passwd );
630 if (!passwdU) goto exit;
632 pwd.bv_len = strlen( passwdU );
633 pwd.bv_val = passwdU;
636 ret = ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL );
638 exit:
639 strfreeU( dnU );
640 strfreeU( passwdU );
642 #endif
643 return ret;
646 /***********************************************************************
647 * ldap_unbind (WLDAP32.@)
649 * Close LDAP connection and free resources (asynchronous operation).
651 * PARAMS
652 * ld [I] Pointer to an LDAP context.
654 * RETURNS
655 * Success: LDAP_SUCCESS
656 * Failure: An LDAP error code.
658 ULONG CDECL WLDAP32_ldap_unbind( WLDAP32_LDAP *ld )
660 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
661 #ifdef HAVE_LDAP
663 TRACE( "(%p)\n", ld );
665 if (ld)
666 ret = ldap_unbind_ext( ld, NULL, NULL );
667 else
668 ret = WLDAP32_LDAP_PARAM_ERROR;
670 #endif
671 return ret;
674 /***********************************************************************
675 * ldap_unbind_s (WLDAP32.@)
677 * Close LDAP connection and free resources (synchronous operation).
679 * PARAMS
680 * ld [I] Pointer to an LDAP context.
682 * RETURNS
683 * Success: LDAP_SUCCESS
684 * Failure: An LDAP error code.
686 ULONG CDECL WLDAP32_ldap_unbind_s( WLDAP32_LDAP *ld )
688 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
689 #ifdef HAVE_LDAP
691 TRACE( "(%p)\n", ld );
693 if (ld)
694 ret = ldap_unbind_ext_s( ld, NULL, NULL );
695 else
696 ret = WLDAP32_LDAP_PARAM_ERROR;
698 #endif
699 return ret;