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
23 #include "wine/port.h"
24 #include "wine/debug.h"
37 #include "winldap_private.h"
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;
50 str = strdupU( hostnames );
51 if (!str) return NULL;
54 while (isspace( *p )) p++;
61 while (isspace( *p )) p++;
67 res = HeapAlloc( GetProcessHeap(), 0, (i + 1) * sizeof(char *) );
70 HeapFree( GetProcessHeap(), 0, str );
75 while (isspace( *p )) p++;
87 res[i] = strdupU( q );
88 if (!res[i]) goto oom;
91 while (isspace( *p )) p++;
97 res[i] = strdupU( q );
98 if (!res[i]) goto oom;
105 HeapFree( GetProcessHeap(), 0, str );
109 while (i > 0) strfreeU( res[--i] );
111 HeapFree( GetProcessHeap(), 0, res );
112 HeapFree( GetProcessHeap(), 0, str );
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;
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
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";
138 sprintf( port, fmt, portnumber );
140 for (v = hostnames; *v; v++)
142 if (!has_ldap_scheme( *v ))
144 size += strlen( scheme );
148 /* skip past colon in scheme prefix */
149 q = strchr( *v, '/' );
151 size += strlen( *v );
153 if (!strchr( q, ':' ))
154 size += strlen( port );
159 size += (i - 1) * strlen( sep );
161 res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
162 if (!res) return NULL;
165 for (v = hostnames; *v; v++)
173 if (!has_ldap_scheme( *v ))
176 p += strlen( scheme );
180 /* skip past colon in scheme prefix */
181 q = strchr( *v, '/' );
186 if (!strchr( q, ':' ))
195 static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
197 char *url = NULL, **strarray;
199 strarray = split_hostnames( hostnames );
201 url = join_hostnames( scheme, strarray, port );
205 strarrayfreeU( strarray );
210 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
212 /***********************************************************************
213 * cldap_openA (WLDAP32.@)
217 WLDAP32_LDAP * CDECL cldap_openA( PCHAR hostname, ULONG portnumber )
220 WLDAP32_LDAP *ld = NULL;
221 WCHAR *hostnameW = NULL;
223 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
226 hostnameW = strAtoW( hostname );
227 if (!hostnameW) goto exit;
230 ld = cldap_openW( hostnameW, portnumber );
233 strfreeW( hostnameW );
241 /***********************************************************************
242 * cldap_openW (WLDAP32.@)
244 * Initialize an LDAP context and create a UDP connection.
247 * hostname [I] Name of the host to connect to.
248 * portnumber [I] Portnumber to use.
251 * Success: Pointer to an LDAP context.
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
262 WLDAP32_LDAP * CDECL cldap_openW( PWCHAR hostname, ULONG portnumber )
266 char *hostnameU = NULL, *url = NULL;
268 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
271 hostnameU = strWtoU( hostname );
272 if (!hostnameU) goto exit;
275 hostnameU = strWtoU( defaulthost );
276 if (!hostnameU) goto exit;
279 url = urlify_hostnames( "cldap://", hostnameU, portnumber );
282 ldap_initialize( &ld, url );
285 strfreeU( hostnameU );
294 /***********************************************************************
295 * ldap_connect (WLDAP32.@)
297 * Connect to an LDAP server.
300 * ld [I] Pointer to an LDAP context.
301 * timeout [I] Pointer to an l_timeval structure specifying the
302 * timeout in seconds.
305 * Success: LDAP_SUCCESS
306 * Failure: An LDAP error code.
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.@)
325 WLDAP32_LDAP * CDECL ldap_initA( PCHAR hostname, ULONG portnumber )
328 WLDAP32_LDAP *ld = NULL;
329 WCHAR *hostnameW = NULL;
331 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
334 hostnameW = strAtoW( hostname );
335 if (!hostnameW) goto exit;
338 ld = ldap_initW( hostnameW, portnumber );
341 strfreeW( hostnameW );
349 /***********************************************************************
350 * ldap_initW (WLDAP32.@)
352 * Initialize an LDAP context and create a TCP connection.
355 * hostname [I] Name of the host to connect to.
356 * portnumber [I] Portnumber to use.
359 * Success: Pointer to an LDAP context.
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 )
375 char *hostnameU = NULL, *url = NULL;
377 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
380 hostnameU = strWtoU( hostname );
381 if (!hostnameU) goto exit;
384 hostnameU = strWtoU( defaulthost );
385 if (!hostnameU) goto exit;
388 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
391 ldap_initialize( &ld, url );
394 strfreeU( hostnameU );
403 /***********************************************************************
404 * ldap_openA (WLDAP32.@)
408 WLDAP32_LDAP * CDECL ldap_openA( PCHAR hostname, ULONG portnumber )
411 WLDAP32_LDAP *ld = NULL;
412 WCHAR *hostnameW = NULL;
414 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
417 hostnameW = strAtoW( hostname );
418 if (!hostnameW) goto exit;
421 ld = ldap_openW( hostnameW, portnumber );
424 strfreeW( hostnameW );
432 /***********************************************************************
433 * ldap_openW (WLDAP32.@)
435 * Initialize an LDAP context and create a TCP connection.
438 * hostname [I] Name of the host to connect to.
439 * portnumber [I] Portnumber to use.
442 * Success: Pointer to an LDAP context.
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
453 WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
457 char *hostnameU = NULL, *url = NULL;
459 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
462 hostnameU = strWtoU( hostname );
463 if (!hostnameU) goto exit;
466 hostnameU = strWtoU( defaulthost );
467 if (!hostnameU) goto exit;
470 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
473 ldap_initialize( &ld, url );
476 strfreeU( hostnameU );
485 /***********************************************************************
486 * ldap_sslinitA (WLDAP32.@)
490 WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure )
494 WCHAR *hostnameW = NULL;
496 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
499 hostnameW = strAtoW( hostname );
500 if (!hostnameW) return NULL;
503 ld = ldap_sslinitW( hostnameW, portnumber, secure );
505 strfreeW( hostnameW );
513 /***********************************************************************
514 * ldap_sslinitW (WLDAP32.@)
516 * Initialize an LDAP context and create a secure TCP connection.
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.
524 * Success: Pointer to an LDAP context.
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 )
539 WLDAP32_LDAP *ld = NULL;
540 char *hostnameU = NULL, *url = NULL;
542 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
545 hostnameU = strWtoU( hostname );
546 if (!hostnameU) goto exit;
549 hostnameU = strWtoU( defaulthost );
550 if (!hostnameU) goto exit;
554 url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
556 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
559 ldap_initialize( &ld, url );
562 strfreeU( hostnameU );
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;
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 );
590 serverctrlsW = controlarrayAtoW( serverctrls );
591 if (!serverctrlsW) goto exit;
594 clientctrlsW = controlarrayAtoW( clientctrls );
595 if (!clientctrlsW) goto exit;
598 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
601 controlarrayfreeW( serverctrlsW );
602 controlarrayfreeW( clientctrlsW );
608 /***********************************************************************
609 * ldap_start_tls_s (WLDAP32.@)
611 * Start TLS encryption on an LDAP connection.
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.
621 * Success: LDAP_SUCCESS
622 * Failure: An LDAP error code.
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;
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 );
641 serverctrlsU = controlarrayWtoU( serverctrls );
642 if (!serverctrlsU) goto exit;
645 clientctrlsU = controlarrayWtoU( clientctrls );
646 if (!clientctrlsU) goto exit;
649 ret = map_error( ldap_start_tls_s( ld, serverctrlsU, clientctrlsU ));
652 controlarrayfreeU( serverctrlsU );
653 controlarrayfreeU( clientctrlsU );
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.
674 * ld [I] Pointer to an LDAP context.
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 */