xref: /reactos/dll/win32/wldap32/control.c (revision 84ccccab)
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_control_freeA     (WLDAP32.@)
25  *
26  * See ldap_control_freeW.
27  */
28 ULONG CDECL ldap_control_freeA( LDAPControlA *control )
29 {
30     ULONG ret = WLDAP32_LDAP_SUCCESS;
31 #ifdef HAVE_LDAP
32 
33     TRACE( "(%p)\n", control );
34     controlfreeA( control );
35 
36 #endif
37     return ret;
38 }
39 
40 /***********************************************************************
41  *      ldap_control_freeW     (WLDAP32.@)
42  *
43  * Free an LDAPControl structure.
44  *
45  * PARAMS
46  *  control  [I] LDAPControl structure to free.
47  *
48  * RETURNS
49  *  LDAP_SUCCESS
50  */
51 ULONG CDECL ldap_control_freeW( LDAPControlW *control )
52 {
53     ULONG ret = WLDAP32_LDAP_SUCCESS;
54 #ifdef HAVE_LDAP
55 
56     TRACE( "(%p)\n", control );
57     controlfreeW( control );
58 
59 #endif
60     return ret;
61 }
62 
63 /***********************************************************************
64  *      ldap_controls_freeA     (WLDAP32.@)
65  *
66  * See ldap_controls_freeW.
67  */
68 ULONG CDECL ldap_controls_freeA( LDAPControlA **controls )
69 {
70     ULONG ret = WLDAP32_LDAP_SUCCESS;
71 #ifdef HAVE_LDAP
72 
73     TRACE( "(%p)\n", controls );
74     controlarrayfreeA( controls );
75 
76 #endif
77     return ret;
78 }
79 
80 /***********************************************************************
81  *      ldap_controls_freeW     (WLDAP32.@)
82  *
83  * Free an array of LDAPControl structures.
84  *
85  * PARAMS
86  *  controls  [I] Array of LDAPControl structures to free.
87  *
88  * RETURNS
89  *  LDAP_SUCCESS
90  */
91 ULONG CDECL ldap_controls_freeW( LDAPControlW **controls )
92 {
93     ULONG ret = WLDAP32_LDAP_SUCCESS;
94 #ifdef HAVE_LDAP
95 
96     TRACE( "(%p)\n", controls );
97     controlarrayfreeW( controls );
98 
99 #endif
100     return ret;
101 }
102 
103 /***********************************************************************
104  *      ldap_create_sort_controlA     (WLDAP32.@)
105  *
106  * See ldap_create_sort_controlW.
107  */
108 ULONG CDECL ldap_create_sort_controlA( WLDAP32_LDAP *ld, PLDAPSortKeyA *sortkey,
109     UCHAR critical, PLDAPControlA *control )
110 {
111     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
112 #ifdef HAVE_LDAP
113     LDAPSortKeyW **sortkeyW = NULL;
114     LDAPControlW *controlW = NULL;
115 
116     TRACE( "(%p, %p, 0x%02x, %p)\n", ld, sortkey, critical, control );
117 
118     if (!ld || !sortkey || !control)
119         return WLDAP32_LDAP_PARAM_ERROR;
120 
121     sortkeyW = sortkeyarrayAtoW( sortkey );
122     if (!sortkeyW) return WLDAP32_LDAP_NO_MEMORY;
123 
124     ret = ldap_create_sort_controlW( ld, sortkeyW, critical, &controlW );
125 
126     *control = controlWtoA( controlW );
127     if (!*control) ret = WLDAP32_LDAP_NO_MEMORY;
128 
129     ldap_control_freeW( controlW );
130     sortkeyarrayfreeW( sortkeyW );
131 
132 #endif
133     return ret;
134 }
135 
136 /***********************************************************************
137  *      ldap_create_sort_controlW     (WLDAP32.@)
138  *
139  * Create a control for server sorted search results.
140  *
141  * PARAMS
142  *  ld       [I] Pointer to an LDAP context.
143  *  sortkey  [I] Array of LDAPSortKey structures, each specifying an
144  *               attribute to use as a sort key, a matching rule and
145  *               the sort order (ascending or descending).
146  *  critical [I] Tells the server this control is critical to the
147  *               search operation.
148  *  control  [O] LDAPControl created.
149  *
150  * RETURNS
151  *  Success: LDAP_SUCCESS
152  *  Failure: An LDAP error code.
153  *
154  * NOTES
155  *  Pass the created control as a server control in subsequent calls
156  *  to ldap_search_ext(_s) to obtain sorted search results.
157  */
158 ULONG CDECL ldap_create_sort_controlW( WLDAP32_LDAP *ld, PLDAPSortKeyW *sortkey,
159     UCHAR critical, PLDAPControlW *control )
160 {
161     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
162 #ifdef HAVE_LDAP
163     LDAPSortKey **sortkeyU = NULL;
164     LDAPControl *controlU = NULL;
165 
166     TRACE( "(%p, %p, 0x%02x, %p)\n", ld, sortkey, critical, control );
167 
168     if (!ld || !sortkey || !control)
169         return WLDAP32_LDAP_PARAM_ERROR;
170 
171     sortkeyU = sortkeyarrayWtoU( sortkey );
172     if (!sortkeyU) return WLDAP32_LDAP_NO_MEMORY;
173 
174     ret = map_error( ldap_create_sort_control( ld, sortkeyU, critical, &controlU ));
175 
176     *control = controlUtoW( controlU );
177     if (!*control) ret = WLDAP32_LDAP_NO_MEMORY;
178 
179     ldap_control_free( controlU );
180     sortkeyarrayfreeU( sortkeyU );
181 
182 #endif
183     return ret;
184 }
185 
186 /***********************************************************************
187  *      ldap_create_vlv_controlA     (WLDAP32.@)
188  *
189  * See ldap_create_vlv_controlW.
190  */
191 INT CDECL ldap_create_vlv_controlA( WLDAP32_LDAP *ld, WLDAP32_LDAPVLVInfo *info,
192     UCHAR critical, LDAPControlA **control )
193 {
194     INT ret = WLDAP32_LDAP_NOT_SUPPORTED;
195 #ifdef HAVE_LDAP
196     LDAPControlW *controlW = NULL;
197 
198     TRACE( "(%p, %p, 0x%02x, %p)\n", ld, info, critical, control );
199 
200     if (!ld || !control) return ~0u;
201 
202     ret = ldap_create_vlv_controlW( ld, info, critical, &controlW );
203 
204     if (ret == WLDAP32_LDAP_SUCCESS)
205     {
206         *control = controlWtoA( controlW );
207         if (!*control) ret = WLDAP32_LDAP_NO_MEMORY;
208         ldap_control_freeW( controlW );
209     }
210 
211 #endif
212     return ret;
213 }
214 
215 /***********************************************************************
216  *      ldap_create_vlv_controlW     (WLDAP32.@)
217  *
218  * Create a virtual list view control.
219  *
220  * PARAMS
221  *  ld       [I] Pointer to an LDAP context.
222  *  info     [I] LDAPVLVInfo structure specifying a list view window.
223  *  critical [I] Tells the server this control is critical to the
224  *               search operation.
225  *  control  [O] LDAPControl created.
226  *
227  * RETURNS
228  *  Success: LDAP_SUCCESS
229  *  Failure: An LDAP error code.
230  *
231  * NOTES
232  *  Pass the created control in conjunction with a sort control as
233  *  server controls in subsequent calls to ldap_search_ext(_s). The
234  *  server will then return a sorted, contiguous subset of results
235  *  that meets the criteria specified in the LDAPVLVInfo structure.
236  */
237 INT CDECL ldap_create_vlv_controlW( WLDAP32_LDAP *ld, WLDAP32_LDAPVLVInfo *info,
238     UCHAR critical, LDAPControlW **control )
239 {
240     INT ret = WLDAP32_LDAP_NOT_SUPPORTED;
241 #ifdef HAVE_LDAP
242     LDAPControl *controlU = NULL;
243 
244     TRACE( "(%p, %p, 0x%02x, %p)\n", ld, info, critical, control );
245 
246     if (!ld || !control) return ~0u;
247 
248     ret = map_error( ldap_create_vlv_control( ld, (LDAPVLVInfo *)info, &controlU ));
249 
250     if (ret == WLDAP32_LDAP_SUCCESS)
251     {
252         *control = controlUtoW( controlU );
253         if (!*control) ret = WLDAP32_LDAP_NO_MEMORY;
254         ldap_control_free( controlU );
255     }
256 
257 #endif
258     return ret;
259 }
260 
261 static inline void bv_val_dup( const struct WLDAP32_berval *src, struct WLDAP32_berval *dst )
262 {
263     dst->bv_val = HeapAlloc( GetProcessHeap(), 0, src->bv_len );
264     if (dst->bv_val)
265     {
266         memcpy( dst->bv_val, src->bv_val, src->bv_len );
267         dst->bv_len = src->bv_len;
268     }
269     else
270         dst->bv_len = 0;
271 }
272 
273 /***********************************************************************
274  *      ldap_encode_sort_controlA     (WLDAP32.@)
275  *
276  * See ldap_encode_sort_controlW.
277  */
278 ULONG CDECL ldap_encode_sort_controlA( WLDAP32_LDAP *ld, PLDAPSortKeyA *sortkeys,
279     PLDAPControlA ret, BOOLEAN critical )
280 {
281     LDAPControlA *control = NULL;
282     ULONG result;
283 
284     if ((result = ldap_create_sort_controlA( ld, sortkeys, critical, &control )) == WLDAP32_LDAP_SUCCESS)
285     {
286         ret->ldctl_oid = strdupU(control->ldctl_oid);
287         bv_val_dup( &control->ldctl_value, &ret->ldctl_value );
288         ret->ldctl_iscritical = control->ldctl_iscritical;
289         ldap_control_freeA( control );
290     }
291     return result;
292 }
293 
294 /***********************************************************************
295  *      ldap_encode_sort_controlW     (WLDAP32.@)
296  *
297  * Create a control for server sorted search results.
298  *
299  * PARAMS
300  *  ld       [I] Pointer to an LDAP context.
301  *  sortkey  [I] Array of LDAPSortKey structures, each specifying an
302  *               attribute to use as a sort key, a matching rule and
303  *               the sort order (ascending or descending).
304  *  critical [I] Tells the server this control is critical to the
305  *               search operation.
306  *  control  [O] LDAPControl created.
307  *
308  * RETURNS
309  *  Success: LDAP_SUCCESS
310  *  Failure: An LDAP error code.
311  *
312  * NOTES
313  *  This function is obsolete. Use its equivalent
314  *  ldap_create_sort_control instead.
315  */
316 ULONG CDECL ldap_encode_sort_controlW( WLDAP32_LDAP *ld, PLDAPSortKeyW *sortkeys,
317     PLDAPControlW ret, BOOLEAN critical )
318 {
319     LDAPControlW *control = NULL;
320     ULONG result;
321 
322     if ((result = ldap_create_sort_controlW( ld, sortkeys, critical, &control )) == WLDAP32_LDAP_SUCCESS)
323     {
324         ret->ldctl_oid = strdupW(control->ldctl_oid);
325         bv_val_dup( &control->ldctl_value, &ret->ldctl_value );
326         ret->ldctl_iscritical = control->ldctl_iscritical;
327         ldap_control_freeW( control );
328     }
329     return result;
330 }
331 
332 /***********************************************************************
333  *      ldap_free_controlsA     (WLDAP32.@)
334  *
335  * See ldap_free_controlsW.
336  */
337 ULONG CDECL ldap_free_controlsA( LDAPControlA **controls )
338 {
339     return ldap_controls_freeA( controls );
340 }
341 
342 /***********************************************************************
343  *      ldap_free_controlsW     (WLDAP32.@)
344  *
345  * Free an array of LDAPControl structures.
346  *
347  * PARAMS
348  *  controls  [I] Array of LDAPControl structures to free.
349  *
350  * RETURNS
351  *  LDAP_SUCCESS
352  *
353  * NOTES
354  *  Obsolete, use ldap_controls_freeW.
355  */
356 ULONG CDECL ldap_free_controlsW( LDAPControlW **controls )
357 {
358     return ldap_controls_freeW( controls );
359 }
360