widl: Generate client and server code for using context handles.
[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     while (i > 0) strfreeU( res[--i] );
113
114     HeapFree( GetProcessHeap(), 0, res );
115     HeapFree( GetProcessHeap(), 0, str );
116
117     return NULL;
118 }
119
120 /* Determine if a URL starts with a known LDAP scheme */
121 static int has_ldap_scheme( char *url )
122 {
123     if (!strncasecmp( url, "ldap://", 7 ) || 
124         !strncasecmp( url, "ldaps://", 8 ) ||
125         !strncasecmp( url, "ldapi://", 8 ) ||
126         !strncasecmp( url, "cldap://", 8 )) return 1;
127     return 0;
128 }
129
130 /* Flatten an array of hostnames into a space separated string of URLs.
131  * Prepend a given scheme and append a given portnumber to each hostname
132  * if necessary.
133  */
134 static char *join_hostnames( const char *scheme, char **hostnames, ULONG portnumber )
135 {
136     char *res, *p, *q, **v;
137     unsigned int i = 0, size = 0; 
138     static const char sep[] = " ", fmt[] = ":%d";
139     char port[7];
140
141     sprintf( port, fmt, portnumber ); 
142
143     for (v = hostnames; *v; v++)
144     {
145         if (!has_ldap_scheme( *v ))
146         {
147             size += strlen( scheme );
148             q = *v;
149         }
150         else
151             /* skip past colon in scheme prefix */
152             q = strchr( *v, '/' );
153
154         size += strlen( *v );
155
156         if (!strchr( q, ':' )) 
157             size += strlen( port );
158
159         i++;
160     }
161
162     size += (i - 1) * strlen( sep );
163  
164     res = HeapAlloc( GetProcessHeap(), 0, size + 1 );
165     if (!res) return NULL;
166
167     p = res;
168     for (v = hostnames; *v; v++)
169     {
170         if (v != hostnames)
171         {
172             strcpy( p, sep );
173             p += strlen( sep );
174         }
175
176         if (!has_ldap_scheme( *v ))
177         {
178             strcpy( p, scheme );
179             p += strlen( scheme );
180             q = *v;
181         }
182         else
183             /* skip past colon in scheme prefix */
184             q = strchr( *v, '/' );
185
186         strcpy( p, *v );
187         p += strlen( *v );
188
189         if (!strchr( q, ':' ))
190         {
191             strcpy( p, port );
192             p += strlen( port );
193         }
194     }
195     return res;
196 }
197
198 static char *urlify_hostnames( const char *scheme, char *hostnames, ULONG port )
199 {
200     char *url = NULL, **strarray;
201
202     strarray = split_hostnames( hostnames );
203     if (strarray)
204         url = join_hostnames( scheme, strarray, port );
205     else
206         return NULL;
207
208     strarrayfreeU( strarray );
209     return url;
210 }
211 #endif
212
213 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
214
215 /***********************************************************************
216  *      cldap_openA     (WLDAP32.@)
217  *
218  * See cldap_openW.
219  */
220 WLDAP32_LDAP * CDECL cldap_openA( PCHAR hostname, ULONG portnumber )
221 {
222 #ifdef HAVE_LDAP
223     WLDAP32_LDAP *ld = NULL;
224     WCHAR *hostnameW = NULL;
225
226     TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
227
228     if (hostname) {
229         hostnameW = strAtoW( hostname );
230         if (!hostnameW) goto exit;
231     }
232
233     ld = cldap_openW( hostnameW, portnumber );
234
235 exit:
236     strfreeW( hostnameW );
237     return ld;
238
239 #else
240     return NULL;
241 #endif
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 #else
293     return NULL;
294 #endif
295 }
296
297 /***********************************************************************
298  *      ldap_connect     (WLDAP32.@)
299  *
300  * Connect to an LDAP server. 
301  *
302  * PARAMS
303  *  ld      [I] Pointer to an LDAP context.
304  *  timeout [I] Pointer to an l_timeval structure specifying the
305  *              timeout in seconds.
306  *
307  * RETURNS
308  *  Success: LDAP_SUCCESS
309  *  Failure: An LDAP error code.
310  *
311  * NOTES
312  *  The timeout parameter may be NULL in which case a default timeout
313  *  value will be used.
314  */
315 ULONG CDECL ldap_connect( WLDAP32_LDAP *ld, struct l_timeval *timeout )
316 {
317     TRACE( "(%p, %p)\n", ld, timeout );
318
319     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
320     return LDAP_SUCCESS; /* FIXME: do something, e.g. ping the host */
321 }
322
323 /***********************************************************************
324  *      ldap_initA     (WLDAP32.@)
325  *
326  * See ldap_initW.
327  */
328 WLDAP32_LDAP *  CDECL ldap_initA( PCHAR hostname, ULONG portnumber )
329 {
330 #ifdef HAVE_LDAP
331     WLDAP32_LDAP *ld = NULL;
332     WCHAR *hostnameW = NULL;
333
334     TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
335
336     if (hostname) {
337         hostnameW = strAtoW( hostname );
338         if (!hostnameW) goto exit;
339     }
340
341     ld = ldap_initW( hostnameW, portnumber );
342
343 exit:
344     strfreeW( hostnameW );
345     return ld;
346
347 #else
348     return NULL;
349 #endif
350 }
351
352 /***********************************************************************
353  *      ldap_initW     (WLDAP32.@)
354  *
355  * Initialize an LDAP context and create a TCP connection.
356  *
357  * PARAMS
358  *  hostname   [I] Name of the host to connect to.
359  *  portnumber [I] Portnumber to use.
360  *
361  * RETURNS
362  *  Success: Pointer to an LDAP context.
363  *  Failure: NULL
364  *
365  * NOTES
366  *  The hostname string can be a space separated string of hostnames,
367  *  in which case the LDAP runtime will try to connect to the hosts
368  *  in order, until a connection can be made. A hostname may have a
369  *  trailing portnumber (separated from the hostname by a ':'), which 
370  *  will take precedence over the portnumber supplied as a parameter
371  *  to this function. The connection will not be made until the first
372  *  LDAP function that needs it is called.
373  */
374 WLDAP32_LDAP * CDECL ldap_initW( PWCHAR hostname, ULONG portnumber )
375 {
376 #ifdef HAVE_LDAP
377     LDAP *ld = NULL;
378     char *hostnameU = NULL, *url = NULL;
379
380     TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
381
382     if (hostname) {
383         hostnameU = strWtoU( hostname );
384         if (!hostnameU) goto exit;
385     }
386     else {
387         hostnameU = strWtoU( defaulthost );
388         if (!hostnameU) goto exit;
389     }
390
391     url = urlify_hostnames( "ldap://", hostnameU, portnumber );
392     if (!url) goto exit;
393
394     ldap_initialize( &ld, url );
395
396 exit:
397     strfreeU( hostnameU );
398     strfreeU( url );
399     return ld;
400
401 #else
402     return NULL;
403 #endif
404 }
405
406 /***********************************************************************
407  *      ldap_openA     (WLDAP32.@)
408  *
409  * See ldap_openW.
410  */
411 WLDAP32_LDAP * CDECL ldap_openA( PCHAR hostname, ULONG portnumber )
412 {
413 #ifdef HAVE_LDAP
414     WLDAP32_LDAP *ld = NULL;
415     WCHAR *hostnameW = NULL;
416
417     TRACE( "(%s, %d)\n", debugstr_a(hostname), portnumber );
418
419     if (hostname) {
420         hostnameW = strAtoW( hostname );
421         if (!hostnameW) goto exit;
422     }
423
424     ld = ldap_openW( hostnameW, portnumber );
425
426 exit:
427     strfreeW( hostnameW );
428     return ld;
429
430 #else
431     return NULL;
432 #endif
433 }
434
435 /***********************************************************************
436  *      ldap_openW     (WLDAP32.@)
437  *
438  * Initialize an LDAP context and create a TCP connection.
439  *
440  * PARAMS
441  *  hostname   [I] Name of the host to connect to.
442  *  portnumber [I] Portnumber to use.
443  *
444  * RETURNS
445  *  Success: Pointer to an LDAP context.
446  *  Failure: NULL
447  *
448  * NOTES
449  *  The hostname string can be a space separated string of hostnames,
450  *  in which case the LDAP runtime will try to connect to the hosts
451  *  in order, until a connection can be made. A hostname may have a
452  *  trailing portnumber (separated from the hostname by a ':'), which 
453  *  will take precedence over the portnumber supplied as a parameter
454  *  to this function.
455  */
456 WLDAP32_LDAP * CDECL ldap_openW( PWCHAR hostname, ULONG portnumber )
457 {
458 #ifdef HAVE_LDAP
459     LDAP *ld = NULL;
460     char *hostnameU = NULL, *url = NULL;
461
462     TRACE( "(%s, %d)\n", debugstr_w(hostname), portnumber );
463
464     if (hostname) {
465         hostnameU = strWtoU( hostname );
466         if (!hostnameU) goto exit;
467     }
468     else {
469         hostnameU = strWtoU( defaulthost );
470         if (!hostnameU) goto exit;
471     }
472
473     url = urlify_hostnames( "ldap://", hostnameU, portnumber );
474     if (!url) goto exit;
475
476     ldap_initialize( &ld, url );
477
478 exit:
479     strfreeU( hostnameU );
480     strfreeU( url );
481     return ld;
482
483 #else
484     return NULL;
485 #endif
486 }
487
488 /***********************************************************************
489  *      ldap_sslinitA     (WLDAP32.@)
490  *
491  * See ldap_sslinitW.
492  */
493 WLDAP32_LDAP * CDECL ldap_sslinitA( PCHAR hostname, ULONG portnumber, int secure )
494 {
495 #ifdef HAVE_LDAP
496     WLDAP32_LDAP *ld;
497     WCHAR *hostnameW = NULL;
498
499     TRACE( "(%s, %d, 0x%08x)\n", debugstr_a(hostname), portnumber, secure );
500
501     if (hostname) {
502         hostnameW = strAtoW( hostname );
503         if (!hostnameW) return NULL;
504     }
505
506     ld  = ldap_sslinitW( hostnameW, portnumber, secure );
507
508     strfreeW( hostnameW );
509     return ld;
510
511 #else
512     return NULL;
513 #endif
514 }
515
516 /***********************************************************************
517  *      ldap_sslinitW     (WLDAP32.@)
518  *
519  * Initialize an LDAP context and create a secure TCP connection.
520  *
521  * PARAMS
522  *  hostname   [I] Name of the host to connect to.
523  *  portnumber [I] Portnumber to use.
524  *  secure     [I] Ask the server to create an SSL connection.
525  *
526  * RETURNS
527  *  Success: Pointer to an LDAP context.
528  *  Failure: NULL
529  *
530  * NOTES
531  *  The hostname string can be a space separated string of hostnames,
532  *  in which case the LDAP runtime will try to connect to the hosts
533  *  in order, until a connection can be made. A hostname may have a
534  *  trailing portnumber (separated from the hostname by a ':'), which 
535  *  will take precedence over the portnumber supplied as a parameter
536  *  to this function. The connection will not be made until the first
537  *  LDAP function that needs it is called.
538  */
539 WLDAP32_LDAP * CDECL ldap_sslinitW( PWCHAR hostname, ULONG portnumber, int secure )
540 {
541 #ifdef HAVE_LDAP
542     WLDAP32_LDAP *ld = NULL;
543     char *hostnameU = NULL, *url = NULL;
544
545     TRACE( "(%s, %d, 0x%08x)\n", debugstr_w(hostname), portnumber, secure );
546
547     if (hostname) {
548         hostnameU = strWtoU( hostname );
549         if (!hostnameU) goto exit;
550     }
551     else {
552         hostnameU = strWtoU( defaulthost );
553         if (!hostnameU) goto exit;
554     }
555
556     if (secure)
557         url = urlify_hostnames( "ldaps://", hostnameU, portnumber );
558     else
559         url = urlify_hostnames( "ldap://", hostnameU, portnumber );
560
561     if (!url) goto exit;
562     ldap_initialize( &ld, url );
563
564 exit:
565     strfreeU( hostnameU );
566     strfreeU( url );
567     return ld;
568
569 #else
570     return NULL;
571 #endif
572 }
573
574 /***********************************************************************
575  *      ldap_start_tls_sA     (WLDAP32.@)
576  *
577  * See ldap_start_tls_sW.
578  */
579 ULONG CDECL ldap_start_tls_sA( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
580     PLDAPControlA *serverctrls, PLDAPControlA *clientctrls )
581 {
582     ULONG ret = LDAP_NOT_SUPPORTED;
583 #ifdef HAVE_LDAP
584     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
585
586     ret = WLDAP32_LDAP_NO_MEMORY;
587
588     TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
589
590     if (!ld) return ~0UL;
591
592     if (serverctrls) {
593         serverctrlsW = controlarrayAtoW( serverctrls );
594         if (!serverctrlsW) goto exit;
595     }
596     if (clientctrls) {
597         clientctrlsW = controlarrayAtoW( clientctrls );
598         if (!clientctrlsW) goto exit;
599     }
600
601     ret = ldap_start_tls_sW( ld, retval, result, serverctrlsW, clientctrlsW );
602
603 exit:
604     controlarrayfreeW( serverctrlsW );
605     controlarrayfreeW( clientctrlsW );
606
607 #endif
608     return ret;
609 }
610
611 /***********************************************************************
612  *      ldap_start_tls_s     (WLDAP32.@)
613  *
614  * Start TLS encryption on an LDAP connection.
615  *
616  * PARAMS
617  *  ld          [I] Pointer to an LDAP context.
618  *  retval      [I] Return value from the server.
619  *  result      [I] Response message from the server.
620  *  serverctrls [I] Array of LDAP server controls.
621  *  clientctrls [I] Array of LDAP client controls.
622  *
623  * RETURNS
624  *  Success: LDAP_SUCCESS
625  *  Failure: An LDAP error code.
626  *
627  * NOTES
628  *  LDAP function that needs it is called.
629  */
630 ULONG CDECL ldap_start_tls_sW( WLDAP32_LDAP *ld, PULONG retval, WLDAP32_LDAPMessage **result,
631     PLDAPControlW *serverctrls, PLDAPControlW *clientctrls )
632 {
633     ULONG ret = LDAP_NOT_SUPPORTED;
634 #ifdef HAVE_LDAP
635     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
636
637     ret = WLDAP32_LDAP_NO_MEMORY;
638
639     TRACE( "(%p, %p, %p, %p, %p)\n", ld, retval, result, serverctrls, clientctrls );
640
641     if (!ld) return ~0UL;
642
643     if (serverctrls) {
644         serverctrlsU = controlarrayWtoU( serverctrls );
645         if (!serverctrlsU) goto exit;
646     }
647     if (clientctrls) {
648         clientctrlsU = controlarrayWtoU( clientctrls );
649         if (!clientctrlsU) goto exit;
650     }
651
652     ret = ldap_start_tls_s( ld, serverctrlsU, clientctrlsU );
653
654 exit:
655     controlarrayfreeU( serverctrlsU );
656     controlarrayfreeU( clientctrlsU );
657
658 #endif
659     return ret;
660 }
661
662 /***********************************************************************
663  *      ldap_startup     (WLDAP32.@)
664  */
665 ULONG CDECL ldap_startup( PLDAP_VERSION_INFO version, HANDLE *instance )
666 {
667     TRACE( "(%p, %p)\n", version, instance );
668     return LDAP_SUCCESS;
669 }
670
671 /***********************************************************************
672  *      ldap_stop_tls_s     (WLDAP32.@)
673  *
674  * Stop TLS encryption on an LDAP connection.
675  *
676  * PARAMS
677  *  ld [I] Pointer to an LDAP context.
678  *
679  * RETURNS
680  *  Success: TRUE
681  *  Failure: FALSE
682  */
683 BOOLEAN CDECL ldap_stop_tls_s( WLDAP32_LDAP *ld )
684 {
685     TRACE( "(%p)\n", ld );
686     return TRUE; /* FIXME: find a way to stop tls on a connection */
687 }