7680ac7b3c72b91d6d2fadb09aa474c802dbb27c
[reactos.git] / dll / win32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "winldap_private.h"
22
23 /***********************************************************************
24 * ldap_searchA (WLDAP32.@)
25 *
26 * See ldap_searchW.
27 */
28 ULONG CDECL ldap_searchA( WLDAP32_LDAP *ld, PCHAR base, ULONG scope, PCHAR filter,
29 PCHAR attrs[], ULONG attrsonly )
30 {
31 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
32 #ifdef HAVE_LDAP
33 WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
34
35 ret = WLDAP32_LDAP_NO_MEMORY;
36
37 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_a(base),
38 scope, debugstr_a(filter), attrs, attrsonly );
39
40 if (!ld) return ~0u;
41
42 if (base) {
43 baseW = strAtoW( base );
44 if (!baseW) goto exit;
45 }
46 if (filter) {
47 filterW = strAtoW( filter );
48 if (!filterW) goto exit;
49 }
50 if (attrs) {
51 attrsW = strarrayAtoW( attrs );
52 if (!attrsW) goto exit;
53 }
54
55 ret = ldap_searchW( ld, baseW, scope, filterW, attrsW, attrsonly );
56
57 exit:
58 strfreeW( baseW );
59 strfreeW( filterW );
60 strarrayfreeW( attrsW );
61
62 #endif
63 return ret;
64 }
65
66 /***********************************************************************
67 * ldap_searchW (WLDAP32.@)
68 *
69 * Search a directory tree (asynchronous operation).
70 *
71 * PARAMS
72 * ld [I] Pointer to an LDAP context.
73 * base [I] Starting point for the search.
74 * scope [I] Search scope. One of LDAP_SCOPE_BASE,
75 * LDAP_SCOPE_ONELEVEL and LDAP_SCOPE_SUBTREE.
76 * filter [I] Search filter.
77 * attrs [I] Attributes to return.
78 * attrsonly [I] Return no values, only attributes.
79 *
80 * RETURNS
81 * Success: Message ID of the search operation.
82 * Failure: ~0u
83 *
84 * NOTES
85 * Call ldap_result with the message ID to get the result of
86 * the operation. Cancel the operation by calling ldap_abandon
87 * with the message ID.
88 */
89 ULONG CDECL ldap_searchW( WLDAP32_LDAP *ld, PWCHAR base, ULONG scope, PWCHAR filter,
90 PWCHAR attrs[], ULONG attrsonly )
91 {
92 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
93 #ifdef HAVE_LDAP
94 char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
95 int msg;
96
97 ret = WLDAP32_LDAP_NO_MEMORY;
98
99 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_w(base),
100 scope, debugstr_w(filter), attrs, attrsonly );
101
102 if (!ld) return ~0u;
103
104 if (base) {
105 baseU = strWtoU( base );
106 if (!baseU) goto exit;
107 }
108 if (filter) {
109 filterU = strWtoU( filter );
110 if (!filterU) goto exit;
111 }
112 if (attrs) {
113 attrsU = strarrayWtoU( attrs );
114 if (!attrsU) goto exit;
115 }
116
117 ret = ldap_search_ext( ld, baseU, scope, filterU, attrsU, attrsonly,
118 NULL, NULL, NULL, 0, &msg );
119
120 if (ret == LDAP_SUCCESS)
121 ret = msg;
122 else
123 ret = ~0u;
124
125 exit:
126 strfreeU( baseU );
127 strfreeU( filterU );
128 strarrayfreeU( attrsU );
129
130 #endif
131 return ret;
132 }
133
134 /***********************************************************************
135 * ldap_search_extA (WLDAP32.@)
136 *
137 * See ldap_search_extW.
138 */
139 ULONG CDECL ldap_search_extA( WLDAP32_LDAP *ld, PCHAR base, ULONG scope,
140 PCHAR filter, PCHAR attrs[], ULONG attrsonly, PLDAPControlA *serverctrls,
141 PLDAPControlA *clientctrls, ULONG timelimit, ULONG sizelimit, ULONG *message )
142 {
143 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
144 #ifdef HAVE_LDAP
145 WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
146 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
147
148 ret = WLDAP32_LDAP_NO_MEMORY;
149
150 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p, %p, 0x%08x, 0x%08x, %p)\n",
151 ld, debugstr_a(base), scope, debugstr_a(filter), attrs, attrsonly,
152 serverctrls, clientctrls, timelimit, sizelimit, message );
153
154 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
155
156 if (base) {
157 baseW = strAtoW( base );
158 if (!baseW) goto exit;
159 }
160 if (filter)
161 {
162 filterW = strAtoW( filter );
163 if (!filterW) goto exit;
164 }
165 if (attrs) {
166 attrsW = strarrayAtoW( attrs );
167 if (!attrsW) goto exit;
168 }
169 if (serverctrls) {
170 serverctrlsW = controlarrayAtoW( serverctrls );
171 if (!serverctrlsW) goto exit;
172 }
173 if (clientctrls) {
174 clientctrlsW = controlarrayAtoW( clientctrls );
175 if (!clientctrlsW) goto exit;
176 }
177
178 ret = ldap_search_extW( ld, baseW, scope, filterW, attrsW, attrsonly,
179 serverctrlsW, clientctrlsW, timelimit, sizelimit, message );
180
181 exit:
182 strfreeW( baseW );
183 strfreeW( filterW );
184 strarrayfreeW( attrsW );
185 controlarrayfreeW( serverctrlsW );
186 controlarrayfreeW( clientctrlsW );
187
188 #endif
189 return ret;
190 }
191
192 /***********************************************************************
193 * ldap_search_extW (WLDAP32.@)
194 *
195 * Search a directory tree (asynchronous operation).
196 *
197 * PARAMS
198 * ld [I] Pointer to an LDAP context.
199 * base [I] Starting point for the search.
200 * scope [I] Search scope. One of LDAP_SCOPE_BASE,
201 * LDAP_SCOPE_ONELEVEL and LDAP_SCOPE_SUBTREE.
202 * filter [I] Search filter.
203 * attrs [I] Attributes to return.
204 * attrsonly [I] Return no values, only attributes.
205 * serverctrls [I] Array of LDAP server controls.
206 * clientctrls [I] Array of LDAP client controls.
207 * timelimit [I] Timeout in seconds.
208 * sizelimit [I] Maximum number of entries to return. Zero means unlimited.
209 * message [O] Message ID of the search operation.
210 *
211 * RETURNS
212 * Success: LDAP_SUCCESS
213 * Failure: An LDAP error code.
214 *
215 * NOTES
216 * Call ldap_result with the message ID to get the result of
217 * the operation. Cancel the operation by calling ldap_abandon
218 * with the message ID.
219 */
220 ULONG CDECL ldap_search_extW( WLDAP32_LDAP *ld, PWCHAR base, ULONG scope,
221 PWCHAR filter, PWCHAR attrs[], ULONG attrsonly, PLDAPControlW *serverctrls,
222 PLDAPControlW *clientctrls, ULONG timelimit, ULONG sizelimit, ULONG *message )
223 {
224 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
225 #ifdef HAVE_LDAP
226 char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
227 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
228 struct timeval tv, *tvp = NULL;
229
230 ret = WLDAP32_LDAP_NO_MEMORY;
231
232 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p, %p, 0x%08x, 0x%08x, %p)\n",
233 ld, debugstr_w(base), scope, debugstr_w(filter), attrs, attrsonly,
234 serverctrls, clientctrls, timelimit, sizelimit, message );
235
236 if (!ld) return ~0u;
237
238 if (base) {
239 baseU = strWtoU( base );
240 if (!baseU) goto exit;
241 }
242 if (filter) {
243 filterU = strWtoU( filter );
244 if (!filterU) goto exit;
245 }
246 if (attrs) {
247 attrsU = strarrayWtoU( attrs );
248 if (!attrsU) goto exit;
249 }
250 if (serverctrls) {
251 serverctrlsU = controlarrayWtoU( serverctrls );
252 if (!serverctrlsU) goto exit;
253 }
254 if (clientctrls) {
255 clientctrlsU = controlarrayWtoU( clientctrls );
256 if (!clientctrlsU) goto exit;
257 }
258
259 if (timelimit)
260 {
261 tv.tv_sec = timelimit;
262 tv.tv_usec = 0;
263 tvp = &tv;
264 }
265
266 ret = map_error( ldap_search_ext( ld, baseU, scope, filterU, attrsU, attrsonly,
267 serverctrlsU, clientctrlsU, tvp, sizelimit, (int *)message ));
268
269 exit:
270 strfreeU( baseU );
271 strfreeU( filterU );
272 strarrayfreeU( attrsU );
273 controlarrayfreeU( serverctrlsU );
274 controlarrayfreeU( clientctrlsU );
275
276 #endif
277 return ret;
278 }
279
280 /***********************************************************************
281 * ldap_search_ext_sA (WLDAP32.@)
282 *
283 * See ldap_search_ext_sW.
284 */
285 ULONG CDECL ldap_search_ext_sA( WLDAP32_LDAP *ld, PCHAR base, ULONG scope,
286 PCHAR filter, PCHAR attrs[], ULONG attrsonly, PLDAPControlA *serverctrls,
287 PLDAPControlA *clientctrls, struct l_timeval* timeout, ULONG sizelimit, WLDAP32_LDAPMessage **res )
288 {
289 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
290 #ifdef HAVE_LDAP
291 WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
292 LDAPControlW **serverctrlsW = NULL, **clientctrlsW = NULL;
293
294 ret = WLDAP32_LDAP_NO_MEMORY;
295
296 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p, %p, %p, 0x%08x, %p)\n",
297 ld, debugstr_a(base), scope, debugstr_a(filter), attrs, attrsonly,
298 serverctrls, clientctrls, timeout, sizelimit, res );
299
300 if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
301
302 if (base) {
303 baseW = strAtoW( base );
304 if (!baseW) goto exit;
305 }
306 if (filter) {
307 filterW = strAtoW( filter );
308 if (!filterW) goto exit;
309 }
310 if (attrs) {
311 attrsW = strarrayAtoW( attrs );
312 if (!attrsW) goto exit;
313 }
314 if (serverctrls) {
315 serverctrlsW = controlarrayAtoW( serverctrls );
316 if (!serverctrlsW) goto exit;
317 }
318 if (clientctrls) {
319 clientctrlsW = controlarrayAtoW( clientctrls );
320 if (!clientctrlsW) goto exit;
321 }
322
323 ret = ldap_search_ext_sW( ld, baseW, scope, filterW, attrsW, attrsonly,
324 serverctrlsW, clientctrlsW, timeout, sizelimit, res );
325
326 exit:
327 strfreeW( baseW );
328 strfreeW( filterW );
329 strarrayfreeW( attrsW );
330 controlarrayfreeW( serverctrlsW );
331 controlarrayfreeW( clientctrlsW );
332
333 #endif
334 return ret;
335 }
336
337 /***********************************************************************
338 * ldap_search_ext_sW (WLDAP32.@)
339 *
340 * Search a directory tree (synchronous operation).
341 *
342 * PARAMS
343 * ld [I] Pointer to an LDAP context.
344 * base [I] Starting point for the search.
345 * scope [I] Search scope. One of LDAP_SCOPE_BASE,
346 * LDAP_SCOPE_ONELEVEL and LDAP_SCOPE_SUBTREE.
347 * filter [I] Search filter.
348 * attrs [I] Attributes to return.
349 * attrsonly [I] Return no values, only attributes.
350 * serverctrls [I] Array of LDAP server controls.
351 * clientctrls [I] Array of LDAP client controls.
352 * timeout [I] Timeout in seconds.
353 * sizelimit [I] Maximum number of entries to return. Zero means unlimited.
354 * res [O] Results of the search operation.
355 *
356 * RETURNS
357 * Success: LDAP_SUCCESS
358 * Failure: An LDAP error code.
359 *
360 * NOTES
361 * Call ldap_msgfree to free the results.
362 */
363 ULONG CDECL ldap_search_ext_sW( WLDAP32_LDAP *ld, PWCHAR base, ULONG scope,
364 PWCHAR filter, PWCHAR attrs[], ULONG attrsonly, PLDAPControlW *serverctrls,
365 PLDAPControlW *clientctrls, struct l_timeval* timeout, ULONG sizelimit, WLDAP32_LDAPMessage **res )
366 {
367 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
368 #ifdef HAVE_LDAP
369 char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
370 LDAPControl **serverctrlsU = NULL, **clientctrlsU = NULL;
371
372 ret = WLDAP32_LDAP_NO_MEMORY;
373
374 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p, %p, %p, 0x%08x, %p)\n",
375 ld, debugstr_w(base), scope, debugstr_w(filter), attrs, attrsonly,
376 serverctrls, clientctrls, timeout, sizelimit, res );
377
378 if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
379
380 if (base) {
381 baseU = strWtoU( base );
382 if (!baseU) goto exit;
383 }
384 if (filter) {
385 filterU = strWtoU( filter );
386 if (!filterU) goto exit;
387 }
388 if (attrs) {
389 attrsU = strarrayWtoU( attrs );
390 if (!attrsU) goto exit;
391 }
392 if (serverctrls) {
393 serverctrlsU = controlarrayWtoU( serverctrls );
394 if (!serverctrlsU) goto exit;
395 }
396 if (clientctrls) {
397 clientctrlsU = controlarrayWtoU( clientctrls );
398 if (!clientctrlsU) goto exit;
399 }
400
401 ret = map_error( ldap_search_ext_s( ld, baseU, scope, filterU, attrsU, attrsonly,
402 serverctrlsU, clientctrlsU, (struct timeval *)timeout,
403 sizelimit, res ));
404
405 exit:
406 strfreeU( baseU );
407 strfreeU( filterU );
408 strarrayfreeU( attrsU );
409 controlarrayfreeU( serverctrlsU );
410 controlarrayfreeU( clientctrlsU );
411
412 #endif
413 return ret;
414 }
415
416 /***********************************************************************
417 * ldap_search_sA (WLDAP32.@)
418 *
419 * See ldap_search_sW.
420 */
421 ULONG CDECL ldap_search_sA( WLDAP32_LDAP *ld, PCHAR base, ULONG scope, PCHAR filter,
422 PCHAR attrs[], ULONG attrsonly, WLDAP32_LDAPMessage **res )
423 {
424 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
425 #ifdef HAVE_LDAP
426 WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
427
428 ret = WLDAP32_LDAP_NO_MEMORY;
429
430 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p)\n", ld, debugstr_a(base),
431 scope, debugstr_a(filter), attrs, attrsonly, res );
432
433 if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
434
435 if (base) {
436 baseW = strAtoW( base );
437 if (!baseW) goto exit;
438 }
439 if (filter) {
440 filterW = strAtoW( filter );
441 if (!filterW) goto exit;
442 }
443 if (attrs) {
444 attrsW = strarrayAtoW( attrs );
445 if (!attrsW) goto exit;
446 }
447
448 ret = ldap_search_sW( ld, baseW, scope, filterW, attrsW, attrsonly, res );
449
450 exit:
451 strfreeW( baseW );
452 strfreeW( filterW );
453 strarrayfreeW( attrsW );
454
455 #endif
456 return ret;
457 }
458
459 /***********************************************************************
460 * ldap_search_sW (WLDAP32.@)
461 *
462 * Search a directory tree (synchronous operation).
463 *
464 * PARAMS
465 * ld [I] Pointer to an LDAP context.
466 * base [I] Starting point for the search.
467 * scope [I] Search scope. One of LDAP_SCOPE_BASE,
468 * LDAP_SCOPE_ONELEVEL and LDAP_SCOPE_SUBTREE.
469 * filter [I] Search filter.
470 * attrs [I] Attributes to return.
471 * attrsonly [I] Return no values, only attributes.
472 * res [O] Results of the search operation.
473 *
474 * RETURNS
475 * Success: LDAP_SUCCESS
476 * Failure: An LDAP error code.
477 *
478 * NOTES
479 * Call ldap_msgfree to free the results.
480 */
481 ULONG CDECL ldap_search_sW( WLDAP32_LDAP *ld, PWCHAR base, ULONG scope, PWCHAR filter,
482 PWCHAR attrs[], ULONG attrsonly, WLDAP32_LDAPMessage **res )
483 {
484 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
485 #ifdef HAVE_LDAP
486 char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
487
488 ret = WLDAP32_LDAP_NO_MEMORY;
489
490 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p)\n", ld, debugstr_w(base),
491 scope, debugstr_w(filter), attrs, attrsonly, res );
492
493 if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
494
495 if (base) {
496 baseU = strWtoU( base );
497 if (!baseU) goto exit;
498 }
499 if (filter) {
500 filterU = strWtoU( filter );
501 if (!filterU) goto exit;
502 }
503 if (attrs) {
504 attrsU = strarrayWtoU( attrs );
505 if (!attrsU) goto exit;
506 }
507
508 ret = map_error( ldap_search_ext_s( ld, baseU, scope, filterU, attrsU, attrsonly,
509 NULL, NULL, NULL, 0, res ));
510
511 exit:
512 strfreeU( baseU );
513 strfreeU( filterU );
514 strarrayfreeU( attrsU );
515
516 #endif
517 return ret;
518 }
519
520 /***********************************************************************
521 * ldap_search_stA (WLDAP32.@)
522 *
523 * See ldap_search_stW.
524 */
525 ULONG CDECL ldap_search_stA( WLDAP32_LDAP *ld, const PCHAR base, ULONG scope,
526 const PCHAR filter, PCHAR attrs[], ULONG attrsonly,
527 struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
528 {
529 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
530 #ifdef HAVE_LDAP
531 WCHAR *baseW = NULL, *filterW = NULL, **attrsW = NULL;
532
533 ret = WLDAP32_LDAP_NO_MEMORY;
534
535 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p, %p)\n", ld,
536 debugstr_a(base), scope, debugstr_a(filter), attrs,
537 attrsonly, timeout, res );
538
539 if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
540
541 if (base) {
542 baseW = strAtoW( base );
543 if (!baseW) goto exit;
544 }
545 if (filter) {
546 filterW = strAtoW( filter );
547 if (!filterW) goto exit;
548 }
549 if (attrs) {
550 attrsW = strarrayAtoW( attrs );
551 if (!attrsW) goto exit;
552 }
553
554 ret = ldap_search_stW( ld, baseW, scope, filterW, attrsW, attrsonly,
555 timeout, res );
556
557 exit:
558 strfreeW( baseW );
559 strfreeW( filterW );
560 strarrayfreeW( attrsW );
561
562 #endif
563 return ret;
564 }
565
566 /***********************************************************************
567 * ldap_search_stW (WLDAP32.@)
568 *
569 * Search a directory tree (synchronous operation).
570 *
571 * PARAMS
572 * ld [I] Pointer to an LDAP context.
573 * base [I] Starting point for the search.
574 * scope [I] Search scope. One of LDAP_SCOPE_BASE,
575 * LDAP_SCOPE_ONELEVEL and LDAP_SCOPE_SUBTREE.
576 * filter [I] Search filter.
577 * attrs [I] Attributes to return.
578 * attrsonly [I] Return no values, only attributes.
579 * timeout [I] Timeout in seconds.
580 * res [O] Results of the search operation.
581 *
582 * RETURNS
583 * Success: LDAP_SUCCESS
584 * Failure: An LDAP error code.
585 *
586 * NOTES
587 * Call ldap_msgfree to free the results.
588 */
589 ULONG CDECL ldap_search_stW( WLDAP32_LDAP *ld, const PWCHAR base, ULONG scope,
590 const PWCHAR filter, PWCHAR attrs[], ULONG attrsonly,
591 struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
592 {
593 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
594 #ifdef HAVE_LDAP
595 char *baseU = NULL, *filterU = NULL, **attrsU = NULL;
596
597 ret = WLDAP32_LDAP_NO_MEMORY;
598
599 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p, %p)\n", ld,
600 debugstr_w(base), scope, debugstr_w(filter), attrs,
601 attrsonly, timeout, res );
602
603 if (!ld || !res) return WLDAP32_LDAP_PARAM_ERROR;
604
605 if (base) {
606 baseU = strWtoU( base );
607 if (!baseU) goto exit;
608 }
609 if (filter) {
610 filterU = strWtoU( filter );
611 if (!filterU) goto exit;
612 }
613 if (attrs) {
614 attrsU = strarrayWtoU( attrs );
615 if (!attrsU) goto exit;
616 }
617
618 ret = map_error( ldap_search_ext_s( ld, baseU, scope, filterU, attrsU, attrsonly,
619 NULL, NULL, (struct timeval *)timeout, 0, res ));
620
621 exit:
622 strfreeU( baseU );
623 strfreeU( filterU );
624 strarrayfreeU( attrsU );
625
626 #endif
627 return ret;
628 }