/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 1998-2009 The OpenLDAP Foundation.
+ * Copyright 1998-2013 The OpenLDAP Foundation.
* Portions Copyright 1998-2003 Kurt D. Zeilenga.
* Portions Copyright 1998-2001 Net Boolean Incorporated.
* Portions Copyright 2001-2003 IBM Corporation.
#include <stdio.h>
#include <ac/stdlib.h>
-
#include <ac/ctype.h>
#include <ac/string.h>
#include <ac/unistd.h>
#include <ac/errno.h>
+#include <ac/time.h>
+
#include <sys/stat.h>
#include <ac/signal.h>
#include "lutil.h"
#include "lutil_ldap.h"
#include "ldap_defaults.h"
-#include "ldap_log.h"
#include "ldap_pvt.h"
#include "common.h"
#ifdef LDAP_CONTROL_X_DEREF
fprintf( stderr, _(" [!]deref=derefAttr:attr[,...][;derefAttr:attr[,...][;...]]\n"));
#endif
- fprintf( stderr, _(" [!]<oid>=:<value> (generic control; no response handling)\n"));
+ fprintf( stderr, _(" [!]<oid>[=:<b64value>] (generic control; no response handling)\n"));
fprintf( stderr, _(" -f file read operations from `file'\n"));
fprintf( stderr, _(" -F prefix URL prefix for files (default: %s)\n"), def_urlpre);
fprintf( stderr, _(" -l limit time limit (in seconds, or \"none\" or \"max\") for search\n"));
exit( EXIT_FAILURE );
}
for ( ispecs = 0; specs[ ispecs ] != NULL; ispecs++ )
- /* count'em */
+ /* count'em */ ;
ds = ldap_memcalloc( ispecs + 1, sizeof( LDAPDerefSpec ) );
if ( ds == NULL ) {
c[ nctrls - 1 ].ldctl_value.bv_len = 0;
} else if ( cvalue[ 0 ] == ':' ) {
- struct berval type;
- struct berval value;
- int freeval;
+ struct berval type;
+ struct berval value;
+ int freeval;
+ char save_c;
cvalue++;
/* dummy type "x"
* to use ldif_parse_line2() */
+ save_c = cvalue[ -2 ];
cvalue[ -2 ] = 'x';
ldif_parse_line2( &cvalue[ -2 ], &type,
&value, &freeval );
- cvalue[ -2 ] = '\0';
+ cvalue[ -2 ] = save_c;
if ( freeval ) {
c[ nctrls - 1 ].ldctl_value = value;
} else {
ber_dupbv( &c[ nctrls - 1 ].ldctl_value, &value );
}
+
+ } else {
+ fprintf( stderr, "unable to parse %s control value\n", control );
+ exit( EXIT_FAILURE );
+
}
/* criticality */
!= LDAP_OPT_SUCCESS )
{
fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
- exit( EXIT_FAILURE );
- }
- if (timelimit > 0 &&
- ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit )
- != LDAP_OPT_SUCCESS )
- {
- fprintf( stderr,
- _("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
- exit( EXIT_FAILURE );
- }
- if (sizelimit > 0 &&
- ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit )
- != LDAP_OPT_SUCCESS )
- {
- fprintf( stderr,
- _("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
}
ld = tool_conn_setup( 0, &private_conn_setup );
- if ( pw_file || want_bindpw ) {
- if ( pw_file ) {
- rc = lutil_get_filed_password( pw_file, &passwd );
- if( rc ) return EXIT_FAILURE;
- } else {
- passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
- passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
- }
- }
-
tool_bind( ld );
getNextPage:
+ /* fp may have been closed, need to reopen if code jumps
+ * back here to getNextPage.
+ */
+ if ( !fp && infile ) {
+ if (( fp = fopen( infile, "r" )) == NULL ) {
+ perror( infile );
+ tool_exit( ld, EXIT_FAILURE );
+ }
+ }
save_nctrls = nctrls;
i = nctrls;
if ( nctrls > 0
#ifdef LDAP_CONTROL_DONTUSECOPY
if ( dontUseCopy ) {
if ( ctrl_add() ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
if ( domainScope ) {
if ( ctrl_add() ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
if ( subentries ) {
if ( ctrl_add() ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
err = ber_printf( seber, "b", abs(subentries) == 1 ? 0 : 1 );
if ( err == -1 ) {
ber_free( seber, 1 );
fprintf( stderr, _("Subentries control encoding error!\n") );
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
if ( ldapsync ) {
if ( ctrl_add() ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( sync_cookie.bv_len == 0 ) {
if ( err == -1 ) {
ber_free( syncber, 1 );
fprintf( stderr, _("ldap sync control encoding error!\n") );
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ber_flatten( syncber, &syncbvalp ) == -1 ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_SYNC;
if ( valuesReturnFilter ) {
if ( ctrl_add() ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
ber_free( vrber, 1 );
fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
if ( pagedResults ) {
if ( ctrl_add() ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ldap_create_page_control_value( ld,
pageSize, &pr_cookie, &c[i].ldctl_value ) )
{
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( pr_cookie.bv_val != NULL ) {
if ( sss ) {
if ( ctrl_add() ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ldap_create_sort_control_value( ld,
sss_keys, &c[i].ldctl_value ) )
{
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_SORTREQUEST;
if ( vlv ) {
if ( ctrl_add() ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ldap_create_vlv_control_value( ld,
&vlvInfo, &c[i].ldctl_value ) )
{
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
c[i].ldctl_oid = LDAP_CONTROL_VLVREQUEST;
assert( ds != NULL );
if ( ldap_create_deref_control_value( ld, ds, &derefval ) != LDAP_SUCCESS ) {
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
for ( i = 0; ds[ i ].derefAttr != NULL; i++ ) {
}
if ( ctrl_add() ) {
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
c[ i ].ldctl_iscritical = derefcrit > 1;
if ( infile == NULL ) {
rc = dosearch( ld, base, scope, NULL, filtpattern,
- attrs, attrsonly, NULL, NULL, NULL, -1 );
+ attrs, attrsonly, NULL, NULL, NULL, sizelimit );
} else {
rc = 0;
first = 0;
}
rc1 = dosearch( ld, base, scope, filtpattern, line,
- attrs, attrsonly, NULL, NULL, NULL, -1 );
+ attrs, attrsonly, NULL, NULL, NULL, sizelimit );
if ( rc1 != 0 ) {
rc = rc1;
}
if ( fp != stdin ) {
fclose( fp );
+ fp = NULL;
}
}
if ( num != 1 ) {
fprintf( stderr,
_("Invalid value for PagedResultsControl, %s.\n"), buf);
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
}
pageSize = (ber_int_t)tmpSize;
if (( rc == LDAP_SUCCESS ) && vlv ) {
char buf[BUFSIZ];
- int i, moreEntries, tmpSize;
+ int i, moreEntries;
/* Loop to get the next window when
* enter is pressed on the terminal.
if ( buf[0] ) {
i = parse_vlv( strdup( buf ));
if ( i )
- return EXIT_FAILURE;
+ tool_exit( ld, EXIT_FAILURE );
} else {
vlvInfo.ldvlv_attrvalue = NULL;
vlvInfo.ldvlv_count = vlvCount;
goto getNextPage;
}
- tool_unbind( ld );
- tool_destroy();
if ( base != NULL ) {
ber_memfree( base );
}
if ( derefval.bv_val != NULL ) {
ldap_memfree( derefval.bv_val );
}
+ if ( urlpre != NULL ) {
+ if ( def_urlpre != urlpre )
+ free( def_urlpre );
+ free( urlpre );
+ }
if ( c ) {
for ( ; save_nctrls-- > 0; ) {
c = NULL;
}
- return( rc );
+ tool_exit( ld, rc );
}
int sizelimit )
{
char *filter;
- int rc;
+ int rc, rc2 = LDAP_OTHER;
int nresponses;
int nentries;
int nreferences;
struct berval *retdata = NULL;
int nresponses_psearch = -1;
int cancel_msgid = -1;
+ struct timeval tv, *tvp = NULL;
+ struct timeval tv_timelimit, *tv_timelimitp = NULL;
if( filtpatt != NULL ) {
size_t max_fsize = strlen( filtpatt ) + strlen( value ) + 1, outlen;
return LDAP_SUCCESS;
}
+ if ( timelimit > 0 ) {
+ tv_timelimit.tv_sec = timelimit;
+ tv_timelimit.tv_usec = 0;
+ tv_timelimitp = &tv_timelimit;
+ }
+
rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
- sctrls, cctrls, timeout, sizelimit, &msgid );
+ sctrls, cctrls, tv_timelimitp, sizelimit, &msgid );
if ( filtpatt != NULL ) {
free( filter );
}
if( rc != LDAP_SUCCESS ) {
- fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
- prog, ldap_err2string( rc ), rc );
+ tool_perror( "ldap_search_ext", rc, NULL, NULL, NULL, NULL );
return( rc );
}
res = NULL;
+ if ( timelimit > 0 ) {
+ /* disable timeout */
+ tv.tv_sec = -1;
+ tv.tv_usec = 0;
+ tvp = &tv;
+ }
+
while ((rc = ldap_result( ld, LDAP_RES_ANY,
sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
- NULL, &res )) > 0 )
+ tvp, &res )) > 0 )
{
- rc = tool_check_abandon( ld, msgid );
- if ( rc ) {
- return rc;
+ if ( tool_check_abandon( ld, msgid ) ) {
+ return -1;
}
if( sortattr ) {
/* pagedResults stuff is dealt with
* in tool_print_ctrls(), called by
* print_results(). */
- rc = print_result( ld, msg, 1 );
+ rc2 = print_result( ld, msg, 1 );
if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
break;
}
}
done:
- if ( rc == -1 ) {
- tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
- return( rc );
+ if ( tvp == NULL && rc != LDAP_RES_SEARCH_RESULT ) {
+ ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void *)&rc2 );
}
ldap_msgfree( res );
if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
}
- return( rc );
+ if ( rc != LDAP_RES_SEARCH_RESULT ) {
+ tool_perror( "ldap_result", rc2, NULL, NULL, NULL, NULL );
+ }
+
+ return( rc2 );
}
/* This is the proposed new way of doing things.
if( rc != LDAP_SUCCESS ) {
fprintf(stderr, _("print_entry: %d\n"), rc );
tool_perror( "ldap_get_entry_controls", rc, NULL, NULL, NULL, NULL );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if( ctrls ) {
if( rc != LDAP_SUCCESS ) {
tool_perror( "ldap_parse_reference", rc, NULL, NULL, NULL, NULL );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if( refs ) {
if( rc != LDAP_SUCCESS ) {
tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ldif < 2 ) {
if( rc != LDAP_SUCCESS ) {
tool_perror( "ldap_parse_intermediate", rc, NULL, NULL, NULL, NULL );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
if ( ldif < 2 ) {
if( rc != LDAP_SUCCESS ) {
tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
- exit( EXIT_FAILURE );
+ tool_exit( ld, EXIT_FAILURE );
}
return err;
}
-