* Sync up to trunk head (r64921).
[reactos.git] / dll / win32 / wldap32 / page.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 #ifndef LDAP_MAXINT
24 #define LDAP_MAXINT 2147483647
25 #endif
26
27 /***********************************************************************
28 * ldap_create_page_controlA (WLDAP32.@)
29 *
30 * See ldap_create_page_controlW.
31 */
32 ULONG CDECL ldap_create_page_controlA( WLDAP32_LDAP *ld, ULONG pagesize,
33 struct WLDAP32_berval *cookie, UCHAR critical, PLDAPControlA *control )
34 {
35 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
36 #ifdef HAVE_LDAP
37 LDAPControlW *controlW = NULL;
38
39 TRACE( "(%p, 0x%08x, %p, 0x%02x, %p)\n", ld, pagesize, cookie,
40 critical, control );
41
42 if (!ld || !control || pagesize > LDAP_MAXINT)
43 return WLDAP32_LDAP_PARAM_ERROR;
44
45 ret = ldap_create_page_controlW( ld, pagesize, cookie, critical, &controlW );
46 if (ret == LDAP_SUCCESS)
47 {
48 *control = controlWtoA( controlW );
49 ldap_control_freeW( controlW );
50 }
51
52 #endif
53 return ret;
54 }
55
56 #ifdef HAVE_LDAP
57
58 /* create a page control by hand */
59 static ULONG create_page_control( ULONG pagesize, struct WLDAP32_berval *cookie,
60 UCHAR critical, PLDAPControlW *control )
61 {
62 LDAPControlW *ctrl;
63 BerElement *ber;
64 ber_tag_t tag;
65 struct berval *berval, null_cookie = { 0, NULL };
66 INT ret, len;
67 char *val;
68
69 ber = ber_alloc_t( LBER_USE_DER );
70 if (!ber) return WLDAP32_LDAP_NO_MEMORY;
71
72 if (cookie)
73 tag = ber_printf( ber, "{iO}", (ber_int_t)pagesize, cookie );
74 else
75 tag = ber_printf( ber, "{iO}", (ber_int_t)pagesize, &null_cookie );
76
77 ret = ber_flatten( ber, &berval );
78 ber_free( ber, 1 );
79
80 if (tag == LBER_ERROR)
81 return WLDAP32_LDAP_ENCODING_ERROR;
82
83 if (ret == -1)
84 return WLDAP32_LDAP_NO_MEMORY;
85
86 /* copy the berval so it can be properly freed by the caller */
87 val = HeapAlloc( GetProcessHeap(), 0, berval->bv_len );
88 if (!val) return WLDAP32_LDAP_NO_MEMORY;
89
90 len = berval->bv_len;
91 memcpy( val, berval->bv_val, len );
92 ber_bvfree( berval );
93
94 ctrl = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
95 if (!ctrl)
96 {
97 HeapFree( GetProcessHeap(), 0, val );
98 return WLDAP32_LDAP_NO_MEMORY;
99 }
100
101 ctrl->ldctl_oid = strAtoW( LDAP_PAGED_RESULT_OID_STRING );
102 ctrl->ldctl_value.bv_len = len;
103 ctrl->ldctl_value.bv_val = val;
104 ctrl->ldctl_iscritical = critical;
105
106 *control = ctrl;
107
108 return WLDAP32_LDAP_SUCCESS;
109 }
110
111 #endif /* HAVE_LDAP */
112
113 /***********************************************************************
114 * ldap_create_page_controlW (WLDAP32.@)
115 *
116 * Create a control for paged search results.
117 *
118 * PARAMS
119 * ld [I] Pointer to an LDAP context.
120 * pagesize [I] Number of entries to return per page.
121 * cookie [I] Used by the server to track its location in the
122 * search results.
123 * critical [I] Tells the server this control is critical to the
124 * search operation.
125 * control [O] LDAPControl created.
126 *
127 * RETURNS
128 * Success: LDAP_SUCCESS
129 * Failure: An LDAP error code.
130 */
131 ULONG CDECL ldap_create_page_controlW( WLDAP32_LDAP *ld, ULONG pagesize,
132 struct WLDAP32_berval *cookie, UCHAR critical, PLDAPControlW *control )
133 {
134 #ifdef HAVE_LDAP
135 TRACE( "(%p, 0x%08x, %p, 0x%02x, %p)\n", ld, pagesize, cookie,
136 critical, control );
137
138 if (!ld || !control || pagesize > LDAP_MAXINT)
139 return WLDAP32_LDAP_PARAM_ERROR;
140
141 return create_page_control( pagesize, cookie, critical, control );
142
143 #else
144 return WLDAP32_LDAP_NOT_SUPPORTED;
145 #endif
146 }
147
148 ULONG CDECL ldap_get_next_page( WLDAP32_LDAP *ld, PLDAPSearch search, ULONG pagesize,
149 ULONG *message )
150 {
151 FIXME( "(%p, %p, 0x%08x, %p)\n", ld, search, pagesize, message );
152
153 if (!ld) return ~0u;
154 return WLDAP32_LDAP_NOT_SUPPORTED;
155 }
156
157 ULONG CDECL ldap_get_next_page_s( WLDAP32_LDAP *ld, PLDAPSearch search,
158 struct l_timeval *timeout, ULONG pagesize, ULONG *count,
159 WLDAP32_LDAPMessage **results )
160 {
161 FIXME( "(%p, %p, %p, 0x%08x, %p, %p)\n", ld, search, timeout,
162 pagesize, count, results );
163
164 if (!ld) return ~0u;
165 return WLDAP32_LDAP_NOT_SUPPORTED;
166 }
167
168 ULONG CDECL ldap_get_paged_count( WLDAP32_LDAP *ld, PLDAPSearch search,
169 ULONG *count, WLDAP32_LDAPMessage *results )
170 {
171 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
172 #ifdef HAVE_LDAP
173 FIXME( "(%p, %p, %p, %p)\n", ld, search, count, results );
174
175 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
176 /* FIXME: save the cookie from the server here */
177
178 #endif
179 return ret;
180 }
181
182 /***********************************************************************
183 * ldap_parse_page_controlA (WLDAP32.@)
184 */
185 ULONG CDECL ldap_parse_page_controlA( WLDAP32_LDAP *ld, PLDAPControlA *ctrls,
186 ULONG *count, struct WLDAP32_berval **cookie )
187 {
188 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
189 #ifdef HAVE_LDAP
190 LDAPControlW **ctrlsW = NULL;
191
192 TRACE( "(%p, %p, %p, %p)\n", ld, ctrls, count, cookie );
193
194 if (!ld || !ctrls || !count || !cookie)
195 return WLDAP32_LDAP_PARAM_ERROR;
196
197 ctrlsW = controlarrayAtoW( ctrls );
198 if (!ctrlsW) return WLDAP32_LDAP_NO_MEMORY;
199
200 ret = ldap_parse_page_controlW( ld, ctrlsW, count, cookie );
201 controlarrayfreeW( ctrlsW );
202
203 #endif
204 return ret;
205 }
206
207 /***********************************************************************
208 * ldap_parse_page_controlW (WLDAP32.@)
209 */
210 ULONG CDECL ldap_parse_page_controlW( WLDAP32_LDAP *ld, PLDAPControlW *ctrls,
211 ULONG *count, struct WLDAP32_berval **cookie )
212 {
213 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
214 #ifdef HAVE_LDAP
215 LDAPControlW *control = NULL;
216 BerElement *ber;
217 ber_tag_t tag;
218 ULONG i;
219
220 TRACE( "(%p, %p, %p, %p)\n", ld, ctrls, count, cookie );
221
222 if (!ld || !ctrls || !count || !cookie)
223 return WLDAP32_LDAP_PARAM_ERROR;
224
225 for (i = 0; ctrls[i]; i++)
226 {
227 if (!lstrcmpW( LDAP_PAGED_RESULT_OID_STRING_W, ctrls[i]->ldctl_oid ))
228 control = ctrls[i];
229 }
230
231 if (!control)
232 return WLDAP32_LDAP_CONTROL_NOT_FOUND;
233
234 ber = ber_init( &((LDAPControl *)control)->ldctl_value );
235 if (!ber)
236 return WLDAP32_LDAP_NO_MEMORY;
237
238 tag = ber_scanf( ber, "{iO}", count, cookie );
239 if ( tag == LBER_ERROR )
240 ret = WLDAP32_LDAP_DECODING_ERROR;
241 else
242 ret = WLDAP32_LDAP_SUCCESS;
243
244 ber_free( ber, 1 );
245
246 #endif
247 return ret;
248 }
249
250 ULONG CDECL ldap_search_abandon_page( WLDAP32_LDAP *ld, PLDAPSearch search )
251 {
252 FIXME( "(%p, %p)\n", ld, search );
253
254 if (!ld) return ~0u;
255 return WLDAP32_LDAP_SUCCESS;
256 }
257
258 PLDAPSearch CDECL ldap_search_init_pageA( WLDAP32_LDAP *ld, PCHAR dn, ULONG scope,
259 PCHAR filter, PCHAR attrs[], ULONG attrsonly, PLDAPControlA *serverctrls,
260 PLDAPControlA *clientctrls, ULONG timelimit, ULONG sizelimit, PLDAPSortKeyA *sortkeys )
261 {
262 FIXME( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_a(dn),
263 scope, debugstr_a(filter), attrs, attrsonly );
264 return NULL;
265 }
266
267 PLDAPSearch CDECL ldap_search_init_pageW( WLDAP32_LDAP *ld, PWCHAR dn, ULONG scope,
268 PWCHAR filter, PWCHAR attrs[], ULONG attrsonly, PLDAPControlW *serverctrls,
269 PLDAPControlW *clientctrls, ULONG timelimit, ULONG sizelimit, PLDAPSortKeyW *sortkeys )
270 {
271 FIXME( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_w(dn),
272 scope, debugstr_w(filter), attrs, attrsonly );
273 return NULL;
274 }