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