2 * WLDAP32 - LDAP support for Wine
4 * Copyright 2005 Hans Leidekker
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.
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.
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
23 #include "wine/port.h"
24 #include "wine/debug.h"
36 #define LDAP_SUCCESS 0x00
37 #define LDAP_NOT_SUPPORTED 0x5c
40 #include "winldap_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
45 /***********************************************************************
46 * ldap_abandon (WLDAP32.@)
48 * Cancel an asynchronous operation.
51 * ld [I] Pointer to an LDAP context.
52 * msgid [I] ID of the operation to cancel.
55 * Success: LDAP_SUCCESS
56 * Failure: An LDAP error code.
58 ULONG CDECL WLDAP32_ldap_abandon( WLDAP32_LDAP *ld, ULONG msgid )
60 ULONG ret = LDAP_NOT_SUPPORTED;
63 TRACE( "(%p, 0x%08lx)\n", ld, msgid );
66 ret = ldap_abandon_ext( ld, msgid, NULL, NULL );
72 /***********************************************************************
73 * ldap_check_filterA (WLDAP32.@)
75 * See ldap_check_filterW.
77 ULONG CDECL ldap_check_filterA( WLDAP32_LDAP *ld, PCHAR filter )
80 WCHAR *filterW = NULL;
82 TRACE( "(%p, %s)\n", ld, debugstr_a(filter) );
84 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
87 filterW = strAtoW( filter );
88 if (!filterW) return WLDAP32_LDAP_NO_MEMORY;
91 ret = ldap_check_filterW( ld, filterW );
97 /***********************************************************************
98 * ldap_check_filterW (WLDAP32.@)
100 * Check filter syntax.
103 * ld [I] Pointer to an LDAP context.
104 * filter [I] Filter string.
107 * Success: LDAP_SUCCESS
108 * Failure: An LDAP error code.
110 ULONG CDECL ldap_check_filterW( WLDAP32_LDAP *ld, PWCHAR filter )
112 TRACE( "(%p, %s)\n", ld, debugstr_w(filter) );
114 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
115 return LDAP_SUCCESS; /* FIXME: do some checks */
118 /***********************************************************************
119 * ldap_cleanup (WLDAP32.@)
121 ULONG CDECL ldap_cleanup( HANDLE instance )
123 TRACE( "(%p)\n", instance );
127 /***********************************************************************
128 * ldap_conn_from_msg (WLDAP32.@)
130 * Get the LDAP context for a given message.
133 * ld [I] Pointer to an LDAP context.
134 * res [I] LDAP message.
137 * Success: Pointer to an LDAP context.
140 WLDAP32_LDAP * CDECL ldap_conn_from_msg( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
142 TRACE( "(%p, %p)\n", ld, res );
144 if (!ld || !res) return NULL;
145 return ld; /* FIXME: not always correct */
148 /***********************************************************************
149 * ldap_count_entries (WLDAP32.@)
151 * Count the number of entries returned from a search.
154 * ld [I] Pointer to an LDAP context.
155 * res [I] LDAP message.
158 * Success: The number of entries.
161 ULONG CDECL WLDAP32_ldap_count_entries( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
163 ULONG ret = LDAP_NOT_SUPPORTED;
166 TRACE( "(%p, %p)\n", ld, res );
168 if (!ld) return ~0UL;
169 ret = ldap_count_entries( ld, res );
175 /***********************************************************************
176 * ldap_count_references (WLDAP32.@)
178 * Count the number of references returned from a search.
181 * ld [I] Pointer to an LDAP context.
182 * res [I] LDAP message.
185 * Success: The number of references.
188 ULONG CDECL WLDAP32_ldap_count_references( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
190 ULONG ret = LDAP_NOT_SUPPORTED;
191 #ifdef HAVE_LDAP_COUNT_REFERENCES
193 TRACE( "(%p, %p)\n", ld, res );
196 ret = ldap_count_references( ld, res );
202 static ULONG get_escape_size( PCHAR src, ULONG srclen )
208 for (i = 0; i < srclen; i++)
210 if ((src[i] >= '0' && src[i] <= '9') ||
211 (src[i] >= 'A' && src[i] <= 'Z') ||
212 (src[i] >= 'a' && src[i] <= 'z'))
221 static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst )
224 static const char fmt[] = "\\%02X";
227 for (i = 0; i < srclen; i++)
229 if ((src[i] >= '0' && src[i] <= '9') ||
230 (src[i] >= 'A' && src[i] <= 'Z') ||
231 (src[i] >= 'a' && src[i] <= 'z'))
235 sprintf( d, fmt, (unsigned char)src[i] );
242 /***********************************************************************
243 * ldap_escape_filter_elementA (WLDAP32.@)
245 * See ldap_escape_filter_elementW.
247 ULONG CDECL ldap_escape_filter_elementA( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen )
251 TRACE( "(%p, 0x%08lx, %p, 0x%08lx)\n", src, srclen, dst, dstlen );
253 len = get_escape_size( src, srclen );
254 if (!dst) return len;
256 if (!src || dstlen < len)
257 return WLDAP32_LDAP_PARAM_ERROR;
260 escape_filter_element( src, srclen, dst );
265 /***********************************************************************
266 * ldap_escape_filter_elementW (WLDAP32.@)
268 * Escape binary data for safe passing in filters.
271 * src [I] Filter element to be escaped.
272 * srclen [I] Length in bytes of the filter element.
273 * dst [O] Destination buffer for the escaped filter element.
274 * dstlen [I] Length in bytes of the destination buffer.
277 * Success: LDAP_SUCCESS
278 * Failure: An LDAP error code.
280 ULONG CDECL ldap_escape_filter_elementW( PCHAR src, ULONG srclen, PWCHAR dst, ULONG dstlen )
284 TRACE( "(%p, 0x%08lx, %p, 0x%08lx)\n", src, srclen, dst, dstlen );
286 len = get_escape_size( src, srclen );
287 if (!dst) return len;
289 /* no matter what you throw at it, this is what native returns */
290 return WLDAP32_LDAP_PARAM_ERROR;
293 /***********************************************************************
294 * ldap_first_attributeA (WLDAP32.@)
296 * See ldap_first_attributeW.
298 PCHAR CDECL ldap_first_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
299 WLDAP32_BerElement** ptr )
305 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
307 if (!ld || !entry) return NULL;
308 retW = ldap_first_attributeW( ld, entry, ptr );
310 ret = strWtoA( retW );
311 ldap_memfreeW( retW );
317 /***********************************************************************
318 * ldap_first_attributeW (WLDAP32.@)
320 * Get the first attribute for a given entry.
323 * ld [I] Pointer to an LDAP context.
324 * entry [I] Entry to retrieve attribute for.
325 * ptr [O] Position pointer.
328 * Success: Name of the first attribute.
332 * Use ldap_memfree to free the returned string.
334 PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
335 WLDAP32_BerElement** ptr )
341 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
343 if (!ld || !entry) return NULL;
344 retU = ldap_first_attribute( ld, entry, ptr );
346 ret = strUtoW( retU );
347 ldap_memfree( retU );
353 /***********************************************************************
354 * ldap_first_entry (WLDAP32.@)
356 * Get the first entry from a result message.
359 * ld [I] Pointer to an LDAP context.
360 * res [I] Search result message.
363 * Success: The first entry.
367 * The returned entry will be freed when the message is freed.
369 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
373 TRACE( "(%p, %p)\n", ld, res );
375 if (!ld || !res) return NULL;
376 return ldap_first_entry( ld, res );
382 /***********************************************************************
383 * ldap_first_reference (WLDAP32.@)
385 * Get the first reference from a result message.
388 * ld [I] Pointer to an LDAP context.
389 * res [I] Search result message.
392 * Success: The first reference.
395 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
397 #ifdef HAVE_LDAP_FIRST_REFERENCE
399 TRACE( "(%p, %p)\n", ld, res );
401 if (!ld) return NULL;
402 return ldap_first_reference( ld, res );
408 /***********************************************************************
409 * ldap_memfreeA (WLDAP32.@)
413 void CDECL ldap_memfreeA( PCHAR block )
415 TRACE( "(%p)\n", block );
419 /***********************************************************************
420 * ldap_memfreeW (WLDAP32.@)
422 * Free a block of memory.
425 * block [I] Pointer to memory block to be freed.
427 void CDECL ldap_memfreeW( PWCHAR block )
429 TRACE( "(%p)\n", block );
433 /***********************************************************************
434 * ldap_msgfree (WLDAP32.@)
439 * res [I] Message to be freed.
441 ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res )
443 ULONG ret = LDAP_SUCCESS;
446 TRACE( "(%p)\n", res );
453 /***********************************************************************
454 * ldap_next_attributeA (WLDAP32.@)
456 * See ldap_next_attributeW.
458 PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
459 WLDAP32_BerElement *ptr )
465 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
467 if (!ld || !entry || !ptr) return NULL;
468 retW = ldap_next_attributeW( ld, entry, ptr );
470 ret = strWtoA( retW );
471 ldap_memfreeW( retW );
477 /***********************************************************************
478 * ldap_next_attributeW (WLDAP32.@)
480 * Get the next attribute for a given entry.
483 * ld [I] Pointer to an LDAP context.
484 * entry [I] Entry to retrieve attribute for.
485 * ptr [I/O] Position pointer.
488 * Success: The name of the next attribute.
492 * Free the returned string after each iteration with ldap_memfree.
493 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
495 PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
496 WLDAP32_BerElement *ptr )
502 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
504 if (!ld || !entry || !ptr) return NULL;
505 retU = ldap_next_attribute( ld, entry, ptr );
507 ret = strUtoW( retU );
508 ldap_memfree( retU );
514 /***********************************************************************
515 * ldap_next_entry (WLDAP32.@)
517 * Get the next entry from a result message.
520 * ld [I] Pointer to an LDAP context.
521 * entry [I] Entry returned by a previous call.
524 * Success: The next entry.
528 * The returned entry will be freed when the message is freed.
530 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
534 TRACE( "(%p, %p)\n", ld, entry );
536 if (!ld || !entry) return NULL;
537 return ldap_next_entry( ld, entry );
543 /***********************************************************************
544 * ldap_next_reference (WLDAP32.@)
546 * Get the next reference from a result message.
549 * ld [I] Pointer to an LDAP context.
550 * entry [I] Entry returned by a previous call.
553 * Success: The next reference.
557 * The returned entry will be freed when the message is freed.
559 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
561 #ifdef HAVE_LDAP_NEXT_REFERENCE
563 TRACE( "(%p, %p)\n", ld, entry );
565 if (!ld || !entry) return NULL;
566 return ldap_next_reference( ld, entry );
572 /***********************************************************************
573 * ldap_result (WLDAP32.@)
575 * Get the result of an asynchronous operation.
578 * ld [I] Pointer to an LDAP context.
579 * msgid [I] Message ID of the operation.
580 * all [I] How many results should be returned?
581 * timeout [I] How long to wait for the results?
582 * res [O] Result message for the operation.
585 * Success: One of the following values:
595 * LDAP_RES_SEARCH_ENTRY
596 * LDAP_RES_SEARCH_RESULT
600 * This function returns 0 when the timeout has expired.
603 * A NULL timeout pointer causes the function to block waiting
604 * for results to arrive. A timeout value of 0 causes the function
605 * to immediately return any available results. Free returned results
608 ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all,
609 struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
611 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
614 TRACE( "(%p, 0x%08lx, 0x%08lx, %p, %p)\n", ld, msgid, all, timeout, res );
616 if (!ld || !res || msgid == ~0UL) return ~0UL;
617 ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res );
623 /***********************************************************************
624 * LdapUnicodeToUTF8 (WLDAP32.@)
626 * Convert a wide character string to a UTF8 string.
629 * src [I] Wide character string to convert.
630 * srclen [I] Size of string to convert, in characters.
631 * dst [O] Pointer to a buffer that receives the converted string.
632 * dstlen [I] Size of the destination buffer in characters.
635 * The number of characters written into the destination buffer.
638 * Set dstlen to zero to ask for the required buffer size.
640 int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen )
642 return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL );
645 /***********************************************************************
646 * LdapUTF8ToUnicode (WLDAP32.@)
648 * Convert a UTF8 string to a wide character string.
651 * src [I] UTF8 string to convert.
652 * srclen [I] Size of string to convert, in characters.
653 * dst [O] Pointer to a buffer that receives the converted string.
654 * dstlen [I] Size of the destination buffer in characters.
657 * The number of characters written into the destination buffer.
660 * Set dstlen to zero to ask for the required buffer size.
662 int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen )
664 return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen );