msi/tests: Remove workarounds for Windows 2000.
[wine.git] / dlls / wldap32 / bind.c
blob0e9404ec504da4f110b27088c556f45da7a0f48f
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"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #ifdef HAVE_LDAP_H
26 #include <ldap.h>
27 #endif
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
33 #include "winldap_private.h"
34 #include "wldap32.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
39 /***********************************************************************
40 * ldap_bindA (WLDAP32.@)
42 * See ldap_bindW.
44 ULONG CDECL ldap_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
46 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
47 #ifdef HAVE_LDAP
48 WCHAR *dnW = NULL, *credW = NULL;
50 ret = WLDAP32_LDAP_NO_MEMORY;
52 TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_a(dn), cred, method );
54 if (!ld) return ~0u;
56 if (dn) {
57 dnW = strAtoW( dn );
58 if (!dnW) goto exit;
60 if (cred) {
61 credW = strAtoW( cred );
62 if (!credW) goto exit;
65 ret = ldap_bindW( ld, dnW, credW, method );
67 exit:
68 strfreeW( dnW );
69 strfreeW( credW );
71 #endif
72 return ret;
75 /***********************************************************************
76 * ldap_bindW (WLDAP32.@)
78 * Authenticate with an LDAP server (asynchronous operation).
80 * PARAMS
81 * ld [I] Pointer to an LDAP context.
82 * dn [I] DN of entry to bind as.
83 * cred [I] Credentials (e.g. password string).
84 * method [I] Authentication method.
86 * RETURNS
87 * Success: Message ID of the bind operation.
88 * Failure: An LDAP error code.
90 * NOTES
91 * Only LDAP_AUTH_SIMPLE is supported (just like native).
93 ULONG CDECL ldap_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
95 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
96 #ifdef HAVE_LDAP
97 char *dnU = NULL, *credU = NULL;
98 struct berval pwd = { 0, NULL };
99 int msg;
101 ret = WLDAP32_LDAP_NO_MEMORY;
103 TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_w(dn), cred, method );
105 if (!ld) return ~0u;
106 if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
108 if (dn) {
109 dnU = strWtoU( dn );
110 if (!dnU) goto exit;
112 if (cred) {
113 credU = strWtoU( cred );
114 if (!credU) goto exit;
116 pwd.bv_len = strlen( credU );
117 pwd.bv_val = credU;
120 ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
122 if (ret == LDAP_SUCCESS)
123 ret = msg;
124 else
125 ret = ~0u;
127 exit:
128 strfreeU( dnU );
129 strfreeU( credU );
131 #endif
132 return ret;
135 /***********************************************************************
136 * ldap_bind_sA (WLDAP32.@)
138 * See ldap_bind_sW.
140 ULONG CDECL ldap_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
142 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
143 #ifdef HAVE_LDAP
144 WCHAR *dnW = NULL, *credW = NULL;
146 ret = WLDAP32_LDAP_NO_MEMORY;
148 TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_a(dn), cred, method );
150 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
152 if (dn) {
153 dnW = strAtoW( dn );
154 if (!dnW) goto exit;
156 if (cred) {
157 credW = strAtoW( cred );
158 if (!credW) goto exit;
161 ret = ldap_bind_sW( ld, dnW, credW, method );
163 exit:
164 strfreeW( dnW );
165 strfreeW( credW );
167 #endif
168 return ret;
171 /***********************************************************************
172 * ldap_bind_sW (WLDAP32.@)
174 * Authenticate with an LDAP server (synchronous operation).
176 * PARAMS
177 * ld [I] Pointer to an LDAP context.
178 * dn [I] DN of entry to bind as.
179 * cred [I] Credentials (e.g. password string).
180 * method [I] Authentication method.
182 * RETURNS
183 * Success: LDAP_SUCCESS
184 * Failure: An LDAP error code.
186 ULONG CDECL ldap_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
188 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
189 #ifdef HAVE_LDAP
190 char *dnU = NULL, *credU = NULL;
191 struct berval pwd = { 0, NULL };
193 ret = WLDAP32_LDAP_NO_MEMORY;
195 TRACE( "(%p, %s, %p, 0x%08x)\n", ld, debugstr_w(dn), cred, method );
197 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
198 if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
200 if (dn) {
201 dnU = strWtoU( dn );
202 if (!dnU) goto exit;
204 if (cred) {
205 credU = strWtoU( cred );
206 if (!credU) goto exit;
208 pwd.bv_len = strlen( credU );
209 pwd.bv_val = credU;
212 ret = map_error( ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL ));
214 exit:
215 strfreeU( dnU );
216 strfreeU( credU );
218 #endif
219 return ret;
222 /***********************************************************************
223 * ldap_sasl_bindA (WLDAP32.@)
225 * See ldap_sasl_bindW.
227 ULONG CDECL ldap_sasl_bindA( WLDAP32_LDAP *ld, const PCHAR dn,
228 const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
229 PLDAPControlA *clientctrls, int *message )
231 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
232 #ifdef HAVE_LDAP
233 WCHAR *dnW, *mechanismW = NULL;
234 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
236 ret = WLDAP32_LDAP_NO_MEMORY;
238 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
239 debugstr_a(mechanism), cred, serverctrls, clientctrls, message );
241 if (!ld || !dn || !mechanism || !cred || !message)
242 return WLDAP32_LDAP_PARAM_ERROR;
244 dnW = strAtoW( dn );
245 if (!dnW) goto exit;
247 mechanismW = strAtoW( mechanism );
248 if (!mechanismW) goto exit;
250 if (serverctrls) {
251 serverctrlsW = controlarrayAtoW( serverctrls );
252 if (!serverctrlsW) goto exit;
254 if (clientctrls) {
255 clientctrlsW = controlarrayAtoW( clientctrls );
256 if (!clientctrlsW) goto exit;
259 ret = ldap_sasl_bindW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, message );
261 exit:
262 strfreeW( dnW );
263 strfreeW( mechanismW );
264 controlarrayfreeW( serverctrlsW );
265 controlarrayfreeW( clientctrlsW );
267 #endif
268 return ret;
271 /***********************************************************************
272 * ldap_sasl_bindW (WLDAP32.@)
274 * Authenticate with an LDAP server using SASL (asynchronous operation).
276 * PARAMS
277 * ld [I] Pointer to an LDAP context.
278 * dn [I] DN of entry to bind as.
279 * mechanism [I] Authentication method.
280 * cred [I] Credentials.
281 * serverctrls [I] Array of LDAP server controls.
282 * clientctrls [I] Array of LDAP client controls.
283 * message [O] Message ID of the bind operation.
285 * RETURNS
286 * Success: LDAP_SUCCESS
287 * Failure: An LDAP error code.
289 * NOTES
290 * The serverctrls and clientctrls parameters are optional and should
291 * be set to NULL if not used.
293 ULONG CDECL ldap_sasl_bindW( WLDAP32_LDAP *ld, const PWCHAR dn,
294 const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
295 PLDAPControlW *clientctrls, int *message )
297 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
298 #ifdef HAVE_LDAP
299 char *dnU, *mechanismU = NULL;
300 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
301 struct berval credU;
303 ret = WLDAP32_LDAP_NO_MEMORY;
305 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
306 debugstr_w(mechanism), cred, serverctrls, clientctrls, message );
308 if (!ld || !dn || !mechanism || !cred || !message)
309 return WLDAP32_LDAP_PARAM_ERROR;
311 dnU = strWtoU( dn );
312 if (!dnU) goto exit;
314 mechanismU = strWtoU( mechanism );
315 if (!mechanismU) goto exit;
317 if (serverctrls) {
318 serverctrlsU = controlarrayWtoU( serverctrls );
319 if (!serverctrlsU) goto exit;
321 if (clientctrls) {
322 clientctrlsU = controlarrayWtoU( clientctrls );
323 if (!clientctrlsU) goto exit;
326 credU.bv_len = cred->bv_len;
327 credU.bv_val = cred->bv_val;
329 ret = map_error( ldap_sasl_bind( ld, dnU, mechanismU, &credU,
330 serverctrlsU, clientctrlsU, message ));
332 exit:
333 strfreeU( dnU );
334 strfreeU( mechanismU );
335 controlarrayfreeU( serverctrlsU );
336 controlarrayfreeU( clientctrlsU );
338 #endif
339 return ret;
342 /***********************************************************************
343 * ldap_sasl_bind_sA (WLDAP32.@)
345 * See ldap_sasl_bind_sW.
347 ULONG CDECL ldap_sasl_bind_sA( WLDAP32_LDAP *ld, const PCHAR dn,
348 const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
349 PLDAPControlA *clientctrls, PBERVAL *serverdata )
351 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
352 #ifdef HAVE_LDAP
353 WCHAR *dnW, *mechanismW = NULL;
354 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
356 ret = WLDAP32_LDAP_NO_MEMORY;
358 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
359 debugstr_a(mechanism), cred, serverctrls, clientctrls, serverdata );
361 if (!ld || !dn || !mechanism || !cred || !serverdata)
362 return WLDAP32_LDAP_PARAM_ERROR;
364 dnW = strAtoW( dn );
365 if (!dnW) goto exit;
367 mechanismW = strAtoW( mechanism );
368 if (!mechanismW) goto exit;
370 if (serverctrls) {
371 serverctrlsW = controlarrayAtoW( serverctrls );
372 if (!serverctrlsW) goto exit;
374 if (clientctrls) {
375 clientctrlsW = controlarrayAtoW( clientctrls );
376 if (!clientctrlsW) goto exit;
379 ret = ldap_sasl_bind_sW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, serverdata );
381 exit:
382 strfreeW( dnW );
383 strfreeW( mechanismW );
384 controlarrayfreeW( serverctrlsW );
385 controlarrayfreeW( clientctrlsW );
387 #endif
388 return ret;
391 /***********************************************************************
392 * ldap_sasl_bind_sW (WLDAP32.@)
394 * Authenticate with an LDAP server using SASL (synchronous operation).
396 * PARAMS
397 * ld [I] Pointer to an LDAP context.
398 * dn [I] DN of entry to bind as.
399 * mechanism [I] Authentication method.
400 * cred [I] Credentials.
401 * serverctrls [I] Array of LDAP server controls.
402 * clientctrls [I] Array of LDAP client controls.
403 * serverdata [O] Authentication response from the server.
405 * RETURNS
406 * Success: LDAP_SUCCESS
407 * Failure: An LDAP error code.
409 * NOTES
410 * The serverctrls and clientctrls parameters are optional and should
411 * be set to NULL if not used.
413 ULONG CDECL ldap_sasl_bind_sW( WLDAP32_LDAP *ld, const PWCHAR dn,
414 const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
415 PLDAPControlW *clientctrls, PBERVAL *serverdata )
417 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
418 #ifdef HAVE_LDAP
419 char *dnU, *mechanismU = NULL;
420 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
421 struct berval credU;
423 ret = WLDAP32_LDAP_NO_MEMORY;
425 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
426 debugstr_w(mechanism), cred, serverctrls, clientctrls, serverdata );
428 if (!ld || !dn || !mechanism || !cred || !serverdata)
429 return WLDAP32_LDAP_PARAM_ERROR;
431 dnU = strWtoU( dn );
432 if (!dnU) goto exit;
434 mechanismU = strWtoU( mechanism );
435 if (!mechanismU) goto exit;
437 if (serverctrls) {
438 serverctrlsU = controlarrayWtoU( serverctrls );
439 if (!serverctrlsU) goto exit;
441 if (clientctrls) {
442 clientctrlsU = controlarrayWtoU( clientctrls );
443 if (!clientctrlsU) goto exit;
446 credU.bv_len = cred->bv_len;
447 credU.bv_val = cred->bv_val;
449 ret = map_error( ldap_sasl_bind_s( ld, dnU, mechanismU, &credU,
450 serverctrlsU, clientctrlsU, (struct berval **)serverdata ));
452 exit:
453 strfreeU( dnU );
454 strfreeU( mechanismU );
455 controlarrayfreeU( serverctrlsU );
456 controlarrayfreeU( clientctrlsU );
458 #endif
459 return ret;
462 /***********************************************************************
463 * ldap_simple_bindA (WLDAP32.@)
465 * See ldap_simple_bindW.
467 ULONG CDECL ldap_simple_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
469 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
470 #ifdef HAVE_LDAP
471 WCHAR *dnW = NULL, *passwdW = NULL;
473 ret = WLDAP32_LDAP_NO_MEMORY;
475 TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
477 if (!ld) return ~0u;
479 if (dn) {
480 dnW = strAtoW( dn );
481 if (!dnW) goto exit;
483 if (passwd) {
484 passwdW = strAtoW( passwd );
485 if (!passwdW) goto exit;
488 ret = ldap_simple_bindW( ld, dnW, passwdW );
490 exit:
491 strfreeW( dnW );
492 strfreeW( passwdW );
494 #endif
495 return ret;
498 /***********************************************************************
499 * ldap_simple_bindW (WLDAP32.@)
501 * Authenticate with an LDAP server (asynchronous operation).
503 * PARAMS
504 * ld [I] Pointer to an LDAP context.
505 * dn [I] DN of entry to bind as.
506 * passwd [I] Password string.
508 * RETURNS
509 * Success: Message ID of the bind operation.
510 * Failure: An LDAP error code.
512 * NOTES
513 * Set dn and passwd to NULL to bind as an anonymous user.
515 ULONG CDECL ldap_simple_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
517 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
518 #ifdef HAVE_LDAP
519 char *dnU = NULL, *passwdU = NULL;
520 struct berval pwd = { 0, NULL };
521 int msg;
523 ret = WLDAP32_LDAP_NO_MEMORY;
525 TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
527 if (!ld) return ~0u;
529 if (dn) {
530 dnU = strWtoU( dn );
531 if (!dnU) goto exit;
533 if (passwd) {
534 passwdU = strWtoU( passwd );
535 if (!passwdU) goto exit;
537 pwd.bv_len = strlen( passwdU );
538 pwd.bv_val = passwdU;
541 ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
543 if (ret == LDAP_SUCCESS)
544 ret = msg;
545 else
546 ret = ~0u;
548 exit:
549 strfreeU( dnU );
550 strfreeU( passwdU );
552 #endif
553 return ret;
556 /***********************************************************************
557 * ldap_simple_bind_sA (WLDAP32.@)
559 * See ldap_simple_bind_sW.
561 ULONG CDECL ldap_simple_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
563 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
564 #ifdef HAVE_LDAP
565 WCHAR *dnW = NULL, *passwdW = NULL;
567 ret = WLDAP32_LDAP_NO_MEMORY;
569 TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
571 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
573 if (dn) {
574 dnW = strAtoW( dn );
575 if (!dnW) goto exit;
577 if (passwd) {
578 passwdW = strAtoW( passwd );
579 if (!passwdW) goto exit;
582 ret = ldap_simple_bind_sW( ld, dnW, passwdW );
584 exit:
585 strfreeW( dnW );
586 strfreeW( passwdW );
588 #endif
589 return ret;
592 /***********************************************************************
593 * ldap_simple_bind_sW (WLDAP32.@)
595 * Authenticate with an LDAP server (synchronous operation).
597 * PARAMS
598 * ld [I] Pointer to an LDAP context.
599 * dn [I] DN of entry to bind as.
600 * passwd [I] Password string.
602 * RETURNS
603 * Success: LDAP_SUCCESS
604 * Failure: An LDAP error code.
606 * NOTES
607 * Set dn and passwd to NULL to bind as an anonymous user.
609 ULONG CDECL ldap_simple_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
611 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
612 #ifdef HAVE_LDAP
613 char *dnU = NULL, *passwdU = NULL;
614 struct berval pwd = { 0, NULL };
616 ret = WLDAP32_LDAP_NO_MEMORY;
618 TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
620 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
622 if (dn) {
623 dnU = strWtoU( dn );
624 if (!dnU) goto exit;
626 if (passwd) {
627 passwdU = strWtoU( passwd );
628 if (!passwdU) goto exit;
630 pwd.bv_len = strlen( passwdU );
631 pwd.bv_val = passwdU;
634 ret = map_error( ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL ));
636 exit:
637 strfreeU( dnU );
638 strfreeU( passwdU );
640 #endif
641 return ret;
644 /***********************************************************************
645 * ldap_unbind (WLDAP32.@)
647 * Close LDAP connection and free resources (asynchronous operation).
649 * PARAMS
650 * ld [I] Pointer to an LDAP context.
652 * RETURNS
653 * Success: LDAP_SUCCESS
654 * Failure: An LDAP error code.
656 ULONG CDECL WLDAP32_ldap_unbind( WLDAP32_LDAP *ld )
658 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
659 #ifdef HAVE_LDAP
661 TRACE( "(%p)\n", ld );
663 if (ld)
664 ret = map_error( ldap_unbind_ext( ld, NULL, NULL ));
665 else
666 ret = WLDAP32_LDAP_PARAM_ERROR;
668 #endif
669 return ret;
672 /***********************************************************************
673 * ldap_unbind_s (WLDAP32.@)
675 * Close LDAP connection and free resources (synchronous operation).
677 * PARAMS
678 * ld [I] Pointer to an LDAP context.
680 * RETURNS
681 * Success: LDAP_SUCCESS
682 * Failure: An LDAP error code.
684 ULONG CDECL WLDAP32_ldap_unbind_s( WLDAP32_LDAP *ld )
686 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
687 #ifdef HAVE_LDAP
689 TRACE( "(%p)\n", ld );
691 if (ld)
692 ret = map_error( ldap_unbind_ext_s( ld, NULL, NULL ));
693 else
694 ret = WLDAP32_LDAP_PARAM_ERROR;
696 #endif
697 return ret;