]> git.sur5r.net Git - openldap/blobdiff - clients/tools/ldapsearch.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / clients / tools / ldapsearch.c
index 6ca5d274ddcb8178ccd86e7b2f1566dfeb14f472..d7a2b776660740ecfa8f9020b1d46d72eca99f42 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2007 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>
@@ -66,7 +67,6 @@
 #include "lutil.h"
 #include "lutil_ldap.h"
 #include "ldap_defaults.h"
-#include "ldap_log.h"
 #include "ldap_pvt.h"
 
 #include "common.h"
@@ -123,21 +123,32 @@ usage( void )
        fprintf( stderr, _("  -a deref   one of never (default), always, search, or find\n"));
        fprintf( stderr, _("  -A         retrieve attribute names only (no values)\n"));
        fprintf( stderr, _("  -b basedn  base dn for search\n"));
+       fprintf( stderr, _("  -c         continuous operation mode (do not stop on errors)\n"));
        fprintf( stderr, _("  -E [!]<ext>[=<extparam>] search extensions (! indicates criticality)\n"));
        fprintf( stderr, _("             [!]domainScope              (domain scope)\n"));
        fprintf( stderr, _("             !dontUseCopy                (Don't Use Copy)\n"));
-       fprintf( stderr, _("             [!]mv=<filter>              (matched values filter)\n"));
-       fprintf( stderr, _("             [!]pr=<size>[/prompt|noprompt]   (paged results/prompt)\n"));
-       fprintf( stderr, _("             [!]subentries[=true|false]  (subentries)\n"));
-       fprintf( stderr, _("             [!]sync=ro[/<cookie>]            (LDAP Sync refreshOnly)\n"));
-       fprintf( stderr, _("                     rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
-       fprintf( stderr, _("             [!]<oid>=:<value>           (generic control; no response handling)\n"));
+       fprintf( stderr, _("             [!]mv=<filter>              (RFC 3876 matched values filter)\n"));
+       fprintf( stderr, _("             [!]pr=<size>[/prompt|noprompt] (RFC 2696 paged results/prompt)\n"));
+       fprintf( stderr, _("             [!]sss=[-]<attr[:OID]>[/[-]<attr[:OID]>...]\n"));
+       fprintf( stderr, _("                                         (RFC 2891 server side sorting)\n"));
+       fprintf( stderr, _("             [!]subentries[=true|false]  (RFC 3672 subentries)\n"));
+       fprintf( stderr, _("             [!]sync=ro[/<cookie>]       (RFC 4533 LDAP Sync refreshOnly)\n"));
+       fprintf( stderr, _("                     rp[/<cookie>][/<slimit>] (refreshAndPersist)\n"));
+       fprintf( stderr, _("             [!]vlv=<before>/<after>(/<offset>/<count>|:<value>)\n"));
+       fprintf( stderr, _("                                         (ldapv3-vlv-09 virtual list views)\n"));
+#ifdef LDAP_CONTROL_X_DEREF
+       fprintf( stderr, _("             [!]deref=derefAttr:attr[,...][;derefAttr:attr[,...][;...]]\n"));
+#endif
+       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"));
        fprintf( stderr, _("  -L         print responses in LDIFv1 format\n"));
        fprintf( stderr, _("  -LL        print responses in LDIF format without comments\n"));
        fprintf( stderr, _("  -LLL       print responses in LDIF format without comments\n"));
        fprintf( stderr, _("             and version\n"));
+       fprintf( stderr, _("  -M         enable Manage DSA IT control (-MM to make critical)\n"));
+       fprintf( stderr, _("  -P version protocol version (default: 3)\n"));
        fprintf( stderr, _("  -s scope   one of base, one, sub or children (search scope)\n"));
        fprintf( stderr, _("  -S attr    sort the results by attribute `attr'\n"));
        fprintf( stderr, _("  -t         write binary values to files in temporary directory\n"));
@@ -199,6 +210,13 @@ static int dontUseCopy = 0;
 
 static int domainScope = 0;
 
+static int sss = 0;
+static LDAPSortKey **sss_keys = NULL;
+
+static int vlv = 0;
+static LDAPVLVInfo vlvInfo;
+static struct berval vlvValue;
+
 static int ldapsync = 0;
 static struct berval sync_cookie = { 0, NULL };
 static int sync_slimit = -1;
@@ -218,6 +236,12 @@ static LDAPControl *c = NULL;
 static int nctrls = 0;
 static int save_nctrls = 0;
 
+#ifdef LDAP_CONTROL_X_DEREF
+static int derefcrit;
+static LDAPDerefSpec *ds;
+static struct berval derefval;
+#endif
+
 static int
 ctrl_add( void )
 {
@@ -249,9 +273,50 @@ urlize(char *url)
        }
 }
 
+static int
+parse_vlv(char *cvalue)
+{
+       char *keyp, *key2;
+       int num1, num2;
+
+       keyp = cvalue;
+       if ( sscanf( keyp, "%d/%d", &num1, &num2 ) != 2 ) {
+               fprintf( stderr,
+                       _("VLV control value \"%s\" invalid\n"),
+                       cvalue );
+               return -1;
+       }
+       vlvInfo.ldvlv_before_count = num1;
+       vlvInfo.ldvlv_after_count = num2;
+       keyp = strchr( keyp, '/' ) + 1;
+       key2 = strchr( keyp, '/' );
+       if ( key2 ) {
+               keyp = key2 + 1;
+               if ( sscanf( keyp, "%d/%d", &num1, &num2 ) != 2 ) {
+                       fprintf( stderr,
+                               _("VLV control value \"%s\" invalid\n"),
+                               cvalue );
+                       return -1;
+               }
+               vlvInfo.ldvlv_offset = num1;
+               vlvInfo.ldvlv_count = num2;
+               vlvInfo.ldvlv_attrvalue = NULL;
+       } else {
+               key2 = strchr( keyp, ':' );
+               if ( !key2 ) {
+                       fprintf( stderr,
+                               _("VLV control value \"%s\" invalid\n"),
+                               cvalue );
+                       return -1;
+               }
+               ber_str2bv( key2+1, 0, 0, &vlvValue );
+               vlvInfo.ldvlv_attrvalue = &vlvValue;
+       }
+       return 0;
+}
 
 const char options[] = "a:Ab:cE:F:l:Ls:S:tT:uz:"
-       "Cd:D:e:f:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
+       "Cd:D:e:f:h:H:IMnNO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
 
 int
 handle_private_option( int i )
@@ -329,6 +394,11 @@ handle_private_option( int i )
                                        _("PagedResultsControl previously specified\n") );
                                exit( EXIT_FAILURE );
                        }
