/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 1998-2011 The OpenLDAP Foundation.
+ * Copyright 1998-2012 The OpenLDAP Foundation.
* Portions Copyright 2003 Kurt D. Zeilenga.
* Portions Copyright 2003 IBM Corporation.
* All rights reserved.
void
tool_destroy( void )
{
+ static int destroyed;
+ if ( destroyed++ )
+ return;
+
#ifdef HAVE_CYRUS_SASL
sasl_done();
#endif
N_(" -n show what would be done but don't actually do it\n"),
N_(" -N do not use reverse DNS to canonicalize SASL host name\n"),
N_(" -O props SASL security properties\n"),
-N_(" -o <opt>[=<optparam] general options\n"),
+N_(" -o <opt>[=<optparam>] general options\n"),
N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
N_(" ldif-wrap=<width> (in columns, or \"no\" for no wrapping)\n"),
N_(" -p port port on LDAP server\n"),
fputs( _(*cpp), stderr );
}
}
+
+ tool_destroy();
}
void tool_perror(
case 'c': /* continuous operation mode */
contoper++;
break;
- case 'C':
+ case 'C': /* referrals: obsolete */
referrals++;
break;
case 'd':
}
#endif
}
+
+ if ( ( pw_file || want_bindpw ) && !BER_BVISNULL( &passwd ) ) {
+ fprintf( stderr, "%s: -%c incompatible with -w\n",
+ prog, ( pw_file ? 'y' : 'W' ) );
+ exit( EXIT_FAILURE );
+ }
}
if( private_setup ) private_setup( ld );
- /* referrals */
+ /* referrals: obsolete */
if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
{
fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
referrals ? "on" : "off" );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
#ifdef HAVE_CYRUS_SASL
/* canon */
- if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
- nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
- {
- fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
- nocanon ? "on" : "off" );
- exit( EXIT_FAILURE );
+ if( nocanon ) {
+ if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
+ LDAP_OPT_ON ) != LDAP_OPT_SUCCESS )
+ {
+ fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON on\n" );
+ tool_exit( ld, EXIT_FAILURE );
+ }
}
#endif
if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
{
fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
protocol );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if ( use_tls ) {
tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
ldap_memfree(msg);
if ( use_tls > 1 ) {
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
}
}
{
fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
(long)nettimeout.tv_sec );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
}
}
if ( sessionTracking ) {
LDAPControl c;
- if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
- exit( EXIT_FAILURE );
+ if ( BER_BVISNULL( &stValue) && st_value( ld, &stValue ) ) {
+ tool_exit( ld, EXIT_FAILURE );
}
c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
if ( pw_file ) {
if ( lutil_get_filed_password( pw_file, &passwd ) ) {
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
} else {
fprintf( stderr,
"Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
sasl_secprops );
- exit( LDAP_LOCAL_ERROR );
+ tool_exit( ld, LDAP_LOCAL_ERROR );
}
}
tool_perror( "ldap_sasl_interactive_bind_s",
rc, NULL, NULL, msg, NULL );
ldap_memfree(msg);
- exit( rc );
+ tool_exit( ld, rc );
}
#else
fprintf( stderr, "%s: not compiled with SASL support\n", prog );
- exit( LDAP_NOT_SUPPORTED );
+ tool_exit( ld, LDAP_NOT_SUPPORTED );
#endif
} else {
int msgid, err, rc;
if ( msgid == -1 ) {
tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
NULL, NULL, NULL, NULL );
- exit( rc );
+ tool_exit( ld, rc );
}
}
rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
if ( rc == -1 ) {
tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
- exit( LDAP_LOCAL_ERROR );
+ tool_exit( ld, LDAP_LOCAL_ERROR );
}
if ( rc == 0 ) {
tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
- exit( LDAP_LOCAL_ERROR );
+ tool_exit( ld, LDAP_LOCAL_ERROR );
}
rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
&ctrls, 1 );
if ( rc != LDAP_SUCCESS ) {
tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
- exit( LDAP_LOCAL_ERROR );
+ tool_exit( ld, LDAP_LOCAL_ERROR );
}
#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
if( info ) ber_memfree( info );
if( refs ) ber_memvfree( (void **)refs );
- if ( err != LDAP_SUCCESS ) exit( err );
+ if ( err != LDAP_SUCCESS ) tool_exit( ld, err );
}
}
}
(void) ldap_unbind_ext( ld, NULL, NULL );
}
+void
+tool_exit( LDAP *ld, int status )
+{
+ if ( ld != NULL ) {
+ tool_unbind( ld );
+ }
+ tool_destroy();
+ exit( status );
+}
+
/* Set server controls. Add controls extra_c[0..count-1], if set. */
void
ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
if ( ctrls == NULL ) {
fprintf( stderr, "No memory\n" );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if ( assertctl ) {
ber_init2( ber, NULL, LBER_USE_DER );
if ( ber_printf( ber, "s", proxydn ) == -1 ) {
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
if( ber_printf( ber, "{v}", attrs ) == -1 ) {
fprintf( stderr, "preread attrs encode failed.\n" );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
if( err < 0 ) {
fprintf( stderr, "preread flatten failed (%d)\n", err );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
if( ber_printf( ber, "{v}", attrs ) == -1 ) {
fprintf( stderr, "postread attrs encode failed.\n" );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
if( err < 0 ) {
fprintf( stderr, "postread flatten failed (%d)\n", err );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
if ( err == -1 ) {
ber_free( ber, 1 );
fprintf( stderr, _("Chaining behavior control encoding error!\n") );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if ( chainingContinuation > -1 ) {
if ( err == -1 ) {
ber_free( ber, 1 );
fprintf( stderr, _("Chaining behavior control encoding error!\n") );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
}
if ( err == -1 ) {
ber_free( ber, 1 );
fprintf( stderr, _("Chaining behavior control encoding error!\n") );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
} else {
#ifdef LDAP_CONTROL_X_SESSION_TRACKING
if ( sessionTracking ) {
- if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
- exit( EXIT_FAILURE );
+ if ( BER_BVISNULL( &stValue ) && st_value( ld, &stValue ) ) {
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
free( ctrls );
if ( crit ) {
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
}
return 1;
}
-