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
22 #include "wine/port.h"
34 #include "winldap_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
40 /***********************************************************************
41 * ldap_abandon (WLDAP32.@)
43 * Cancel an asynchronous operation.
46 * ld [I] Pointer to an LDAP context.
47 * msgid [I] ID of the operation to cancel.
50 * Success: LDAP_SUCCESS
51 * Failure: An LDAP error code.
53 ULONG CDECL
WLDAP32_ldap_abandon( WLDAP32_LDAP
*ld
, ULONG msgid
)
55 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
58 TRACE( "(%p, 0x%08x)\n", ld
, msgid
);
61 ret
= map_error( ldap_abandon_ext( ld
, msgid
, NULL
, NULL
));
67 /***********************************************************************
68 * ldap_check_filterA (WLDAP32.@)
70 * See ldap_check_filterW.
72 ULONG CDECL
ldap_check_filterA( WLDAP32_LDAP
*ld
, PCHAR filter
)
75 WCHAR
*filterW
= NULL
;
77 TRACE( "(%p, %s)\n", ld
, debugstr_a(filter
) );
79 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
82 filterW
= strAtoW( filter
);
83 if (!filterW
) return WLDAP32_LDAP_NO_MEMORY
;
86 ret
= ldap_check_filterW( ld
, filterW
);
92 /***********************************************************************
93 * ldap_check_filterW (WLDAP32.@)
95 * Check filter syntax.
98 * ld [I] Pointer to an LDAP context.
99 * filter [I] Filter string.
102 * Success: LDAP_SUCCESS
103 * Failure: An LDAP error code.
105 ULONG CDECL
ldap_check_filterW( WLDAP32_LDAP
*ld
, PWCHAR filter
)
107 TRACE( "(%p, %s)\n", ld
, debugstr_w(filter
) );
109 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
110 return WLDAP32_LDAP_SUCCESS
; /* FIXME: do some checks */
113 /***********************************************************************
114 * ldap_cleanup (WLDAP32.@)
116 ULONG CDECL
ldap_cleanup( HANDLE instance
)
118 TRACE( "(%p)\n", instance
);
119 return WLDAP32_LDAP_SUCCESS
;
122 /***********************************************************************
123 * ldap_conn_from_msg (WLDAP32.@)
125 * Get the LDAP context for a given message.
128 * ld [I] Pointer to an LDAP context.
129 * res [I] LDAP message.
132 * Success: Pointer to an LDAP context.
135 WLDAP32_LDAP
* CDECL
ldap_conn_from_msg( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
137 TRACE( "(%p, %p)\n", ld
, res
);
139 if (!ld
|| !res
) return NULL
;
140 return ld
; /* FIXME: not always correct */
143 /***********************************************************************
144 * ldap_count_entries (WLDAP32.@)
146 * Count the number of entries returned from a search.
149 * ld [I] Pointer to an LDAP context.
150 * res [I] LDAP message.
153 * Success: The number of entries.
156 ULONG CDECL
WLDAP32_ldap_count_entries( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
158 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
161 TRACE( "(%p, %p)\n", ld
, res
);
164 ret
= ldap_count_entries( ld
, res
);
170 /***********************************************************************
171 * ldap_count_references (WLDAP32.@)
173 * Count the number of references returned from a search.
176 * ld [I] Pointer to an LDAP context.
177 * res [I] LDAP message.
180 * Success: The number of references.
183 ULONG CDECL
WLDAP32_ldap_count_references( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
185 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
186 #ifdef HAVE_LDAP_COUNT_REFERENCES
188 TRACE( "(%p, %p)\n", ld
, res
);
191 ret
= ldap_count_references( ld
, res
);
197 static ULONG
get_escape_size( PCHAR src
, ULONG srclen
)
203 for (i
= 0; i
< srclen
; i
++)
205 if ((src
[i
] >= '0' && src
[i
] <= '9') ||
206 (src
[i
] >= 'A' && src
[i
] <= 'Z') ||
207 (src
[i
] >= 'a' && src
[i
] <= 'z'))
216 static void escape_filter_element( PCHAR src
, ULONG srclen
, PCHAR dst
)
219 static const char fmt
[] = "\\%02X";
222 for (i
= 0; i
< srclen
; i
++)
224 if ((src
[i
] >= '0' && src
[i
] <= '9') ||
225 (src
[i
] >= 'A' && src
[i
] <= 'Z') ||
226 (src
[i
] >= 'a' && src
[i
] <= 'z'))
230 sprintf( d
, fmt
, (unsigned char)src
[i
] );
237 /***********************************************************************
238 * ldap_escape_filter_elementA (WLDAP32.@)
240 * See ldap_escape_filter_elementW.
242 ULONG CDECL
ldap_escape_filter_elementA( PCHAR src
, ULONG srclen
, PCHAR dst
, ULONG dstlen
)
246 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src
, srclen
, dst
, dstlen
);
248 len
= get_escape_size( src
, srclen
);
249 if (!dst
) return len
;
251 if (!src
|| dstlen
< len
)
252 return WLDAP32_LDAP_PARAM_ERROR
;
255 escape_filter_element( src
, srclen
, dst
);
256 return WLDAP32_LDAP_SUCCESS
;
260 /***********************************************************************
261 * ldap_escape_filter_elementW (WLDAP32.@)
263 * Escape binary data for safe passing in filters.
266 * src [I] Filter element to be escaped.
267 * srclen [I] Length in bytes of the filter element.
268 * dst [O] Destination buffer for the escaped filter element.
269 * dstlen [I] Length in bytes of the destination buffer.
272 * Success: LDAP_SUCCESS
273 * Failure: An LDAP error code.
275 ULONG CDECL
ldap_escape_filter_elementW( PCHAR src
, ULONG srclen
, PWCHAR dst
, ULONG dstlen
)
279 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src
, srclen
, dst
, dstlen
);
281 len
= get_escape_size( src
, srclen
);
282 if (!dst
) return len
;
284 /* no matter what you throw at it, this is what native returns */
285 return WLDAP32_LDAP_PARAM_ERROR
;
288 /***********************************************************************
289 * ldap_first_attributeA (WLDAP32.@)
291 * See ldap_first_attributeW.
293 PCHAR CDECL
ldap_first_attributeA( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
294 WLDAP32_BerElement
** ptr
)
300 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
302 if (!ld
|| !entry
) return NULL
;
303 retW
= ldap_first_attributeW( ld
, entry
, ptr
);
305 ret
= strWtoA( retW
);
306 ldap_memfreeW( retW
);
312 /***********************************************************************
313 * ldap_first_attributeW (WLDAP32.@)
315 * Get the first attribute for a given entry.
318 * ld [I] Pointer to an LDAP context.
319 * entry [I] Entry to retrieve attribute for.
320 * ptr [O] Position pointer.
323 * Success: Name of the first attribute.
327 * Use ldap_memfree to free the returned string.
329 PWCHAR CDECL
ldap_first_attributeW( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
330 WLDAP32_BerElement
** ptr
)
336 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
338 if (!ld
|| !entry
) return NULL
;
339 retU
= ldap_first_attribute( ld
, entry
, ptr
);
341 ret
= strUtoW( retU
);
342 ldap_memfree( retU
);
348 /***********************************************************************
349 * ldap_first_entry (WLDAP32.@)
351 * Get the first entry from a result message.
354 * ld [I] Pointer to an LDAP context.
355 * res [I] Search result message.
358 * Success: The first entry.
362 * The returned entry will be freed when the message is freed.
364 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_entry( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
368 TRACE( "(%p, %p)\n", ld
, res
);
370 if (!ld
|| !res
) return NULL
;
371 return ldap_first_entry( ld
, res
);
378 /***********************************************************************
379 * ldap_first_reference (WLDAP32.@)
381 * Get the first reference from a result message.
384 * ld [I] Pointer to an LDAP context.
385 * res [I] Search result message.
388 * Success: The first reference.
391 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_reference( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
393 #ifdef HAVE_LDAP_FIRST_REFERENCE
395 TRACE( "(%p, %p)\n", ld
, res
);
397 if (!ld
) return NULL
;
398 return ldap_first_reference( ld
, res
);
405 /***********************************************************************
406 * ldap_memfreeA (WLDAP32.@)
410 void CDECL
ldap_memfreeA( PCHAR block
)
412 TRACE( "(%p)\n", block
);
416 /***********************************************************************
417 * ldap_memfreeW (WLDAP32.@)
419 * Free a block of memory.
422 * block [I] Pointer to memory block to be freed.
424 void CDECL
ldap_memfreeW( PWCHAR block
)
426 TRACE( "(%p)\n", block
);
430 /***********************************************************************
431 * ldap_msgfree (WLDAP32.@)
436 * res [I] Message to be freed.
438 ULONG CDECL
WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage
*res
)
440 ULONG ret
= WLDAP32_LDAP_SUCCESS
;
443 TRACE( "(%p)\n", res
);
450 /***********************************************************************
451 * ldap_next_attributeA (WLDAP32.@)
453 * See ldap_next_attributeW.
455 PCHAR CDECL
ldap_next_attributeA( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
456 WLDAP32_BerElement
*ptr
)
462 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
464 if (!ld
|| !entry
|| !ptr
) return NULL
;
465 retW
= ldap_next_attributeW( ld
, entry
, ptr
);
467 ret
= strWtoA( retW
);
468 ldap_memfreeW( retW
);
474 /***********************************************************************
475 * ldap_next_attributeW (WLDAP32.@)
477 * Get the next attribute for a given entry.
480 * ld [I] Pointer to an LDAP context.
481 * entry [I] Entry to retrieve attribute for.
482 * ptr [I/O] Position pointer.
485 * Success: The name of the next attribute.
489 * Free the returned string after each iteration with ldap_memfree.
490 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
492 PWCHAR CDECL
ldap_next_attributeW( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
493 WLDAP32_BerElement
*ptr
)
499 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
501 if (!ld
|| !entry
|| !ptr
) return NULL
;
502 retU
= ldap_next_attribute( ld
, entry
, ptr
);
504 ret
= strUtoW( retU
);
505 ldap_memfree( retU
);
511 /***********************************************************************
512 * ldap_next_entry (WLDAP32.@)
514 * Get the next entry from a result message.
517 * ld [I] Pointer to an LDAP context.
518 * entry [I] Entry returned by a previous call.
521 * Success: The next entry.
525 * The returned entry will be freed when the message is freed.
527 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_entry( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
531 TRACE( "(%p, %p)\n", ld
, entry
);
533 if (!ld
|| !entry
) return NULL
;
534 return ldap_next_entry( ld
, entry
);
541 /***********************************************************************
542 * ldap_next_reference (WLDAP32.@)
544 * Get the next reference from a result message.
547 * ld [I] Pointer to an LDAP context.
548 * entry [I] Entry returned by a previous call.
551 * Success: The next reference.
555 * The returned entry will be freed when the message is freed.
557 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_reference( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
559 #ifdef HAVE_LDAP_NEXT_REFERENCE
561 TRACE( "(%p, %p)\n", ld
, entry
);
563 if (!ld
|| !entry
) return NULL
;
564 return ldap_next_reference( ld
, entry
);
571 /***********************************************************************
572 * ldap_result (WLDAP32.@)
574 * Get the result of an asynchronous operation.
577 * ld [I] Pointer to an LDAP context.
578 * msgid [I] Message ID of the operation.
579 * all [I] How many results should be returned?
580 * timeout [I] How long to wait for the results?
581 * res [O] Result message for the operation.
584 * Success: One of the following values:
594 * LDAP_RES_SEARCH_ENTRY
595 * LDAP_RES_SEARCH_RESULT
599 * This function returns 0 when the timeout has expired.
602 * A NULL timeout pointer causes the function to block waiting
603 * for results to arrive. A timeout value of 0 causes the function
604 * to immediately return any available results. Free returned results
607 ULONG CDECL
WLDAP32_ldap_result( WLDAP32_LDAP
*ld
, ULONG msgid
, ULONG all
,
608 struct l_timeval
*timeout
, WLDAP32_LDAPMessage
**res
)
610 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
613 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld
, msgid
, all
, timeout
, res
);
615 if (!ld
|| !res
|| msgid
== ~0u) return ~0u;
616 ret
= ldap_result( ld
, msgid
, all
, (struct timeval
*)timeout
, res
);
622 /***********************************************************************
623 * LdapUnicodeToUTF8 (WLDAP32.@)
625 * Convert a wide character string to a UTF8 string.
628 * src [I] Wide character string to convert.
629 * srclen [I] Size of string to convert, in characters.
630 * dst [O] Pointer to a buffer that receives the converted string.
631 * dstlen [I] Size of the destination buffer in characters.
634 * The number of characters written into the destination buffer.
637 * Set dstlen to zero to ask for the required buffer size.
639 int CDECL
LdapUnicodeToUTF8( LPCWSTR src
, int srclen
, LPSTR dst
, int dstlen
)
641 return WideCharToMultiByte( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
, NULL
, NULL
);
644 /***********************************************************************
645 * LdapUTF8ToUnicode (WLDAP32.@)
647 * Convert a UTF8 string to a wide character string.
650 * src [I] UTF8 string to convert.
651 * srclen [I] Size of string to convert, in characters.
652 * dst [O] Pointer to a buffer that receives the converted string.
653 * dstlen [I] Size of the destination buffer in characters.
656 * The number of characters written into the destination buffer.
659 * Set dstlen to zero to ask for the required buffer size.
661 int CDECL
LdapUTF8ToUnicode( LPCSTR src
, int srclen
, LPWSTR dst
, int dstlen
)
663 return MultiByteToWideChar( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
);