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