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
21 #include "winldap_private.h"
25 /***********************************************************************
26 * ldap_abandon (WLDAP32.@)
28 * Cancel an asynchronous operation.
31 * ld [I] Pointer to an LDAP context.
32 * msgid [I] ID of the operation to cancel.
35 * Success: LDAP_SUCCESS
36 * Failure: An LDAP error code.
38 ULONG CDECL
WLDAP32_ldap_abandon( WLDAP32_LDAP
*ld
, ULONG msgid
)
40 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
43 TRACE( "(%p, 0x%08x)\n", ld
, msgid
);
46 ret
= map_error( ldap_abandon_ext( ld
, msgid
, NULL
, NULL
));
52 /***********************************************************************
53 * ldap_check_filterA (WLDAP32.@)
55 * See ldap_check_filterW.
57 ULONG CDECL
ldap_check_filterA( WLDAP32_LDAP
*ld
, PCHAR filter
)
60 WCHAR
*filterW
= NULL
;
62 TRACE( "(%p, %s)\n", ld
, debugstr_a(filter
) );
64 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
67 filterW
= strAtoW( filter
);
68 if (!filterW
) return WLDAP32_LDAP_NO_MEMORY
;
71 ret
= ldap_check_filterW( ld
, filterW
);
77 /***********************************************************************
78 * ldap_check_filterW (WLDAP32.@)
80 * Check filter syntax.
83 * ld [I] Pointer to an LDAP context.
84 * filter [I] Filter string.
87 * Success: LDAP_SUCCESS
88 * Failure: An LDAP error code.
90 ULONG CDECL
ldap_check_filterW( WLDAP32_LDAP
*ld
, PWCHAR filter
)
92 TRACE( "(%p, %s)\n", ld
, debugstr_w(filter
) );
94 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
95 return WLDAP32_LDAP_SUCCESS
; /* FIXME: do some checks */
98 /***********************************************************************
99 * ldap_cleanup (WLDAP32.@)
101 ULONG CDECL
ldap_cleanup( HANDLE instance
)
103 TRACE( "(%p)\n", instance
);
104 return WLDAP32_LDAP_SUCCESS
;
107 /***********************************************************************
108 * ldap_conn_from_msg (WLDAP32.@)
110 * Get the LDAP context for a given message.
113 * ld [I] Pointer to an LDAP context.
114 * res [I] LDAP message.
117 * Success: Pointer to an LDAP context.
120 WLDAP32_LDAP
* CDECL
ldap_conn_from_msg( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
122 TRACE( "(%p, %p)\n", ld
, res
);
124 if (!ld
|| !res
) return NULL
;
125 return ld
; /* FIXME: not always correct */
128 /***********************************************************************
129 * ldap_count_entries (WLDAP32.@)
131 * Count the number of entries returned from a search.
134 * ld [I] Pointer to an LDAP context.
135 * res [I] LDAP message.
138 * Success: The number of entries.
141 ULONG CDECL
WLDAP32_ldap_count_entries( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
143 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
146 TRACE( "(%p, %p)\n", ld
, res
);
149 ret
= ldap_count_entries( ld
, res
);
155 /***********************************************************************
156 * ldap_count_references (WLDAP32.@)
158 * Count the number of references returned from a search.
161 * ld [I] Pointer to an LDAP context.
162 * res [I] LDAP message.
165 * Success: The number of references.
168 ULONG CDECL
WLDAP32_ldap_count_references( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
170 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
171 #ifdef HAVE_LDAP_COUNT_REFERENCES
173 TRACE( "(%p, %p)\n", ld
, res
);
176 ret
= ldap_count_references( ld
, res
);
182 static ULONG
get_escape_size( PCHAR src
, ULONG srclen
)
188 for (i
= 0; i
< srclen
; i
++)
190 if ((src
[i
] >= '0' && src
[i
] <= '9') ||
191 (src
[i
] >= 'A' && src
[i
] <= 'Z') ||
192 (src
[i
] >= 'a' && src
[i
] <= 'z'))
201 static void escape_filter_element( PCHAR src
, ULONG srclen
, PCHAR dst
)
204 static const char fmt
[] = "\\%02X";
207 for (i
= 0; i
< srclen
; i
++)
209 if ((src
[i
] >= '0' && src
[i
] <= '9') ||
210 (src
[i
] >= 'A' && src
[i
] <= 'Z') ||
211 (src
[i
] >= 'a' && src
[i
] <= 'z'))
215 sprintf( d
, fmt
, (unsigned char)src
[i
] );
222 /***********************************************************************
223 * ldap_escape_filter_elementA (WLDAP32.@)
225 * See ldap_escape_filter_elementW.
227 ULONG CDECL
ldap_escape_filter_elementA( PCHAR src
, ULONG srclen
, PCHAR dst
, ULONG dstlen
)
231 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src
, srclen
, dst
, dstlen
);
233 len
= get_escape_size( src
, srclen
);
234 if (!dst
) return len
;
236 if (!src
|| dstlen
< len
)
237 return WLDAP32_LDAP_PARAM_ERROR
;
240 escape_filter_element( src
, srclen
, dst
);
241 return WLDAP32_LDAP_SUCCESS
;
245 /***********************************************************************
246 * ldap_escape_filter_elementW (WLDAP32.@)
248 * Escape binary data for safe passing in filters.
251 * src [I] Filter element to be escaped.
252 * srclen [I] Length in bytes of the filter element.
253 * dst [O] Destination buffer for the escaped filter element.
254 * dstlen [I] Length in bytes of the destination buffer.
257 * Success: LDAP_SUCCESS
258 * Failure: An LDAP error code.
260 ULONG CDECL
ldap_escape_filter_elementW( PCHAR src
, ULONG srclen
, PWCHAR dst
, ULONG dstlen
)
264 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src
, srclen
, dst
, dstlen
);
266 len
= get_escape_size( src
, srclen
);
267 if (!dst
) return len
;
269 /* no matter what you throw at it, this is what native returns */
270 return WLDAP32_LDAP_PARAM_ERROR
;
273 /***********************************************************************
274 * ldap_first_attributeA (WLDAP32.@)
276 * See ldap_first_attributeW.
278 PCHAR CDECL
ldap_first_attributeA( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
279 WLDAP32_BerElement
** ptr
)
285 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
287 if (!ld
|| !entry
) return NULL
;
288 retW
= ldap_first_attributeW( ld
, entry
, ptr
);
290 ret
= strWtoA( retW
);
291 ldap_memfreeW( retW
);
297 /***********************************************************************
298 * ldap_first_attributeW (WLDAP32.@)
300 * Get the first attribute for a given entry.
303 * ld [I] Pointer to an LDAP context.
304 * entry [I] Entry to retrieve attribute for.
305 * ptr [O] Position pointer.
308 * Success: Name of the first attribute.
312 * Use ldap_memfree to free the returned string.
314 PWCHAR CDECL
ldap_first_attributeW( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
315 WLDAP32_BerElement
** ptr
)
321 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
323 if (!ld
|| !entry
) return NULL
;
324 retU
= ldap_first_attribute( ld
, entry
, ptr
);
326 ret
= strUtoW( retU
);
327 ldap_memfree( retU
);
333 /***********************************************************************
334 * ldap_first_entry (WLDAP32.@)
336 * Get the first entry from a result message.
339 * ld [I] Pointer to an LDAP context.
340 * res [I] Search result message.
343 * Success: The first entry.
347 * The returned entry will be freed when the message is freed.
349 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_entry( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
353 TRACE( "(%p, %p)\n", ld
, res
);
355 if (!ld
|| !res
) return NULL
;
356 return ldap_first_entry( ld
, res
);
363 /***********************************************************************
364 * ldap_first_reference (WLDAP32.@)
366 * Get the first reference from a result message.
369 * ld [I] Pointer to an LDAP context.
370 * res [I] Search result message.
373 * Success: The first reference.
376 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_reference( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
378 #ifdef HAVE_LDAP_FIRST_REFERENCE
380 TRACE( "(%p, %p)\n", ld
, res
);
382 if (!ld
) return NULL
;
383 return ldap_first_reference( ld
, res
);
390 /***********************************************************************
391 * ldap_memfreeA (WLDAP32.@)
395 void CDECL
ldap_memfreeA( PCHAR block
)
397 TRACE( "(%p)\n", block
);
401 /***********************************************************************
402 * ldap_memfreeW (WLDAP32.@)
404 * Free a block of memory.
407 * block [I] Pointer to memory block to be freed.
409 void CDECL
ldap_memfreeW( PWCHAR block
)
411 TRACE( "(%p)\n", block
);
415 /***********************************************************************
416 * ldap_msgfree (WLDAP32.@)
421 * res [I] Message to be freed.
423 ULONG CDECL
WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage
*res
)
425 ULONG ret
= WLDAP32_LDAP_SUCCESS
;
428 TRACE( "(%p)\n", res
);
435 /***********************************************************************
436 * ldap_next_attributeA (WLDAP32.@)
438 * See ldap_next_attributeW.
440 PCHAR CDECL
ldap_next_attributeA( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
441 WLDAP32_BerElement
*ptr
)
447 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
449 if (!ld
|| !entry
|| !ptr
) return NULL
;
450 retW
= ldap_next_attributeW( ld
, entry
, ptr
);
452 ret
= strWtoA( retW
);
453 ldap_memfreeW( retW
);
459 /***********************************************************************
460 * ldap_next_attributeW (WLDAP32.@)
462 * Get the next attribute for a given entry.
465 * ld [I] Pointer to an LDAP context.
466 * entry [I] Entry to retrieve attribute for.
467 * ptr [I/O] Position pointer.
470 * Success: The name of the next attribute.
474 * Free the returned string after each iteration with ldap_memfree.
475 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
477 PWCHAR CDECL
ldap_next_attributeW( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
478 WLDAP32_BerElement
*ptr
)
484 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
486 if (!ld
|| !entry
|| !ptr
) return NULL
;
487 retU
= ldap_next_attribute( ld
, entry
, ptr
);
489 ret
= strUtoW( retU
);
490 ldap_memfree( retU
);
496 /***********************************************************************
497 * ldap_next_entry (WLDAP32.@)
499 * Get the next entry from a result message.
502 * ld [I] Pointer to an LDAP context.
503 * entry [I] Entry returned by a previous call.
506 * Success: The next entry.
510 * The returned entry will be freed when the message is freed.
512 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_entry( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
516 TRACE( "(%p, %p)\n", ld
, entry
);
518 if (!ld
|| !entry
) return NULL
;
519 return ldap_next_entry( ld
, entry
);
526 /***********************************************************************
527 * ldap_next_reference (WLDAP32.@)
529 * Get the next reference from a result message.
532 * ld [I] Pointer to an LDAP context.
533 * entry [I] Entry returned by a previous call.
536 * Success: The next reference.
540 * The returned entry will be freed when the message is freed.
542 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_reference( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
544 #ifdef HAVE_LDAP_NEXT_REFERENCE
546 TRACE( "(%p, %p)\n", ld
, entry
);
548 if (!ld
|| !entry
) return NULL
;
549 return ldap_next_reference( ld
, entry
);
556 /***********************************************************************
557 * ldap_result (WLDAP32.@)
559 * Get the result of an asynchronous operation.
562 * ld [I] Pointer to an LDAP context.
563 * msgid [I] Message ID of the operation.
564 * all [I] How many results should be returned?
565 * timeout [I] How long to wait for the results?
566 * res [O] Result message for the operation.
569 * Success: One of the following values:
579 * LDAP_RES_SEARCH_ENTRY
580 * LDAP_RES_SEARCH_RESULT
584 * This function returns 0 when the timeout has expired.
587 * A NULL timeout pointer causes the function to block waiting
588 * for results to arrive. A timeout value of 0 causes the function
589 * to immediately return any available results. Free returned results
592 ULONG CDECL
WLDAP32_ldap_result( WLDAP32_LDAP
*ld
, ULONG msgid
, ULONG all
,
593 struct l_timeval
*timeout
, WLDAP32_LDAPMessage
**res
)
595 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
598 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld
, msgid
, all
, timeout
, res
);
600 if (!ld
|| !res
|| msgid
== ~0u) return ~0u;
601 ret
= ldap_result( ld
, msgid
, all
, (struct timeval
*)timeout
, res
);
607 /***********************************************************************
608 * LdapUnicodeToUTF8 (WLDAP32.@)
610 * Convert a wide character string to a UTF8 string.
613 * src [I] Wide character string to convert.
614 * srclen [I] Size of string to convert, in characters.
615 * dst [O] Pointer to a buffer that receives the converted string.
616 * dstlen [I] Size of the destination buffer in characters.
619 * The number of characters written into the destination buffer.
622 * Set dstlen to zero to ask for the required buffer size.
624 int CDECL
LdapUnicodeToUTF8( LPCWSTR src
, int srclen
, LPSTR dst
, int dstlen
)
626 return WideCharToMultiByte( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
, NULL
, NULL
);
629 /***********************************************************************
630 * LdapUTF8ToUnicode (WLDAP32.@)
632 * Convert a UTF8 string to a wide character string.
635 * src [I] UTF8 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
LdapUTF8ToUnicode( LPCSTR src
, int srclen
, LPWSTR dst
, int dstlen
)
648 return MultiByteToWideChar( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
);