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
22 #include "wine/port.h"
34 #include "winldap_private.h"
36 #include "wine/debug.h"
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
;
48 str
= strdupU( hostnames
);
49 if (!str
) return NULL
;
52 while (isspace( *p
)) p
++;
59 while (isspace( *p
)) p
++;
65 res
= HeapAlloc( GetProcessHeap(), 0, (i
+ 1) * sizeof(char *) );
68 HeapFree( GetProcessHeap(), 0, str
);
73 while (isspace( *p
)) p
++;
85 res
[i
] = strdupU( q
);
86 if (!res
[i
]) goto oom
;
89 while (isspace( *p
)) p
++;
95 res
[i
] = strdupU( q
);
96 if (!res
[i
]) goto oom
;
103 HeapFree( GetProcessHeap(), 0, str
);
107 while (i
> 0) strfreeU( res
[--i
] );
109 HeapFree( GetProcessHeap(), 0, res
);
110 HeapFree( GetProcessHeap(), 0, str
);
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;
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
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";
136 sprintf( port
, fmt
, portnumber
);
138 for (v
= hostnames
; *v
; v
++)
140 if (!has_ldap_scheme( *v
))
142 size
+= strlen( scheme
);
146 /* skip past colon in scheme prefix */
147 q
= strchr( *v
, '/' );
149 size
+= strlen( *v
);
151 if (!strchr( q
, ':' ))
152 size
+= strlen( port
);
157 size
+= (i
- 1) * strlen( sep
);
159 res
= HeapAlloc( GetProcessHeap(), 0, size
+ 1 );
160 if (!res
) return NULL
;
163 for (v
= hostnames
; *v
; v
++)
171 if (!has_ldap_scheme( *v
))
174 p
+= strlen( scheme
);
178 /* skip past colon in scheme prefix */
179 q
= strchr( *v
, '/' );
184 if (!strchr( q
, ':' ))
193 static char *urlify_hostnames( const char *scheme
, char *hostnames
, ULONG port
)
195 char *url
= NULL
, **strarray
;
197 strarray
= split_hostnames( hostnames
);
199 url
= join_hostnames( scheme
, strarray
, port
);
203 strarrayfreeU( strarray
);
208 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
210 /***********************************************************************
211 * cldap_openA (WLDAP32.@)
215 WLDAP32_LDAP
* CDECL
cldap_openA( PCHAR hostname
, ULONG portnumber
)
218 WLDAP32_LDAP
*ld
= NULL
;
219 WCHAR
*hostnameW
= NULL
;
221 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
224 hostnameW
= strAtoW( hostname
);
225 if (!hostnameW
) goto exit
;
228 ld
= cldap_openW( hostnameW
, portnumber
);
231 strfreeW( hostnameW
);
239 /***********************************************************************
240 * cldap_openW (WLDAP32.@)
242 * Initialize an LDAP context and create a UDP connection.
245 * hostname [I] Name of the host to connect to.
246 * portnumber [I] Port number to use.
249 * Success: Pointer to an LDAP context.
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
260 WLDAP32_LDAP
* CDECL
cldap_openW( PWCHAR hostname
, ULONG portnumber
)
264 char *hostnameU
= NULL
, *url
= NULL
;
266 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
269 hostnameU
= strWtoU( hostname
);
270 if (!hostnameU
) goto exit
;
273 hostnameU
= strWtoU( defaulthost
);
274 if (!hostnameU
) goto exit
;
277 url
= urlify_hostnames( "cldap://", hostnameU
, portnumber
);
280 ldap_initialize( &ld
, url
);
283 strfreeU( hostnameU
);
292 /***********************************************************************
293 * ldap_connect (WLDAP32.@)
295 * Connect to an LDAP server.
298 * ld [I] Pointer to an LDAP context.
299 * timeout [I] Pointer to an l_timeval structure specifying the
300 * timeout in seconds.
303 * Success: LDAP_SUCCESS
304 * Failure: An LDAP error code.
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.@)
323 WLDAP32_LDAP
* CDECL
ldap_initA( PCHAR hostname
, ULONG portnumber
)
326 WLDAP32_LDAP
*ld
= NULL
;
327 WCHAR
*hostnameW
= NULL
;
329 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
332 hostnameW
= strAtoW( hostname
);
333 if (!hostnameW
) goto exit
;
336 ld
= ldap_initW( hostnameW
, portnumber
);
339 strfreeW( hostnameW
);
347 /***********************************************************************
348 * ldap_initW (WLDAP32.@)
350 * Initialize an LDAP context and create a TCP connection.
353 * hostname [I] Name of the host to connect to.
354 * portnumber [I] Port number to use.
357 * Success: Pointer to an LDAP context.
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
)
373 char *hostnameU
= NULL
, *url
= NULL
;
375 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
378 hostnameU
= strWtoU( hostname
);
379 if (!hostnameU
) goto exit
;
382 hostnameU
= strWtoU( defaulthost
);
383 if (!hostnameU
) goto exit
;
386 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
389 ldap_initialize( &ld
, url
);
392 strfreeU( hostnameU
);
401 /***********************************************************************
402 * ldap_openA (WLDAP32.@)
406 WLDAP32_LDAP
* CDECL
ldap_openA( PCHAR hostname
, ULONG portnumber
)
409 WLDAP32_LDAP
*ld
= NULL
;
410 WCHAR
*hostnameW
= NULL
;
412 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
415 hostnameW
= strAtoW( hostname
);
416 if (!hostnameW
) goto exit
;
419 ld
= ldap_openW( hostnameW
, portnumber
);
422 strfreeW( hostnameW
);
430 /***********************************************************************
431 * ldap_openW (WLDAP32.@)
433 * Initialize an LDAP context and create a TCP connection.
436 * hostname [I] Name of the host to connect to.
437 * portnumber [I] Port number to use.
440 * Success: Pointer to an LDAP context.
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
451 WLDAP32_LDAP
* CDECL
ldap_openW( PWCHAR hostname
, ULONG portnumber
)
455 char *hostnameU
= NULL
, *url
= NULL
;
457 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
460 hostnameU
= strWtoU( hostname
);
461 if (!hostnameU
) goto exit
;
464 hostnameU
= strWtoU( defaulthost
);
465 if (!hostnameU
) goto exit
;
468 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
471 ldap_initialize( &ld
, url
);
474 strfreeU( hostnameU
);
483 /***********************************************************************
484 * ldap_sslinitA (WLDAP32.@)
488 WLDAP32_LDAP
* CDECL
ldap_sslinitA( PCHAR hostname
, ULONG portnumber
, int secure
)
492 WCHAR
*hostnameW
= NULL
;
494 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname
), portnumber
, secure
);
497 hostnameW
= strAtoW( hostname
);
498 if (!hostnameW
) return NULL
;
501 ld
= ldap_sslinitW( hostnameW
, portnumber
, secure
);
503 strfreeW( hostnameW
);
511 /***********************************************************************
512 * ldap_sslinitW (WLDAP32.@)
514 * Initialize an LDAP context and create a secure TCP connection.
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.
522 * Success: Pointer to an LDAP context.
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
)
537 WLDAP32_LDAP
*ld
= NULL
;
538 char *hostnameU
= NULL
, *url
= NULL
;
540 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname
), portnumber
, secure
);
543 hostnameU
= strWtoU( hostname
);
544 if (!hostnameU
) goto exit
;
547 hostnameU
= strWtoU( defaulthost
);
548 if (!hostnameU
) goto exit
;
552 url
= urlify_hostnames( "ldaps://", hostnameU
, portnumber
);
554 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
557 ldap_initialize( &ld
, url
);
560 strfreeU( hostnameU
);
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
;
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
);
588 serverctrlsW
= controlarrayAtoW( serverctrls
);
589 if (!serverctrlsW
) goto exit
;
592 clientctrlsW
= controlarrayAtoW( clientctrls
);
593 if (!clientctrlsW
) goto exit
;
596 ret
= ldap_start_tls_sW( ld
, retval
, result
, serverctrlsW
, clientctrlsW
);
599 controlarrayfreeW( serverctrlsW
);
600 controlarrayfreeW( clientctrlsW
);
606 /***********************************************************************
607 * ldap_start_tls_s (WLDAP32.@)
609 * Start TLS encryption on an LDAP connection.
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.
619 * Success: LDAP_SUCCESS
620 * Failure: An LDAP error code.
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
;
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
);
639 serverctrlsU
= controlarrayWtoU( serverctrls
);
640 if (!serverctrlsU
) goto exit
;
643 clientctrlsU
= controlarrayWtoU( clientctrls
);
644 if (!clientctrlsU
) goto exit
;
647 ret
= map_error( ldap_start_tls_s( ld
, serverctrlsU
, clientctrlsU
));
650 controlarrayfreeU( serverctrlsU
);
651 controlarrayfreeU( clientctrlsU
);
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.
672 * ld [I] Pointer to an LDAP context.
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 */