+                       if ( vlv != 0 ) {
+                               fprintf( stderr,
+                                       _("PagedResultsControl incompatible with VLV\n") );
+                               exit( EXIT_FAILURE );
+                       }
 
                        if( cvalue != NULL ) {
                                char *promptp;
@@ -395,6 +465,31 @@ handle_private_option( int i )
 
                        domainScope = 1 + crit;
 
+               } else if ( strcasecmp( control, "sss" ) == 0 ) {
+                       char *keyp;
+                       if( sss ) {
+                               fprintf( stderr,
+                                       _("server side sorting control previously specified\n"));
+                               exit( EXIT_FAILURE );
+                       }
+                       if( cvalue == NULL ) {
+                               fprintf( stderr,
+                                _("missing specification of sss control\n") );
+                               exit( EXIT_FAILURE );
+                       }
+                       keyp = cvalue;
+                       while ( ( keyp = strchr(keyp, '/') ) != NULL ) {
+                               *keyp++ = ' ';
+                       }
+                       if ( ldap_create_sort_keylist( &sss_keys, cvalue )) {
+                               fprintf( stderr,
+                                       _("server side sorting control value \"%s\" invalid\n"),
+                                       cvalue );
+                               exit( EXIT_FAILURE );
+                       }
+
+                       sss = 1 + crit;
+
                } else if ( strcasecmp( control, "subentries" ) == 0 ) {
                        if( subentries ) {
                                fprintf( stderr,
@@ -461,6 +556,72 @@ handle_private_option( int i )
                        }
                        if ( crit ) ldapsync *= -1;
 
+               } else if ( strcasecmp( control, "vlv" ) == 0 ) {
+                       if( vlv ) {
+                               fprintf( stderr,
+                                       _("virtual list view control previously specified\n"));
+                               exit( EXIT_FAILURE );
+                       }
+                       if ( pagedResults != 0 ) {
+                               fprintf( stderr,
+                                       _("PagedResultsControl incompatible with VLV\n") );
+                               exit( EXIT_FAILURE );
+                       }
+                       if( cvalue == NULL ) {
+                               fprintf( stderr,
+                                _("missing specification of vlv control\n") );
+                               exit( EXIT_FAILURE );
+                       }
+                       if ( parse_vlv( cvalue ))
+                               exit( EXIT_FAILURE );
+
+                       vlv = 1 + crit;
+
+#ifdef LDAP_CONTROL_X_DEREF
+               } else if ( strcasecmp( control, "deref" ) == 0 ) {
+                       int ispecs;
+                       char **specs;
+
+                       /* cvalue is something like
+                        *
+                        * derefAttr:attr[,attr[...]][;derefAttr:attr[,attr[...]]]"
+                        */
+
+                       specs = ldap_str2charray( cvalue, ";" );
+                       if ( specs == NULL ) {
+                               fprintf( stderr, _("deref specs \"%s\" invalid\n"),
+                                       cvalue );
+                               exit( EXIT_FAILURE );
+                       }
+                       for ( ispecs = 0; specs[ ispecs ] != NULL; ispecs++ )
+                               /* count'em */ ;
+
+                       ds = ldap_memcalloc( ispecs + 1, sizeof( LDAPDerefSpec ) );
+                       if ( ds == NULL ) {
+                               perror( "malloc" );
+                               exit( EXIT_FAILURE );
+                       }
+
+                       for ( ispecs = 0; specs[ ispecs ] != NULL; ispecs++ ) {
+                               char *ptr;
+
+                               ptr = strchr( specs[ ispecs ], ':' );
+                               if ( ptr == NULL ) {
+                                       fprintf( stderr, _("deref specs \"%s\" invalid\n"),
+                                               cvalue );
+                                       exit( EXIT_FAILURE );
+                               }
+
+                               ds[ ispecs ].derefAttr = specs[ ispecs ];
+                               *ptr++ = '\0';
+                               ds[ ispecs ].attributes = ldap_str2charray( ptr, "," );
+                       }
+
+                       derefcrit = 1 + crit;
+
+                       ldap_memfree( specs );
+#endif /* LDAP_CONTROL_X_DEREF */
+
                } else if ( tool_is_oid( control ) ) {
                        if ( ctrl_add() ) {
                                exit( EXIT_FAILURE );
@@ -475,18 +636,20 @@ handle_private_option( int i )
                                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;
@@ -494,6 +657,11 @@ handle_private_option( int i )
                                } else {
                                        ber_dupbv( &c[ nctrls - 1 ].ldctl_value, &value );
                                }
+
+                       } else {
+                               fprintf( stderr, "unable to parse %s control value\n", control );
+                               exit( EXIT_FAILURE );
+                               
                        }
 
                        /* criticality */
@@ -600,23 +768,7 @@ private_conn_setup( LDAP *ld )
                        != 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 );
        }
 }
 
