usp10: Update tests in test_ScriptItemIzeShapePlace to match Windows results.
[wine/multimedia.git] / dlls / wldap32 / bind.c
blob852d22944a9d3643b1f8bd6a9f3433dd025814e7
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 #else
35 #define LDAP_SUCCESS 0x00
36 #define LDAP_NOT_SUPPORTED 0x5c
37 #endif
39 #include "winldap_private.h"
40 #include "wldap32.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
44 /***********************************************************************
45 * ldap_bindA (WLDAP32.@)
47 * See ldap_bindW.
49 ULONG ldap_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
51 ULONG ret = LDAP_NOT_SUPPORTED;
52 #ifdef HAVE_LDAP
53 WCHAR *dnW = NULL, *credW = NULL;
55 ret = WLDAP32_LDAP_NO_MEMORY;
57 TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_a(dn), cred, method );
59 if (!ld) return ~0UL;
61 if (dn) {
62 dnW = strAtoW( dn );
63 if (!dnW) goto exit;
65 if (cred) {
66 credW = strAtoW( cred );
67 if (!credW) goto exit;
70 ret = ldap_bindW( ld, dnW, credW, method );
72 exit:
73 strfreeW( dnW );
74 strfreeW( credW );
76 #endif
77 return ret;
80 /***********************************************************************
81 * ldap_bindW (WLDAP32.@)
83 * Authenticate with an LDAP server (asynchronous operation).
85 * PARAMS
86 * ld [I] Pointer to an LDAP context.
87 * dn [I] DN of entry to bind as.
88 * cred [I] Credentials (e.g. password string).
89 * method [I] Authentication method.
91 * RETURNS
92 * Success: Message ID of the bind operation.
93 * Failure: An LDAP error code.
95 * NOTES
96 * Only LDAP_AUTH_SIMPLE is supported (just like native).
98 ULONG ldap_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
100 ULONG ret = LDAP_NOT_SUPPORTED;
101 #ifdef HAVE_LDAP
102 char *dnU = NULL, *credU = NULL;
103 struct berval pwd = { 0, NULL };
104 int msg;
106 ret = WLDAP32_LDAP_NO_MEMORY;
108 TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_w(dn), cred, method );
110 if (!ld) return ~0UL;
111 if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
113 if (dn) {
114 dnU = strWtoU( dn );
115 if (!dnU) goto exit;
117 if (cred) {
118 credU = strWtoU( cred );
119 if (!credU) goto exit;
121 pwd.bv_len = strlen( credU );
122 pwd.bv_val = credU;
125 ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
127 if (ret == LDAP_SUCCESS)
128 ret = msg;
129 else
130 ret = ~0UL;
132 exit:
133 strfreeU( dnU );
134 strfreeU( credU );
136 #endif
137 return ret;
140 /***********************************************************************
141 * ldap_bind_sA (WLDAP32.@)
143 * See ldap_bind_sW.
145 ULONG ldap_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
147 ULONG ret = LDAP_NOT_SUPPORTED;
148 #ifdef HAVE_LDAP
149 WCHAR *dnW = NULL, *credW = NULL;
151 ret = WLDAP32_LDAP_NO_MEMORY;
153 TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_a(dn), cred, method );
155 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
157 if (dn) {
158 dnW = strAtoW( dn );
159 if (!dnW) goto exit;
161 if (cred) {
162 credW = strAtoW( cred );
163 if (!credW) goto exit;
166 ret = ldap_bind_sW( ld, dnW, credW, method );
168 exit:
169 strfreeW( dnW );
170 strfreeW( credW );
172 #endif
173 return ret;
176 /***********************************************************************
177 * ldap_bind_sW (WLDAP32.@)
179 * Authenticate with an LDAP server (synchronous operation).
181 * PARAMS
182 * ld [I] Pointer to an LDAP context.
183 * dn [I] DN of entry to bind as.
184 * cred [I] Credentials (e.g. password string).
185 * method [I] Authentication method.
187 * RETURNS
188 * Success: LDAP_SUCCESS
189 * Failure: An LDAP error code.
191 ULONG ldap_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
193 ULONG ret = LDAP_NOT_SUPPORTED;
194 #ifdef HAVE_LDAP
195 char *dnU = NULL, *credU = NULL;
196 struct berval pwd = { 0, NULL };
198 ret = WLDAP32_LDAP_NO_MEMORY;
200 TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_w(dn), cred, method );
202 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
203 if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
205 if (dn) {
206 dnU = strWtoU( dn );
207 if (!dnU) goto exit;
209 if (cred) {
210 credU = strWtoU( cred );
211 if (!credU) goto exit;
213 pwd.bv_len = strlen( credU );
214 pwd.bv_val = credU;
217 ret = ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL );
219 exit:
220 strfreeU( dnU );
221 strfreeU( credU );
223 #endif
224 return ret;
227 /***********************************************************************
228 * ldap_sasl_bindA (WLDAP32.@)
230 * See ldap_sasl_bindW.
232 ULONG ldap_sasl_bindA( WLDAP32_LDAP *ld, const PCHAR dn,
233 const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
234 PLDAPControlA *clientctrls, int *message )
236 ULONG ret = LDAP_NOT_SUPPORTED;
237 #ifdef HAVE_LDAP
238 WCHAR *dnW, *mechanismW = NULL;
239 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
241 ret = WLDAP32_LDAP_NO_MEMORY;
243 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
244 debugstr_a(mechanism), cred, serverctrls, clientctrls, message );
246 if (!ld || !dn || !mechanism || !cred || !message)
247 return WLDAP32_LDAP_PARAM_ERROR;
249 dnW = strAtoW( dn );
250 if (!dnW) goto exit;
252 mechanismW = strAtoW( mechanism );
253 if (!mechanismW) goto exit;
255 if (serverctrls) {
256 serverctrlsW = controlarrayAtoW( serverctrls );
257 if (!serverctrlsW) goto exit;
259 if (clientctrls) {
260 clientctrlsW = controlarrayAtoW( clientctrls );
261 if (!clientctrlsW) goto exit;
264 ret = ldap_sasl_bindW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, message );
266 exit:
267 strfreeW( dnW );
268 strfreeW( mechanismW );
269 controlarrayfreeW( serverctrlsW );
270 controlarrayfreeW( clientctrlsW );
272 #endif
273 return ret;
276 /***********************************************************************
277 * ldap_sasl_bindW (WLDAP32.@)
279 * Authenticate with an LDAP server using SASL (asynchronous operation).
281 * PARAMS
282 * ld [I] Pointer to an LDAP context.
283 * dn [I] DN of entry to bind as.
284 * mechanism [I] Authentication method.
285 * cred [I] Credentials.
286 * serverctrls [I] Array of LDAP server controls.
287 * clientctrls [I] Array of LDAP client controls.
288 * message [O] Message ID of the bind operation.
290 * RETURNS
291 * Success: LDAP_SUCCESS
292 * Failure: An LDAP error code.
294 * NOTES
295 * The serverctrls and clientctrls parameters are optional and should
296 * be set to NULL if not used.
298 ULONG ldap_sasl_bindW( WLDAP32_LDAP *ld, const PWCHAR dn,
299 const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
300 PLDAPControlW *clientctrls, int *message )
302 ULONG ret = LDAP_NOT_SUPPORTED;
303 #ifdef HAVE_LDAP
304 char *dnU, *mechanismU = NULL;
305 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
307 ret = WLDAP32_LDAP_NO_MEMORY;
309 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
310 debugstr_w(mechanism), cred, serverctrls, clientctrls, message );
312 if (!ld || !dn || !mechanism || !cred || !message)
313 return WLDAP32_LDAP_PARAM_ERROR;
315 dnU = strWtoU( dn );
316 if (!dnU) goto exit;
318 mechanismU = strWtoU( mechanism );
319 if (!mechanismU) goto exit;
321 if (serverctrls) {
322 serverctrlsU = controlarrayWtoU( serverctrls );
323 if (!serverctrlsU) goto exit;
325 if (clientctrls) {
326 clientctrlsU = controlarrayWtoU( clientctrls );
327 if (!clientctrlsU) goto exit;
330 ret = ldap_sasl_bind( ld, dnU, mechanismU, (struct berval *)cred,
331 serverctrlsU, clientctrlsU, message );
333 exit:
334 strfreeU( dnU );
335 strfreeU( mechanismU );
336 controlarrayfreeU( serverctrlsU );
337 controlarrayfreeU( clientctrlsU );
339 #endif
340 return ret;
343 /***********************************************************************
344 * ldap_sasl_bind_sA (WLDAP32.@)
346 * See ldap_sasl_bind_sW.
348 ULONG ldap_sasl_bind_sA( WLDAP32_LDAP *ld, const PCHAR dn,
349 const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
350 PLDAPControlA *clientctrls, PBERVAL *serverdata )
352 ULONG ret = LDAP_NOT_SUPPORTED;
353 #ifdef HAVE_LDAP
354 WCHAR *dnW, *mechanismW = NULL;
355 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
357 ret = WLDAP32_LDAP_NO_MEMORY;
359 TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
360 debugstr_a(mechanism), cred, serverctrls, clientctrls, serverdata );
362 if (!ld || !dn || !mechanism || !cred || !serverdata)
363 return WLDAP32_LDAP_PARAM_ERROR;
365 dnW = strAtoW( dn );
366 if (!dnW) goto exit;
368 mechanismW = strAtoW( mechanism );
369 if (!mechanismW) goto exit;
371 if (serverctrls) {
372 serverctrlsW = controlarrayAtoW( serverctrls );
373 if (!serverctrlsW) goto exit;
375 if (clientctrls) {
376 clientctrlsW = controlarrayAtoW( clientctrls );
377 if (!clientctrlsW) goto exit;
380 ret = ldap_sasl_bind_sW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, serverdata );
382 exit:
383 strfreeW( dnW );
384 strfreeW( mechanismW );
385 controlarrayfreeW( serverctrlsW );
386 controlarrayfreeW( clientctrlsW );
388 #endif
389 return ret;
392 /***********************************************************************
393 * ldap_sasl_bind_sW (WLDAP32.@)
395 * Authenticate with an LDAP server using SASL (synchronous operation).
397 * PARAMS
398 * ld [I] Pointer to an LDAP context.
399 * dn [I] DN of entry to bind as.
400 * mechanism [I] Authentication method.
401 * cred [I] Credentials.
402 * serverctrls [I] Array of LDAP server controls.
403 * clientctrls [I] Array of LDAP client controls.
404 * serverdata [O] Authentication response from the server.
406 * RETURNS
407 * Success: LDAP_SUCCESS
408 * Failure: An LDAP error code.
410 * NOTES
411 * The serverctrls and clientctrls parameters are optional and should
412 * be set to NULL if not used.
414 ULONG ldap_sasl_bind_sW( WLDAP32_LDAP *ld, const PWCHAR dn,
415 const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
416 PLDAPControlW *clientctrls, PBERVAL *serverdata )
418 ULONG ret = LDAP_NOT_SUPPORTED;
419 #ifdef HAVE_LDAP
420 char *dnU, *mechanismU = NULL;
421 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
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 ret = ldap_sasl_bind_s( ld, dnU, mechanismU, (struct berval *)cred,
447 serverctrlsU, clientctrlsU, (struct berval **)serverdata );
449 exit:
450 strfreeU( dnU );
451 strfreeU( mechanismU );
452 controlarrayfreeU( serverctrlsU );
453 controlarrayfreeU( clientctrlsU );
455 #endif
456 return ret;
459 /***********************************************************************
460 * ldap_simple_bindA (WLDAP32.@)
462 * See ldap_simple_bindW.
464 ULONG ldap_simple_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
466 ULONG ret = LDAP_NOT_SUPPORTED;
467 #ifdef HAVE_LDAP
468 WCHAR *dnW = NULL, *passwdW = NULL;
470 ret = WLDAP32_LDAP_NO_MEMORY;
472 TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
474 if (!ld) return ~0UL;
476 if (dn) {
477 dnW = strAtoW( dn );
478 if (!dnW) goto exit;
480 if (passwd) {
481 passwdW = strAtoW( passwd );
482 if (!passwdW) goto exit;
485 ret = ldap_simple_bindW( ld, dnW, passwdW );
487 exit:
488 strfreeW( dnW );
489 strfreeW( passwdW );
491 #endif
492 return ret;
495 /***********************************************************************
496 * ldap_simple_bindW (WLDAP32.@)
498 * Authenticate with an LDAP server (asynchronous operation).
500 * PARAMS
501 * ld [I] Pointer to an LDAP context.
502 * dn [I] DN of entry to bind as.
503 * passwd [I] Password string.
505 * RETURNS
506 * Success: Message ID of the bind operation.
507 * Failure: An LDAP error code.
509 * NOTES
510 * Set dn and passwd to NULL to bind as an anonymous user.
512 ULONG ldap_simple_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
514 ULONG ret = LDAP_NOT_SUPPORTED;
515 #ifdef HAVE_LDAP
516 char *dnU = NULL, *passwdU = NULL;
517 struct berval pwd = { 0, NULL };
518 int msg;
520 ret = WLDAP32_LDAP_NO_MEMORY;
522 TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
524 if (!ld) return ~0UL;
526 if (dn) {
527 dnU = strWtoU( dn );
528 if (!dnU) goto exit;
530 if (passwd) {
531 passwdU = strWtoU( passwd );
532 if (!passwdU) goto exit;
534 pwd.bv_len = strlen( passwdU );
535 pwd.bv_val = passwdU;
538 ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
540 if (ret == LDAP_SUCCESS)
541 ret = msg;
542 else
543 ret = ~0UL;
545 exit:
546 strfreeU( dnU );
547 strfreeU( passwdU );
549 #endif
550 return ret;
553 /***********************************************************************
554 * ldap_simple_bind_sA (WLDAP32.@)
556 * See ldap_simple_bind_sW.
558 ULONG ldap_simple_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
560 ULONG ret = LDAP_NOT_SUPPORTED;
561 #ifdef HAVE_LDAP
562 WCHAR *dnW = NULL, *passwdW = NULL;
564 ret = WLDAP32_LDAP_NO_MEMORY;
566 TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
568 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
570 if (dn) {
571 dnW = strAtoW( dn );
572 if (!dnW) goto exit;
574 if (passwd) {
575 passwdW = strAtoW( passwd );
576 if (!passwdW) goto exit;
579 ret = ldap_simple_bind_sW( ld, dnW, passwdW );
581 exit:
582 strfreeW( dnW );
583 strfreeW( passwdW );
585 #endif
586 return ret;
589 /***********************************************************************
590 * ldap_simple_bind_sW (WLDAP32.@)
592 * Authenticate with an LDAP server (synchronous operation).
594 * PARAMS
595 * ld [I] Pointer to an LDAP context.
596 * dn [I] DN of entry to bind as.
597 * passwd [I] Password string.
599 * RETURNS
600 * Success: LDAP_SUCCESS
601 * Failure: An LDAP error code.
603 * NOTES
604 * Set dn and passwd to NULL to bind as an anonymous user.
606 ULONG ldap_simple_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
608 ULONG ret = LDAP_NOT_SUPPORTED;
609 #ifdef HAVE_LDAP
610 char *dnU = NULL, *passwdU = NULL;
611 struct berval pwd = { 0, NULL };
613 ret = WLDAP32_LDAP_NO_MEMORY;
615 TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
617 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
619 if (dn) {
620 dnU = strWtoU( dn );
621 if (!dnU) goto exit;
623 if (passwd) {
624 passwdU = strWtoU( passwd );
625 if (!passwdU) goto exit;
627 pwd.bv_len = strlen( passwdU );
628 pwd.bv_val = passwdU;
631 ret = ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL );
633 exit:
634 strfreeU( dnU );
635 strfreeU( passwdU );
637 #endif
638 return ret;
641 /***********************************************************************
642 * ldap_unbind (WLDAP32.@)
644 * Close LDAP connection and free resources (asynchronous operation).
646 * PARAMS
647 * ld [I] Pointer to an LDAP context.
649 * RETURNS
650 * Success: LDAP_SUCCESS
651 * Failure: An LDAP error code.
653 ULONG WLDAP32_ldap_unbind( WLDAP32_LDAP *ld )
655 ULONG ret = LDAP_NOT_SUPPORTED;
656 #ifdef HAVE_LDAP
658 TRACE( "(%p)\n", ld );
660 if (ld)
661 ret = ldap_unbind_ext( ld, NULL, NULL );
662 else
663 ret = WLDAP32_LDAP_PARAM_ERROR;
665 #endif
666 return ret;
669 /***********************************************************************
670 * ldap_unbind_s (WLDAP32.@)
672 * Close LDAP connection and free resources (synchronous operation).
674 * PARAMS
675 * ld [I] Pointer to an LDAP context.
677 * RETURNS
678 * Success: LDAP_SUCCESS
679 * Failure: An LDAP error code.
681 ULONG WLDAP32_ldap_unbind_s( WLDAP32_LDAP *ld )
683 ULONG ret = LDAP_NOT_SUPPORTED;
684 #ifdef HAVE_LDAP
686 TRACE( "(%p)\n", ld );
688 if (ld)
689 ret = ldap_unbind_ext_s( ld, NULL, NULL );
690 else
691 ret = WLDAP32_LDAP_PARAM_ERROR;
693 #endif
694 return ret;