From 596027271333ee7091348326751ef48ee679443a Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Mon, 9 Nov 1998 18:40:37 +0000 Subject: [PATCH] Split out ldapoptions from struct ldap to facilate global options. Add openldap_ldap_initialize() call to ldap_init/ldap_open/ldap_get/set_options. --- libraries/libldap/init.c | 2 +- libraries/libldap/ldap-int.h | 59 +++++++++++++++++++++----------- libraries/libldap/open.c | 14 +++++--- libraries/libldap/options.c | 65 +++++++++++++++++++++++------------- libraries/libldap/request.c | 2 +- libraries/libldap/result.c | 15 ++++++--- libraries/libldap/test.c | 6 ++-- 7 files changed, 105 insertions(+), 58 deletions(-) diff --git a/libraries/libldap/init.c b/libraries/libldap/init.c index 1074bf96be..f457511493 100644 --- a/libraries/libldap/init.c +++ b/libraries/libldap/init.c @@ -9,7 +9,7 @@ #include "ldap-int.h" -struct ldap openldap_ld_globals; +struct ldapoptions openldap_ldap_global_options; int openldap_ldap_initialized = 0; diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h index 5bed6b379b..89402feec1 100644 --- a/libraries/libldap/ldap-int.h +++ b/libraries/libldap/ldap-int.h @@ -34,12 +34,13 @@ LDAP_BEGIN_DECL #define LDAP_BOOL_RESTART 1 #define LDAP_BOOL_DNS 2 +#define LDAP_BOOLEANS unsigned long #define LDAP_BOOL(n) (1 << (n)) -#define LDAP_BOOL_GET(ld, bool) ((ld)->ld_booleans & LDAP_BOOL(bool) \ +#define LDAP_BOOL_GET(lo, bool) ((lo)->ldo_booleans & LDAP_BOOL(bool) \ ? LDAP_OPT_ON : LDAP_OPT_OFF) -#define LDAP_BOOL_SET(ld, bool) ((ld)->ld_booleans |= LDAP_BOOL(bool)) -#define LDAP_BOOL_CLR(ld, bool) ((ld)->ld_booleans &= ~LDAP_BOOL(bool)) -#define LDAP_BOOL_ZERO(ld) ((ld)->ld_booleans = 0) +#define LDAP_BOOL_SET(lo, bool) ((lo)->ldo_booleans |= LDAP_BOOL(bool)) +#define LDAP_BOOL_CLR(lo, bool) ((lo)->ldo_booleans &= ~LDAP_BOOL(bool)) +#define LDAP_BOOL_ZERO(lo) ((lo)->ldo_booleans = 0) /* * This structure represents both ldap messages and ldap responses. @@ -56,25 +57,50 @@ struct ldapmsg { unsigned int lm_time; /* used to maintain cache */ }; +/* + * structure representing get/set'able options + * which have global defaults. + */ +struct ldapoptions { + int ldo_version; /* version to connect at */ + int ldo_deref; + int ldo_timelimit; + int ldo_sizelimit; + + int ldo_cldaptries; /* connectionless search retry count */ + int ldo_cldaptimeout;/* time between retries */ + int ldo_refhoplimit; /* limit on referral nesting */ + + LDAP_BOOLEANS ldo_booleans; /* boolean options */ +}; + /* * structure representing an ldap connection */ struct ldap { Sockbuf ld_sb; /* socket descriptor & buffer */ - char *ld_host; - int ld_version; - char ld_lberoptions; - int ld_deref; - int ld_timelimit; - int ld_sizelimit; + + struct ldapoptions ld_options; + +#define ld_deref ld_options.ldo_deref +#define ld_timelimit ld_options.ldo_timelimit +#define ld_sizelimit ld_options.ldo_sizelimit + +#define ld_cldaptries ld_options.ldo_cldaptries +#define ld_cldaptimeout ld_options.ldo_cldaptimeout +#define ld_refhoplimit ld_options.ldo_refhoplimit + + int ld_version; /* version connected at */ + char *ld_host; + char ld_lberoptions; LDAPFiltDesc *ld_filtd; /* from getfilter for ufn searches */ char *ld_ufnprefix; /* for incomplete ufn's */ int ld_errno; - char *ld_error; - char *ld_matched; + char *ld_error; + char *ld_matched; int ld_msgid; /* do not mess with these */ @@ -89,17 +115,12 @@ struct ldap { LDAPCache *ld_cache; /* non-null if cache is initialized */ char *ld_cldapdn; /* DN used in connectionless search */ - /* it is OK to change these next four values directly */ - int ld_cldaptries; /* connectionless search retry count */ - int ld_cldaptimeout;/* time between retries */ - int ld_refhoplimit; /* limit on referral nesting */ - unsigned long ld_booleans; /* boolean options */ - /* do not mess with the rest though */ char *ld_defhost; /* full name of default server */ int ld_defport; /* port of default server */ BERTranslateProc ld_lber_encode_translate_proc; BERTranslateProc ld_lber_decode_translate_proc; + #ifdef LDAP_REFERRALS LDAPConn *ld_defconn; /* default connection */ LDAPConn *ld_conns; /* list of server connections */ @@ -115,7 +136,7 @@ struct ldap { * in init.c */ extern int openldap_ldap_initialized; -extern struct ldap openldap_ld_globals; +extern struct ldapoptions openldap_ldap_global_options; void openldap_ldap_initialize LDAP_P((void)); /* diff --git a/libraries/libldap/open.c b/libraries/libldap/open.c index 303c3ef4f7..0cbb873607 100644 --- a/libraries/libldap/open.c +++ b/libraries/libldap/open.c @@ -99,6 +99,10 @@ ldap_init( char *defhost, int defport ) { LDAP *ld; + if(!openldap_ldap_initialized) { + openldap_ldap_initialize(); + } + Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 ); #ifdef HAVE_WINSOCK2 @@ -149,10 +153,10 @@ ldap_init( char *defhost, int defport ) return( NULL ); } - LDAP_BOOL_ZERO(ld); - LDAP_BOOL_SET(ld, LDAP_BOOL_REFERRALS); + LDAP_BOOL_ZERO(&ld->ld_options); + LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS); #else - LDAP_BOOL_ZERO(ld); + LDAP_BOOL_ZERO(&ld->ld_options); #endif if ( defhost != NULL && @@ -169,10 +173,10 @@ ldap_init( char *defhost, int defport ) ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport; ld->ld_version = LDAP_VERSION; ld->ld_lberoptions = LBER_USE_DER; - ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT; + ld->ld_options.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT; #ifdef LDAP_REFERRALS - LDAP_BOOL_SET(ld, LDAP_BOOL_REFERRALS); + LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS); #endif /* LDAP_REFERRALS */ #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET ) diff --git a/libraries/libldap/options.c b/libraries/libldap/options.c index 66ccfbd523..b52e95cc78 100644 --- a/libraries/libldap/options.c +++ b/libraries/libldap/options.c @@ -10,11 +10,11 @@ int ldap_get_option( - LDAP *ldp, + LDAP *ld, int option, void *outvalue) { - LDAP *ld; + struct ldapoptions *lo; if(!openldap_ldap_initialized) { openldap_ldap_initialize(); @@ -25,10 +25,10 @@ ldap_get_option( return -1; } - if(ldp == NULL) { - ld = &openldap_ld_globals; + if(ld == NULL) { + lo = &openldap_ldap_global_options; } else { - ld = ldp; + lo = &ld->ld_options; } switch(option) { @@ -57,7 +57,7 @@ ldap_get_option( } break; case LDAP_OPT_DESC: - if(ldp == NULL) { + if(ld == NULL) { /* bad param */ break; } @@ -66,30 +66,35 @@ ldap_get_option( return 0; case LDAP_OPT_DEREF: - * (int *) outvalue = ld->ld_deref; + * (int *) outvalue = lo->ldo_deref; return 0; case LDAP_OPT_SIZELIMIT: - * (int *) outvalue = ld->ld_sizelimit; + * (int *) outvalue = lo->ldo_sizelimit; return 0; case LDAP_OPT_TIMELIMIT: - * (int *) outvalue = ld->ld_timelimit; + * (int *) outvalue = lo->ldo_timelimit; return 0; case LDAP_OPT_REFERRALS: - * (int *) outvalue = (int) LDAP_BOOL_GET(ld, LDAP_BOOL_REFERRALS); + * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS); return 0; case LDAP_OPT_RESTART: - * (int *) outvalue = (int) LDAP_BOOL_GET(ld, LDAP_BOOL_RESTART); + * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART); return 0; case LDAP_OPT_DNS: /* LDAPv2 */ - * (int *) outvalue = (int) LDAP_BOOL_GET(ld, LDAP_BOOL_DNS); + * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_DNS); return 0; case LDAP_OPT_PROTOCOL_VERSION: + if(ld == NULL) { + /* bad param */ + break; + } + * (int *) outvalue = ld->ld_version; return 0; @@ -99,15 +104,27 @@ ldap_get_option( break; case LDAP_OPT_HOST_NAME: + if(ld == NULL) { + /* bad param */ + break; + } * (char **) outvalue = ld->ld_host; return 0; case LDAP_OPT_ERROR_NUMBER: + if(ld == NULL) { + /* bad param */ + break; + } * (int *) outvalue = ld->ld_errno; return 0; case LDAP_OPT_ERROR_STRING: /* not yet supported */ + if(ld == NULL) { + /* bad param */ + break; + } break; default: @@ -120,11 +137,11 @@ ldap_get_option( int ldap_set_option( - LDAP *ldp, + LDAP *ld, int option, void *invalue) { - LDAP *ld; + struct ldapoptions *lo; if(!openldap_ldap_initialized) { openldap_ldap_initialize(); @@ -135,10 +152,10 @@ ldap_set_option( return -1; } - if(ldp == NULL) { - ld = &openldap_ld_globals; + if(ld == NULL) { + lo = &openldap_ldap_global_options; } else { - ld = ldp; + lo = &ld->ld_options; } switch(option) { @@ -148,30 +165,30 @@ ldap_set_option( break; case LDAP_OPT_DEREF: - ld->ld_deref = * (int *) invalue; + lo->ldo_deref = * (int *) invalue; return 0; case LDAP_OPT_SIZELIMIT: - ld->ld_sizelimit = * (int *) invalue; + lo->ldo_sizelimit = * (int *) invalue; return 0; case LDAP_OPT_TIMELIMIT: - ld->ld_timelimit = * (int *) invalue; + lo->ldo_timelimit = * (int *) invalue; return 0; case LDAP_OPT_REFERRALS: if((int) invalue == (int) LDAP_OPT_ON) { - LDAP_BOOL_SET(ld, LDAP_BOOL_REFERRALS); + LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS); } else { - LDAP_BOOL_CLR(ld, LDAP_BOOL_REFERRALS); + LDAP_BOOL_CLR(lo, LDAP_BOOL_REFERRALS); } return 0; case LDAP_OPT_RESTART: if((int) invalue == (int) LDAP_OPT_ON) { - LDAP_BOOL_SET(ld, LDAP_BOOL_RESTART); + LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART); } else { - LDAP_BOOL_CLR(ld, LDAP_BOOL_RESTART); + LDAP_BOOL_CLR(lo, LDAP_BOOL_RESTART); } return 0; diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c index de6ed9492f..7de337c59e 100644 --- a/libraries/libldap/request.c +++ b/libraries/libldap/request.c @@ -91,7 +91,7 @@ ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn, #else /* !LDAP_REFERRALS && !LDAP_DNS */ #ifdef LDAP_DNS - if (( LDAP_BOOL_GET(ld, LDAP_BOOL_DNS ) == LDAP_OPT_ON ) + if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON ) && ldap_is_dns_dn( dn ) ) { if (( servers = dn2servers( ld, dn )) == NULL ) { diff --git a/libraries/libldap/result.c b/libraries/libldap/result.c index c022a993c3..182a13ccd7 100644 --- a/libraries/libldap/result.c +++ b/libraries/libldap/result.c @@ -169,7 +169,8 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout, rc = ldap_select1( ld, tvp ); if ( rc == 0 || ( rc == -1 && ( - (LDAP_BOOL_GET(ld, LDAP_BOOL_RESTART) == LDAP_OPT_OFF) + ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART) + == LDAP_OPT_OFF ) || errno != EINTR ))) { ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN : LDAP_TIMEOUT); @@ -211,8 +212,10 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout, #endif if ( rc == 0 || ( rc == -1 && ( - (LDAP_BOOL_GET(ld, LDAP_BOOL_RESTART) == LDAP_OPT_OFF) - || errno != EINTR ))) { + ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART) + == LDAP_OPT_OFF ) + || errno != EINTR ))) + { ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN : LDAP_TIMEOUT); return( rc ); @@ -327,8 +330,10 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb, if ( tag != LDAP_RES_SEARCH_ENTRY ) { if ( ld->ld_version >= LDAP_VERSION2 && - ( lr->lr_parent != NULL || - (LDAP_BOOL_GET(ld, LDAP_BOOL_REFERRALS) != LDAP_OPT_OFF))) { + ( lr->lr_parent != NULL || + ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) + != LDAP_OPT_OFF ) ) ) + { tmpber = ber; /* struct copy */ if ( ber_scanf( &tmpber, "{iaa}", &lderr, &lr->lr_res_matched, &lr->lr_res_error ) diff --git a/libraries/libldap/test.c b/libraries/libldap/test.c index 6abfbe2bf7..43c02ca8fe 100644 --- a/libraries/libldap/test.c +++ b/libraries/libldap/test.c @@ -750,7 +750,7 @@ main( int argc, char **argv ) getline( line, sizeof(line), stdin, "sizelimit?" ); ld->ld_sizelimit = atoi( line ); - LDAP_BOOL_ZERO(ld); + LDAP_BOOL_ZERO(&ld->ld_options); #ifdef STR_TRANSLATION getline( line, sizeof(line), stdin, @@ -775,7 +775,7 @@ main( int argc, char **argv ) getline( line, sizeof(line), stdin, "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" ); if ( atoi( line ) != 0 ) { - LDAP_BOOL_SET(ld, LDAP_BOOL_DNS); + LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS); } #endif /* LDAP_DNS */ @@ -783,7 +783,7 @@ main( int argc, char **argv ) getline( line, sizeof(line), stdin, "Recognize and chase referrals (0=no, 1=yes)?" ); if ( atoi( line ) != 0 ) { - LDAP_BOOL_SET(ld, LDAP_BOOL_REFERRALS); + LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS); getline( line, sizeof(line), stdin, "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" ); if ( atoi( line ) != 0 ) { -- 2.39.5