Fix the build without openldap.
[wine] / dlls / wldap32 / bind.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
31
32 #ifdef HAVE_LDAP_H
33 #include <ldap.h>
34 #else
35 #define LDAP_SUCCESS        0x00
36 #define LDAP_NOT_SUPPORTED  0x5c
37 #endif
38
39 #include "winldap_private.h"
40 #include "wldap32.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
43
44 ULONG ldap_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
45 {
46     ULONG ret = LDAP_NOT_SUPPORTED;
47 #ifdef HAVE_LDAP
48     WCHAR *dnW = NULL, *credW = NULL;
49
50     ret = WLDAP32_LDAP_NO_MEMORY;
51
52     TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_a(dn), cred, method );
53
54     if (!ld) return ~0UL;
55
56     if (dn) {
57         dnW = strAtoW( dn );
58         if (!dnW) goto exit;
59     }
60     if (cred) {
61         credW = strAtoW( cred );
62         if (!credW) goto exit;
63     }
64
65     ret = ldap_bindW( ld, dnW, credW, method );
66
67 exit:
68     strfreeW( dnW );
69     strfreeW( credW );
70
71 #endif
72     return ret;
73 }
74
75 ULONG ldap_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
76 {
77     ULONG ret = LDAP_NOT_SUPPORTED;
78 #ifdef HAVE_LDAP
79     char *dnU = NULL, *credU = NULL;
80     struct berval pwd = { 0, NULL };
81     int msg;
82
83     ret = WLDAP32_LDAP_NO_MEMORY;
84
85     TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_w(dn), cred, method );
86
87     if (!ld) return ~0UL;
88     if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
89
90     if (dn) {
91         dnU = strWtoU( dn );
92         if (!dnU) goto exit;
93     }
94     if (cred) {
95         credU = strWtoU( cred );
96         if (!credU) goto exit;
97
98         pwd.bv_len = strlen( credU );
99         pwd.bv_val = credU;
100     }
101
102     ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
103
104     if (ret == LDAP_SUCCESS)
105         ret = msg;
106     else
107         ret = ~0UL;
108
109 exit:
110     strfreeU( dnU );
111     strfreeU( credU );
112
113 #endif
114     return ret;
115 }
116
117 ULONG ldap_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
118 {
119     ULONG ret = LDAP_NOT_SUPPORTED;
120 #ifdef HAVE_LDAP
121     WCHAR *dnW = NULL, *credW = NULL;
122
123     ret = WLDAP32_LDAP_NO_MEMORY;
124
125     TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_a(dn), cred, method );
126
127     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
128
129     if (dn) {
130         dnW = strAtoW( dn );
131         if (!dnW) goto exit;
132     }
133     if (cred) {
134         credW = strAtoW( cred );
135         if (!credW) goto exit;
136     }
137
138     ret = ldap_bind_sW( ld, dnW, credW, method );
139
140 exit:
141     strfreeW( dnW );
142     strfreeW( credW );
143
144 #endif
145     return ret;
146 }
147
148 ULONG ldap_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
149 {
150     ULONG ret = LDAP_NOT_SUPPORTED;
151 #ifdef HAVE_LDAP
152     char *dnU = NULL, *credU = NULL;
153     struct berval pwd = { 0, NULL };
154
155     ret = WLDAP32_LDAP_NO_MEMORY;
156
157     TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_w(dn), cred, method );
158
159     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
160     if (method != LDAP_AUTH_SIMPLE) return WLDAP32_LDAP_PARAM_ERROR;
161
162     if (dn) {
163         dnU = strWtoU( dn );
164         if (!dnU) goto exit;
165     }
166     if (cred) {
167         credU = strWtoU( cred );
168         if (!credU) goto exit;
169
170         pwd.bv_len = strlen( credU );
171         pwd.bv_val = credU;
172     }
173
174     ret = ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL );
175
176 exit:
177     strfreeU( dnU );
178     strfreeU( credU );
179
180 #endif
181     return ret;
182 }
183
184 ULONG ldap_sasl_bindA( WLDAP32_LDAP *ld, const PCHAR dn,
185     const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
186     PLDAPControlA *clientctrls, int *message )
187 {
188     ULONG ret = LDAP_NOT_SUPPORTED;
189 #ifdef HAVE_LDAP
190     WCHAR *dnW, *mechanismW = NULL;
191     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
192
193     ret = WLDAP32_LDAP_NO_MEMORY;
194
195     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
196            debugstr_a(mechanism), cred, serverctrls, clientctrls, message );
197
198     if (!ld || !dn || !mechanism || !cred || !message)
199         return WLDAP32_LDAP_PARAM_ERROR;
200
201     dnW = strAtoW( dn );
202     if (!dnW) goto exit;
203
204     mechanismW = strAtoW( mechanism );
205     if (!mechanismW) goto exit;
206
207     if (serverctrls) {
208         serverctrlsW = controlarrayAtoW( serverctrls );
209         if (!serverctrlsW) goto exit;
210     }
211     if (clientctrls) {
212         clientctrlsW = controlarrayAtoW( clientctrls );
213         if (!clientctrlsW) goto exit;
214     }
215
216     ret = ldap_sasl_bindW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, message );
217
218 exit:
219     strfreeW( dnW );
220     strfreeW( mechanismW );
221     controlarrayfreeW( serverctrlsW );
222     controlarrayfreeW( clientctrlsW );
223
224 #endif
225     return ret;
226 }
227
228 ULONG ldap_sasl_bindW( WLDAP32_LDAP *ld, const PWCHAR dn,
229     const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
230     PLDAPControlW *clientctrls, int *message )
231 {
232     ULONG ret = LDAP_NOT_SUPPORTED;
233 #ifdef HAVE_LDAP
234     char *dnU, *mechanismU = NULL;
235     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
236
237     ret = WLDAP32_LDAP_NO_MEMORY;
238
239     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
240            debugstr_w(mechanism), cred, serverctrls, clientctrls, message );
241
242     if (!ld || !dn || !mechanism || !cred || !message)
243         return WLDAP32_LDAP_PARAM_ERROR;
244
245     dnU = strWtoU( dn );
246     if (!dnU) goto exit;
247
248     mechanismU = strWtoU( mechanism );
249     if (!mechanismU) goto exit;
250
251     if (serverctrls) {
252         serverctrlsU = controlarrayWtoU( serverctrls );
253         if (!serverctrlsU) goto exit;
254     }
255     if (clientctrls) {
256         clientctrlsU = controlarrayWtoU( clientctrls );
257         if (!clientctrlsU) goto exit;
258     }
259
260     ret = ldap_sasl_bind( ld, dnU, mechanismU, (struct berval *)cred,
261                           serverctrlsU, clientctrlsU, message );
262
263 exit:
264     strfreeU( dnU );
265     strfreeU( mechanismU );
266     controlarrayfreeU( serverctrlsU );
267     controlarrayfreeU( clientctrlsU );
268
269 #endif
270     return ret;
271 }
272
273 ULONG ldap_sasl_bind_sA( WLDAP32_LDAP *ld, const PCHAR dn,
274     const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
275     PLDAPControlA *clientctrls, PBERVAL *serverdata )
276 {
277     ULONG ret = LDAP_NOT_SUPPORTED;
278 #ifdef HAVE_LDAP
279     WCHAR *dnW, *mechanismW = NULL;
280     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
281
282     ret = WLDAP32_LDAP_NO_MEMORY;
283
284     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
285            debugstr_a(mechanism), cred, serverctrls, clientctrls, serverdata );
286
287     if (!ld || !dn || !mechanism || !cred || !serverdata)
288         return WLDAP32_LDAP_PARAM_ERROR;
289
290     dnW = strAtoW( dn );
291     if (!dnW) goto exit;
292
293     mechanismW = strAtoW( mechanism );
294     if (!mechanismW) goto exit;
295
296     if (serverctrls) {
297         serverctrlsW = controlarrayAtoW( serverctrls );
298         if (!serverctrlsW) goto exit;
299     }
300     if (clientctrls) {
301         clientctrlsW = controlarrayAtoW( clientctrls );
302         if (!clientctrlsW) goto exit;
303     }
304
305     ret = ldap_sasl_bind_sW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, serverdata );
306
307 exit:
308     strfreeW( dnW );
309     strfreeW( mechanismW );
310     controlarrayfreeW( serverctrlsW );
311     controlarrayfreeW( clientctrlsW );
312
313 #endif
314     return ret;
315 }
316
317 ULONG ldap_sasl_bind_sW( WLDAP32_LDAP *ld, const PWCHAR dn,
318     const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
319     PLDAPControlW *clientctrls, PBERVAL *serverdata )
320 {
321     ULONG ret = LDAP_NOT_SUPPORTED;
322 #ifdef HAVE_LDAP
323     char *dnU, *mechanismU = NULL;
324     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
325
326     ret = WLDAP32_LDAP_NO_MEMORY;
327
328     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
329            debugstr_w(mechanism), cred, serverctrls, clientctrls, serverdata );
330
331     if (!ld || !dn || !mechanism || !cred || !serverdata)
332         return WLDAP32_LDAP_PARAM_ERROR;
333
334     dnU = strWtoU( dn );
335     if (!dnU) goto exit;
336
337     mechanismU = strWtoU( mechanism );
338     if (!mechanismU) goto exit;
339
340     if (serverctrls) {
341         serverctrlsU = controlarrayWtoU( serverctrls );
342         if (!serverctrlsU) goto exit;
343     }
344     if (clientctrls) {
345         clientctrlsU = controlarrayWtoU( clientctrls );
346         if (!clientctrlsU) goto exit;
347     }
348
349     ret = ldap_sasl_bind_s( ld, dnU, mechanismU, (struct berval *)cred,
350                             serverctrlsU, clientctrlsU, (struct berval **)serverdata );
351
352 exit:
353     strfreeU( dnU );
354     strfreeU( mechanismU );
355     controlarrayfreeU( serverctrlsU );
356     controlarrayfreeU( clientctrlsU );
357
358 #endif
359     return ret;
360 }
361
362 ULONG ldap_simple_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
363 {
364     ULONG ret = LDAP_NOT_SUPPORTED;
365 #ifdef HAVE_LDAP
366     WCHAR *dnW = NULL, *passwdW = NULL;
367
368     ret = WLDAP32_LDAP_NO_MEMORY;
369
370     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
371
372     if (!ld) return ~0UL;
373
374     if (dn) {
375         dnW = strAtoW( dn );
376         if (!dnW) goto exit;
377     }
378     if (passwd) {
379         passwdW = strAtoW( passwd );
380         if (!passwdW) goto exit;
381     }
382
383     ret = ldap_simple_bindW( ld, dnW, passwdW );
384
385 exit:
386     strfreeW( dnW );
387     strfreeW( passwdW );
388
389 #endif
390     return ret;
391 }
392
393 ULONG ldap_simple_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
394 {
395     ULONG ret = LDAP_NOT_SUPPORTED;
396 #ifdef HAVE_LDAP
397     char *dnU = NULL, *passwdU = NULL;
398     struct berval pwd = { 0, NULL };
399     int msg;
400
401     ret = WLDAP32_LDAP_NO_MEMORY;
402
403     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
404
405     if (!ld) return ~0UL;
406
407     if (dn) {
408         dnU = strWtoU( dn );
409         if (!dnU) goto exit;
410     }
411     if (passwd) {
412         passwdU = strWtoU( passwd );
413         if (!passwdU) goto exit;
414
415         pwd.bv_len = strlen( passwdU );
416         pwd.bv_val = passwdU;
417     }
418
419     ret = ldap_sasl_bind( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, &msg );
420
421     if (ret == LDAP_SUCCESS)
422         ret = msg;
423     else
424         ret = ~0UL;
425
426 exit:
427     strfreeU( dnU );
428     strfreeU( passwdU );
429
430 #endif
431     return ret;
432 }
433
434 ULONG ldap_simple_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
435 {
436     ULONG ret = LDAP_NOT_SUPPORTED;
437 #ifdef HAVE_LDAP
438     WCHAR *dnW = NULL, *passwdW = NULL;
439
440     ret = WLDAP32_LDAP_NO_MEMORY;
441
442     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
443
444     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
445
446     if (dn) {
447         dnW = strAtoW( dn );
448         if (!dnW) goto exit;
449     }
450     if (passwd) {
451         passwdW = strAtoW( passwd );
452         if (!passwdW) goto exit;
453     }
454
455     ret = ldap_simple_bind_sW( ld, dnW, passwdW );
456
457 exit:
458     strfreeW( dnW );
459     strfreeW( passwdW );
460
461 #endif
462     return ret;
463 }
464
465 ULONG ldap_simple_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
466 {
467     ULONG ret = LDAP_NOT_SUPPORTED;
468 #ifdef HAVE_LDAP
469     char *dnU = NULL, *passwdU = NULL;
470     struct berval pwd = { 0, NULL };
471
472     ret = WLDAP32_LDAP_NO_MEMORY;
473
474     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
475
476     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
477
478     if (dn) {
479         dnU = strWtoU( dn );
480         if (!dnU) goto exit;
481     }
482     if (passwd) {
483         passwdU = strWtoU( passwd );
484         if (!passwdU) goto exit;
485
486         pwd.bv_len = strlen( passwdU );
487         pwd.bv_val = passwdU;
488     }
489
490     ret = ldap_sasl_bind_s( ld, dnU, LDAP_SASL_SIMPLE, &pwd, NULL, NULL, NULL );
491
492 exit:
493     strfreeU( dnU );
494     strfreeU( passwdU );
495
496 #endif
497     return ret;
498 }
499
500 ULONG WLDAP32_ldap_unbind( WLDAP32_LDAP *ld )
501 {
502     ULONG ret = LDAP_NOT_SUPPORTED;
503 #ifdef HAVE_LDAP
504
505     TRACE( "(%p)\n", ld );
506
507     if (ld)
508         ret = ldap_unbind_ext( ld, NULL, NULL );
509     else
510         ret = WLDAP32_LDAP_PARAM_ERROR;
511
512 #endif
513     return ret;
514 }
515
516 ULONG WLDAP32_ldap_unbind_s( WLDAP32_LDAP *ld )
517 {
518     ULONG ret = LDAP_NOT_SUPPORTED;
519 #ifdef HAVE_LDAP
520
521     TRACE( "(%p)\n", ld );
522
523     if (ld)
524         ret = ldap_unbind_ext_s( ld, NULL, NULL );
525     else
526         ret = WLDAP32_LDAP_PARAM_ERROR;
527
528 #endif
529     return ret;
530 }