usp10: Update tests in test_ScriptItemIzeShapePlace to match Windows results.
[wine/multimedia.git] / dlls / wldap32 / init.c
blobc7bf4e016aaa57812d2b36f37705ac374b5ca3cc
1 /*
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 "config.h"
23 #include "wine/port.h"
24 #include "wine/debug.h"
26 #include <stdio.h>
27 #include <stdarg.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
33 #ifdef HAVE_LDAP_H
34 #include <ldap.h>
35 #else
36 #define LDAP_SUCCESS 0x00
37 #define LDAP_NOT_SUPPORTED 0x5c
38 #endif
40 #include "winldap_private.h"
41 #include "wldap32.h"
43 #ifdef HAVE_LDAP
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;
51 unsigned int i = 0;
53 str = strdupU( hostnames );
54 if (!str) return NULL;
56 p = str;
57 while (isspace( *p )) p++;
58 if (*p) i++;
60 while (*p)
62 if (isspace( *p ))
64 while (isspace( *p )) p++;
65 if (*p) i++;
67 p++;
70 res = HeapAlloc( GetProcessHeap(), 0, (i + 1) * sizeof(char *) );
71 if (!res)
73 HeapFree( GetProcessHeap(), 0, str );
74 return NULL;
77 p = str;
78 while (isspace( *p )) p++;
80 q = p;
81 i = 0;
83 while (*p)
85 if (p[1] != '\0')
87 if (isspace( *p ))
89 *p = '\0'; p++;
90 res[i] = strdupU( q );
91 if (!res[i]) goto oom;
92 i++;
94 while (isspace( *p )) p++;
95 q = p;
98 else
100 res[i] = strdupU( q );
101 if (!res[i]) goto oom;
102 i++;
104 p++;
106 res[i] = NULL;
108 HeapFree( GetProcessHeap(), 0, str );
109 return res;
111 oom:
112 for (--i; i >= 0; i--)
113 strfreeU( res[i] );
115 HeapFree( GetProcessHeap(), 0, res );
116 HeapFree( GetProcessHeap(), 0, str );
118 return NULL;
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;
128 return 0;
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
133 * if necessary.
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";
140 char port[7];
142 sprintf( port, fmt, portnumber );
144 for (v = hostnames; *v; v++)
146 if (!has_ldap_scheme( *v ))
148 size += strlen( scheme );
149 q = *v;
151 else
152 /* skip past colon in scheme prefix */
153 q = strchr( *v, '/' );
155 size += strlen( *v );
157 if (!strchr( q, ':' ))
158 size += strlen( port );
160 i++;
163 size += (i - 1) * strlen( sep );
165 res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
166 if (!res) return NULL;
168 p = res;
169 for (v = hostnames; *v; v++)
171 if (v != hostnames)
173 strcpy( p, sep );
174 p += strlen( sep );
177 if (!has_ldap_scheme( *v ))
179 strcpy( p, scheme );
180 p += strlen( scheme );
181 q = *v;
183 else
184 /* skip past colon in scheme prefix */
185 q = strchr( *v, '/' );
187 strcpy( p, *v );
188 p += strlen( *v );
190 if (!strchr( q, ':' ))
192 strcpy( p, port );
193 p += strlen( port );
196 return res;
199 static char *urlify_hostnames( char *scheme, char *hostnames, ULONG port )
201 char *url = NULL, **strarray;
203 strarray = split_hostnames( hostnames );
204 if (strarray)
205 url = join_hostnames( scheme, strarray, port );
206 else
207 return NULL;
209 strarrayfreeU( strarray );
210 return url;
212 #endif
214 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
216 /***********************************************************************
217 * cldap_openA (WLDAP32.@)
219 * See cldap_openW.
221 WLDAP32_LDAP *cldap_openA( PCHAR hostname, ULONG portnumber )
223 #ifdef HAVE_LDAP
224 WLDAP32_LDAP *ld = NULL;
225 WCHAR *hostnameW = NULL;
227 TRACE( "(%s, %ld)\n", debugstr_a(hostname), portnumber );
229 if (hostname) {
230 hostnameW = strAtoW( hostname );
231 if (!hostnameW) goto exit;
234 ld = cldap_openW( hostnameW, portnumber );
236 exit:
237 strfreeW( hostnameW );
238 return ld;
240 #endif
241 return NULL;
244 /***********************************************************************
245 * cldap_openW (WLDAP32.@)
247 * Initialize an LDAP context and create a UDP connection.
249 * PARAMS
250 * hostname [I] Name of the host to connect to.
251 * portnumber [I] Portnumber to use.
253 * RETURNS
254 * Success: Pointer to an LDAP context.
255 * Failure: NULL
257 * NOTES
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
263 * to this function.
265 WLDAP32_LDAP *cldap_openW( PWCHAR hostname, ULONG portnumber )
267 #ifdef HAVE_LDAP
268 LDAP *ld = NULL;
269 char *hostnameU = NULL, *url = NULL;
271 TRACE( "(%s, %ld)\n", debugstr_w(hostname), portnumber );
273 if (hostname) {
274 hostnameU = strWtoU( hostname );
275 if (!hostnameU) goto exit;
277 else {
278 hostnameU = strWtoU( defaulthost );
279 if (!hostnameU) goto exit;
282 url = urlify_hostnames( "cldap://", hostnameU, portnumber );
283 if (!url) goto exit;
285 ldap_initialize( &ld, url );
287 exit:
288 strfreeU( hostnameU );
289 strfreeU( url );
290 return ld;
292 #endif
293 return NULL;
296 /***********************************************************************
297 * ldap_connect (WLDAP32.@)
299 * Connect to an LDAP server.
301 * PARAMS
302 * ld [I] Pointer to an LDAP context.
303 * timeout [I] Pointer to an l_timeval structure specifying the
304 * timeout in seconds.
306 * RETURNS
307 * Success: LDAP_SUCCESS
308 * Failure: An LDAP error code.
310 * NOTES
311 * The timeout parameter may be NULL in which case a default timeout
312 * value will be used.
314 ULONG 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.@)
325 * See ldap_initW.
327 WLDAP32_LDAP *ldap_initA( PCHAR hostname, ULONG portnumber )
329 #ifdef HAVE_LDAP
330 WLDAP32_LDAP *ld = NULL;
331 WCHAR *hostnameW = NULL;
333 TRACE( "(%s, %ld)\n", debugstr_a(hostname), portnumber );
335 if (hostname) {
336 hostnameW = strAtoW( hostname );
337 if (!hostnameW) goto exit;
340 ld = ldap_initW( hostnameW, portnumber );
342 exit:
343 strfreeW( hostnameW );
344 return ld;
346 #endif
347 return NULL;
350 /***********************************************************************
351 * ldap_initW (WLDAP32.@)
353 * Initialize an LDAP context and create a TCP connection.
355 * PARAMS
356 * hostname [I] Name of the host to connect to.
357 * portnumber [I] Portnumber to use.
359 * RETURNS
360 * Success: Pointer to an LDAP context.
361 * Failure: NULL
363 * NOTES
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 *ldap_initW( PWCHAR hostname, ULONG portnumber )
374 #ifdef HAVE_LDAP
375 LDAP *ld = NULL;
376 char *hostnameU = NULL, *url = NULL;
378 TRACE( "(%s, %ld)\n", debugstr_w(hostname), portnumber );
380 if (hostname) {
381 hostnameU = strWtoU( hostname );
382 if (!hostnameU) goto exit;
384 else {
385 hostnameU = strWtoU( defaulthost );
386 if (!hostnameU) goto exit;
389 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
390 if (!url) goto exit;
392 ldap_initialize( &ld, url );
394 exit:
395 strfreeU( hostnameU );
396 strfreeU( url );
397 return ld;
399 #endif
400 return NULL;
403 /***********************************************************************
404 * ldap_openA (WLDAP32.@)
406 * See ldap_openW.
408 WLDAP32_LDAP *ldap_openA( PCHAR hostname, ULONG portnumber )
410 #ifdef HAVE_LDAP
411 WLDAP32_LDAP *ld = NULL;
412 WCHAR *hostnameW = NULL;
414 TRACE( "(%s, %ld)\n", debugstr_a(hostname), portnumber );
416 if (hostname) {
417 hostnameW = strAtoW( hostname );
418 if (!hostnameW) goto exit;
421 ld = ldap_openW( hostnameW, portnumber );
423 exit:
424 strfreeW( hostnameW );
425 return ld;
427 #endif
428 return NULL;
431 /***********************************************************************
432 * ldap_openW (WLDAP32.@)
434 * Initialize an LDAP context and create a TCP connection.
436 * PARAMS
437 * hostname [I] Name of the host to connect to.
438 * portnumber [I] Portnumber to use.
440 * RETURNS
441 * Success: Pointer to an LDAP context.
442 * Failure: NULL
444 * NOTES
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
450 * to this function.
452 WLDAP32_LDAP *ldap_openW( PWCHAR hostname, ULONG portnumber )
454 #ifdef HAVE_LDAP
455 LDAP *ld = NULL;
456 char *hostnameU = NULL, *url = NULL;
458 TRACE( "(%s, %ld)\n", debugstr_w(hostname), portnumber );
460 if (hostname) {
461 hostnameU = strWtoU( hostname );
462 if (!hostnameU) goto exit;
464 else {
465 hostnameU = strWtoU( defaulthost );
466 if (!hostnameU) goto exit;
469 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
470 if (!url) goto exit;
472 ldap_initialize( &ld, url );
474 exit:
475 strfreeU( hostnameU );
476 strfreeU( url );
477 return ld;
479 #endif
480 return NULL;
483 /***********************************************************************
484 * ldap_sslinitA (WLDAP32.@)
486 * See ldap_sslinitW.
488 WLDAP32_LDAP *ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure )
490 #ifdef HAVE_LDAP
491 WLDAP32_LDAP *ld;
492 WCHAR *hostnameW = NULL;
494 TRACE( "(%s, %ld, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
496 if (hostname) {
497 hostnameW = strAtoW( hostname );
498 if (!hostnameW) return NULL;
501 ld = ldap_sslinitW( hostnameW, portnumber, secure );
503 strfreeW( hostnameW );
504 return ld;
506 #endif
507 return NULL;
510 /***********************************************************************
511 * ldap_sslinitW (WLDAP32.@)
513 * Initialize an LDAP context and create a secure TCP connection.
515 * PARAMS
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.
520 * RETURNS
521 * Success: Pointer to an LDAP context.
522 * Failure: NULL
524 * NOTES
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 *ldap_sslinitW( PWCHAR hostname, ULONG portnumber, int secure )
535 #ifdef HAVE_LDAP
536 WLDAP32_LDAP *ld = NULL;
537 char *hostnameU = NULL, *url = NULL;
539 TRACE( "(%s, %ld, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
541 if (hostname) {
542 hostnameU = strWtoU( hostname );
543 if (!hostnameU) goto exit;
545 else {
546 hostnameU = strWtoU( defaulthost );
547 if (!hostnameU) goto exit;
550 if (secure)
551 url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
552 else
553 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
555 if (!url) goto exit;
556 ldap_initialize( &ld, url );
558 exit:
559 strfreeU( hostnameU );
560 strfreeU( url );
561 return ld;
563 #endif
564 return NULL;
567 /***********************************************************************
568 * ldap_start_tls_sA (WLDAP32.@)
570 * See ldap_start_tls_sW.
572 ULONG ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
573 PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
575 ULONG ret = LDAP_NOT_SUPPORTED;
576 #ifdef HAVE_LDAP
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;
585 if (serverctrls) {
586 serverctrlsW = controlarrayAtoW( serverctrls );
587 if (!serverctrlsW) goto exit;
589 if (clientctrls) {
590 clientctrlsW = controlarrayAtoW( clientctrls );
591 if (!clientctrlsW) goto exit;
594 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
596 exit:
597 controlarrayfreeW( serverctrlsW );
598 controlarrayfreeW( clientctrlsW );
600 #endif
601 return ret;
604 /***********************************************************************
605 * ldap_start_tls_s (WLDAP32.@)
607 * Start TLS encryption on an LDAP connection.
609 * PARAMS
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.
616 * RETURNS
617 * Success: LDAP_SUCCESS
618 * Failure: An LDAP error code.
620 * NOTES
621 * LDAP function that needs it is called.
623 ULONG ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
624 PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
626 ULONG ret = LDAP_NOT_SUPPORTED;
627 #ifdef HAVE_LDAP
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;
636 if (serverctrls) {
637 serverctrlsU = controlarrayWtoU( serverctrls );
638 if (!serverctrlsU) goto exit;
640 if (clientctrls) {
641 clientctrlsU = controlarrayWtoU( clientctrls );
642 if (!clientctrlsU) goto exit;
645 ret = ldap_start_tls_s( ld, serverctrlsU, clientctrlsU );
647 exit:
648 controlarrayfreeU( serverctrlsU );
649 controlarrayfreeU( clientctrlsU );
651 #endif
652 return ret;
655 /***********************************************************************
656 * ldap_startup (WLDAP32.@)
658 ULONG ldap_startup( PLDAP_VERSION_INFO version, HANDLE *instance )
660 TRACE( "(%p, %p)\n", version, instance );
661 return LDAP_SUCCESS;
664 /***********************************************************************
665 * ldap_stop_tls_s (WLDAP32.@)
667 * Stop TLS encryption on an LDAP connection.
669 * PARAMS
670 * ld [I] Pointer to an LDAP context.
672 * RETURNS
673 * Success: TRUE
674 * Failure: FALSE
676 BOOLEAN 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 */