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