msvcrt: Improve acosf compatibility with native ucrtbase.
[wine.git] / dlls / wldap32 / page.c
bloba16ea52409ef92d6efdbf39ba7328212473c2b4c
1 /*
2 * WLDAP32 - LDAP support for Wine
4 * Copyright 2005 Hans Leidekker
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #ifdef HAVE_LDAP_H
26 #include <ldap.h>
27 #endif
28 #ifndef LDAP_MAXINT
29 #define LDAP_MAXINT 2147483647
30 #endif
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winnls.h"
36 #include "winldap_private.h"
37 #include "wldap32.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
42 #ifdef HAVE_LDAP
43 static struct berval null_cookieU = { 0, NULL };
44 static struct WLDAP32_berval null_cookieW = { 0, NULL };
45 #endif
47 /***********************************************************************
48 * ldap_create_page_controlA (WLDAP32.@)
50 * See ldap_create_page_controlW.
52 ULONG CDECL ldap_create_page_controlA( WLDAP32_LDAP *ld, ULONG pagesize,
53 struct WLDAP32_berval *cookie, UCHAR critical, PLDAPControlA *control )
55 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
56 #ifdef HAVE_LDAP
57 LDAPControlW *controlW = NULL;
59 TRACE( "(%p, 0x%08x, %p, 0x%02x, %p)\n", ld, pagesize, cookie,
60 critical, control );
62 if (!ld || !control || pagesize > LDAP_MAXINT)
63 return WLDAP32_LDAP_PARAM_ERROR;
65 ret = ldap_create_page_controlW( ld, pagesize, cookie, critical, &controlW );
66 if (ret == LDAP_SUCCESS)
68 *control = controlWtoA( controlW );
69 ldap_control_freeW( controlW );
72 #endif
73 return ret;
76 #ifdef HAVE_LDAP
78 /* create a page control by hand */
79 static ULONG create_page_control( ULONG pagesize, struct berval *cookie,
80 UCHAR critical, PLDAPControlW *control )
82 LDAPControlW *ctrl;
83 BerElement *ber;
84 ber_tag_t tag;
85 struct berval *berval;
86 INT ret, len;
87 char *val;
89 ber = ber_alloc_t( LBER_USE_DER );
90 if (!ber) return WLDAP32_LDAP_NO_MEMORY;
92 if (cookie)
93 tag = ber_printf( ber, "{iO}", (ber_int_t)pagesize, cookie );
94 else
95 tag = ber_printf( ber, "{iO}", (ber_int_t)pagesize, &null_cookieU );
97 ret = ber_flatten( ber, &berval );
98 ber_free( ber, 1 );
100 if (tag == LBER_ERROR)
101 return WLDAP32_LDAP_ENCODING_ERROR;
103 if (ret == -1)
104 return WLDAP32_LDAP_NO_MEMORY;
106 /* copy the berval so it can be properly freed by the caller */
107 if (!(val = heap_alloc( berval->bv_len ))) return WLDAP32_LDAP_NO_MEMORY;
109 len = berval->bv_len;
110 memcpy( val, berval->bv_val, len );
111 ber_bvfree( berval );
113 if (!(ctrl = heap_alloc( sizeof(LDAPControlW) )))
115 heap_free( val );
116 return WLDAP32_LDAP_NO_MEMORY;
119 ctrl->ldctl_oid = strAtoW( LDAP_PAGED_RESULT_OID_STRING );
120 ctrl->ldctl_value.bv_len = len;
121 ctrl->ldctl_value.bv_val = val;
122 ctrl->ldctl_iscritical = critical;
124 *control = ctrl;
126 return WLDAP32_LDAP_SUCCESS;
129 #endif /* HAVE_LDAP */
131 /***********************************************************************
132 * ldap_create_page_controlW (WLDAP32.@)
134 * Create a control for paged search results.
136 * PARAMS
137 * ld [I] Pointer to an LDAP context.
138 * pagesize [I] Number of entries to return per page.
139 * cookie [I] Used by the server to track its location in the
140 * search results.
141 * critical [I] Tells the server this control is critical to the
142 * search operation.
143 * control [O] LDAPControl created.
145 * RETURNS
146 * Success: LDAP_SUCCESS
147 * Failure: An LDAP error code.
149 ULONG CDECL ldap_create_page_controlW( WLDAP32_LDAP *ld, ULONG pagesize,
150 struct WLDAP32_berval *cookie, UCHAR critical, PLDAPControlW *control )
152 #ifdef HAVE_LDAP
153 struct berval *cookieU = NULL;
154 ULONG ret;
156 TRACE( "(%p, 0x%08x, %p, 0x%02x, %p)\n", ld, pagesize, cookie,
157 critical, control );
159 if (!ld || !control || pagesize > LDAP_MAXINT)
160 return WLDAP32_LDAP_PARAM_ERROR;
162 if (cookie && !(cookieU = bervalWtoU( cookie ))) return WLDAP32_LDAP_NO_MEMORY;
163 ret = create_page_control( pagesize, cookieU, critical, control );
164 heap_free( cookieU );
165 return ret;
167 #else
168 return WLDAP32_LDAP_NOT_SUPPORTED;
169 #endif
172 ULONG CDECL ldap_get_next_page( WLDAP32_LDAP *ld, PLDAPSearch search, ULONG pagesize,
173 ULONG *message )
175 FIXME( "(%p, %p, 0x%08x, %p)\n", ld, search, pagesize, message );
177 if (!ld) return ~0u;
178 return WLDAP32_LDAP_NOT_SUPPORTED;
181 ULONG CDECL ldap_get_next_page_s( WLDAP32_LDAP *ld, PLDAPSearch search,
182 struct l_timeval *timeout, ULONG pagesize, ULONG *count,
183 WLDAP32_LDAPMessage **results )
185 #ifdef HAVE_LDAP
186 ULONG ret;
188 TRACE( "(%p, %p, %p, %u, %p, %p)\n", ld, search, timeout,
189 pagesize, count, results );
190 if (!ld || !search || !count || !results) return ~0u;
192 if (search->cookie && search->cookie->bv_len == 0)
194 /* end of paged results */
195 *count = 0;
196 *results = NULL;
197 return WLDAP32_LDAP_NO_RESULTS_RETURNED;
200 if (search->serverctrls[0])
202 controlfreeW( search->serverctrls[0] );
203 search->serverctrls[0] = NULL;
206 TRACE("search->cookie: %s\n", search->cookie ? debugstr_an(search->cookie->bv_val, search->cookie->bv_len) : "NULL");
207 ret = ldap_create_page_controlW( ld, pagesize, search->cookie, 1, &search->serverctrls[0] );
208 if (ret != WLDAP32_LDAP_SUCCESS) return ret;
210 ret = ldap_search_ext_sW( ld, search->dn, search->scope,
211 search->filter, search->attrs, search->attrsonly,
212 search->serverctrls, search->clientctrls,
213 search->timeout.tv_sec ? &search->timeout : NULL, search->sizelimit, results );
214 if (ret != WLDAP32_LDAP_SUCCESS) return ret;
216 return ldap_get_paged_count( ld, search, count, *results );
218 #endif
219 return WLDAP32_LDAP_NOT_SUPPORTED;
222 ULONG CDECL ldap_get_paged_count( WLDAP32_LDAP *ld, PLDAPSearch search,
223 ULONG *count, WLDAP32_LDAPMessage *results )
225 #ifdef HAVE_LDAP
226 ULONG ret;
227 LDAPControlW **server_ctrls = NULL;
229 TRACE( "(%p, %p, %p, %p)\n", ld, search, count, results );
231 if (!ld || !count || !results) return WLDAP32_LDAP_PARAM_ERROR;
233 *count = 0;
235 ret = ldap_parse_resultW( ld, results, NULL, NULL, NULL, NULL, &server_ctrls, 0 );
236 if (ret != WLDAP32_LDAP_SUCCESS) return ret;
238 if (!server_ctrls) /* assume end of paged results */
240 search->cookie = &null_cookieW;
241 return WLDAP32_LDAP_SUCCESS;
244 if (search->cookie)
246 heap_free( search->cookie );
247 search->cookie = NULL;
250 ret = ldap_parse_page_controlW( ld, server_ctrls, count, &search->cookie );
251 if (ret == WLDAP32_LDAP_SUCCESS)
252 TRACE("new search->cookie: %s, count %u\n", debugstr_an(search->cookie->bv_val, search->cookie->bv_len), *count);
254 ldap_controls_freeW( server_ctrls );
256 return ret;
258 #endif
259 return WLDAP32_LDAP_NOT_SUPPORTED;
262 /***********************************************************************
263 * ldap_parse_page_controlA (WLDAP32.@)
265 ULONG CDECL ldap_parse_page_controlA( WLDAP32_LDAP *ld, PLDAPControlA *ctrls,
266 ULONG *count, struct WLDAP32_berval **cookie )
268 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
269 #ifdef HAVE_LDAP
270 LDAPControlW **ctrlsW = NULL;
272 TRACE( "(%p, %p, %p, %p)\n", ld, ctrls, count, cookie );
274 if (!ld || !ctrls || !count || !cookie)
275 return WLDAP32_LDAP_PARAM_ERROR;
277 ctrlsW = controlarrayAtoW( ctrls );
278 if (!ctrlsW) return WLDAP32_LDAP_NO_MEMORY;
280 ret = ldap_parse_page_controlW( ld, ctrlsW, count, cookie );
281 controlarrayfreeW( ctrlsW );
283 #endif
284 return ret;
287 /***********************************************************************
288 * ldap_parse_page_controlW (WLDAP32.@)
290 ULONG CDECL ldap_parse_page_controlW( WLDAP32_LDAP *ld, PLDAPControlW *ctrls,
291 ULONG *count, struct WLDAP32_berval **cookie )
293 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
294 #ifdef HAVE_LDAP
295 LDAPControlW *control = NULL;
296 struct berval *cookieU = NULL, *valueU;
297 BerElement *ber;
298 ber_tag_t tag;
299 ULONG i;
301 TRACE( "(%p, %p, %p, %p)\n", ld, ctrls, count, cookie );
303 if (!ld || !ctrls || !count || !cookie)
304 return WLDAP32_LDAP_PARAM_ERROR;
306 for (i = 0; ctrls[i]; i++)
308 if (!lstrcmpW( LDAP_PAGED_RESULT_OID_STRING_W, ctrls[i]->ldctl_oid ))
309 control = ctrls[i];
312 if (!control)
313 return WLDAP32_LDAP_CONTROL_NOT_FOUND;
315 if (cookie && !(cookieU = bervalWtoU( *cookie )))
316 return WLDAP32_LDAP_NO_MEMORY;
318 if (!(valueU = bervalWtoU( &control->ldctl_value )))
320 heap_free( cookieU );
321 return WLDAP32_LDAP_NO_MEMORY;
324 ber = ber_init( valueU );
325 heap_free( valueU );
326 if (!ber)
328 heap_free( cookieU );
329 return WLDAP32_LDAP_NO_MEMORY;
332 tag = ber_scanf( ber, "{iO}", count, &cookieU );
333 if (tag == LBER_ERROR)
334 ret = WLDAP32_LDAP_DECODING_ERROR;
335 else
336 ret = WLDAP32_LDAP_SUCCESS;
338 heap_free( cookieU );
339 ber_free( ber, 1 );
341 #endif
342 return ret;
345 ULONG CDECL ldap_search_abandon_page( WLDAP32_LDAP *ld, PLDAPSearch search )
347 #ifdef HAVE_LDAP
348 LDAPControlW **ctrls;
350 TRACE( "(%p, %p)\n", ld, search );
352 if (!ld || !search) return ~0u;
354 strfreeW( search->dn );
355 strfreeW( search->filter );
356 strarrayfreeW( search->attrs );
357 ctrls = search->serverctrls;
358 controlfreeW( ctrls[0] ); /* page control */
359 ctrls++;
360 while (*ctrls) controlfreeW( *ctrls++ );
361 heap_free( search->serverctrls );
362 controlarrayfreeW( search->clientctrls );
363 if (search->cookie && search->cookie != &null_cookieW)
364 heap_free( search->cookie );
365 heap_free( search );
367 return WLDAP32_LDAP_SUCCESS;
369 #else
370 return WLDAP32_LDAP_NOT_SUPPORTED;
371 #endif
374 PLDAPSearch CDECL ldap_search_init_pageA( WLDAP32_LDAP *ld, PCHAR dn, ULONG scope,
375 PCHAR filter, PCHAR attrs[], ULONG attrsonly, PLDAPControlA *serverctrls,
376 PLDAPControlA *clientctrls, ULONG timelimit, ULONG sizelimit, PLDAPSortKeyA *sortkeys )
378 FIXME( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_a(dn),
379 scope, debugstr_a(filter), attrs, attrsonly );
380 return NULL;
383 PLDAPSearch CDECL ldap_search_init_pageW( WLDAP32_LDAP *ld, PWCHAR dn, ULONG scope,
384 PWCHAR filter, PWCHAR attrs[], ULONG attrsonly, PLDAPControlW *serverctrls,
385 PLDAPControlW *clientctrls, ULONG timelimit, ULONG sizelimit, PLDAPSortKeyW *sortkeys )
387 #ifdef HAVE_LDAP
388 LDAPSearch *search;
389 DWORD i, len;
391 TRACE( "(%p, %s, 0x%08x, %s, %p, 0x%08x, %p, %p, 0x%08x, 0x%08x, %p)\n",
392 ld, debugstr_w(dn), scope, debugstr_w(filter), attrs, attrsonly,
393 serverctrls, clientctrls, timelimit, sizelimit, sortkeys );
395 search = heap_alloc_zero( sizeof(*search) );
396 if (!search)
398 ld->ld_errno = WLDAP32_LDAP_NO_MEMORY;
399 return NULL;
402 if (dn)
404 search->dn = strdupW( dn );
405 if (!search->dn) goto fail;
407 if (filter)
409 search->filter = strdupW( filter );
410 if (!search->filter) goto fail;
412 if (attrs)
414 search->attrs = strarraydupW( attrs );
415 if (!search->attrs) goto fail;
418 len = serverctrls ? controlarraylenW( serverctrls ) : 0;
419 search->serverctrls = heap_alloc( sizeof(LDAPControl *) * (len + 2) );
420 if (!search->serverctrls) goto fail;
421 search->serverctrls[0] = NULL; /* reserve 0 for page control */
422 for (i = 0; i < len; i++)
424 search->serverctrls[i + 1] = controldupW( serverctrls[i] );
425 if (!search->serverctrls[i + 1]) goto fail;
427 search->serverctrls[len + 1] = NULL;
429 if (clientctrls)
431 search->clientctrls = controlarraydupW( clientctrls );
432 if (!search->clientctrls) goto fail;
435 search->scope = scope;
436 search->attrsonly = attrsonly;
437 search->timeout.tv_sec = timelimit;
438 search->timeout.tv_usec = 0;
439 search->sizelimit = sizelimit;
440 search->cookie = NULL;
442 return search;
444 fail:
445 ldap_search_abandon_page( ld, search );
446 ld->ld_errno = WLDAP32_LDAP_NO_MEMORY;
448 #endif
449 return NULL;