winedbg: Set reuseaddr flag for gdb remote socket.
[wine.git] / dlls / wldap32 / init.c
blobe62fd2fe79462bc9b6d3a154d6207127d0a05163
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 <stdarg.h>
22 #include <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winternl.h"
27 #include "winldap.h"
29 #include "wine/debug.h"
30 #include "winldap_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
34 /* Split a space separated string of hostnames into a string array */
35 static char **split_hostnames( const char *hostnames )
37 char **res, *str, *p, *q;
38 unsigned int i = 0;
40 str = strdupU( hostnames );
41 if (!str) return NULL;
43 p = str;
44 while (isspace( *p )) p++;
45 if (*p) i++;
47 while (*p)
49 if (isspace( *p ))
51 while (isspace( *p )) p++;
52 if (*p) i++;
54 p++;
57 if (!(res = malloc( (i + 1) * sizeof(char *) )))
59 free( str );
60 return NULL;
63 p = str;
64 while (isspace( *p )) p++;
66 q = p;
67 i = 0;
69 while (*p)
71 if (p[1] != '\0')
73 if (isspace( *p ))
75 *p = '\0'; p++;
76 res[i] = strdupU( q );
77 if (!res[i]) goto oom;
78 i++;
80 while (isspace( *p )) p++;
81 q = p;
84 else
86 res[i] = strdupU( q );
87 if (!res[i]) goto oom;
88 i++;
90 p++;
92 res[i] = NULL;
94 free( str );
95 return res;
97 oom:
98 while (i > 0) free( res[--i] );
99 free( res );
100 free( str );
101 return NULL;
104 /* Determine if a URL starts with a known LDAP scheme */
105 static BOOL has_ldap_scheme( char *url )
107 return !_strnicmp( url, "ldap://", 7 ) ||
108 !_strnicmp( url, "ldaps://", 8 ) ||
109 !_strnicmp( url, "ldapi://", 8 ) ||
110 !_strnicmp( url, "cldap://", 8 );
113 /* Flatten an array of hostnames into a space separated string of URLs.
114 * Prepend a given scheme and append a given port number to each hostname
115 * if necessary.
117 static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnumber )
119 char *res, *p, *q, **v;
120 unsigned int i = 0, size = 0;
121 static const char sep[] = " ", fmt[] = ":%d";
122 char port[7];
124 sprintf( port, fmt, portnumber );
126 for (v = hostnames; *v; v++)
128 if (!has_ldap_scheme( *v ))
130 size += strlen( scheme );
131 q = *v;
133 else
134 /* skip past colon in scheme prefix */
135 q = strchr( *v, '/' );
137 size += strlen( *v );
139 if (!strchr( q, ':' ))
140 size += strlen( port );
142 i++;
145 size += (i - 1) * strlen( sep );
146 if (!(res = malloc( size + 1 ))) return NULL;
148 p = res;
149 for (v = hostnames; *v; v++)
151 if (v != hostnames)
153 strcpy( p, sep );
154 p += strlen( sep );
157 if (!has_ldap_scheme( *v ))
159 strcpy( p, scheme );
160 p += strlen( scheme );
161 q = *v;
163 else
164 /* skip past colon in scheme prefix */
165 q = strchr( *v, '/' );
167 strcpy( p, *v );
168 p += strlen( *v );
170 if (!strchr( q, ':' ))
172 strcpy( p, port );
173 p += strlen( port );
176 return res;
179 static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
181 char *url = NULL, **strarray;
183 strarray = split_hostnames( hostnames );
184 if (strarray)
185 url = join_hostnames( scheme, strarray, port );
186 else
187 return NULL;
189 strarrayfreeU( strarray );
190 return url;
194 static LDAP *create_context( const char *url )
196 LDAP *ld;
197 int version = LDAP_VERSION3;
198 struct ldap_initialize_params params;
200 if (!(ld = calloc( 1, sizeof( *ld )))) return NULL;
201 params.ld = &CTX(ld);
202 params.url = url;
203 if (map_error( LDAP_CALL( ldap_initialize, &params )) == LDAP_SUCCESS)
205 struct ldap_set_option_params opt_params = { CTX(ld), LDAP_OPT_PROTOCOL_VERSION, &version };
206 LDAP_CALL( ldap_set_option, &opt_params );
207 return ld;
209 free( ld );
210 return NULL;
213 /***********************************************************************
214 * cldap_openA (WLDAP32.@)
216 * See cldap_openW.
218 LDAP * CDECL cldap_openA( char *hostname, ULONG portnumber )
220 LDAP *ld;
221 WCHAR *hostnameW = NULL;
223 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
225 if (hostname && !(hostnameW = strAtoW( hostname ))) return NULL;
227 ld = cldap_openW( hostnameW, portnumber );
229 free( hostnameW );
230 return ld;
233 /***********************************************************************
234 * cldap_openW (WLDAP32.@)
236 * Initialize an LDAP context and create a UDP connection.
238 * PARAMS
239 * hostname [I] Name of the host to connect to.
240 * portnumber [I] Port number to use.
242 * RETURNS
243 * Success: Pointer to an LDAP context.
244 * Failure: NULL
246 * NOTES
247 * The hostname string can be a space separated string of hostnames,
248 * in which case the LDAP runtime will try to connect to the hosts
249 * in order, until a connection can be made. A hostname may have a
250 * trailing port number (separated from the hostname by a ':'), which
251 * will take precedence over the port number supplied as a parameter
252 * to this function.
254 LDAP * CDECL cldap_openW( WCHAR *hostname, ULONG portnumber )
256 LDAP *ld = NULL;
257 char *hostnameU, *url = NULL;
259 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
261 if (!(hostnameU = strWtoU( hostname ? hostname : L"localhost" ))) return NULL;
262 if (!(url = urlify_hostnames( "cldap://", hostnameU, portnumber ))) goto exit;
264 ld = create_context( url );
266 exit:
267 free( hostnameU );
268 free( url );
269 return ld;
272 /***********************************************************************
273 * ldap_connect (WLDAP32.@)
275 * Connect to an LDAP server.
277 * PARAMS
278 * ld [I] Pointer to an LDAP context.
279 * timeout [I] Pointer to an l_timeval structure specifying the
280 * timeout in seconds.
282 * RETURNS
283 * Success: LDAP_SUCCESS
284 * Failure: An LDAP error code.
286 * NOTES
287 * The timeout parameter may be NULL in which case a default timeout
288 * value will be used.
290 ULONG CDECL ldap_connect( LDAP *ld, struct l_timeval *timeout )
292 TRACE( "(%p, %p)\n", ld, timeout );
294 if (!ld) return LDAP_PARAM_ERROR;
295 return LDAP_SUCCESS; /* FIXME: do something, e.g. ping the host */
298 /***********************************************************************
299 * ldap_initA (WLDAP32.@)
301 * See ldap_initW.
303 LDAP * CDECL ldap_initA( const PCHAR hostname, ULONG portnumber )
305 LDAP *ld;
306 WCHAR *hostnameW = NULL;
308 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
310 if (hostname && !(hostnameW = strAtoW( hostname ))) return NULL;
312 ld = ldap_initW( hostnameW, portnumber );
314 free( hostnameW );
315 return ld;
318 /***********************************************************************
319 * ldap_initW (WLDAP32.@)
321 * Initialize an LDAP context and create a TCP connection.
323 * PARAMS
324 * hostname [I] Name of the host to connect to.
325 * portnumber [I] Port number to use.
327 * RETURNS
328 * Success: Pointer to an LDAP context.
329 * Failure: NULL
331 * NOTES
332 * The hostname string can be a space separated string of hostnames,
333 * in which case the LDAP runtime will try to connect to the hosts
334 * in order, until a connection can be made. A hostname may have a
335 * trailing port number (separated from the hostname by a ':'), which
336 * will take precedence over the port number supplied as a parameter
337 * to this function. The connection will not be made until the first
338 * LDAP function that needs it is called.
340 LDAP * CDECL ldap_initW( const PWCHAR hostname, ULONG portnumber )
342 LDAP *ld = NULL;
343 char *hostnameU, *url = NULL;
345 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
347 if (!(hostnameU = strWtoU( hostname ? hostname : L"localhost" ))) return NULL;
348 if (!(url = urlify_hostnames( "ldap://", hostnameU, portnumber ))) goto exit;
350 ld = create_context( url );
352 exit:
353 free( hostnameU );
354 free( url );
355 return ld;
358 /***********************************************************************
359 * ldap_openA (WLDAP32.@)
361 * See ldap_openW.
363 LDAP * CDECL ldap_openA( char *hostname, ULONG portnumber )
365 LDAP *ld;
366 WCHAR *hostnameW = NULL;
368 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
370 if (hostname && !(hostnameW = strAtoW( hostname ))) return NULL;
372 ld = ldap_openW( hostnameW, portnumber );
374 free( hostnameW );
375 return ld;
378 /***********************************************************************
379 * ldap_openW (WLDAP32.@)
381 * Initialize an LDAP context and create a TCP connection.
383 * PARAMS
384 * hostname [I] Name of the host to connect to.
385 * portnumber [I] Port number to use.
387 * RETURNS
388 * Success: Pointer to an LDAP context.
389 * Failure: NULL
391 * NOTES
392 * The hostname string can be a space separated string of hostnames,
393 * in which case the LDAP runtime will try to connect to the hosts
394 * in order, until a connection can be made. A hostname may have a
395 * trailing port number (separated from the hostname by a ':'), which
396 * will take precedence over the port number supplied as a parameter
397 * to this function.
399 LDAP * CDECL ldap_openW( WCHAR *hostname, ULONG portnumber )
401 LDAP *ld = NULL;
402 char *hostnameU, *url = NULL;
404 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
406 if (!(hostnameU = strWtoU( hostname ? hostname : L"localhost" ))) return NULL;
407 if (!(url = urlify_hostnames( "ldap://", hostnameU, portnumber ))) goto exit;
409 ld = create_context( url );
411 exit:
412 free( hostnameU );
413 free( url );
414 return ld;
417 /***********************************************************************
418 * ldap_sslinitA (WLDAP32.@)
420 * See ldap_sslinitW.
422 LDAP * CDECL ldap_sslinitA( char *hostname, ULONG portnumber, int secure )
424 LDAP *ld;
425 WCHAR *hostnameW = NULL;
427 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
429 if (hostname && !(hostnameW = strAtoW( hostname ))) return NULL;
431 ld = ldap_sslinitW( hostnameW, portnumber, secure );
433 free( hostnameW );
434 return ld;
437 /***********************************************************************
438 * ldap_sslinitW (WLDAP32.@)
440 * Initialize an LDAP context and create a secure TCP connection.
442 * PARAMS
443 * hostname [I] Name of the host to connect to.
444 * portnumber [I] Port number to use.
445 * secure [I] Ask the server to create an SSL connection.
447 * RETURNS
448 * Success: Pointer to an LDAP context.
449 * Failure: NULL
451 * NOTES
452 * The hostname string can be a space separated string of hostnames,
453 * in which case the LDAP runtime will try to connect to the hosts
454 * in order, until a connection can be made. A hostname may have a
455 * trailing port number (separated from the hostname by a ':'), which
456 * will take precedence over the port number supplied as a parameter
457 * to this function. The connection will not be made until the first
458 * LDAP function that needs it is called.
460 LDAP * CDECL ldap_sslinitW( WCHAR *hostname, ULONG portnumber, int secure )
462 LDAP *ld = NULL;
463 char *hostnameU, *url = NULL;
465 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
467 if (!(hostnameU = strWtoU( hostname ? hostname : L"localhost" ))) return NULL;
469 if (secure)
470 url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
471 else
472 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
473 if (!url) goto exit;
475 ld = create_context( url );
477 exit:
478 free( hostnameU );
479 free( url );
480 return ld;
483 /***********************************************************************
484 * ldap_start_tls_sA (WLDAP32.@)
486 * See ldap_start_tls_sW.
488 ULONG CDECL ldap_start_tls_sA( LDAP *ld, ULONG *retval, LDAPMessage **result, LDAPControlA **serverctrls,
489 LDAPControlA **clientctrls )
491 ULONG ret = LDAP_NO_MEMORY;
492 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
494 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
496 if (!ld) return ~0u;
498 if (serverctrls && !(serverctrlsW = controlarrayAtoW( serverctrls ))) goto exit;
499 if (clientctrls && !(clientctrlsW = controlarrayAtoW( clientctrls ))) goto exit;
501 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
503 exit:
504 controlarrayfreeW( serverctrlsW );
505 controlarrayfreeW( clientctrlsW );
506 return ret;
509 /***********************************************************************
510 * ldap_start_tls_s (WLDAP32.@)
512 * Start TLS encryption on an LDAP connection.
514 * PARAMS
515 * ld [I] Pointer to an LDAP context.
516 * retval [I] Return value from the server.
517 * result [I] Response message from the server.
518 * serverctrls [I] Array of LDAP server controls.
519 * clientctrls [I] Array of LDAP client controls.
521 * RETURNS
522 * Success: LDAP_SUCCESS
523 * Failure: An LDAP error code.
525 * NOTES
526 * LDAP function that needs it is called.
528 ULONG CDECL ldap_start_tls_sW( LDAP *ld, ULONG *retval, LDAPMessage **result, LDAPControlW **serverctrls,
529 LDAPControlW **clientctrls )
531 ULONG ret = LDAP_NO_MEMORY;
532 LDAPControlU **serverctrlsU = NULL, **clientctrlsU = NULL;
534 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
535 if (result)
537 FIXME( "result message not supported\n" );
538 *result = NULL;
541 if (!ld) return ~0u;
543 if (serverctrls && !(serverctrlsU = controlarrayWtoU( serverctrls ))) goto exit;
544 if (clientctrls && !(clientctrlsU = controlarrayWtoU( clientctrls ))) goto exit;
545 else
547 struct ldap_start_tls_s_params params = { CTX(ld), serverctrlsU, clientctrlsU };
548 ret = map_error( LDAP_CALL( ldap_start_tls_s, &params ));
551 exit:
552 controlarrayfreeU( serverctrlsU );
553 controlarrayfreeU( clientctrlsU );
554 return ret;
557 /***********************************************************************
558 * ldap_startup (WLDAP32.@)
560 ULONG CDECL ldap_startup( LDAP_VERSION_INFO *version, HANDLE *instance )
562 TRACE( "(%p, %p)\n", version, instance );
563 return LDAP_SUCCESS;
566 /***********************************************************************
567 * ldap_stop_tls_s (WLDAP32.@)
569 * Stop TLS encryption on an LDAP connection.
571 * PARAMS
572 * ld [I] Pointer to an LDAP context.
574 * RETURNS
575 * Success: TRUE
576 * Failure: FALSE
578 BOOLEAN CDECL ldap_stop_tls_s( LDAP *ld )
580 TRACE( "(%p)\n", ld );
581 return TRUE; /* FIXME: find a way to stop tls on a connection */