1 /* $OpenLDAP$ */ 2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 3 * 4 * Copyright 1998-2021 The OpenLDAP Foundation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted only as authorized by the OpenLDAP 9 * Public License. 10 * 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>. 14 */ 15 16 /* ldap-pvt.h - Header for ldap_pvt_ functions. 17 * These are meant to be internal to OpenLDAP Software. 18 */ 19 20 #ifndef _LDAP_PVT_H 21 #define _LDAP_PVT_H 1 22 23 #include <openldap.h> /* get public interfaces */ 24 #include <lber.h> /* get ber_slen_t */ 25 #include <lber_pvt.h> /* get Sockbuf_Buf */ 26 27 LDAP_BEGIN_DECL 28 29 LDAP_F ( int ) 30 ldap_pvt_url_scheme2proto LDAP_P(( 31 const char * )); 32 LDAP_F ( int ) 33 ldap_pvt_url_scheme2tls LDAP_P(( 34 const char * )); 35 36 LDAP_F ( int ) 37 ldap_pvt_url_scheme_port LDAP_P(( 38 const char *, int )); 39 40 struct ldap_url_desc; /* avoid pulling in <ldap.h> */ 41 42 #define LDAP_PVT_URL_PARSE_NONE (0x00U) 43 #define LDAP_PVT_URL_PARSE_NOEMPTY_HOST (0x01U) 44 #define LDAP_PVT_URL_PARSE_DEF_PORT (0x02U) 45 #define LDAP_PVT_URL_PARSE_NOEMPTY_DN (0x04U) 46 #define LDAP_PVT_URL_PARSE_NODEF_SCOPE (0x08U) 47 #define LDAP_PVT_URL_PARSE_HISTORIC (LDAP_PVT_URL_PARSE_NODEF_SCOPE | \ 48 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | \ 49 LDAP_PVT_URL_PARSE_DEF_PORT) 50 51 LDAP_F( int ) 52 ldap_url_parse_ext LDAP_P(( 53 LDAP_CONST char *url, 54 struct ldap_url_desc **ludpp, 55 unsigned flags )); 56 57 LDAP_F (int) ldap_url_parselist LDAP_P(( /* deprecated, use ldap_url_parselist_ext() */ 58 struct ldap_url_desc **ludlist, 59 const char *url )); 60 61 LDAP_F (int) ldap_url_parselist_ext LDAP_P(( 62 struct ldap_url_desc **ludlist, 63 const char *url, 64 const char *sep, 65 unsigned flags )); 66 67 LDAP_F (char *) ldap_url_list2urls LDAP_P(( 68 struct ldap_url_desc *ludlist )); 69 70 LDAP_F (void) ldap_free_urllist LDAP_P(( 71 struct ldap_url_desc *ludlist )); 72 73 LDAP_F (int) ldap_pvt_scope2bv LDAP_P (( 74 int scope, struct berval *bv )); 75 76 LDAP_F (LDAP_CONST char *) ldap_pvt_scope2str LDAP_P (( 77 int scope )); 78 79 LDAP_F (int) ldap_pvt_bv2scope LDAP_P (( 80 struct berval *bv )); 81 82 LDAP_F (int) ldap_pvt_str2scope LDAP_P (( 83 LDAP_CONST char * )); 84 85 LDAP_F( char * ) 86 ldap_pvt_ctime LDAP_P(( 87 const time_t *tp, 88 char *buf )); 89 90 # if defined( HAVE_GMTIME_R ) 91 # define USE_GMTIME_R 92 # define ldap_pvt_gmtime(timep, result) gmtime_r((timep), (result)) 93 # else 94 LDAP_F( struct tm * ) 95 ldap_pvt_gmtime LDAP_P(( 96 LDAP_CONST time_t *timep, 97 struct tm *result )); 98 #endif 99 100 # if defined( HAVE_LOCALTIME_R ) 101 # define USE_LOCALTIME_R 102 # define ldap_pvt_localtime(timep, result) localtime_r((timep), (result)) 103 # else 104 LDAP_F( struct tm * ) 105 ldap_pvt_localtime LDAP_P(( 106 LDAP_CONST time_t *timep, 107 struct tm *result )); 108 # endif 109 110 #if defined( USE_GMTIME_R ) && defined( USE_LOCALTIME_R ) 111 # define ldap_pvt_gmtime_lock() (0) 112 # define ldap_pvt_gmtime_unlock() (0) 113 #else 114 LDAP_F( int ) 115 ldap_pvt_gmtime_lock LDAP_P(( void )); 116 117 LDAP_F( int ) 118 ldap_pvt_gmtime_unlock LDAP_P(( void )); 119 #endif /* USE_GMTIME_R && USE_LOCALTIME_R */ 120 121 /* Get current time as a structured time */ 122 struct lutil_tm; 123 LDAP_F( void ) 124 ldap_pvt_gettime LDAP_P(( struct lutil_tm * )); 125 126 #ifdef _WIN32 127 #define gettimeofday(tv,tz) ldap_pvt_gettimeofday(tv,tz) 128 struct timeval; 129 LDAP_F( int ) 130 ldap_pvt_gettimeofday LDAP_P(( struct timeval *tv, void *unused )); 131 #endif 132 133 /* use this macro to allocate buffer for ldap_pvt_csnstr */ 134 #define LDAP_PVT_CSNSTR_BUFSIZE 64 135 LDAP_F( size_t ) 136 ldap_pvt_csnstr( char *buf, size_t len, unsigned int replica, unsigned int mod ); 137 138 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * )); 139 140 struct hostent; /* avoid pulling in <netdb.h> */ 141 142 LDAP_F( int ) 143 ldap_pvt_gethostbyname_a LDAP_P(( 144 const char *name, 145 struct hostent *resbuf, 146 char **buf, 147 struct hostent **result, 148 int *herrno_ptr )); 149 150 LDAP_F( int ) 151 ldap_pvt_gethostbyaddr_a LDAP_P(( 152 const char *addr, 153 int len, 154 int type, 155 struct hostent *resbuf, 156 char **buf, 157 struct hostent **result, 158 int *herrno_ptr )); 159 160 struct sockaddr; 161 162 LDAP_F( int ) 163 ldap_pvt_get_hname LDAP_P(( 164 const struct sockaddr * sa, 165 int salen, 166 char *name, 167 int namelen, 168 char **herr )); 169 170 171 /* charray.c */ 172 173 LDAP_F( int ) 174 ldap_charray_add LDAP_P(( 175 char ***a, 176 const char *s )); 177 178 LDAP_F( int ) 179 ldap_charray_merge LDAP_P(( 180 char ***a, 181 char **s )); 182 183 LDAP_F( void ) 184 ldap_charray_free LDAP_P(( char **a )); 185 186 LDAP_F( int ) 187 ldap_charray_inlist LDAP_P(( 188 char **a, 189 const char *s )); 190 191 LDAP_F( char ** ) 192 ldap_charray_dup LDAP_P(( char **a )); 193 194 LDAP_F( char ** ) 195 ldap_str2charray LDAP_P(( 196 const char *str, 197 const char *brkstr )); 198 199 LDAP_F( char * ) 200 ldap_charray2str LDAP_P(( 201 char **array, const char* sep )); 202 203 /* getdn.c */ 204 205 #ifdef LDAP_AVA_NULL /* in ldap.h */ 206 LDAP_F( void ) ldap_rdnfree_x LDAP_P(( LDAPRDN rdn, void *ctx )); 207 LDAP_F( void ) ldap_dnfree_x LDAP_P(( LDAPDN dn, void *ctx )); 208 209 LDAP_F( int ) ldap_bv2dn_x LDAP_P(( 210 struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx )); 211 LDAP_F( int ) ldap_dn2bv_x LDAP_P(( 212 LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )); 213 LDAP_F( int ) ldap_bv2rdn_x LDAP_P(( 214 struct berval *, LDAPRDN *, char **, unsigned flags, void *ctx )); 215 LDAP_F( int ) ldap_rdn2bv_x LDAP_P(( 216 LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )); 217 #endif /* LDAP_AVA_NULL */ 218 219 /* url.c */ 220 LDAP_F (void) ldap_pvt_hex_unescape LDAP_P(( char *s )); 221 222 /* 223 * these macros assume 'x' is an ASCII x 224 * and assume the "C" locale 225 */ 226 #define LDAP_ASCII(c) (!((c) & 0x80)) 227 #define LDAP_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n') 228 #define LDAP_DIGIT(c) ((c) >= '0' && (c) <= '9') 229 #define LDAP_LOWER(c) ((c) >= 'a' && (c) <= 'z') 230 #define LDAP_UPPER(c) ((c) >= 'A' && (c) <= 'Z') 231 #define LDAP_ALPHA(c) (LDAP_LOWER(c) || LDAP_UPPER(c)) 232 #define LDAP_ALNUM(c) (LDAP_ALPHA(c) || LDAP_DIGIT(c)) 233 234 #define LDAP_LDH(c) (LDAP_ALNUM(c) || (c) == '-') 235 236 #define LDAP_HEXLOWER(c) ((c) >= 'a' && (c) <= 'f') 237 #define LDAP_HEXUPPER(c) ((c) >= 'A' && (c) <= 'F') 238 #define LDAP_HEX(c) (LDAP_DIGIT(c) || \ 239 LDAP_HEXLOWER(c) || LDAP_HEXUPPER(c)) 240 241 /* controls.c */ 242 struct ldapcontrol; 243 LDAP_F (int) 244 ldap_pvt_put_control LDAP_P(( 245 const struct ldapcontrol *c, 246 BerElement *ber )); 247 LDAP_F (int) ldap_pvt_get_controls LDAP_P(( 248 BerElement *be, 249 struct ldapcontrol ***ctrlsp)); 250 251 #ifdef HAVE_CYRUS_SASL 252 /* cyrus.c */ 253 struct sasl_security_properties; /* avoid pulling in <sasl.h> */ 254 LDAP_F (int) ldap_pvt_sasl_secprops LDAP_P(( 255 const char *in, 256 struct sasl_security_properties *secprops )); 257 LDAP_F (void) ldap_pvt_sasl_secprops_unparse LDAP_P(( 258 struct sasl_security_properties *secprops, 259 struct berval *out )); 260 261 LDAP_F (void *) ldap_pvt_sasl_mutex_new LDAP_P((void)); 262 LDAP_F (int) ldap_pvt_sasl_mutex_lock LDAP_P((void *mutex)); 263 LDAP_F (int) ldap_pvt_sasl_mutex_unlock LDAP_P((void *mutex)); 264 LDAP_F (void) ldap_pvt_sasl_mutex_dispose LDAP_P((void *mutex)); 265 #endif /* HAVE_CYRUS_SASL */ 266 267 struct sockbuf; /* avoid pulling in <lber.h> */ 268 LDAP_F (int) ldap_pvt_sasl_install LDAP_P(( struct sockbuf *, void * )); 269 LDAP_F (void) ldap_pvt_sasl_remove LDAP_P(( struct sockbuf * )); 270 271 /* 272 * SASL encryption support for LBER Sockbufs 273 */ 274 275 struct sb_sasl_generic_data; 276 277 struct sb_sasl_generic_ops { 278 void (*init)(struct sb_sasl_generic_data *p, 279 ber_len_t *min_send, 280 ber_len_t *max_send, 281 ber_len_t *max_recv); 282 ber_int_t (*encode)(struct sb_sasl_generic_data *p, 283 unsigned char *buf, 284 ber_len_t len, 285 Sockbuf_Buf *dst); 286 ber_int_t (*decode)(struct sb_sasl_generic_data *p, 287 const Sockbuf_Buf *src, 288 Sockbuf_Buf *dst); 289 void (*reset_buf)(struct sb_sasl_generic_data *p, 290 Sockbuf_Buf *buf); 291 void (*fini)(struct sb_sasl_generic_data *p); 292 }; 293 294 struct sb_sasl_generic_install { 295 const struct sb_sasl_generic_ops *ops; 296 void *ops_private; 297 }; 298 299 struct sb_sasl_generic_data { 300 const struct sb_sasl_generic_ops *ops; 301 void *ops_private; 302 Sockbuf_IO_Desc *sbiod; 303 ber_len_t min_send; 304 ber_len_t max_send; 305 ber_len_t max_recv; 306 Sockbuf_Buf sec_buf_in; 307 Sockbuf_Buf buf_in; 308 Sockbuf_Buf buf_out; 309 unsigned int flags; 310 #define LDAP_PVT_SASL_PARTIAL_WRITE 1 311 }; 312 313 #ifndef LDAP_PVT_SASL_LOCAL_SSF 314 #define LDAP_PVT_SASL_LOCAL_SSF 71 /* SSF for Unix Domain Sockets */ 315 #endif /* ! LDAP_PVT_SASL_LOCAL_SSF */ 316 317 struct ldap; 318 struct ldapmsg; 319 320 /* abandon */ 321 LDAP_F ( int ) ldap_pvt_discard LDAP_P(( 322 struct ldap *ld, ber_int_t msgid )); 323 324 /* messages.c */ 325 LDAP_F( BerElement * ) 326 ldap_get_message_ber LDAP_P(( 327 struct ldapmsg * )); 328 329 /* open */ 330 LDAP_F (int) ldap_open_internal_connection LDAP_P(( 331 struct ldap **ldp, ber_socket_t *fdp )); 332 333 /* sasl.c */ 334 LDAP_F (int) ldap_pvt_sasl_generic_install LDAP_P(( Sockbuf *sb, 335 struct sb_sasl_generic_install *install_arg )); 336 LDAP_F (void) ldap_pvt_sasl_generic_remove LDAP_P(( Sockbuf *sb )); 337 338 /* search.c */ 339 LDAP_F( int ) ldap_pvt_put_filter LDAP_P(( 340 BerElement *ber, 341 const char *str )); 342 343 LDAP_F( char * ) 344 ldap_pvt_find_wildcard LDAP_P(( const char *s )); 345 346 LDAP_F( ber_slen_t ) 347 ldap_pvt_filter_value_unescape LDAP_P(( char *filter )); 348 349 LDAP_F( ber_len_t ) 350 ldap_bv2escaped_filter_value_len LDAP_P(( struct berval *in )); 351 352 LDAP_F( int ) 353 ldap_bv2escaped_filter_value_x LDAP_P(( struct berval *in, struct berval *out, 354 int inplace, void *ctx )); 355 356 LDAP_F (int) ldap_pvt_search LDAP_P(( 357 struct ldap *ld, 358 LDAP_CONST char *base, 359 int scope, 360 LDAP_CONST char *filter, 361 char **attrs, 362 int attrsonly, 363 struct ldapcontrol **sctrls, 364 struct ldapcontrol **cctrls, 365 struct timeval *timeout, 366 int sizelimit, 367 int deref, 368 int *msgidp )); 369 370 LDAP_F(int) ldap_pvt_search_s LDAP_P(( 371 struct ldap *ld, 372 LDAP_CONST char *base, 373 int scope, 374 LDAP_CONST char *filter, 375 char **attrs, 376 int attrsonly, 377 struct ldapcontrol **sctrls, 378 struct ldapcontrol **cctrls, 379 struct timeval *timeout, 380 int sizelimit, 381 int deref, 382 struct ldapmsg **res )); 383 384 /* string.c */ 385 LDAP_F( char * ) 386 ldap_pvt_str2upper LDAP_P(( char *str )); 387 388 LDAP_F( char * ) 389 ldap_pvt_str2lower LDAP_P(( char *str )); 390 391 LDAP_F( struct berval * ) 392 ldap_pvt_str2upperbv LDAP_P(( char *str, struct berval *bv )); 393 394 LDAP_F( struct berval * ) 395 ldap_pvt_str2lowerbv LDAP_P(( char *str, struct berval *bv )); 396 397 /* tls.c */ 398 LDAP_F (int) ldap_int_tls_config LDAP_P(( struct ldap *ld, 399 int option, const char *arg )); 400 LDAP_F (int) ldap_pvt_tls_get_option LDAP_P(( struct ldap *ld, 401 int option, void *arg )); 402 LDAP_F (int) ldap_pvt_tls_set_option LDAP_P(( struct ldap *ld, 403 int option, void *arg )); 404 405 LDAP_F (void) ldap_pvt_tls_destroy LDAP_P(( void )); 406 LDAP_F (int) ldap_pvt_tls_init LDAP_P(( void )); 407 LDAP_F (int) ldap_pvt_tls_init_def_ctx LDAP_P(( int is_server )); 408 LDAP_F (int) ldap_pvt_tls_accept LDAP_P(( Sockbuf *sb, void *ctx_arg )); 409 LDAP_F (int) ldap_pvt_tls_inplace LDAP_P(( Sockbuf *sb )); 410 LDAP_F (void *) ldap_pvt_tls_sb_ctx LDAP_P(( Sockbuf *sb )); 411 LDAP_F (void) ldap_pvt_tls_ctx_free LDAP_P(( void * )); 412 413 typedef int LDAPDN_rewrite_dummy LDAP_P (( void *dn, unsigned flags )); 414 415 typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl, 416 void *ctx, void *arg )); 417 418 LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn, 419 LDAPDN_rewrite_dummy *func, unsigned flags )); 420 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn, 421 LDAPDN_rewrite_dummy *func, unsigned flags )); 422 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx )); 423 424 LDAP_END_DECL 425 426 /* 427 * Multiple precision stuff 428 * 429 * May use OpenSSL's BIGNUM if built with TLS, 430 * or GNU's multiple precision library. But if 431 * long long is available, that's big enough 432 * and much more efficient. 433 * 434 * If none is available, unsigned long data is used. 435 */ 436 437 LDAP_BEGIN_DECL 438 439 #ifdef USE_MP_BIGNUM 440 /* 441 * Use OpenSSL's BIGNUM 442 */ 443 #include <openssl/crypto.h> 444 #include <openssl/bn.h> 445 446 typedef BIGNUM* ldap_pvt_mp_t; 447 #define LDAP_PVT_MP_INIT (NULL) 448 449 #define ldap_pvt_mp_init(mp) \ 450 do { (mp) = BN_new(); } while (0) 451 452 /* FIXME: we rely on mpr being initialized */ 453 #define ldap_pvt_mp_init_set(mpr,mpv) \ 454 do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0) 455 456 #define ldap_pvt_mp_add(mpr,mpv) \ 457 BN_add((mpr), (mpr), (mpv)) 458 459 #define ldap_pvt_mp_add_ulong(mp,v) \ 460 BN_add_word((mp), (v)) 461 462 #define ldap_pvt_mp_clear(mp) \ 463 do { BN_free((mp)); (mp) = 0; } while (0) 464 465 #elif defined(USE_MP_GMP) 466 /* 467 * Use GNU's multiple precision library 468 */ 469 #include <gmp.h> 470 471 typedef mpz_t ldap_pvt_mp_t; 472 #define LDAP_PVT_MP_INIT { 0 } 473 474 #define ldap_pvt_mp_init(mp) \ 475 mpz_init((mp)) 476 477 #define ldap_pvt_mp_init_set(mpr,mpv) \ 478 mpz_init_set((mpr), (mpv)) 479 480 #define ldap_pvt_mp_add(mpr,mpv) \ 481 mpz_add((mpr), (mpr), (mpv)) 482 483 #define ldap_pvt_mp_add_ulong(mp,v) \ 484 mpz_add_ui((mp), (mp), (v)) 485 486 #define ldap_pvt_mp_clear(mp) \ 487 mpz_clear((mp)) 488 489 #else 490 /* 491 * Use unsigned long long 492 */ 493 494 #ifdef USE_MP_LONG_LONG 495 typedef unsigned long long ldap_pvt_mp_t; 496 #define LDAP_PVT_MP_INIT (0LL) 497 #elif defined(USE_MP_LONG) 498 typedef unsigned long ldap_pvt_mp_t; 499 #define LDAP_PVT_MP_INIT (0L) 500 #elif defined(HAVE_LONG_LONG) 501 typedef unsigned long long ldap_pvt_mp_t; 502 #define LDAP_PVT_MP_INIT (0LL) 503 #else 504 typedef unsigned long ldap_pvt_mp_t; 505 #define LDAP_PVT_MP_INIT (0L) 506 #endif 507 508 #define ldap_pvt_mp_init(mp) \ 509 do { (mp) = 0; } while (0) 510 511 #define ldap_pvt_mp_init_set(mpr,mpv) \ 512 do { (mpr) = (mpv); } while (0) 513 514 #define ldap_pvt_mp_add(mpr,mpv) \ 515 do { (mpr) += (mpv); } while (0) 516 517 #define ldap_pvt_mp_add_ulong(mp,v) \ 518 do { (mp) += (v); } while (0) 519 520 #define ldap_pvt_mp_clear(mp) \ 521 do { (mp) = 0; } while (0) 522 523 #endif /* MP */ 524 525 #include "ldap_pvt_uc.h" 526 527 LDAP_END_DECL 528 529 LDAP_BEGIN_DECL 530 531 #include <limits.h> /* get CHAR_BIT */ 532 533 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */ 534 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3) 535 536 LDAP_END_DECL 537 538 #endif /* _LDAP_PVT_H */ 539