]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/test.c
Don't copy and leak user; it's an env var so just use it directly.
[openldap] / libraries / libldap / test.c
index 7e3770f53826bf72be15fe360dd4fe62cb605490..77aca1e03602d455d2acb9aa59b06404b67a7543 100644 (file)
@@ -1,12 +1,14 @@
+/* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
 
 #include <ac/ctype.h>
 #include <ac/socket.h>
@@ -83,7 +85,7 @@ get_list( char *prompt )
                        result = (char **) realloc( result,
                            sizeof(char *) * (num + 1) );
 
-               result[num++] = (char *) ldap_strdup( buf );
+               result[num++] = (char *) strdup( buf );
        }
        if ( result == (char **) 0 )
                return( NULL );
@@ -112,7 +114,7 @@ static int
 file_read( char *path, struct berval *bv )
 {
        FILE            *fp;
-       long            rlen;
+       ber_slen_t      rlen;
        int             eof;
 
        if (( fp = fopen( path, "r" )) == NULL ) {
@@ -144,7 +146,7 @@ file_read( char *path, struct berval *bv )
        eof = feof( fp );
        fclose( fp );
 
-       if ( (unsigned long) rlen != bv->bv_len ) {
+       if ( (ber_len_t) rlen != bv->bv_len ) {
                perror( path );
                free( bv->bv_val );
                return( -1 );
@@ -177,7 +179,7 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
                getline( buf, sizeof(buf), stdin, prompt2 );
                if ( buf[0] == '\0' )
                        break;
-               tmp.mod_type = ldap_strdup( buf );
+               tmp.mod_type = strdup( buf );
 
                tmp.mod_values = get_list( prompt3 );
 
@@ -226,49 +228,49 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
 }
 
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 static int
-bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
-       int freeit )
+bind_prompt( LDAP *ld,
+       LDAP_CONST char *url,
+       ber_tag_t request, ber_int_t msgid,
+       void *params )
 {
        static char     dn[256], passwd[256];
+       int     authmethod;
+
+       printf("rebind for request=%d msgid=%ld url=%s\n",
+               request, (long) msgid, url );
 
-       if ( !freeit ) {
-#ifdef HAVE_KERBEROS
+#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 (( *authmethodp = atoi( dn )) == 3 ) {
-                       *authmethodp = LDAP_AUTH_KRBV4;
+       if (( authmethod = atoi( dn )) == 3 ) {
+               authmethod = LDAP_AUTH_KRBV4;
                } else {
-                       *authmethodp |= 0x80;
+               authmethod |= 0x80;
                }
-#else /* HAVE_KERBEROS */
-               *authmethodp = LDAP_AUTH_SIMPLE;
-#endif /* HAVE_KERBEROS */
+#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? " );
                strcat( dn, dnsuffix );
-               *dnp = dn;
 
-               if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
+       if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
                        getline( passwd, sizeof(passwd), stdin,
                            "re-bind password? " );
                } else {
                        passwd[0] = '\0';
                }
-               *passwdp = passwd;
-       }
 
