]> git.sur5r.net Git - openldap/blobdiff - clients/tools/ldapsearch.c
Merge branch 'mdb.master' of ssh://git-master.openldap.org/~git/git/openldap
[openldap] / clients / tools / ldapsearch.c
index c1f76f719a798c5f99682c8c4bc6b504c7f99a05..068acb3e1b5e30485409e7ef5dffaef49fd518a9 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2009 The OpenLDAP Foundation.
+ * Copyright 1998-2011 The OpenLDAP Foundation.
  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
  * Portions Copyright 1998-2001 Net Boolean Incorporated.
  * Portions Copyright 2001-2003 IBM Corporation.
@@ -66,7 +66,6 @@
 #include "lutil.h"
 #include "lutil_ldap.h"
 #include "ldap_defaults.h"
-#include "ldap_log.h"
 #include "ldap_pvt.h"
 
 #include "common.h"
@@ -139,7 +138,7 @@ usage( void )
 #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"));
@@ -594,7 +593,7 @@ handle_private_option( int i )
                                exit( EXIT_FAILURE );
                        }
                        for ( ispecs = 0; specs[ ispecs ] != NULL; ispecs++ )
-                               /* count'em */
+                               /* count'em */ ;
 
                        ds = ldap_memcalloc( ispecs + 1, sizeof( LDAPDerefSpec ) );
                        if ( ds == NULL ) {
@@ -625,7 +624,7 @@ handle_private_option( int i )
                } else if ( tool_is_oid( control ) ) {
                        if ( c != NULL ) {
                                int i;
-                               for ( i = 0; c[ i ].ldctl_oid != NULL; i++ ) {
+                               for ( i = 0; i < nctrls; i++ ) {
                                        if ( strcmp( control, c[ i ].ldctl_oid ) == 0 ) {
                                                fprintf( stderr, "%s control previously specified\n", control );
                                                exit( EXIT_FAILURE );
@@ -646,18 +645,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;
@@ -665,6 +666,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 */
@@ -773,22 +779,6 @@ private_conn_setup( LDAP *ld )
                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 );
-       }
 }
 
 int
@@ -912,6 +902,15 @@ main( int argc, char **argv )
        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 );
+                       return EXIT_FAILURE;
+               }
+       }
        save_nctrls = nctrls;
        i = nctrls;
        if ( nctrls > 0
@@ -1230,7 +1229,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;
@@ -1243,7 +1242,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;
@@ -1253,6 +1252,7 @@ getNextPage:
                }
                if ( fp != stdin ) {
                        fclose( fp );
+                       fp = NULL;
                }
        }
 
@@ -1344,6 +1344,11 @@ getNextPage:
        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; ) {
@@ -1371,7 +1376,7 @@ static int dosearch(
        int sizelimit )
 {
        char                    *filter;
-       int                     rc;
+       int                     rc, rc2 = LDAP_OTHER;
        int                     nresponses;
        int                     nentries;
        int                     nreferences;
@@ -1383,6 +1388,8 @@ 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, outlen;
@@ -1418,16 +1425,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 );
        }
 
@@ -1435,13 +1447,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 ) {
@@ -1489,7 +1507,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;
                                }
@@ -1533,9 +1551,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 );
@@ -1569,7 +1586,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.
@@ -1897,4 +1918,3 @@ static int print_result(
 
        return err;
 }
-