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