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"
36 #define LDAP_SUCCESS 0x00
37 #define LDAP_NOT_SUPPORTED 0x5c
40 #include "winldap_private.h"
44 /* Should eventually be determined by the algorithm documented on MSDN. */
45 static const WCHAR defaulthost
[] = { 'l','o','c','a','l','h','o','s','t',0 };
47 /* Split a space separated string of hostnames into a string array */
48 static char **split_hostnames( const char *hostnames
)
50 char **res
, *str
, *p
, *q
;
53 str
= strdupU( hostnames
);
54 if (!str
) return NULL
;
57 while (isspace( *p
)) p
++;
64 while (isspace( *p
)) p
++;
70 res
= HeapAlloc( GetProcessHeap(), 0, (i
+ 1) * sizeof(char *) );
73 HeapFree( GetProcessHeap(), 0, str
);
78 while (isspace( *p
)) p
++;
90 res
[i
] = strdupU( q
);
91 if (!res
[i
]) goto oom
;
94 while (isspace( *p
)) p
++;
100 res
[i
] = strdupU( q
);
101 if (!res
[i
]) goto oom
;
108 HeapFree( GetProcessHeap(), 0, str
);
112 for (--i
; i
>= 0; i
--)
115 HeapFree( GetProcessHeap(), 0, res
);
116 HeapFree( GetProcessHeap(), 0, str
);
121 /* Determine if a URL starts with a known LDAP scheme */
122 static int has_ldap_scheme( char *url
)
124 if (!strncasecmp( url
, "ldap://", 7 ) ||
125 !strncasecmp( url
, "ldaps://", 8 ) ||
126 !strncasecmp( url
, "ldapi://", 8 ) ||
127 !strncasecmp( url
, "cldap://", 8 )) return 1;
131 /* Flatten an array of hostnames into a space separated string of URLs.
132 * Prepend a given scheme and append a given portnumber to each hostname
135 static char *join_hostnames( char *scheme
, char **hostnames
, ULONG portnumber
)
137 char *res
, *p
, *q
, **v
;
138 unsigned int i
= 0, size
= 0;
139 static const char sep
[] = " ", fmt
[] = ":%ld";
142 sprintf( port
, fmt
, portnumber
);
144 for (v
= hostnames
; *v
; v
++)
146 if (!has_ldap_scheme( *v
))
148 size
+= strlen( scheme
);
152 /* skip past colon in scheme prefix */
153 q
= strchr( *v
, '/' );
155 size
+= strlen( *v
);
157 if (!strchr( q
, ':' ))
158 size
+= strlen( port
);
163 size
+= (i
- 1) * strlen( sep
);
165 res
= HeapAlloc( GetProcessHeap(), 0, size
+ 1 );
166 if (!res
) return NULL
;
169 for (v
= hostnames
; *v
; v
++)
177 if (!has_ldap_scheme( *v
))
180 p
+= strlen( scheme
);
184 /* skip past colon in scheme prefix */
185 q
= strchr( *v
, '/' );
190 if (!strchr( q
, ':' ))
199 static char *urlify_hostnames( char *scheme
, char *hostnames
, ULONG port
)
201 char *url
= NULL
, **strarray
;
203 strarray
= split_hostnames( hostnames
);
205 url
= join_hostnames( scheme
, strarray
, port
);
209 strarrayfreeU( strarray
);
214 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
216 /***********************************************************************
217 * cldap_openA (WLDAP32.@)
221 WLDAP32_LDAP
* CDECL
cldap_openA( PCHAR hostname
, ULONG portnumber
)
224 WLDAP32_LDAP
*ld
= NULL
;
225 WCHAR
*hostnameW
= NULL
;
227 TRACE( "(%s, %ld)\n", debugstr_a(hostname
), portnumber
);
230 hostnameW
= strAtoW( hostname
);
231 if (!hostnameW
) goto exit
;
234 ld
= cldap_openW( hostnameW
, portnumber
);
237 strfreeW( hostnameW
);
244 /***********************************************************************
245 * cldap_openW (WLDAP32.@)
247 * Initialize an LDAP context and create a UDP connection.
250 * hostname [I] Name of the host to connect to.
251 * portnumber [I] Portnumber to use.
254 * Success: Pointer to an LDAP context.
258 * The hostname string can be a space separated string of hostnames,
259 * in which case the LDAP runtime will try to connect to the hosts
260 * in order, until a connection can be made. A hostname may have a
261 * trailing portnumber (separated from the hostname by a ':'), which
262 * will take precedence over the portnumber supplied as a parameter
265 WLDAP32_LDAP
* CDECL
cldap_openW( PWCHAR hostname
, ULONG portnumber
)
269 char *hostnameU
= NULL
, *url
= NULL
;
271 TRACE( "(%s, %ld)\n", debugstr_w(hostname
), portnumber
);
274 hostnameU
= strWtoU( hostname
);
275 if (!hostnameU
) goto exit
;
278 hostnameU
= strWtoU( defaulthost
);
279 if (!hostnameU
) goto exit
;
282 url
= urlify_hostnames( "cldap://", hostnameU
, portnumber
);
285 ldap_initialize( &ld
, url
);
288 strfreeU( hostnameU
);
296 /***********************************************************************
297 * ldap_connect (WLDAP32.@)
299 * Connect to an LDAP server.
302 * ld [I] Pointer to an LDAP context.
303 * timeout [I] Pointer to an l_timeval structure specifying the
304 * timeout in seconds.
307 * Success: LDAP_SUCCESS
308 * Failure: An LDAP error code.
311 * The timeout parameter may be NULL in which case a default timeout
312 * value will be used.
314 ULONG CDECL
ldap_connect( WLDAP32_LDAP
*ld
, struct l_timeval
*timeout
)
316 TRACE( "(%p, %p)\n", ld
, timeout
);
318 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
319 return LDAP_SUCCESS
; /* FIXME: do something, e.g. ping the host */
322 /***********************************************************************
323 * ldap_initA (WLDAP32.@)
327 WLDAP32_LDAP
* CDECL
ldap_initA( PCHAR hostname
, ULONG portnumber
)
330 WLDAP32_LDAP
*ld
= NULL
;
331 WCHAR
*hostnameW
= NULL
;
333 TRACE( "(%s, %ld)\n", debugstr_a(hostname
), portnumber
);
336 hostnameW
= strAtoW( hostname
);
337 if (!hostnameW
) goto exit
;
340 ld
= ldap_initW( hostnameW
, portnumber
);
343 strfreeW( hostnameW
);
350 /***********************************************************************
351 * ldap_initW (WLDAP32.@)
353 * Initialize an LDAP context and create a TCP connection.
356 * hostname [I] Name of the host to connect to.
357 * portnumber [I] Portnumber to use.
360 * Success: Pointer to an LDAP context.
364 * The hostname string can be a space separated string of hostnames,
365 * in which case the LDAP runtime will try to connect to the hosts
366 * in order, until a connection can be made. A hostname may have a
367 * trailing portnumber (separated from the hostname by a ':'), which
368 * will take precedence over the portnumber supplied as a parameter
369 * to this function. The connection will not be made until the first
370 * LDAP function that needs it is called.
372 WLDAP32_LDAP
* CDECL
ldap_initW( PWCHAR hostname
, ULONG portnumber
)
376 char *hostnameU
= NULL
, *url
= NULL
;
378 TRACE( "(%s, %ld)\n", debugstr_w(hostname
), portnumber
);
381 hostnameU
= strWtoU( hostname
);
382 if (!hostnameU
) goto exit
;
385 hostnameU
= strWtoU( defaulthost
);
386 if (!hostnameU
) goto exit
;
389 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
392 ldap_initialize( &ld
, url
);
395 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, %ld)\n", debugstr_a(hostname
), portnumber
);
417 hostnameW
= strAtoW( hostname
);
418 if (!hostnameW
) goto exit
;
421 ld
= ldap_openW( hostnameW
, portnumber
);
424 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, %ld)\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
);
483 /***********************************************************************
484 * ldap_sslinitA (WLDAP32.@)
488 WLDAP32_LDAP
* CDECL
ldap_sslinitA( PCHAR hostname
, ULONG portnumber
, int secure
)
492 WCHAR
*hostnameW
= NULL
;
494 TRACE( "(%s, %ld, 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
);
510 /***********************************************************************
511 * ldap_sslinitW (WLDAP32.@)
513 * Initialize an LDAP context and create a secure TCP connection.
516 * hostname [I] Name of the host to connect to.
517 * portnumber [I] Portnumber to use.
518 * secure [I] Ask the server to create an SSL connection.
521 * Success: Pointer to an LDAP context.
525 * The hostname string can be a space separated string of hostnames,
526 * in which case the LDAP runtime will try to connect to the hosts
527 * in order, until a connection can be made. A hostname may have a
528 * trailing portnumber (separated from the hostname by a ':'), which
529 * will take precedence over the portnumber supplied as a parameter
530 * to this function. The connection will not be made until the first
531 * LDAP function that needs it is called.
533 WLDAP32_LDAP
* CDECL
ldap_sslinitW( PWCHAR hostname
, ULONG portnumber
, int secure
)
536 WLDAP32_LDAP
*ld
= NULL
;
537 char *hostnameU
= NULL
, *url
= NULL
;
539 TRACE( "(%s, %ld, 0x%08x)\n", debugstr_w(hostname
), portnumber
, secure
);
542 hostnameU
= strWtoU( hostname
);
543 if (!hostnameU
) goto exit
;
546 hostnameU
= strWtoU( defaulthost
);
547 if (!hostnameU
) goto exit
;
551 url
= urlify_hostnames( "ldaps://", hostnameU
, portnumber
);
553 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
556 ldap_initialize( &ld
, url
);
559 strfreeU( hostnameU
);
567 /***********************************************************************
568 * ldap_start_tls_sA (WLDAP32.@)
570 * See ldap_start_tls_sW.
572 ULONG CDECL
ldap_start_tls_sA( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
573 PLDAPControlA
*serverctrls
, PLDAPControlA
*clientctrls
)
575 ULONG ret
= LDAP_NOT_SUPPORTED
;
577 LDAPControlW
**serverctrlsW
= NULL
, **clientctrlsW
= NULL
;
579 ret
= WLDAP32_LDAP_NO_MEMORY
;
581 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
583 if (!ld
) return ~0UL;
586 serverctrlsW
= controlarrayAtoW( serverctrls
);
587 if (!serverctrlsW
) goto exit
;
590 clientctrlsW
= controlarrayAtoW( clientctrls
);
591 if (!clientctrlsW
) goto exit
;
594 ret
= ldap_start_tls_sW( ld
, retval
, result
, serverctrlsW
, clientctrlsW
);
597 controlarrayfreeW( serverctrlsW
);
598 controlarrayfreeW( clientctrlsW
);
604 /***********************************************************************
605 * ldap_start_tls_s (WLDAP32.@)
607 * Start TLS encryption on an LDAP connection.
610 * ld [I] Pointer to an LDAP context.
611 * retval [I] Return value from the server.
612 * result [I] Response message from the server.
613 * serverctrls [I] Array of LDAP server controls.
614 * clientctrls [I] Array of LDAP client controls.
617 * Success: LDAP_SUCCESS
618 * Failure: An LDAP error code.
621 * LDAP function that needs it is called.
623 ULONG CDECL
ldap_start_tls_sW( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
624 PLDAPControlW
*serverctrls
, PLDAPControlW
*clientctrls
)
626 ULONG ret
= LDAP_NOT_SUPPORTED
;
628 LDAPControl
**serverctrlsU
= NULL
, **clientctrlsU
= NULL
;
630 ret
= WLDAP32_LDAP_NO_MEMORY
;
632 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
634 if (!ld
) return ~0UL;
637 serverctrlsU
= controlarrayWtoU( serverctrls
);
638 if (!serverctrlsU
) goto exit
;
641 clientctrlsU
= controlarrayWtoU( clientctrls
);
642 if (!clientctrlsU
) goto exit
;
645 ret
= ldap_start_tls_s( ld
, serverctrlsU
, clientctrlsU
);
648 controlarrayfreeU( serverctrlsU
);
649 controlarrayfreeU( clientctrlsU
);
655 /***********************************************************************
656 * ldap_startup (WLDAP32.@)
658 ULONG CDECL
ldap_startup( PLDAP_VERSION_INFO version
, HANDLE
*instance
)
660 TRACE( "(%p, %p)\n", version
, instance
);
664 /***********************************************************************
665 * ldap_stop_tls_s (WLDAP32.@)
667 * Stop TLS encryption on an LDAP connection.
670 * ld [I] Pointer to an LDAP context.
676 BOOLEAN CDECL
ldap_stop_tls_s( WLDAP32_LDAP
*ld
)
678 TRACE( "(%p)\n", ld
);
679 return TRUE
; /* FIXME: find a way to stop tls on a connection */