include/icftypes.idl: Add more typedefs.
[wine.git] / dlls / wldap32 / init.c
blob46a078aabaf8a30f3f96dd38cf0d96f73c4a62b0
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 <stdio.h>
25 #include <stdarg.h>
26 #ifdef HAVE_LDAP_H
27 #include <ldap.h>
28 #endif
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winnls.h"
34 #include "winldap_private.h"
35 #include "wldap32.h"
36 #include "wine/debug.h"
38 #ifdef HAVE_LDAP
39 /* Should eventually be determined by the algorithm documented on MSDN. */
40 static const WCHAR defaulthost[] = { 'l','o','c','a','l','h','o','s','t',0 };
42 /* Split a space separated string of hostnames into a string array */
43 static char **split_hostnames( const char *hostnames )
45 char **res, *str, *p, *q;
46 unsigned int i = 0;
48 str = strdupU( hostnames );
49 if (!str) return NULL;
51 p = str;
52 while (isspace( *p )) p++;
53 if (*p) i++;
55 while (*p)
57 if (isspace( *p ))
59 while (isspace( *p )) p++;
60 if (*p) i++;
62 p++;
65 res = HeapAlloc( GetProcessHeap(), 0, (i + 1) * sizeof(char *) );
66 if (!res)
68 HeapFree( GetProcessHeap(), 0, str );
69 return NULL;
72 p = str;
73 while (isspace( *p )) p++;
75 q = p;
76 i = 0;
78 while (*p)
80 if (p[1] != '\0')
82 if (isspace( *p ))
84 *p = '\0'; p++;
85 res[i] = strdupU( q );
86 if (!res[i]) goto oom;
87 i++;
89 while (isspace( *p )) p++;
90 q = p;
93 else
95 res[i] = strdupU( q );
96 if (!res[i]) goto oom;
97 i++;
99 p++;
101 res[i] = NULL;
103 HeapFree( GetProcessHeap(), 0, str );
104 return res;
106 oom:
107 while (i > 0) strfreeU( res[--i] );
109 HeapFree( GetProcessHeap(), 0, res );
110 HeapFree( GetProcessHeap(), 0, str );
112 return NULL;
115 /* Determine if a URL starts with a known LDAP scheme */
116 static BOOL has_ldap_scheme( char *url )
118 return !strncasecmp( url, "ldap://", 7 ) ||
119 !strncasecmp( url, "ldaps://", 8 ) ||
120 !strncasecmp( url, "ldapi://", 8 ) ||
121 !strncasecmp( url, "cldap://", 8 );
124 /* Flatten an array of hostnames into a space separated string of URLs.
125 * Prepend a given scheme and append a given port number to each hostname
126 * if necessary.
128 static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnumber )
130 char *res, *p, *q, **v;
131 unsigned int i = 0, size = 0;
132 static const char sep[] = " ", fmt[] = ":%d";
133 char port[7];
135 sprintf( port, fmt, portnumber );
137 for (v = hostnames; *v; v++)
139 if (!has_ldap_scheme( *v ))
141 size += strlen( scheme );
142 q = *v;
144 else
145 /* skip past colon in scheme prefix */
146 q = strchr( *v, '/' );
148 size += strlen( *v );
150 if (!strchr( q, ':' ))
151 size += strlen( port );
153 i++;
156 size += (i - 1) * strlen( sep );
158 res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
159 if (!res) return NULL;
161 p = res;
162 for (v = hostnames; *v; v++)
164 if (v != hostnames)
166 strcpy( p, sep );
167 p += strlen( sep );
170 if (!has_ldap_scheme( *v ))
172 strcpy( p, scheme );
173 p += strlen( scheme );
174 q = *v;
176 else
177 /* skip past colon in scheme prefix */
178 q = strchr( *v, '/' );
180 strcpy( p, *v );
181 p += strlen( *v );
183 if (!strchr( q, ':' ))
185 strcpy( p, port );
186 p += strlen( port );
189 return res;
192 static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
194 char *url = NULL, **strarray;
196 strarray = split_hostnames( hostnames );
197 if (strarray)
198 url = join_hostnames( scheme, strarray, port );
199 else
200 return NULL;
202 strarrayfreeU( strarray );
203 return url;
205 #endif
207 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
209 /***********************************************************************
210 * cldap_openA (WLDAP32.@)
212 * See cldap_openW.
214 WLDAP32_LDAP * CDECL cldap_openA( PCHAR hostname, ULONG portnumber )
216 #ifdef HAVE_LDAP
217 WLDAP32_LDAP *ld = NULL;
218 WCHAR *hostnameW = NULL;
220 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
222 if (hostname) {
223 hostnameW = strAtoW( hostname );
224 if (!hostnameW) goto exit;
227 ld = cldap_openW( hostnameW, portnumber );
229 exit:
230 strfreeW( hostnameW );
231 return ld;
233 #else
234 return NULL;
235 #endif
238 /***********************************************************************
239 * cldap_openW (WLDAP32.@)
241 * Initialize an LDAP context and create a UDP connection.
243 * PARAMS
244 * hostname [I] Name of the host to connect to.
245 * portnumber [I] Port number to use.
247 * RETURNS
248 * Success: Pointer to an LDAP context.
249 * Failure: NULL
251 * NOTES
252 * The hostname string can be a space separated string of hostnames,
253 * in which case the LDAP runtime will try to connect to the hosts
254 * in order, until a connection can be made. A hostname may have a
255 * trailing port number (separated from the hostname by a ':'), which
256 * will take precedence over the port number supplied as a parameter
257 * to this function.
259 WLDAP32_LDAP * CDECL cldap_openW( PWCHAR hostname, ULONG portnumber )
261 #ifdef HAVE_LDAP
262 LDAP *ld = NULL;
263 char *hostnameU = NULL, *url = NULL;
265 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
267 if (hostname) {
268 hostnameU = strWtoU( hostname );
269 if (!hostnameU) goto exit;
271 else {
272 hostnameU = strWtoU( defaulthost );
273 if (!hostnameU) goto exit;
276 url = urlify_hostnames( "cldap://", hostnameU, portnumber );
277 if (!url) goto exit;
279 ldap_initialize( &ld, url );
281 exit:
282 strfreeU( hostnameU );
283 strfreeU( url );
284 return ld;
286 #else
287 return NULL;
288 #endif
291 /***********************************************************************
292 * ldap_connect (WLDAP32.@)
294 * Connect to an LDAP server.
296 * PARAMS
297 * ld [I] Pointer to an LDAP context.
298 * timeout [I] Pointer to an l_timeval structure specifying the
299 * timeout in seconds.
301 * RETURNS
302 * Success: LDAP_SUCCESS
303 * Failure: An LDAP error code.
305 * NOTES
306 * The timeout parameter may be NULL in which case a default timeout
307 * value will be used.
309 ULONG CDECL ldap_connect( WLDAP32_LDAP *ld, struct l_timeval *timeout )
311 TRACE( "(%p, %p)\n", ld, timeout );
313 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
314 return WLDAP32_LDAP_SUCCESS; /* FIXME: do something, e.g. ping the host */
317 /***********************************************************************
318 * ldap_initA (WLDAP32.@)
320 * See ldap_initW.
322 WLDAP32_LDAP * CDECL ldap_initA( const PCHAR hostname, ULONG portnumber )
324 #ifdef HAVE_LDAP
325 WLDAP32_LDAP *ld = NULL;
326 WCHAR *hostnameW = NULL;
328 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
330 if (hostname) {
331 hostnameW = strAtoW( hostname );
332 if (!hostnameW) goto exit;
335 ld = ldap_initW( hostnameW, portnumber );
337 exit:
338 strfreeW( hostnameW );
339 return ld;
341 #else
342 return NULL;
343 #endif
346 /***********************************************************************
347 * ldap_initW (WLDAP32.@)
349 * Initialize an LDAP context and create a TCP connection.
351 * PARAMS
352 * hostname [I] Name of the host to connect to.
353 * portnumber [I] Port number to use.
355 * RETURNS
356 * Success: Pointer to an LDAP context.
357 * Failure: NULL
359 * NOTES
360 * The hostname string can be a space separated string of hostnames,
361 * in which case the LDAP runtime will try to connect to the hosts
362 * in order, until a connection can be made. A hostname may have a
363 * trailing port number (separated from the hostname by a ':'), which
364 * will take precedence over the port number supplied as a parameter
365 * to this function. The connection will not be made until the first
366 * LDAP function that needs it is called.
368 WLDAP32_LDAP * CDECL ldap_initW( const PWCHAR hostname, ULONG portnumber )
370 #ifdef HAVE_LDAP
371 LDAP *ld = NULL;
372 char *hostnameU = NULL, *url = NULL;
374 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
376 if (hostname) {
377 hostnameU = strWtoU( hostname );
378 if (!hostnameU) goto exit;
380 else {
381 hostnameU = strWtoU( defaulthost );
382 if (!hostnameU) goto exit;
385 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
386 if (!url) goto exit;
388 ldap_initialize( &ld, url );
390 exit:
391 strfreeU( hostnameU );
392 strfreeU( url );
393 return ld;
395 #else
396 return NULL;
397 #endif
400 /***********************************************************************
401 * ldap_openA (WLDAP32.@)
403 * See ldap_openW.
405 WLDAP32_LDAP * CDECL ldap_openA( PCHAR hostname, ULONG portnumber )
407 #ifdef HAVE_LDAP
408 WLDAP32_LDAP *ld = NULL;
409 WCHAR *hostnameW = NULL;
411 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
413 if (hostname) {
414 hostnameW = strAtoW( hostname );
415 if (!hostnameW) goto exit;
418 ld = ldap_openW( hostnameW, portnumber );
420 exit:
421 strfreeW( hostnameW );
422 return ld;
424 #else
425 return NULL;
426 #endif
429 /***********************************************************************
430 * ldap_openW (WLDAP32.@)
432 * Initialize an LDAP context and create a TCP connection.
434 * PARAMS
435 * hostname [I] Name of the host to connect to.
436 * portnumber [I] Port number to use.
438 * RETURNS
439 * Success: Pointer to an LDAP context.
440 * Failure: NULL
442 * NOTES
443 * The hostname string can be a space separated string of hostnames,
444 * in which case the LDAP runtime will try to connect to the hosts
445 * in order, until a connection can be made. A hostname may have a
446 * trailing port number (separated from the hostname by a ':'), which
447 * will take precedence over the port number supplied as a parameter
448 * to this function.
450 WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
452 #ifdef HAVE_LDAP
453 LDAP *ld = NULL;
454 char *hostnameU = NULL, *url = NULL;
456 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
458 if (hostname) {
459 hostnameU = strWtoU( hostname );
460 if (!hostnameU) goto exit;
462 else {
463 hostnameU = strWtoU( defaulthost );
464 if (!hostnameU) goto exit;
467 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
468 if (!url) goto exit;
470 ldap_initialize( &ld, url );
472 exit:
473 strfreeU( hostnameU );
474 strfreeU( url );
475 return ld;
477 #else
478 return NULL;
479 #endif
482 /***********************************************************************
483 * ldap_sslinitA (WLDAP32.@)
485 * See ldap_sslinitW.
487 WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure )
489 #ifdef HAVE_LDAP
490 WLDAP32_LDAP *ld;
491 WCHAR *hostnameW = NULL;
493 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
495 if (hostname) {
496 hostnameW = strAtoW( hostname );
497 if (!hostnameW) return NULL;
500 ld = ldap_sslinitW( hostnameW, portnumber, secure );
502 strfreeW( hostnameW );
503 return ld;
505 #else
506 return NULL;
507 #endif
510 /***********************************************************************
511 * ldap_sslinitW (WLDAP32.@)
513 * Initialize an LDAP context and create a secure TCP connection.
515 * PARAMS
516 * hostname [I] Name of the host to connect to.
517 * portnumber [I] Port number to use.
518 * secure [I] Ask the server to create an SSL connection.
520 * RETURNS
521 * Success: Pointer to an LDAP context.
522 * Failure: NULL
524 * NOTES
525 * The hostname string can be a space separated string of hostnames,
526 * in which case the LDAP runtime will try to connect to the hosts
527 * in order, until a connection can be made. A hostname may have a
528 * trailing port number (separated from the hostname by a ':'), which
529 * will take precedence over the port number supplied as a parameter
530 * to this function. The connection will not be made until the first
531 * LDAP function that needs it is called.
533 WLDAP32_LDAP * CDECL ldap_sslinitW( PWCHAR hostname, ULONG portnumber, int secure )
535 #ifdef HAVE_LDAP
536 WLDAP32_LDAP *ld = NULL;
537 char *hostnameU = NULL, *url = NULL;
539 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
541 if (hostname) {
542 hostnameU = strWtoU( hostname );
543 if (!hostnameU) goto exit;
545 else {
546 hostnameU = strWtoU( defaulthost );
547 if (!hostnameU) goto exit;
550 if (secure)
551 url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
552 else
553 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
555 if (!url) goto exit;
556 ldap_initialize( &ld, url );
558 exit:
559 strfreeU( hostnameU );
560 strfreeU( url );
561 return ld;
563 #else
564 return NULL;
565 #endif
568 /***********************************************************************
569 * ldap_start_tls_sA (WLDAP32.@)
571 * See ldap_start_tls_sW.
573 ULONG CDECL ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
574 PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
576 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
577 #ifdef HAVE_LDAP
578 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
580 ret = WLDAP32_LDAP_NO_MEMORY;
582 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
584 if (!ld) return ~0u;
586 if (serverctrls) {
587 serverctrlsW = controlarrayAtoW( serverctrls );
588 if (!serverctrlsW) goto exit;
590 if (clientctrls) {
591 clientctrlsW = controlarrayAtoW( clientctrls );
592 if (!clientctrlsW) goto exit;
595 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
597 exit:
598 controlarrayfreeW( serverctrlsW );
599 controlarrayfreeW( clientctrlsW );
601 #endif
602 return ret;
605 /***********************************************************************
606 * ldap_start_tls_s (WLDAP32.@)
608 * Start TLS encryption on an LDAP connection.
610 * PARAMS
611 * ld [I] Pointer to an LDAP context.
612 * retval [I] Return value from the server.
613 * result [I] Response message from the server.
614 * serverctrls [I] Array of LDAP server controls.
615 * clientctrls [I] Array of LDAP client controls.
617 * RETURNS
618 * Success: LDAP_SUCCESS
619 * Failure: An LDAP error code.
621 * NOTES
622 * LDAP function that needs it is called.
624 ULONG CDECL ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
625 PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
627 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
628 #ifdef HAVE_LDAP
629 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
631 ret = WLDAP32_LDAP_NO_MEMORY;
633 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
635 if (!ld) return ~0u;
637 if (serverctrls) {
638 serverctrlsU = controlarrayWtoU( serverctrls );
639 if (!serverctrlsU) goto exit;
641 if (clientctrls) {
642 clientctrlsU = controlarrayWtoU( clientctrls );
643 if (!clientctrlsU) goto exit;
646 ret = map_error( ldap_start_tls_s( ld, serverctrlsU, clientctrlsU ));
648 exit:
649 controlarrayfreeU( serverctrlsU );
650 controlarrayfreeU( clientctrlsU );
652 #endif
653 return ret;
656 /***********************************************************************
657 * ldap_startup (WLDAP32.@)
659 ULONG CDECL ldap_startup( PLDAP_VERSION_INFO version, HANDLE *instance )
661 TRACE( "(%p, %p)\n", version, instance );
662 return WLDAP32_LDAP_SUCCESS;
665 /***********************************************************************
666 * ldap_stop_tls_s (WLDAP32.@)
668 * Stop TLS encryption on an LDAP connection.
670 * PARAMS
671 * ld [I] Pointer to an LDAP context.
673 * RETURNS
674 * Success: TRUE
675 * Failure: FALSE
677 BOOLEAN CDECL ldap_stop_tls_s( WLDAP32_LDAP *ld )
679 TRACE( "(%p)\n", ld );
680 return TRUE; /* FIXME: find a way to stop tls on a connection */