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
28 #include "wine/debug.h"
29 #include "winldap_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
33 /***********************************************************************
34 * ldap_abandon (WLDAP32.@)
36 ULONG CDECL
WLDAP32_ldap_abandon( LDAP
*ld
, ULONG msgid
)
38 TRACE( "(%p, %#lx)\n", ld
, msgid
);
41 return map_error( ldap_abandon_ext( CTX(ld
), msgid
, NULL
, NULL
) );
44 /***********************************************************************
45 * ldap_check_filterA (WLDAP32.@)
47 ULONG CDECL
ldap_check_filterA( LDAP
*ld
, char *filter
)
50 WCHAR
*filterW
= NULL
;
52 TRACE( "(%p, %s)\n", ld
, debugstr_a(filter
) );
54 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
55 if (filter
&& !(filterW
= strAtoW( filter
))) return WLDAP32_LDAP_NO_MEMORY
;
57 ret
= ldap_check_filterW( ld
, filterW
);
63 /***********************************************************************
64 * ldap_check_filterW (WLDAP32.@)
66 ULONG CDECL
ldap_check_filterW( LDAP
*ld
, WCHAR
*filter
)
68 TRACE( "(%p, %s)\n", ld
, debugstr_w(filter
) );
70 if (!ld
) return WLDAP32_LDAP_PARAM_ERROR
;
71 return WLDAP32_LDAP_SUCCESS
; /* FIXME: do some checks */
74 /***********************************************************************
75 * ldap_cleanup (WLDAP32.@)
77 ULONG CDECL
ldap_cleanup( HANDLE instance
)
79 TRACE( "(%p)\n", instance
);
80 return WLDAP32_LDAP_SUCCESS
;
83 /***********************************************************************
84 * ldap_conn_from_msg (WLDAP32.@)
86 LDAP
* CDECL
ldap_conn_from_msg( LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
88 TRACE( "(%p, %p)\n", ld
, res
);
90 if (!ld
|| !res
) return NULL
;
91 return ld
; /* FIXME: not always correct */
94 /***********************************************************************
95 * ldap_count_entries (WLDAP32.@)
97 ULONG CDECL
WLDAP32_ldap_count_entries( LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
99 TRACE( "(%p, %p)\n", ld
, res
);
103 return ldap_count_entries( CTX(ld
), MSG(res
) );
106 /***********************************************************************
107 * ldap_count_references (WLDAP32.@)
109 ULONG CDECL
WLDAP32_ldap_count_references( LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
111 TRACE( "(%p, %p)\n", ld
, res
);
114 return ldap_count_references( CTX(ld
), MSG(res
) );
117 static ULONG
get_escape_size( PCHAR src
, ULONG srclen
)
123 for (i
= 0; i
< srclen
; i
++)
125 if ((src
[i
] >= '0' && src
[i
] <= '9') ||
126 (src
[i
] >= 'A' && src
[i
] <= 'Z') ||
127 (src
[i
] >= 'a' && src
[i
] <= 'z'))
136 static void escape_filter_element( char *src
, ULONG srclen
, char *dst
)
139 static const char fmt
[] = "\\%02X";
142 for (i
= 0; i
< srclen
; i
++)
144 if ((src
[i
] >= '0' && src
[i
] <= '9') ||
145 (src
[i
] >= 'A' && src
[i
] <= 'Z') ||
146 (src
[i
] >= 'a' && src
[i
] <= 'z'))
149 d
+= sprintf( d
, fmt
, (unsigned char)src
[i
] );
154 /***********************************************************************
155 * ldap_escape_filter_elementA (WLDAP32.@)
157 ULONG CDECL
ldap_escape_filter_elementA( char *src
, ULONG srclen
, char *dst
, ULONG dstlen
)
159 ULONG len
= get_escape_size( src
, srclen
);
161 TRACE( "(%p, %#lx, %p, %#lx)\n", src
, srclen
, dst
, dstlen
);
163 if (!dst
) return len
;
164 if (!src
|| dstlen
< len
) return WLDAP32_LDAP_PARAM_ERROR
;
166 escape_filter_element( src
, srclen
, dst
);
167 return WLDAP32_LDAP_SUCCESS
;
170 /***********************************************************************
171 * ldap_escape_filter_elementW (WLDAP32.@)
173 ULONG CDECL
ldap_escape_filter_elementW( char *src
, ULONG srclen
, WCHAR
*dst
, ULONG dstlen
)
175 ULONG len
= get_escape_size( src
, srclen
);
177 TRACE( "(%p, %#lx, %p, %#lx)\n", src
, srclen
, dst
, dstlen
);
179 if (!dst
) return len
;
181 /* no matter what you throw at it, this is what native returns */
182 return WLDAP32_LDAP_PARAM_ERROR
;
185 /***********************************************************************
186 * ldap_first_attributeA (WLDAP32.@)
188 char * CDECL
ldap_first_attributeA( LDAP
*ld
, WLDAP32_LDAPMessage
*entry
, WLDAP32_BerElement
**ber
)
193 TRACE( "(%p, %p, %p)\n", ld
, entry
, ber
);
195 if (!ld
|| !entry
) return NULL
;
197 retW
= ldap_first_attributeW( ld
, entry
, ber
);
200 ret
= strWtoA( retW
);
201 ldap_memfreeW( retW
);
207 /***********************************************************************
208 * ldap_first_attributeW (WLDAP32.@)
210 WCHAR
* CDECL
ldap_first_attributeW( LDAP
*ld
, WLDAP32_LDAPMessage
*entry
, WLDAP32_BerElement
**ptr
)
213 WLDAP32_BerElement
*ber
;
217 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
219 if (ld
&& entry
) retU
= ldap_first_attribute( CTX(ld
), MSG(entry
), &berU
);
225 if (!(ber
= malloc( sizeof(*ber
) )))
227 ld
->ld_errno
= WLDAP32_LDAP_NO_MEMORY
;
228 ldap_memfree( retU
);
232 ber
->opaque
= (char *)berU
;
234 ret
= strUtoW( retU
);
235 ldap_memfree( retU
);
239 /***********************************************************************
240 * ldap_first_entry (WLDAP32.@)
242 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_entry( LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
246 TRACE( "(%p, %p)\n", ld
, res
);
248 if (ld
&& res
&& (msgU
= ldap_first_entry( CTX(ld
), MSG(res
) )))
250 assert( msgU
== MSG(res
) );
256 /***********************************************************************
257 * ldap_first_reference (WLDAP32.@)
259 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_first_reference( LDAP
*ld
, WLDAP32_LDAPMessage
*res
)
263 TRACE( "(%p, %p)\n", ld
, res
);
265 if (ld
&& (msgU
= ldap_first_reference( CTX(ld
), MSG(res
) )))
267 assert( msgU
== MSG(res
) );
273 /***********************************************************************
274 * ldap_memfreeA (WLDAP32.@)
276 void CDECL
ldap_memfreeA( char *block
)
278 TRACE( "(%p)\n", block
);
282 /***********************************************************************
283 * ldap_memfreeW (WLDAP32.@)
285 void CDECL
ldap_memfreeW( WCHAR
*block
)
287 TRACE( "(%p)\n", block
);
291 /***********************************************************************
292 * ldap_msgfree (WLDAP32.@)
294 ULONG CDECL
WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage
*res
)
296 WLDAP32_LDAPMessage
*entry
, *list
= res
;
298 TRACE( "(%p)\n", res
);
300 if (!res
) return WLDAP32_LDAP_SUCCESS
;
302 ldap_msgfree( MSG(res
) );
306 list
= entry
->lm_next
;
310 return WLDAP32_LDAP_SUCCESS
;
313 /***********************************************************************
314 * ldap_next_attributeA (WLDAP32.@)
316 char * CDECL
ldap_next_attributeA( LDAP
*ld
, WLDAP32_LDAPMessage
*entry
, WLDAP32_BerElement
*ptr
)
321 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
323 if (!ld
|| !entry
|| !ptr
) return NULL
;
325 retW
= ldap_next_attributeW( ld
, entry
, ptr
);
328 ret
= strWtoA( retW
);
329 ldap_memfreeW( retW
);
335 /***********************************************************************
336 * ldap_next_attributeW (WLDAP32.@)
338 WCHAR
* CDECL
ldap_next_attributeW( LDAP
*ld
, WLDAP32_LDAPMessage
*entry
, WLDAP32_BerElement
*ptr
)
343 TRACE( "(%p, %p, %p)\n", ld
, entry
, ptr
);
345 if (ld
&& entry
&& ptr
&& (retU
= ldap_next_attribute( CTX(ld
), MSG(entry
), BER(ptr
) )))
347 ret
= strUtoW( retU
);
348 ldap_memfree( retU
);
353 /***********************************************************************
354 * ldap_next_entry (WLDAP32.@)
356 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_entry( LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
358 WLDAP32_LDAPMessage
*msg
= NULL
;
361 TRACE( "(%p, %p)\n", ld
, entry
);
363 if (!ld
|| !entry
) return NULL
;
365 if (entry
->lm_next
) return entry
->lm_next
;
367 msgU
= ldap_next_entry( CTX(ld
), MSG(entry
) );
372 if (!(msg
= calloc( 1, sizeof(*msg
) )))
374 ld
->ld_errno
= WLDAP32_LDAP_NO_MEMORY
;
379 entry
->lm_next
= msg
;
383 /***********************************************************************
384 * ldap_next_reference (WLDAP32.@)
386 WLDAP32_LDAPMessage
* CDECL
WLDAP32_ldap_next_reference( LDAP
*ld
, WLDAP32_LDAPMessage
*entry
)
388 WLDAP32_LDAPMessage
*msg
= NULL
;
391 TRACE( "(%p, %p)\n", ld
, entry
);
393 if (!ld
|| !entry
) return NULL
;
395 if (entry
->lm_next
) return entry
->lm_next
;
397 msgU
= ldap_next_reference( CTX(ld
), MSG(entry
) );
402 if (!(msg
= calloc( 1, sizeof(*msg
) )))
404 ld
->ld_errno
= WLDAP32_LDAP_NO_MEMORY
;
409 entry
->lm_next
= msg
;
413 /***********************************************************************
414 * ldap_result (WLDAP32.@)
416 ULONG CDECL
WLDAP32_ldap_result( LDAP
*ld
, ULONG msgid
, ULONG all
, struct l_timeval
*timeout
, WLDAP32_LDAPMessage
**res
)
418 WLDAP32_LDAPMessage
*msg
;
419 struct timeval timeval
;
420 LDAPMessage
*msgU
= NULL
;
423 TRACE( "(%p, %#lx, %#lx, %p, %p)\n", ld
, msgid
, all
, timeout
, res
);
425 if (ld
&& res
&& msgid
!= ~0u)
429 timeval
.tv_sec
= timeout
->tv_sec
;
430 timeval
.tv_usec
= timeout
->tv_usec
;
433 ret
= ldap_result( CTX(ld
), msgid
, all
, timeout
? &timeval
: NULL
, &msgU
);
439 if (!(msg
= calloc( 1, sizeof(*msg
) )))
442 return WLDAP32_LDAP_NO_MEMORY
;
450 /***********************************************************************
451 * ldap_set_dbg_flags (WLDAP32.@)
453 ULONG CDECL
ldap_set_dbg_flags( ULONG flags
)
455 FIXME( "(0x%lx) stub\n", flags
);
459 /***********************************************************************
460 * LdapUnicodeToUTF8 (WLDAP32.@)
462 int CDECL
LdapUnicodeToUTF8( const WCHAR
*src
, int srclen
, char *dst
, int dstlen
)
464 return WideCharToMultiByte( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
, NULL
, NULL
);
467 /***********************************************************************
468 * LdapUTF8ToUnicode (WLDAP32.@)
470 int CDECL
LdapUTF8ToUnicode( const char *src
, int srclen
, WCHAR
*dst
, int dstlen
)
472 return MultiByteToWideChar( CP_UTF8
, 0, src
, srclen
, dst
, dstlen
);