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>
29 //#include "winbase.h"
36 #include "winldap_private.h"
37 //#include "wldap32.h"
40 /* Should eventually be determined by the algorithm documented on MSDN. */
41 static const WCHAR defaulthost
[] = { 'l','o','c','a','l','h','o','s','t',0 };
43 /* Split a space separated string of hostnames into a string array */
44 static char **split_hostnames( const char *hostnames
)
46 char **res
, *str
, *p
, *q
;
49 str
= strdupU( hostnames
);
50 if (!str
) return NULL
;
53 while (isspace( *p
)) p
++;
60 while (isspace( *p
)) p
++;
66 res
= HeapAlloc( GetProcessHeap(), 0, (i
+ 1) * sizeof(char *) );
69 HeapFree( GetProcessHeap(), 0, str
);
74 while (isspace( *p
)) p
++;
86 res
[i
] = strdupU( q
);
87 if (!res
[i
]) goto oom
;
90 while (isspace( *p
)) p
++;
96 res
[i
] = strdupU( q
);
97 if (!res
[i
]) goto oom
;
104 HeapFree( GetProcessHeap(), 0, str
);
108 while (i
> 0) strfreeU( res
[--i
] );
110 HeapFree( GetProcessHeap(), 0, res
);
111 HeapFree( GetProcessHeap(), 0, str
);
116 /* Determine if a URL starts with a known LDAP scheme */
117 static int has_ldap_scheme( char *url
)
119 if (!strncasecmp( url
, "ldap://", 7 ) ||
120 !strncasecmp( url
, "ldaps://", 8 ) ||
121 !strncasecmp( url
, "ldapi://", 8 ) ||
122 !strncasecmp( url
, "cldap://", 8 )) return 1;
126 /* Flatten an array of hostnames into a space separated string of URLs.
127 * Prepend a given scheme and append a given portnumber to each hostname
130 static char *join_hostnames( const char *scheme
, char **hostnames
, ULONG portnumber
)
132 char *res
, *p
, *q
, **v
;
133 unsigned int i
= 0, size
= 0;
134 static const char sep
[] = " ", fmt
[] = ":%d";
137 sprintf( port
, fmt
, portnumber
);
139 for (v
= hostnames
; *v
; v
++)
141 if (!has_ldap_scheme( *v
))
143 size
+= strlen( scheme
);
147 /* skip past colon in scheme prefix */
148 q
= strchr( *v
, '/' );
150 size
+= strlen( *v
);
152 if (!strchr( q
, ':' ))
153 size
+= strlen( port
);
158 size
+= (i
- 1) * strlen( sep
);
160 res
= HeapAlloc( GetProcessHeap(), 0, size
+ 1 );
161 if (!res
) return NULL
;
164 for (v
= hostnames
; *v
; v
++)
172 if (!has_ldap_scheme( *v
))
175 p
+= strlen( scheme
);
179 /* skip past colon in scheme prefix */
180 q
= strchr( *v
, '/' );
185 if (!strchr( q
, ':' ))
194 static char *urlify_hostnames( const char *scheme
, char *hostnames
, ULONG port
)
196 char *url
= NULL
, **strarray
;
198 strarray
= split_hostnames( hostnames
);
200 url
= join_hostnames( scheme
, strarray
, port
);
204 strarrayfreeU( strarray
);
209 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
211 /***********************************************************************
212 * cldap_openA (WLDAP32.@)
216 WLDAP32_LDAP
* CDECL
cldap_openA( PCHAR hostname
, ULONG portnumber
)
219 WLDAP32_LDAP
*ld
= NULL
;
220 WCHAR
*hostnameW
= NULL
;
222 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
225 hostnameW
= strAtoW( hostname
);
226 if (!hostnameW
) goto exit
;
229 ld
= cldap_openW( hostnameW
, portnumber
);
232 strfreeW( hostnameW
);
240 /***********************************************************************
241 * cldap_openW (WLDAP32.@)
243 * Initialize an LDAP context and create a UDP connection.
246 * hostname [I] Name of the host to connect to.
247 * portnumber [I] Portnumber to use.
250 * Success: Pointer to an LDAP context.
254 * The hostname string can be a space separated string of hostnames,
255 * in which case the LDAP runtime will try to connect to the hosts
256 * in order, until a connection can be made. A hostname may have a
257 * trailing portnumber (separated from the hostname by a ':'), which
258 * will take precedence over the portnumber supplied as a parameter
261 WLDAP32_LDAP
* CDECL
cldap_openW( PWCHAR hostname
, ULONG portnumber
)
265 char *hostnameU
= NULL
, *url
= NULL
;
267 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
270 hostnameU
= strWtoU( hostname
);
271 if (!hostnameU
) goto exit
;
274 hostnameU
= strWtoU( defaulthost
);
275 if (!hostnameU
) goto exit
;
278 url
= urlify_hostnames( "cldap://", hostnameU
, portnumber
);
281 ldap_initialize( &ld
, url
);
284 strfreeU( hostnameU
);
293 /***********************************************************************
294 * ldap_connect (WLDAP32.@)
296 * Connect to an LDAP server.
299 * ld [I] Pointer to an LDAP context.
300 * timeout [I] Pointer to an l_timeval structure specifying the
301 * timeout in seconds.
304 * Success: LDAP_SUCCESS
305 * Failure: An LDAP error code.
308 * The timeout parameter may be NULL in which case a default timeout
309 * value will be used.
311 ULONG CDECL
ldap_connect( WLDAP32_LDAP
*ld
, struct l_timeval
*timeout
)
313 TRACE( "(%p, %p)\n", ld
, timeout
);
315 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
316 return WLDAP32_LDAP_SUCCESS
; /* FIXME: do something, e.g. ping the host */
319 /***********************************************************************
320 * ldap_initA (WLDAP32.@)
324 WLDAP32_LDAP
* CDECL
ldap_initA( PCHAR hostname
, ULONG portnumber
)
327 WLDAP32_LDAP
*ld
= NULL
;
328 WCHAR
*hostnameW
= NULL
;
330 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
333 hostnameW
= strAtoW( hostname
);
334 if (!hostnameW
) goto exit
;
337 ld
= ldap_initW( hostnameW
, portnumber
);
340 strfreeW( hostnameW
);
348 /***********************************************************************
349 * ldap_initW (WLDAP32.@)
351 * Initialize an LDAP context and create a TCP connection.
354 * hostname [I] Name of the host to connect to.
355 * portnumber [I] Portnumber to use.
358 * Success: Pointer to an LDAP context.
362 * The hostname string can be a space separated string of hostnames,
363 * in which case the LDAP runtime will try to connect to the hosts
364 * in order, until a connection can be made. A hostname may have a
365 * trailing portnumber (separated from the hostname by a ':'), which
366 * will take precedence over the portnumber supplied as a parameter
367 * to this function. The connection will not be made until the first
368 * LDAP function that needs it is called.
370 WLDAP32_LDAP
* CDECL
ldap_initW( PWCHAR hostname
, ULONG portnumber
)
374 char *hostnameU
= NULL
, *url
= NULL
;
376 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
379 hostnameU
= strWtoU( hostname
);
380 if (!hostnameU
) goto exit
;
383 hostnameU
= strWtoU( defaulthost
);
384 if (!hostnameU
) goto exit
;
387 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
390 ldap_initialize( &ld
, url
);
393 strfreeU( hostnameU
);
402 /***********************************************************************
403 * ldap_openA (WLDAP32.@)
407 WLDAP32_LDAP
* CDECL
ldap_openA( PCHAR hostname
, ULONG portnumber
)
410 WLDAP32_LDAP
*ld
= NULL
;
411 WCHAR
*hostnameW
= NULL
;
413 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
416 hostnameW
= strAtoW( hostname
);
417 if (!hostnameW
) goto exit
;
420 ld
= ldap_openW( hostnameW
, portnumber
);
423 strfreeW( hostnameW
);
431 /***********************************************************************
432 * ldap_openW (WLDAP32.@)
434 * Initialize an LDAP context and create a TCP connection.
437 * hostname [I] Name of the host to connect to.
438 * portnumber [I] Portnumber to use.
441 * Success: Pointer to an LDAP context.
445 * The hostname string can be a space separated string of hostnames,
446 * in which case the LDAP runtime will try to connect to the hosts
447 * in order, until a connection can be made. A hostname may have a
448 * trailing portnumber (separated from the hostname by a ':'), which
449 * will take precedence over the portnumber supplied as a parameter
452 WLDAP32_LDAP
* CDECL
ldap_openW( PWCHAR hostname
, ULONG portnumber
)
456 char *hostnameU
= NULL
, *url
= NULL
;
458 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
461 hostnameU
= strWtoU( hostname
);
462 if (!hostnameU
) goto exit
;
465 hostnameU
= strWtoU( defaulthost
);
466 if (!hostnameU
) goto exit
;
469 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
472 ldap_initialize( &ld
, url
);
475 strfreeU( hostnameU
);
484 /***********************************************************************
485 * ldap_sslinitA (WLDAP32.@)
489 WLDAP32_LDAP
* CDECL
ldap_sslinitA( PCHAR hostname
, ULONG portnumber
, int secure
)
493 WCHAR
*hostnameW
= NULL
;
495 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname
), portnumber
, secure
);
498 hostnameW
= strAtoW( hostname
);
499 if (!hostnameW
) return NULL
;
502 ld
= ldap_sslinitW( hostnameW
, portnumber
, secure
);
504 strfreeW( hostnameW
);
512 /***********************************************************************
513 * ldap_sslinitW (WLDAP32.@)
515 * Initialize an LDAP context and create a secure TCP connection.
518 * hostname [I] Name of the host to connect to.
519 * portnumber [I] Portnumber to use.
520 * secure [I] Ask the server to create an SSL connection.
523 * Success: Pointer to an LDAP context.
527 * The hostname string can be a space separated string of hostnames,
528 * in which case the LDAP runtime will try to connect to the hosts
529 * in order, until a connection can be made. A hostname may have a
530 * trailing portnumber (separated from the hostname by a ':'), which
531 * will take precedence over the portnumber supplied as a parameter
532 * to this function. The connection will not be made until the first
533 * LDAP function that needs it is called.
535 WLDAP32_LDAP
* CDECL
ldap_sslinitW( PWCHAR hostname
, ULONG portnumber
, int secure
)
538 WLDAP32_LDAP
*ld
= NULL
;
539 char *hostnameU
= NULL
, *url
= NULL
;
541 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname
), portnumber
, secure
);
544 hostnameU
= strWtoU( hostname
);
545 if (!hostnameU
) goto exit
;
548 hostnameU
= strWtoU( defaulthost
);
549 if (!hostnameU
) goto exit
;
553 url
= urlify_hostnames( "ldaps://", hostnameU
, portnumber
);
555 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
558 ldap_initialize( &ld
, url
);
561 strfreeU( hostnameU
);
570 /***********************************************************************
571 * ldap_start_tls_sA (WLDAP32.@)
573 * See ldap_start_tls_sW.
575 ULONG CDECL
ldap_start_tls_sA( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
576 PLDAPControlA
*serverctrls
, PLDAPControlA
*clientctrls
)
578 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
580 LDAPControlW
**serverctrlsW
= NULL
, **clientctrlsW
= NULL
;
582 ret
= WLDAP32_LDAP_NO_MEMORY
;
584 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
589 serverctrlsW
= controlarrayAtoW( serverctrls
);
590 if (!serverctrlsW
) goto exit
;
593 clientctrlsW
= controlarrayAtoW( clientctrls
);
594 if (!clientctrlsW
) goto exit
;
597 ret
= ldap_start_tls_sW( ld
, retval
, result
, serverctrlsW
, clientctrlsW
);
600 controlarrayfreeW( serverctrlsW
);
601 controlarrayfreeW( clientctrlsW
);
607 /***********************************************************************
608 * ldap_start_tls_s (WLDAP32.@)
610 * Start TLS encryption on an LDAP connection.
613 * ld [I] Pointer to an LDAP context.
614 * retval [I] Return value from the server.
615 * result [I] Response message from the server.
616 * serverctrls [I] Array of LDAP server controls.
617 * clientctrls [I] Array of LDAP client controls.
620 * Success: LDAP_SUCCESS
621 * Failure: An LDAP error code.
624 * LDAP function that needs it is called.
626 ULONG CDECL
ldap_start_tls_sW( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
627 PLDAPControlW
*serverctrls
, PLDAPControlW
*clientctrls
)
629 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
631 LDAPControl
**serverctrlsU
= NULL
, **clientctrlsU
= NULL
;
633 ret
= WLDAP32_LDAP_NO_MEMORY
;
635 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
640 serverctrlsU
= controlarrayWtoU( serverctrls
);
641 if (!serverctrlsU
) goto exit
;
644 clientctrlsU
= controlarrayWtoU( clientctrls
);
645 if (!clientctrlsU
) goto exit
;
648 ret
= map_error( ldap_start_tls_s( ld
, serverctrlsU
, clientctrlsU
));
651 controlarrayfreeU( serverctrlsU
);
652 controlarrayfreeU( clientctrlsU
);
658 /***********************************************************************
659 * ldap_startup (WLDAP32.@)
661 ULONG CDECL
ldap_startup( PLDAP_VERSION_INFO version
, HANDLE
*instance
)
663 TRACE( "(%p, %p)\n", version
, instance
);
664 return WLDAP32_LDAP_SUCCESS
;
667 /***********************************************************************
668 * ldap_stop_tls_s (WLDAP32.@)
670 * Stop TLS encryption on an LDAP connection.
673 * ld [I] Pointer to an LDAP context.
679 BOOLEAN CDECL
ldap_stop_tls_s( WLDAP32_LDAP
*ld
)
681 TRACE( "(%p)\n", ld
);
682 return TRUE
; /* FIXME: find a way to stop tls on a connection */