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