push b79aff4f9e064e6702329573e5ebb8548e254b61
[wine/hacks.git] / dlls / wldap32 / init.c
blob08cab81a94817f453293dbf6e30558660ef2da7a
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 #else
36 #define LDAP_SUCCESS 0x00
37 #define LDAP_NOT_SUPPORTED 0x5c
38 #endif
40 #include "winldap_private.h"
41 #include "wldap32.h"
43 #ifdef HAVE_LDAP
44 /* Should eventually be determined by the algorithm documented on MSDN. */
45 static const WCHAR defaulthost[] = { 'l','o','c','a','l','h','o','s','t',0 };
47 /* Split a space separated string of hostnames into a string array */
48 static char **split_hostnames( const char *hostnames )
50 char **res, *str, *p, *q;
51 unsigned int i = 0;
53 str = strdupU( hostnames );
54 if (!str) return NULL;
56 p = str;
57 while (isspace( *p )) p++;
58 if (*p) i++;
60 while (*p)
62 if (isspace( *p ))
64 while (isspace( *p )) p++;
65 if (*p) i++;
67 p++;
70 res = HeapAlloc( GetProcessHeap(), 0, (i + 1) * sizeof(char *) );
71 if (!res)
73 HeapFree( GetProcessHeap(), 0, str );
74 return NULL;
77 p = str;
78 while (isspace( *p )) p++;
80 q = p;
81 i = 0;
83 while (*p)
85 if (p[1] != '\0')
87 if (isspace( *p ))
89 *p = '\0'; p++;
90 res[i] = strdupU( q );
91 if (!res[i]) goto oom;
92 i++;
94 while (isspace( *p )) p++;
95 q = p;
98 else
100 res[i] = strdupU( q );
101 if (!res[i]) goto oom;
102 i++;
104 p++;
106 res[i] = NULL;
108 HeapFree( GetProcessHeap(), 0, str );
109 return res;
111 oom:
112 while (i > 0) strfreeU( res[--i] );
114 HeapFree( GetProcessHeap(), 0, res );
115 HeapFree( GetProcessHeap(), 0, str );
117 return NULL;
120 /* Determine if a URL starts with a known LDAP scheme */
121 static int has_ldap_scheme( char *url )
123 if (!strncasecmp( url, "ldap://", 7 ) ||
124 !strncasecmp( url, "ldaps://", 8 ) ||
125 !strncasecmp( url, "ldapi://", 8 ) ||
126 !strncasecmp( url, "cldap://", 8 )) return 1;
127 return 0;
130 /* Flatten an array of hostnames into a space separated string of URLs.
131 * Prepend a given scheme and append a given portnumber to each hostname
132 * if necessary.
134 static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnumber )
136 char *res, *p, *q, **v;
137 unsigned int i = 0, size = 0;
138 static const char sep[] = " ", fmt[] = ":%d";
139 char port[7];
141 sprintf( port, fmt, portnumber );
143 for (v = hostnames; *v; v++)
145 if (!has_ldap_scheme( *v ))
147 size += strlen( scheme );
148 q = *v;
150 else
151 /* skip past colon in scheme prefix */
152 q = strchr( *v, '/' );
154 size += strlen( *v );
156 if (!strchr( q, ':' ))
157 size += strlen( port );
159 i++;
162 size += (i - 1) * strlen( sep );
164 res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
165 if (!res) return NULL;
167 p = res;
168 for (v = hostnames; *v; v++)
170 if (v != hostnames)
172 strcpy( p, sep );
173 p += strlen( sep );
176 if (!has_ldap_scheme( *v ))
178 strcpy( p, scheme );
179 p += strlen( scheme );
180 q = *v;
182 else
183 /* skip past colon in scheme prefix */
184 q = strchr( *v, '/' );
186 strcpy( p, *v );
187 p += strlen( *v );
189 if (!strchr( q, ':' ))
191 strcpy( p, port );
192 p += strlen( port );
195 return res;
198 static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
200 char *url = NULL, **strarray;
202 strarray = split_hostnames( hostnames );
203 if (strarray)
204 url = join_hostnames( scheme, strarray, port );
205 else
206 return NULL;
208 strarrayfreeU( strarray );
209 return url;
211 #endif
213 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
215 /***********************************************************************
216 * cldap_openA (WLDAP32.@)
218 * See cldap_openW.
220 WLDAP32_LDAP * CDECL cldap_openA( PCHAR hostname, ULONG portnumber )
222 #ifdef HAVE_LDAP
223 WLDAP32_LDAP *ld = NULL;
224 WCHAR *hostnameW = NULL;
226 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
228 if (hostname) {
229 hostnameW = strAtoW( hostname );
230 if (!hostnameW) goto exit;
233 ld = cldap_openW( hostnameW, portnumber );
235 exit:
236 strfreeW( hostnameW );
237 return ld;
239 #else
240 return NULL;
241 #endif
244 /***********************************************************************
245 * cldap_openW (WLDAP32.@)
247 * Initialize an LDAP context and create a UDP connection.
249 * PARAMS
250 * hostname [I] Name of the host to connect to.
251 * portnumber [I] Portnumber to use.
253 * RETURNS
254 * Success: Pointer to an LDAP context.
255 * Failure: NULL
257 * NOTES
258 * The hostname string can be a space separated string of hostnames,
259 * in which case the LDAP runtime will try to connect to the hosts
260 * in order, until a connection can be made. A hostname may have a
261 * trailing portnumber (separated from the hostname by a ':'), which
262 * will take precedence over the portnumber supplied as a parameter
263 * to this function.
265 WLDAP32_LDAP * CDECL cldap_openW( PWCHAR hostname, ULONG portnumber )
267 #ifdef HAVE_LDAP
268 LDAP *ld = NULL;
269 char *hostnameU = NULL, *url = NULL;
271 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
273 if (hostname) {
274 hostnameU = strWtoU( hostname );
275 if (!hostnameU) goto exit;
277 else {
278 hostnameU = strWtoU( defaulthost );
279 if (!hostnameU) goto exit;
282 url = urlify_hostnames( "cldap://", hostnameU, portnumber );
283 if (!url) goto exit;
285 ldap_initialize( &ld, url );
287 exit:
288 strfreeU( hostnameU );
289 strfreeU( url );
290 return ld;
292 #else
293 return NULL;
294 #endif
297 /***********************************************************************
298 * ldap_connect (WLDAP32.@)
300 * Connect to an LDAP server.
302 * PARAMS
303 * ld [I] Pointer to an LDAP context.
304 * timeout [I] Pointer to an l_timeval structure specifying the
305 * timeout in seconds.
307 * RETURNS
308 * Success: LDAP_SUCCESS
309 * Failure: An LDAP error code.
311 * NOTES
312 * The timeout parameter may be NULL in which case a default timeout
313 * value will be used.
315 ULONG CDECL ldap_connect( WLDAP32_LDAP *ld, struct l_timeval *timeout )
317 TRACE( "(%p, %p)\n", ld, timeout );
319 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
320 return LDAP_SUCCESS; /* FIXME: do something, e.g. ping the host */
323 /***********************************************************************
324 * ldap_initA (WLDAP32.@)
326 * See ldap_initW.
328 WLDAP32_LDAP * CDECL ldap_initA( PCHAR hostname, ULONG portnumber )
330 #ifdef HAVE_LDAP
331 WLDAP32_LDAP *ld = NULL;
332 WCHAR *hostnameW = NULL;
334 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
336 if (hostname) {
337 hostnameW = strAtoW( hostname );
338 if (!hostnameW) goto exit;
341 ld = ldap_initW( hostnameW, portnumber );
343 exit:
344 strfreeW( hostnameW );
345 return ld;
347 #else
348 return NULL;
349 #endif
352 /***********************************************************************
353 * ldap_initW (WLDAP32.@)
355 * Initialize an LDAP context and create a TCP connection.
357 * PARAMS
358 * hostname [I] Name of the host to connect to.
359 * portnumber [I] Portnumber to use.
361 * RETURNS
362 * Success: Pointer to an LDAP context.
363 * Failure: NULL
365 * NOTES
366 * The hostname string can be a space separated string of hostnames,
367 * in which case the LDAP runtime will try to connect to the hosts
368 * in order, until a connection can be made. A hostname may have a
369 * trailing portnumber (separated from the hostname by a ':'), which
370 * will take precedence over the portnumber supplied as a parameter
371 * to this function. The connection will not be made until the first
372 * LDAP function that needs it is called.
374 WLDAP32_LDAP * CDECL ldap_initW( PWCHAR hostname, ULONG portnumber )
376 #ifdef HAVE_LDAP
377 LDAP *ld = NULL;
378 char *hostnameU = NULL, *url = NULL;
380 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
382 if (hostname) {
383 hostnameU = strWtoU( hostname );
384 if (!hostnameU) goto exit;
386 else {
387 hostnameU = strWtoU( defaulthost );
388 if (!hostnameU) goto exit;
391 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
392 if (!url) goto exit;
394 ldap_initialize( &ld, url );
396 exit:
397 strfreeU( hostnameU );
398 strfreeU( url );
399 return ld;
401 #else
402 return NULL;
403 #endif
406 /***********************************************************************
407 * ldap_openA (WLDAP32.@)
409 * See ldap_openW.
411 WLDAP32_LDAP * CDECL ldap_openA( PCHAR hostname, ULONG portnumber )
413 #ifdef HAVE_LDAP
414 WLDAP32_LDAP *ld = NULL;
415 WCHAR *hostnameW = NULL;
417 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
419 if (hostname) {
420 hostnameW = strAtoW( hostname );
421 if (!hostnameW) goto exit;
424 ld = ldap_openW( hostnameW, portnumber );
426 exit:
427 strfreeW( hostnameW );
428 return ld;
430 #else
431 return NULL;
432 #endif
435 /***********************************************************************
436 * ldap_openW (WLDAP32.@)
438 * Initialize an LDAP context and create a TCP connection.
440 * PARAMS
441 * hostname [I] Name of the host to connect to.
442 * portnumber [I] Portnumber to use.
444 * RETURNS
445 * Success: Pointer to an LDAP context.
446 * Failure: NULL
448 * NOTES
449 * The hostname string can be a space separated string of hostnames,
450 * in which case the LDAP runtime will try to connect to the hosts
451 * in order, until a connection can be made. A hostname may have a
452 * trailing portnumber (separated from the hostname by a ':'), which
453 * will take precedence over the portnumber supplied as a parameter
454 * to this function.
456 WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
458 #ifdef HAVE_LDAP
459 LDAP *ld = NULL;
460 char *hostnameU = NULL, *url = NULL;
462 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
464 if (hostname) {
465 hostnameU = strWtoU( hostname );
466 if (!hostnameU) goto exit;
468 else {
469 hostnameU = strWtoU( defaulthost );
470 if (!hostnameU) goto exit;
473 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
474 if (!url) goto exit;
476 ldap_initialize( &ld, url );
478 exit:
479 strfreeU( hostnameU );
480 strfreeU( url );
481 return ld;
483 #else
484 return NULL;
485 #endif
488 /***********************************************************************
489 * ldap_sslinitA (WLDAP32.@)
491 * See ldap_sslinitW.
493 WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure )
495 #ifdef HAVE_LDAP
496 WLDAP32_LDAP *ld;
497 WCHAR *hostnameW = NULL;
499 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
501 if (hostname) {
502 hostnameW = strAtoW( hostname );
503 if (!hostnameW) return NULL;
506 ld = ldap_sslinitW( hostnameW, portnumber, secure );
508 strfreeW( hostnameW );
509 return ld;
511 #else
512 return NULL;
513 #endif
516 /***********************************************************************
517 * ldap_sslinitW (WLDAP32.@)
519 * Initialize an LDAP context and create a secure TCP connection.
521 * PARAMS
522 * hostname [I] Name of the host to connect to.
523 * portnumber [I] Portnumber to use.
524 * secure [I] Ask the server to create an SSL connection.
526 * RETURNS
527 * Success: Pointer to an LDAP context.
528 * Failure: NULL
530 * NOTES
531 * The hostname string can be a space separated string of hostnames,
532 * in which case the LDAP runtime will try to connect to the hosts
533 * in order, until a connection can be made. A hostname may have a
534 * trailing portnumber (separated from the hostname by a ':'), which
535 * will take precedence over the portnumber supplied as a parameter
536 * to this function. The connection will not be made until the first
537 * LDAP function that needs it is called.
539 WLDAP32_LDAP * CDECL ldap_sslinitW( PWCHAR hostname, ULONG portnumber, int secure )
541 #ifdef HAVE_LDAP
542 WLDAP32_LDAP *ld = NULL;
543 char *hostnameU = NULL, *url = NULL;
545 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
547 if (hostname) {
548 hostnameU = strWtoU( hostname );
549 if (!hostnameU) goto exit;
551 else {
552 hostnameU = strWtoU( defaulthost );
553 if (!hostnameU) goto exit;
556 if (secure)
557 url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
558 else
559 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
561 if (!url) goto exit;
562 ldap_initialize( &ld, url );
564 exit:
565 strfreeU( hostnameU );
566 strfreeU( url );
567 return ld;
569 #else
570 return NULL;
571 #endif
574 /***********************************************************************
575 * ldap_start_tls_sA (WLDAP32.@)
577 * See ldap_start_tls_sW.
579 ULONG CDECL ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
580 PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
582 ULONG ret = LDAP_NOT_SUPPORTED;
583 #ifdef HAVE_LDAP
584 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
586 ret = WLDAP32_LDAP_NO_MEMORY;
588 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
590 if (!ld) return ~0UL;
592 if (serverctrls) {
593 serverctrlsW = controlarrayAtoW( serverctrls );
594 if (!serverctrlsW) goto exit;
596 if (clientctrls) {
597 clientctrlsW = controlarrayAtoW( clientctrls );
598 if (!clientctrlsW) goto exit;
601 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
603 exit:
604 controlarrayfreeW( serverctrlsW );
605 controlarrayfreeW( clientctrlsW );
607 #endif
608 return ret;
611 /***********************************************************************
612 * ldap_start_tls_s (WLDAP32.@)
614 * Start TLS encryption on an LDAP connection.
616 * PARAMS
617 * ld [I] Pointer to an LDAP context.
618 * retval [I] Return value from the server.
619 * result [I] Response message from the server.
620 * serverctrls [I] Array of LDAP server controls.
621 * clientctrls [I] Array of LDAP client controls.
623 * RETURNS
624 * Success: LDAP_SUCCESS
625 * Failure: An LDAP error code.
627 * NOTES
628 * LDAP function that needs it is called.
630 ULONG CDECL ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
631 PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
633 ULONG ret = LDAP_NOT_SUPPORTED;
634 #ifdef HAVE_LDAP
635 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
637 ret = WLDAP32_LDAP_NO_MEMORY;
639 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
641 if (!ld) return ~0UL;
643 if (serverctrls) {
644 serverctrlsU = controlarrayWtoU( serverctrls );
645 if (!serverctrlsU) goto exit;
647 if (clientctrls) {
648 clientctrlsU = controlarrayWtoU( clientctrls );
649 if (!clientctrlsU) goto exit;
652 ret = ldap_start_tls_s( ld, serverctrlsU, clientctrlsU );
654 exit:
655 controlarrayfreeU( serverctrlsU );
656 controlarrayfreeU( clientctrlsU );
658 #endif
659 return ret;
662 /***********************************************************************
663 * ldap_startup (WLDAP32.@)
665 ULONG CDECL ldap_startup( PLDAP_VERSION_INFO version, HANDLE *instance )
667 TRACE( "(%p, %p)\n", version, instance );
668 return LDAP_SUCCESS;
671 /***********************************************************************
672 * ldap_stop_tls_s (WLDAP32.@)
674 * Stop TLS encryption on an LDAP connection.
676 * PARAMS
677 * ld [I] Pointer to an LDAP context.
679 * RETURNS
680 * Success: TRUE
681 * Failure: FALSE
683 BOOLEAN CDECL ldap_stop_tls_s( WLDAP32_LDAP *ld )
685 TRACE( "(%p)\n", ld );
686 return TRUE; /* FIXME: find a way to stop tls on a connection */