Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / wldap32 / init.c
blobdb9b45172389956b1177b3e8928ff0bb8f067a67
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 <stdio.h>
27 #include <stdarg.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
33 #ifdef HAVE_LDAP_H
34 #include <ldap.h>
35 #endif
37 #include "winldap_private.h"
38 #include "wldap32.h"
40 #ifdef HAVE_LDAP
41 /* Should eventually be determined by the algorithm documented on MSDN. */
42 static const WCHAR defaulthost[] = { 'l','o','c','a','l','h','o','s','t',0 };
44 /* Split a space separated string of hostnames into a string array */
45 static char **split_hostnames( const char *hostnames )
47 char **res, *str, *p, *q;
48 unsigned int i = 0;
50 str = strdupU( hostnames );
51 if (!str) return NULL;
53 p = str;
54 while (isspace( *p )) p++;
55 if (*p) i++;
57 while (*p)
59 if (isspace( *p ))
61 while (isspace( *p )) p++;
62 if (*p) i++;
64 p++;
67 res = HeapAlloc( GetProcessHeap(), 0, (i + 1) * sizeof(char *) );
68 if (!res)
70 HeapFree( GetProcessHeap(), 0, str );
71 return NULL;
74 p = str;
75 while (isspace( *p )) p++;
77 q = p;
78 i = 0;
80 while (*p)
82 if (p[1] != '\0')
84 if (isspace( *p ))
86 *p = '\0'; p++;
87 res[i] = strdupU( q );
88 if (!res[i]) goto oom;
89 i++;
91 while (isspace( *p )) p++;
92 q = p;
95 else
97 res[i] = strdupU( q );
98 if (!res[i]) goto oom;
99 i++;
101 p++;
103 res[i] = NULL;
105 HeapFree( GetProcessHeap(), 0, str );
106 return res;
108 oom:
109 while (i > 0) strfreeU( res[--i] );
111 HeapFree( GetProcessHeap(), 0, res );
112 HeapFree( GetProcessHeap(), 0, str );
114 return NULL;
117 /* Determine if a URL starts with a known LDAP scheme */
118 static int has_ldap_scheme( char *url )
120 if (!strncasecmp( url, "ldap://", 7 ) ||
121 !strncasecmp( url, "ldaps://", 8 ) ||
122 !strncasecmp( url, "ldapi://", 8 ) ||
123 !strncasecmp( url, "cldap://", 8 )) return 1;
124 return 0;
127 /* Flatten an array of hostnames into a space separated string of URLs.
128 * Prepend a given scheme and append a given portnumber to each hostname
129 * if necessary.
131 static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnumber )
133 char *res, *p, *q, **v;
134 unsigned int i = 0, size = 0;
135 static const char sep[] = " ", fmt[] = ":%d";
136 char port[7];
138 sprintf( port, fmt, portnumber );
140 for (v = hostnames; *v; v++)
142 if (!has_ldap_scheme( *v ))
144 size += strlen( scheme );
145 q = *v;
147 else
148 /* skip past colon in scheme prefix */
149 q = strchr( *v, '/' );
151 size += strlen( *v );
153 if (!strchr( q, ':' ))
154 size += strlen( port );
156 i++;
159 size += (i - 1) * strlen( sep );
161 res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
162 if (!res) return NULL;
164 p = res;
165 for (v = hostnames; *v; v++)
167 if (v != hostnames)
169 strcpy( p, sep );
170 p += strlen( sep );
173 if (!has_ldap_scheme( *v ))
175 strcpy( p, scheme );
176 p += strlen( scheme );
177 q = *v;
179 else
180 /* skip past colon in scheme prefix */
181 q = strchr( *v, '/' );
183 strcpy( p, *v );
184 p += strlen( *v );
186 if (!strchr( q, ':' ))
188 strcpy( p, port );
189 p += strlen( port );
192 return res;
195 static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
197 char *url = NULL, **strarray;
199 strarray = split_hostnames( hostnames );
200 if (strarray)
201 url = join_hostnames( scheme, strarray, port );
202 else
203 return NULL;
205 strarrayfreeU( strarray );
206 return url;
208 #endif
210 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
212 /***********************************************************************
213 * cldap_openA (WLDAP32.@)
215 * See cldap_openW.
217 WLDAP32_LDAP * CDECL cldap_openA( PCHAR hostname, ULONG portnumber )
219 #ifdef HAVE_LDAP
220 WLDAP32_LDAP *ld = NULL;
221 WCHAR *hostnameW = NULL;
223 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
225 if (hostname) {
226 hostnameW = strAtoW( hostname );
227 if (!hostnameW) goto exit;
230 ld = cldap_openW( hostnameW, portnumber );
232 exit:
233 strfreeW( hostnameW );
234 return ld;
236 #else
237 return NULL;
238 #endif
241 /***********************************************************************
242 * cldap_openW (WLDAP32.@)
244 * Initialize an LDAP context and create a UDP connection.
246 * PARAMS
247 * hostname [I] Name of the host to connect to.
248 * portnumber [I] Portnumber to use.
250 * RETURNS
251 * Success: Pointer to an LDAP context.
252 * Failure: NULL
254 * NOTES
255 * The hostname string can be a space separated string of hostnames,
256 * in which case the LDAP runtime will try to connect to the hosts
257 * in order, until a connection can be made. A hostname may have a
258 * trailing portnumber (separated from the hostname by a ':'), which
259 * will take precedence over the portnumber supplied as a parameter
260 * to this function.
262 WLDAP32_LDAP * CDECL cldap_openW( PWCHAR hostname, ULONG portnumber )
264 #ifdef HAVE_LDAP
265 LDAP *ld = NULL;
266 char *hostnameU = NULL, *url = NULL;
268 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
270 if (hostname) {
271 hostnameU = strWtoU( hostname );
272 if (!hostnameU) goto exit;
274 else {
275 hostnameU = strWtoU( defaulthost );
276 if (!hostnameU) goto exit;
279 url = urlify_hostnames( "cldap://", hostnameU, portnumber );
280 if (!url) goto exit;
282 ldap_initialize( &ld, url );
284 exit:
285 strfreeU( hostnameU );
286 strfreeU( url );
287 return ld;
289 #else
290 return NULL;
291 #endif
294 /***********************************************************************
295 * ldap_connect (WLDAP32.@)
297 * Connect to an LDAP server.
299 * PARAMS
300 * ld [I] Pointer to an LDAP context.
301 * timeout [I] Pointer to an l_timeval structure specifying the
302 * timeout in seconds.
304 * RETURNS
305 * Success: LDAP_SUCCESS
306 * Failure: An LDAP error code.
308 * NOTES
309 * The timeout parameter may be NULL in which case a default timeout
310 * value will be used.
312 ULONG CDECL ldap_connect( WLDAP32_LDAP *ld, struct l_timeval *timeout )
314 TRACE( "(%p, %p)\n", ld, timeout );
316 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
317 return WLDAP32_LDAP_SUCCESS; /* FIXME: do something, e.g. ping the host */
320 /***********************************************************************
321 * ldap_initA (WLDAP32.@)
323 * See ldap_initW.
325 WLDAP32_LDAP * CDECL ldap_initA( PCHAR hostname, ULONG portnumber )
327 #ifdef HAVE_LDAP
328 WLDAP32_LDAP *ld = NULL;
329 WCHAR *hostnameW = NULL;
331 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
333 if (hostname) {
334 hostnameW = strAtoW( hostname );
335 if (!hostnameW) goto exit;
338 ld = ldap_initW( hostnameW, portnumber );
340 exit:
341 strfreeW( hostnameW );
342 return ld;
344 #else
345 return NULL;
346 #endif
349 /***********************************************************************
350 * ldap_initW (WLDAP32.@)
352 * Initialize an LDAP context and create a TCP connection.
354 * PARAMS
355 * hostname [I] Name of the host to connect to.
356 * portnumber [I] Portnumber to use.
358 * RETURNS
359 * Success: Pointer to an LDAP context.
360 * Failure: NULL
362 * NOTES
363 * The hostname string can be a space separated string of hostnames,
364 * in which case the LDAP runtime will try to connect to the hosts
365 * in order, until a connection can be made. A hostname may have a
366 * trailing portnumber (separated from the hostname by a ':'), which
367 * will take precedence over the portnumber supplied as a parameter
368 * to this function. The connection will not be made until the first
369 * LDAP function that needs it is called.
371 WLDAP32_LDAP * CDECL ldap_initW( PWCHAR hostname, ULONG portnumber )
373 #ifdef HAVE_LDAP
374 LDAP *ld = NULL;
375 char *hostnameU = NULL, *url = NULL;
377 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
379 if (hostname) {
380 hostnameU = strWtoU( hostname );
381 if (!hostnameU) goto exit;
383 else {
384 hostnameU = strWtoU( defaulthost );
385 if (!hostnameU) goto exit;
388 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
389 if (!url) goto exit;
391 ldap_initialize( &ld, url );
393 exit:
394 strfreeU( hostnameU );
395 strfreeU( url );
396 return ld;
398 #else
399 return NULL;
400 #endif
403 /***********************************************************************
404 * ldap_openA (WLDAP32.@)
406 * See ldap_openW.
408 WLDAP32_LDAP * CDECL ldap_openA( PCHAR hostname, ULONG portnumber )
410 #ifdef HAVE_LDAP
411 WLDAP32_LDAP *ld = NULL;
412 WCHAR *hostnameW = NULL;
414 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
416 if (hostname) {
417 hostnameW = strAtoW( hostname );
418 if (!hostnameW) goto exit;
421 ld = ldap_openW( hostnameW, portnumber );
423 exit:
424 strfreeW( hostnameW );
425 return ld;
427 #else
428 return NULL;
429 #endif
432 /***********************************************************************
433 * ldap_openW (WLDAP32.@)
435 * Initialize an LDAP context and create a TCP connection.
437 * PARAMS
438 * hostname [I] Name of the host to connect to.
439 * portnumber [I] Portnumber to use.
441 * RETURNS
442 * Success: Pointer to an LDAP context.
443 * Failure: NULL
445 * NOTES
446 * The hostname string can be a space separated string of hostnames,
447 * in which case the LDAP runtime will try to connect to the hosts
448 * in order, until a connection can be made. A hostname may have a
449 * trailing portnumber (separated from the hostname by a ':'), which
450 * will take precedence over the portnumber supplied as a parameter
451 * to this function.
453 WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
455 #ifdef HAVE_LDAP
456 LDAP *ld = NULL;
457 char *hostnameU = NULL, *url = NULL;
459 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
461 if (hostname) {
462 hostnameU = strWtoU( hostname );
463 if (!hostnameU) goto exit;
465 else {
466 hostnameU = strWtoU( defaulthost );
467 if (!hostnameU) goto exit;
470 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
471 if (!url) goto exit;
473 ldap_initialize( &ld, url );
475 exit:
476 strfreeU( hostnameU );
477 strfreeU( url );
478 return ld;
480 #else
481 return NULL;
482 #endif
485 /***********************************************************************
486 * ldap_sslinitA (WLDAP32.@)
488 * See ldap_sslinitW.
490 WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure )
492 #ifdef HAVE_LDAP
493 WLDAP32_LDAP *ld;
494 WCHAR *hostnameW = NULL;
496 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
498 if (hostname) {
499 hostnameW = strAtoW( hostname );
500 if (!hostnameW) return NULL;
503 ld = ldap_sslinitW( hostnameW, portnumber, secure );
505 strfreeW( hostnameW );
506 return ld;
508 #else
509 return NULL;
510 #endif
513 /***********************************************************************
514 * ldap_sslinitW (WLDAP32.@)
516 * Initialize an LDAP context and create a secure TCP connection.
518 * PARAMS
519 * hostname [I] Name of the host to connect to.
520 * portnumber [I] Portnumber to use.
521 * secure [I] Ask the server to create an SSL connection.
523 * RETURNS
524 * Success: Pointer to an LDAP context.
525 * Failure: NULL
527 * NOTES
528 * The hostname string can be a space separated string of hostnames,
529 * in which case the LDAP runtime will try to connect to the hosts
530 * in order, until a connection can be made. A hostname may have a
531 * trailing portnumber (separated from the hostname by a ':'), which
532 * will take precedence over the portnumber supplied as a parameter
533 * to this function. The connection will not be made until the first
534 * LDAP function that needs it is called.
536 WLDAP32_LDAP * CDECL ldap_sslinitW( PWCHAR hostname, ULONG portnumber, int secure )
538 #ifdef HAVE_LDAP
539 WLDAP32_LDAP *ld = NULL;
540 char *hostnameU = NULL, *url = NULL;
542 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
544 if (hostname) {
545 hostnameU = strWtoU( hostname );
546 if (!hostnameU) goto exit;
548 else {
549 hostnameU = strWtoU( defaulthost );
550 if (!hostnameU) goto exit;
553 if (secure)
554 url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
555 else
556 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
558 if (!url) goto exit;
559 ldap_initialize( &ld, url );
561 exit:
562 strfreeU( hostnameU );
563 strfreeU( url );
564 return ld;
566 #else
567 return NULL;
568 #endif
571 /***********************************************************************
572 * ldap_start_tls_sA (WLDAP32.@)
574 * See ldap_start_tls_sW.
576 ULONG CDECL ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
577 PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
579 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
580 #ifdef HAVE_LDAP
581 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
583 ret = WLDAP32_LDAP_NO_MEMORY;
585 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
587 if (!ld) return ~0u;
589 if (serverctrls) {
590 serverctrlsW = controlarrayAtoW( serverctrls );
591 if (!serverctrlsW) goto exit;
593 if (clientctrls) {
594 clientctrlsW = controlarrayAtoW( clientctrls );
595 if (!clientctrlsW) goto exit;
598 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
600 exit:
601 controlarrayfreeW( serverctrlsW );
602 controlarrayfreeW( clientctrlsW );
604 #endif
605 return ret;
608 /***********************************************************************
609 * ldap_start_tls_s (WLDAP32.@)
611 * Start TLS encryption on an LDAP connection.
613 * PARAMS
614 * ld [I] Pointer to an LDAP context.
615 * retval [I] Return value from the server.
616 * result [I] Response message from the server.
617 * serverctrls [I] Array of LDAP server controls.
618 * clientctrls [I] Array of LDAP client controls.
620 * RETURNS
621 * Success: LDAP_SUCCESS
622 * Failure: An LDAP error code.
624 * NOTES
625 * LDAP function that needs it is called.
627 ULONG CDECL ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
628 PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
630 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
631 #ifdef HAVE_LDAP
632 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
634 ret = WLDAP32_LDAP_NO_MEMORY;
636 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
638 if (!ld) return ~0u;
640 if (serverctrls) {
641 serverctrlsU = controlarrayWtoU( serverctrls );
642 if (!serverctrlsU) goto exit;
644 if (clientctrls) {
645 clientctrlsU = controlarrayWtoU( clientctrls );
646 if (!clientctrlsU) goto exit;
649 ret = map_error( ldap_start_tls_s( ld, serverctrlsU, clientctrlsU ));
651 exit:
652 controlarrayfreeU( serverctrlsU );
653 controlarrayfreeU( clientctrlsU );
655 #endif
656 return ret;
659 /***********************************************************************
660 * ldap_startup (WLDAP32.@)
662 ULONG CDECL ldap_startup( PLDAP_VERSION_INFO version, HANDLE *instance )
664 TRACE( "(%p, %p)\n", version, instance );
665 return WLDAP32_LDAP_SUCCESS;
668 /***********************************************************************
669 * ldap_stop_tls_s (WLDAP32.@)
671 * Stop TLS encryption on an LDAP connection.
673 * PARAMS
674 * ld [I] Pointer to an LDAP context.
676 * RETURNS
677 * Success: TRUE
678 * Failure: FALSE
680 BOOLEAN CDECL ldap_stop_tls_s( WLDAP32_LDAP *ld )
682 TRACE( "(%p)\n", ld );
683 return TRUE; /* FIXME: find a way to stop tls on a connection */