@@ -625,7 +777,7 @@ main( int argc, char **argv )
 {
        char            *filtpattern, **attrs = NULL, line[BUFSIZ];
        FILE            *fp = NULL;
-       int             rc, i, first;
+       int                     rc, rc1, i, first;
        LDAP            *ld = NULL;
        BerElement      *seber = NULL, *vrber = NULL;
 
@@ -664,6 +816,12 @@ main( int argc, char **argv )
 
        tool_args( argc, argv );
 
+       if ( vlv && !sss ) {
+               fprintf( stderr,
+                       _("VLV control requires server side sort control\n" ));
+               return EXIT_FAILURE;
+       }
+
        if (( argc - optind < 1 ) ||
                ( *argv[optind] != '(' /*')'*/ &&
                ( strchr( argv[optind], '=' ) == NULL ) ) )
@@ -732,36 +890,40 @@ main( int argc, char **argv )
 
        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
                || dontUseCopy
+#endif
+#ifdef LDAP_CONTROL_X_DEREF
+               || derefcrit
 #endif
                || domainScope
                || pagedResults
                || ldapsync
+               || sss
                || subentries
-               || valuesReturnFilter )
+               || valuesReturnFilter
+               || vlv )
        {
 
 #ifdef LDAP_CONTROL_DONTUSECOPY
                if ( dontUseCopy ) {
                        if ( ctrl_add() ) {
-                               return EXIT_FAILURE;
+                               tool_exit( ld, EXIT_FAILURE );
                        }
 
                        c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
@@ -774,7 +936,7 @@ getNextPage:
 
                if ( domainScope ) {
                        if ( ctrl_add() ) {
-                               return EXIT_FAILURE;
+                               tool_exit( ld, EXIT_FAILURE );
                        }
 
                        c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
@@ -786,22 +948,22 @@ getNextPage:
 
                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;
@@ -811,11 +973,11 @@ getNextPage:
 
                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 ) {
@@ -825,14 +987,14 @@ getNextPage:
                                                        &sync_cookie );
                        }
 
-                       if ( err == LBER_ERROR ) {
+                       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 ) == LBER_ERROR ) {
-                               return EXIT_FAILURE;
+                       if ( ber_flatten( syncber, &syncbvalp ) == -1 ) {
+                               tool_exit( ld, EXIT_FAILURE );
                        }
 
                        c[i].ldctl_oid = LDAP_CONTROL_SYNC;
@@ -843,21 +1005,21 @@ getNextPage:
 
                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;
@@ -867,13 +1029,13 @@ getNextPage:
 
                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 ) {
@@ -886,12 +1048,73 @@ getNextPage:
                        c[i].ldctl_iscritical = pagedResults > 1;
                        i++;
                }
