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"
37 #include "winldap_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
42 /***********************************************************************
43 * ldap_abandon (WLDAP32.@)
45 * Cancel an asynchronous operation.
48 * ld [I] Pointer to an LDAP context.
49 * msgid [I] ID of the operation to cancel.
52 * Success: LDAP_SUCCESS
53 * Failure: An LDAP error code.
55 ULONG CDECL WLDAP32_ldap_abandon( WLDAP32_LDAP *ld, ULONG msgid )
57 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
60 TRACE( "(%p, 0x%08x)\n", ld, msgid );
63 ret = map_error( ldap_abandon_ext( ld, msgid, NULL, NULL ));
69 /***********************************************************************
70 * ldap_check_filterA (WLDAP32.@)
72 * See ldap_check_filterW.
74 ULONG CDECL ldap_check_filterA( WLDAP32_LDAP *ld, PCHAR filter )
77 WCHAR *filterW = NULL;
79 TRACE( "(%p, %s)\n", ld, debugstr_a(filter) );
81 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
84 filterW = strAtoW( filter );
85 if (!filterW) return WLDAP32_LDAP_NO_MEMORY;
88 ret = ldap_check_filterW( ld, filterW );
94 /***********************************************************************
95 * ldap_check_filterW (WLDAP32.@)
97 * Check filter syntax.
100 * ld [I] Pointer to an LDAP context.
101 * filter [I] Filter string.
104 * Success: LDAP_SUCCESS
105 * Failure: An LDAP error code.
107 ULONG CDECL ldap_check_filterW( WLDAP32_LDAP *ld, PWCHAR filter )
109 TRACE( "(%p, %s)\n", ld, debugstr_w(filter) );
111 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
112 return WLDAP32_LDAP_SUCCESS; /* FIXME: do some checks */
115 /***********************************************************************
116 * ldap_cleanup (WLDAP32.@)
118 ULONG CDECL ldap_cleanup( HANDLE instance )
120 TRACE( "(%p)\n", instance );
121 return WLDAP32_LDAP_SUCCESS;
124 /***********************************************************************
125 * ldap_conn_from_msg (WLDAP32.@)
127 * Get the LDAP context for a given message.
130 * ld [I] Pointer to an LDAP context.
131 * res [I] LDAP message.
134 * Success: Pointer to an LDAP context.
137 WLDAP32_LDAP * CDECL ldap_conn_from_msg( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
139 TRACE( "(%p, %p)\n", ld, res );
141 if (!ld || !res) return NULL;
142 return ld; /* FIXME: not always correct */
145 /***********************************************************************
146 * ldap_count_entries (WLDAP32.@)
148 * Count the number of entries returned from a search.
151 * ld [I] Pointer to an LDAP context.
152 * res [I] LDAP message.
155 * Success: The number of entries.
158 ULONG CDECL WLDAP32_ldap_count_entries( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
160 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
163 TRACE( "(%p, %p)\n", ld, res );
166 ret = ldap_count_entries( ld, res );
172 /***********************************************************************
173 * ldap_count_references (WLDAP32.@)
175 * Count the number of references returned from a search.
178 * ld [I] Pointer to an LDAP context.
179 * res [I] LDAP message.
182 * Success: The number of references.
185 ULONG CDECL WLDAP32_ldap_count_references( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
187 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
188 #ifdef HAVE_LDAP_COUNT_REFERENCES
190 TRACE( "(%p, %p)\n", ld, res );
193 ret = ldap_count_references( ld, res );
199 static ULONG get_escape_size( PCHAR src, ULONG srclen )
205 for (i = 0; i < srclen; i++)
207 if ((src[i] >= '0' && src[i] <= '9') ||
208 (src[i] >= 'A' && src[i] <= 'Z') ||
209 (src[i] >= 'a' && src[i] <= 'z'))
218 static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst )
221 static const char fmt[] = "\\%02X";
224 for (i = 0; i < srclen; i++)
226 if ((src[i] >= '0' && src[i] <= '9') ||
227 (src[i] >= 'A' && src[i] <= 'Z') ||
228 (src[i] >= 'a' && src[i] <= 'z'))
232 sprintf( d, fmt, (unsigned char)src[i] );
239 /***********************************************************************
240 * ldap_escape_filter_elementA (WLDAP32.@)
242 * See ldap_escape_filter_elementW.
244 ULONG CDECL ldap_escape_filter_elementA( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen )
248 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
250 len = get_escape_size( src, srclen );
251 if (!dst) return len;
253 if (!src || dstlen < len)
254 return WLDAP32_LDAP_PARAM_ERROR;
257 escape_filter_element( src, srclen, dst );
258 return WLDAP32_LDAP_SUCCESS;
262 /***********************************************************************
263 * ldap_escape_filter_elementW (WLDAP32.@)
265 * Escape binary data for safe passing in filters.
268 * src [I] Filter element to be escaped.
269 * srclen [I] Length in bytes of the filter element.
270 * dst [O] Destination buffer for the escaped filter element.
271 * dstlen [I] Length in bytes of the destination buffer.
274 * Success: LDAP_SUCCESS
275 * Failure: An LDAP error code.
277 ULONG CDECL ldap_escape_filter_elementW( PCHAR src, ULONG srclen, PWCHAR dst, ULONG dstlen )
281 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
283 len = get_escape_size( src, srclen );
284 if (!dst) return len;
286 /* no matter what you throw at it, this is what native returns */
287 return WLDAP32_LDAP_PARAM_ERROR;
290 /***********************************************************************
291 * ldap_first_attributeA (WLDAP32.@)
293 * See ldap_first_attributeW.
295 PCHAR CDECL ldap_first_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
296 WLDAP32_BerElement** ptr )
302 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
304 if (!ld || !entry) return NULL;
305 retW = ldap_first_attributeW( ld, entry, ptr );
307 ret = strWtoA( retW );
308 ldap_memfreeW( retW );
314 /***********************************************************************
315 * ldap_first_attributeW (WLDAP32.@)
317 * Get the first attribute for a given entry.
320 * ld [I] Pointer to an LDAP context.
321 * entry [I] Entry to retrieve attribute for.
322 * ptr [O] Position pointer.
325 * Success: Name of the first attribute.
329 * Use ldap_memfree to free the returned string.
331 PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
332 WLDAP32_BerElement** ptr )
338 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
340 if (!ld || !entry) return NULL;
341 retU = ldap_first_attribute( ld, entry, ptr );
343 ret = strUtoW( retU );
344 ldap_memfree( retU );
350 /***********************************************************************
351 * ldap_first_entry (WLDAP32.@)
353 * Get the first entry from a result message.
356 * ld [I] Pointer to an LDAP context.
357 * res [I] Search result message.
360 * Success: The first entry.
364 * The returned entry will be freed when the message is freed.
366 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
370 TRACE( "(%p, %p)\n", ld, res );
372 if (!ld || !res) return NULL;
373 return ldap_first_entry( ld, res );
380 /***********************************************************************
381 * ldap_first_reference (WLDAP32.@)
383 * Get the first reference from a result message.
386 * ld [I] Pointer to an LDAP context.
387 * res [I] Search result message.
390 * Success: The first reference.
393 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
395 #ifdef HAVE_LDAP_FIRST_REFERENCE
397 TRACE( "(%p, %p)\n", ld, res );
399 if (!ld) return NULL;
400 return ldap_first_reference( ld, res );
407 /***********************************************************************
408 * ldap_memfreeA (WLDAP32.@)
412 void CDECL ldap_memfreeA( PCHAR block )
414 TRACE( "(%p)\n", block );
418 /***********************************************************************
419 * ldap_memfreeW (WLDAP32.@)
421 * Free a block of memory.
424 * block [I] Pointer to memory block to be freed.
426 void CDECL ldap_memfreeW( PWCHAR block )
428 TRACE( "(%p)\n", block );
432 /***********************************************************************
433 * ldap_msgfree (WLDAP32.@)
438 * res [I] Message to be freed.
440 ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res )
442 ULONG ret = WLDAP32_LDAP_SUCCESS;
445 TRACE( "(%p)\n", res );
452 /***********************************************************************
453 * ldap_next_attributeA (WLDAP32.@)
455 * See ldap_next_attributeW.
457 PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
458 WLDAP32_BerElement *ptr )
464 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
466 if (!ld || !entry || !ptr) return NULL;
467 retW = ldap_next_attributeW( ld, entry, ptr );
469 ret = strWtoA( retW );
470 ldap_memfreeW( retW );
476 /***********************************************************************
477 * ldap_next_attributeW (WLDAP32.@)
479 * Get the next attribute for a given entry.
482 * ld [I] Pointer to an LDAP context.
483 * entry [I] Entry to retrieve attribute for.
484 * ptr [I/O] Position pointer.
487 * Success: The name of the next attribute.
491 * Free the returned string after each iteration with ldap_memfree.
492 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
494 PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
495 WLDAP32_BerElement *ptr )
501 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
503 if (!ld || !entry || !ptr) return NULL;
504 retU = ldap_next_attribute( ld, entry, ptr );
506 ret = strUtoW( retU );
507 ldap_memfree( retU );
513 /***********************************************************************
514 * ldap_next_entry (WLDAP32.@)
516 * Get the next entry from a result message.
519 * ld [I] Pointer to an LDAP context.
520 * entry [I] Entry returned by a previous call.
523 * Success: The next entry.
527 * The returned entry will be freed when the message is freed.
529 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
533 TRACE( "(%p, %p)\n", ld, entry );
535 if (!ld || !entry) return NULL;
536 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 );
573 /***********************************************************************
574 * ldap_result (WLDAP32.@)
576 * Get the result of an asynchronous operation.
579 * ld [I] Pointer to an LDAP context.
580 * msgid [I] Message ID of the operation.
581 * all [I] How many results should be returned?
582 * timeout [I] How long to wait for the results?
583 * res [O] Result message for the operation.
586 * Success: One of the following values:
596 * LDAP_RES_SEARCH_ENTRY
597 * LDAP_RES_SEARCH_RESULT
601 * This function returns 0 when the timeout has expired.
604 * A NULL timeout pointer causes the function to block waiting
605 * for results to arrive. A timeout value of 0 causes the function
606 * to immediately return any available results. Free returned results
609 ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all,
610 struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
612 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
615 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld, msgid, all, timeout, res );
617 if (!ld || !res || msgid == ~0u) return ~0u;
618 ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res );
624 /***********************************************************************
625 * LdapUnicodeToUTF8 (WLDAP32.@)
627 * Convert a wide character string to a UTF8 string.
630 * src [I] Wide character string to convert.
631 * srclen [I] Size of string to convert, in characters.
632 * dst [O] Pointer to a buffer that receives the converted string.
633 * dstlen [I] Size of the destination buffer in characters.
636 * The number of characters written into the destination buffer.
639 * Set dstlen to zero to ask for the required buffer size.
641 int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen )
643 return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL );
646 /***********************************************************************
647 * LdapUTF8ToUnicode (WLDAP32.@)
649 * Convert a UTF8 string to a wide character string.
652 * src [I] UTF8 string to convert.
653 * srclen [I] Size of string to convert, in characters.
654 * dst [O] Pointer to a buffer that receives the converted string.
655 * dstlen [I] Size of the destination buffer in characters.
658 * The number of characters written into the destination buffer.
661 * Set dstlen to zero to ask for the required buffer size.
663 int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen )
665 return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen );