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