+
+               if ( sss ) {
+                       if ( ctrl_add() ) {
+                               tool_exit( ld, EXIT_FAILURE );
+                       }
+
+                       if ( ldap_create_sort_control_value( ld,
+                               sss_keys, &c[i].ldctl_value ) )
+                       {
+                               tool_exit( ld, EXIT_FAILURE );
+                       }
+
+                       c[i].ldctl_oid = LDAP_CONTROL_SORTREQUEST;
+                       c[i].ldctl_iscritical = sss > 1;
+                       i++;
+               }
+
+               if ( vlv ) {
+                       if ( ctrl_add() ) {
+                               tool_exit( ld, EXIT_FAILURE );
+                       }
+
+                       if ( ldap_create_vlv_control_value( ld,
+                               &vlvInfo, &c[i].ldctl_value ) )
+                       {
+                               tool_exit( ld, EXIT_FAILURE );
+                       }
+
+                       c[i].ldctl_oid = LDAP_CONTROL_VLVREQUEST;
+                       c[i].ldctl_iscritical = sss > 1;
+                       i++;
+               }
+#ifdef LDAP_CONTROL_X_DEREF
+               if ( derefcrit ) {
+                       if ( derefval.bv_val == NULL ) {
+                               int i;
+
+                               assert( ds != NULL );
+
+                               if ( ldap_create_deref_control_value( ld, ds, &derefval ) != LDAP_SUCCESS ) {
+                                       tool_exit( ld, EXIT_FAILURE );
+                               }
+
+                               for ( i = 0; ds[ i ].derefAttr != NULL; i++ ) {
+                                       ldap_memfree( ds[ i ].derefAttr );
+                                       ldap_charray_free( ds[ i ].attributes );
+                               }
+                               ldap_memfree( ds );
+                               ds = NULL;
+                       }
+
+                       if ( ctrl_add() ) {
+                               tool_exit( ld, EXIT_FAILURE );
+                       }
+
+                       c[ i ].ldctl_iscritical = derefcrit > 1;
+                       c[ i ].ldctl_oid = LDAP_CONTROL_X_DEREF;
+                       c[ i ].ldctl_value = derefval;
+                       i++;
+               }
+#endif /* LDAP_CONTROL_X_DEREF */
        }
 
        tool_server_controls( ld, c, i );
 
