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