]> 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 271a1bec726df0848daae9887105a960e1d79ce5..d7a2b776660740ecfa8f9020b1d46d72eca99f42 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2011 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>
@@ -767,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 );
        }
 }
 
@@ -914,7 +899,7 @@ getNextPage:
        if ( !fp && infile ) {
                if (( fp = fopen( infile, "r" )) == NULL ) {
                        perror( infile );
-                       return EXIT_FAILURE;
+                       tool_exit( ld, EXIT_FAILURE );
                }
        }
        save_nctrls = nctrls;
@@ -938,7 +923,7 @@ getNextPage:
 #ifdef LDAP_CONTROL_DONTUSECOPY
                if ( dontUseCopy ) {
                        if ( ctrl_add() ) {
-                               return EXIT_FAILURE;
+                               tool_exit( ld, EXIT_FAILURE );
                        }
 
                        c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
@@ -951,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;
@@ -963,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;
@@ -988,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 ) {
@@ -1005,11 +990,11 @@ getNextPage:
                        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;
@@ -1020,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;
@@ -1044,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 ) {
@@ -1066,13 +1051,13 @@ getNextPage:
 
                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;
@@ -1082,13 +1067,13 @@ getNextPage:
 
                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;
@@ -1103,7 +1088,7 @@ getNextPage:
                                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++ ) {
@@ -1115,7 +1100,7 @@ getNextPage:
                        }
 
                        if ( ctrl_add() ) {
-                               exit( EXIT_FAILURE );
+                               tool_exit( ld, EXIT_FAILURE );
                        }
 
                        c[ i ].ldctl_iscritical = derefcrit > 1;
@@ -1235,7 +1220,7 @@ 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;
@@ -1248,7 +1233,7 @@ getNextPage:
                                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;
@@ -1291,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;
@@ -1322,7 +1307,7 @@ getNextPage:
                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;
@@ -1336,8 +1321,6 @@ getNextPage:
                goto getNextPage;
        }
 
-       tool_unbind( ld );
-       tool_destroy();
        if ( base != NULL ) {
                ber_memfree( base );
        }
@@ -1364,7 +1347,7 @@ getNextPage:
                c = NULL;
        }
 
-       return( rc );
+       tool_exit( ld, rc );
 }
 
 
@@ -1382,7 +1365,7 @@ static int dosearch(
        int sizelimit )
 {
        char                    *filter;
-       int                     rc;
+       int                     rc, rc2 = LDAP_OTHER;
        int                     nresponses;
        int                     nentries;
        int                     nreferences;
@@ -1445,8 +1428,7 @@ static int dosearch(
        }
 
        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 );
        }
 
@@ -1465,9 +1447,8 @@ static int dosearch(
                sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
                tvp, &res )) > 0 )
        {
-               rc = tool_check_abandon( ld, msgid );
-               if ( rc ) {
-                       return rc;
+               if ( tool_check_abandon( ld, msgid ) ) {
+                       return -1;
                }
 
                if( sortattr ) {
@@ -1515,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;
                                }
@@ -1559,13 +1540,8 @@ static int dosearch(
        }
 
 done:
-       if ( tvp == NULL && rc == 0 ) {
-               ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void *)&rc );
-       }
-
-       if ( rc != 0 ) {
-               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 );
@@ -1599,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.
@@ -1632,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 ) {
@@ -1730,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 ) {
@@ -1765,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 ) {
@@ -1803,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 ) {
@@ -1853,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 );
        }