oleaut32: Zero-init [out] only parameters.
[wine.git] / dlls / wldap32 / init.c
blobb5aed58e97642a6b434428ec67e3b2b2cc4df889
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 int has_ldap_scheme( char *url )
118 if (!strncasecmp( url, "ldap://", 7 ) ||
119 !strncasecmp( url, "ldaps://", 8 ) ||
120 !strncasecmp( url, "ldapi://", 8 ) ||
121 !strncasecmp( url, "cldap://", 8 )) return 1;
122 return 0;
125 /* Flatten an array of hostnames into a space separated string of URLs.
126 * Prepend a given scheme and append a given port number to each hostname
127 * if necessary.
129 static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnumber )
131 char *res, *p, *q, **v;
132 unsigned int i = 0, size = 0;
133 static const char sep[] = " ", fmt[] = ":%d";
134 char port[7];
136 sprintf( port, fmt, portnumber );
138 for (v = hostnames; *v; v++)
140 if (!has_ldap_scheme( *v ))
142 size += strlen( scheme );
143 q = *v;
145 else
146 /* skip past colon in scheme prefix */
147 q = strchr( *v, '/' );
149 size += strlen( *v );
151 if (!strchr( q, ':' ))
152 size += strlen( port );
154 i++;
157 size += (i - 1) * strlen( sep );
159 res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
160 if (!res) return NULL;
162 p = res;
163 for (v = hostnames; *v; v++)
165 if (v != hostnames)
167 strcpy( p, sep );
168 p += strlen( sep );
171 if (!has_ldap_scheme( *v ))
173 strcpy( p, scheme );
174 p += strlen( scheme );
175 q = *v;
177 else
178 /* skip past colon in scheme prefix */
179 q = strchr( *v, '/' );
181 strcpy( p, *v );
182 p += strlen( *v );
184 if (!strchr( q, ':' ))
186 strcpy( p, port );
187 p += strlen( port );
190 return res;
193 static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
195 char *url = NULL, **strarray;
197 strarray = split_hostnames( hostnames );
198 if (strarray)
199 url = join_hostnames( scheme, strarray, port );
200 else
201 return NULL;
203 strarrayfreeU( strarray );
204 return url;
206 #endif
208 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
210 /***********************************************************************
211 * cldap_openA (WLDAP32.@)
213 * See cldap_openW.
215 WLDAP32_LDAP * CDECL cldap_openA( PCHAR hostname, ULONG portnumber )
217 #ifdef HAVE_LDAP
218 WLDAP32_LDAP *ld = NULL;
219 WCHAR *hostnameW = NULL;
221 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
223 if (hostname) {
224 hostnameW = strAtoW( hostname );
225 if (!hostnameW) goto exit;
228 ld = cldap_openW( hostnameW, portnumber );
230 exit:
231 strfreeW( hostnameW );
232 return ld;
234 #else
235 return NULL;
236 #endif
239 /***********************************************************************
240 * cldap_openW (WLDAP32.@)
242 * Initialize an LDAP context and create a UDP connection.
244 * PARAMS
245 * hostname [I] Name of the host to connect to.
246 * portnumber [I] Port number to use.
248 * RETURNS
249 * Success: Pointer to an LDAP context.
250 * Failure: NULL
252 * NOTES
253 * The hostname string can be a space separated string of hostnames,
254 * in which case the LDAP runtime will try to connect to the hosts
255 * in order, until a connection can be made. A hostname may have a
256 * trailing port number (separated from the hostname by a ':'), which
257 * will take precedence over the port number supplied as a parameter
258 * to this function.
260 WLDAP32_LDAP * CDECL cldap_openW( PWCHAR hostname, ULONG portnumber )
262 #ifdef HAVE_LDAP
263 LDAP *ld = NULL;
264 char *hostnameU = NULL, *url = NULL;
266 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
268 if (hostname) {
269 hostnameU = strWtoU( hostname );
270 if (!hostnameU) goto exit;
272 else {
273 hostnameU = strWtoU( defaulthost );
274 if (!hostnameU) goto exit;
277 url = urlify_hostnames( "cldap://", hostnameU, portnumber );
278 if (!url) goto exit;
280 ldap_initialize( &ld, url );
282 exit:
283 strfreeU( hostnameU );
284 strfreeU( url );
285 return ld;
287 #else
288 return NULL;
289 #endif
292 /***********************************************************************
293 * ldap_connect (WLDAP32.@)
295 * Connect to an LDAP server.
297 * PARAMS
298 * ld [I] Pointer to an LDAP context.
299 * timeout [I] Pointer to an l_timeval structure specifying the
300 * timeout in seconds.
302 * RETURNS
303 * Success: LDAP_SUCCESS
304 * Failure: An LDAP error code.
306 * NOTES
307 * The timeout parameter may be NULL in which case a default timeout
308 * value will be used.
310 ULONG CDECL ldap_connect( WLDAP32_LDAP *ld, struct l_timeval *timeout )
312 TRACE( "(%p, %p)\n", ld, timeout );
314 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
315 return WLDAP32_LDAP_SUCCESS; /* FIXME: do something, e.g. ping the host */
318 /***********************************************************************
319 * ldap_initA (WLDAP32.@)
321 * See ldap_initW.
323 WLDAP32_LDAP * CDECL ldap_initA( PCHAR hostname, ULONG portnumber )
325 #ifdef HAVE_LDAP
326 WLDAP32_LDAP *ld = NULL;
327 WCHAR *hostnameW = NULL;
329 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
331 if (hostname) {
332 hostnameW = strAtoW( hostname );
333 if (!hostnameW) goto exit;
336 ld = ldap_initW( hostnameW, portnumber );
338 exit:
339 strfreeW( hostnameW );
340 return ld;
342 #else
343 return NULL;
344 #endif
347 /***********************************************************************
348 * ldap_initW (WLDAP32.@)
350 * Initialize an LDAP context and create a TCP connection.
352 * PARAMS
353 * hostname [I] Name of the host to connect to.
354 * portnumber [I] Port number to use.
356 * RETURNS
357 * Success: Pointer to an LDAP context.
358 * Failure: NULL
360 * NOTES
361 * The hostname string can be a space separated string of hostnames,
362 * in which case the LDAP runtime will try to connect to the hosts
363 * in order, until a connection can be made. A hostname may have a
364 * trailing port number (separated from the hostname by a ':'), which
365 * will take precedence over the port number supplied as a parameter
366 * to this function. The connection will not be made until the first
367 * LDAP function that needs it is called.
369 WLDAP32_LDAP * CDECL ldap_initW( PWCHAR hostname, ULONG portnumber )
371 #ifdef HAVE_LDAP
372 LDAP *ld = NULL;
373 char *hostnameU = NULL, *url = NULL;
375 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
377 if (hostname) {
378 hostnameU = strWtoU( hostname );
379 if (!hostnameU) goto exit;
381 else {
382 hostnameU = strWtoU( defaulthost );
383 if (!hostnameU) goto exit;
386 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
387 if (!url) goto exit;
389 ldap_initialize( &ld, url );
391 exit:
392 strfreeU( hostnameU );
393 strfreeU( url );
394 return ld;
396 #else
397 return NULL;
398 #endif
401 /***********************************************************************
402 * ldap_openA (WLDAP32.@)
404 * See ldap_openW.
406 WLDAP32_LDAP * CDECL ldap_openA( PCHAR hostname, ULONG portnumber )
408 #ifdef HAVE_LDAP
409 WLDAP32_LDAP *ld = NULL;
410 WCHAR *hostnameW = NULL;
412 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
414 if (hostname) {
415 hostnameW = strAtoW( hostname );
416 if (!hostnameW) goto exit;
419 ld = ldap_openW( hostnameW, portnumber );
421 exit:
422 strfreeW( hostnameW );
423 return ld;
425 #else
426 return NULL;
427 #endif
430 /***********************************************************************
431 * ldap_openW (WLDAP32.@)
433 * Initialize an LDAP context and create a TCP connection.
435 * PARAMS
436 * hostname [I] Name of the host to connect to.
437 * portnumber [I] Port number to use.
439 * RETURNS
440 * Success: Pointer to an LDAP context.
441 * Failure: NULL
443 * NOTES
444 * The hostname string can be a space separated string of hostnames,
445 * in which case the LDAP runtime will try to connect to the hosts
446 * in order, until a connection can be made. A hostname may have a
447 * trailing port number (separated from the hostname by a ':'), which
448 * will take precedence over the port number supplied as a parameter
449 * to this function.
451 WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
453 #ifdef HAVE_LDAP
454 LDAP *ld = NULL;
455 char *hostnameU = NULL, *url = NULL;
457 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
459 if (hostname) {
460 hostnameU = strWtoU( hostname );
461 if (!hostnameU) goto exit;
463 else {
464 hostnameU = strWtoU( defaulthost );
465 if (!hostnameU) goto exit;
468 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
469 if (!url) goto exit;
471 ldap_initialize( &ld, url );
473 exit:
474 strfreeU( hostnameU );
475 strfreeU( url );
476 return ld;
478 #else
479 return NULL;
480 #endif
483 /***********************************************************************
484 * ldap_sslinitA (WLDAP32.@)
486 * See ldap_sslinitW.
488 WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure )
490 #ifdef HAVE_LDAP
491 WLDAP32_LDAP *ld;
492 WCHAR *hostnameW = NULL;
494 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
496 if (hostname) {
497 hostnameW = strAtoW( hostname );
498 if (!hostnameW) return NULL;
501 ld = ldap_sslinitW( hostnameW, portnumber, secure );
503 strfreeW( hostnameW );
504 return ld;
506 #else
507 return NULL;
508 #endif
511 /***********************************************************************
512 * ldap_sslinitW (WLDAP32.@)
514 * Initialize an LDAP context and create a secure TCP connection.
516 * PARAMS
517 * hostname [I] Name of the host to connect to.
518 * portnumber [I] Port number to use.
519 * secure [I] Ask the server to create an SSL connection.
521 * RETURNS
522 * Success: Pointer to an LDAP context.
523 * Failure: NULL
525 * NOTES
526 * The hostname string can be a space separated string of hostnames,
527 * in which case the LDAP runtime will try to connect to the hosts
528 * in order, until a connection can be made. A hostname may have a
529 * trailing port number (separated from the hostname by a ':'), which
530 * will take precedence over the port number supplied as a parameter
531 * to this function. The connection will not be made until the first
532 * LDAP function that needs it is called.
534 WLDAP32_LDAP * CDECL ldap_sslinitW( PWCHAR hostname, ULONG portnumber, int secure )
536 #ifdef HAVE_LDAP
537 WLDAP32_LDAP *ld = NULL;
538 char *hostnameU = NULL, *url = NULL;
540 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
542 if (hostname) {
543 hostnameU = strWtoU( hostname );
544 if (!hostnameU) goto exit;
546 else {
547 hostnameU = strWtoU( defaulthost );
548 if (!hostnameU) goto exit;
551 if (secure)
552 url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
553 else
554 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
556 if (!url) goto exit;
557 ldap_initialize( &ld, url );
559 exit:
560 strfreeU( hostnameU );
561 strfreeU( url );
562 return ld;
564 #else
565 return NULL;
566 #endif
569 /***********************************************************************
570 * ldap_start_tls_sA (WLDAP32.@)
572 * See ldap_start_tls_sW.
574 ULONG CDECL ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
575 PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
577 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
578 #ifdef HAVE_LDAP
579 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
581 ret = WLDAP32_LDAP_NO_MEMORY;
583 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
585 if (!ld) return ~0u;
587 if (serverctrls) {
588 serverctrlsW = controlarrayAtoW( serverctrls );
589 if (!serverctrlsW) goto exit;
591 if (clientctrls) {
592 clientctrlsW = controlarrayAtoW( clientctrls );
593 if (!clientctrlsW) goto exit;
596 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
598 exit:
599 controlarrayfreeW( serverctrlsW );
600 controlarrayfreeW( clientctrlsW );
602 #endif
603 return ret;
606 /***********************************************************************
607 * ldap_start_tls_s (WLDAP32.@)
609 * Start TLS encryption on an LDAP connection.
611 * PARAMS
612 * ld [I] Pointer to an LDAP context.
613 * retval [I] Return value from the server.
614 * result [I] Response message from the server.
615 * serverctrls [I] Array of LDAP server controls.
616 * clientctrls [I] Array of LDAP client controls.
618 * RETURNS
619 * Success: LDAP_SUCCESS
620 * Failure: An LDAP error code.
622 * NOTES
623 * LDAP function that needs it is called.
625 ULONG CDECL ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
626 PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
628 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
629 #ifdef HAVE_LDAP
630 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
632 ret = WLDAP32_LDAP_NO_MEMORY;
634 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
636 if (!ld) return ~0u;
638 if (serverctrls) {
639 serverctrlsU = controlarrayWtoU( serverctrls );
640 if (!serverctrlsU) goto exit;
642 if (clientctrls) {
643 clientctrlsU = controlarrayWtoU( clientctrls );
644 if (!clientctrlsU) goto exit;
647 ret = map_error( ldap_start_tls_s( ld, serverctrlsU, clientctrlsU ));
649 exit:
650 controlarrayfreeU( serverctrlsU );
651 controlarrayfreeU( clientctrlsU );
653 #endif
654 return ret;
657 /***********************************************************************
658 * ldap_startup (WLDAP32.@)
660 ULONG CDECL ldap_startup( PLDAP_VERSION_INFO version, HANDLE *instance )
662 TRACE( "(%p, %p)\n", version, instance );
663 return WLDAP32_LDAP_SUCCESS;
666 /***********************************************************************
667 * ldap_stop_tls_s (WLDAP32.@)
669 * Stop TLS encryption on an LDAP connection.
671 * PARAMS
672 * ld [I] Pointer to an LDAP context.
674 * RETURNS
675 * Success: TRUE
676 * Failure: FALSE
678 BOOLEAN CDECL ldap_stop_tls_s( WLDAP32_LDAP *ld )
680 TRACE( "(%p)\n", ld );
681 return TRUE; /* FIXME: find a way to stop tls on a connection */