Use HAVE_LDAP_H when checking if ldap.h is available.
[wine] / dlls / wldap32 / search.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_searchA( WLDAP32_LDAP *ld, PCHAR base, ULONG scope, PCHAR filter,
45     PCHAR attrs[], ULONG attrsonly )
46 {
47     ULONG ret = LDAP_NOT_SUPPORTED;
48 #ifdef HAVE_LDAP
49     WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
50
51     ret = WLDAP32_LDAP_NO_MEMORY;
52
53     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx)\n", ld, debugstr_a(base),
54            scope, debugstr_a(filter), attrs, attrsonly );
55
56     if (!ld) return ~0UL;
57
58     if (base) {
59         baseW = strAtoW( base );
60         if (!baseW) goto exit;
61     }
62     if (filter) {
63         filterW = strAtoW( filter );
64         if (!filterW) goto exit;
65     }
66     if (attrs) {
67         attrsW = strarrayAtoW( attrs );
68         if (!attrsW) goto exit;
69     }
70
71     ret = ldap_searchW( ld, baseW, scope, filterW, attrsW, attrsonly );
72
73 exit:
74     strfreeW( baseW );
75     strfreeW( filterW );
76     strarrayfreeW( attrsW );
77
78 #endif
79     return ret;
80 }
81
82 ULONG ldap_searchW( WLDAP32_LDAP *ld, PWCHAR base, ULONG scope, PWCHAR filter,
83     PWCHAR attrs[], ULONG attrsonly )
84 {
85     ULONG ret = LDAP_NOT_SUPPORTED;
86 #ifdef HAVE_LDAP
87     char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
88
89     ret = WLDAP32_LDAP_NO_MEMORY;
90
91     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx)\n", ld, debugstr_w(base),
92            scope, debugstr_w(filter), attrs, attrsonly );
93
94     if (!ld) return ~0UL;
95
96     if (base) {
97         baseU = strWtoU( base );
98         if (!baseU) goto exit;
99     }
100     if (filter) {
101         filterU = strWtoU( filter );
102         if (!filterU) goto exit;
103     }
104     if (attrs) {
105         attrsU = strarrayWtoU( attrs );
106         if (!attrsU) goto exit;
107     }
108
109     ret = ldap_search( ld, baseU, scope, filterU, attrsU, attrsonly );
110
111 exit:
112     strfreeU( baseU );
113     strfreeU( filterU );
114     strarrayfreeU( attrsU );
115
116 #endif
117     return ret;
118 }
119
120 ULONG ldap_search_extA( WLDAP32_LDAP *ld, PCHAR base, ULONG scope,
121     PCHAR filter, PCHAR attrs[], ULONG attrsonly, PLDAPControlA *serverctrls,
122     PLDAPControlA *clientctrls, ULONG timelimit, ULONG sizelimit, ULONG *message )
123 {
124     ULONG ret = LDAP_NOT_SUPPORTED;
125 #ifdef HAVE_LDAP
126     WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
127     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
128
129     ret = WLDAP32_LDAP_NO_MEMORY;
130
131     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx, %p, %p, 0x%08lx, 0x%08lx, %p)\n",
132            ld, debugstr_a(base), scope, debugstr_a(filter), attrs, attrsonly,
133            serverctrls, clientctrls, timelimit, sizelimit, message );
134
135     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
136
137     if (base) {
138         baseW = strAtoW( base );
139         if (!baseW) goto exit;
140     }
141     if (filter)
142     {
143         filterW = strAtoW( filter );
144         if (!filterW) goto exit;
145     }
146     if (attrs) {
147         attrsW = strarrayAtoW( attrs );
148         if (!attrsW) goto exit;
149     }
150     if (serverctrls) {
151         serverctrlsW = controlarrayAtoW( serverctrls );
152         if (!serverctrlsW) goto exit;
153     }
154     if (clientctrls) {
155         clientctrlsW = controlarrayAtoW( clientctrls );
156         if (!clientctrlsW) goto exit;
157     }
158
159     ret = ldap_search_extW( ld, baseW, scope, filterW, attrsW, attrsonly,
160                             serverctrlsW, clientctrlsW, timelimit, sizelimit, message );
161
162 exit:
163     strfreeW( baseW );
164     strfreeW( filterW );
165     strarrayfreeW( attrsW );
166     controlarrayfreeW( serverctrlsW );
167     controlarrayfreeW( clientctrlsW );
168
169 #endif
170     return ret;
171 }
172
173 ULONG ldap_search_extW( WLDAP32_LDAP *ld, PWCHAR base, ULONG scope,
174     PWCHAR filter, PWCHAR attrs[], ULONG attrsonly, PLDAPControlW *serverctrls,
175     PLDAPControlW *clientctrls, ULONG timelimit, ULONG sizelimit, ULONG *message )
176 {
177     ULONG ret = LDAP_NOT_SUPPORTED;
178 #ifdef HAVE_LDAP
179     char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
180     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
181     struct timeval tv;
182
183     ret = WLDAP32_LDAP_NO_MEMORY;
184
185     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx, %p, %p, 0x%08lx, 0x%08lx, %p)\n",
186            ld, debugstr_w(base), scope, debugstr_w(filter), attrs, attrsonly,
187            serverctrls, clientctrls, timelimit, sizelimit, message );
188
189     if (!ld) return ~0UL;
190
191     if (base) {
192         baseU = strWtoU( base );
193         if (!baseU) goto exit;
194     }
195     if (filter) {
196         filterU = strWtoU( filter );
197         if (!filterU) goto exit;
198     }
199     if (attrs) {
200         attrsU = strarrayWtoU( attrs );
201         if (!attrsU) goto exit;
202     }
203     if (serverctrls) {
204         serverctrlsU = controlarrayWtoU( serverctrls );
205         if (!serverctrlsU) goto exit;
206     }
207     if (clientctrls) {
208         clientctrlsU = controlarrayWtoU( clientctrls );
209         if (!clientctrlsU) goto exit;
210     }
211
212     tv.tv_sec = timelimit;
213     tv.tv_usec = 0;
214
215     ret = ldap_search_ext( ld, baseU, scope, filterU, attrsU, attrsonly,
216                            serverctrlsU, clientctrlsU, &tv, sizelimit, (int *)message );
217
218 exit:
219     strfreeU( baseU );
220     strfreeU( filterU );
221     strarrayfreeU( attrsU );
222     controlarrayfreeU( serverctrlsU );
223     controlarrayfreeU( clientctrlsU );
224
225 #endif
226     return ret;
227 }
228
229 ULONG ldap_search_ext_sA( WLDAP32_LDAP *ld, PCHAR base, ULONG scope,
230     PCHAR filter, PCHAR attrs[], ULONG attrsonly, PLDAPControlA *serverctrls,
231     PLDAPControlA *clientctrls, struct l_timeval* timeout, ULONG sizelimit, WLDAP32_LDAPMessage **res )
232 {
233     ULONG ret = LDAP_NOT_SUPPORTED;
234 #ifdef HAVE_LDAP
235     WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
236     LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
237
238     ret = WLDAP32_LDAP_NO_MEMORY;
239
240     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx, %p, %p, %p, 0x%08lx, %p)\n",
241            ld, debugstr_a(base), scope, debugstr_a(filter), attrs, attrsonly,
242            serverctrls, clientctrls, timeout, sizelimit, res );
243
244     if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
245
246     if (base) {
247         baseW = strAtoW( base );
248         if (!baseW) goto exit;
249     }
250     if (filter) {
251         filterW = strAtoW( filter );
252         if (!filterW) goto exit;
253     }
254     if (attrs) {
255         attrsW = strarrayAtoW( attrs );
256         if (!attrsW) goto exit;
257     }
258     if (serverctrls) {
259         serverctrlsW = controlarrayAtoW( serverctrls );
260         if (!serverctrlsW) goto exit;
261     }
262     if (clientctrls) {
263         clientctrlsW = controlarrayAtoW( clientctrls );
264         if (!clientctrlsW) goto exit;
265     }
266
267     ret = ldap_search_ext_sW( ld, baseW, scope, filterW, attrsW, attrsonly,
268                               serverctrlsW, clientctrlsW, timeout, sizelimit, res );
269
270 exit:
271     strfreeW( baseW );
272     strfreeW( filterW );
273     strarrayfreeW( attrsW );
274     controlarrayfreeW( serverctrlsW );
275     controlarrayfreeW( clientctrlsW );
276
277 #endif
278     return ret;
279 }
280
281 ULONG ldap_search_ext_sW( WLDAP32_LDAP *ld, PWCHAR base, ULONG scope,
282     PWCHAR filter, PWCHAR attrs[], ULONG attrsonly, PLDAPControlW *serverctrls,
283     PLDAPControlW *clientctrls, struct l_timeval* timeout, ULONG sizelimit, WLDAP32_LDAPMessage **res )
284 {
285     ULONG ret = LDAP_NOT_SUPPORTED;
286 #ifdef HAVE_LDAP
287     char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
288     LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
289
290     ret = WLDAP32_LDAP_NO_MEMORY;
291
292     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx, %p, %p, %p, 0x%08lx, %p)\n",
293            ld, debugstr_w(base), scope, debugstr_w(filter), attrs, attrsonly,
294            serverctrls, clientctrls, timeout, sizelimit, res );
295
296     if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
297
298     if (base) {
299         baseU = strWtoU( base );
300         if (!baseU) goto exit;
301     }
302     if (filter) {
303         filterU = strWtoU( filter );
304         if (!filterU) goto exit;
305     }
306     if (attrs) {
307         attrsU = strarrayWtoU( attrs );
308         if (!attrsU) goto exit;
309     }
310     if (serverctrls) {
311         serverctrlsU = controlarrayWtoU( serverctrls );
312         if (!serverctrlsU) goto exit;
313     }
314     if (clientctrls) {
315         clientctrlsU = controlarrayWtoU( clientctrls );
316         if (!clientctrlsU) goto exit;
317     }
318
319     ret = ldap_search_ext_s( ld, baseU, scope, filterU, attrsU, attrsonly,
320                              serverctrlsU, clientctrlsU, (struct timeval *)timeout, sizelimit, res );
321
322 exit:
323     strfreeU( baseU );
324     strfreeU( filterU );
325     strarrayfreeU( attrsU );
326     controlarrayfreeU( serverctrlsU );
327     controlarrayfreeU( clientctrlsU );
328
329 #endif
330     return ret;
331 }
332
333 ULONG ldap_search_sA( WLDAP32_LDAP *ld, PCHAR base, ULONG scope, PCHAR filter,
334     PCHAR attrs[], ULONG attrsonly, WLDAP32_LDAPMessage **res )
335 {
336     ULONG ret = LDAP_NOT_SUPPORTED;
337 #ifdef HAVE_LDAP
338     WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
339
340     ret = WLDAP32_LDAP_NO_MEMORY;
341
342     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx, %p)\n", ld, debugstr_a(base),
343            scope, debugstr_a(filter), attrs, attrsonly, res );
344
345     if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
346
347     if (base) {
348         baseW = strAtoW( base );
349         if (!baseW) goto exit;
350     }
351     if (filter) {
352         filterW = strAtoW( filter );
353         if (!filterW) goto exit;
354     }
355     if (attrs) {
356         attrsW = strarrayAtoW( attrs );
357         if (!attrsW) goto exit;
358     }
359
360     ret = ldap_search_sW( ld, baseW, scope, filterW, attrsW, attrsonly, res );
361
362 exit:
363     strfreeW( baseW );
364     strfreeW( filterW );
365     strarrayfreeW( attrsW );
366
367 #endif
368     return ret;
369 }
370
371 ULONG ldap_search_sW( WLDAP32_LDAP *ld, PWCHAR base, ULONG scope, PWCHAR filter,
372     PWCHAR attrs[], ULONG attrsonly, WLDAP32_LDAPMessage **res )
373 {
374     ULONG ret = LDAP_NOT_SUPPORTED;
375 #ifdef HAVE_LDAP
376     char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
377
378     ret = WLDAP32_LDAP_NO_MEMORY;
379
380     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx, %p)\n", ld, debugstr_w(base),
381            scope, debugstr_w(filter), attrs, attrsonly, res );
382
383     if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
384
385     if (base) {
386         baseU = strWtoU( base );
387         if (!baseU) goto exit;
388     }
389     if (filter) {
390         filterU = strWtoU( filter );
391         if (!filterU) goto exit;
392     }
393     if (attrs) {
394         attrsU = strarrayWtoU( attrs );
395         if (!attrsU) goto exit;
396     }
397
398     ret = ldap_search_s( ld, baseU, scope, filterU, attrsU, attrsonly, res );
399
400 exit:
401     strfreeU( baseU );
402     strfreeU( filterU );
403     strarrayfreeU( attrsU );
404
405 #endif
406     return ret;
407 }
408
409 ULONG ldap_search_stA( WLDAP32_LDAP *ld, const PCHAR base, ULONG scope,
410     const PCHAR filter, PCHAR attrs[], ULONG attrsonly,
411     struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
412 {
413     ULONG ret = LDAP_NOT_SUPPORTED;
414 #ifdef HAVE_LDAP
415     WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
416
417     ret = WLDAP32_LDAP_NO_MEMORY;
418
419     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx, %p, %p)\n", ld,
420            debugstr_a(base), scope, debugstr_a(filter), attrs,
421            attrsonly, timeout, res );
422
423     if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
424
425     if (base) {
426         baseW = strAtoW( base );
427         if (!baseW) goto exit;
428     }
429     if (filter) {
430         filterW = strAtoW( filter );
431         if (!filterW) goto exit;
432     }
433     if (attrs) {
434         attrsW = strarrayAtoW( attrs );
435         if (!attrsW) goto exit;
436     }
437
438     ret = ldap_search_stW( ld, baseW, scope, filterW, attrsW, attrsonly,
439                            timeout, res );
440
441 exit:
442     strfreeW( baseW );
443     strfreeW( filterW );
444     strarrayfreeW( attrsW );
445
446 #endif
447     return ret;
448 }
449
450 ULONG ldap_search_stW( WLDAP32_LDAP *ld, const PWCHAR base, ULONG scope,
451     const PWCHAR filter, PWCHAR attrs[], ULONG attrsonly,
452     struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
453 {
454     ULONG ret = LDAP_NOT_SUPPORTED;
455 #ifdef HAVE_LDAP
456     char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
457
458     ret = WLDAP32_LDAP_NO_MEMORY;
459
460     TRACE( "(%p, %s, 0x%08lx, %s, %p, 0x%08lx, %p, %p)\n", ld,
461            debugstr_w(base), scope, debugstr_w(filter), attrs,
462            attrsonly, timeout, res );
463
464     if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
465
466     if (base) {
467         baseU = strWtoU( base );
468         if (!baseU) goto exit;
469     }
470     if (filter) {
471         filterU = strWtoU( filter );
472         if (!filterU) goto exit;
473     }
474     if (attrs) {
475         attrsU = strarrayWtoU( attrs );
476         if (!attrsU) goto exit;
477     }
478
479     ret = ldap_search_st( ld, baseU, scope, filterU, attrsU, attrsonly,
480                           (struct timeval *)timeout, res );
481
482 exit:
483     strfreeU( baseU );
484     strfreeU( filterU );
485     strarrayfreeU( attrsU );
486
487 #endif
488     return ret;
489 }