-       ber_free( seber, 1 );
-       ber_free( vrber, 1 );
+       if ( seber ) ber_free( seber, 1 );
+       if ( vrber ) ber_free( vrber, 1 );
 
        /* step back to the original number of controls, so that 
         * those set while parsing args are preserved */
@@ -968,6 +1191,25 @@ getNextPage:
                                (pagedResults > 1) ? _("critical ") : "", 
                                pageSize );
                }
+               if ( sss ) {
+                       printf(_("\n# with server side sorting %scontrol"),
+                               sss > 1 ? _("critical ") : "" );
+               }
+               if ( vlv ) {
+                       printf(_("\n# with virtual list view %scontrol: %d/%d"),
+                               vlv > 1 ? _("critical ") : "",
+                               vlvInfo.ldvlv_before_count, vlvInfo.ldvlv_after_count);
+                       if ( vlvInfo.ldvlv_attrvalue )
+                               printf(":%s", vlvInfo.ldvlv_attrvalue->bv_val );
+                       else
+                               printf("/%d/%d", vlvInfo.ldvlv_offset, vlvInfo.ldvlv_count );
+               }
+#ifdef LDAP_CONTROL_X_DEREF
+               if ( derefcrit ) {
+                       printf(_("\n# with dereference %scontrol"),
+                               derefcrit > 1 ? _("critical ") : "" );
+               }
+#endif
 
                printf( _("\n#\n\n") );
 
@@ -978,9 +1220,10 @@ getNextPage:
 
        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 = 1;
                while ( fgets( line, sizeof( line ), fp ) != NULL ) { 
                        line[ strlen( line ) - 1 ] = '\0';
@@ -989,20 +1232,23 @@ getNextPage:
                        } else {
                                first = 0;
                        }
-                       rc = dosearch( ld, base, scope, filtpattern, line,
-                               attrs, attrsonly, NULL, NULL, NULL, -1 );
+                       rc1 = dosearch( ld, base, scope, filtpattern, line,
+                               attrs, attrsonly, NULL, NULL, NULL, sizelimit );
 
-                       if ( rc != 0 && !contoper ) {
-                               break;
+                       if ( rc1 != 0 ) {
+                               rc = rc1;
+                               if ( !contoper )
+                                       break;
                        }
                }
                if ( fp != stdin ) {
                        fclose( fp );
+                       fp = NULL;
                }
        }
 
        if (( rc == LDAP_SUCCESS ) && pageSize && pr_morePagedResults ) {
-               char    buf[6];
+               char    buf[12];
                int     i, moreEntries, tmpSize;
 
                /* Loop to get the next pages when 
@@ -1030,7 +1276,7 @@ getNextPage:
                                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;
@@ -1040,14 +1286,58 @@ getNextPage:
                goto getNextPage;
        }
 
-       tool_unbind( ld );
-       tool_destroy();
+       if (( rc == LDAP_SUCCESS ) && vlv ) {
+               char    buf[BUFSIZ];
+               int     i, moreEntries;
+
+               /* Loop to get the next window when 
+                * enter is pressed on the terminal.
+                */
+               printf( _("Press [before/after(/offset/count|:value)] Enter for the next window.\n"));
+               i = 0;
+               moreEntries = getchar();
+               while ( moreEntries != EOF && moreEntries != '\n' ) { 
+                       if ( i < (int)sizeof(buf) - 1 ) {
+                               buf[i] = moreEntries;
+                               i++;
+                       }
+                       moreEntries = getchar();
+               }
+               buf[i] = '\0';
+               if ( buf[0] ) {
+                       i = parse_vlv( strdup( buf ));
+                       if ( i )
+                               tool_exit( ld, EXIT_FAILURE );
+               } else {
+                       vlvInfo.ldvlv_attrvalue = NULL;
+                       vlvInfo.ldvlv_count = vlvCount;
+                       vlvInfo.ldvlv_offset += vlvInfo.ldvlv_after_count;
+               }
+
+               if ( vlvInfo.ldvlv_context )
+                       ber_bvfree( vlvInfo.ldvlv_context );
+               vlvInfo.ldvlv_context = vlvContext;
+
+               goto getNextPage;
+       }
+
        if ( base != NULL ) {
                ber_memfree( base );
        }
        if ( control != NULL ) {
                ber_memfree( control );
        }
