2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2022 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 /* ldap-pvt.h - Header for ldap_pvt_ functions.
17 * These are meant to be internal to OpenLDAP Software.
23 #include <openldap.h> /* get public interfaces */
24 #include <lber.h> /* get ber_slen_t */
25 #include <lber_pvt.h> /* get Sockbuf_Buf */
30 ldap_pvt_url_scheme2proto
LDAP_P((
33 ldap_pvt_url_scheme2tls
LDAP_P((
36 ldap_pvt_url_scheme2proxied
LDAP_P((
40 ldap_pvt_url_scheme_port
LDAP_P((
43 struct ldap_url_desc
; /* avoid pulling in <ldap.h> */
45 #define LDAP_PVT_URL_PARSE_NONE (0x00U)
46 #define LDAP_PVT_URL_PARSE_NOEMPTY_HOST (0x01U)
47 #define LDAP_PVT_URL_PARSE_DEF_PORT (0x02U)
48 #define LDAP_PVT_URL_PARSE_NOEMPTY_DN (0x04U)
49 #define LDAP_PVT_URL_PARSE_NODEF_SCOPE (0x08U)
50 #define LDAP_PVT_URL_PARSE_HISTORIC (LDAP_PVT_URL_PARSE_NODEF_SCOPE | \
51 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | \
52 LDAP_PVT_URL_PARSE_DEF_PORT)
55 ldap_url_parse_ext
LDAP_P((
57 struct ldap_url_desc
**ludpp
,
60 LDAP_F (int) ldap_url_parselist
LDAP_P(( /* deprecated, use ldap_url_parselist_ext() */
61 struct ldap_url_desc
**ludlist
,
64 LDAP_F (int) ldap_url_parselist_ext
LDAP_P((
65 struct ldap_url_desc
**ludlist
,
70 LDAP_F (char *) ldap_url_list2urls
LDAP_P((
71 struct ldap_url_desc
*ludlist
));
73 LDAP_F (void) ldap_free_urllist
LDAP_P((
74 struct ldap_url_desc
*ludlist
));
76 LDAP_F (int) ldap_pvt_scope2bv
LDAP_P ((
77 int scope
, struct berval
*bv
));
79 LDAP_F (LDAP_CONST
char *) ldap_pvt_scope2str
LDAP_P ((
82 LDAP_F (int) ldap_pvt_bv2scope
LDAP_P ((
85 LDAP_F (int) ldap_pvt_str2scope
LDAP_P ((
89 ldap_pvt_ctime
LDAP_P((
93 # if defined( HAVE_GMTIME_R )
95 # define ldap_pvt_gmtime(timep, result) gmtime_r((timep), (result))
98 ldap_pvt_gmtime
LDAP_P((
99 LDAP_CONST
time_t *timep
,
100 struct tm
*result
));
103 # if defined( HAVE_LOCALTIME_R )
104 # define USE_LOCALTIME_R
105 # define ldap_pvt_localtime(timep, result) localtime_r((timep), (result))
107 LDAP_F( struct tm
* )
108 ldap_pvt_localtime
LDAP_P((
109 LDAP_CONST
time_t *timep
,
110 struct tm
*result
));
113 #if defined( USE_GMTIME_R ) && defined( USE_LOCALTIME_R )
114 # define ldap_pvt_gmtime_lock() (0)
115 # define ldap_pvt_gmtime_unlock() (0)
118 ldap_pvt_gmtime_lock
LDAP_P(( void ));
121 ldap_pvt_gmtime_unlock
LDAP_P(( void ));
122 #endif /* USE_GMTIME_R && USE_LOCALTIME_R */
124 /* Get current time as a structured time */
127 ldap_pvt_gettime
LDAP_P(( struct lutil_tm
* ));
130 #define gettimeofday(tv,tz) ldap_pvt_gettimeofday(tv,tz)
133 ldap_pvt_gettimeofday
LDAP_P(( struct timeval
*tv
, void *unused
));
134 #ifndef CLOCK_REALTIME
135 #define CLOCK_REALTIME 0
137 #define clock_gettime(clkid,tv) ldap_pvt_clock_gettime(clkid,tv)
140 ldap_pvt_clock_gettime
LDAP_P(( int clkid
, struct timespec
*tv
));
143 /* use this macro to allocate buffer for ldap_pvt_csnstr */
144 #define LDAP_PVT_CSNSTR_BUFSIZE 64
146 ldap_pvt_csnstr( char *buf
, size_t len
, unsigned int replica
, unsigned int mod
);
148 LDAP_F( char *) ldap_pvt_get_fqdn
LDAP_P(( char * ));
150 struct hostent
; /* avoid pulling in <netdb.h> */
153 ldap_pvt_gethostbyname_a
LDAP_P((
155 struct hostent
*resbuf
,
157 struct hostent
**result
,
161 ldap_pvt_gethostbyaddr_a
LDAP_P((
165 struct hostent
*resbuf
,
167 struct hostent
**result
,
173 ldap_pvt_get_hname
LDAP_P((
174 const struct sockaddr
* sa
,
181 #define LDAP_IPADDRLEN (MAXPATHLEN + sizeof("PATH="))
182 #elif defined(LDAP_PF_INET6)
183 #define LDAP_IPADDRLEN sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")
185 #define LDAP_IPADDRLEN sizeof("IP=255.255.255.255:65336")
191 ldap_pvt_sockaddrstr
LDAP_P((
199 ldap_charray_add
LDAP_P((
204 ldap_charray_merge
LDAP_P((
209 ldap_charray_free
LDAP_P(( char **a
));
212 ldap_charray_inlist
LDAP_P((
217 ldap_charray_dup
LDAP_P(( char **a
));
220 ldap_str2charray
LDAP_P((
222 const char *brkstr
));
225 ldap_charray2str
LDAP_P((
226 char **array
, const char* sep
));
230 #ifdef LDAP_AVA_NULL /* in ldap.h */
231 LDAP_F( void ) ldap_rdnfree_x
LDAP_P(( LDAPRDN rdn
, void *ctx
));
232 LDAP_F( void ) ldap_dnfree_x
LDAP_P(( LDAPDN dn
, void *ctx
));
234 LDAP_F( int ) ldap_bv2dn_x
LDAP_P((
235 struct berval
*bv
, LDAPDN
*dn
, unsigned flags
, void *ctx
));
236 LDAP_F( int ) ldap_dn2bv_x
LDAP_P((
237 LDAPDN dn
, struct berval
*bv
, unsigned flags
, void *ctx
));
238 LDAP_F( int ) ldap_bv2rdn_x
LDAP_P((
239 struct berval
*, LDAPRDN
*, char **, unsigned flags
, void *ctx
));
240 LDAP_F( int ) ldap_rdn2bv_x
LDAP_P((
241 LDAPRDN rdn
, struct berval
*bv
, unsigned flags
, void *ctx
));
242 #endif /* LDAP_AVA_NULL */
245 LDAP_F (void) ldap_pvt_hex_unescape
LDAP_P(( char *s
));
248 * these macros assume 'x' is an ASCII x
249 * and assume the "C" locale
251 #define LDAP_ASCII(c) (!((c) & 0x80))
252 #define LDAP_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
253 #define LDAP_DIGIT(c) ((c) >= '0' && (c) <= '9')
254 #define LDAP_LOWER(c) ((c) >= 'a' && (c) <= 'z')
255 #define LDAP_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
256 #define LDAP_ALPHA(c) (LDAP_LOWER(c) || LDAP_UPPER(c))
257 #define LDAP_ALNUM(c) (LDAP_ALPHA(c) || LDAP_DIGIT(c))
259 #define LDAP_LDH(c) (LDAP_ALNUM(c) || (c) == '-')
261 #define LDAP_HEXLOWER(c) ((c) >= 'a' && (c) <= 'f')
262 #define LDAP_HEXUPPER(c) ((c) >= 'A' && (c) <= 'F')
263 #define LDAP_HEX(c) (LDAP_DIGIT(c) || \
264 LDAP_HEXLOWER(c) || LDAP_HEXUPPER(c))
269 ldap_pvt_put_control
LDAP_P((
270 const struct ldapcontrol
*c
,
272 LDAP_F (int) ldap_pvt_get_controls
LDAP_P((
274 struct ldapcontrol
***ctrlsp
));
276 #ifdef HAVE_CYRUS_SASL
278 struct sasl_security_properties
; /* avoid pulling in <sasl.h> */
279 LDAP_F (int) ldap_pvt_sasl_secprops
LDAP_P((
281 struct sasl_security_properties
*secprops
));
282 LDAP_F (void) ldap_pvt_sasl_secprops_unparse
LDAP_P((
283 struct sasl_security_properties
*secprops
,
284 struct berval
*out
));
286 LDAP_F (void *) ldap_pvt_sasl_mutex_new
LDAP_P((void));
287 LDAP_F (int) ldap_pvt_sasl_mutex_lock
LDAP_P((void *mutex
));
288 LDAP_F (int) ldap_pvt_sasl_mutex_unlock
LDAP_P((void *mutex
));
289 LDAP_F (void) ldap_pvt_sasl_mutex_dispose
LDAP_P((void *mutex
));
291 LDAP_F (int) ldap_pvt_sasl_cbinding_parse
LDAP_P(( const char *arg
));
292 LDAP_F (void *) ldap_pvt_sasl_cbinding
LDAP_P(( void *ssl
, int type
,
294 #endif /* HAVE_CYRUS_SASL */
296 struct sockbuf
; /* avoid pulling in <lber.h> */
297 LDAP_F (int) ldap_pvt_sasl_install
LDAP_P(( struct sockbuf
*, void * ));
298 LDAP_F (void) ldap_pvt_sasl_remove
LDAP_P(( struct sockbuf
* ));
301 * SASL encryption support for LBER Sockbufs
304 struct sb_sasl_generic_data
;
306 struct sb_sasl_generic_ops
{
307 void (*init
)(struct sb_sasl_generic_data
*p
,
310 ber_len_t
*max_recv
);
311 ber_int_t (*encode
)(struct sb_sasl_generic_data
*p
,
315 ber_int_t (*decode
)(struct sb_sasl_generic_data
*p
,
316 const Sockbuf_Buf
*src
,
318 void (*reset_buf
)(struct sb_sasl_generic_data
*p
,
320 void (*fini
)(struct sb_sasl_generic_data
*p
);
323 struct sb_sasl_generic_install
{
324 const struct sb_sasl_generic_ops
*ops
;
328 struct sb_sasl_generic_data
{
329 const struct sb_sasl_generic_ops
*ops
;
331 Sockbuf_IO_Desc
*sbiod
;
335 Sockbuf_Buf sec_buf_in
;
339 #define LDAP_PVT_SASL_PARTIAL_WRITE 1
342 #ifndef LDAP_PVT_SASL_LOCAL_SSF
343 #define LDAP_PVT_SASL_LOCAL_SSF 71 /* SSF for Unix Domain Sockets */
344 #endif /* ! LDAP_PVT_SASL_LOCAL_SSF */
351 LDAP_F ( int ) ldap_pvt_discard
LDAP_P((
352 struct ldap
*ld
, ber_int_t msgid
));
356 ldap_pvt_conf_option
LDAP_P((
357 char *cmd
, char *opt
, int userconf
));
361 ldap_parse_ldif_record_x
LDAP_P((
363 unsigned long linenum
,
364 struct ldifrecord
*lr
,
370 LDAP_F( BerElement
* )
371 ldap_get_message_ber
LDAP_P((
375 LDAP_F (int) ldap_open_internal_connection
LDAP_P((
376 struct ldap
**ldp
, ber_socket_t
*fdp
));
379 LDAP_F (int) ldap_pvt_sasl_generic_install
LDAP_P(( Sockbuf
*sb
,
380 struct sb_sasl_generic_install
*install_arg
));
381 LDAP_F (void) ldap_pvt_sasl_generic_remove
LDAP_P(( Sockbuf
*sb
));
384 LDAP_F( int ) ldap_pvt_put_filter
LDAP_P((
389 ldap_pvt_find_wildcard
LDAP_P(( const char *s
));
392 ldap_pvt_filter_value_unescape
LDAP_P(( char *filter
));
395 ldap_bv2escaped_filter_value_len
LDAP_P(( struct berval
*in
));
398 ldap_bv2escaped_filter_value_x
LDAP_P(( struct berval
*in
, struct berval
*out
,
399 int inplace
, void *ctx
));
401 LDAP_F (int) ldap_pvt_search
LDAP_P((
403 LDAP_CONST
char *base
,
405 LDAP_CONST
char *filter
,
408 struct ldapcontrol
**sctrls
,
409 struct ldapcontrol
**cctrls
,
410 struct timeval
*timeout
,
415 LDAP_F(int) ldap_pvt_search_s
LDAP_P((
417 LDAP_CONST
char *base
,
419 LDAP_CONST
char *filter
,
422 struct ldapcontrol
**sctrls
,
423 struct ldapcontrol
**cctrls
,
424 struct timeval
*timeout
,
427 struct ldapmsg
**res
));
431 ldap_pvt_str2upper
LDAP_P(( char *str
));
434 ldap_pvt_str2lower
LDAP_P(( char *str
));
436 LDAP_F( struct berval
* )
437 ldap_pvt_str2upperbv
LDAP_P(( char *str
, struct berval
*bv
));
439 LDAP_F( struct berval
* )
440 ldap_pvt_str2lowerbv
LDAP_P(( char *str
, struct berval
*bv
));
443 LDAP_F (int) ldap_pvt_tls_config
LDAP_P(( struct ldap
*ld
,
444 int option
, const char *arg
));
445 LDAP_F (int) ldap_pvt_tls_get_option
LDAP_P(( struct ldap
*ld
,
446 int option
, void *arg
));
447 LDAP_F (int) ldap_pvt_tls_set_option
LDAP_P(( struct ldap
*ld
,
448 int option
, void *arg
));
450 LDAP_F (void) ldap_pvt_tls_destroy
LDAP_P(( void ));
451 LDAP_F (int) ldap_pvt_tls_init
LDAP_P(( int do_threads
));
452 LDAP_F (int) ldap_pvt_tls_init_def_ctx
LDAP_P(( int is_server
));
453 LDAP_F (int) ldap_pvt_tls_accept
LDAP_P(( Sockbuf
*sb
, void *ctx_arg
));
454 LDAP_F (int) ldap_pvt_tls_connect
LDAP_P(( struct ldap
*ld
, Sockbuf
*sb
, const char *host
));
455 LDAP_F (int) ldap_pvt_tls_inplace
LDAP_P(( Sockbuf
*sb
));
456 LDAP_F (void *) ldap_pvt_tls_sb_ctx
LDAP_P(( Sockbuf
*sb
));
457 LDAP_F (void) ldap_pvt_tls_ctx_free
LDAP_P(( void * ));
459 typedef int LDAPDN_rewrite_dummy
LDAP_P (( void *dn
, unsigned flags
));
461 typedef int (LDAP_TLS_CONNECT_CB
) LDAP_P (( struct ldap
*ld
, void *ssl
,
462 void *ctx
, void *arg
));
464 LDAP_F (int) ldap_pvt_tls_get_my_dn
LDAP_P(( void *ctx
, struct berval
*dn
,
465 LDAPDN_rewrite_dummy
*func
, unsigned flags
));
466 LDAP_F (int) ldap_pvt_tls_get_peer_dn
LDAP_P(( void *ctx
, struct berval
*dn
,
467 LDAPDN_rewrite_dummy
*func
, unsigned flags
));
468 LDAP_F (int) ldap_pvt_tls_get_strength
LDAP_P(( void *ctx
));
469 LDAP_F (int) ldap_pvt_tls_get_unique
LDAP_P(( void *ctx
, struct berval
*buf
, int is_server
));
470 LDAP_F (int) ldap_pvt_tls_get_endpoint
LDAP_P(( void *ctx
, struct berval
*buf
, int is_server
));
471 LDAP_F (const char *) ldap_pvt_tls_get_version
LDAP_P(( void *ctx
));
472 LDAP_F (const char *) ldap_pvt_tls_get_cipher
LDAP_P(( void *ctx
));
477 * Multiple precision stuff
479 * May use OpenSSL's BIGNUM if built with TLS,
480 * or GNU's multiple precision library. But if
481 * long long is available, that's big enough
482 * and much more efficient.
484 * If none is available, unsigned long data is used.
491 * Use OpenSSL's BIGNUM
493 #include <openssl/crypto.h>
494 #include <openssl/bn.h>
496 typedef BIGNUM
* ldap_pvt_mp_t
;
497 #define LDAP_PVT_MP_INIT (NULL)
499 #define ldap_pvt_mp_init(mp) \
500 do { (mp) = BN_new(); } while (0)
502 /* FIXME: we rely on mpr being initialized */
503 #define ldap_pvt_mp_init_set(mpr,mpv) \
504 do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0)
506 #define ldap_pvt_mp_add(mpr,mpv) \
507 BN_add((mpr), (mpr), (mpv))
509 #define ldap_pvt_mp_add_ulong(mp,v) \
510 BN_add_word((mp), (v))
512 #define ldap_pvt_mp_clear(mp) \
513 do { BN_free((mp)); (mp) = 0; } while (0)
515 #elif defined(USE_MP_GMP)
517 * Use GNU's multiple precision library
521 typedef mpz_t ldap_pvt_mp_t
;
522 #define LDAP_PVT_MP_INIT { 0 }
524 #define ldap_pvt_mp_init(mp) \
527 #define ldap_pvt_mp_init_set(mpr,mpv) \
528 mpz_init_set((mpr), (mpv))
530 #define ldap_pvt_mp_add(mpr,mpv) \
531 mpz_add((mpr), (mpr), (mpv))
533 #define ldap_pvt_mp_add_ulong(mp,v) \
534 mpz_add_ui((mp), (mp), (v))
536 #define ldap_pvt_mp_clear(mp) \
541 * Use unsigned long long
544 #ifdef USE_MP_LONG_LONG
545 typedef unsigned long long ldap_pvt_mp_t
;
546 #define LDAP_PVT_MP_INIT (0LL)
547 #elif defined(USE_MP_LONG)
548 typedef unsigned long ldap_pvt_mp_t
;
549 #define LDAP_PVT_MP_INIT (0L)
550 #elif defined(HAVE_LONG_LONG)
551 typedef unsigned long long ldap_pvt_mp_t
;
552 #define LDAP_PVT_MP_INIT (0LL)
554 typedef unsigned long ldap_pvt_mp_t
;
555 #define LDAP_PVT_MP_INIT (0L)
558 #define ldap_pvt_mp_init(mp) \
559 do { (mp) = 0; } while (0)
561 #define ldap_pvt_mp_init_set(mpr,mpv) \
562 do { (mpr) = (mpv); } while (0)
564 #define ldap_pvt_mp_add(mpr,mpv) \
565 do { (mpr) += (mpv); } while (0)
567 #define ldap_pvt_mp_add_ulong(mp,v) \
568 do { (mp) += (v); } while (0)
570 #define ldap_pvt_mp_clear(mp) \
571 do { (mp) = 0; } while (0)
575 #include "ldap_pvt_uc.h"
581 #include <limits.h> /* get CHAR_BIT */
583 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */
584 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3)
588 #endif /* _LDAP_PVT_H */