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'))
214 d
+= sprintf( d
, fmt
, (unsigned char)src
[i
] );
219 /***********************************************************************
220 * ldap_escape_filter_elementA (WLDAP32.@)
222 * See ldap_escape_filter_elementW.
224 ULONG CDECL
ldap_escape_filter_elementA( PCHAR src
, ULONG srclen
, PCHAR dst
, ULONG dstlen
)
228 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src
, srclen
, dst
, dstlen
);
230 len
= get_escape_size( src
, srclen
);
231 if (!dst
) return len
;
233 if (!src
|| dstlen
< len
)
234 return WLDAP32_LDAP_PARAM_ERROR
;
237 escape_filter_element( src
, srclen
, dst
);
238 return WLDAP32_LDAP_SUCCESS
;
242 /***********************************************************************
243 * ldap_escape_filter_elementW (WLDAP32.@)
245 * Escape binary data for safe passing in filters.
248 * src [I] Filter element to be escaped.
249 * srclen [I] Length in bytes of the filter element.
250 * dst [O] Destination buffer for the escaped filter element.
251 * dstlen [I] Length in bytes of the destination buffer.
254 * Success: LDAP_SUCCESS
255 * Failure: An LDAP error code.
257 ULONG CDECL
ldap_escape_filter_elementW( PCHAR src
, ULONG srclen
, PWCHAR dst
, ULONG dstlen
)
261 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src
, srclen
, dst
, dstlen
);
263 len
= get_escape_size( src
, srclen
);
264 if (!dst
) return len
;
266 /* no matter what you throw at it, this is what native returns */
267 return WLDAP32_LDAP_PARAM_ERROR
;
270 /***********************************************************************
271 * ldap_first_attributeA (WLDAP32.@)
273 * See ldap_first_attributeW.
275 PCHAR CDECL
ldap_first_attributeA( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
276 WLDAP32_BerElement
** ptr
)
282 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
284 if (!ld
|| !entry
) return NULL
;
285 retW
= ldap_first_attributeW( ld
, entry
, ptr
);
287 ret
= strWtoA( retW
);
288 ldap_memfreeW( retW
);
294 /***********************************************************************
295 * ldap_first_attributeW (WLDAP32.@)
297 * Get the first attribute for a given entry.
300 * ld [I] Pointer to an LDAP context.
301 * entry [I] Entry to retrieve attribute for.
302 * ptr [O] Position pointer.
305 * Success: Name of the first attribute.
309 * Use ldap_memfree to free the returned string.
311 PWCHAR CDECL
ldap_first_attributeW( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
312 WLDAP32_BerElement
** ptr
)
318 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
320 if (!ld
|| !entry
) return NULL
;
321 retU
= ldap_first_attribute( ld
, entry
, ptr
);
323 ret
= strUtoW( retU
);
324 ldap_memfree( retU
);
330 /***********************************************************************
331 * ldap_first_entry (WLDAP32.@)
333 * Get the first entry from a result message.
336 * ld [I] Pointer to an LDAP context.
337 * res [I] Search result message.
340 * Success: The first entry.
344 * The returned entry will be freed when the message is freed.
346 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_entry( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
350 TRACE( "(%p, %p)\n", ld
, res
);
352 if (!ld
|| !res
) return NULL
;
353 return ldap_first_entry( ld
, res
);
360 /***********************************************************************
361 * ldap_first_reference (WLDAP32.@)
363 * Get the first reference from a result message.
366 * ld [I] Pointer to an LDAP context.
367 * res [I] Search result message.
370 * Success: The first reference.
373 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_reference( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
375 #ifdef HAVE_LDAP_FIRST_REFERENCE
377 TRACE( "(%p, %p)\n", ld
, res
);
379 if (!ld
) return NULL
;
380 return ldap_first_reference( ld
, res
);
387 /***********************************************************************
388 * ldap_memfreeA (WLDAP32.@)
392 void CDECL
ldap_memfreeA( PCHAR block
)
394 TRACE( "(%p)\n", block
);
398 /***********************************************************************
399 * ldap_memfreeW (WLDAP32.@)
401 * Free a block of memory.
404 * block [I] Pointer to memory block to be freed.
406 void CDECL
ldap_memfreeW( PWCHAR block
)
408 TRACE( "(%p)\n", block
);
412 /***********************************************************************
413 * ldap_msgfree (WLDAP32.@)
418 * res [I] Message to be freed.
420 ULONG CDECL
WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage
*res
)
422 ULONG ret
= WLDAP32_LDAP_SUCCESS
;
425 TRACE( "(%p)\n", res
);
432 /***********************************************************************
433 * ldap_next_attributeA (WLDAP32.@)
435 * See ldap_next_attributeW.
437 PCHAR CDECL
ldap_next_attributeA( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
438 WLDAP32_BerElement
*ptr
)
444 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
446 if (!ld
|| !entry
|| !ptr
) return NULL
;
447 retW
= ldap_next_attributeW( ld
, entry
, ptr
);
449 ret
= strWtoA( retW
);
450 ldap_memfreeW( retW
);
456 /***********************************************************************
457 * ldap_next_attributeW (WLDAP32.@)
459 * Get the next attribute for a given entry.
462 * ld [I] Pointer to an LDAP context.
463 * entry [I] Entry to retrieve attribute for.
464 * ptr [I/O] Position pointer.
467 * Success: The name of the next attribute.
471 * Free the returned string after each iteration with ldap_memfree.
472 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
474 PWCHAR CDECL
ldap_next_attributeW( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
,
475 WLDAP32_BerElement
*ptr
)
481 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
483 if (!ld
|| !entry
|| !ptr
) return NULL
;
484 retU
= ldap_next_attribute( ld
, entry
, ptr
);
486 ret
= strUtoW( retU
);
487 ldap_memfree( retU
);
493 /***********************************************************************
494 * ldap_next_entry (WLDAP32.@)
496 * Get the next entry from a result message.
499 * ld [I] Pointer to an LDAP context.
500 * entry [I] Entry returned by a previous call.
503 * Success: The next entry.
507 * The returned entry will be freed when the message is freed.
509 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_entry( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
513 TRACE( "(%p, %p)\n", ld
, entry
);
515 if (!ld
|| !entry
) return NULL
;
516 return ldap_next_entry( ld
, entry
);
523 /***********************************************************************
524 * ldap_next_reference (WLDAP32.@)
526 * Get the next reference from a result message.
529 * ld [I] Pointer to an LDAP context.
530 * entry [I] Entry returned by a previous call.
533 * Success: The next reference.
537 * The returned entry will be freed when the message is freed.
539 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_reference( WLDAP32_LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
541 #ifdef HAVE_LDAP_NEXT_REFERENCE
543 TRACE( "(%p, %p)\n", ld
, entry
);
545 if (!ld
|| !entry
) return NULL
;
546 return ldap_next_reference( ld
, entry
);
553 /***********************************************************************
554 * ldap_result (WLDAP32.@)
556 * Get the result of an asynchronous operation.
559 * ld [I] Pointer to an LDAP context.
560 * msgid [I] Message ID of the operation.
561 * all [I] How many results should be returned?
562 * timeout [I] How long to wait for the results?
563 * res [O] Result message for the operation.
566 * Success: One of the following values:
576 * LDAP_RES_SEARCH_ENTRY
577 * LDAP_RES_SEARCH_RESULT
581 * This function returns 0 when the timeout has expired.
584 * A NULL timeout pointer causes the function to block waiting
585 * for results to arrive. A timeout value of 0 causes the function
586 * to immediately return any available results. Free returned results
589 ULONG CDECL
WLDAP32_ldap_result( WLDAP32_LDAP
*ld
, ULONG msgid
, ULONG all
,
590 struct l_timeval
*timeout
, WLDAP32_LDAPMessage
**res
)
592 ULONG ret
= WLDAP32_LDAP_NOT_SUPPORTED
;
595 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld
, msgid
, all
, timeout
, res
);
597 if (!ld
|| !res
|| msgid
== ~0u) return ~0u;
598 ret
= ldap_result( ld
, msgid
, all
, (struct timeval
*)timeout
, res
);
604 /***********************************************************************
605 * LdapUnicodeToUTF8 (WLDAP32.@)
607 * Convert a wide character string to a UTF8 string.
610 * src [I] Wide character string to convert.
611 * srclen [I] Size of string to convert, in characters.
612 * dst [O] Pointer to a buffer that receives the converted string.
613 * dstlen [I] Size of the destination buffer in characters.
616 * The number of characters written into the destination buffer.
619 * Set dstlen to zero to ask for the required buffer size.
621 int CDECL
LdapUnicodeToUTF8( LPCWSTR src
, int srclen
, LPSTR dst
, int dstlen
)
623 return WideCharToMultiByte( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
, NULL
, NULL
);
626 /***********************************************************************
627 * LdapUTF8ToUnicode (WLDAP32.@)
629 * Convert a UTF8 string to a wide character string.
632 * src [I] UTF8 string to convert.
633 * srclen [I] Size of string to convert, in characters.
634 * dst [O] Pointer to a buffer that receives the converted string.
635 * dstlen [I] Size of the destination buffer in characters.
638 * The number of characters written into the destination buffer.
641 * Set dstlen to zero to ask for the required buffer size.
643 int CDECL
LdapUTF8ToUnicode( LPCSTR src
, int srclen
, LPWSTR dst
, int dstlen
)
645 return MultiByteToWideChar( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
);