Back to index

openldap  2.4.31
ldap_pvt.h
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  * 
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 /* ldap-pvt.h - Header for ldap_pvt_ functions.
00017  * These are meant to be internal to OpenLDAP Software.
00018  */
00019 
00020 #ifndef _LDAP_PVT_H
00021 #define _LDAP_PVT_H 1
00022 
00023 #include <lber.h>                         /* get ber_slen_t */
00024 #include <lber_pvt.h>                            /* get Sockbuf_Buf */
00025 
00026 LDAP_BEGIN_DECL
00027 
00028 #define LDAP_PROTO_TCP 1 /* ldap://  */
00029 #define LDAP_PROTO_UDP 2 /* reserved */
00030 #define LDAP_PROTO_IPC 3 /* ldapi:// */
00031 #define LDAP_PROTO_EXT 4 /* user-defined socket/sockbuf */
00032 
00033 LDAP_F ( int )
00034 ldap_pvt_url_scheme2proto LDAP_P((
00035        const char * ));
00036 LDAP_F ( int )
00037 ldap_pvt_url_scheme2tls LDAP_P((
00038        const char * ));
00039 
00040 LDAP_F ( int )
00041 ldap_pvt_url_scheme_port LDAP_P((
00042        const char *, int ));
00043 
00044 struct ldap_url_desc; /* avoid pulling in <ldap.h> */
00045 
00046 #define LDAP_PVT_URL_PARSE_NONE                  (0x00U)
00047 #define LDAP_PVT_URL_PARSE_NOEMPTY_HOST          (0x01U)
00048 #define LDAP_PVT_URL_PARSE_DEF_PORT              (0x02U)
00049 #define LDAP_PVT_URL_PARSE_NOEMPTY_DN            (0x04U)
00050 #define LDAP_PVT_URL_PARSE_NODEF_SCOPE           (0x08U)
00051 #define       LDAP_PVT_URL_PARSE_HISTORIC        (LDAP_PVT_URL_PARSE_NODEF_SCOPE | \
00052                                            LDAP_PVT_URL_PARSE_NOEMPTY_HOST | \
00053                                            LDAP_PVT_URL_PARSE_DEF_PORT)
00054 
00055 LDAP_F( int )
00056 ldap_url_parse_ext LDAP_P((
00057        LDAP_CONST char *url,
00058        struct ldap_url_desc **ludpp,
00059        unsigned flags ));
00060 
00061 LDAP_F (int) ldap_url_parselist LDAP_P((  /* deprecated, use ldap_url_parselist_ext() */
00062        struct ldap_url_desc **ludlist,
00063        const char *url ));
00064 
00065 LDAP_F (int) ldap_url_parselist_ext LDAP_P((
00066        struct ldap_url_desc **ludlist,
00067        const char *url,
00068        const char *sep,
00069        unsigned flags ));
00070 
00071 LDAP_F (char *) ldap_url_list2urls LDAP_P((
00072        struct ldap_url_desc *ludlist ));
00073 
00074 LDAP_F (void) ldap_free_urllist LDAP_P((
00075        struct ldap_url_desc *ludlist ));
00076 
00077 LDAP_F (int) ldap_pvt_scope2bv LDAP_P ((
00078        int scope, struct berval *bv ));
00079 
00080 LDAP_F (LDAP_CONST char *) ldap_pvt_scope2str LDAP_P ((
00081        int scope ));
00082 
00083 LDAP_F (int) ldap_pvt_bv2scope LDAP_P ((
00084        struct berval *bv ));
00085 
00086 LDAP_F (int) ldap_pvt_str2scope LDAP_P ((
00087        LDAP_CONST char * ));
00088 
00089 LDAP_F( char * )
00090 ldap_pvt_ctime LDAP_P((
00091        const time_t *tp,
00092        char *buf ));
00093 
00094 # if defined( HAVE_GMTIME_R )
00095 #   define USE_GMTIME_R
00096 #   define ldap_pvt_gmtime(timep, result) gmtime_r((timep), (result))
00097 # else
00098 LDAP_F( struct tm * )
00099 ldap_pvt_gmtime LDAP_P((
00100        LDAP_CONST time_t *timep,
00101        struct tm *result ));
00102 #endif
00103 
00104 # if defined( HAVE_LOCALTIME_R )
00105 #   define USE_LOCALTIME_R
00106 #   define ldap_pvt_localtime(timep, result) localtime_r((timep), (result))
00107 # else
00108 LDAP_F( struct tm * )
00109 ldap_pvt_localtime LDAP_P((
00110        LDAP_CONST time_t *timep,
00111        struct tm *result ));
00112 # endif
00113 
00114 #if defined( USE_GMTIME_R ) && defined( USE_LOCALTIME_R )
00115 #   define ldap_pvt_gmtime_lock() (0)
00116 #   define ldap_pvt_gmtime_unlock() (0)
00117 #else
00118 LDAP_F( int )
00119 ldap_pvt_gmtime_lock LDAP_P(( void ));
00120 
00121 LDAP_F( int )
00122 ldap_pvt_gmtime_unlock LDAP_P(( void ));
00123 #endif /* USE_GMTIME_R && USE_LOCALTIME_R */
00124 
00125 /* Get current time as a structured time */
00126 struct lutil_tm;
00127 LDAP_F( void )
00128 ldap_pvt_gettime LDAP_P(( struct lutil_tm * ));
00129 
00130 /* use this macro to allocate buffer for ldap_pvt_csnstr */
00131 #define LDAP_PVT_CSNSTR_BUFSIZE    64
00132 LDAP_F( size_t )
00133 ldap_pvt_csnstr( char *buf, size_t len, unsigned int replica, unsigned int mod );
00134 
00135 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
00136 
00137 struct hostent;      /* avoid pulling in <netdb.h> */
00138 
00139 LDAP_F( int )
00140 ldap_pvt_gethostbyname_a LDAP_P((
00141        const char *name,
00142        struct hostent *resbuf,
00143        char **buf,
00144        struct hostent **result,
00145        int *herrno_ptr ));
00146 
00147 LDAP_F( int )
00148 ldap_pvt_gethostbyaddr_a LDAP_P((
00149        const char *addr,
00150        int len,
00151        int type,
00152        struct hostent *resbuf,
00153        char **buf,
00154        struct hostent **result,
00155        int *herrno_ptr ));
00156 
00157 struct sockaddr;
00158 
00159 LDAP_F( int )
00160 ldap_pvt_get_hname LDAP_P((
00161        const struct sockaddr * sa,
00162        int salen,
00163        char *name,
00164        int namelen,
00165        char **herr ));
00166 
00167 
00168 /* charray.c */
00169 
00170 LDAP_F( int )
00171 ldap_charray_add LDAP_P((
00172     char      ***a,
00173     const char *s ));
00174 
00175 LDAP_F( int )
00176 ldap_charray_merge LDAP_P((
00177     char      ***a,
00178     char      **s ));
00179 
00180 LDAP_F( void )
00181 ldap_charray_free LDAP_P(( char **a ));
00182 
00183 LDAP_F( int )
00184 ldap_charray_inlist LDAP_P((
00185     char      **a,
00186     const char *s ));
00187 
00188 LDAP_F( char ** )
00189 ldap_charray_dup LDAP_P(( char **a ));
00190 
00191 LDAP_F( char ** )
00192 ldap_str2charray LDAP_P((
00193        const char *str,
00194        const char *brkstr ));
00195 
00196 LDAP_F( char * )
00197 ldap_charray2str LDAP_P((
00198        char **array, const char* sep ));
00199 
00200 /* getdn.c */
00201 
00202 #ifdef LDAP_AVA_NULL /* in ldap.h */
00203 LDAP_F( void ) ldap_rdnfree_x LDAP_P(( LDAPRDN rdn, void *ctx ));
00204 LDAP_F( void ) ldap_dnfree_x LDAP_P(( LDAPDN dn, void *ctx ));
00205 
00206 LDAP_F( int ) ldap_bv2dn_x LDAP_P(( 
00207        struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx ));
00208 LDAP_F( int ) ldap_dn2bv_x LDAP_P(( 
00209        LDAPDN dn, struct berval *bv, unsigned flags, void *ctx ));
00210 LDAP_F( int ) ldap_bv2rdn_x LDAP_P(( 
00211        struct berval *, LDAPRDN *, char **, unsigned flags, void *ctx ));
00212 LDAP_F( int ) ldap_rdn2bv_x LDAP_P(( 
00213        LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx ));
00214 #endif /* LDAP_AVA_NULL */
00215 
00216 /* url.c */
00217 LDAP_F (void) ldap_pvt_hex_unescape LDAP_P(( char *s ));
00218 
00219 /*
00220  * these macros assume 'x' is an ASCII x
00221  * and assume the "C" locale
00222  */
00223 #define LDAP_ASCII(c)              (!((c) & 0x80))
00224 #define LDAP_SPACE(c)              ((c) == ' ' || (c) == '\t' || (c) == '\n')
00225 #define LDAP_DIGIT(c)              ((c) >= '0' && (c) <= '9')
00226 #define LDAP_LOWER(c)              ((c) >= 'a' && (c) <= 'z')
00227 #define LDAP_UPPER(c)              ((c) >= 'A' && (c) <= 'Z')
00228 #define LDAP_ALPHA(c)              (LDAP_LOWER(c) || LDAP_UPPER(c))
00229 #define LDAP_ALNUM(c)              (LDAP_ALPHA(c) || LDAP_DIGIT(c))
00230 
00231 #define LDAP_LDH(c)                (LDAP_ALNUM(c) || (c) == '-')
00232 
00233 #define LDAP_HEXLOWER(c)    ((c) >= 'a' && (c) <= 'f')
00234 #define LDAP_HEXUPPER(c)    ((c) >= 'A' && (c) <= 'F')
00235 #define LDAP_HEX(c)                (LDAP_DIGIT(c) || \
00236                                                         LDAP_HEXLOWER(c) || LDAP_HEXUPPER(c))
00237 
00238 /* controls.c */
00239 struct ldapcontrol;
00240 LDAP_F (int)
00241 ldap_pvt_put_control LDAP_P((
00242        const struct ldapcontrol *c,
00243        BerElement *ber ));
00244 LDAP_F (int) ldap_pvt_get_controls LDAP_P((
00245        BerElement *be,
00246        struct ldapcontrol ***ctrlsp));
00247 
00248 #ifdef HAVE_CYRUS_SASL
00249 /* cyrus.c */
00250 struct sasl_security_properties; /* avoid pulling in <sasl.h> */
00251 LDAP_F (int) ldap_pvt_sasl_secprops LDAP_P((
00252        const char *in,
00253        struct sasl_security_properties *secprops ));
00254 LDAP_F (void) ldap_pvt_sasl_secprops_unparse LDAP_P((
00255        struct sasl_security_properties *secprops,
00256        struct berval *out ));
00257 
00258 LDAP_F (void *) ldap_pvt_sasl_mutex_new LDAP_P((void));
00259 LDAP_F (int) ldap_pvt_sasl_mutex_lock LDAP_P((void *mutex));
00260 LDAP_F (int) ldap_pvt_sasl_mutex_unlock LDAP_P((void *mutex));
00261 LDAP_F (void) ldap_pvt_sasl_mutex_dispose LDAP_P((void *mutex));
00262 #endif /* HAVE_CYRUS_SASL */
00263 
00264 struct sockbuf; /* avoid pulling in <lber.h> */
00265 LDAP_F (int) ldap_pvt_sasl_install LDAP_P(( struct sockbuf *, void * ));
00266 LDAP_F (void) ldap_pvt_sasl_remove LDAP_P(( struct sockbuf * ));
00267 
00268 /*
00269  * SASL encryption support for LBER Sockbufs
00270  */
00271 
00272 struct sb_sasl_generic_data;
00273 
00274 struct sb_sasl_generic_ops {
00275        void (*init)(struct sb_sasl_generic_data *p,
00276                    ber_len_t *min_send,
00277                    ber_len_t *max_send,
00278                    ber_len_t *max_recv);
00279        ber_int_t (*encode)(struct sb_sasl_generic_data *p,
00280                          unsigned char *buf,
00281                          ber_len_t len,
00282                          Sockbuf_Buf *dst);
00283        ber_int_t (*decode)(struct sb_sasl_generic_data *p,
00284                          const Sockbuf_Buf *src,
00285                          Sockbuf_Buf *dst);
00286        void (*reset_buf)(struct sb_sasl_generic_data *p,
00287                        Sockbuf_Buf *buf);
00288        void (*fini)(struct sb_sasl_generic_data *p);
00289 };
00290 
00291 struct sb_sasl_generic_install {
00292        const struct sb_sasl_generic_ops   *ops;
00293        void                               *ops_private;
00294 };
00295 
00296 struct sb_sasl_generic_data {
00297        const struct sb_sasl_generic_ops   *ops;
00298        void                               *ops_private;
00299        Sockbuf_IO_Desc                           *sbiod;
00300        ber_len_t                          min_send;
00301        ber_len_t                          max_send;
00302        ber_len_t                          max_recv;
00303        Sockbuf_Buf                        sec_buf_in;
00304        Sockbuf_Buf                        buf_in;
00305        Sockbuf_Buf                        buf_out;
00306        unsigned int                       flags;
00307 #define LDAP_PVT_SASL_PARTIAL_WRITE       1
00308 };
00309  
00310 #ifndef LDAP_PVT_SASL_LOCAL_SSF
00311 #define LDAP_PVT_SASL_LOCAL_SSF    71     /* SSF for Unix Domain Sockets */
00312 #endif /* ! LDAP_PVT_SASL_LOCAL_SSF */
00313 
00314 struct ldap;
00315 struct ldapmsg;
00316 
00317 /* abandon */
00318 LDAP_F ( int ) ldap_pvt_discard LDAP_P((
00319        struct ldap *ld, ber_int_t msgid ));
00320 
00321 /* messages.c */
00322 LDAP_F( BerElement * )
00323 ldap_get_message_ber LDAP_P((
00324        struct ldapmsg * ));
00325 
00326 /* open */
00327 LDAP_F (int) ldap_open_internal_connection LDAP_P((
00328        struct ldap **ldp, ber_socket_t *fdp ));
00329 LDAP_F (int) ldap_init_fd LDAP_P((
00330        ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap **ldp ));
00331 
00332 /* sasl.c */
00333 LDAP_F (int) ldap_pvt_sasl_generic_install LDAP_P(( Sockbuf *sb,
00334        struct sb_sasl_generic_install *install_arg ));
00335 LDAP_F (void) ldap_pvt_sasl_generic_remove LDAP_P(( Sockbuf *sb ));
00336 
00337 /* search.c */
00338 LDAP_F( int ) ldap_pvt_put_filter LDAP_P((
00339        BerElement *ber,
00340        const char *str ));
00341 
00342 LDAP_F( char * )
00343 ldap_pvt_find_wildcard LDAP_P((    const char *s ));
00344 
00345 LDAP_F( ber_slen_t )
00346 ldap_pvt_filter_value_unescape LDAP_P(( char *filter ));
00347 
00348 LDAP_F( ber_len_t )
00349 ldap_bv2escaped_filter_value_len LDAP_P(( struct berval *in ));
00350 
00351 LDAP_F( int )
00352 ldap_bv2escaped_filter_value_x LDAP_P(( struct berval *in, struct berval *out,
00353        int inplace, void *ctx ));
00354 
00355 LDAP_F (int) ldap_pvt_search LDAP_P((
00356        struct ldap *ld,
00357        LDAP_CONST char *base,
00358        int scope,
00359        LDAP_CONST char *filter,
00360        char **attrs,
00361        int attrsonly,
00362        struct ldapcontrol **sctrls,
00363        struct ldapcontrol **cctrls,
00364        struct timeval *timeout,
00365        int sizelimit,
00366        int deref,
00367        int *msgidp ));
00368 
00369 LDAP_F(int) ldap_pvt_search_s LDAP_P((
00370        struct ldap *ld,
00371        LDAP_CONST char *base,
00372        int scope,
00373        LDAP_CONST char *filter,
00374        char **attrs,
00375        int attrsonly,
00376        struct ldapcontrol **sctrls,
00377        struct ldapcontrol **cctrls,
00378        struct timeval *timeout,
00379        int sizelimit,
00380        int deref,
00381        struct ldapmsg **res ));
00382 
00383 /* string.c */
00384 LDAP_F( char * )
00385 ldap_pvt_str2upper LDAP_P(( char *str ));
00386 
00387 LDAP_F( char * )
00388 ldap_pvt_str2lower LDAP_P(( char *str ));
00389 
00390 LDAP_F( struct berval * )
00391 ldap_pvt_str2upperbv LDAP_P(( char *str, struct berval *bv ));
00392 
00393 LDAP_F( struct berval * )
00394 ldap_pvt_str2lowerbv LDAP_P(( char *str, struct berval *bv ));
00395 
00396 /* tls.c */
00397 LDAP_F (int) ldap_int_tls_config LDAP_P(( struct ldap *ld,
00398        int option, const char *arg ));
00399 LDAP_F (int) ldap_pvt_tls_get_option LDAP_P(( struct ldap *ld,
00400        int option, void *arg ));
00401 LDAP_F (int) ldap_pvt_tls_set_option LDAP_P(( struct ldap *ld,
00402        int option, void *arg ));
00403 
00404 LDAP_F (void) ldap_pvt_tls_destroy LDAP_P(( void ));
00405 LDAP_F (int) ldap_pvt_tls_init LDAP_P(( void ));
00406 LDAP_F (int) ldap_pvt_tls_init_def_ctx LDAP_P(( int is_server ));
00407 LDAP_F (int) ldap_pvt_tls_accept LDAP_P(( Sockbuf *sb, void *ctx_arg ));
00408 LDAP_F (int) ldap_pvt_tls_inplace LDAP_P(( Sockbuf *sb ));
00409 LDAP_F (void *) ldap_pvt_tls_sb_ctx LDAP_P(( Sockbuf *sb ));
00410 LDAP_F (void) ldap_pvt_tls_ctx_free LDAP_P(( void * ));
00411 
00412 typedef int LDAPDN_rewrite_dummy LDAP_P (( void *dn, unsigned flags ));
00413 
00414 typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl,
00415        void *ctx, void *arg ));
00416 
00417 LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn,
00418        LDAPDN_rewrite_dummy *func, unsigned flags ));
00419 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn,
00420        LDAPDN_rewrite_dummy *func, unsigned flags ));
00421 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx ));
00422 
00423 LDAP_END_DECL
00424 
00425 /*
00426  * Multiple precision stuff
00427  * 
00428  * May use OpenSSL's BIGNUM if built with TLS,
00429  * or GNU's multiple precision library. But if
00430  * long long is available, that's big enough
00431  * and much more efficient.
00432  *
00433  * If none is available, unsigned long data is used.
00434  */
00435 
00436 LDAP_BEGIN_DECL
00437 
00438 #ifdef USE_MP_BIGNUM
00439 /*
00440  * Use OpenSSL's BIGNUM
00441  */
00442 #include <openssl/crypto.h>
00443 #include <openssl/bn.h>
00444 
00445 typedef       BIGNUM* ldap_pvt_mp_t;
00446 #define       LDAP_PVT_MP_INIT     (NULL)
00447 
00448 #define       ldap_pvt_mp_init(mp) \
00449        do { (mp) = BN_new(); } while (0)
00450 
00451 /* FIXME: we rely on mpr being initialized */
00452 #define       ldap_pvt_mp_init_set(mpr,mpv) \
00453        do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0)
00454 
00455 #define       ldap_pvt_mp_add(mpr,mpv) \
00456        BN_add((mpr), (mpr), (mpv))
00457 
00458 #define       ldap_pvt_mp_add_ulong(mp,v) \
00459        BN_add_word((mp), (v))
00460 
00461 #define ldap_pvt_mp_clear(mp) \
00462        do { BN_free((mp)); (mp) = 0; } while (0)
00463 
00464 #elif defined(USE_MP_GMP)
00465 /*
00466  * Use GNU's multiple precision library
00467  */
00468 #include <gmp.h>
00469 
00470 typedef mpz_t        ldap_pvt_mp_t;
00471 #define       LDAP_PVT_MP_INIT     { 0 }
00472 
00473 #define ldap_pvt_mp_init(mp) \
00474        mpz_init((mp))
00475 
00476 #define       ldap_pvt_mp_init_set(mpr,mpv) \
00477        mpz_init_set((mpr), (mpv))
00478 
00479 #define       ldap_pvt_mp_add(mpr,mpv) \
00480        mpz_add((mpr), (mpr), (mpv))
00481 
00482 #define       ldap_pvt_mp_add_ulong(mp,v) \
00483        mpz_add_ui((mp), (mp), (v))
00484 
00485 #define ldap_pvt_mp_clear(mp) \
00486        mpz_clear((mp))
00487 
00488 #else
00489 /*
00490  * Use unsigned long long
00491  */
00492 
00493 #ifdef USE_MP_LONG_LONG
00494 typedef       unsigned long long   ldap_pvt_mp_t;
00495 #define       LDAP_PVT_MP_INIT     (0LL)
00496 #elif defined(USE_MP_LONG)
00497 typedef       unsigned long        ldap_pvt_mp_t;
00498 #define       LDAP_PVT_MP_INIT     (0L)
00499 #elif defined(HAVE_LONG_LONG)
00500 typedef       unsigned long long   ldap_pvt_mp_t;
00501 #define       LDAP_PVT_MP_INIT     (0LL)
00502 #else
00503 typedef       unsigned long        ldap_pvt_mp_t;
00504 #define       LDAP_PVT_MP_INIT     (0L)
00505 #endif
00506 
00507 #define ldap_pvt_mp_init(mp) \
00508        do { (mp) = 0; } while (0)
00509 
00510 #define       ldap_pvt_mp_init_set(mpr,mpv) \
00511        do { (mpr) = (mpv); } while (0)
00512 
00513 #define       ldap_pvt_mp_add(mpr,mpv) \
00514        do { (mpr) += (mpv); } while (0)
00515 
00516 #define       ldap_pvt_mp_add_ulong(mp,v) \
00517        do { (mp) += (v); } while (0)
00518 
00519 #define ldap_pvt_mp_clear(mp) \
00520        do { (mp) = 0; } while (0)
00521 
00522 #endif /* MP */
00523 
00524 #include "ldap_pvt_uc.h"
00525 
00526 LDAP_END_DECL
00527 
00528 LDAP_BEGIN_DECL
00529 
00530 #include <limits.h>                       /* get CHAR_BIT */
00531 
00532 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */
00533 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3)
00534 
00535 LDAP_END_DECL
00536 
00537 #endif /* _LDAP_PVT_H */