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 BOOL
has_ldap_scheme( char *url
)
118 return !strncasecmp( url
, "ldap://", 7 ) ||
119 !strncasecmp( url
, "ldaps://", 8 ) ||
120 !strncasecmp( url
, "ldapi://", 8 ) ||
121 !strncasecmp( url
, "cldap://", 8 );
124 /* Flatten an array of hostnames into a space separated string of URLs.
125 * Prepend a given scheme and append a given port number to each hostname
128 static char *join_hostnames( const char *scheme
, char **hostnames
, ULONG portnumber
)
130 char *res
, *p
, *q
, **v
;
131 unsigned int i
= 0, size
= 0;
132 static const char sep
[] = " ", fmt
[] = ":%d";
135 sprintf( port
, fmt
, portnumber
);
137 for (v
= hostnames
; *v
; v
++)
139 if (!has_ldap_scheme( *v
))
141 size
+= strlen( scheme
);
145 /* skip past colon in scheme prefix */
146 q
= strchr( *v
, '/' );
148 size
+= strlen( *v
);
150 if (!strchr( q
, ':' ))
151 size
+= strlen( port
);
156 size
+= (i
- 1) * strlen( sep
);
158 res
= HeapAlloc( GetProcessHeap(), 0, size
+ 1 );
159 if (!res
) return NULL
;
162 for (v
= hostnames
; *v
; v
++)
170 if (!has_ldap_scheme( *v
))
173 p
+= strlen( scheme
);
177 /* skip past colon in scheme prefix */
178 q
= strchr( *v
, '/' );
183 if (!strchr( q
, ':' ))
192 static char *urlify_hostnames( const char *scheme
, char *hostnames
, ULONG port
)
194 char *url
= NULL
, **strarray
;
196 strarray
= split_hostnames( hostnames
);
198 url
= join_hostnames( scheme
, strarray
, port
);
202 strarrayfreeU( strarray
);
207 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
210 static LDAP
*create_context( const char *url
)
213 int version
= LDAP_VERSION3
;
214 if (ldap_initialize( &ld
, url
) != LDAP_SUCCESS
) return NULL
;
215 ldap_set_option( ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
220 /***********************************************************************
221 * cldap_openA (WLDAP32.@)
225 WLDAP32_LDAP
* CDECL
cldap_openA( PCHAR hostname
, ULONG portnumber
)
228 WLDAP32_LDAP
*ld
= NULL
;
229 WCHAR
*hostnameW
= NULL
;
231 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
234 hostnameW
= strAtoW( hostname
);
235 if (!hostnameW
) goto exit
;
238 ld
= cldap_openW( hostnameW
, portnumber
);
241 strfreeW( hostnameW
);
249 /***********************************************************************
250 * cldap_openW (WLDAP32.@)
252 * Initialize an LDAP context and create a UDP connection.
255 * hostname [I] Name of the host to connect to.
256 * portnumber [I] Port number to use.
259 * Success: Pointer to an LDAP context.
263 * The hostname string can be a space separated string of hostnames,
264 * in which case the LDAP runtime will try to connect to the hosts
265 * in order, until a connection can be made. A hostname may have a
266 * trailing port number (separated from the hostname by a ':'), which
267 * will take precedence over the port number supplied as a parameter
270 WLDAP32_LDAP
* CDECL
cldap_openW( PWCHAR hostname
, ULONG portnumber
)
274 char *hostnameU
= NULL
, *url
= NULL
;
276 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
279 hostnameU
= strWtoU( hostname
);
280 if (!hostnameU
) goto exit
;
283 hostnameU
= strWtoU( defaulthost
);
284 if (!hostnameU
) goto exit
;
287 url
= urlify_hostnames( "cldap://", hostnameU
, portnumber
);
290 ld
= create_context( url
);
293 strfreeU( hostnameU
);
302 /***********************************************************************
303 * ldap_connect (WLDAP32.@)
305 * Connect to an LDAP server.
308 * ld [I] Pointer to an LDAP context.
309 * timeout [I] Pointer to an l_timeval structure specifying the
310 * timeout in seconds.
313 * Success: LDAP_SUCCESS
314 * Failure: An LDAP error code.
317 * The timeout parameter may be NULL in which case a default timeout
318 * value will be used.
320 ULONG CDECL
ldap_connect( WLDAP32_LDAP
*ld
, struct l_timeval
*timeout
)
322 TRACE( "(%p, %p)\n", ld
, timeout
);
324 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
325 return WLDAP32_LDAP_SUCCESS
; /* FIXME: do something, e.g. ping the host */
328 /***********************************************************************
329 * ldap_initA (WLDAP32.@)
333 WLDAP32_LDAP
* CDECL
ldap_initA( const PCHAR hostname
, ULONG portnumber
)
336 WLDAP32_LDAP
*ld
= NULL
;
337 WCHAR
*hostnameW
= NULL
;
339 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
342 hostnameW
= strAtoW( hostname
);
343 if (!hostnameW
) goto exit
;
346 ld
= ldap_initW( hostnameW
, portnumber
);
349 strfreeW( hostnameW
);
357 /***********************************************************************
358 * ldap_initW (WLDAP32.@)
360 * Initialize an LDAP context and create a TCP connection.
363 * hostname [I] Name of the host to connect to.
364 * portnumber [I] Port number to use.
367 * Success: Pointer to an LDAP context.
371 * The hostname string can be a space separated string of hostnames,
372 * in which case the LDAP runtime will try to connect to the hosts
373 * in order, until a connection can be made. A hostname may have a
374 * trailing port number (separated from the hostname by a ':'), which
375 * will take precedence over the port number supplied as a parameter
376 * to this function. The connection will not be made until the first
377 * LDAP function that needs it is called.
379 WLDAP32_LDAP
* CDECL
ldap_initW( const PWCHAR hostname
, ULONG portnumber
)
383 char *hostnameU
= NULL
, *url
= NULL
;
385 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
388 hostnameU
= strWtoU( hostname
);
389 if (!hostnameU
) goto exit
;
392 hostnameU
= strWtoU( defaulthost
);
393 if (!hostnameU
) goto exit
;
396 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
399 ld
= create_context( url
);
402 strfreeU( hostnameU
);
411 /***********************************************************************
412 * ldap_openA (WLDAP32.@)
416 WLDAP32_LDAP
* CDECL
ldap_openA( PCHAR hostname
, ULONG portnumber
)
419 WLDAP32_LDAP
*ld
= NULL
;
420 WCHAR
*hostnameW
= NULL
;
422 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
425 hostnameW
= strAtoW( hostname
);
426 if (!hostnameW
) goto exit
;
429 ld
= ldap_openW( hostnameW
, portnumber
);
432 strfreeW( hostnameW
);
440 /***********************************************************************
441 * ldap_openW (WLDAP32.@)
443 * Initialize an LDAP context and create a TCP connection.
446 * hostname [I] Name of the host to connect to.
447 * portnumber [I] Port number to use.
450 * Success: Pointer to an LDAP context.
454 * The hostname string can be a space separated string of hostnames,
455 * in which case the LDAP runtime will try to connect to the hosts
456 * in order, until a connection can be made. A hostname may have a
457 * trailing port number (separated from the hostname by a ':'), which
458 * will take precedence over the port number supplied as a parameter
461 WLDAP32_LDAP
* CDECL
ldap_openW( PWCHAR hostname
, ULONG portnumber
)
465 char *hostnameU
= NULL
, *url
= NULL
;
467 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
470 hostnameU
= strWtoU( hostname
);
471 if (!hostnameU
) goto exit
;
474 hostnameU
= strWtoU( defaulthost
);
475 if (!hostnameU
) goto exit
;
478 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
481 ld
= create_context( url
);
484 strfreeU( hostnameU
);
493 /***********************************************************************
494 * ldap_sslinitA (WLDAP32.@)
498 WLDAP32_LDAP
* CDECL
ldap_sslinitA( PCHAR hostname
, ULONG portnumber
, int secure
)
502 WCHAR
*hostnameW
= NULL
;
504 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname
), portnumber
, secure
);
507 hostnameW
= strAtoW( hostname
);
508 if (!hostnameW
) return NULL
;
511 ld
= ldap_sslinitW( hostnameW
, portnumber
, secure
);
513 strfreeW( hostnameW
);
521 /***********************************************************************
522 * ldap_sslinitW (WLDAP32.@)
524 * Initialize an LDAP context and create a secure TCP connection.
527 * hostname [I] Name of the host to connect to.
528 * portnumber [I] Port number to use.
529 * secure [I] Ask the server to create an SSL connection.
532 * Success: Pointer to an LDAP context.
536 * The hostname string can be a space separated string of hostnames,
537 * in which case the LDAP runtime will try to connect to the hosts
538 * in order, until a connection can be made. A hostname may have a
539 * trailing port number (separated from the hostname by a ':'), which
540 * will take precedence over the port number supplied as a parameter
541 * to this function. The connection will not be made until the first
542 * LDAP function that needs it is called.
544 WLDAP32_LDAP
* CDECL
ldap_sslinitW( PWCHAR hostname
, ULONG portnumber
, int secure
)
547 WLDAP32_LDAP
*ld
= NULL
;
548 char *hostnameU
= NULL
, *url
= NULL
;
550 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname
), portnumber
, secure
);
553 hostnameU
= strWtoU( hostname
);
554 if (!hostnameU
) goto exit
;
557 hostnameU
= strWtoU( defaulthost
);
558 if (!hostnameU
) goto exit
;
562 url
= urlify_hostnames( "ldaps://", hostnameU
, portnumber
);
564 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
567 ldap_initialize( &ld
, url
);
570 strfreeU( hostnameU
);
579 /***********************************************************************
580 * ldap_start_tls_sA (WLDAP32.@)
582 * See ldap_start_tls_sW.
584 ULONG CDECL
ldap_start_tls_sA( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
585 PLDAPControlA
*serverctrls
, PLDAPControlA
*clientctrls
)
587 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
589 LDAPControlW
**serverctrlsW
= NULL
, **clientctrlsW
= NULL
;
591 ret
= WLDAP32_LDAP_NO_MEMORY
;
593 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
598 serverctrlsW
= controlarrayAtoW( serverctrls
);
599 if (!serverctrlsW
) goto exit
;
602 clientctrlsW
= controlarrayAtoW( clientctrls
);
603 if (!clientctrlsW
) goto exit
;
606 ret
= ldap_start_tls_sW( ld
, retval
, result
, serverctrlsW
, clientctrlsW
);
609 controlarrayfreeW( serverctrlsW
);
610 controlarrayfreeW( clientctrlsW
);
616 /***********************************************************************
617 * ldap_start_tls_s (WLDAP32.@)
619 * Start TLS encryption on an LDAP connection.
622 * ld [I] Pointer to an LDAP context.
623 * retval [I] Return value from the server.
624 * result [I] Response message from the server.
625 * serverctrls [I] Array of LDAP server controls.
626 * clientctrls [I] Array of LDAP client controls.
629 * Success: LDAP_SUCCESS
630 * Failure: An LDAP error code.
633 * LDAP function that needs it is called.
635 ULONG CDECL
ldap_start_tls_sW( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
636 PLDAPControlW
*serverctrls
, PLDAPControlW
*clientctrls
)
638 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
640 LDAPControl
**serverctrlsU
= NULL
, **clientctrlsU
= NULL
;
642 ret
= WLDAP32_LDAP_NO_MEMORY
;
644 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
649 serverctrlsU
= controlarrayWtoU( serverctrls
);
650 if (!serverctrlsU
) goto exit
;
653 clientctrlsU
= controlarrayWtoU( clientctrls
);
654 if (!clientctrlsU
) goto exit
;
657 ret
= map_error( ldap_start_tls_s( ld
, serverctrlsU
, clientctrlsU
));
660 controlarrayfreeU( serverctrlsU
);
661 controlarrayfreeU( clientctrlsU
);
667 /***********************************************************************
668 * ldap_startup (WLDAP32.@)
670 ULONG CDECL
ldap_startup( PLDAP_VERSION_INFO version
, HANDLE
*instance
)
672 TRACE( "(%p, %p)\n", version
, instance
);
673 return WLDAP32_LDAP_SUCCESS
;
676 /***********************************************************************
677 * ldap_stop_tls_s (WLDAP32.@)
679 * Stop TLS encryption on an LDAP connection.
682 * ld [I] Pointer to an LDAP context.
688 BOOLEAN CDECL
ldap_stop_tls_s( WLDAP32_LDAP
*ld
)
690 TRACE( "(%p)\n", ld
);
691 return TRUE
; /* FIXME: find a way to stop tls on a connection */