2 * Unix interface for libldap
4 * Copyright 2021 Hans Leidekker for CodeWeavers
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
34 #ifdef HAVE_SASL_SASL_H
35 # include <sasl/sasl.h>
39 #define WIN32_NO_STATUS
44 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
49 C_ASSERT( sizeof(BerValueU
) == sizeof(BerValue
) );
50 C_ASSERT( sizeof(LDAPModU
) == sizeof(LDAPMod
) );
51 C_ASSERT( sizeof(LDAPControlU
) == sizeof(LDAPControl
) );
52 C_ASSERT( sizeof(LDAPSortKeyU
) == sizeof(LDAPSortKey
) );
53 C_ASSERT( sizeof(LDAPVLVInfoU
) == sizeof(LDAPVLVInfo
) );
54 C_ASSERT( sizeof(LDAPAPIInfoU
) == sizeof(LDAPAPIInfo
) );
55 C_ASSERT( sizeof(LDAPAPIFeatureInfoU
) == sizeof(LDAPAPIFeatureInfo
) );
56 C_ASSERT( sizeof(struct timevalU
) == sizeof(struct timeval
) );
58 #define WLDAP32_LBER_ERROR (~0l)
60 static LDAPMod
*nullmods
[] = { NULL
};
62 static const struct ldap_callbacks
*callbacks
;
64 static void * CDECL
wrap_ber_alloc_t( int options
)
66 return ber_alloc_t( options
);
69 static void CDECL
wrap_ber_bvecfree( struct bervalU
**berval
)
71 ber_bvecfree( (struct berval
**)berval
);
74 static void CDECL
wrap_ber_bvfree( struct bervalU
*berval
)
76 ber_bvfree( (struct berval
*)berval
);
79 static unsigned int CDECL
wrap_ber_first_element( void *ber
, ULONG
*ret_len
, char **last
)
84 if ((ret
= ber_first_element( ber
, &len
, last
)) == LBER_ERROR
) return WLDAP32_LBER_ERROR
;
87 ERR( "ret too large\n" );
88 return WLDAP32_LBER_ERROR
;
92 ERR( "len too large\n" );
93 return WLDAP32_LBER_ERROR
;
100 static int CDECL
wrap_ber_flatten( void *ber
, struct bervalU
**berval
)
102 return ber_flatten( ber
, (struct berval
**)berval
);
105 static void CDECL
wrap_ber_free( void *ber
, int freebuf
)
107 ber_free( ber
, freebuf
);
110 static void * CDECL
wrap_ber_init( struct bervalU
*berval
)
112 return ber_init( (struct berval
*)berval
);
115 static unsigned int CDECL
wrap_ber_next_element( void *ber
, unsigned int *ret_len
, char *last
)
120 if ((ret
= ber_next_element( ber
, &len
, last
)) == LBER_ERROR
) return WLDAP32_LBER_ERROR
;
123 ERR( "ret too large\n" );
124 return WLDAP32_LBER_ERROR
;
128 ERR( "len too large\n" );
129 return WLDAP32_LBER_ERROR
;
136 static unsigned int CDECL
wrap_ber_peek_tag( void *ber
, unsigned int *ret_len
)
141 if ((ret
= ber_peek_tag( ber
, &len
)) == LBER_ERROR
) return WLDAP32_LBER_ERROR
;
144 ERR( "len too large\n" );
145 return WLDAP32_LBER_ERROR
;
152 static unsigned int CDECL
wrap_ber_skip_tag( void *ber
, unsigned int *ret_len
)
157 if ((ret
= ber_skip_tag( ber
, &len
)) == LBER_ERROR
) return WLDAP32_LBER_ERROR
;
160 ERR( "len too large\n" );
161 return WLDAP32_LBER_ERROR
;
168 static int WINAPIV
wrap_ber_printf( void *ber
, char *fmt
, ... )
173 assert( strlen(fmt
) == 1 );
175 __ms_va_start( args
, fmt
);
182 int i
= va_arg( args
, int );
183 ret
= ber_printf( ber
, fmt
, i
);
189 char *str
= va_arg( args
, char * );
190 ret
= ber_printf( ber
, fmt
, str
);
195 unsigned int tag
= va_arg( args
, unsigned int );
196 ret
= ber_printf( ber
, fmt
, tag
);
201 char **array
= va_arg( args
, char ** );
202 ret
= ber_printf( ber
, fmt
, array
);
207 struct berval
**array
= va_arg( args
, struct berval
** );
208 ret
= ber_printf( ber
, fmt
, array
);
213 char *str
= va_arg( args
, char * );
214 int len
= va_arg( args
, int );
215 ret
= ber_printf( ber
, fmt
, str
, len
);
223 ret
= ber_printf( ber
, fmt
);
233 static int WINAPIV
wrap_ber_scanf( void *ber
, char *fmt
, ... )
238 assert( strlen(fmt
) == 1 );
240 __ms_va_start( args
, fmt
);
245 char **str
= va_arg( args
, char ** );
246 ret
= ber_scanf( ber
, fmt
, str
);
253 int *i
= va_arg( args
, int * );
254 ret
= ber_scanf( ber
, fmt
, i
);
259 unsigned int *tag
= va_arg( args
, unsigned int * );
260 ret
= ber_scanf( ber
, fmt
, tag
);
265 char ***array
= va_arg( args
, char *** );
266 ret
= ber_scanf( ber
, fmt
, array
);
271 char **str
= va_arg( args
, char ** );
272 int *len
= va_arg( args
, int * );
273 ret
= ber_scanf( ber
, fmt
, str
, len
);
278 struct berval
**berval
= va_arg( args
, struct berval
** );
279 ret
= ber_scanf( ber
, fmt
, berval
);
284 struct berval
***array
= va_arg( args
, struct berval
*** );
285 ret
= ber_scanf( ber
, fmt
, array
);
294 ret
= ber_scanf( ber
, fmt
);
304 static int CDECL
wrap_ldap_abandon_ext( void *ld
, int msgid
, LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
)
306 return ldap_abandon_ext( ld
, msgid
, (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
);
309 static int CDECL
wrap_ldap_add_ext( void *ld
, const char *dn
, LDAPModU
**attrs
, LDAPControlU
**serverctrls
,
310 LDAPControlU
**clientctrls
, ULONG
*msg
)
313 return ldap_add_ext( ld
, dn
? dn
: "", attrs
? (LDAPMod
**)attrs
: nullmods
, (LDAPControl
**)serverctrls
,
314 (LDAPControl
**)clientctrls
, msg
? (int *)msg
: &dummy
);
317 static int CDECL
wrap_ldap_add_ext_s( void *ld
, const char *dn
, LDAPModU
**attrs
, LDAPControlU
**serverctrls
,
318 LDAPControlU
**clientctrls
)
320 return ldap_add_ext_s( ld
, dn
? dn
: "", attrs
? (LDAPMod
**)attrs
: nullmods
, (LDAPControl
**)serverctrls
,
321 (LDAPControl
**)clientctrls
);
324 static int CDECL
wrap_ldap_compare_ext( void *ld
, const char *dn
, const char *attrs
, struct bervalU
*value
,
325 LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
, ULONG
*msg
)
328 return ldap_compare_ext( ld
, dn
? dn
: "", attrs
? attrs
: "", (struct berval
*)value
,
329 (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
, msg
? (int *)msg
: &dummy
);
332 static int CDECL
wrap_ldap_compare_ext_s( void *ld
, const char *dn
, const char *attrs
, struct bervalU
*value
,
333 LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
)
335 return ldap_compare_ext_s( ld
, dn
? dn
: "", attrs
? attrs
: "", (struct berval
*)value
,
336 (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
);
339 static void CDECL
wrap_ldap_control_free( LDAPControlU
*control
)
341 ldap_control_free( (LDAPControl
*)control
);
344 static void CDECL
wrap_ldap_controls_free( LDAPControlU
**control
)
346 ldap_controls_free( (LDAPControl
**)control
);
349 static int CDECL
wrap_ldap_count_entries( void *ld
, void *chain
)
351 return ldap_count_entries( ld
, chain
);
354 static int CDECL
wrap_ldap_count_references( void *ld
, void *chain
)
356 return ldap_count_references( ld
, chain
);
359 static int CDECL
wrap_ldap_count_values_len( struct bervalU
**values
)
361 return ldap_count_values_len( (struct berval
**)values
);
364 static int CDECL
wrap_ldap_create_sort_control( void *ld
, LDAPSortKeyU
**keylist
, int critical
, LDAPControlU
**control
)
366 return ldap_create_sort_control( ld
, (LDAPSortKey
**)keylist
, critical
, (LDAPControl
**)control
);
369 static int CDECL
wrap_ldap_create_vlv_control( void *ld
, LDAPVLVInfoU
*info
, LDAPControlU
**control
)
371 return ldap_create_vlv_control( ld
, (LDAPVLVInfo
*)info
, (LDAPControl
**)control
);
374 static int CDECL
wrap_ldap_delete_ext( void *ld
, const char *dn
, LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
,
378 return ldap_delete_ext( ld
, dn
? dn
: "", (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
,
379 msg
? (int *)msg
: &dummy
);
382 static int CDECL
wrap_ldap_delete_ext_s( void *ld
, const char *dn
, LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
)
384 return ldap_delete_ext_s( ld
, dn
? dn
: "", (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
);
387 static char * CDECL
wrap_ldap_dn2ufn( const char *dn
)
389 return ldap_dn2ufn( dn
);
392 static char ** CDECL
wrap_ldap_explode_dn( const char *dn
, int notypes
)
394 return ldap_explode_dn( dn
, notypes
);
397 static int CDECL
wrap_ldap_extended_operation( void *ld
, const char *oid
, struct bervalU
*data
, LDAPControlU
**serverctrls
,
398 LDAPControlU
**clientctrls
, ULONG
*msg
)
401 return ldap_extended_operation( ld
, oid
? oid
: "", (struct berval
*)data
, (LDAPControl
**)serverctrls
,
402 (LDAPControl
**)clientctrls
, msg
? (int *)msg
: &dummy
);
405 static int CDECL
wrap_ldap_extended_operation_s( void *ld
, const char *oid
, struct bervalU
*data
, LDAPControlU
**serverctrls
,
406 LDAPControlU
**clientctrls
, char **retoid
, struct bervalU
**retdata
)
408 return ldap_extended_operation_s( ld
, oid
? oid
: "", (struct berval
*)data
, (LDAPControl
**)serverctrls
,
409 (LDAPControl
**)clientctrls
, retoid
, (struct berval
**)retdata
);
412 static char * CDECL
wrap_ldap_get_dn( void *ld
, void *entry
)
414 return ldap_get_dn( ld
, entry
);
417 static char * CDECL
wrap_ldap_first_attribute( void *ld
, void *entry
, void **ber
)
419 return ldap_first_attribute( ld
, entry
, (BerElement
**)ber
);
422 static void * CDECL
wrap_ldap_first_entry( void *ld
, void *chain
)
424 return ldap_first_entry( ld
, chain
);
427 static void * CDECL
wrap_ldap_first_reference( void *ld
, void *chain
)
429 return ldap_first_reference( ld
, chain
);
432 static int CDECL
wrap_ldap_get_option( void *ld
, int option
, void *value
)
434 return ldap_get_option( ld
, option
, value
);
437 static struct bervalU
** CDECL
wrap_ldap_get_values_len( void *ld
, void *entry
, const char *attr
)
439 return (struct bervalU
**)ldap_get_values_len( ld
, entry
, attr
);
442 static int CDECL
wrap_ldap_initialize( void **ld
, const char *url
)
444 return ldap_initialize( (LDAP
**)ld
, url
);
447 static void CDECL
wrap_ldap_memfree( void *ptr
)
449 return ldap_memfree( ptr
);
452 static void CDECL
wrap_ldap_memvfree( void **ptr
)
454 ldap_memvfree( ptr
);
457 static int CDECL
wrap_ldap_modify_ext( void *ld
, const char *dn
, LDAPModU
**mods
, LDAPControlU
**serverctrls
,
458 LDAPControlU
**clientctrls
, ULONG
*msg
)
461 return ldap_modify_ext( ld
, dn
? dn
: "", mods
? (LDAPMod
**)mods
: nullmods
, (LDAPControl
**)serverctrls
,
462 (LDAPControl
**)clientctrls
, msg
? (int *)msg
: &dummy
);
465 static int CDECL
wrap_ldap_modify_ext_s( void *ld
, const char *dn
, LDAPModU
**mods
, LDAPControlU
**serverctrls
,
466 LDAPControlU
**clientctrls
)
468 return ldap_modify_ext_s( ld
, dn
? dn
: "", mods
? (LDAPMod
**)mods
: nullmods
, (LDAPControl
**)serverctrls
,
469 (LDAPControl
**)clientctrls
);
472 static int CDECL
wrap_ldap_msgfree( void *msg
)
474 return ldap_msgfree( msg
);
477 static char * CDECL
wrap_ldap_next_attribute( void *ld
, void *entry
, void *ber
)
479 return ldap_next_attribute( ld
, entry
, ber
);
482 static void * CDECL
wrap_ldap_next_entry( void *ld
, void *entry
)
484 return ldap_next_entry( ld
, entry
);
487 static void * CDECL
wrap_ldap_next_reference( void *ld
, void *entry
)
489 return ldap_next_reference( ld
, entry
);
492 static int CDECL
wrap_ldap_parse_extended_result( void *ld
, void *result
, char **retoid
, struct bervalU
**retdata
, int free
)
494 return ldap_parse_extended_result( ld
, result
, retoid
, (struct berval
**)retdata
, free
);
497 static int CDECL
wrap_ldap_parse_reference( void *ld
, void *ref
, char ***referrals
, LDAPControlU
***serverctrls
, int free
)
499 return ldap_parse_reference( ld
, ref
, referrals
, (LDAPControl
***)serverctrls
, free
);
502 static int CDECL
wrap_ldap_parse_result( void *ld
, void *res
, int *errcode
, char **matcheddn
, char **errmsg
,
503 char ***referrals
, LDAPControlU
***serverctrls
, int free
)
505 return ldap_parse_result( ld
, res
, errcode
, matcheddn
, errmsg
, referrals
, (LDAPControl
***)serverctrls
, free
);
508 static int CDECL
wrap_ldap_parse_sortresponse_control( void *ld
, LDAPControlU
*ctrl
, int *result
, char **attr
)
510 return ldap_parse_sortresponse_control( ld
, (LDAPControl
*)ctrl
, result
, attr
);
513 static int CDECL
wrap_ldap_parse_vlvresponse_control( void *ld
, LDAPControlU
*ctrls
, int *target_pos
, int *list_count
,
514 struct bervalU
**ctx
, int *errcode
)
516 return ldap_parse_vlvresponse_control( ld
, (LDAPControl
*)ctrls
, target_pos
, list_count
, (struct berval
**)ctx
,
520 static int CDECL
wrap_ldap_rename( void *ld
, const char *dn
, const char *newrdn
, const char *newparent
, int delete,
521 LDAPControlU
**clientctrls
, LDAPControlU
**serverctrls
, ULONG
*msg
)
523 return ldap_rename( ld
, dn
? dn
: "", newrdn
, newparent
, delete, (LDAPControl
**)clientctrls
,
524 (LDAPControl
**)serverctrls
, (int *)msg
);
527 static int CDECL
wrap_ldap_rename_s( void *ld
, const char *dn
, const char *newrdn
, const char *newparent
, int delete,
528 LDAPControlU
**clientctrls
, LDAPControlU
**serverctrls
)
530 return ldap_rename_s( ld
, dn
? dn
: "", newrdn
, newparent
, delete, (LDAPControl
**)clientctrls
,
531 (LDAPControl
**)serverctrls
);
534 static int CDECL
wrap_ldap_result( void *ld
, int msgid
, int all
, struct timevalU
*timeout
, void **result
)
536 return ldap_result( ld
, msgid
, all
, (struct timeval
*)timeout
, (LDAPMessage
**)result
);
539 static int CDECL
wrap_ldap_sasl_bind( void *ld
, const char *dn
, const char *mech
, struct bervalU
*cred
,
540 LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
, int *msgid
)
542 return ldap_sasl_bind( ld
, dn
, mech
, (struct berval
*)cred
, (LDAPControl
**)serverctrls
,
543 (LDAPControl
**)clientctrls
, msgid
);
546 static int CDECL
wrap_ldap_sasl_bind_s( void *ld
, const char *dn
, const char *mech
, struct bervalU
*cred
,
547 LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
, struct bervalU
**servercred
)
549 return ldap_sasl_bind_s( ld
, dn
, mech
, (struct berval
*)cred
, (LDAPControl
**)serverctrls
,
550 (LDAPControl
**)clientctrls
, (struct berval
**)servercred
);
553 static int wrap_sasl_interact( LDAP
*ld
, unsigned int flags
, void *defaults
, void *interact
)
555 #ifdef HAVE_SASL_SASL_H
556 C_ASSERT( sizeof(struct sasl_interactU
) == sizeof(struct sasl_interact
) );
557 return callbacks
->sasl_interact( ld
, flags
, defaults
, interact
);
562 static int CDECL
wrap_ldap_sasl_interactive_bind_s( void *ld
, const char *dn
, const char *mech
, LDAPControlU
**serverctrls
,
563 LDAPControlU
**clientctrls
, unsigned int flags
, void *defaults
)
565 return ldap_sasl_interactive_bind_s( ld
, dn
, mech
, (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
, flags
,
566 wrap_sasl_interact
, defaults
);
569 static int CDECL
wrap_ldap_search_ext( void *ld
, const char *base
, int scope
, const char *filter
, char **attrs
, int attrsonly
,
570 LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
, struct timevalU
*timeout
,
571 int sizelimit
, ULONG
*msg
)
573 return ldap_search_ext( ld
, base
, scope
, filter
, attrs
, attrsonly
, (LDAPControl
**)serverctrls
,
574 (LDAPControl
**)clientctrls
, (struct timeval
*)timeout
, sizelimit
, (int *)msg
);
577 static int CDECL
wrap_ldap_search_ext_s( void *ld
, const char *base
, int scope
, const char *filter
, char **attrs
,
578 int attrsonly
, LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
,
579 struct timevalU
*timeout
, int sizelimit
, void **result
)
581 return ldap_search_ext_s( ld
, base
, scope
, filter
, attrs
, attrsonly
, (LDAPControl
**)serverctrls
,
582 (LDAPControl
**)clientctrls
, (struct timeval
*)timeout
, sizelimit
,
583 (LDAPMessage
**)result
);
586 static int CDECL
wrap_ldap_set_option( void *ld
, int option
, const void *value
)
588 return ldap_set_option( ld
, option
, value
);
591 static int CDECL
wrap_ldap_start_tls_s( void *ld
, LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
)
593 return ldap_start_tls_s( ld
, (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
);
596 static int CDECL
wrap_ldap_unbind_ext( void *ld
, LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
)
598 return ldap_unbind_ext( ld
, (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
);
601 static int CDECL
wrap_ldap_unbind_ext_s( void *ld
, LDAPControlU
**serverctrls
, LDAPControlU
**clientctrls
)
603 return ldap_unbind_ext_s( ld
, (LDAPControl
**)serverctrls
, (LDAPControl
**)clientctrls
);
606 static void CDECL
wrap_ldap_value_free_len( struct bervalU
**values
)
608 ldap_value_free_len( (struct berval
**)values
);
611 static const struct ldap_funcs funcs
=
616 wrap_ber_first_element
,
620 wrap_ber_next_element
,
625 wrap_ldap_abandon_ext
,
628 wrap_ldap_compare_ext
,
629 wrap_ldap_compare_ext_s
,
630 wrap_ldap_control_free
,
631 wrap_ldap_controls_free
,
632 wrap_ldap_count_entries
,
633 wrap_ldap_count_references
,
634 wrap_ldap_count_values_len
,
635 wrap_ldap_create_sort_control
,
636 wrap_ldap_create_vlv_control
,
637 wrap_ldap_delete_ext
,
638 wrap_ldap_delete_ext_s
,
640 wrap_ldap_explode_dn
,
641 wrap_ldap_extended_operation
,
642 wrap_ldap_extended_operation_s
,
644 wrap_ldap_get_option
,
645 wrap_ldap_get_values_len
,
646 wrap_ldap_initialize
,
647 wrap_ldap_first_attribute
,
648 wrap_ldap_first_entry
,
649 wrap_ldap_first_reference
,
652 wrap_ldap_modify_ext
,
653 wrap_ldap_modify_ext_s
,
655 wrap_ldap_next_attribute
,
656 wrap_ldap_next_entry
,
657 wrap_ldap_next_reference
,
658 wrap_ldap_parse_extended_result
,
659 wrap_ldap_parse_reference
,
660 wrap_ldap_parse_result
,
661 wrap_ldap_parse_sortresponse_control
,
662 wrap_ldap_parse_vlvresponse_control
,
667 wrap_ldap_sasl_bind_s
,
668 wrap_ldap_sasl_interactive_bind_s
,
669 wrap_ldap_search_ext
,
670 wrap_ldap_search_ext_s
,
671 wrap_ldap_set_option
,
672 wrap_ldap_start_tls_s
,
673 wrap_ldap_unbind_ext
,
674 wrap_ldap_unbind_ext_s
,
675 wrap_ldap_value_free_len
,
678 NTSTATUS CDECL
__wine_init_unix_lib( HMODULE module
, DWORD reason
, const void *ptr_in
, void *ptr_out
)
680 if (reason
!= DLL_PROCESS_ATTACH
) return STATUS_SUCCESS
;
682 *(const struct ldap_funcs
**)ptr_out
= &funcs
;
683 return STATUS_SUCCESS
;
685 #endif /* HAVE_LDAP */