Use WLDAP32_LDAP_NO_MEMORY instead of LDAP_NO_MEMORY.
[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
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
81     ret = WLDAP32_LDAP_NO_MEMORY;
82
83     TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_w(dn), cred, method );
84
85     if (!ld) return ~0UL;
86
87     if (dn) {
88         dnU = strWtoU( dn );
89         if (!dnU) goto exit;
90     }
91     if (cred) {
92         credU = strWtoU( cred );
93         if (!credU) goto exit;
94     }
95
96     ret = ldap_bind( ld, dnU, credU, method );
97
98 exit:
99     strfreeU( dnU );
100     strfreeU( credU );
101
102 #endif
103     return ret;
104 }
105
106 ULONG ldap_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR cred, ULONG method )
107 {
108     ULONG ret = LDAP_NOT_SUPPORTED;
109 #ifdef HAVE_LDAP
110     WCHAR *dnW = NULL, *credW = NULL;
111
112     ret = WLDAP32_LDAP_NO_MEMORY;
113
114     TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_a(dn), cred, method );
115
116     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
117
118     if (dn) {
119         dnW = strAtoW( dn );
120         if (!dnW) goto exit;
121     }
122     if (cred) {
123         credW = strAtoW( cred );
124         if (!credW) goto exit;
125     }
126
127     ret = ldap_bind_sW( ld, dnW, credW, method );
128
129 exit:
130     strfreeW( dnW );
131     strfreeW( credW );
132
133 #endif
134     return ret;
135 }
136
137 ULONG ldap_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR cred, ULONG method )
138 {
139     ULONG ret = LDAP_NOT_SUPPORTED;
140 #ifdef HAVE_LDAP
141     char *dnU = NULL, *credU = NULL;
142
143     ret = WLDAP32_LDAP_NO_MEMORY;
144
145     TRACE( "(%p, %s, %p, 0x%08lx)\n", ld, debugstr_w(dn), cred, method );
146
147     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
148
149     if (dn) {
150         dnU = strWtoU( dn );
151         if (!dnU) goto exit;
152     }
153     if (cred) {
154         credU = strWtoU( cred );
155         if (!credU) goto exit;
156     }
157
158     ret = ldap_bind_s( ld, dnU, credU, method );
159
160 exit:
161     strfreeU( dnU );
162     strfreeU( credU );
163
164 #endif
165     return ret;
166 }
167
168 ULONG ldap_sasl_bindA( WLDAP32_LDAP *ld, const PCHAR dn,
169     const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
170     PLDAPControlA *clientctrls, int *message )
171 {
172     ULONG ret = LDAP_NOT_SUPPORTED;
173 #ifdef HAVE_LDAP
174     WCHAR *dnW, *mechanismW = NULL;
175     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
176
177     ret = WLDAP32_LDAP_NO_MEMORY;
178
179     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
180            debugstr_a(mechanism), cred, serverctrls, clientctrls, message );
181
182     if (!ld || !dn || !mechanism || !cred || !message)
183         return WLDAP32_LDAP_PARAM_ERROR;
184
185     dnW = strAtoW( dn );
186     if (!dnW) goto exit;
187
188     mechanismW = strAtoW( mechanism );
189     if (!mechanismW) goto exit;
190
191     if (serverctrls) {
192         serverctrlsW = controlarrayAtoW( serverctrls );
193         if (!serverctrlsW) goto exit;
194     }
195     if (clientctrls) {
196         clientctrlsW = controlarrayAtoW( clientctrls );
197         if (!clientctrlsW) goto exit;
198     }
199
200     ret = ldap_sasl_bindW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, message );
201
202 exit:
203     strfreeW( dnW );
204     strfreeW( mechanismW );
205     controlarrayfreeW( serverctrlsW );
206     controlarrayfreeW( clientctrlsW );
207
208 #endif
209     return ret;
210 }
211
212 ULONG ldap_sasl_bindW( WLDAP32_LDAP *ld, const PWCHAR dn,
213     const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
214     PLDAPControlW *clientctrls, int *message )
215 {
216     ULONG ret = LDAP_NOT_SUPPORTED;
217 #ifdef HAVE_LDAP
218     char *dnU, *mechanismU = NULL;
219     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
220
221     ret = WLDAP32_LDAP_NO_MEMORY;
222
223     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
224            debugstr_w(mechanism), cred, serverctrls, clientctrls, message );
225
226     if (!ld || !dn || !mechanism || !cred || !message)
227         return WLDAP32_LDAP_PARAM_ERROR;
228
229     dnU = strWtoU( dn );
230     if (!dnU) goto exit;
231
232     mechanismU = strWtoU( mechanism );
233     if (!mechanismU) goto exit;
234
235     if (serverctrls) {
236         serverctrlsU = controlarrayWtoU( serverctrls );
237         if (!serverctrlsU) goto exit;
238     }
239     if (clientctrls) {
240         clientctrlsU = controlarrayWtoU( clientctrls );
241         if (!clientctrlsU) goto exit;
242     }
243
244     ret = ldap_sasl_bind( ld, dnU, mechanismU, (struct berval *)cred,
245                           serverctrlsU, clientctrlsU, message );
246
247 exit:
248     strfreeU( dnU );
249     strfreeU( mechanismU );
250     controlarrayfreeU( serverctrlsU );
251     controlarrayfreeU( clientctrlsU );
252
253 #endif
254     return ret;
255 }
256
257 ULONG ldap_sasl_bind_sA( WLDAP32_LDAP *ld, const PCHAR dn,
258     const PCHAR mechanism, const BERVAL *cred, PLDAPControlA *serverctrls,
259     PLDAPControlA *clientctrls, PBERVAL *serverdata )
260 {
261     ULONG ret = LDAP_NOT_SUPPORTED;
262 #ifdef HAVE_LDAP
263     WCHAR *dnW, *mechanismW = NULL;
264     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
265
266     ret = WLDAP32_LDAP_NO_MEMORY;
267
268     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_a(dn),
269            debugstr_a(mechanism), cred, serverctrls, clientctrls, serverdata );
270
271     if (!ld || !dn || !mechanism || !cred || !serverdata)
272         return WLDAP32_LDAP_PARAM_ERROR;
273
274     dnW = strAtoW( dn );
275     if (!dnW) goto exit;
276
277     mechanismW = strAtoW( mechanism );
278     if (!mechanismW) goto exit;
279
280     if (serverctrls) {
281         serverctrlsW = controlarrayAtoW( serverctrls );
282         if (!serverctrlsW) goto exit;
283     }
284     if (clientctrls) {
285         clientctrlsW = controlarrayAtoW( clientctrls );
286         if (!clientctrlsW) goto exit;
287     }
288
289     ret = ldap_sasl_bind_sW( ld, dnW, mechanismW, cred, serverctrlsW, clientctrlsW, serverdata );
290
291 exit:
292     strfreeW( dnW );
293     strfreeW( mechanismW );
294     controlarrayfreeW( serverctrlsW );
295     controlarrayfreeW( clientctrlsW );
296
297 #endif
298     return ret;
299 }
300
301 ULONG ldap_sasl_bind_sW( WLDAP32_LDAP *ld, const PWCHAR dn,
302     const PWCHAR mechanism, const BERVAL *cred, PLDAPControlW *serverctrls,
303     PLDAPControlW *clientctrls, PBERVAL *serverdata )
304 {
305     ULONG ret = LDAP_NOT_SUPPORTED;
306 #ifdef HAVE_LDAP
307     char *dnU, *mechanismU = NULL;
308     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
309
310     ret = WLDAP32_LDAP_NO_MEMORY;
311
312     TRACE( "(%p, %s, %s, %p, %p, %p, %p)\n", ld, debugstr_w(dn),
313            debugstr_w(mechanism), cred, serverctrls, clientctrls, serverdata );
314
315     if (!ld || !dn || !mechanism || !cred || !serverdata)
316         return WLDAP32_LDAP_PARAM_ERROR;
317
318     dnU = strWtoU( dn );
319     if (!dnU) goto exit;
320
321     mechanismU = strWtoU( mechanism );
322     if (!mechanismU) goto exit;
323
324     if (serverctrls) {
325         serverctrlsU = controlarrayWtoU( serverctrls );
326         if (!serverctrlsU) goto exit;
327     }
328     if (clientctrls) {
329         clientctrlsU = controlarrayWtoU( clientctrls );
330         if (!clientctrlsU) goto exit;
331     }
332
333     ret = ldap_sasl_bind_s( ld, dnU, mechanismU, (struct berval *)cred,
334                             serverctrlsU, clientctrlsU, (struct berval **)serverdata );
335
336 exit:
337     strfreeU( dnU );
338     strfreeU( mechanismU );
339     controlarrayfreeU( serverctrlsU );
340     controlarrayfreeU( clientctrlsU );
341
342 #endif
343     return ret;
344 }
345
346 ULONG ldap_simple_bindA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
347 {
348     ULONG ret = LDAP_NOT_SUPPORTED;
349 #ifdef HAVE_LDAP
350     WCHAR *dnW = NULL, *passwdW = NULL;
351
352     ret = WLDAP32_LDAP_NO_MEMORY;
353
354     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
355
356     if (!ld) return ~0UL;
357
358     if (dn) {
359         dnW = strAtoW( dn );
360         if (!dnW) goto exit;
361     }
362     if (passwd) {
363         passwdW = strAtoW( passwd );
364         if (!passwdW) goto exit;
365     }
366
367     ret = ldap_simple_bindW( ld, dnW, passwdW );
368
369 exit:
370     strfreeW( dnW );
371     strfreeW( passwdW );
372
373 #endif
374     return ret;
375 }
376
377 ULONG ldap_simple_bindW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
378 {
379     ULONG ret = LDAP_NOT_SUPPORTED;
380 #ifdef HAVE_LDAP
381     char *dnU = NULL, *passwdU = NULL;
382
383     ret = WLDAP32_LDAP_NO_MEMORY;
384
385     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
386
387     if (!ld) return ~0UL;
388
389     if (dn) {
390         dnU = strWtoU( dn );
391         if (!dnU) goto exit;
392     }
393     if (passwd) {
394         passwdU = strWtoU( passwd );
395         if (!passwdU) goto exit;
396     }
397
398     ret = ldap_simple_bind( ld, dnU, passwdU );
399
400 exit:
401     strfreeU( dnU );
402     strfreeU( passwdU );
403
404 #endif
405     return ret;
406 }
407
408 ULONG ldap_simple_bind_sA( WLDAP32_LDAP *ld, PCHAR dn, PCHAR passwd )
409 {
410     ULONG ret = LDAP_NOT_SUPPORTED;
411 #ifdef HAVE_LDAP
412     WCHAR *dnW = NULL, *passwdW = NULL;
413
414     ret = WLDAP32_LDAP_NO_MEMORY;
415
416     TRACE( "(%p, %s, %p)\n", ld, debugstr_a(dn), passwd );
417
418     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
419
420     if (dn) {
421         dnW = strAtoW( dn );
422         if (!dnW) goto exit;
423     }
424     if (passwd) {
425         passwdW = strAtoW( passwd );
426         if (!passwdW) goto exit;
427     }
428
429     ret = ldap_simple_bind_sW( ld, dnW, passwdW );
430
431 exit:
432     strfreeW( dnW );
433     strfreeW( passwdW );
434
435 #endif
436     return ret;
437 }
438
439 ULONG ldap_simple_bind_sW( WLDAP32_LDAP *ld, PWCHAR dn, PWCHAR passwd )
440 {
441     ULONG ret = LDAP_NOT_SUPPORTED;
442 #ifdef HAVE_LDAP
443     char *dnU = NULL, *passwdU = NULL;
444
445     ret = WLDAP32_LDAP_NO_MEMORY;
446
447     TRACE( "(%p, %s, %p)\n", ld, debugstr_w(dn), passwd );
448
449     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
450
451     if (dn) {
452         dnU = strWtoU( dn );
453         if (!dnU) goto exit;
454     }
455     if (passwd) {
456         passwdU = strWtoU( passwd );
457         if (!passwdU) goto exit;
458     }
459
460     ret = ldap_simple_bind_s( ld, dnU, passwdU );
461
462 exit:
463     strfreeU( dnU );
464     strfreeU( passwdU );
465
466 #endif
467     return ret;
468 }
469
470 ULONG WLDAP32_ldap_unbind( WLDAP32_LDAP *ld )
471 {
472     ULONG ret = LDAP_NOT_SUPPORTED;
473 #ifdef HAVE_LDAP
474
475     TRACE( "(%p)\n", ld );
476
477     if (ld)
478         ret = ldap_unbind( ld );
479     else
480         ret = WLDAP32_LDAP_PARAM_ERROR;
481
482 #endif
483     return ret;
484 }
485
486 ULONG WLDAP32_ldap_unbind_s( WLDAP32_LDAP *ld )
487 {
488     ULONG ret = LDAP_NOT_SUPPORTED;
489 #ifdef HAVE_LDAP
490
491     TRACE( "(%p)\n", ld );
492
493     if (ld)
494         ret = ldap_unbind_s( ld );
495     else
496         ret = WLDAP32_LDAP_PARAM_ERROR;
497
498 #endif
499     return ret;
500 }