-       return( LDAP_SUCCESS );
+       return ldap_bind_s( ld, dn, passwd, authmethod);
 }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 int
 main( int argc, char **argv )
 {
        LDAP            *ld = NULL;
-       int             i, c, port, cldapflg, errflg, method, id, msgtype;
+       int             i, c, port, errflg, method, id, msgtype;
        char            line[256], command1, command2, command3;
        char            passwd[64], dn[256], rdn[64], attr[64], value[256];
        char            filter[256], *host, **types;
@@ -285,24 +287,16 @@ main( int argc, char **argv )
        host = NULL;
        port = LDAP_PORT;
        dnsuffix = "";
-       cldapflg = errflg = 0;
+       errflg = 0;
 
-       while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
+       while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
                switch( c ) {
-               case 'u':
-#ifdef LDAP_CONNECTIONLESS
-                       cldapflg++;
-#else /* LDAP_CONNECTIONLESS */
-                       printf( "Compile with -DLDAP_CONNECTIONLESS for UDP support\n" );
-#endif /* LDAP_CONNECTIONLESS */
-                       break;
-
                case 'd':
 #ifdef LDAP_DEBUG
                        ldap_debug = atoi( optarg );
 #ifdef LBER_DEBUG
                        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
-                               lber_debug = ldap_debug;
+                               ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
                        }
 #endif
 #else
@@ -323,13 +317,13 @@ main( int argc, char **argv )
                        break;
 
                case 't':       /* copy ber's to given file */
-                       copyfname = ldap_strdup( optarg );
-                       copyoptions = LBER_TO_FILE;
+                       copyfname = strdup( optarg );
+/*                     copyoptions = LBER_TO_FILE; */
                        break;
 
                case 'T':       /* only output ber's to given file */
-                       copyfname = ldap_strdup( optarg );
-                       copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
+                       copyfname = strdup( optarg );
+/*                     copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
                        break;
 
                default:
@@ -344,32 +338,26 @@ main( int argc, char **argv )
 
        if ( errflg || optind < argc - 1 ) {
                fprintf( stderr, usage, argv[ 0 ] );
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
        
-       printf( "%sldap_open( %s, %d )\n", cldapflg ? "c" : "",
+       printf( "ldap_init( %s, %d )\n",
                host == NULL ? "(null)" : host, port );
 
-       if ( cldapflg ) {
-#ifdef LDAP_CONNECTIONLESS
-               ld = cldap_open( host, port );
-#endif /* LDAP_CONNECTIONLESS */
-       } else {
-               ld = ldap_open( host, port );
-       }
+       ld = ldap_init( host, port );
 
        if ( ld == NULL ) {
-               perror( "ldap_open" );
-               exit(1);
+               perror( "ldap_init" );
+               exit( EXIT_FAILURE );
        }
 
        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,
                    0600 ))  == -1 ) {
                        perror( copyfname );
-                       exit ( 1 );
+                       exit ( EXIT_FAILURE );
                }
-               ld->ld_sb.sb_options = copyoptions;
+               ld->ld_sb->sb_options = copyoptions;
        }
 
        bound = 0;
@@ -412,13 +400,13 @@ main( int argc, char **argv )
                        break;
 
                case 'b':       /* asynch bind */
-#ifdef HAVE_KERBEROS
+#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 /* HAVE_KERBEROS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
                        method = LDAP_AUTH_SIMPLE;
-#endif /* HAVE_KERBEROS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
                        getline( dn, sizeof(dn), stdin, "dn? " );
                        strcat( dn, dnsuffix );
 
@@ -438,7 +426,7 @@ main( int argc, char **argv )
                        break;
 
                case 'B':       /* synch bind */
-#ifdef HAVE_KERBEROS
+#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 );
@@ -446,9 +434,9 @@ main( int argc, char **argv )
                                method = LDAP_AUTH_KRBV4;
                        else
                                method = method | 0x80;
-#else /* HAVE_KERBEROS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
                        method = LDAP_AUTH_SIMPLE;
-#endif /* HAVE_KERBEROS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
                        getline( dn, sizeof(dn), stdin, "dn? " );
                        strcat( dn, dnsuffix );
 
@@ -486,7 +474,7 @@ main( int argc, char **argv )
                        ldap_debug = atoi( line );
 #ifdef LBER_DEBUG
                        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
-                               lber_debug = ldap_debug;
+                               ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
                        }
 #endif
 #else
@@ -541,17 +529,8 @@ main( int argc, char **argv )
                        break;
 
                case 'q':       /* quit */
