]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/test.c
ITS#5980 clear res_matched after successfully chasing referral
[openldap] / libraries / libldap / test.c
index 29cdf494096ac85b6abb5cdabc76189e98f01414..409005996dfdd9c8eeb69557cf9e3890d2b431ce 100644 (file)
@@ -1,7 +1,16 @@
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2009 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
 #include "ldap-int.h"
 
 /* local functions */
-#ifndef HAVE_GETLINE
-static char *getline LDAP_P(( char *line, int len, FILE *fp, char *prompt ));
-#endif
-static char **get_list LDAP_P(( char *prompt ));
-static int file_read LDAP_P(( char *path, struct berval *bv ));
-static LDAPMod **get_modlist LDAP_P(( char *prompt1, char *prompt2, char *prompt3 ));
+static char *get_line LDAP_P(( char *line, int len, FILE *fp, const char *prompt ));
+static char **get_list LDAP_P(( const char *prompt ));
+static int file_read LDAP_P(( const char *path, struct berval *bv ));
+static LDAPMod **get_modlist LDAP_P(( const char *prompt1,
+       const char *prompt2, const char *prompt3 ));
 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
-static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm, char *s ));
+static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm,
+       const char *s ));
 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
 static void free_list LDAP_P(( char **list ));
 
-#define NOCACHEERRMSG  "don't compile with -DLDAP_NOCACHE if you desire local caching"
-
 static char *dnsuffix;
 
-#ifndef HAVE_GETLINE
 static char *
-getline( char *line, int len, FILE *fp, char *prompt )
+get_line( char *line, int len, FILE *fp, const char *prompt )
 {
-       printf(prompt);
+       fputs(prompt, stdout);
 
        if ( fgets( line, len, fp ) == NULL )
                return( NULL );
@@ -62,10 +68,9 @@ getline( char *line, int len, FILE *fp, char *prompt )
 
        return( line );
 }
-#endif
 
 static char **
