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 BOOL
has_ldap_scheme( char *url
)
103 return !strncasecmp( url
, "ldap://", 7 ) ||
104 !strncasecmp( url
, "ldaps://", 8 ) ||
105 !strncasecmp( url
, "ldapi://", 8 ) ||
106 !strncasecmp( url
, "cldap://", 8 );
109 /* Flatten an array of hostnames into a space separated string of URLs.
110 * Prepend a given scheme and append a given port number to each hostname
113 static char *join_hostnames( const char *scheme
, char **hostnames
, ULONG portnumber
)
115 char *res
, *p
, *q
, **v
;
116 unsigned int i
= 0, size
= 0;
117 static const char sep
[] = " ", fmt
[] = ":%d";
120 sprintf( port
, fmt
, portnumber
);
122 for (v
= hostnames
; *v
; v
++)
124 if (!has_ldap_scheme( *v
))
126 size
+= strlen( scheme
);
130 /* skip past colon in scheme prefix */
131 q
= strchr( *v
, '/' );
133 size
+= strlen( *v
);
135 if (!strchr( q
, ':' ))
136 size
+= strlen( port
);
141 size
+= (i
- 1) * strlen( sep
);
143 res
= HeapAlloc( GetProcessHeap(), 0, size
+ 1 );
144 if (!res
) return NULL
;
147 for (v
= hostnames
; *v
; v
++)
155 if (!has_ldap_scheme( *v
))
158 p
+= strlen( scheme
);
162 /* skip past colon in scheme prefix */
163 q
= strchr( *v
, '/' );
168 if (!strchr( q
, ':' ))
177 static char *urlify_hostnames( const char *scheme
, char *hostnames
, ULONG port
)
179 char *url
= NULL
, **strarray
;
181 strarray
= split_hostnames( hostnames
);
183 url
= join_hostnames( scheme
, strarray
, port
);
187 strarrayfreeU( strarray
);
192 /***********************************************************************
193 * cldap_openA (WLDAP32.@)
197 WLDAP32_LDAP
* CDECL
cldap_openA( PCHAR hostname
, ULONG portnumber
)
200 WLDAP32_LDAP
*ld
= NULL
;
201 WCHAR
*hostnameW
= NULL
;
203 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
206 hostnameW
= strAtoW( hostname
);
207 if (!hostnameW
) goto exit
;
210 ld
= cldap_openW( hostnameW
, portnumber
);
213 strfreeW( hostnameW
);
221 /***********************************************************************
222 * cldap_openW (WLDAP32.@)
224 * Initialize an LDAP context and create a UDP connection.
227 * hostname [I] Name of the host to connect to.
228 * portnumber [I] Port number to use.
231 * Success: Pointer to an LDAP context.
235 * The hostname string can be a space separated string of hostnames,
236 * in which case the LDAP runtime will try to connect to the hosts
237 * in order, until a connection can be made. A hostname may have a
238 * trailing port number (separated from the hostname by a ':'), which
239 * will take precedence over the port number supplied as a parameter
242 WLDAP32_LDAP
* CDECL
cldap_openW( PWCHAR hostname
, ULONG portnumber
)
246 char *hostnameU
= NULL
, *url
= NULL
;
248 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
251 hostnameU
= strWtoU( hostname
);
252 if (!hostnameU
) goto exit
;
255 hostnameU
= strWtoU( defaulthost
);
256 if (!hostnameU
) goto exit
;
259 url
= urlify_hostnames( "cldap://", hostnameU
, portnumber
);
262 ldap_initialize( &ld
, url
);
265 strfreeU( hostnameU
);
274 /***********************************************************************
275 * ldap_connect (WLDAP32.@)
277 * Connect to an LDAP server.
280 * ld [I] Pointer to an LDAP context.
281 * timeout [I] Pointer to an l_timeval structure specifying the
282 * timeout in seconds.
285 * Success: LDAP_SUCCESS
286 * Failure: An LDAP error code.
289 * The timeout parameter may be NULL in which case a default timeout
290 * value will be used.
292 ULONG CDECL
ldap_connect( WLDAP32_LDAP
*ld
, struct l_timeval
*timeout
)
294 TRACE( "(%p, %p)\n", ld
, timeout
);
296 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
297 return WLDAP32_LDAP_SUCCESS
; /* FIXME: do something, e.g. ping the host */
300 /***********************************************************************
301 * ldap_initA (WLDAP32.@)
305 WLDAP32_LDAP
* CDECL
ldap_initA( PCHAR hostname
, ULONG portnumber
)
308 WLDAP32_LDAP
*ld
= NULL
;
309 WCHAR
*hostnameW
= NULL
;
311 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
314 hostnameW
= strAtoW( hostname
);
315 if (!hostnameW
) goto exit
;
318 ld
= ldap_initW( hostnameW
, portnumber
);
321 strfreeW( hostnameW
);
329 /***********************************************************************
330 * ldap_initW (WLDAP32.@)
332 * Initialize an LDAP context and create a TCP connection.
335 * hostname [I] Name of the host to connect to.
336 * portnumber [I] Port number to use.
339 * Success: Pointer to an LDAP context.
343 * The hostname string can be a space separated string of hostnames,
344 * in which case the LDAP runtime will try to connect to the hosts
345 * in order, until a connection can be made. A hostname may have a
346 * trailing port number (separated from the hostname by a ':'), which
347 * will take precedence over the port number supplied as a parameter
348 * to this function. The connection will not be made until the first
349 * LDAP function that needs it is called.
351 WLDAP32_LDAP
* CDECL
ldap_initW( PWCHAR hostname
, ULONG portnumber
)
355 char *hostnameU
= NULL
, *url
= NULL
;
357 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
360 hostnameU
= strWtoU( hostname
);
361 if (!hostnameU
) goto exit
;
364 hostnameU
= strWtoU( defaulthost
);
365 if (!hostnameU
) goto exit
;
368 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
371 ldap_initialize( &ld
, url
);
374 strfreeU( hostnameU
);
383 /***********************************************************************
384 * ldap_openA (WLDAP32.@)
388 WLDAP32_LDAP
* CDECL
ldap_openA( PCHAR hostname
, ULONG portnumber
)
391 WLDAP32_LDAP
*ld
= NULL
;
392 WCHAR
*hostnameW
= NULL
;
394 TRACE( "(%s, %d)\n", debugstr_a(hostname
), portnumber
);
397 hostnameW
= strAtoW( hostname
);
398 if (!hostnameW
) goto exit
;
401 ld
= ldap_openW( hostnameW
, portnumber
);
404 strfreeW( hostnameW
);
412 /***********************************************************************
413 * ldap_openW (WLDAP32.@)
415 * Initialize an LDAP context and create a TCP connection.
418 * hostname [I] Name of the host to connect to.
419 * portnumber [I] Port number to use.
422 * Success: Pointer to an LDAP context.
426 * The hostname string can be a space separated string of hostnames,
427 * in which case the LDAP runtime will try to connect to the hosts
428 * in order, until a connection can be made. A hostname may have a
429 * trailing port number (separated from the hostname by a ':'), which
430 * will take precedence over the port number supplied as a parameter
433 WLDAP32_LDAP
* CDECL
ldap_openW( PWCHAR hostname
, ULONG portnumber
)
437 char *hostnameU
= NULL
, *url
= NULL
;
439 TRACE( "(%s, %d)\n", debugstr_w(hostname
), portnumber
);
442 hostnameU
= strWtoU( hostname
);
443 if (!hostnameU
) goto exit
;
446 hostnameU
= strWtoU( defaulthost
);
447 if (!hostnameU
) goto exit
;
450 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
453 ldap_initialize( &ld
, url
);
456 strfreeU( hostnameU
);
465 /***********************************************************************
466 * ldap_sslinitA (WLDAP32.@)
470 WLDAP32_LDAP
* CDECL
ldap_sslinitA( PCHAR hostname
, ULONG portnumber
, int secure
)
474 WCHAR
*hostnameW
= NULL
;
476 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname
), portnumber
, secure
);
479 hostnameW
= strAtoW( hostname
);
480 if (!hostnameW
) return NULL
;
483 ld
= ldap_sslinitW( hostnameW
, portnumber
, secure
);
485 strfreeW( hostnameW
);
493 /***********************************************************************
494 * ldap_sslinitW (WLDAP32.@)
496 * Initialize an LDAP context and create a secure TCP connection.
499 * hostname [I] Name of the host to connect to.
500 * portnumber [I] Port number to use.
501 * secure [I] Ask the server to create an SSL connection.
504 * Success: Pointer to an LDAP context.
508 * The hostname string can be a space separated string of hostnames,
509 * in which case the LDAP runtime will try to connect to the hosts
510 * in order, until a connection can be made. A hostname may have a
511 * trailing port number (separated from the hostname by a ':'), which
512 * will take precedence over the port number supplied as a parameter
513 * to this function. The connection will not be made until the first
514 * LDAP function that needs it is called.
516 WLDAP32_LDAP
* CDECL
ldap_sslinitW( PWCHAR hostname
, ULONG portnumber
, int secure
)
519 WLDAP32_LDAP
*ld
= NULL
;
520 char *hostnameU
= NULL
, *url
= NULL
;
522 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname
), portnumber
, secure
);
525 hostnameU
= strWtoU( hostname
);
526 if (!hostnameU
) goto exit
;
529 hostnameU
= strWtoU( defaulthost
);
530 if (!hostnameU
) goto exit
;
534 url
= urlify_hostnames( "ldaps://", hostnameU
, portnumber
);
536 url
= urlify_hostnames( "ldap://", hostnameU
, portnumber
);
539 ldap_initialize( &ld
, url
);
542 strfreeU( hostnameU
);
551 /***********************************************************************
552 * ldap_start_tls_sA (WLDAP32.@)
554 * See ldap_start_tls_sW.
556 ULONG CDECL
ldap_start_tls_sA( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
557 PLDAPControlA
*serverctrls
, PLDAPControlA
*clientctrls
)
559 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
561 LDAPControlW
**serverctrlsW
= NULL
, **clientctrlsW
= NULL
;
563 ret
= WLDAP32_LDAP_NO_MEMORY
;
565 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
570 serverctrlsW
= controlarrayAtoW( serverctrls
);
571 if (!serverctrlsW
) goto exit
;
574 clientctrlsW
= controlarrayAtoW( clientctrls
);
575 if (!clientctrlsW
) goto exit
;
578 ret
= ldap_start_tls_sW( ld
, retval
, result
, serverctrlsW
, clientctrlsW
);
581 controlarrayfreeW( serverctrlsW
);
582 controlarrayfreeW( clientctrlsW
);
588 /***********************************************************************
589 * ldap_start_tls_s (WLDAP32.@)
591 * Start TLS encryption on an LDAP connection.
594 * ld [I] Pointer to an LDAP context.
595 * retval [I] Return value from the server.
596 * result [I] Response message from the server.
597 * serverctrls [I] Array of LDAP server controls.
598 * clientctrls [I] Array of LDAP client controls.
601 * Success: LDAP_SUCCESS
602 * Failure: An LDAP error code.
605 * LDAP function that needs it is called.
607 ULONG CDECL
ldap_start_tls_sW( WLDAP32_LDAP
*ld
, PULONG retval
, WLDAP32_LDAPMessage
**result
,
608 PLDAPControlW
*serverctrls
, PLDAPControlW
*clientctrls
)
610 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
612 LDAPControl
**serverctrlsU
= NULL
, **clientctrlsU
= NULL
;
614 ret
= WLDAP32_LDAP_NO_MEMORY
;
616 TRACE( "(%p, %p, %p, %p, %p)\n", ld
, retval
, result
, serverctrls
, clientctrls
);
621 serverctrlsU
= controlarrayWtoU( serverctrls
);
622 if (!serverctrlsU
) goto exit
;
625 clientctrlsU
= controlarrayWtoU( clientctrls
);
626 if (!clientctrlsU
) goto exit
;
629 ret
= map_error( ldap_start_tls_s( ld
, serverctrlsU
, clientctrlsU
));
632 controlarrayfreeU( serverctrlsU
);
633 controlarrayfreeU( clientctrlsU
);
639 /***********************************************************************
640 * ldap_startup (WLDAP32.@)
642 ULONG CDECL
ldap_startup( PLDAP_VERSION_INFO version
, HANDLE
*instance
)
644 TRACE( "(%p, %p)\n", version
, instance
);
645 return WLDAP32_LDAP_SUCCESS
;
648 /***********************************************************************
649 * ldap_stop_tls_s (WLDAP32.@)
651 * Stop TLS encryption on an LDAP connection.
654 * ld [I] Pointer to an LDAP context.
660 BOOLEAN CDECL
ldap_stop_tls_s( WLDAP32_LDAP
*ld
)
662 TRACE( "(%p)\n", ld
);
663 return TRUE
; /* FIXME: find a way to stop tls on a connection */