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 "winldap_private.h"
24 /* Should eventually be determined by the algorithm documented on MSDN. */
25 static const WCHAR defaulthost
[] = { 'l','o','c','a','l','h','o','s','t',0 };
27 /* Split a space separated string of hostnames into a string array */
28 static char **split_hostnames( const char *hostnames
)
30 char **res
, *str
, *p
, *q
;
33 str
= strdupU( hostnames
);
34 if (!str
) return NULL
;
37 while (isspace( *p
)) p
++;
44 while (isspace( *p
)) p
++;
50 res
= HeapAlloc( GetProcessHeap(), 0, (i
+ 1) * sizeof(char *) );
53 HeapFree( GetProcessHeap(), 0, str
);
58 while (isspace( *p
)) p
++;
70 res
[i
] = strdupU( q
);
71 if (!res
[i
]) goto oom
;
74 while (isspace( *p
)) p
++;
80 res
[i
] = strdupU( q
);
81 if (!res
[i
]) goto oom
;
88 HeapFree( GetProcessHeap(), 0, str
);
92 while (i
> 0) strfreeU( res
[--i
] );
94 HeapFree( GetProcessHeap(), 0, res
);
95 HeapFree( GetProcessHeap(), 0, str
);
100 /* Determine if a URL starts with a known LDAP scheme */
101 static int has_ldap_scheme( char *url
)
103 if (!strncasecmp( url
, "ldap://", 7 ) ||
104 !strncasecmp( url
, "ldaps://", 8 ) ||
105 !strncasecmp( url
, "ldapi://", 8 ) ||
106 !strncasecmp( url
, "cldap://", 8 )) return 1;
110 /* Flatten an array of hostnames into a space separated string of URLs.
111 * Prepend a given scheme and append a given port number to each hostname
114 static char *join_hostnames( const char *scheme
, char **hostnames
, ULONG portnumber
)
116 char *res
, *p
, *q
, **v
;
117 unsigned int i
= 0, size
= 0;
118 static const char sep
[] = " ", fmt
[] = ":%d";
121 sprintf( port
, fmt
, portnumber
);
123 for (v
= hostnames
; *v
; v
++)
125 if (!has_ldap_scheme( *v
))
127 size
+= strlen( scheme
);
131 /* skip past colon in scheme prefix */
132 q
= strchr( *v
, '/' );
134 size
+= strlen( *v
);
136 if (!strchr( q
, ':' ))
137 size
+= strlen( port
);
142 size
+= (i
- 1) * strlen( sep
);
144 res
= HeapAlloc( GetProcessHeap(), 0, size
+ 1 );
145 if (!res
) return NULL
;
148 for (v
= hostnames
; *v
; v
++)
156 if (!has_ldap_scheme( *v
))
159 p
+= strlen( scheme
);
163 /* skip past colon in scheme prefix */
164 q
= strchr( *v
, '/' );
169 if (!strchr( q
, ':' ))
178 static char *urlify_hostnames( const char *scheme
, char *hostnames
, ULONG port
)
180 char *url
= NULL
, **strarray
;
182 strarray
= split_hostnames( hostnames
);
184 url
= join_hostnames( scheme
, strarray
, port
);
188 strarrayfreeU( strarray
);
193 /***********************************************************************
194 * cldap_openA (WLDAP32.@)
198 WLDAP32_LDAP
* CDECL
cldap_openA( PCHAR hostname
, ULONG portnumber
)
201 WLDAP32_LDAP
*ld
= NULL
;
202 WCHAR
*hostnameW
= NULL
;
204 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
207 hostnameW
= strAtoW( hostname
);
208 if (!hostnameW
) goto exit
;
211 ld
= cldap_openW( hostnameW
, portnumber
);
214 strfreeW( hostnameW
);
222 /***********************************************************************
223 * cldap_openW (WLDAP32.@)
225 * Initialize an LDAP context and create a UDP connection.
228 * hostname [I] Name of the host to connect to.
229 * portnumber [I] Port number to use.
232 * Success: Pointer to an LDAP context.
236 * The hostname string can be a space separated string of hostnames,
237 * in which case the LDAP runtime will try to connect to the hosts
238 * in order, until a connection can be made. A hostname may have a
239 * trailing port number (separated from the hostname by a ':'), which
240 * will take precedence over the port number supplied as a parameter
243 WLDAP32_LDAP
* CDECL
cldap_openW( PWCHAR hostname
, ULONG portnumber
)
247 char *hostnameU
= NULL
, *url
= NULL
;
249 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
252 hostnameU
= strWtoU( hostname
);
253 if (!hostnameU
) goto exit
;
256 hostnameU
= strWtoU( defaulthost
);
257 if (!hostnameU
) goto exit
;
260 url
= urlify_hostnames( "cldap://", hostnameU
, portnumber
);
263 ldap_initialize( &ld
, url
);
266 strfreeU( hostnameU
);
275 /***********************************************************************
276 * ldap_connect (WLDAP32.@)
278 * Connect to an LDAP server.
281 * ld [I] Pointer to an LDAP context.
282 * timeout [I] Pointer to an l_timeval structure specifying the
283 * timeout in seconds.
286 * Success: LDAP_SUCCESS
287 * Failure: An LDAP error code.
290 * The timeout parameter may be NULL in which case a default timeout
291 * value will be used.
293 ULONG CDECL
ldap_connect( WLDAP32_LDAP
*ld
, struct l_timeval
*timeout
)
295 TRACE( "(%p, %p)\n", ld
, timeout
);
297 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
298 return WLDAP32_LDAP_SUCCESS
; /* FIXME: do something, e.g. ping the host */
301 /***********************************************************************
302 * ldap_initA (WLDAP32.@)
306 WLDAP32_LDAP
* CDECL
ldap_initA( PCHAR hostname
, ULONG portnumber
)
309 WLDAP32_LDAP
*ld
= NULL
;
310 WCHAR
*hostnameW
= NULL
;
312 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
315 hostnameW
= strAtoW( hostname
);
316 if (!hostnameW
) goto exit
;
319 ld
= ldap_initW( hostnameW
, portnumber
);
322 strfreeW( hostnameW
);
330 /***********************************************************************
331 * ldap_initW (WLDAP32.@)
333 * Initialize an LDAP context and create a TCP connection.
336 * hostname [I] Name of the host to connect to.
337 * portnumber [I] Port number to use.
340 * Success: Pointer to an LDAP context.
344 * The hostname string can be a space separated string of hostnames,
345 * in which case the LDAP runtime will try to connect to the hosts
346 * in order, until a connection can be made. A hostname may have a
347 * trailing port number (separated from the hostname by a ':'), which
348 * will take precedence over the port number supplied as a parameter
349 * to this function. The connection will not be made until the first
350 * LDAP function that needs it is called.
352 WLDAP32_LDAP
* CDECL
ldap_initW( PWCHAR hostname
, ULONG portnumber
)
356 char *hostnameU
= NULL
, *url
= NULL
;
358 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
361 hostnameU
= strWtoU( hostname
);
362 if (!hostnameU
) goto exit
;
365 hostnameU
= strWtoU( defaulthost
);
366 if (!hostnameU
) goto exit
;
369 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
372 ldap_initialize( &ld
, url
);
375 strfreeU( hostnameU
);
384 /***********************************************************************
385 * ldap_openA (WLDAP32.@)
389 WLDAP32_LDAP
* CDECL
ldap_openA( PCHAR hostname
, ULONG portnumber
)
392 WLDAP32_LDAP
*ld
= NULL
;
393 WCHAR
*hostnameW
= NULL
;
395 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
398 hostnameW
= strAtoW( hostname
);
399 if (!hostnameW
) goto exit
;
402 ld
= ldap_openW( hostnameW
, portnumber
);
405 strfreeW( hostnameW
);
413 /***********************************************************************
414 * ldap_openW (WLDAP32.@)
416 * Initialize an LDAP context and create a TCP connection.
419 * hostname [I] Name of the host to connect to.
420 * portnumber [I] Port number to use.
423 * Success: Pointer to an LDAP context.
427 * The hostname string can be a space separated string of hostnames,
428 * in which case the LDAP runtime will try to connect to the hosts
429 * in order, until a connection can be made. A hostname may have a
430 * trailing port number (separated from the hostname by a ':'), which
431 * will take precedence over the port number supplied as a parameter
434 WLDAP32_LDAP
* CDECL
ldap_openW( PWCHAR hostname
, ULONG portnumber
)
438 char *hostnameU
= NULL
, *url
= NULL
;
440 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
443 hostnameU
= strWtoU( hostname
);
444 if (!hostnameU
) goto exit
;
447 hostnameU
= strWtoU( defaulthost
);
448 if (!hostnameU
) goto exit
;
451 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
454 ldap_initialize( &ld
, url
);
457 strfreeU( hostnameU
);
466 /***********************************************************************
467 * ldap_sslinitA (WLDAP32.@)
471 WLDAP32_LDAP
* CDECL
ldap_sslinitA( PCHAR hostname
, ULONG portnumber
, int secure
)
475 WCHAR
*hostnameW
= NULL
;
477 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname
), portnumber
, secure
);
480 hostnameW
= strAtoW( hostname
);
481 if (!hostnameW
) return NULL
;
484 ld
= ldap_sslinitW( hostnameW
, portnumber
, secure
);
486 strfreeW( hostnameW
);
494 /***********************************************************************
495 * ldap_sslinitW (WLDAP32.@)
497 * Initialize an LDAP context and create a secure TCP connection.
500 * hostname [I] Name of the host to connect to.
501 * portnumber [I] Port number to use.
502 * secure [I] Ask the server to create an SSL connection.
505 * Success: Pointer to an LDAP context.
509 * The hostname string can be a space separated string of hostnames,
510 * in which case the LDAP runtime will try to connect to the hosts
511 * in order, until a connection can be made. A hostname may have a
512 * trailing port number (separated from the hostname by a ':'), which
513 * will take precedence over the port number supplied as a parameter
514 * to this function. The connection will not be made until the first
515 * LDAP function that needs it is called.
517 WLDAP32_LDAP
* CDECL
ldap_sslinitW( PWCHAR hostname
, ULONG portnumber
, int secure
)
520 WLDAP32_LDAP
*ld
= NULL
;
521 char *hostnameU
= NULL
, *url
= NULL
;
523 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname
), portnumber
, secure
);
526 hostnameU
= strWtoU( hostname
);
527 if (!hostnameU
) goto exit
;
530 hostnameU
= strWtoU( defaulthost
);
531 if (!hostnameU
) goto exit
;
535 url
= urlify_hostnames( "ldaps://", hostnameU
, portnumber
);
537 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
540 ldap_initialize( &ld
, url
);
543 strfreeU( hostnameU
);
552 /***********************************************************************
553 * ldap_start_tls_sA (WLDAP32.@)
555 * See ldap_start_tls_sW.
557 ULONG CDECL
ldap_start_tls_sA( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
558 PLDAPControlA
*serverctrls
, PLDAPControlA
*clientctrls
)
560 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
562 LDAPControlW
**serverctrlsW
= NULL
, **clientctrlsW
= NULL
;
564 ret
= WLDAP32_LDAP_NO_MEMORY
;
566 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
571 serverctrlsW
= controlarrayAtoW( serverctrls
);
572 if (!serverctrlsW
) goto exit
;
575 clientctrlsW
= controlarrayAtoW( clientctrls
);
576 if (!clientctrlsW
) goto exit
;
579 ret
= ldap_start_tls_sW( ld
, retval
, result
, serverctrlsW
, clientctrlsW
);
582 controlarrayfreeW( serverctrlsW
);
583 controlarrayfreeW( clientctrlsW
);
589 /***********************************************************************
590 * ldap_start_tls_s (WLDAP32.@)
592 * Start TLS encryption on an LDAP connection.
595 * ld [I] Pointer to an LDAP context.
596 * retval [I] Return value from the server.
597 * result [I] Response message from the server.
598 * serverctrls [I] Array of LDAP server controls.
599 * clientctrls [I] Array of LDAP client controls.
602 * Success: LDAP_SUCCESS
603 * Failure: An LDAP error code.
606 * LDAP function that needs it is called.
608 ULONG CDECL
ldap_start_tls_sW( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
609 PLDAPControlW
*serverctrls
, PLDAPControlW
*clientctrls
)
611 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
613 LDAPControl
**serverctrlsU
= NULL
, **clientctrlsU
= NULL
;
615 ret
= WLDAP32_LDAP_NO_MEMORY
;
617 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
622 serverctrlsU
= controlarrayWtoU( serverctrls
);
623 if (!serverctrlsU
) goto exit
;
626 clientctrlsU
= controlarrayWtoU( clientctrls
);
627 if (!clientctrlsU
) goto exit
;
630 ret
= map_error( ldap_start_tls_s( ld
, serverctrlsU
, clientctrlsU
));
633 controlarrayfreeU( serverctrlsU
);
634 controlarrayfreeU( clientctrlsU
);
640 /***********************************************************************
641 * ldap_startup (WLDAP32.@)
643 ULONG CDECL
ldap_startup( PLDAP_VERSION_INFO version
, HANDLE
*instance
)
645 TRACE( "(%p, %p)\n", version
, instance
);
646 return WLDAP32_LDAP_SUCCESS
;
649 /***********************************************************************
650 * ldap_stop_tls_s (WLDAP32.@)
652 * Stop TLS encryption on an LDAP connection.
655 * ld [I] Pointer to an LDAP context.
661 BOOLEAN CDECL
ldap_stop_tls_s( WLDAP32_LDAP
*ld
)
663 TRACE( "(%p)\n", ld
);
664 return TRUE
; /* FIXME: find a way to stop tls on a connection */