-get_list( char *prompt )
+get_list( const char *prompt )
 {
        static char     buf[256];
        int             num;
@@ -74,7 +79,7 @@ get_list( char *prompt )
        num = 0;
        result = (char **) 0;
        while ( 1 ) {
-               getline( buf, sizeof(buf), stdin, prompt );
+               get_line( buf, sizeof(buf), stdin, prompt );
 
                if ( *buf == '\0' )
                        break;
@@ -111,7 +116,7 @@ free_list( char **list )
 
 
 static int
-file_read( char *path, struct berval *bv )
+file_read( const char *path, struct berval *bv )
 {
        FILE            *fp;
        ber_slen_t      rlen;
@@ -157,11 +162,14 @@ file_read( char *path, struct berval *bv )
 
 
 static LDAPMod **
-get_modlist( char *prompt1, char *prompt2, char *prompt3 )
+get_modlist(
+       const char *prompt1,
+       const char *prompt2,
+       const char *prompt3 )
 {
        static char     buf[256];
        int             num;
-       LDAPMod         tmp;
+       LDAPMod         tmp = { 0 };
        LDAPMod         **result;
        struct berval   **bvals;
 
@@ -169,14 +177,14 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
        result = NULL;
        while ( 1 ) {
                if ( prompt1 ) {
-                       getline( buf, sizeof(buf), stdin, prompt1 );
+                       get_line( buf, sizeof(buf), stdin, prompt1 );
                        tmp.mod_op = atoi( buf );
 
                        if ( tmp.mod_op == -1 || buf[0] == '\0' )
                                break;
                }
 
-               getline( buf, sizeof(buf), stdin, prompt2 );
+               get_line( buf, sizeof(buf), stdin, prompt2 );
                if ( buf[0] == '\0' )
                        break;
                tmp.mod_type = strdup( buf );
@@ -197,6 +205,10 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
                                    6 ) == 0 ) {
                                        if ( file_read( tmp.mod_values[i] + 6,
                                            bvals[i] ) < 0 ) {
+                                               free( bvals );
+                                               for ( i = 0; i<num; i++ )
+                                                       free( result[ i ] );
+                                               free( result );
                                                return( NULL );
                                        }
                                } else {
@@ -240,23 +252,13 @@ bind_prompt( LDAP *ld,
        printf("rebind for request=%ld msgid=%ld url=%s\n",
                request, (long) msgid, url );
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
-               getline( dn, sizeof(dn), stdin,
-                   "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
-       if (( authmethod = atoi( dn )) == 3 ) {
-               authmethod = LDAP_AUTH_KRBV4;
-               } else {
-               authmethod |= 0x80;
-               }
-#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
        authmethod = LDAP_AUTH_SIMPLE;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
 
-               getline( dn, sizeof(dn), stdin, "re-bind dn? " );
+               get_line( dn, sizeof(dn), stdin, "re-bind dn? " );
                strcat( dn, dnsuffix );
 
        if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
-                       getline( passwd, sizeof(passwd), stdin,
+                       get_line( passwd, sizeof(passwd), stdin,
                            "re-bind password? " );
                } else {
                        passwd[0] = '\0';
@@ -275,7 +277,8 @@ main( int argc, char **argv )
        char            passwd[64], dn[256], rdn[64], attr[64], value[256];
        char            filter[256], *host, **types;
        char            **exdn;
-       char            *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
+       static const char usage[] =
+               "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
        int             bound, all, scope, attrsonly;
        LDAPMessage     *res;
        LDAPMod         **mods, **attrs;
@@ -352,7 +355,7 @@ main( int argc, char **argv )
        }
 
        if ( copyfname != NULL ) {
-               if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY | O_CREAT,
+               if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
                    0600 ))  == -1 ) {
                        perror( copyfname );
                        exit ( EXIT_FAILURE );
@@ -365,7 +368,7 @@ main( int argc, char **argv )
        timeout.tv_usec = 0;
 
        (void) memset( line, '\0', sizeof(line) );
-       while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
+       while ( get_line( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
                command1 = line[0];
                command2 = line[1];
                command3 = line[2];
@@ -374,7 +377,7 @@ main( int argc, char **argv )
                case 'a':       /* add or abandon */
                        switch ( command2 ) {
                        case 'd':       /* add */
-                               getline( dn, sizeof(dn), stdin, "dn? " );
+                               get_line( dn, sizeof(dn), stdin, "dn? " );
                                strcat( dn, dnsuffix );
                                if ( (attrs = get_modlist( NULL, "attr? ",
                                    "value? " )) == NULL )
@@ -387,7 +390,7 @@ main( int argc, char **argv )
                                break;
 
                        case 'b':       /* abandon */
-                               getline( line, sizeof(line), stdin, "msgid? " );
+                               get_line( line, sizeof(line), stdin, "msgid? " );
                                id = atoi( line );
                                if ( ldap_abandon( ld, id ) != 0 )
                                        ldap_perror( ld, "ldap_abandon" );
@@ -400,18 +403,12 @@ main( int argc, char **argv )
                        break;
 
                case 'b':       /* asynch bind */
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
-                       getline( line, sizeof(line), stdin,
-                           "method (0->simple, 1->krbv41, 2->krbv42)? " );
-                       method = atoi( line ) | 0x80;
-#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
                        method = LDAP_AUTH_SIMPLE;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
-                       getline( dn, sizeof(dn), stdin, "dn? " );
+                       get_line( dn, sizeof(dn), stdin, "dn? " );
                        strcat( dn, dnsuffix );
 
                        if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
-                               getline( passwd, sizeof(passwd), stdin,
+                               get_line( passwd, sizeof(passwd), stdin,
                                    "password? " );
                        else
                                passwd[0] = '\0';
@@ -426,22 +423,12 @@ main( int argc, char **argv )
                        break;
 
                case 'B':       /* synch bind */
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
-                       getline( line, sizeof(line), stdin,
-                           "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
-                       method = atoi( line );
-                       if ( method == 3 )
-                               method = LDAP_AUTH_KRBV4;
-                       else
-                               method = method | 0x80;
-#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
                        method = LDAP_AUTH_SIMPLE;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
-                       getline( dn, sizeof(dn), stdin, "dn? " );
+                       get_line( dn, sizeof(dn), stdin, "dn? " );
                        strcat( dn, dnsuffix );
 
                        if ( dn[0] != '\0' )
-                               getline( passwd, sizeof(passwd), stdin,
+                               get_line( passwd, sizeof(passwd), stdin,
                                    "password? " );
                        else
                                passwd[0] = '\0';
@@ -457,10 +444,10 @@ main( int argc, char **argv )
                        break;
 
                case 'c':       /* compare */
-                       getline( dn, sizeof(dn), stdin, "dn? " );
+                       get_line( dn, sizeof(dn), stdin, "dn? " );
                        strcat( dn, dnsuffix );
-                       getline( attr, sizeof(attr), stdin, "attr? " );
-                       getline( value, sizeof(value), stdin, "value? " );
+                       get_line( attr, sizeof(attr), stdin, "attr? " );
+                       get_line( value, sizeof(value), stdin, "value? " );
 
                        if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
                                ldap_perror( ld, "ldap_compare" );
@@ -470,7 +457,7 @@ main( int argc, char **argv )
 
                case 'd':       /* turn on debugging */
 #ifdef LDAP_DEBUG
-                       getline( line, sizeof(line), stdin, "debug level? " );
+                       get_line( line, sizeof(line), stdin, "debug level? " );
                        ldap_debug = atoi( line );
 #ifdef LBER_DEBUG
                        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
@@ -483,7 +470,7 @@ main( int argc, char **argv )
                        break;
 
                case 'E':       /* explode a dn */
-                       getline( line, sizeof(line), stdin, "dn? " );
+                       get_line( line, sizeof(line), stdin, "dn? " );
                        exdn = ldap_explode_dn( line, 0 );
                        for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
                                printf( "\t%s\n", exdn[i] );
@@ -491,18 +478,18 @@ main( int argc, char **argv )
                        break;
 
                case 'g':       /* set next msgid */
-                       getline( line, sizeof(line), stdin, "msgid? " );
+                       get_line( line, sizeof(line), stdin, "msgid? " );
                        ld->ld_msgid = atoi( line );
                        break;
 
                case 'v':       /* set version number */
-                       getline( line, sizeof(line), stdin, "version? " );
+                       get_line( line, sizeof(line), stdin, "version? " );
                        ld->ld_version = atoi( line );
                        break;
 
                case 'm':       /* modify or modifyrdn */
                        if ( strncmp( line, "modify", 4 ) == 0 ) {
-                               getline( dn, sizeof(dn), stdin, "dn? " );
+                               get_line( dn, sizeof(dn), stdin, "dn? " );
                                strcat( dn, dnsuffix );
                                if ( (mods = get_modlist(
                                    "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
@@ -515,9 +502,9 @@ main( int argc, char **argv )
                                        printf( "Modify initiated with id %d\n",
                                            id );
                        } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
-                               getline( dn, sizeof(dn), stdin, "dn? " );
+                               get_line( dn, sizeof(dn), stdin, "dn? " );
                                strcat( dn, dnsuffix );
-                               getline( rdn, sizeof(rdn), stdin, "newrdn? " );
+                               get_line( rdn, sizeof(rdn), stdin, "newrdn? " );
                                if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
                                        ldap_perror( ld, "ldap_modrdn" );
                                else
@@ -536,13 +523,13 @@ main( int argc, char **argv )
                case 'r':       /* result or remove */
                        switch ( command3 ) {
                        case 's':       /* result */
-                               getline( line, sizeof(line), stdin,
+                               get_line( line, sizeof(line), stdin,
                                    "msgid (-1=>any)? " );
                                if ( line[0] == '\0' )
                                        id = -1;
                                else
                                        id = atoi( line );
-                               getline( line, sizeof(line), stdin,
+                               get_line( line, sizeof(line), stdin,
                                    "all (0=>any, 1=>all)? " );
                                if ( line[0] == '\0' )
                                        all = 1;
@@ -560,7 +547,7 @@ main( int argc, char **argv )
                                break;
 
                        case 'm':       /* remove */
-                               getline( dn, sizeof(dn), stdin, "dn? " );
+                               get_line( dn, sizeof(dn), stdin, "dn? " );
                                strcat( dn, dnsuffix );
                                if ( (id = ldap_delete( ld, dn )) == -1 )
                                        ldap_perror( ld, "ldap_delete" );
@@ -576,15 +563,15 @@ main( int argc, char **argv )
                        break;
 
                case 's':       /* search */
-                       getline( dn, sizeof(dn), stdin, "searchbase? " );
+                       get_line( dn, sizeof(dn), stdin, "searchbase? " );
                        strcat( dn, dnsuffix );
-                       getline( line, sizeof(line), stdin,
-                           "scope (0=Base, 1=One Level, 2=Subtree)? " );
+                       get_line( line, sizeof(line), stdin,
+                           "scope (0=baseObject, 1=oneLevel, 2=subtree, 3=children)? " );
                        scope = atoi( line );
-                       getline( filter, sizeof(filter), stdin,
+                       get_line( filter, sizeof(filter), stdin,
                            "search filter (e.g. sn=jones)? " );
                        types = get_list( "attrs to return? " );
-                       getline( line, sizeof(line), stdin,
+                       get_line( line, sizeof(line), stdin,
                            "attrsonly (0=attrs&values, 1=attrs only)? " );
                        attrsonly = atoi( line );
 
@@ -598,25 +585,12 @@ main( int argc, char **argv )
                        break;
 
                case 't':       /* set timeout value */
-                       getline( line, sizeof(line), stdin, "timeout? " );
+                       get_line( line, sizeof(line), stdin, "timeout? " );
                        timeout.tv_sec = atoi( line );
                        break;
 
-               case 'l':       /* URL search */
-                       getline( line, sizeof(line), stdin,
-                           "attrsonly (0=attrs&values, 1=attrs only)? " );
-                       attrsonly = atoi( line );
-                       getline( line, sizeof(line), stdin, "LDAP URL? " );
-                       if (( id = ldap_url_search( ld, line, attrsonly  ))
-                               == -1 ) {
-                           ldap_perror( ld, "ldap_url_search" );
-                       } else {
-                           printf( "URL search initiated with id %d\n", id );
-                       }
-                       break;
-
                case 'p':       /* parse LDAP URL */
-                       getline( line, sizeof(line), stdin, "LDAP URL? " );
+                       get_line( line, sizeof(line), stdin, "LDAP URL? " );
                        if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
                            fprintf( stderr, "ldap_url_parse: error %d\n", i );
                        } else {
@@ -641,67 +615,39 @@ main( int argc, char **argv )
                                    printf( " <%s>", ludp->lud_attrs[ i ] );
                                }
                            }
-                           printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
-                               "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
-                               ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
+                           printf( "\n\t scope: %s\n",
+                                       ludp->lud_scope == LDAP_SCOPE_BASE ? "baseObject"
+                                       : ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "oneLevel"
+                                       : ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "subtree"
+#ifdef LDAP_SCOPE_SUBORDINATE
+                                       : ludp->lud_scope == LDAP_SCOPE_SUBORDINATE ? "children"
+#endif
+                                       : "**invalid**" );
                            printf( "\tfilter: <%s>\n", ludp->lud_filter );
                            ldap_free_urldesc( ludp );
                        }
                            break;
 
                case 'n':       /* set dn suffix, for convenience */
-                       getline( line, sizeof(line), stdin, "DN suffix? " );
+                       get_line( line, sizeof(line), stdin, "DN suffix? " );
                        strcpy( dnsuffix, line );
                        break;
 
-               case 'e':       /* enable cache */
-#ifdef LDAP_NOCACHE
-                       printf( NOCACHEERRMSG );
-#else /* LDAP_NOCACHE */
-                       getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
-                       i = atoi( line );
-                       getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
-                       if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
-                               printf( "local cache is on\n" ); 
-                       } else {
-                               printf( "ldap_enable_cache failed\n" ); 
-                       }
-#endif /* LDAP_NOCACHE */
-                       break;
-
-               case 'x':       /* uncache entry */
-#ifdef LDAP_NOCACHE
-                       printf( NOCACHEERRMSG );
-#else /* LDAP_NOCACHE */
-                       getline( line, sizeof(line), stdin, "DN? " );
-                       ldap_uncache_entry( ld, line );
-#endif /* LDAP_NOCACHE */
-                       break;
-
-               case 'X':       /* uncache request */
-#ifdef LDAP_NOCACHE
-                       printf( NOCACHEERRMSG );
-#else /* LDAP_NOCACHE */
-                       getline( line, sizeof(line), stdin, "request msgid? " );
-                       ldap_uncache_request( ld, atoi( line ));
-#endif /* LDAP_NOCACHE */
-                       break;
-
                case 'o':       /* set ldap options */
-                       getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
+                       get_line( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
                        ld->ld_deref = atoi( line );
-                       getline( line, sizeof(line), stdin, "timelimit?" );
+                       get_line( line, sizeof(line), stdin, "timelimit?" );
                        ld->ld_timelimit = atoi( line );
-                       getline( line, sizeof(line), stdin, "sizelimit?" );
+                       get_line( line, sizeof(line), stdin, "sizelimit?" );
                        ld->ld_sizelimit = atoi( line );
 
                        LDAP_BOOL_ZERO(&ld->ld_options);
 
-                       getline( line, sizeof(line), stdin,
+                       get_line( line, sizeof(line), stdin,
                                "Recognize and chase referrals (0=no, 1=yes)?" );
                        if ( atoi( line ) != 0 ) {
                                LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
-                               getline( line, sizeof(line), stdin,
+                               get_line( line, sizeof(line), stdin,
                                        "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
                                if ( atoi( line ) != 0 ) {
                                        ldap_set_rebind_proc( ld, bind_prompt, NULL );
@@ -709,39 +655,16 @@ main( int argc, char **argv )
                        }
                        break;
 
-               case 'O':       /* set cache options */
-#ifdef LDAP_NOCACHE
-                       printf( NOCACHEERRMSG );
-#else /* LDAP_NOCACHE */
-                       getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
-                       switch( atoi( line )) {
-                       case 0:
-                               ldap_set_cache_options( ld, 0 );
-                               break;
-                       case 1:
-                               ldap_set_cache_options( ld,
-                                       LDAP_CACHE_OPT_CACHENOERRS );
-                               break;
-                       case 2:
-                               ldap_set_cache_options( ld,
-                                       LDAP_CACHE_OPT_CACHEALLERRS );
-                               break;
-                       default:
-                               printf( "not a valid cache option\n" );
-                       }
-#endif /* LDAP_NOCACHE */
-                       break;
-
                case '?':       /* help */
-    printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
-    printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
-    printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
-    printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
-    printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
-    printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
-    printf( "          [?]help       [o]ptions         [O]cache options\n" );
-    printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
-    printf( "          [x]uncache entry  [X]uncache request\n" );
+                       printf(
+"Commands: [ad]d         [ab]andon         [b]ind\n"
+"          [B]ind async  [c]ompare\n"
+"          [modi]fy      [modr]dn          [rem]ove\n"
+"          [res]ult      [s]earch          [q]uit/unbind\n\n"
+"          [d]ebug       set ms[g]id\n"
+"          d[n]suffix    [t]imeout         [v]ersion\n"
+"          [?]help       [o]ptions"
+"          [E]xplode dn  [p]arse LDAP URL\n" );
                        break;
 
                default:
@@ -802,7 +725,7 @@ handle_result( LDAP *ld, LDAPMessage *lm )
 }
 
 static void
-print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
+print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
 {
        ldap_result2error( ld, lm, 1 );
        ldap_perror( ld, s );