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
28 #include "wine/debug.h"
29 #include "winldap_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
33 /***********************************************************************
34 * ldap_abandon (WLDAP32.@)
36 * Cancel an asynchronous operation.
39 * ld [I] Pointer to an LDAP context.
40 * msgid [I] ID of the operation to cancel.
43 * Success: LDAP_SUCCESS
44 * Failure: An LDAP error code.
46 ULONG CDECL
ldap_abandon( LDAP
*ld
, ULONG msgid
)
48 TRACE( "(%p, 0x%08x)\n", ld
, msgid
);
51 return map_error( ldap_funcs
->fn_ldap_abandon_ext( CTX(ld
), msgid
, NULL
, NULL
) );
54 /***********************************************************************
55 * ldap_check_filterA (WLDAP32.@)
57 * See ldap_check_filterW.
59 ULONG CDECL
ldap_check_filterA( LDAP
*ld
, char *filter
)
62 WCHAR
*filterW
= NULL
;
64 TRACE( "(%p, %s)\n", ld
, debugstr_a(filter
) );
66 if (!ld
) return LDAP_PARAM_ERROR
;
67 if (filter
&& !(filterW
= strAtoW( filter
))) return LDAP_NO_MEMORY
;
69 ret
= ldap_check_filterW( ld
, filterW
);
75 /***********************************************************************
76 * ldap_check_filterW (WLDAP32.@)
78 * Check filter syntax.
81 * ld [I] Pointer to an LDAP context.
82 * filter [I] Filter string.
85 * Success: LDAP_SUCCESS
86 * Failure: An LDAP error code.
88 ULONG CDECL
ldap_check_filterW( LDAP
*ld
, WCHAR
*filter
)
90 TRACE( "(%p, %s)\n", ld
, debugstr_w(filter
) );
92 if (!ld
) return LDAP_PARAM_ERROR
;
93 return LDAP_SUCCESS
; /* FIXME: do some checks */
96 /***********************************************************************
97 * ldap_cleanup (WLDAP32.@)
99 ULONG CDECL
ldap_cleanup( HANDLE instance
)
101 TRACE( "(%p)\n", instance
);
105 /***********************************************************************
106 * ldap_conn_from_msg (WLDAP32.@)
108 * Get the LDAP context for a given message.
111 * ld [I] Pointer to an LDAP context.
112 * res [I] LDAP message.
115 * Success: Pointer to an LDAP context.
118 LDAP
* CDECL
ldap_conn_from_msg( LDAP
*ld
, LDAPMessage
*res
)
120 TRACE( "(%p, %p)\n", ld
, res
);
122 if (!ld
|| !res
) return NULL
;
123 return ld
; /* FIXME: not always correct */
126 /***********************************************************************
127 * ldap_count_entries (WLDAP32.@)
129 * Count the number of entries returned from a search.
132 * ld [I] Pointer to an LDAP context.
133 * res [I] LDAP message.
136 * Success: The number of entries.
139 ULONG CDECL
ldap_count_entries( LDAP
*ld
, LDAPMessage
*res
)
141 TRACE( "(%p, %p)\n", ld
, res
);
144 return ldap_funcs
->fn_ldap_count_entries( CTX(ld
), MSG(res
) );
147 /***********************************************************************
148 * ldap_count_references (WLDAP32.@)
150 * Count the number of references returned from a search.
153 * ld [I] Pointer to an LDAP context.
154 * res [I] LDAP message.
157 * Success: The number of references.
160 ULONG CDECL
ldap_count_references( LDAP
*ld
, LDAPMessage
*res
)
162 TRACE( "(%p, %p)\n", ld
, res
);
165 return ldap_funcs
->fn_ldap_count_references( CTX(ld
), MSG(res
) );
168 static ULONG
get_escape_size( PCHAR src
, ULONG srclen
)
174 for (i
= 0; i
< srclen
; i
++)
176 if ((src
[i
] >= '0' && src
[i
] <= '9') ||
177 (src
[i
] >= 'A' && src
[i
] <= 'Z') ||
178 (src
[i
] >= 'a' && src
[i
] <= 'z'))
187 static void escape_filter_element( char *src
, ULONG srclen
, char *dst
)
190 static const char fmt
[] = "\\%02X";
193 for (i
= 0; i
< srclen
; i
++)
195 if ((src
[i
] >= '0' && src
[i
] <= '9') ||
196 (src
[i
] >= 'A' && src
[i
] <= 'Z') ||
197 (src
[i
] >= 'a' && src
[i
] <= 'z'))
200 d
+= sprintf( d
, fmt
, (unsigned char)src
[i
] );
205 /***********************************************************************
206 * ldap_escape_filter_elementA (WLDAP32.@)
208 * See ldap_escape_filter_elementW.
210 ULONG CDECL
ldap_escape_filter_elementA( char *src
, ULONG srclen
, char *dst
, ULONG dstlen
)
212 ULONG len
= get_escape_size( src
, srclen
);
214 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src
, srclen
, dst
, dstlen
);
216 if (!dst
) return len
;
217 if (!src
|| dstlen
< len
) return LDAP_PARAM_ERROR
;
219 escape_filter_element( src
, srclen
, dst
);
223 /***********************************************************************
224 * ldap_escape_filter_elementW (WLDAP32.@)
226 * Escape binary data for safe passing in filters.
229 * src [I] Filter element to be escaped.
230 * srclen [I] Length in bytes of the filter element.
231 * dst [O] Destination buffer for the escaped filter element.
232 * dstlen [I] Length in bytes of the destination buffer.
235 * Success: LDAP_SUCCESS
236 * Failure: An LDAP error code.
238 ULONG CDECL
ldap_escape_filter_elementW( char *src
, ULONG srclen
, WCHAR
*dst
, ULONG dstlen
)
240 ULONG len
= get_escape_size( src
, srclen
);
242 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src
, srclen
, dst
, dstlen
);
244 if (!dst
) return len
;
246 /* no matter what you throw at it, this is what native returns */
247 return LDAP_PARAM_ERROR
;
250 /***********************************************************************
251 * ldap_first_attributeA (WLDAP32.@)
253 * See ldap_first_attributeW.
255 char * CDECL
ldap_first_attributeA( LDAP
*ld
, LDAPMessage
*entry
, BerElement
**ber
)
260 TRACE( "(%p, %p, %p)\n", ld
, entry
, ber
);
262 if (!ld
|| !entry
) return NULL
;
264 retW
= ldap_first_attributeW( ld
, entry
, ber
);
267 ret
= strWtoA( retW
);
268 ldap_memfreeW( retW
);
274 /***********************************************************************
275 * ldap_first_attributeW (WLDAP32.@)
277 * Get the first attribute for a given entry.
280 * ld [I] Pointer to an LDAP context.
281 * entry [I] Entry to retrieve attribute for.
282 * ptr [O] Position pointer.
285 * Success: Name of the first attribute.
289 * Use ldap_memfree to free the returned string.
291 WCHAR
* CDECL
ldap_first_attributeW( LDAP
*ld
, LDAPMessage
*entry
, BerElement
**ptr
)
298 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
300 if (!ld
|| !entry
) return NULL
;
302 retU
= ldap_funcs
->fn_ldap_first_attribute( CTX(ld
), MSG(entry
), &berU
);
303 if (retU
&& (ber
= malloc( sizeof(*ber
) )))
305 BER(ber
) = (char *)berU
;
307 ret
= strUtoW( retU
);
310 ldap_funcs
->fn_ldap_memfree( retU
);
314 /***********************************************************************
315 * ldap_first_entry (WLDAP32.@)
317 * Get the first entry from a result message.
320 * ld [I] Pointer to an LDAP context.
321 * res [I] Search result message.
324 * Success: The first entry.
328 * The returned entry will be freed when the message is freed.
330 LDAPMessage
* CDECL
ldap_first_entry( LDAP
*ld
, LDAPMessage
*res
)
334 TRACE( "(%p, %p)\n", ld
, res
);
336 if (!ld
|| !res
) return NULL
;
338 msgU
= ldap_funcs
->fn_ldap_first_entry( CTX(ld
), MSG(res
) );
341 assert( msgU
== MSG(res
) );
348 /***********************************************************************
349 * ldap_first_reference (WLDAP32.@)
351 * Get the first reference from a result message.
354 * ld [I] Pointer to an LDAP context.
355 * res [I] Search result message.
358 * Success: The first reference.
361 LDAPMessage
* CDECL
ldap_first_reference( LDAP
*ld
, LDAPMessage
*res
)
365 TRACE( "(%p, %p)\n", ld
, res
);
367 if (!ld
) return NULL
;
369 msgU
= ldap_funcs
->fn_ldap_first_reference( CTX(ld
), MSG(res
) );
372 assert( msgU
== MSG(res
) );
379 /***********************************************************************
380 * ldap_memfreeA (WLDAP32.@)
384 void CDECL
ldap_memfreeA( char *block
)
386 TRACE( "(%p)\n", block
);
390 /***********************************************************************
391 * ldap_memfreeW (WLDAP32.@)
393 * Free a block of memory.
396 * block [I] Pointer to memory block to be freed.
398 void CDECL
ldap_memfreeW( WCHAR
*block
)
400 TRACE( "(%p)\n", block
);
404 /***********************************************************************
405 * ldap_msgfree (WLDAP32.@)
410 * res [I] Message to be freed.
412 ULONG CDECL
ldap_msgfree( LDAPMessage
*res
)
414 LDAPMessage
*entry
, *list
= res
;
416 TRACE( "(%p)\n", res
);
418 if (!res
) return LDAP_SUCCESS
;
420 ldap_funcs
->fn_ldap_msgfree( MSG(res
) );
424 list
= entry
->lm_next
;
431 /***********************************************************************
432 * ldap_next_attributeA (WLDAP32.@)
434 * See ldap_next_attributeW.
436 char * CDECL
ldap_next_attributeA( LDAP
*ld
, LDAPMessage
*entry
, BerElement
*ptr
)
441 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
443 if (!ld
|| !entry
|| !ptr
) return NULL
;
445 retW
= ldap_next_attributeW( ld
, entry
, ptr
);
448 ret
= strWtoA( retW
);
449 ldap_memfreeW( retW
);
455 /***********************************************************************
456 * ldap_next_attributeW (WLDAP32.@)
458 * Get the next attribute for a given entry.
461 * ld [I] Pointer to an LDAP context.
462 * entry [I] Entry to retrieve attribute for.
463 * ptr [I/O] Position pointer.
466 * Success: The name of the next attribute.
470 * Free the returned string after each iteration with ldap_memfree.
471 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
473 WCHAR
* CDECL
ldap_next_attributeW( LDAP
*ld
, LDAPMessage
*entry
, BerElement
*ptr
)
478 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
480 if (!ld
|| !entry
|| !ptr
) return NULL
;
482 retU
= ldap_funcs
->fn_ldap_next_attribute( CTX(ld
), MSG(entry
), BER(ptr
) );
485 ret
= strUtoW( retU
);
486 ldap_funcs
->fn_ldap_memfree( retU
);
492 /***********************************************************************
493 * ldap_next_entry (WLDAP32.@)
495 * Get the next entry from a result message.
498 * ld [I] Pointer to an LDAP context.
499 * entry [I] Entry returned by a previous call.
502 * Success: The next entry.
506 * The returned entry will be freed when the message is freed.
508 LDAPMessage
* CDECL
ldap_next_entry( LDAP
*ld
, LDAPMessage
*entry
)
510 LDAPMessage
*msg
= NULL
;
513 TRACE( "(%p, %p)\n", ld
, entry
);
515 if (!ld
|| !entry
) return NULL
;
517 if (entry
->lm_next
) return entry
->lm_next
;
519 msgU
= ldap_funcs
->fn_ldap_next_entry( CTX(ld
), MSG(entry
) );
520 if (msgU
&& (msg
= calloc( 1, sizeof(*msg
) )))
523 entry
->lm_next
= msg
;
529 /***********************************************************************
530 * ldap_next_reference (WLDAP32.@)
532 * Get the next reference from a result message.
535 * ld [I] Pointer to an LDAP context.
536 * entry [I] Entry returned by a previous call.
539 * Success: The next reference.
543 * The returned entry will be freed when the message is freed.
545 LDAPMessage
* CDECL
ldap_next_reference( LDAP
*ld
, LDAPMessage
*entry
)
547 LDAPMessage
*msg
= NULL
;
550 TRACE( "(%p, %p)\n", ld
, entry
);
552 if (!ld
|| !entry
) return NULL
;
554 if (entry
->lm_next
) return entry
->lm_next
;
556 msgU
= ldap_funcs
->fn_ldap_next_reference( CTX(ld
), MSG(entry
) );
557 if (msgU
&& (msg
= calloc( 1, sizeof(*msg
) )))
560 entry
->lm_next
= msg
;
566 /***********************************************************************
567 * ldap_result (WLDAP32.@)
569 * Get the result of an asynchronous operation.
572 * ld [I] Pointer to an LDAP context.
573 * msgid [I] Message ID of the operation.
574 * all [I] How many results should be returned?
575 * timeout [I] How long to wait for the results?
576 * res [O] Result message for the operation.
579 * Success: One of the following values:
589 * LDAP_RES_SEARCH_ENTRY
590 * LDAP_RES_SEARCH_RESULT
594 * This function returns 0 when the timeout has expired.
597 * A NULL timeout pointer causes the function to block waiting
598 * for results to arrive. A timeout value of 0 causes the function
599 * to immediately return any available results. Free returned results
602 ULONG CDECL
ldap_result( LDAP
*ld
, ULONG msgid
, ULONG all
, struct l_timeval
*timeout
, LDAPMessage
**res
)
605 struct timevalU timeval
;
609 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld
, msgid
, all
, timeout
, res
);
611 if (!ld
|| !res
|| msgid
== ~0u) return ~0u;
615 timeval
.tv_sec
= timeout
->tv_sec
;
616 timeval
.tv_usec
= timeout
->tv_usec
;
619 ret
= ldap_funcs
->fn_ldap_result( CTX(ld
), msgid
, all
, timeout
? &timeval
: NULL
, &msgU
);
620 if (msgU
&& (msg
= calloc( 1, sizeof(*msg
) )))
629 /***********************************************************************
630 * LdapUnicodeToUTF8 (WLDAP32.@)
632 * Convert a wide character string to a UTF8 string.
635 * src [I] Wide character string to convert.
636 * srclen [I] Size of string to convert, in characters.
637 * dst [O] Pointer to a buffer that receives the converted string.
638 * dstlen [I] Size of the destination buffer in characters.
641 * The number of characters written into the destination buffer.
644 * Set dstlen to zero to ask for the required buffer size.
646 int CDECL
LdapUnicodeToUTF8( const WCHAR
*src
, int srclen
, char *dst
, int dstlen
)
648 return WideCharToMultiByte( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
, NULL
, NULL
);
651 /***********************************************************************
652 * LdapUTF8ToUnicode (WLDAP32.@)
654 * Convert a UTF8 string to a wide character string.
657 * src [I] UTF8 string to convert.
658 * srclen [I] Size of string to convert, in characters.
659 * dst [O] Pointer to a buffer that receives the converted string.
660 * dstlen [I] Size of the destination buffer in characters.
663 * The number of characters written into the destination buffer.
666 * Set dstlen to zero to ask for the required buffer size.
668 int CDECL
LdapUTF8ToUnicode( const char *src
, int srclen
, WCHAR
*dst
, int dstlen
)
670 return MultiByteToWideChar( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
);