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