2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 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)
133 ldap_pvt_gettimeofday LDAP_P(( struct timeval *tv, void *unused ));
136 /* use this macro to allocate buffer for ldap_pvt_csnstr */
137 #define LDAP_PVT_CSNSTR_BUFSIZE 64
139 ldap_pvt_csnstr( char *buf, size_t len, unsigned int replica, unsigned int mod );
141 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
143 struct hostent; /* avoid pulling in <netdb.h> */
146 ldap_pvt_gethostbyname_a LDAP_P((
148 struct hostent *resbuf,
150 struct hostent **result,
154 ldap_pvt_gethostbyaddr_a LDAP_P((
158 struct hostent *resbuf,
160 struct hostent **result,
166 ldap_pvt_get_hname LDAP_P((
167 const struct sockaddr * sa,
177 ldap_charray_add LDAP_P((
182 ldap_charray_merge LDAP_P((
187 ldap_charray_free LDAP_P(( char **a ));
190 ldap_charray_inlist LDAP_P((
195 ldap_charray_dup LDAP_P(( char **a ));
198 ldap_str2charray LDAP_P((
200 const char *brkstr ));
203 ldap_charray2str LDAP_P((
204 char **array, const char* sep ));
208 #ifdef LDAP_AVA_NULL /* in ldap.h */
209 LDAP_F( void ) ldap_rdnfree_x LDAP_P(( LDAPRDN rdn, void *ctx ));
210 LDAP_F( void ) ldap_dnfree_x LDAP_P(( LDAPDN dn, void *ctx ));
212 LDAP_F( int ) ldap_bv2dn_x LDAP_P((
213 struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx ));
214 LDAP_F( int ) ldap_dn2bv_x LDAP_P((
215 LDAPDN dn, struct berval *bv, unsigned flags, void *ctx ));
216 LDAP_F( int ) ldap_bv2rdn_x LDAP_P((
217 struct berval *, LDAPRDN *, char **, unsigned flags, void *ctx ));
218 LDAP_F( int ) ldap_rdn2bv_x LDAP_P((
219 LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx ));
220 #endif /* LDAP_AVA_NULL */
223 LDAP_F (void) ldap_pvt_hex_unescape LDAP_P(( char *s ));
226 * these macros assume 'x' is an ASCII x
227 * and assume the "C" locale
229 #define LDAP_ASCII(c) (!((c) & 0x80))
230 #define LDAP_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
231 #define LDAP_DIGIT(c) ((c) >= '0' && (c) <= '9')
232 #define LDAP_LOWER(c) ((c) >= 'a' && (c) <= 'z')
233 #define LDAP_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
234 #define LDAP_ALPHA(c) (LDAP_LOWER(c) || LDAP_UPPER(c))
235 #define LDAP_ALNUM(c) (LDAP_ALPHA(c) || LDAP_DIGIT(c))
237 #define LDAP_LDH(c) (LDAP_ALNUM(c) || (c) == '-')
239 #define LDAP_HEXLOWER(c) ((c) >= 'a' && (c) <= 'f')
240 #define LDAP_HEXUPPER(c) ((c) >= 'A' && (c) <= 'F')
241 #define LDAP_HEX(c) (LDAP_DIGIT(c) || \
242 LDAP_HEXLOWER(c) || LDAP_HEXUPPER(c))
247 ldap_pvt_put_control LDAP_P((
248 const struct ldapcontrol *c,
250 LDAP_F (int) ldap_pvt_get_controls LDAP_P((
252 struct ldapcontrol ***ctrlsp));
254 #ifdef HAVE_CYRUS_SASL
256 struct sasl_security_properties; /* avoid pulling in <sasl.h> */
257 LDAP_F (int) ldap_pvt_sasl_secprops LDAP_P((
259 struct sasl_security_properties *secprops ));
260 LDAP_F (void) ldap_pvt_sasl_secprops_unparse LDAP_P((
261 struct sasl_security_properties *secprops,
262 struct berval *out ));
264 LDAP_F (void *) ldap_pvt_sasl_mutex_new LDAP_P((void));
265 LDAP_F (int) ldap_pvt_sasl_mutex_lock LDAP_P((void *mutex));
266 LDAP_F (int) ldap_pvt_sasl_mutex_unlock LDAP_P((void *mutex));
267 LDAP_F (void) ldap_pvt_sasl_mutex_dispose LDAP_P((void *mutex));
268 #endif /* HAVE_CYRUS_SASL */
270 struct sockbuf; /* avoid pulling in <lber.h> */
271 LDAP_F (int) ldap_pvt_sasl_install LDAP_P(( struct sockbuf *, void * ));
272 LDAP_F (void) ldap_pvt_sasl_remove LDAP_P(( struct sockbuf * ));
275 * SASL encryption support for LBER Sockbufs
278 struct sb_sasl_generic_data;
280 struct sb_sasl_generic_ops {
281 void (*init)(struct sb_sasl_generic_data *p,
284 ber_len_t *max_recv);
285 ber_int_t (*encode)(struct sb_sasl_generic_data *p,
289 ber_int_t (*decode)(struct sb_sasl_generic_data *p,
290 const Sockbuf_Buf *src,
292 void (*reset_buf)(struct sb_sasl_generic_data *p,
294 void (*fini)(struct sb_sasl_generic_data *p);
297 struct sb_sasl_generic_install {
298 const struct sb_sasl_generic_ops *ops;
302 struct sb_sasl_generic_data {
303 const struct sb_sasl_generic_ops *ops;
305 Sockbuf_IO_Desc *sbiod;
309 Sockbuf_Buf sec_buf_in;
313 #define LDAP_PVT_SASL_PARTIAL_WRITE 1
316 #ifndef LDAP_PVT_SASL_LOCAL_SSF
317 #define LDAP_PVT_SASL_LOCAL_SSF 71 /* SSF for Unix Domain Sockets */
318 #endif /* ! LDAP_PVT_SASL_LOCAL_SSF */
325 LDAP_F ( int ) ldap_pvt_discard LDAP_P((
326 struct ldap *ld, ber_int_t msgid ));
330 ldap_parse_ldif_record_x LDAP_P((
332 unsigned long linenum,
333 struct ldifrecord *lr,
339 LDAP_F( BerElement * )
340 ldap_get_message_ber LDAP_P((
344 LDAP_F (int) ldap_open_internal_connection LDAP_P((
345 struct ldap **ldp, ber_socket_t *fdp ));
346 LDAP_F (int) ldap_init_fd LDAP_P((
347 ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap **ldp ));
350 LDAP_F (int) ldap_pvt_sasl_generic_install LDAP_P(( Sockbuf *sb,
351 struct sb_sasl_generic_install *install_arg ));
352 LDAP_F (void) ldap_pvt_sasl_generic_remove LDAP_P(( Sockbuf *sb ));
355 LDAP_F( int ) ldap_pvt_put_filter LDAP_P((
360 ldap_pvt_find_wildcard LDAP_P(( const char *s ));
363 ldap_pvt_filter_value_unescape LDAP_P(( char *filter ));
366 ldap_bv2escaped_filter_value_len LDAP_P(( struct berval *in ));
369 ldap_bv2escaped_filter_value_x LDAP_P(( struct berval *in, struct berval *out,
370 int inplace, void *ctx ));
372 LDAP_F (int) ldap_pvt_search LDAP_P((
374 LDAP_CONST char *base,
376 LDAP_CONST char *filter,
379 struct ldapcontrol **sctrls,
380 struct ldapcontrol **cctrls,
381 struct timeval *timeout,
386 LDAP_F(int) ldap_pvt_search_s LDAP_P((
388 LDAP_CONST char *base,
390 LDAP_CONST char *filter,
393 struct ldapcontrol **sctrls,
394 struct ldapcontrol **cctrls,
395 struct timeval *timeout,
398 struct ldapmsg **res ));
402 ldap_pvt_str2upper LDAP_P(( char *str ));
405 ldap_pvt_str2lower LDAP_P(( char *str ));
407 LDAP_F( struct berval * )
408 ldap_pvt_str2upperbv LDAP_P(( char *str, struct berval *bv ));
410 LDAP_F( struct berval * )
411 ldap_pvt_str2lowerbv LDAP_P(( char *str, struct berval *bv ));
414 LDAP_F (int) ldap_pvt_tls_config LDAP_P(( struct ldap *ld,
415 int option, const char *arg ));
416 LDAP_F (int) ldap_pvt_tls_get_option LDAP_P(( struct ldap *ld,
417 int option, void *arg ));
418 LDAP_F (int) ldap_pvt_tls_set_option LDAP_P(( struct ldap *ld,
419 int option, void *arg ));
421 LDAP_F (void) ldap_pvt_tls_destroy LDAP_P(( void ));
422 LDAP_F (int) ldap_pvt_tls_init LDAP_P(( void ));
423 LDAP_F (int) ldap_pvt_tls_init_def_ctx LDAP_P(( int is_server ));
424 LDAP_F (int) ldap_pvt_tls_accept LDAP_P(( Sockbuf *sb, void *ctx_arg ));
425 LDAP_F (int) ldap_pvt_tls_inplace LDAP_P(( Sockbuf *sb ));
426 LDAP_F (void *) ldap_pvt_tls_sb_ctx LDAP_P(( Sockbuf *sb ));
427 LDAP_F (void) ldap_pvt_tls_ctx_free LDAP_P(( void * ));
429 typedef int LDAPDN_rewrite_dummy LDAP_P (( void *dn, unsigned flags ));
431 typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl,
432 void *ctx, void *arg ));
434 LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn,
435 LDAPDN_rewrite_dummy *func, unsigned flags ));
436 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn,
437 LDAPDN_rewrite_dummy *func, unsigned flags ));
438 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx ));
439 LDAP_F (int) ldap_pvt_tls_get_unique LDAP_P(( void *ctx, struct berval *buf, int is_server ));
440 LDAP_F (const char *) ldap_pvt_tls_get_version LDAP_P(( void *ctx ));
441 LDAP_F (const char *) ldap_pvt_tls_get_cipher LDAP_P(( void *ctx ));
446 * Multiple precision stuff
448 * May use OpenSSL's BIGNUM if built with TLS,
449 * or GNU's multiple precision library. But if
450 * long long is available, that's big enough
451 * and much more efficient.
453 * If none is available, unsigned long data is used.
460 * Use OpenSSL's BIGNUM
462 #include <openssl/crypto.h>
463 #include <openssl/bn.h>
465 typedef BIGNUM* ldap_pvt_mp_t;
466 #define LDAP_PVT_MP_INIT (NULL)
468 #define ldap_pvt_mp_init(mp) \
469 do { (mp) = BN_new(); } while (0)
471 /* FIXME: we rely on mpr being initialized */
472 #define ldap_pvt_mp_init_set(mpr,mpv) \
473 do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0)
475 #define ldap_pvt_mp_add(mpr,mpv) \
476 BN_add((mpr), (mpr), (mpv))
478 #define ldap_pvt_mp_add_ulong(mp,v) \
479 BN_add_word((mp), (v))
481 #define ldap_pvt_mp_clear(mp) \
482 do { BN_free((mp)); (mp) = 0; } while (0)
484 #elif defined(USE_MP_GMP)
486 * Use GNU's multiple precision library
490 typedef mpz_t ldap_pvt_mp_t;
491 #define LDAP_PVT_MP_INIT { 0 }
493 #define ldap_pvt_mp_init(mp) \
496 #define ldap_pvt_mp_init_set(mpr,mpv) \
497 mpz_init_set((mpr), (mpv))
499 #define ldap_pvt_mp_add(mpr,mpv) \
500 mpz_add((mpr), (mpr), (mpv))
502 #define ldap_pvt_mp_add_ulong(mp,v) \
503 mpz_add_ui((mp), (mp), (v))
505 #define ldap_pvt_mp_clear(mp) \
510 * Use unsigned long long
513 #ifdef USE_MP_LONG_LONG
514 typedef unsigned long long ldap_pvt_mp_t;
515 #define LDAP_PVT_MP_INIT (0LL)
516 #elif defined(USE_MP_LONG)
517 typedef unsigned long ldap_pvt_mp_t;
518 #define LDAP_PVT_MP_INIT (0L)
519 #elif defined(HAVE_LONG_LONG)
520 typedef unsigned long long ldap_pvt_mp_t;
521 #define LDAP_PVT_MP_INIT (0LL)
523 typedef unsigned long ldap_pvt_mp_t;
524 #define LDAP_PVT_MP_INIT (0L)
527 #define ldap_pvt_mp_init(mp) \
528 do { (mp) = 0; } while (0)
530 #define ldap_pvt_mp_init_set(mpr,mpv) \
531 do { (mpr) = (mpv); } while (0)
533 #define ldap_pvt_mp_add(mpr,mpv) \
534 do { (mpr) += (mpv); } while (0)
536 #define ldap_pvt_mp_add_ulong(mp,v) \
537 do { (mp) += (v); } while (0)
539 #define ldap_pvt_mp_clear(mp) \
540 do { (mp) = 0; } while (0)
544 #include "ldap_pvt_uc.h"
550 #include <limits.h> /* get CHAR_BIT */
552 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */
553 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3)
557 #endif /* _LDAP_PVT_H */