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