-#ifdef LDAP_CONNECTIONLESS
-                       if ( cldapflg )
-                               cldap_close( ld );
-#endif /* LDAP_CONNECTIONLESS */
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
-                       if ( !cldapflg )
-#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
-                       if ( !cldapflg && bound )
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
-                               ldap_unbind( ld );
-                       exit( 0 );
+                       ldap_unbind( ld );
+                       exit( EXIT_SUCCESS );
                        break;
 
                case 'r':       /* result or remove */
@@ -577,7 +556,7 @@ main( int argc, char **argv )
                                printf( "\nresult: msgtype %d msgid %d\n",
                                    msgtype, res->lm_msgid );
                                handle_result( ld, res );
-                               res = NULLMSG;
+                               res = NULL;
                                break;
 
                        case 'm':       /* remove */
@@ -609,28 +588,12 @@ main( int argc, char **argv )
                            "attrsonly (0=attrs&values, 1=attrs only)? " );
                        attrsonly = atoi( line );
 
-                       if ( cldapflg ) {
-#ifdef LDAP_CONNECTIONLESS
-                           getline( line, sizeof(line), stdin,
-                               "Requestor DN (for logging)? " );
-                           if ( cldap_search_s( ld, dn, scope, filter, types,
-                                   attrsonly, &res, line ) != 0 ) {
-                               ldap_perror( ld, "cldap_search_s" );
-                           } else {
-                               printf( "\nresult: msgid %d\n",
-                                   res->lm_msgid );
-                               handle_result( ld, res );
-                               res = NULLMSG;
-                           }
-#endif /* LDAP_CONNECTIONLESS */
-                       } else {
                            if (( id = ldap_search( ld, dn, scope, filter,
                                    types, attrsonly  )) == -1 ) {
                                ldap_perror( ld, "ldap_search" );
                            } else {
                                printf( "Search initiated with id %d\n", id );
                            }
-                       }
                        free_list( types );
                        break;
 
@@ -639,37 +602,6 @@ main( int argc, char **argv )
                        timeout.tv_sec = atoi( line );
                        break;
 
-               case 'U':       /* set ufn search prefix */
-                       getline( line, sizeof(line), stdin, "ufn prefix? " );
-                       ldap_ufn_setprefix( ld, line );
-                       break;
-
-               case 'u':       /* user friendly search w/optional timeout */
-                       getline( dn, sizeof(dn), stdin, "ufn? " );
-                       strcat( dn, dnsuffix );
-                       types = get_list( "attrs to return? " );
-                       getline( line, sizeof(line), stdin,
-                           "attrsonly (0=attrs&values, 1=attrs only)? " );
-                       attrsonly = atoi( line );
-
-                       if ( command2 == 't' ) {
-                               id = ldap_ufn_search_c( ld, dn, types,
-                                   attrsonly, &res, ldap_ufn_timeout,
-                                   &timeout );
-                       } else {
-                               id = ldap_ufn_search_s( ld, dn, types,
-                                   attrsonly, &res );
-                       }
-                       if ( res == NULL )
-                               ldap_perror( ld, "ldap_ufn_search" );
-                       else {
-                               printf( "\nresult: err %d\n", id );
-                               handle_result( ld, res );
-                               res = NULLMSG;
-                       }
-                       free_list( types );
-                       break;
-
                case 'l':       /* URL search */
                        getline( line, sizeof(line), stdin,
                            "attrsonly (0=attrs&values, 1=attrs only)? " );
@@ -765,34 +697,6 @@ main( int argc, char **argv )
 
                        LDAP_BOOL_ZERO(&ld->ld_options);
 
