xref: /reactos/dll/win32/wldap32/misc.c (revision e6368408)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * WLDAP32 - LDAP support for Wine
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2005 Hans Leidekker
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
21*e6368408SAmine Khaldi #include "config.h"
22*e6368408SAmine Khaldi #include "wine/port.h"
23c2c66affSColin Finck 
24*e6368408SAmine Khaldi #include <stdarg.h>
25c2c66affSColin Finck #include <stdio.h>
26*e6368408SAmine Khaldi #ifdef HAVE_LDAP_H
27*e6368408SAmine Khaldi #include <ldap.h>
28*e6368408SAmine Khaldi #endif
29*e6368408SAmine Khaldi 
30*e6368408SAmine Khaldi #include "windef.h"
31*e6368408SAmine Khaldi #include "winbase.h"
32*e6368408SAmine Khaldi #include "winnls.h"
33*e6368408SAmine Khaldi 
34*e6368408SAmine Khaldi #include "winldap_private.h"
35*e6368408SAmine Khaldi #include "wldap32.h"
36*e6368408SAmine Khaldi #include "wine/debug.h"
37*e6368408SAmine Khaldi 
38*e6368408SAmine Khaldi WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
39c2c66affSColin Finck 
40c2c66affSColin Finck /***********************************************************************
41c2c66affSColin Finck  *      ldap_abandon     (WLDAP32.@)
42c2c66affSColin Finck  *
43c2c66affSColin Finck  * Cancel an asynchronous operation.
44c2c66affSColin Finck  *
45c2c66affSColin Finck  * PARAMS
46c2c66affSColin Finck  *  ld    [I] Pointer to an LDAP context.
47c2c66affSColin Finck  *  msgid [I] ID of the operation to cancel.
48c2c66affSColin Finck  *
49c2c66affSColin Finck  * RETURNS
50c2c66affSColin Finck  *  Success: LDAP_SUCCESS
51c2c66affSColin Finck  *  Failure: An LDAP error code.
52c2c66affSColin Finck  */
WLDAP32_ldap_abandon(WLDAP32_LDAP * ld,ULONG msgid)53c2c66affSColin Finck ULONG CDECL WLDAP32_ldap_abandon( WLDAP32_LDAP *ld, ULONG msgid )
54c2c66affSColin Finck {
55c2c66affSColin Finck     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
56c2c66affSColin Finck #ifdef HAVE_LDAP
57c2c66affSColin Finck 
58c2c66affSColin Finck     TRACE( "(%p, 0x%08x)\n", ld, msgid );
59c2c66affSColin Finck 
60c2c66affSColin Finck     if (!ld) return ~0u;
61c2c66affSColin Finck     ret = map_error( ldap_abandon_ext( ld, msgid, NULL, NULL ));
62c2c66affSColin Finck 
63c2c66affSColin Finck #endif
64c2c66affSColin Finck     return ret;
65c2c66affSColin Finck }
66c2c66affSColin Finck 
67c2c66affSColin Finck /***********************************************************************
68c2c66affSColin Finck  *      ldap_check_filterA     (WLDAP32.@)
69c2c66affSColin Finck  *
70c2c66affSColin Finck  * See ldap_check_filterW.
71c2c66affSColin Finck  */
ldap_check_filterA(WLDAP32_LDAP * ld,PCHAR filter)72c2c66affSColin Finck ULONG CDECL ldap_check_filterA( WLDAP32_LDAP *ld, PCHAR filter )
73c2c66affSColin Finck {
74c2c66affSColin Finck     ULONG ret;
75c2c66affSColin Finck     WCHAR *filterW = NULL;
76c2c66affSColin Finck 
77c2c66affSColin Finck     TRACE( "(%p, %s)\n", ld, debugstr_a(filter) );
78c2c66affSColin Finck 
79c2c66affSColin Finck     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
80c2c66affSColin Finck 
81c2c66affSColin Finck     if (filter) {
82c2c66affSColin Finck         filterW = strAtoW( filter );
83c2c66affSColin Finck         if (!filterW) return WLDAP32_LDAP_NO_MEMORY;
84c2c66affSColin Finck     }
85c2c66affSColin Finck 
86c2c66affSColin Finck     ret = ldap_check_filterW( ld, filterW );
87c2c66affSColin Finck 
88c2c66affSColin Finck     strfreeW( filterW );
89c2c66affSColin Finck     return ret;
90c2c66affSColin Finck }
91c2c66affSColin Finck 
92c2c66affSColin Finck /***********************************************************************
93c2c66affSColin Finck  *      ldap_check_filterW     (WLDAP32.@)
94c2c66affSColin Finck  *
95c2c66affSColin Finck  * Check filter syntax.
96c2c66affSColin Finck  *
97c2c66affSColin Finck  * PARAMS
98c2c66affSColin Finck  *  ld     [I] Pointer to an LDAP context.
99c2c66affSColin Finck  *  filter [I] Filter string.
100c2c66affSColin Finck  *
101c2c66affSColin Finck  * RETURNS
102c2c66affSColin Finck  *  Success: LDAP_SUCCESS
103c2c66affSColin Finck  *  Failure: An LDAP error code.
104c2c66affSColin Finck  */
ldap_check_filterW(WLDAP32_LDAP * ld,PWCHAR filter)105c2c66affSColin Finck ULONG CDECL ldap_check_filterW( WLDAP32_LDAP *ld, PWCHAR filter )
106c2c66affSColin Finck {
107c2c66affSColin Finck     TRACE( "(%p, %s)\n", ld, debugstr_w(filter) );
108c2c66affSColin Finck 
109c2c66affSColin Finck     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
110c2c66affSColin Finck     return WLDAP32_LDAP_SUCCESS; /* FIXME: do some checks */
111c2c66affSColin Finck }
112c2c66affSColin Finck 
113c2c66affSColin Finck /***********************************************************************
114c2c66affSColin Finck  *      ldap_cleanup     (WLDAP32.@)
115c2c66affSColin Finck  */
ldap_cleanup(HANDLE instance)116c2c66affSColin Finck ULONG CDECL ldap_cleanup( HANDLE instance )
117c2c66affSColin Finck {
118c2c66affSColin Finck     TRACE( "(%p)\n", instance );
119c2c66affSColin Finck     return WLDAP32_LDAP_SUCCESS;
120c2c66affSColin Finck }
121c2c66affSColin Finck 
122c2c66affSColin Finck /***********************************************************************
123c2c66affSColin Finck  *      ldap_conn_from_msg     (WLDAP32.@)
124c2c66affSColin Finck  *
125c2c66affSColin Finck  * Get the LDAP context for a given message.
126c2c66affSColin Finck  *
127c2c66affSColin Finck  * PARAMS
128c2c66affSColin Finck  *  ld  [I] Pointer to an LDAP context.
129c2c66affSColin Finck  *  res [I] LDAP message.
130c2c66affSColin Finck  *
131c2c66affSColin Finck  * RETURNS
132c2c66affSColin Finck  *  Success: Pointer to an LDAP context.
133c2c66affSColin Finck  *  Failure: NULL
134c2c66affSColin Finck  */
ldap_conn_from_msg(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * res)135c2c66affSColin Finck WLDAP32_LDAP * CDECL ldap_conn_from_msg( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
136c2c66affSColin Finck {
137c2c66affSColin Finck     TRACE( "(%p, %p)\n", ld, res );
138c2c66affSColin Finck 
139c2c66affSColin Finck     if (!ld || !res) return NULL;
140c2c66affSColin Finck     return ld; /* FIXME: not always correct */
141c2c66affSColin Finck }
142c2c66affSColin Finck 
143c2c66affSColin Finck /***********************************************************************
144c2c66affSColin Finck  *      ldap_count_entries     (WLDAP32.@)
145c2c66affSColin Finck  *
146c2c66affSColin Finck  * Count the number of entries returned from a search.
147c2c66affSColin Finck  *
148c2c66affSColin Finck  * PARAMS
149c2c66affSColin Finck  *  ld  [I] Pointer to an LDAP context.
150c2c66affSColin Finck  *  res [I] LDAP message.
151c2c66affSColin Finck  *
152c2c66affSColin Finck  * RETURNS
153c2c66affSColin Finck  *  Success: The number of entries.
154c2c66affSColin Finck  *  Failure: ~0u
155c2c66affSColin Finck  */
WLDAP32_ldap_count_entries(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * res)156c2c66affSColin Finck ULONG CDECL WLDAP32_ldap_count_entries( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
157c2c66affSColin Finck {
158c2c66affSColin Finck     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
159c2c66affSColin Finck #ifdef HAVE_LDAP
160c2c66affSColin Finck 
161c2c66affSColin Finck     TRACE( "(%p, %p)\n", ld, res );
162c2c66affSColin Finck 
163c2c66affSColin Finck     if (!ld) return ~0u;
164c2c66affSColin Finck     ret = ldap_count_entries( ld, res );
165c2c66affSColin Finck 
166c2c66affSColin Finck #endif
167c2c66affSColin Finck     return ret;
168c2c66affSColin Finck }
169c2c66affSColin Finck 
170c2c66affSColin Finck /***********************************************************************
171c2c66affSColin Finck  *      ldap_count_references     (WLDAP32.@)
172c2c66affSColin Finck  *
173c2c66affSColin Finck  * Count the number of references returned from a search.
174c2c66affSColin Finck  *
175c2c66affSColin Finck  * PARAMS
176c2c66affSColin Finck  *  ld  [I] Pointer to an LDAP context.
177c2c66affSColin Finck  *  res [I] LDAP message.
178c2c66affSColin Finck  *
179c2c66affSColin Finck  * RETURNS
180c2c66affSColin Finck  *  Success: The number of references.
181c2c66affSColin Finck  *  Failure: ~0u
182c2c66affSColin Finck  */
WLDAP32_ldap_count_references(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * res)183c2c66affSColin Finck ULONG CDECL WLDAP32_ldap_count_references( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
184c2c66affSColin Finck {
185c2c66affSColin Finck     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
186c2c66affSColin Finck #ifdef HAVE_LDAP_COUNT_REFERENCES
187c2c66affSColin Finck 
188c2c66affSColin Finck     TRACE( "(%p, %p)\n", ld, res );
189c2c66affSColin Finck 
190c2c66affSColin Finck     if (!ld) return 0;
191c2c66affSColin Finck     ret = ldap_count_references( ld, res );
192c2c66affSColin Finck 
193c2c66affSColin Finck #endif
194c2c66affSColin Finck     return ret;
195c2c66affSColin Finck }
196c2c66affSColin Finck 
get_escape_size(PCHAR src,ULONG srclen)197c2c66affSColin Finck static ULONG get_escape_size( PCHAR src, ULONG srclen )
198c2c66affSColin Finck {
199c2c66affSColin Finck     ULONG i, size = 0;
200c2c66affSColin Finck 
201c2c66affSColin Finck     if (src)
202c2c66affSColin Finck     {
203c2c66affSColin Finck         for (i = 0; i < srclen; i++)
204c2c66affSColin Finck         {
205c2c66affSColin Finck             if ((src[i] >= '0' && src[i] <= '9') ||
206c2c66affSColin Finck                 (src[i] >= 'A' && src[i] <= 'Z') ||
207c2c66affSColin Finck                 (src[i] >= 'a' && src[i] <= 'z'))
208c2c66affSColin Finck                 size++;
209c2c66affSColin Finck             else
210c2c66affSColin Finck                 size += 3;
211c2c66affSColin Finck         }
212c2c66affSColin Finck     }
213c2c66affSColin Finck     return size + 1;
214c2c66affSColin Finck }
215c2c66affSColin Finck 
escape_filter_element(PCHAR src,ULONG srclen,PCHAR dst)216c2c66affSColin Finck static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst )
217c2c66affSColin Finck {
218c2c66affSColin Finck     ULONG i;
219c2c66affSColin Finck     static const char fmt[] = "\\%02X";
220c2c66affSColin Finck     char *d = dst;
221c2c66affSColin Finck 
222c2c66affSColin Finck     for (i = 0; i < srclen; i++)
223c2c66affSColin Finck     {
224c2c66affSColin Finck         if ((src[i] >= '0' && src[i] <= '9') ||
225c2c66affSColin Finck             (src[i] >= 'A' && src[i] <= 'Z') ||
226c2c66affSColin Finck             (src[i] >= 'a' && src[i] <= 'z'))
227c2c66affSColin Finck             *d++ = src[i];
228c2c66affSColin Finck         else
229c2c66affSColin Finck             d += sprintf( d, fmt, (unsigned char)src[i] );
230c2c66affSColin Finck     }
231c2c66affSColin Finck     *++d = 0;
232c2c66affSColin Finck }
233c2c66affSColin Finck 
234c2c66affSColin Finck /***********************************************************************
235c2c66affSColin Finck  *      ldap_escape_filter_elementA     (WLDAP32.@)
236c2c66affSColin Finck  *
237c2c66affSColin Finck  * See ldap_escape_filter_elementW.
238c2c66affSColin Finck  */
ldap_escape_filter_elementA(PCHAR src,ULONG srclen,PCHAR dst,ULONG dstlen)239c2c66affSColin Finck ULONG CDECL ldap_escape_filter_elementA( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen )
240c2c66affSColin Finck {
241c2c66affSColin Finck     ULONG len;
242c2c66affSColin Finck 
243c2c66affSColin Finck     TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
244c2c66affSColin Finck 
245c2c66affSColin Finck     len = get_escape_size( src, srclen );
246c2c66affSColin Finck     if (!dst) return len;
247c2c66affSColin Finck 
248c2c66affSColin Finck     if (!src || dstlen < len)
249c2c66affSColin Finck         return WLDAP32_LDAP_PARAM_ERROR;
250c2c66affSColin Finck     else
251c2c66affSColin Finck     {
252c2c66affSColin Finck         escape_filter_element( src, srclen, dst );
253c2c66affSColin Finck         return WLDAP32_LDAP_SUCCESS;
254c2c66affSColin Finck     }
255c2c66affSColin Finck }
256c2c66affSColin Finck 
257c2c66affSColin Finck /***********************************************************************
258c2c66affSColin Finck  *      ldap_escape_filter_elementW     (WLDAP32.@)
259c2c66affSColin Finck  *
260c2c66affSColin Finck  * Escape binary data for safe passing in filters.
261c2c66affSColin Finck  *
262c2c66affSColin Finck  * PARAMS
263c2c66affSColin Finck  *  src    [I] Filter element to be escaped.
264c2c66affSColin Finck  *  srclen [I] Length in bytes of the filter element.
265c2c66affSColin Finck  *  dst    [O] Destination buffer for the escaped filter element.
266c2c66affSColin Finck  *  dstlen [I] Length in bytes of the destination buffer.
267c2c66affSColin Finck  *
268c2c66affSColin Finck  * RETURNS
269c2c66affSColin Finck  *  Success: LDAP_SUCCESS
270c2c66affSColin Finck  *  Failure: An LDAP error code.
271c2c66affSColin Finck  */
ldap_escape_filter_elementW(PCHAR src,ULONG srclen,PWCHAR dst,ULONG dstlen)272c2c66affSColin Finck ULONG CDECL ldap_escape_filter_elementW( PCHAR src, ULONG srclen, PWCHAR dst, ULONG dstlen )
273c2c66affSColin Finck {
274c2c66affSColin Finck     ULONG len;
275c2c66affSColin Finck 
276c2c66affSColin Finck     TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
277c2c66affSColin Finck 
278c2c66affSColin Finck     len = get_escape_size( src, srclen );
279c2c66affSColin Finck     if (!dst) return len;
280c2c66affSColin Finck 
281c2c66affSColin Finck     /* no matter what you throw at it, this is what native returns */
282c2c66affSColin Finck     return WLDAP32_LDAP_PARAM_ERROR;
283c2c66affSColin Finck }
284c2c66affSColin Finck 
285c2c66affSColin Finck /***********************************************************************
286c2c66affSColin Finck  *      ldap_first_attributeA     (WLDAP32.@)
287c2c66affSColin Finck  *
288c2c66affSColin Finck  * See ldap_first_attributeW.
289c2c66affSColin Finck  */
ldap_first_attributeA(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * entry,WLDAP32_BerElement ** ptr)290c2c66affSColin Finck PCHAR CDECL ldap_first_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
291c2c66affSColin Finck     WLDAP32_BerElement** ptr )
292c2c66affSColin Finck {
293c2c66affSColin Finck     PCHAR ret = NULL;
294c2c66affSColin Finck #ifdef HAVE_LDAP
295c2c66affSColin Finck     WCHAR *retW;
296c2c66affSColin Finck 
297c2c66affSColin Finck     TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
298c2c66affSColin Finck 
299c2c66affSColin Finck     if (!ld || !entry) return NULL;
300c2c66affSColin Finck     retW = ldap_first_attributeW( ld, entry, ptr );
301c2c66affSColin Finck 
302c2c66affSColin Finck     ret = strWtoA( retW );
303c2c66affSColin Finck     ldap_memfreeW( retW );
304c2c66affSColin Finck 
305c2c66affSColin Finck #endif
306c2c66affSColin Finck     return ret;
307c2c66affSColin Finck }
308c2c66affSColin Finck 
309c2c66affSColin Finck /***********************************************************************
310c2c66affSColin Finck  *      ldap_first_attributeW     (WLDAP32.@)
311c2c66affSColin Finck  *
312c2c66affSColin Finck  * Get the first attribute for a given entry.
313c2c66affSColin Finck  *
314c2c66affSColin Finck  * PARAMS
315c2c66affSColin Finck  *  ld    [I] Pointer to an LDAP context.
316c2c66affSColin Finck  *  entry [I] Entry to retrieve attribute for.
317c2c66affSColin Finck  *  ptr   [O] Position pointer.
318c2c66affSColin Finck  *
319c2c66affSColin Finck  * RETURNS
320c2c66affSColin Finck  *  Success: Name of the first attribute.
321c2c66affSColin Finck  *  Failure: NULL
322c2c66affSColin Finck  *
323c2c66affSColin Finck  * NOTES
324c2c66affSColin Finck  *  Use ldap_memfree to free the returned string.
325c2c66affSColin Finck  */
ldap_first_attributeW(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * entry,WLDAP32_BerElement ** ptr)326c2c66affSColin Finck PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
327c2c66affSColin Finck     WLDAP32_BerElement** ptr )
328c2c66affSColin Finck {
329c2c66affSColin Finck     PWCHAR ret = NULL;
330c2c66affSColin Finck #ifdef HAVE_LDAP
331c2c66affSColin Finck     char *retU;
332c2c66affSColin Finck 
333c2c66affSColin Finck     TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
334c2c66affSColin Finck 
335c2c66affSColin Finck     if (!ld || !entry) return NULL;
336c2c66affSColin Finck     retU = ldap_first_attribute( ld, entry, ptr );
337c2c66affSColin Finck 
338c2c66affSColin Finck     ret = strUtoW( retU );
339c2c66affSColin Finck     ldap_memfree( retU );
340c2c66affSColin Finck 
341c2c66affSColin Finck #endif
342c2c66affSColin Finck     return ret;
343c2c66affSColin Finck }
344c2c66affSColin Finck 
345c2c66affSColin Finck /***********************************************************************
346c2c66affSColin Finck  *      ldap_first_entry     (WLDAP32.@)
347c2c66affSColin Finck  *
348c2c66affSColin Finck  * Get the first entry from a result message.
349c2c66affSColin Finck  *
350c2c66affSColin Finck  * PARAMS
351c2c66affSColin Finck  *  ld  [I] Pointer to an LDAP context.
352c2c66affSColin Finck  *  res [I] Search result message.
353c2c66affSColin Finck  *
354c2c66affSColin Finck  * RETURNS
355c2c66affSColin Finck  *  Success: The first entry.
356c2c66affSColin Finck  *  Failure: NULL
357c2c66affSColin Finck  *
358c2c66affSColin Finck  * NOTES
359c2c66affSColin Finck  *  The returned entry will be freed when the message is freed.
360c2c66affSColin Finck  */
WLDAP32_ldap_first_entry(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * res)361c2c66affSColin Finck WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
362c2c66affSColin Finck {
363c2c66affSColin Finck #ifdef HAVE_LDAP
364c2c66affSColin Finck 
365c2c66affSColin Finck     TRACE( "(%p, %p)\n", ld, res );
366c2c66affSColin Finck 
367c2c66affSColin Finck     if (!ld || !res) return NULL;
368c2c66affSColin Finck     return ldap_first_entry( ld, res );
369c2c66affSColin Finck 
370c2c66affSColin Finck #else
371c2c66affSColin Finck     return NULL;
372c2c66affSColin Finck #endif
373c2c66affSColin Finck }
374c2c66affSColin Finck 
375c2c66affSColin Finck /***********************************************************************
376c2c66affSColin Finck  *      ldap_first_reference     (WLDAP32.@)
377c2c66affSColin Finck  *
378c2c66affSColin Finck  * Get the first reference from a result message.
379c2c66affSColin Finck  *
380c2c66affSColin Finck  * PARAMS
381c2c66affSColin Finck  *  ld  [I] Pointer to an LDAP context.
382c2c66affSColin Finck  *  res [I] Search result message.
383c2c66affSColin Finck  *
384c2c66affSColin Finck  * RETURNS
385c2c66affSColin Finck  *  Success: The first reference.
386c2c66affSColin Finck  *  Failure: NULL
387c2c66affSColin Finck  */
WLDAP32_ldap_first_reference(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * res)388c2c66affSColin Finck WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
389c2c66affSColin Finck {
390c2c66affSColin Finck #ifdef HAVE_LDAP_FIRST_REFERENCE
391c2c66affSColin Finck 
392c2c66affSColin Finck     TRACE( "(%p, %p)\n", ld, res );
393c2c66affSColin Finck 
394c2c66affSColin Finck     if (!ld) return NULL;
395c2c66affSColin Finck     return ldap_first_reference( ld, res );
396c2c66affSColin Finck 
397c2c66affSColin Finck #else
398c2c66affSColin Finck     return NULL;
399c2c66affSColin Finck #endif
400c2c66affSColin Finck }
401c2c66affSColin Finck 
402c2c66affSColin Finck /***********************************************************************
403c2c66affSColin Finck  *      ldap_memfreeA     (WLDAP32.@)
404c2c66affSColin Finck  *
405c2c66affSColin Finck  * See ldap_memfreeW.
406c2c66affSColin Finck  */
ldap_memfreeA(PCHAR block)407c2c66affSColin Finck void CDECL ldap_memfreeA( PCHAR block )
408c2c66affSColin Finck {
409c2c66affSColin Finck     TRACE( "(%p)\n", block );
410c2c66affSColin Finck     strfreeA( block );
411c2c66affSColin Finck }
412c2c66affSColin Finck 
413c2c66affSColin Finck /***********************************************************************
414c2c66affSColin Finck  *      ldap_memfreeW     (WLDAP32.@)
415c2c66affSColin Finck  *
416c2c66affSColin Finck  * Free a block of memory.
417c2c66affSColin Finck  *
418c2c66affSColin Finck  * PARAMS
419c2c66affSColin Finck  *  block [I] Pointer to memory block to be freed.
420c2c66affSColin Finck  */
ldap_memfreeW(PWCHAR block)421c2c66affSColin Finck void CDECL ldap_memfreeW( PWCHAR block )
422c2c66affSColin Finck {
423c2c66affSColin Finck     TRACE( "(%p)\n", block );
424c2c66affSColin Finck     strfreeW( block );
425c2c66affSColin Finck }
426c2c66affSColin Finck 
427c2c66affSColin Finck /***********************************************************************
428c2c66affSColin Finck  *      ldap_msgfree     (WLDAP32.@)
429c2c66affSColin Finck  *
430c2c66affSColin Finck  * Free a message.
431c2c66affSColin Finck  *
432c2c66affSColin Finck  * PARAMS
433c2c66affSColin Finck  *  res [I] Message to be freed.
434c2c66affSColin Finck  */
WLDAP32_ldap_msgfree(WLDAP32_LDAPMessage * res)435c2c66affSColin Finck ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res )
436c2c66affSColin Finck {
437c2c66affSColin Finck     ULONG ret = WLDAP32_LDAP_SUCCESS;
438c2c66affSColin Finck #ifdef HAVE_LDAP
439c2c66affSColin Finck 
440c2c66affSColin Finck     TRACE( "(%p)\n", res );
441c2c66affSColin Finck     ldap_msgfree( res );
442c2c66affSColin Finck 
443c2c66affSColin Finck #endif
444c2c66affSColin Finck     return ret;
445c2c66affSColin Finck }
446c2c66affSColin Finck 
447c2c66affSColin Finck /***********************************************************************
448c2c66affSColin Finck  *      ldap_next_attributeA     (WLDAP32.@)
449c2c66affSColin Finck  *
450c2c66affSColin Finck  * See ldap_next_attributeW.
451c2c66affSColin Finck  */
ldap_next_attributeA(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * entry,WLDAP32_BerElement * ptr)452c2c66affSColin Finck PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
453c2c66affSColin Finck     WLDAP32_BerElement *ptr )
454c2c66affSColin Finck {
455c2c66affSColin Finck     PCHAR ret = NULL;
456c2c66affSColin Finck #ifdef HAVE_LDAP
457c2c66affSColin Finck     WCHAR *retW;
458c2c66affSColin Finck 
459c2c66affSColin Finck     TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
460c2c66affSColin Finck 
461c2c66affSColin Finck     if (!ld || !entry || !ptr) return NULL;
462c2c66affSColin Finck     retW = ldap_next_attributeW( ld, entry, ptr );
463c2c66affSColin Finck 
464c2c66affSColin Finck     ret = strWtoA( retW );
465c2c66affSColin Finck     ldap_memfreeW( retW );
466c2c66affSColin Finck 
467c2c66affSColin Finck #endif
468c2c66affSColin Finck     return ret;
469c2c66affSColin Finck }
470c2c66affSColin Finck 
471c2c66affSColin Finck /***********************************************************************
472c2c66affSColin Finck  *      ldap_next_attributeW     (WLDAP32.@)
473c2c66affSColin Finck  *
474c2c66affSColin Finck  * Get the next attribute for a given entry.
475c2c66affSColin Finck  *
476c2c66affSColin Finck  * PARAMS
477c2c66affSColin Finck  *  ld    [I]   Pointer to an LDAP context.
478c2c66affSColin Finck  *  entry [I]   Entry to retrieve attribute for.
479c2c66affSColin Finck  *  ptr   [I/O] Position pointer.
480c2c66affSColin Finck  *
481c2c66affSColin Finck  * RETURNS
482c2c66affSColin Finck  *  Success: The name of the next attribute.
483c2c66affSColin Finck  *  Failure: NULL
484c2c66affSColin Finck  *
485c2c66affSColin Finck  * NOTES
486c2c66affSColin Finck  *  Free the returned string after each iteration with ldap_memfree.
487c2c66affSColin Finck  *  When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
488c2c66affSColin Finck  */
ldap_next_attributeW(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * entry,WLDAP32_BerElement * ptr)489c2c66affSColin Finck PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
490c2c66affSColin Finck     WLDAP32_BerElement *ptr )
491c2c66affSColin Finck {
492c2c66affSColin Finck     PWCHAR ret = NULL;
493c2c66affSColin Finck #ifdef HAVE_LDAP
494c2c66affSColin Finck     char *retU;
495c2c66affSColin Finck 
496c2c66affSColin Finck     TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
497c2c66affSColin Finck 
498c2c66affSColin Finck     if (!ld || !entry || !ptr) return NULL;
499c2c66affSColin Finck     retU = ldap_next_attribute( ld, entry, ptr );
500c2c66affSColin Finck 
501c2c66affSColin Finck     ret = strUtoW( retU );
502c2c66affSColin Finck     ldap_memfree( retU );
503c2c66affSColin Finck 
504c2c66affSColin Finck #endif
505c2c66affSColin Finck     return ret;
506c2c66affSColin Finck }
507c2c66affSColin Finck 
508c2c66affSColin Finck /***********************************************************************
509c2c66affSColin Finck  *      ldap_next_entry     (WLDAP32.@)
510c2c66affSColin Finck  *
511c2c66affSColin Finck  * Get the next entry from a result message.
512c2c66affSColin Finck  *
513c2c66affSColin Finck  * PARAMS
514c2c66affSColin Finck  *  ld    [I] Pointer to an LDAP context.
515c2c66affSColin Finck  *  entry [I] Entry returned by a previous call.
516c2c66affSColin Finck  *
517c2c66affSColin Finck  * RETURNS
518c2c66affSColin Finck  *  Success: The next entry.
519c2c66affSColin Finck  *  Failure: NULL
520c2c66affSColin Finck  *
521c2c66affSColin Finck  * NOTES
522c2c66affSColin Finck  *  The returned entry will be freed when the message is freed.
523c2c66affSColin Finck  */
WLDAP32_ldap_next_entry(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * entry)524c2c66affSColin Finck WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
525c2c66affSColin Finck {
526c2c66affSColin Finck #ifdef HAVE_LDAP
527c2c66affSColin Finck 
528c2c66affSColin Finck     TRACE( "(%p, %p)\n", ld, entry );
529c2c66affSColin Finck 
530c2c66affSColin Finck     if (!ld || !entry) return NULL;
531c2c66affSColin Finck     return ldap_next_entry( ld, entry );
532c2c66affSColin Finck 
533c2c66affSColin Finck #else
534c2c66affSColin Finck     return NULL;
535c2c66affSColin Finck #endif
536c2c66affSColin Finck }
537c2c66affSColin Finck 
538c2c66affSColin Finck /***********************************************************************
539c2c66affSColin Finck  *      ldap_next_reference     (WLDAP32.@)
540c2c66affSColin Finck  *
541c2c66affSColin Finck  * Get the next reference from a result message.
542c2c66affSColin Finck  *
543c2c66affSColin Finck  * PARAMS
544c2c66affSColin Finck  *  ld    [I] Pointer to an LDAP context.
545c2c66affSColin Finck  *  entry [I] Entry returned by a previous call.
546c2c66affSColin Finck  *
547c2c66affSColin Finck  * RETURNS
548c2c66affSColin Finck  *  Success: The next reference.
549c2c66affSColin Finck  *  Failure: NULL
550c2c66affSColin Finck  *
551c2c66affSColin Finck  * NOTES
552c2c66affSColin Finck  *  The returned entry will be freed when the message is freed.
553c2c66affSColin Finck  */
WLDAP32_ldap_next_reference(WLDAP32_LDAP * ld,WLDAP32_LDAPMessage * entry)554c2c66affSColin Finck WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
555c2c66affSColin Finck {
556c2c66affSColin Finck #ifdef HAVE_LDAP_NEXT_REFERENCE
557c2c66affSColin Finck 
558c2c66affSColin Finck     TRACE( "(%p, %p)\n", ld, entry );
559c2c66affSColin Finck 
560c2c66affSColin Finck     if (!ld || !entry) return NULL;
561c2c66affSColin Finck     return ldap_next_reference( ld, entry );
562c2c66affSColin Finck 
563c2c66affSColin Finck #else
564c2c66affSColin Finck     return NULL;
565c2c66affSColin Finck #endif
566c2c66affSColin Finck }
567c2c66affSColin Finck 
568c2c66affSColin Finck /***********************************************************************
569c2c66affSColin Finck  *      ldap_result     (WLDAP32.@)
570c2c66affSColin Finck  *
571c2c66affSColin Finck  * Get the result of an asynchronous operation.
572c2c66affSColin Finck  *
573c2c66affSColin Finck  * PARAMS
574c2c66affSColin Finck  *  ld      [I] Pointer to an LDAP context.
575c2c66affSColin Finck  *  msgid   [I] Message ID of the operation.
576c2c66affSColin Finck  *  all     [I] How many results should be returned?
577c2c66affSColin Finck  *  timeout [I] How long to wait for the results?
578c2c66affSColin Finck  *  res     [O] Result message for the operation.
579c2c66affSColin Finck  *
580c2c66affSColin Finck  * RETURNS
581c2c66affSColin Finck  *  Success: One of the following values:
582c2c66affSColin Finck  *
583c2c66affSColin Finck  *   LDAP_RES_ADD
584c2c66affSColin Finck  *   LDAP_RES_BIND
585c2c66affSColin Finck  *   LDAP_RES_COMPARE
586c2c66affSColin Finck  *   LDAP_RES_DELETE
587c2c66affSColin Finck  *   LDAP_RES_EXTENDED
588c2c66affSColin Finck  *   LDAP_RES_MODIFY
589c2c66affSColin Finck  *   LDAP_RES_MODRDN
590c2c66affSColin Finck  *   LDAP_RES_REFERRAL
591c2c66affSColin Finck  *   LDAP_RES_SEARCH_ENTRY
592c2c66affSColin Finck  *   LDAP_RES_SEARCH_RESULT
593c2c66affSColin Finck  *
594c2c66affSColin Finck  *  Failure: ~0u
595c2c66affSColin Finck  *
596c2c66affSColin Finck  *  This function returns 0 when the timeout has expired.
597c2c66affSColin Finck  *
598c2c66affSColin Finck  * NOTES
599c2c66affSColin Finck  *  A NULL timeout pointer causes the function to block waiting
600c2c66affSColin Finck  *  for results to arrive. A timeout value of 0 causes the function
601c2c66affSColin Finck  *  to immediately return any available results. Free returned results
602c2c66affSColin Finck  *  with ldap_msgfree.
603c2c66affSColin Finck  */
WLDAP32_ldap_result(WLDAP32_LDAP * ld,ULONG msgid,ULONG all,struct l_timeval * timeout,WLDAP32_LDAPMessage ** res)604c2c66affSColin Finck ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all,
605c2c66affSColin Finck     struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
606c2c66affSColin Finck {
607c2c66affSColin Finck     ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
608c2c66affSColin Finck #ifdef HAVE_LDAP
609c2c66affSColin Finck 
610c2c66affSColin Finck     TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld, msgid, all, timeout, res );
611c2c66affSColin Finck 
612c2c66affSColin Finck     if (!ld || !res || msgid == ~0u) return ~0u;
613c2c66affSColin Finck     ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res );
614c2c66affSColin Finck 
615c2c66affSColin Finck #endif
616c2c66affSColin Finck     return ret;
617c2c66affSColin Finck }
618c2c66affSColin Finck 
619c2c66affSColin Finck /***********************************************************************
620c2c66affSColin Finck  *      LdapUnicodeToUTF8     (WLDAP32.@)
621c2c66affSColin Finck  *
622c2c66affSColin Finck  * Convert a wide character string to a UTF8 string.
623c2c66affSColin Finck  *
624c2c66affSColin Finck  * PARAMS
625c2c66affSColin Finck  *  src    [I] Wide character string to convert.
626c2c66affSColin Finck  *  srclen [I] Size of string to convert, in characters.
627c2c66affSColin Finck  *  dst    [O] Pointer to a buffer that receives the converted string.
628c2c66affSColin Finck  *  dstlen [I] Size of the destination buffer in characters.
629c2c66affSColin Finck  *
630c2c66affSColin Finck  * RETURNS
631c2c66affSColin Finck  *  The number of characters written into the destination buffer.
632c2c66affSColin Finck  *
633c2c66affSColin Finck  * NOTES
634c2c66affSColin Finck  *  Set dstlen to zero to ask for the required buffer size.
635c2c66affSColin Finck  */
LdapUnicodeToUTF8(LPCWSTR src,int srclen,LPSTR dst,int dstlen)636c2c66affSColin Finck int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen )
637c2c66affSColin Finck {
638c2c66affSColin Finck     return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL );
639c2c66affSColin Finck }
640c2c66affSColin Finck 
641c2c66affSColin Finck /***********************************************************************
642c2c66affSColin Finck  *      LdapUTF8ToUnicode     (WLDAP32.@)
643c2c66affSColin Finck  *
644c2c66affSColin Finck  * Convert a UTF8 string to a wide character string.
645c2c66affSColin Finck  *
646c2c66affSColin Finck  * PARAMS
647c2c66affSColin Finck  *  src    [I] UTF8 string to convert.
648c2c66affSColin Finck  *  srclen [I] Size of string to convert, in characters.
649c2c66affSColin Finck  *  dst    [O] Pointer to a buffer that receives the converted string.
650c2c66affSColin Finck  *  dstlen [I] Size of the destination buffer in characters.
651c2c66affSColin Finck  *
652c2c66affSColin Finck  * RETURNS
653c2c66affSColin Finck  *  The number of characters written into the destination buffer.
654c2c66affSColin Finck  *
655c2c66affSColin Finck  * NOTES
656c2c66affSColin Finck  *  Set dstlen to zero to ask for the required buffer size.
657c2c66affSColin Finck  */
LdapUTF8ToUnicode(LPCSTR src,int srclen,LPWSTR dst,int dstlen)658c2c66affSColin Finck int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen )
659c2c66affSColin Finck {
660c2c66affSColin Finck     return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen );
661c2c66affSColin Finck }
662