Sync with trunk r58151 to bring the latest changes from Amine and Timo.
[reactos.git] / dll / win32 / wldap32 / init.c
1 /*
2 * WLDAP32 - LDAP support for Wine
3 *
4 * Copyright 2005 Hans Leidekker
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32
33 #ifdef HAVE_LDAP_H
34 #include <ldap.h>
35 #endif
36
37 #include "winldap_private.h"
38 #include "wldap32.h"
39
40 #ifdef HAVE_LDAP
41 /* Should eventually be determined by the algorithm documented on MSDN. */
42 static const WCHAR defaulthost[] = { 'l','o','c','a','l','h','o','s','t',0 };
43
44 /* Split a space separated string of hostnames into a string array */
45 static char **split_hostnames( const char *hostnames )
46 {
47 char **res, *str, *p, *q;
48 unsigned int i = 0;
49
50 str = strdupU( hostnames );
51 if (!str) return NULL;
52
53 p = str;
54 while (isspace( *p )) p++;
55 if (*p) i++;
56
57 while (*p)
58 {
59 if (isspace( *p ))
60 {
61 while (isspace( *p )) p++;
62 if (*p) i++;
63 }
64 p++;
65 }
66
67 res = HeapAlloc( GetProcessHeap(), 0, (i + 1) * sizeof(char *) );
68 if (!res)
69 {
70 HeapFree( GetProcessHeap(), 0, str );
71 return NULL;
72 }
73
74 p = str;
75 while (isspace( *p )) p++;
76
77 q = p;
78 i = 0;
79
80 while (*p)
81 {
82 if (p[1] != '\0')
83 {
84 if (isspace( *p ))
85 {
86 *p = '\0'; p++;
87 res[i] = strdupU( q );
88 if (!res[i]) goto oom;
89 i++;
90
91 while (isspace( *p )) p++;
92 q = p;
93 }
94 }
95 else
96 {
97 res[i] = strdupU( q );
98 if (!res[i]) goto oom;
99 i++;
100 }
101 p++;
102 }
103 res[i] = NULL;
104
105 HeapFree( GetProcessHeap(), 0, str );
106 return res;
107
108 oom:
109 while (i > 0) strfreeU( res[--i] );
110
111 HeapFree( GetProcessHeap(), 0, res );
112 HeapFree( GetProcessHeap(), 0, str );
113
114 return NULL;
115 }
116
117 /* Determine if a URL starts with a known LDAP scheme */
118 static int has_ldap_scheme( char *url )
119 {
120 if (!strncasecmp( url, "ldap://", 7 ) ||
121 !strncasecmp( url, "ldaps://", 8 ) ||
122 !strncasecmp( url, "ldapi://", 8 ) ||
123 !strncasecmp( url, "cldap://", 8 )) return 1;
124 return 0;
125 }
126
127 /* Flatten an array of hostnames into a space separated string of URLs.
128 * Prepend a given scheme and append a given portnumber to each hostname
129 * if necessary.
130 */
131 static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnumber )
132 {
133 char *res, *p, *q, **v;
134 unsigned int i = 0, size = 0;
135 static const char sep[] = " ", fmt[] = ":%d";
136 char port[7];
137
138 sprintf( port, fmt, portnumber );
139
140 for (v = hostnames; *v; v++)
141 {
142 if (!has_ldap_scheme( *v ))
143 {
144 size += strlen( scheme );
145 q = *v;
146 }
147 else
148 /* skip past colon in scheme prefix */
149 q = strchr( *v, '/' );
150
151 size += strlen( *v );
152
153 if (!strchr( q, ':' ))
154 size += strlen( port );
155
156 i++;
157 }
158
159 size += (i - 1) * strlen( sep );
160
161 res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
162 if (!res) return NULL;
163
164 p = res;
165 for (v = hostnames; *v; v++)
166 {
167 if (v != hostnames)
168 {
169 strcpy( p, sep );
170 p += strlen( sep );
171 }
172
173 if (!has_ldap_scheme( *v ))
174 {
175 strcpy( p, scheme );
176 p += strlen( scheme );
177 q = *v;
178 }
179 else
180 /* skip past colon in scheme prefix */
181 q = strchr( *v, '/' );
182
183 strcpy( p, *v );
184 p += strlen( *v );
185
186 if (!strchr( q, ':' ))
187 {
188 strcpy( p, port );
189 p += strlen( port );
190 }
191 }
192 return res;
193 }
194
195 static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
196 {
197 char *url = NULL, **strarray;
198
199 strarray = split_hostnames( hostnames );
200 if (strarray)
201 url = join_hostnames( scheme, strarray, port );
202 else
203 return NULL;
204
205 strarrayfreeU( strarray );
206 return url;
207 }
208 #endif
209
210 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
211
212 /***********************************************************************
213 * cldap_openA (WLDAP32.@)
214 *
215 * See cldap_openW.
216 */
217 WLDAP32_LDAP * CDECL cldap_openA( PCHAR hostname, ULONG portnumber )
218 {
219 #ifdef HAVE_LDAP
220 WLDAP32_LDAP *ld = NULL;
221 WCHAR *hostnameW = NULL;
222
223 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
224
225 if (hostname) {
226 hostnameW = strAtoW( hostname );
227 if (!hostnameW) goto exit;
228 }
229
230 ld = cldap_openW( hostnameW, portnumber );
231
232 exit:
233 strfreeW( hostnameW );
234 return ld;
235
236 #else
237 return NULL;
238 #endif
239 }
240
241 /***********************************************************************
242 * cldap_openW (WLDAP32.@)
243 *
244 * Initialize an LDAP context and create a UDP connection.
245 *
246 * PARAMS
247 * hostname [I] Name of the host to connect to.
248 * portnumber [I] Portnumber to use.
249 *
250 * RETURNS
251 * Success: Pointer to an LDAP context.
252 * Failure: NULL
253 *
254 * NOTES
255 * The hostname string can be a space separated string of hostnames,
256 * in which case the LDAP runtime will try to connect to the hosts
257 * in order, until a connection can be made. A hostname may have a
258 * trailing portnumber (separated from the hostname by a ':'), which
259 * will take precedence over the portnumber supplied as a parameter
260 * to this function.
261 */
262 WLDAP32_LDAP * CDECL cldap_openW( PWCHAR hostname, ULONG portnumber )
263 {
264 #ifdef HAVE_LDAP
265 LDAP *ld = NULL;
266 char *hostnameU = NULL, *url = NULL;
267
268 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
269
270 if (hostname) {
271 hostnameU = strWtoU( hostname );
272 if (!hostnameU) goto exit;
273 }
274 else {
275 hostnameU = strWtoU( defaulthost );
276 if (!hostnameU) goto exit;
277 }
278
279 url = urlify_hostnames( "cldap://", hostnameU, portnumber );
280 if (!url) goto exit;
281
282 ldap_initialize( &ld, url );
283
284 exit:
285 strfreeU( hostnameU );
286 strfreeU( url );
287 return ld;
288
289 #else
290 return NULL;
291 #endif
292 }
293
294 /***********************************************************************
295 * ldap_connect (WLDAP32.@)
296 *
297 * Connect to an LDAP server.
298 *
299 * PARAMS
300 * ld [I] Pointer to an LDAP context.
301 * timeout [I] Pointer to an l_timeval structure specifying the
302 * timeout in seconds.
303 *
304 * RETURNS
305 * Success: LDAP_SUCCESS
306 * Failure: An LDAP error code.
307 *
308 * NOTES
309 * The timeout parameter may be NULL in which case a default timeout
310 * value will be used.
311 */
312 ULONG CDECL ldap_connect( WLDAP32_LDAP *ld, struct l_timeval *timeout )
313 {
314 TRACE( "(%p, %p)\n", ld, timeout );
315
316 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
317 return WLDAP32_LDAP_SUCCESS; /* FIXME: do something, e.g. ping the host */
318 }
319
320 /***********************************************************************
321 * ldap_initA (WLDAP32.@)
322 *
323 * See ldap_initW.
324 */
325 WLDAP32_LDAP * CDECL ldap_initA( PCHAR hostname, ULONG portnumber )
326 {
327 #ifdef HAVE_LDAP
328 WLDAP32_LDAP *ld = NULL;
329 WCHAR *hostnameW = NULL;
330
331 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
332
333 if (hostname) {
334 hostnameW = strAtoW( hostname );
335 if (!hostnameW) goto exit;
336 }
337
338 ld = ldap_initW( hostnameW, portnumber );
339
340 exit:
341 strfreeW( hostnameW );
342 return ld;
343
344 #else
345 return NULL;
346 #endif
347 }
348
349 /***********************************************************************
350 * ldap_initW (WLDAP32.@)
351 *
352 * Initialize an LDAP context and create a TCP connection.
353 *
354 * PARAMS
355 * hostname [I] Name of the host to connect to.
356 * portnumber [I] Portnumber to use.
357 *
358 * RETURNS
359 * Success: Pointer to an LDAP context.
360 * Failure: NULL
361 *
362 * NOTES
363 * The hostname string can be a space separated string of hostnames,
364 * in which case the LDAP runtime will try to connect to the hosts
365 * in order, until a connection can be made. A hostname may have a
366 * trailing portnumber (separated from the hostname by a ':'), which
367 * will take precedence over the portnumber supplied as a parameter
368 * to this function. The connection will not be made until the first
369 * LDAP function that needs it is called.
370 */
371 WLDAP32_LDAP * CDECL ldap_initW( PWCHAR hostname, ULONG portnumber )
372 {
373 #ifdef HAVE_LDAP
374 LDAP *ld = NULL;
375 char *hostnameU = NULL, *url = NULL;
376
377 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
378
379 if (hostname) {
380 hostnameU = strWtoU( hostname );
381 if (!hostnameU) goto exit;
382 }
383 else {
384 hostnameU = strWtoU( defaulthost );
385 if (!hostnameU) goto exit;
386 }
387
388 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
389 if (!url) goto exit;
390
391 ldap_initialize( &ld, url );
392
393 exit:
394 strfreeU( hostnameU );
395 strfreeU( url );
396 return ld;
397
398 #else
399 return NULL;
400 #endif
401 }
402
403 /***********************************************************************
404 * ldap_openA (WLDAP32.@)
405 *
406 * See ldap_openW.
407 */
408 WLDAP32_LDAP * CDECL ldap_openA( PCHAR hostname, ULONG portnumber )
409 {
410 #ifdef HAVE_LDAP
411 WLDAP32_LDAP *ld = NULL;
412 WCHAR *hostnameW = NULL;
413
414 TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
415
416 if (hostname) {
417 hostnameW = strAtoW( hostname );
418 if (!hostnameW) goto exit;
419 }
420
421 ld = ldap_openW( hostnameW, portnumber );
422
423 exit:
424 strfreeW( hostnameW );
425 return ld;
426
427 #else
428 return NULL;
429 #endif
430 }
431
432 /***********************************************************************
433 * ldap_openW (WLDAP32.@)
434 *
435 * Initialize an LDAP context and create a TCP connection.
436 *
437 * PARAMS
438 * hostname [I] Name of the host to connect to.
439 * portnumber [I] Portnumber to use.
440 *
441 * RETURNS
442 * Success: Pointer to an LDAP context.
443 * Failure: NULL
444 *
445 * NOTES
446 * The hostname string can be a space separated string of hostnames,
447 * in which case the LDAP runtime will try to connect to the hosts
448 * in order, until a connection can be made. A hostname may have a
449 * trailing portnumber (separated from the hostname by a ':'), which
450 * will take precedence over the portnumber supplied as a parameter
451 * to this function.
452 */
453 WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
454 {
455 #ifdef HAVE_LDAP
456 LDAP *ld = NULL;
457 char *hostnameU = NULL, *url = NULL;
458
459 TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
460
461 if (hostname) {
462 hostnameU = strWtoU( hostname );
463 if (!hostnameU) goto exit;
464 }
465 else {
466 hostnameU = strWtoU( defaulthost );
467 if (!hostnameU) goto exit;
468 }
469
470 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
471 if (!url) goto exit;
472
473 ldap_initialize( &ld, url );
474
475 exit:
476 strfreeU( hostnameU );
477 strfreeU( url );
478 return ld;
479
480 #else
481 return NULL;
482 #endif
483 }
484
485 /***********************************************************************
486 * ldap_sslinitA (WLDAP32.@)
487 *
488 * See ldap_sslinitW.
489 */
490 WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure )
491 {
492 #ifdef HAVE_LDAP
493 WLDAP32_LDAP *ld;
494 WCHAR *hostnameW = NULL;
495
496 TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
497
498 if (hostname) {
499 hostnameW = strAtoW( hostname );
500 if (!hostnameW) return NULL;
501 }
502
503 ld = ldap_sslinitW( hostnameW, portnumber, secure );
504
505 strfreeW( hostnameW );
506 return ld;
507
508 #else
509 return NULL;
510 #endif
511 }
512
513 /***********************************************************************
514 * ldap_sslinitW (WLDAP32.@)
515 *
516 * Initialize an LDAP context and create a secure TCP connection.
517 *
518 * PARAMS
519 * hostname [I] Name of the host to connect to.
520 * portnumber [I] Portnumber to use.
521 * secure [I] Ask the server to create an SSL connection.
522 *
523 * RETURNS
524 * Success: Pointer to an LDAP context.
525 * Failure: NULL
526 *
527 * NOTES
528 * The hostname string can be a space separated string of hostnames,
529 * in which case the LDAP runtime will try to connect to the hosts
530 * in order, until a connection can be made. A hostname may have a
531 * trailing portnumber (separated from the hostname by a ':'), which
532 * will take precedence over the portnumber supplied as a parameter
533 * to this function. The connection will not be made until the first
534 * LDAP function that needs it is called.
535 */
536 WLDAP32_LDAP * CDECL ldap_sslinitW( PWCHAR hostname, ULONG portnumber, int secure )
537 {
538 #ifdef HAVE_LDAP
539 WLDAP32_LDAP *ld = NULL;
540 char *hostnameU = NULL, *url = NULL;
541
542 TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
543
544 if (hostname) {
545 hostnameU = strWtoU( hostname );
546 if (!hostnameU) goto exit;
547 }
548 else {
549 hostnameU = strWtoU( defaulthost );
550 if (!hostnameU) goto exit;
551 }
552
553 if (secure)
554 url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
555 else
556 url = urlify_hostnames( "ldap://", hostnameU, portnumber );
557
558 if (!url) goto exit;
559 ldap_initialize( &ld, url );
560
561 exit:
562 strfreeU( hostnameU );
563 strfreeU( url );
564 return ld;
565
566 #else
567 return NULL;
568 #endif
569 }
570
571 /***********************************************************************
572 * ldap_start_tls_sA (WLDAP32.@)
573 *
574 * See ldap_start_tls_sW.
575 */
576 ULONG CDECL ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
577 PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
578 {
579 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
580 #ifdef HAVE_LDAP
581 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
582
583 ret = WLDAP32_LDAP_NO_MEMORY;
584
585 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
586
587 if (!ld) return ~0u;
588
589 if (serverctrls) {
590 serverctrlsW = controlarrayAtoW( serverctrls );
591 if (!serverctrlsW) goto exit;
592 }
593 if (clientctrls) {
594 clientctrlsW = controlarrayAtoW( clientctrls );
595 if (!clientctrlsW) goto exit;
596 }
597
598 ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
599
600 exit:
601 controlarrayfreeW( serverctrlsW );
602 controlarrayfreeW( clientctrlsW );
603
604 #endif
605 return ret;
606 }
607
608 /***********************************************************************
609 * ldap_start_tls_s (WLDAP32.@)
610 *
611 * Start TLS encryption on an LDAP connection.
612 *
613 * PARAMS
614 * ld [I] Pointer to an LDAP context.
615 * retval [I] Return value from the server.
616 * result [I] Response message from the server.
617 * serverctrls [I] Array of LDAP server controls.
618 * clientctrls [I] Array of LDAP client controls.
619 *
620 * RETURNS
621 * Success: LDAP_SUCCESS
622 * Failure: An LDAP error code.
623 *
624 * NOTES
625 * LDAP function that needs it is called.
626 */
627 ULONG CDECL ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
628 PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
629 {
630 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
631 #ifdef HAVE_LDAP
632 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
633
634 ret = WLDAP32_LDAP_NO_MEMORY;
635
636 TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
637
638 if (!ld) return ~0u;
639
640 if (serverctrls) {
641 serverctrlsU = controlarrayWtoU( serverctrls );
642 if (!serverctrlsU) goto exit;
643 }
644 if (clientctrls) {
645 clientctrlsU = controlarrayWtoU( clientctrls );
646 if (!clientctrlsU) goto exit;
647 }
648
649 ret = map_error( ldap_start_tls_s( ld, serverctrlsU, clientctrlsU ));
650
651 exit:
652 controlarrayfreeU( serverctrlsU );
653 controlarrayfreeU( clientctrlsU );
654
655 #endif
656 return ret;
657 }
658
659 /***********************************************************************
660 * ldap_startup (WLDAP32.@)
661 */
662 ULONG CDECL ldap_startup( PLDAP_VERSION_INFO version, HANDLE *instance )
663 {
664 TRACE( "(%p, %p)\n", version, instance );
665 return WLDAP32_LDAP_SUCCESS;
666 }
667
668 /***********************************************************************
669 * ldap_stop_tls_s (WLDAP32.@)
670 *
671 * Stop TLS encryption on an LDAP connection.
672 *
673 * PARAMS
674 * ld [I] Pointer to an LDAP context.
675 *
676 * RETURNS
677 * Success: TRUE
678 * Failure: FALSE
679 */
680 BOOLEAN CDECL ldap_stop_tls_s( WLDAP32_LDAP *ld )
681 {
682 TRACE( "(%p)\n", ld );
683 return TRUE; /* FIXME: find a way to stop tls on a connection */
684 }