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 while (i
> 0) strfreeU( res
[--i
] );
114 HeapFree( GetProcessHeap(), 0, res
);
115 HeapFree( GetProcessHeap(), 0, str
);
120 /* Determine if a URL starts with a known LDAP scheme */
121 static int has_ldap_scheme( char *url
)
123 if (!strncasecmp( url
, "ldap://", 7 ) ||
124 !strncasecmp( url
, "ldaps://", 8 ) ||
125 !strncasecmp( url
, "ldapi://", 8 ) ||
126 !strncasecmp( url
, "cldap://", 8 )) return 1;
130 /* Flatten an array of hostnames into a space separated string of URLs.
131 * Prepend a given scheme and append a given portnumber to each hostname
134 static char *join_hostnames( const char *scheme
, char **hostnames
, ULONG portnumber
)
136 char *res
, *p
, *q
, **v
;
137 unsigned int i
= 0, size
= 0;
138 static const char sep
[] = " ", fmt
[] = ":%d";
141 sprintf( port
, fmt
, portnumber
);
143 for (v
= hostnames
; *v
; v
++)
145 if (!has_ldap_scheme( *v
))
147 size
+= strlen( scheme
);
151 /* skip past colon in scheme prefix */
152 q
= strchr( *v
, '/' );
154 size
+= strlen( *v
);
156 if (!strchr( q
, ':' ))
157 size
+= strlen( port
);
162 size
+= (i
- 1) * strlen( sep
);
164 res
= HeapAlloc( GetProcessHeap(), 0, size
+ 1 );
165 if (!res
) return NULL
;
168 for (v
= hostnames
; *v
; v
++)
176 if (!has_ldap_scheme( *v
))
179 p
+= strlen( scheme
);
183 /* skip past colon in scheme prefix */
184 q
= strchr( *v
, '/' );
189 if (!strchr( q
, ':' ))
198 static char *urlify_hostnames( const char *scheme
, char *hostnames
, ULONG port
)
200 char *url
= NULL
, **strarray
;
202 strarray
= split_hostnames( hostnames
);
204 url
= join_hostnames( scheme
, strarray
, port
);
208 strarrayfreeU( strarray
);
213 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
215 /***********************************************************************
216 * cldap_openA (WLDAP32.@)
220 WLDAP32_LDAP
* CDECL
cldap_openA( PCHAR hostname
, ULONG portnumber
)
223 WLDAP32_LDAP
*ld
= NULL
;
224 WCHAR
*hostnameW
= NULL
;
226 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
229 hostnameW
= strAtoW( hostname
);
230 if (!hostnameW
) goto exit
;
233 ld
= cldap_openW( hostnameW
, portnumber
);
236 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, %d)\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
);
297 /***********************************************************************
298 * ldap_connect (WLDAP32.@)
300 * Connect to an LDAP server.
303 * ld [I] Pointer to an LDAP context.
304 * timeout [I] Pointer to an l_timeval structure specifying the
305 * timeout in seconds.
308 * Success: LDAP_SUCCESS
309 * Failure: An LDAP error code.
312 * The timeout parameter may be NULL in which case a default timeout
313 * value will be used.
315 ULONG CDECL
ldap_connect( WLDAP32_LDAP
*ld
, struct l_timeval
*timeout
)
317 TRACE( "(%p, %p)\n", ld
, timeout
);
319 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
320 return LDAP_SUCCESS
; /* FIXME: do something, e.g. ping the host */
323 /***********************************************************************
324 * ldap_initA (WLDAP32.@)
328 WLDAP32_LDAP
* CDECL
ldap_initA( PCHAR hostname
, ULONG portnumber
)
331 WLDAP32_LDAP
*ld
= NULL
;
332 WCHAR
*hostnameW
= NULL
;
334 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
337 hostnameW
= strAtoW( hostname
);
338 if (!hostnameW
) goto exit
;
341 ld
= ldap_initW( hostnameW
, portnumber
);
344 strfreeW( hostnameW
);
352 /***********************************************************************
353 * ldap_initW (WLDAP32.@)
355 * Initialize an LDAP context and create a TCP connection.
358 * hostname [I] Name of the host to connect to.
359 * portnumber [I] Portnumber to use.
362 * Success: Pointer to an LDAP context.
366 * The hostname string can be a space separated string of hostnames,
367 * in which case the LDAP runtime will try to connect to the hosts
368 * in order, until a connection can be made. A hostname may have a
369 * trailing portnumber (separated from the hostname by a ':'), which
370 * will take precedence over the portnumber supplied as a parameter
371 * to this function. The connection will not be made until the first
372 * LDAP function that needs it is called.
374 WLDAP32_LDAP
* CDECL
ldap_initW( PWCHAR hostname
, ULONG portnumber
)
378 char *hostnameU
= NULL
, *url
= NULL
;
380 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
383 hostnameU
= strWtoU( hostname
);
384 if (!hostnameU
) goto exit
;
387 hostnameU
= strWtoU( defaulthost
);
388 if (!hostnameU
) goto exit
;
391 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
394 ldap_initialize( &ld
, url
);
397 strfreeU( hostnameU
);
406 /***********************************************************************
407 * ldap_openA (WLDAP32.@)
411 WLDAP32_LDAP
* CDECL
ldap_openA( PCHAR hostname
, ULONG portnumber
)
414 WLDAP32_LDAP
*ld
= NULL
;
415 WCHAR
*hostnameW
= NULL
;
417 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
420 hostnameW
= strAtoW( hostname
);
421 if (!hostnameW
) goto exit
;
424 ld
= ldap_openW( hostnameW
, portnumber
);
427 strfreeW( hostnameW
);
435 /***********************************************************************
436 * ldap_openW (WLDAP32.@)
438 * Initialize an LDAP context and create a TCP connection.
441 * hostname [I] Name of the host to connect to.
442 * portnumber [I] Portnumber to use.
445 * Success: Pointer to an LDAP context.
449 * The hostname string can be a space separated string of hostnames,
450 * in which case the LDAP runtime will try to connect to the hosts
451 * in order, until a connection can be made. A hostname may have a
452 * trailing portnumber (separated from the hostname by a ':'), which
453 * will take precedence over the portnumber supplied as a parameter
456 WLDAP32_LDAP
* CDECL
ldap_openW( PWCHAR hostname
, ULONG portnumber
)
460 char *hostnameU
= NULL
, *url
= NULL
;
462 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
465 hostnameU
= strWtoU( hostname
);
466 if (!hostnameU
) goto exit
;
469 hostnameU
= strWtoU( defaulthost
);
470 if (!hostnameU
) goto exit
;
473 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
476 ldap_initialize( &ld
, url
);
479 strfreeU( hostnameU
);
488 /***********************************************************************
489 * ldap_sslinitA (WLDAP32.@)
493 WLDAP32_LDAP
* CDECL
ldap_sslinitA( PCHAR hostname
, ULONG portnumber
, int secure
)
497 WCHAR
*hostnameW
= NULL
;
499 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname
), portnumber
, secure
);
502 hostnameW
= strAtoW( hostname
);
503 if (!hostnameW
) return NULL
;
506 ld
= ldap_sslinitW( hostnameW
, portnumber
, secure
);
508 strfreeW( hostnameW
);
516 /***********************************************************************
517 * ldap_sslinitW (WLDAP32.@)
519 * Initialize an LDAP context and create a secure TCP connection.
522 * hostname [I] Name of the host to connect to.
523 * portnumber [I] Portnumber to use.
524 * secure [I] Ask the server to create an SSL connection.
527 * Success: Pointer to an LDAP context.
531 * The hostname string can be a space separated string of hostnames,
532 * in which case the LDAP runtime will try to connect to the hosts
533 * in order, until a connection can be made. A hostname may have a
534 * trailing portnumber (separated from the hostname by a ':'), which
535 * will take precedence over the portnumber supplied as a parameter
536 * to this function. The connection will not be made until the first
537 * LDAP function that needs it is called.
539 WLDAP32_LDAP
* CDECL
ldap_sslinitW( PWCHAR hostname
, ULONG portnumber
, int secure
)
542 WLDAP32_LDAP
*ld
= NULL
;
543 char *hostnameU
= NULL
, *url
= NULL
;
545 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname
), portnumber
, secure
);
548 hostnameU
= strWtoU( hostname
);
549 if (!hostnameU
) goto exit
;
552 hostnameU
= strWtoU( defaulthost
);
553 if (!hostnameU
) goto exit
;
557 url
= urlify_hostnames( "ldaps://", hostnameU
, portnumber
);
559 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
562 ldap_initialize( &ld
, url
);
565 strfreeU( hostnameU
);
574 /***********************************************************************
575 * ldap_start_tls_sA (WLDAP32.@)
577 * See ldap_start_tls_sW.
579 ULONG CDECL
ldap_start_tls_sA( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
580 PLDAPControlA
*serverctrls
, PLDAPControlA
*clientctrls
)
582 ULONG ret
= LDAP_NOT_SUPPORTED
;
584 LDAPControlW
**serverctrlsW
= NULL
, **clientctrlsW
= NULL
;
586 ret
= WLDAP32_LDAP_NO_MEMORY
;
588 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
590 if (!ld
) return ~0UL;
593 serverctrlsW
= controlarrayAtoW( serverctrls
);
594 if (!serverctrlsW
) goto exit
;
597 clientctrlsW
= controlarrayAtoW( clientctrls
);
598 if (!clientctrlsW
) goto exit
;
601 ret
= ldap_start_tls_sW( ld
, retval
, result
, serverctrlsW
, clientctrlsW
);
604 controlarrayfreeW( serverctrlsW
);
605 controlarrayfreeW( clientctrlsW
);
611 /***********************************************************************
612 * ldap_start_tls_s (WLDAP32.@)
614 * Start TLS encryption on an LDAP connection.
617 * ld [I] Pointer to an LDAP context.
618 * retval [I] Return value from the server.
619 * result [I] Response message from the server.
620 * serverctrls [I] Array of LDAP server controls.
621 * clientctrls [I] Array of LDAP client controls.
624 * Success: LDAP_SUCCESS
625 * Failure: An LDAP error code.
628 * LDAP function that needs it is called.
630 ULONG CDECL
ldap_start_tls_sW( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
631 PLDAPControlW
*serverctrls
, PLDAPControlW
*clientctrls
)
633 ULONG ret
= LDAP_NOT_SUPPORTED
;
635 LDAPControl
**serverctrlsU
= NULL
, **clientctrlsU
= NULL
;
637 ret
= WLDAP32_LDAP_NO_MEMORY
;
639 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
641 if (!ld
) return ~0UL;
644 serverctrlsU
= controlarrayWtoU( serverctrls
);
645 if (!serverctrlsU
) goto exit
;
648 clientctrlsU
= controlarrayWtoU( clientctrls
);
649 if (!clientctrlsU
) goto exit
;
652 ret
= ldap_start_tls_s( ld
, serverctrlsU
, clientctrlsU
);
655 controlarrayfreeU( serverctrlsU
);
656 controlarrayfreeU( clientctrlsU
);
662 /***********************************************************************
663 * ldap_startup (WLDAP32.@)
665 ULONG CDECL
ldap_startup( PLDAP_VERSION_INFO version
, HANDLE
*instance
)
667 TRACE( "(%p, %p)\n", version
, instance
);
671 /***********************************************************************
672 * ldap_stop_tls_s (WLDAP32.@)
674 * Stop TLS encryption on an LDAP connection.
677 * ld [I] Pointer to an LDAP context.
683 BOOLEAN CDECL
ldap_stop_tls_s( WLDAP32_LDAP
*ld
)
685 TRACE( "(%p)\n", ld
);
686 return TRUE
; /* FIXME: find a way to stop tls on a connection */