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