2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2018 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 <lber.h> /* get ber_slen_t */
24 #include <lber_pvt.h> /* get Sockbuf_Buf */
28 #define LDAP_PROTO_TCP 1 /* ldap:// */
29 #define LDAP_PROTO_UDP 2 /* reserved */
30 #define LDAP_PROTO_IPC 3 /* ldapi:// */
31 #define LDAP_PROTO_EXT 4 /* user-defined socket/sockbuf */
34 ldap_pvt_url_scheme2proto LDAP_P((
37 ldap_pvt_url_scheme2tls LDAP_P((
41 ldap_pvt_url_scheme_port LDAP_P((
44 struct ldap_url_desc; /* avoid pulling in <ldap.h> */
46 #define LDAP_PVT_URL_PARSE_NONE (0x00U)
47 #define LDAP_PVT_URL_PARSE_NOEMPTY_HOST (0x01U)
48 #define LDAP_PVT_URL_PARSE_DEF_PORT (0x02U)
49 #define LDAP_PVT_URL_PARSE_NOEMPTY_DN (0x04U)
50 #define LDAP_PVT_URL_PARSE_NODEF_SCOPE (0x08U)
51 #define LDAP_PVT_URL_PARSE_HISTORIC (LDAP_PVT_URL_PARSE_NODEF_SCOPE | \
52 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | \
53 LDAP_PVT_URL_PARSE_DEF_PORT)
56 ldap_url_parse_ext LDAP_P((
58 struct ldap_url_desc **ludpp,
61 LDAP_F (int) ldap_url_parselist LDAP_P(( /* deprecated, use ldap_url_parselist_ext() */
62 struct ldap_url_desc **ludlist,
65 LDAP_F (int) ldap_url_parselist_ext LDAP_P((
66 struct ldap_url_desc **ludlist,
71 LDAP_F (char *) ldap_url_list2urls LDAP_P((
72 struct ldap_url_desc *ludlist ));
74 LDAP_F (void) ldap_free_urllist LDAP_P((
75 struct ldap_url_desc *ludlist ));
77 LDAP_F (int) ldap_pvt_scope2bv LDAP_P ((
78 int scope, struct berval *bv ));
80 LDAP_F (LDAP_CONST char *) ldap_pvt_scope2str LDAP_P ((
83 LDAP_F (int) ldap_pvt_bv2scope LDAP_P ((
86 LDAP_F (int) ldap_pvt_str2scope LDAP_P ((
90 ldap_pvt_ctime LDAP_P((
94 # if defined( HAVE_GMTIME_R )
96 # define ldap_pvt_gmtime(timep, result) gmtime_r((timep), (result))
99 ldap_pvt_gmtime LDAP_P((
100 LDAP_CONST time_t *timep,
101 struct tm *result ));
104 # if defined( HAVE_LOCALTIME_R )
105 # define USE_LOCALTIME_R
106 # define ldap_pvt_localtime(timep, result) localtime_r((timep), (result))
108 LDAP_F( struct tm * )
109 ldap_pvt_localtime LDAP_P((
110 LDAP_CONST time_t *timep,
111 struct tm *result ));
114 #if defined( USE_GMTIME_R ) && defined( USE_LOCALTIME_R )
115 # define ldap_pvt_gmtime_lock() (0)
116 # define ldap_pvt_gmtime_unlock() (0)
119 ldap_pvt_gmtime_lock LDAP_P(( void ));
122 ldap_pvt_gmtime_unlock LDAP_P(( void ));
123 #endif /* USE_GMTIME_R && USE_LOCALTIME_R */
125 /* Get current time as a structured time */
128 ldap_pvt_gettime LDAP_P(( struct lutil_tm * ));
131 #define gettimeofday(tv,tz) ldap_pvt_gettimeofday(tv,tz)
134 ldap_pvt_gettimeofday LDAP_P(( struct timeval *tv, void *unused ));
137 /* use this macro to allocate buffer for ldap_pvt_csnstr */
138 #define LDAP_PVT_CSNSTR_BUFSIZE 64
140 ldap_pvt_csnstr( char *buf, size_t len, unsigned int replica, unsigned int mod );
142 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
144 struct hostent; /* avoid pulling in <netdb.h> */
147 ldap_pvt_gethostbyname_a LDAP_P((
149 struct hostent *resbuf,
151 struct hostent **result,
155 ldap_pvt_gethostbyaddr_a LDAP_P((
159 struct hostent *resbuf,
161 struct hostent **result,
167 ldap_pvt_get_hname LDAP_P((
168 const struct sockaddr * sa,
178 ldap_charray_add LDAP_P((
183 ldap_charray_merge LDAP_P((
188 ldap_charray_free LDAP_P(( char **a ));
191 ldap_charray_inlist LDAP_P((
196 ldap_charray_dup LDAP_P(( char **a ));
199 ldap_str2charray LDAP_P((
201 const char *brkstr ));
204 ldap_charray2str LDAP_P((
205 char **array, const char* sep ));
209 #ifdef LDAP_AVA_NULL /* in ldap.h */
210 LDAP_F( void ) ldap_rdnfree_x LDAP_P(( LDAPRDN rdn, void *ctx ));
211 LDAP_F( void ) ldap_dnfree_x LDAP_P(( LDAPDN dn, void *ctx ));
213 LDAP_F( int ) ldap_bv2dn_x LDAP_P((
214 struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx ));
215 LDAP_F( int ) ldap_dn2bv_x LDAP_P((
216 LDAPDN dn, struct berval *bv, unsigned flags, void *ctx ));
217 LDAP_F( int ) ldap_bv2rdn_x LDAP_P((
218 struct berval *, LDAPRDN *, char **, unsigned flags, void *ctx ));
219 LDAP_F( int ) ldap_rdn2bv_x LDAP_P((
220 LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx ));
221 #endif /* LDAP_AVA_NULL */
224 LDAP_F (void) ldap_pvt_hex_unescape LDAP_P(( char *s ));
227 * these macros assume 'x' is an ASCII x
228 * and assume the "C" locale
230 #define LDAP_ASCII(c) (!((c) & 0x80))
231 #define LDAP_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
232 #define LDAP_DIGIT(c) ((c) >= '0' && (c) <= '9')
233 #define LDAP_LOWER(c) ((c) >= 'a' && (c) <= 'z')
234 #define LDAP_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
235 #define LDAP_ALPHA(c) (LDAP_LOWER(c) || LDAP_UPPER(c))
236 #define LDAP_ALNUM(c) (LDAP_ALPHA(c) || LDAP_DIGIT(c))
238 #define LDAP_LDH(c) (LDAP_ALNUM(c) || (c) == '-')
240 #define LDAP_HEXLOWER(c) ((c) >= 'a' && (c) <= 'f')
241 #define LDAP_HEXUPPER(c) ((c) >= 'A' && (c) <= 'F')
242 #define LDAP_HEX(c) (LDAP_DIGIT(c) || \
243 LDAP_HEXLOWER(c) || LDAP_HEXUPPER(c))
248 ldap_pvt_put_control LDAP_P((
249 const struct ldapcontrol *c,
251 LDAP_F (int) ldap_pvt_get_controls LDAP_P((
253 struct ldapcontrol ***ctrlsp));
255 #ifdef HAVE_CYRUS_SASL
257 struct sasl_security_properties; /* avoid pulling in <sasl.h> */
258 LDAP_F (int) ldap_pvt_sasl_secprops LDAP_P((
260 struct sasl_security_properties *secprops ));
261 LDAP_F (void) ldap_pvt_sasl_secprops_unparse LDAP_P((
262 struct sasl_security_properties *secprops,
263 struct berval *out ));
265 LDAP_F (void *) ldap_pvt_sasl_mutex_new LDAP_P((void));
266 LDAP_F (int) ldap_pvt_sasl_mutex_lock LDAP_P((void *mutex));
267 LDAP_F (int) ldap_pvt_sasl_mutex_unlock LDAP_P((void *mutex));
268 LDAP_F (void) ldap_pvt_sasl_mutex_dispose LDAP_P((void *mutex));
269 #endif /* HAVE_CYRUS_SASL */
271 struct sockbuf; /* avoid pulling in <lber.h> */
272 LDAP_F (int) ldap_pvt_sasl_install LDAP_P(( struct sockbuf *, void * ));
273 LDAP_F (void) ldap_pvt_sasl_remove LDAP_P(( struct sockbuf * ));
276 * SASL encryption support for LBER Sockbufs
279 struct sb_sasl_generic_data;
281 struct sb_sasl_generic_ops {
282 void (*init)(struct sb_sasl_generic_data *p,
285 ber_len_t *max_recv);
286 ber_int_t (*encode)(struct sb_sasl_generic_data *p,
290 ber_int_t (*decode)(struct sb_sasl_generic_data *p,
291 const Sockbuf_Buf *src,
293 void (*reset_buf)(struct sb_sasl_generic_data *p,
295 void (*fini)(struct sb_sasl_generic_data *p);
298 struct sb_sasl_generic_install {
299 const struct sb_sasl_generic_ops *ops;
303 struct sb_sasl_generic_data {
304 const struct sb_sasl_generic_ops *ops;
306 Sockbuf_IO_Desc *sbiod;
310 Sockbuf_Buf sec_buf_in;
314 #define LDAP_PVT_SASL_PARTIAL_WRITE 1
317 #ifndef LDAP_PVT_SASL_LOCAL_SSF
318 #define LDAP_PVT_SASL_LOCAL_SSF 71 /* SSF for Unix Domain Sockets */
319 #endif /* ! LDAP_PVT_SASL_LOCAL_SSF */
326 LDAP_F ( int ) ldap_pvt_discard LDAP_P((
327 struct ldap *ld, ber_int_t msgid ));
331 ldap_pvt_conf_option LDAP_P((
332 char *cmd, char *opt, int userconf ));
336 ldap_parse_ldif_record_x LDAP_P((
338 unsigned long linenum,
339 struct ldifrecord *lr,
345 LDAP_F( BerElement * )
346 ldap_get_message_ber LDAP_P((
350 LDAP_F (int) ldap_open_internal_connection LDAP_P((
351 struct ldap **ldp, ber_socket_t *fdp ));
352 LDAP_F (int) ldap_init_fd LDAP_P((
353 ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap **ldp ));
356 LDAP_F (int) ldap_pvt_sasl_generic_install LDAP_P(( Sockbuf *sb,
357 struct sb_sasl_generic_install *install_arg ));
358 LDAP_F (void) ldap_pvt_sasl_generic_remove LDAP_P(( Sockbuf *sb ));
361 LDAP_F( int ) ldap_pvt_put_filter LDAP_P((
366 ldap_pvt_find_wildcard LDAP_P(( const char *s ));
369 ldap_pvt_filter_value_unescape LDAP_P(( char *filter ));
372 ldap_bv2escaped_filter_value_len LDAP_P(( struct berval *in ));
375 ldap_bv2escaped_filter_value_x LDAP_P(( struct berval *in, struct berval *out,
376 int inplace, void *ctx ));
378 LDAP_F (int) ldap_pvt_search LDAP_P((
380 LDAP_CONST char *base,
382 LDAP_CONST char *filter,
385 struct ldapcontrol **sctrls,
386 struct ldapcontrol **cctrls,
387 struct timeval *timeout,
392 LDAP_F(int) ldap_pvt_search_s LDAP_P((
394 LDAP_CONST char *base,
396 LDAP_CONST char *filter,
399 struct ldapcontrol **sctrls,
400 struct ldapcontrol **cctrls,
401 struct timeval *timeout,
404 struct ldapmsg **res ));
408 ldap_pvt_str2upper LDAP_P(( char *str ));
411 ldap_pvt_str2lower LDAP_P(( char *str ));
413 LDAP_F( struct berval * )
414 ldap_pvt_str2upperbv LDAP_P(( char *str, struct berval *bv ));
416 LDAP_F( struct berval * )
417 ldap_pvt_str2lowerbv LDAP_P(( char *str, struct berval *bv ));
420 LDAP_F (int) ldap_pvt_tls_config LDAP_P(( struct ldap *ld,
421 int option, const char *arg ));
422 LDAP_F (int) ldap_pvt_tls_get_option LDAP_P(( struct ldap *ld,
423 int option, void *arg ));
424 LDAP_F (int) ldap_pvt_tls_set_option LDAP_P(( struct ldap *ld,
425 int option, void *arg ));
427 LDAP_F (void) ldap_pvt_tls_destroy LDAP_P(( void ));
428 LDAP_F (int) ldap_pvt_tls_init LDAP_P(( void ));
429 LDAP_F (int) ldap_pvt_tls_init_def_ctx LDAP_P(( int is_server ));
430 LDAP_F (int) ldap_pvt_tls_accept LDAP_P(( Sockbuf *sb, void *ctx_arg ));
431 LDAP_F (int) ldap_pvt_tls_inplace LDAP_P(( Sockbuf *sb ));
432 LDAP_F (void *) ldap_pvt_tls_sb_ctx LDAP_P(( Sockbuf *sb ));
433 LDAP_F (void) ldap_pvt_tls_ctx_free LDAP_P(( void * ));
435 typedef int LDAPDN_rewrite_dummy LDAP_P (( void *dn, unsigned flags ));
437 typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl,
438 void *ctx, void *arg ));
440 LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn,
441 LDAPDN_rewrite_dummy *func, unsigned flags ));
442 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn,
443 LDAPDN_rewrite_dummy *func, unsigned flags ));
444 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx ));
445 LDAP_F (int) ldap_pvt_tls_get_unique LDAP_P(( void *ctx, struct berval *buf, int is_server ));
446 LDAP_F (const char *) ldap_pvt_tls_get_version LDAP_P(( void *ctx ));
447 LDAP_F (const char *) ldap_pvt_tls_get_cipher LDAP_P(( void *ctx ));
452 * Multiple precision stuff
454 * May use OpenSSL's BIGNUM if built with TLS,
455 * or GNU's multiple precision library. But if
456 * long long is available, that's big enough
457 * and much more efficient.
459 * If none is available, unsigned long data is used.
466 * Use OpenSSL's BIGNUM
468 #include <openssl/crypto.h>
469 #include <openssl/bn.h>
471 typedef BIGNUM* ldap_pvt_mp_t;
472 #define LDAP_PVT_MP_INIT (NULL)
474 #define ldap_pvt_mp_init(mp) \
475 do { (mp) = BN_new(); } while (0)
477 /* FIXME: we rely on mpr being initialized */
478 #define ldap_pvt_mp_init_set(mpr,mpv) \
479 do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0)
481 #define ldap_pvt_mp_add(mpr,mpv) \
482 BN_add((mpr), (mpr), (mpv))
484 #define ldap_pvt_mp_add_ulong(mp,v) \
485 BN_add_word((mp), (v))
487 #define ldap_pvt_mp_clear(mp) \
488 do { BN_free((mp)); (mp) = 0; } while (0)
490 #elif defined(USE_MP_GMP)
492 * Use GNU's multiple precision library
496 typedef mpz_t ldap_pvt_mp_t;
497 #define LDAP_PVT_MP_INIT { 0 }
499 #define ldap_pvt_mp_init(mp) \
502 #define ldap_pvt_mp_init_set(mpr,mpv) \
503 mpz_init_set((mpr), (mpv))
505 #define ldap_pvt_mp_add(mpr,mpv) \
506 mpz_add((mpr), (mpr), (mpv))
508 #define ldap_pvt_mp_add_ulong(mp,v) \
509 mpz_add_ui((mp), (mp), (v))
511 #define ldap_pvt_mp_clear(mp) \
516 * Use unsigned long long
519 #ifdef USE_MP_LONG_LONG
520 typedef unsigned long long ldap_pvt_mp_t;
521 #define LDAP_PVT_MP_INIT (0LL)
522 #elif defined(USE_MP_LONG)
523 typedef unsigned long ldap_pvt_mp_t;
524 #define LDAP_PVT_MP_INIT (0L)
525 #elif defined(HAVE_LONG_LONG)
526 typedef unsigned long long ldap_pvt_mp_t;
527 #define LDAP_PVT_MP_INIT (0LL)
529 typedef unsigned long ldap_pvt_mp_t;
530 #define LDAP_PVT_MP_INIT (0L)
533 #define ldap_pvt_mp_init(mp) \
534 do { (mp) = 0; } while (0)
536 #define ldap_pvt_mp_init_set(mpr,mpv) \
537 do { (mpr) = (mpv); } while (0)
539 #define ldap_pvt_mp_add(mpr,mpv) \
540 do { (mpr) += (mpv); } while (0)
542 #define ldap_pvt_mp_add_ulong(mp,v) \
543 do { (mp) += (v); } while (0)
545 #define ldap_pvt_mp_clear(mp) \
546 do { (mp) = 0; } while (0)
550 #include "ldap_pvt_uc.h"
556 #include <limits.h> /* get CHAR_BIT */
558 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */
559 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3)
563 #endif /* _LDAP_PVT_H */