Create a branch for working on csrss and co.
[reactos.git] / dll / win32 / wldap32 / misc.c
1 /*
2 * WLDAP32 - LDAP support for Wine
3 *
4 * Copyright 2005 Hans Leidekker
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32
33 #ifdef HAVE_LDAP_H
34 #include <ldap.h>
35 #endif
36
37 #include "winldap_private.h"
38 #include "wldap32.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
41
42 /***********************************************************************
43 * ldap_abandon (WLDAP32.@)
44 *
45 * Cancel an asynchronous operation.
46 *
47 * PARAMS
48 * ld [I] Pointer to an LDAP context.
49 * msgid [I] ID of the operation to cancel.
50 *
51 * RETURNS
52 * Success: LDAP_SUCCESS
53 * Failure: An LDAP error code.
54 */
55 ULONG CDECL WLDAP32_ldap_abandon( WLDAP32_LDAP *ld, ULONG msgid )
56 {
57 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
58 #ifdef HAVE_LDAP
59
60 TRACE( "(%p, 0x%08x)\n", ld, msgid );
61
62 if (!ld) return ~0u;
63 ret = map_error( ldap_abandon_ext( ld, msgid, NULL, NULL ));
64
65 #endif
66 return ret;
67 }
68
69 /***********************************************************************
70 * ldap_check_filterA (WLDAP32.@)
71 *
72 * See ldap_check_filterW.
73 */
74 ULONG CDECL ldap_check_filterA( WLDAP32_LDAP *ld, PCHAR filter )
75 {
76 ULONG ret;
77 WCHAR *filterW = NULL;
78
79 TRACE( "(%p, %s)\n", ld, debugstr_a(filter) );
80
81 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
82
83 if (filter) {
84 filterW = strAtoW( filter );
85 if (!filterW) return WLDAP32_LDAP_NO_MEMORY;
86 }
87
88 ret = ldap_check_filterW( ld, filterW );
89
90 strfreeW( filterW );
91 return ret;
92 }
93
94 /***********************************************************************
95 * ldap_check_filterW (WLDAP32.@)
96 *
97 * Check filter syntax.
98 *
99 * PARAMS
100 * ld [I] Pointer to an LDAP context.
101 * filter [I] Filter string.
102 *
103 * RETURNS
104 * Success: LDAP_SUCCESS
105 * Failure: An LDAP error code.
106 */
107 ULONG CDECL ldap_check_filterW( WLDAP32_LDAP *ld, PWCHAR filter )
108 {
109 TRACE( "(%p, %s)\n", ld, debugstr_w(filter) );
110
111 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
112 return WLDAP32_LDAP_SUCCESS; /* FIXME: do some checks */
113 }
114
115 /***********************************************************************
116 * ldap_cleanup (WLDAP32.@)
117 */
118 ULONG CDECL ldap_cleanup( HANDLE instance )
119 {
120 TRACE( "(%p)\n", instance );
121 return WLDAP32_LDAP_SUCCESS;
122 }
123
124 /***********************************************************************
125 * ldap_conn_from_msg (WLDAP32.@)
126 *
127 * Get the LDAP context for a given message.
128 *
129 * PARAMS
130 * ld [I] Pointer to an LDAP context.
131 * res [I] LDAP message.
132 *
133 * RETURNS
134 * Success: Pointer to an LDAP context.
135 * Failure: NULL
136 */
137 WLDAP32_LDAP * CDECL ldap_conn_from_msg( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
138 {
139 TRACE( "(%p, %p)\n", ld, res );
140
141 if (!ld || !res) return NULL;
142 return ld; /* FIXME: not always correct */
143 }
144
145 /***********************************************************************
146 * ldap_count_entries (WLDAP32.@)
147 *
148 * Count the number of entries returned from a search.
149 *
150 * PARAMS
151 * ld [I] Pointer to an LDAP context.
152 * res [I] LDAP message.
153 *
154 * RETURNS
155 * Success: The number of entries.
156 * Failure: ~0u
157 */
158 ULONG CDECL WLDAP32_ldap_count_entries( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
159 {
160 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
161 #ifdef HAVE_LDAP
162
163 TRACE( "(%p, %p)\n", ld, res );
164
165 if (!ld) return ~0u;
166 ret = ldap_count_entries( ld, res );
167
168 #endif
169 return ret;
170 }
171
172 /***********************************************************************
173 * ldap_count_references (WLDAP32.@)
174 *
175 * Count the number of references returned from a search.
176 *
177 * PARAMS
178 * ld [I] Pointer to an LDAP context.
179 * res [I] LDAP message.
180 *
181 * RETURNS
182 * Success: The number of references.
183 * Failure: ~0u
184 */
185 ULONG CDECL WLDAP32_ldap_count_references( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
186 {
187 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
188 #ifdef HAVE_LDAP_COUNT_REFERENCES
189
190 TRACE( "(%p, %p)\n", ld, res );
191
192 if (!ld) return 0;
193 ret = ldap_count_references( ld, res );
194
195 #endif
196 return ret;
197 }
198
199 static ULONG get_escape_size( PCHAR src, ULONG srclen )
200 {
201 ULONG i, size = 0;
202
203 if (src)
204 {
205 for (i = 0; i < srclen; i++)
206 {
207 if ((src[i] >= '0' && src[i] <= '9') ||
208 (src[i] >= 'A' && src[i] <= 'Z') ||
209 (src[i] >= 'a' && src[i] <= 'z'))
210 size++;
211 else
212 size += 3;
213 }
214 }
215 return size + 1;
216 }
217
218 static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst )
219 {
220 ULONG i;
221 static const char fmt[] = "\\%02X";
222 char *d = dst;
223
224 for (i = 0; i < srclen; i++)
225 {
226 if ((src[i] >= '0' && src[i] <= '9') ||
227 (src[i] >= 'A' && src[i] <= 'Z') ||
228 (src[i] >= 'a' && src[i] <= 'z'))
229 *d++ = src[i];
230 else
231 {
232 sprintf( d, fmt, (unsigned char)src[i] );
233 d += 3;
234 }
235 }
236 *++d = 0;
237 }
238
239 /***********************************************************************
240 * ldap_escape_filter_elementA (WLDAP32.@)
241 *
242 * See ldap_escape_filter_elementW.
243 */
244 ULONG CDECL ldap_escape_filter_elementA( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen )
245 {
246 ULONG len;
247
248 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
249
250 len = get_escape_size( src, srclen );
251 if (!dst) return len;
252
253 if (!src || dstlen < len)
254 return WLDAP32_LDAP_PARAM_ERROR;
255 else
256 {
257 escape_filter_element( src, srclen, dst );
258 return WLDAP32_LDAP_SUCCESS;
259 }
260 }
261
262 /***********************************************************************
263 * ldap_escape_filter_elementW (WLDAP32.@)
264 *
265 * Escape binary data for safe passing in filters.
266 *
267 * PARAMS
268 * src [I] Filter element to be escaped.
269 * srclen [I] Length in bytes of the filter element.
270 * dst [O] Destination buffer for the escaped filter element.
271 * dstlen [I] Length in bytes of the destination buffer.
272 *
273 * RETURNS
274 * Success: LDAP_SUCCESS
275 * Failure: An LDAP error code.
276 */
277 ULONG CDECL ldap_escape_filter_elementW( PCHAR src, ULONG srclen, PWCHAR dst, ULONG dstlen )
278 {
279 ULONG len;
280
281 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
282
283 len = get_escape_size( src, srclen );
284 if (!dst) return len;
285
286 /* no matter what you throw at it, this is what native returns */
287 return WLDAP32_LDAP_PARAM_ERROR;
288 }
289
290 /***********************************************************************
291 * ldap_first_attributeA (WLDAP32.@)
292 *
293 * See ldap_first_attributeW.
294 */
295 PCHAR CDECL ldap_first_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
296 WLDAP32_BerElement** ptr )
297 {
298 PCHAR ret = NULL;
299 #ifdef HAVE_LDAP
300 WCHAR *retW;
301
302 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
303
304 if (!ld || !entry) return NULL;
305 retW = ldap_first_attributeW( ld, entry, ptr );
306
307 ret = strWtoA( retW );
308 ldap_memfreeW( retW );
309
310 #endif
311 return ret;
312 }
313
314 /***********************************************************************
315 * ldap_first_attributeW (WLDAP32.@)
316 *
317 * Get the first attribute for a given entry.
318 *
319 * PARAMS
320 * ld [I] Pointer to an LDAP context.
321 * entry [I] Entry to retrieve attribute for.
322 * ptr [O] Position pointer.
323 *
324 * RETURNS
325 * Success: Name of the first attribute.
326 * Failure: NULL
327 *
328 * NOTES
329 * Use ldap_memfree to free the returned string.
330 */
331 PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
332 WLDAP32_BerElement** ptr )
333 {
334 PWCHAR ret = NULL;
335 #ifdef HAVE_LDAP
336 char *retU;
337
338 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
339
340 if (!ld || !entry) return NULL;
341 retU = ldap_first_attribute( ld, entry, ptr );
342
343 ret = strUtoW( retU );
344 ldap_memfree( retU );
345
346 #endif
347 return ret;
348 }
349
350 /***********************************************************************
351 * ldap_first_entry (WLDAP32.@)
352 *
353 * Get the first entry from a result message.
354 *
355 * PARAMS
356 * ld [I] Pointer to an LDAP context.
357 * res [I] Search result message.
358 *
359 * RETURNS
360 * Success: The first entry.
361 * Failure: NULL
362 *
363 * NOTES
364 * The returned entry will be freed when the message is freed.
365 */
366 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
367 {
368 #ifdef HAVE_LDAP
369
370 TRACE( "(%p, %p)\n", ld, res );
371
372 if (!ld || !res) return NULL;
373 return ldap_first_entry( ld, res );
374
375 #else
376 return NULL;
377 #endif
378 }
379
380 /***********************************************************************
381 * ldap_first_reference (WLDAP32.@)
382 *
383 * Get the first reference from a result message.
384 *
385 * PARAMS
386 * ld [I] Pointer to an LDAP context.
387 * res [I] Search result message.
388 *
389 * RETURNS
390 * Success: The first reference.
391 * Failure: NULL
392 */
393 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
394 {
395 #ifdef HAVE_LDAP_FIRST_REFERENCE
396
397 TRACE( "(%p, %p)\n", ld, res );
398
399 if (!ld) return NULL;
400 return ldap_first_reference( ld, res );
401
402 #else
403 return NULL;
404 #endif
405 }
406
407 /***********************************************************************
408 * ldap_memfreeA (WLDAP32.@)
409 *
410 * See ldap_memfreeW.
411 */
412 void CDECL ldap_memfreeA( PCHAR block )
413 {
414 TRACE( "(%p)\n", block );
415 strfreeA( block );
416 }
417
418 /***********************************************************************
419 * ldap_memfreeW (WLDAP32.@)
420 *
421 * Free a block of memory.
422 *
423 * PARAMS
424 * block [I] Pointer to memory block to be freed.
425 */
426 void CDECL ldap_memfreeW( PWCHAR block )
427 {
428 TRACE( "(%p)\n", block );
429 strfreeW( block );
430 }
431
432 /***********************************************************************
433 * ldap_msgfree (WLDAP32.@)
434 *
435 * Free a message.
436 *
437 * PARAMS
438 * res [I] Message to be freed.
439 */
440 ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res )
441 {
442 ULONG ret = WLDAP32_LDAP_SUCCESS;
443 #ifdef HAVE_LDAP
444
445 TRACE( "(%p)\n", res );
446 ldap_msgfree( res );
447
448 #endif
449 return ret;
450 }
451
452 /***********************************************************************
453 * ldap_next_attributeA (WLDAP32.@)
454 *
455 * See ldap_next_attributeW.
456 */
457 PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
458 WLDAP32_BerElement *ptr )
459 {
460 PCHAR ret = NULL;
461 #ifdef HAVE_LDAP
462 WCHAR *retW;
463
464 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
465
466 if (!ld || !entry || !ptr) return NULL;
467 retW = ldap_next_attributeW( ld, entry, ptr );
468
469 ret = strWtoA( retW );
470 ldap_memfreeW( retW );
471
472 #endif
473 return ret;
474 }
475
476 /***********************************************************************
477 * ldap_next_attributeW (WLDAP32.@)
478 *
479 * Get the next attribute for a given entry.
480 *
481 * PARAMS
482 * ld [I] Pointer to an LDAP context.
483 * entry [I] Entry to retrieve attribute for.
484 * ptr [I/O] Position pointer.
485 *
486 * RETURNS
487 * Success: The name of the next attribute.
488 * Failure: NULL
489 *
490 * NOTES
491 * Free the returned string after each iteration with ldap_memfree.
492 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
493 */
494 PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
495 WLDAP32_BerElement *ptr )
496 {
497 PWCHAR ret = NULL;
498 #ifdef HAVE_LDAP
499 char *retU;
500
501 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
502
503 if (!ld || !entry || !ptr) return NULL;
504 retU = ldap_next_attribute( ld, entry, ptr );
505
506 ret = strUtoW( retU );
507 ldap_memfree( retU );
508
509 #endif
510 return ret;
511 }
512
513 /***********************************************************************
514 * ldap_next_entry (WLDAP32.@)
515 *
516 * Get the next entry from a result message.
517 *
518 * PARAMS
519 * ld [I] Pointer to an LDAP context.
520 * entry [I] Entry returned by a previous call.
521 *
522 * RETURNS
523 * Success: The next entry.
524 * Failure: NULL
525 *
526 * NOTES
527 * The returned entry will be freed when the message is freed.
528 */
529 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
530 {
531 #ifdef HAVE_LDAP
532
533 TRACE( "(%p, %p)\n", ld, entry );
534
535 if (!ld || !entry) return NULL;
536 return ldap_next_entry( ld, entry );
537
538 #else
539 return NULL;
540 #endif
541 }
542
543 /***********************************************************************
544 * ldap_next_reference (WLDAP32.@)
545 *
546 * Get the next reference from a result message.
547 *
548 * PARAMS
549 * ld [I] Pointer to an LDAP context.
550 * entry [I] Entry returned by a previous call.
551 *
552 * RETURNS
553 * Success: The next reference.
554 * Failure: NULL
555 *
556 * NOTES
557 * The returned entry will be freed when the message is freed.
558 */
559 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
560 {
561 #ifdef HAVE_LDAP_NEXT_REFERENCE
562
563 TRACE( "(%p, %p)\n", ld, entry );
564
565 if (!ld || !entry) return NULL;
566 return ldap_next_reference( ld, entry );
567
568 #else
569 return NULL;
570 #endif
571 }
572
573 /***********************************************************************
574 * ldap_result (WLDAP32.@)
575 *
576 * Get the result of an asynchronous operation.
577 *
578 * PARAMS
579 * ld [I] Pointer to an LDAP context.
580 * msgid [I] Message ID of the operation.
581 * all [I] How many results should be returned?
582 * timeout [I] How long to wait for the results?
583 * res [O] Result message for the operation.
584 *
585 * RETURNS
586 * Success: One of the following values:
587 *
588 * LDAP_RES_ADD
589 * LDAP_RES_BIND
590 * LDAP_RES_COMPARE
591 * LDAP_RES_DELETE
592 * LDAP_RES_EXTENDED
593 * LDAP_RES_MODIFY
594 * LDAP_RES_MODRDN
595 * LDAP_RES_REFERRAL
596 * LDAP_RES_SEARCH_ENTRY
597 * LDAP_RES_SEARCH_RESULT
598 *
599 * Failure: ~0u
600 *
601 * This function returns 0 when the timeout has expired.
602 *
603 * NOTES
604 * A NULL timeout pointer causes the function to block waiting
605 * for results to arrive. A timeout value of 0 causes the function
606 * to immediately return any available results. Free returned results
607 * with ldap_msgfree.
608 */
609 ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all,
610 struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
611 {
612 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
613 #ifdef HAVE_LDAP
614
615 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld, msgid, all, timeout, res );
616
617 if (!ld || !res || msgid == ~0u) return ~0u;
618 ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res );
619
620 #endif
621 return ret;
622 }
623
624 /***********************************************************************
625 * LdapUnicodeToUTF8 (WLDAP32.@)
626 *
627 * Convert a wide character string to a UTF8 string.
628 *
629 * PARAMS
630 * src [I] Wide character string to convert.
631 * srclen [I] Size of string to convert, in characters.
632 * dst [O] Pointer to a buffer that receives the converted string.
633 * dstlen [I] Size of the destination buffer in characters.
634 *
635 * RETURNS
636 * The number of characters written into the destination buffer.
637 *
638 * NOTES
639 * Set dstlen to zero to ask for the required buffer size.
640 */
641 int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen )
642 {
643 return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL );
644 }
645
646 /***********************************************************************
647 * LdapUTF8ToUnicode (WLDAP32.@)
648 *
649 * Convert a UTF8 string to a wide character string.
650 *
651 * PARAMS
652 * src [I] UTF8 string to convert.
653 * srclen [I] Size of string to convert, in characters.
654 * dst [O] Pointer to a buffer that receives the converted string.
655 * dstlen [I] Size of the destination buffer in characters.
656 *
657 * RETURNS
658 * The number of characters written into the destination buffer.
659 *
660 * NOTES
661 * Set dstlen to zero to ask for the required buffer size.
662 */
663 int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen )
664 {
665 return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen );
666 }