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%08x)\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%08x, %p, 0x%08x)\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%08x, %p, 0x%08x)\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
);
383 /***********************************************************************
384 * ldap_first_reference (WLDAP32.@)
386 * Get the first reference from a result message.
389 * ld [I] Pointer to an LDAP context.
390 * res [I] Search result message.
393 * Success: The first reference.
396 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_reference( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
398 #ifdef HAVE_LDAP_FIRST_REFERENCE
400 TRACE( "(%p, %p)\n", ld
, res
);
402 if (!ld
) return NULL
;
403 return ldap_first_reference( ld
, res
);
410 /***********************************************************************
411 * ldap_memfreeA (WLDAP32.@)
415 void CDECL
ldap_memfreeA( PCHAR block
)
417 TRACE( "(%p)\n", block
);
421 /***********************************************************************
422 * ldap_memfreeW (WLDAP32.@)
424 * Free a block of memory.
427 * block [I] Pointer to memory block to be freed.
429 void CDECL
ldap_memfreeW( PWCHAR block
)
431 TRACE( "(%p)\n", block
);
435 /***********************************************************************
436 * ldap_msgfree (WLDAP32.@)
441 * res [I] Message to be freed.
443 ULONG CDECL
WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage
*res
)
445 ULONG ret
= LDAP_SUCCESS
;
448 TRACE( "(%p)\n", res
);
455 /***********************************************************************
456 * ldap_next_attributeA (WLDAP32.@)
458 * See ldap_next_attributeW.
460 PCHAR CDECL
ldap_next_attributeA( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
461 WLDAP32_BerElement
*ptr
)
467 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
469 if (!ld
|| !entry
|| !ptr
) return NULL
;
470 retW
= ldap_next_attributeW( ld
, entry
, ptr
);
472 ret
= strWtoA( retW
);
473 ldap_memfreeW( retW
);
479 /***********************************************************************
480 * ldap_next_attributeW (WLDAP32.@)
482 * Get the next attribute for a given entry.
485 * ld [I] Pointer to an LDAP context.
486 * entry [I] Entry to retrieve attribute for.
487 * ptr [I/O] Position pointer.
490 * Success: The name of the next attribute.
494 * Free the returned string after each iteration with ldap_memfree.
495 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
497 PWCHAR CDECL
ldap_next_attributeW( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
498 WLDAP32_BerElement
*ptr
)
504 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
506 if (!ld
|| !entry
|| !ptr
) return NULL
;
507 retU
= ldap_next_attribute( ld
, entry
, ptr
);
509 ret
= strUtoW( retU
);
510 ldap_memfree( retU
);
516 /***********************************************************************
517 * ldap_next_entry (WLDAP32.@)
519 * Get the next entry from a result message.
522 * ld [I] Pointer to an LDAP context.
523 * entry [I] Entry returned by a previous call.
526 * Success: The next entry.
530 * The returned entry will be freed when the message is freed.
532 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_entry( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
536 TRACE( "(%p, %p)\n", ld
, entry
);
538 if (!ld
|| !entry
) return NULL
;
539 return ldap_next_entry( ld
, entry
);
546 /***********************************************************************
547 * ldap_next_reference (WLDAP32.@)
549 * Get the next reference from a result message.
552 * ld [I] Pointer to an LDAP context.
553 * entry [I] Entry returned by a previous call.
556 * Success: The next reference.
560 * The returned entry will be freed when the message is freed.
562 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_reference( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
564 #ifdef HAVE_LDAP_NEXT_REFERENCE
566 TRACE( "(%p, %p)\n", ld
, entry
);
568 if (!ld
|| !entry
) return NULL
;
569 return ldap_next_reference( ld
, entry
);
576 /***********************************************************************
577 * ldap_result (WLDAP32.@)
579 * Get the result of an asynchronous operation.
582 * ld [I] Pointer to an LDAP context.
583 * msgid [I] Message ID of the operation.
584 * all [I] How many results should be returned?
585 * timeout [I] How long to wait for the results?
586 * res [O] Result message for the operation.
589 * Success: One of the following values:
599 * LDAP_RES_SEARCH_ENTRY
600 * LDAP_RES_SEARCH_RESULT
604 * This function returns 0 when the timeout has expired.
607 * A NULL timeout pointer causes the function to block waiting
608 * for results to arrive. A timeout value of 0 causes the function
609 * to immediately return any available results. Free returned results
612 ULONG CDECL
WLDAP32_ldap_result( WLDAP32_LDAP
*ld
, ULONG msgid
, ULONG all
,
613 struct l_timeval
*timeout
, WLDAP32_LDAPMessage
**res
)
615 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
618 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld
, msgid
, all
, timeout
, res
);
620 if (!ld
|| !res
|| msgid
== ~0UL) return ~0UL;
621 ret
= ldap_result( ld
, msgid
, all
, (struct timeval
*)timeout
, res
);
627 /***********************************************************************
628 * LdapUnicodeToUTF8 (WLDAP32.@)
630 * Convert a wide character string to a UTF8 string.
633 * src [I] Wide character string to convert.
634 * srclen [I] Size of string to convert, in characters.
635 * dst [O] Pointer to a buffer that receives the converted string.
636 * dstlen [I] Size of the destination buffer in characters.
639 * The number of characters written into the destination buffer.
642 * Set dstlen to zero to ask for the required buffer size.
644 int CDECL
LdapUnicodeToUTF8( LPCWSTR src
, int srclen
, LPSTR dst
, int dstlen
)
646 return WideCharToMultiByte( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
, NULL
, NULL
);
649 /***********************************************************************
650 * LdapUTF8ToUnicode (WLDAP32.@)
652 * Convert a UTF8 string to a wide character string.
655 * src [I] UTF8 string to convert.
656 * srclen [I] Size of string to convert, in characters.
657 * dst [O] Pointer to a buffer that receives the converted string.
658 * dstlen [I] Size of the destination buffer in characters.
661 * The number of characters written into the destination buffer.
664 * Set dstlen to zero to ask for the required buffer size.
666 int CDECL
LdapUTF8ToUnicode( LPCSTR src
, int srclen
, LPWSTR dst
, int dstlen
)
668 return MultiByteToWideChar( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
);