From b7c41e655ffc263ff826d4c9b577b205480d96ab Mon Sep 17 00:00:00 2001 From: Luke Howard Date: Mon, 18 Dec 2006 03:17:43 +0000 Subject: [PATCH] Add common -o nettimeout option to specify network timeout --- clients/tools/common.c | 54 +++++++++++++++++++++++++++++++++++++ clients/tools/common.h | 3 +++ clients/tools/ldapcompare.c | 2 +- clients/tools/ldapdelete.c | 2 +- clients/tools/ldapexop.c | 2 +- clients/tools/ldapmodify.c | 2 +- clients/tools/ldapmodrdn.c | 2 +- clients/tools/ldappasswd.c | 2 +- clients/tools/ldapsearch.c | 2 +- clients/tools/ldapwhoami.c | 2 +- 10 files changed, 65 insertions(+), 8 deletions(-) diff --git a/clients/tools/common.c b/clients/tools/common.c index c6fc018af3..9347c396c8 100644 --- a/clients/tools/common.c +++ b/clients/tools/common.c @@ -125,6 +125,9 @@ static int chainingResolve = -1; static int chainingContinuation = -1; #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */ +/* options */ +struct timeval nettimeout = { -1 , 0 }; + typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl ); static int print_preread( LDAP *ld, LDAPControl *ctrl ); @@ -220,6 +223,8 @@ N_(" -K like -k, but do only step 1 of the Kerberos bind\n"), N_(" -M enable Manage DSA IT control (-MM to make critical)\n"), N_(" -n show what would be done but don't actually do it\n"), N_(" -O props SASL security properties\n"), +N_(" -o [= (in seconds, or \"none\" or \"max\")\n"), N_(" -p port port on LDAP server\n"), N_(" -P version procotol version (default: 3)\n"), N_(" -Q use SASL Quiet mode\n"), @@ -590,6 +595,45 @@ tool_args( int argc, char **argv ) case 'n': /* print operations, don't actually do them */ dont++; break; + case 'o': + control = ber_strdup( optarg ); + if ( (cvalue = strchr( control, '=' )) != NULL ) { + *cvalue++ = '\0'; + } + + if ( strcasecmp( control, "nettimeout" ) == 0 ) { + if( nettimeout.tv_sec != -1 ) { + fprintf( stderr, "nettimeout option previously specified\n"); + exit( EXIT_FAILURE ); + } + if( cvalue == NULL || cvalue[0] == '\0' ) { + fprintf( stderr, "nettimeout: option value expected\n" ); + usage(); + } + if ( strcasecmp( cvalue, "none" ) == 0 ) { + nettimeout.tv_sec = 0; + } else if ( strcasecmp( cvalue, "max" ) == 0 ) { + nettimeout.tv_sec = LDAP_MAXINT; + } else { + ival = strtol( cvalue, &next, 10 ); + if ( next == NULL || next[0] != '\0' ) { + fprintf( stderr, + _("Unable to parse network timeout \"%s\"\n"), cvalue ); + exit( EXIT_FAILURE ); + } + nettimeout.tv_sec = ival; + } + if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) { + fprintf( stderr, _("%s: invalid network timeout (%d) specified\n"), + prog, nettimeout.tv_sec ); + exit( EXIT_FAILURE ); + } + } else { + fprintf( stderr, "Invalid general option name: %s\n", + control ); + usage(); + } + break; case 'O': #ifdef HAVE_CYRUS_SASL if( sasl_secprops != NULL ) { @@ -989,6 +1033,16 @@ tool_conn_setup( int dont, void (*private_setup)( LDAP * ) ) } } } + + if ( nettimeout.tv_sec > 0 ) { + if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout ) + != LDAP_OPT_SUCCESS ) + { + fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %d\n", + nettimeout.tv_sec ); + exit( EXIT_FAILURE ); + } + } } return ld; diff --git a/clients/tools/common.h b/clients/tools/common.h index 9f917843f5..4b4967bd7c 100644 --- a/clients/tools/common.h +++ b/clients/tools/common.h @@ -94,6 +94,9 @@ extern struct berval pr_cookie; extern int chaining; #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */ +/* options */ +extern struct timeval nettimeout; + /* Defined in common.c, set in main() */ extern const char __Version[]; diff --git a/clients/tools/ldapcompare.c b/clients/tools/ldapcompare.c index 317224acfc..5cc9664bc6 100644 --- a/clients/tools/ldapcompare.c +++ b/clients/tools/ldapcompare.c @@ -102,7 +102,7 @@ static int docompare LDAP_P(( const char options[] = "z" - "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z"; + "Cd:D:e:h:H:IkKMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z"; #ifdef LDAP_CONTROL_DONTUSECOPY int dontUseCopy = 0; diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c index 741c52dcdc..71f13cebe8 100644 --- a/clients/tools/ldapdelete.c +++ b/clients/tools/ldapdelete.c @@ -76,7 +76,7 @@ usage( void ) const char options[] = "r" - "cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z"; + "cd:D:e:f:h:H:IkKMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z"; int handle_private_option( int i ) diff --git a/clients/tools/ldapexop.c b/clients/tools/ldapexop.c index 0ffe42dd5e..03ec8fae01 100644 --- a/clients/tools/ldapexop.c +++ b/clients/tools/ldapexop.c @@ -49,7 +49,7 @@ usage( void ) const char options[] = "" - "d:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z"; + "d:D:e:h:H:InO:o:p:QR:U:vVw:WxX:y:Y:Z"; int handle_private_option( int i ) diff --git a/clients/tools/ldapmodify.c b/clients/tools/ldapmodify.c index aa66c161ee..608400c3c0 100644 --- a/clients/tools/ldapmodify.c +++ b/clients/tools/ldapmodify.c @@ -154,7 +154,7 @@ usage( void ) const char options[] = "aE:FrS:" - "cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z"; + "cd:D:e:f:h:H:IkKMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z"; int handle_private_option( int i ) diff --git a/clients/tools/ldapmodrdn.c b/clients/tools/ldapmodrdn.c index 2841d7e087..74ac903af0 100644 --- a/clients/tools/ldapmodrdn.c +++ b/clients/tools/ldapmodrdn.c @@ -91,7 +91,7 @@ usage( void ) const char options[] = "rs:" - "cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z"; + "cd:D:e:f:h:H:IkKMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z"; int handle_private_option( int i ) diff --git a/clients/tools/ldappasswd.c b/clients/tools/ldappasswd.c index b8ce5e899a..a73ddbd773 100644 --- a/clients/tools/ldappasswd.c +++ b/clients/tools/ldappasswd.c @@ -81,7 +81,7 @@ usage( void ) const char options[] = "a:As:St:T:" - "d:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z"; + "d:D:e:h:H:InO:o:p:QR:U:vVw:WxX:y:Y:Z"; int handle_private_option( int i ) diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c index 50053514c9..07ed389b9e 100644 --- a/clients/tools/ldapsearch.c +++ b/clients/tools/ldapsearch.c @@ -249,7 +249,7 @@ urlize(char *url) const char options[] = "a:Ab:cE:F:l:Ls:S:tT:uz:" - "Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z"; + "Cd:D:e:f:h:H:IkKMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z"; int handle_private_option( int i ) diff --git a/clients/tools/ldapwhoami.c b/clients/tools/ldapwhoami.c index 17d48b28e3..a294f51144 100644 --- a/clients/tools/ldapwhoami.c +++ b/clients/tools/ldapwhoami.c @@ -62,7 +62,7 @@ usage( void ) const char options[] = "" - "d:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z"; + "d:D:e:h:H:InO:o:p:QR:U:vVw:WxX:y:Y:Z"; int handle_private_option( int i ) -- 2.39.5