-#ifdef STR_TRANSLATION
-                       getline( line, sizeof(line), stdin,
-                               "Automatic translation of T.61 strings (0=no, 1=yes)?" );
-                       if ( atoi( line ) == 0 ) {
-                               ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
-                       } else {
-                               ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
-#ifdef LDAP_CHARSET_8859
-                               getline( line, sizeof(line), stdin,
-                                       "Translate to/from ISO-8859 (0=no, 1=yes?" );
-                               if ( atoi( line ) != 0 ) {
-                                       ldap_set_string_translators( ld,
-                                           ldap_8859_to_t61,
-                                           ldap_t61_to_8859 );
-                               }
-#endif /* LDAP_CHARSET_8859 */
-                       }
-#endif /* STR_TRANSLATION */
-
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-                       getline( line, sizeof(line), stdin,
-                               "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
-                       if ( atoi( line ) != 0 ) {
-                               LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS);
-                       }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
                        getline( line, sizeof(line), stdin,
                                "Recognize and chase referrals (0=no, 1=yes)?" );
                        if ( atoi( line ) != 0 ) {
@@ -800,10 +704,9 @@ main( int argc, char **argv )
                                getline( 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 );
+                                       ldap_set_rebind_proc( ld, bind_prompt, NULL );
                                }
                        }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
                        break;
 
                case 'O':       /* set cache options */
@@ -834,12 +737,11 @@ main( int argc, char **argv )
     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( "          [u]fn search  [ut]fn search with timeout\n" );
     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
-    printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\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" );
                        break;
 
                default:
@@ -893,7 +795,8 @@ handle_result( LDAP *ld, LDAPMessage *lm )
                break;
 
        default:
-               printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
+               printf( "Unknown result type 0x%lx\n",
+                       (unsigned long) lm->lm_msgtype );
                print_ldap_result( ld, lm, "unknown" );
        }
 }
@@ -906,7 +809,7 @@ print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
 /*
        if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
                fprintf( stderr, "Additional info: %s\n", ld->ld_error );
-       if ( NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
+       if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
                fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
 */
 }
@@ -914,14 +817,14 @@ print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
 static void
 print_search_entry( LDAP *ld, LDAPMessage *res )
 {
-       BerElement      *ber;
-       char            *a, *dn, *ufn;
-       struct berval   **vals;
-       int             i;
        LDAPMessage     *e;
 
-       for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
-           e = ldap_next_entry( ld, e ) ) {
+       for ( e = ldap_first_entry( ld, res ); e != NULL;
+           e = ldap_next_entry( ld, e ) )
+       {
+               BerElement      *ber = NULL;
+               char *a, *dn, *ufn;
+
                if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
                        break;
 
@@ -935,17 +838,21 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
                free( ufn );
 
                for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
-                   a = ldap_next_attribute( ld, e, ber ) ) {
+                   a = ldap_next_attribute( ld, e, ber ) )
+               {
+                       struct berval   **vals;
+
                        printf( "\t\tATTR: %s\n", a );
                        if ( (vals = ldap_get_values_len( ld, e, a ))
                            == NULL ) {
                                printf( "\t\t\t(no values)\n" );
                        } else {
+                               int i;
                                for ( i = 0; vals[i] != NULL; i++ ) {
                                        int     j, nonascii;
 
                                        nonascii = 0;
-                                       for ( j = 0; (unsigned long) j < vals[i]->bv_len; j++ )
+                                       for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
                                                if ( !isascii( vals[i]->bv_val[j] ) ) {
                                                        nonascii = 1;
                                                        break;
@@ -954,7 +861,7 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
                                        if ( nonascii ) {
                                                printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
 #ifdef BPRINT_NONASCII
-                                               lber_bprint( vals[i]->bv_val,
+                                               ber_bprint( vals[i]->bv_val,
                                                    vals[i]->bv_len );
 #endif /* BPRINT_NONASCII */
                                                continue;
@@ -965,9 +872,13 @@ print_search_entry( LDAP *ld, LDAPMessage *res )
                                ber_bvecfree( vals );
                        }
                }
+
+               if(ber != NULL) {
+                       ber_free( ber, 0 );
+               }
        }
 
        if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
-           || res->lm_chain != NULLMSG )
+           || res->lm_chain != NULL )
                print_ldap_result( ld, res, "search" );
 }