+       if ( sss_keys != NULL ) {
+               ldap_free_sort_keylist( sss_keys );
+       }
+       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; ) {
@@ -1057,7 +1347,7 @@ getNextPage:
                c = NULL;
        }
 
-       return( rc );
+       tool_exit( ld, rc );
 }
 
 
@@ -1075,7 +1365,7 @@ static int dosearch(
        int sizelimit )
 {
        char                    *filter;
-       int                     rc;
+       int                     rc, rc2 = LDAP_OTHER;
        int                     nresponses;
        int                     nentries;
        int                     nreferences;
@@ -1087,16 +1377,19 @@ static int dosearch(
        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;
+               size_t max_fsize = strlen( filtpatt ) + strlen( value ) + 1, outlen;
                filter = malloc( max_fsize );
                if( filter == NULL ) {
                        perror( "malloc" );
                        return EXIT_FAILURE;
                }
 
-               if( snprintf( filter, max_fsize, filtpatt, value ) >= max_fsize ) {
+               outlen = snprintf( filter, max_fsize, filtpatt, value );
+               if( outlen >= max_fsize ) {
                        fprintf( stderr, "Bad filter pattern: \"%s\"\n", filtpatt );
                        free( filter );
                        return EXIT_FAILURE;
@@ -1121,16 +1414,21 @@ static int dosearch(
                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 );
        }
 
@@ -1138,13 +1436,19 @@ static int dosearch(
 
        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 ) {
@@ -1192,7 +1496,7 @@ static int dosearch(
                                /* 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;
                                }
@@ -1236,9 +1540,8 @@ static int dosearch(
        }
 
 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 );
@@ -1272,7 +1575,11 @@ done:
                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.
@@ -1305,7 +1612,7 @@ print_entry(
        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 ) {
@@ -1403,7 +1710,7 @@ static void print_reference(
 
        if( rc != LDAP_SUCCESS ) {
                tool_perror( "ldap_parse_reference", rc, NULL, NULL, NULL, NULL );
-               exit( EXIT_FAILURE );
+               tool_exit( ld, EXIT_FAILURE );
        }
 
        if( refs ) {
@@ -1438,7 +1745,7 @@ static void print_extended(
 
        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 ) {
@@ -1476,7 +1783,7 @@ static void print_partial(
 
        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 ) {
@@ -1526,7 +1833,7 @@ static int print_result(
 
        if( rc != LDAP_SUCCESS ) {
                tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
-               exit( EXIT_FAILURE );
+               tool_exit( ld, EXIT_FAILURE );
        }
 
 
@@ -1561,7 +1868,7 @@ static int print_result(
 
                                                tool_write_ldif( LDIF_PUT_TEXT,
                                                        "text", line,
-                                                       next ? next - line : strlen( line ) );
+                                                       next ? (size_t) (next - line) : strlen( line ));
 
                                                line = next ? next + 1 : NULL;
                                        }
@@ -1600,4 +1907,3 @@ static int print_result(
 
        return err;
 }
-