X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Ftest.c;h=793bed66c6d69a6df4d82e43049f22bf864c5d98;hb=bcf8f7eeb788faa25efe0ea78e66e83e9d9b4d73;hp=6abfbe2bf7e7ddec3ab95b986cac8091df491797;hpb=8333a03346539c0e4fc4c7c7df723c541fb14d03;p=openldap diff --git a/libraries/libldap/test.c b/libraries/libldap/test.c index 6abfbe2bf7..793bed66c6 100644 --- a/libraries/libldap/test.c +++ b/libraries/libldap/test.c @@ -1,7 +1,14 @@ +/* $OpenLDAP$ */ +/* + * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ + #include "portable.h" #include -#include + +#include #include #include @@ -26,6 +33,13 @@ */ #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 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_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res )); @@ -33,7 +47,7 @@ static void free_list LDAP_P(( char **list )); #define NOCACHEERRMSG "don't compile with -DLDAP_NOCACHE if you desire local caching" -char *dnsuffix; +static char *dnsuffix; #ifndef HAVE_GETLINE static char * @@ -100,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 ) { @@ -132,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 ); @@ -214,15 +228,14 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 ) } -#ifdef LDAP_REFERRALS -int +static int bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp, int freeit ) { static char dn[256], passwd[256]; 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 ) { @@ -230,9 +243,9 @@ bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp, } else { *authmethodp |= 0x80; } -#else /* HAVE_KERBEROS */ +#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */ *authmethodp = LDAP_AUTH_SIMPLE; -#endif /* HAVE_KERBEROS */ +#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */ getline( dn, sizeof(dn), stdin, "re-bind dn? " ); strcat( dn, dnsuffix ); @@ -249,7 +262,6 @@ bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp, return( LDAP_SUCCESS ); } -#endif /* LDAP_REFERRALS */ int @@ -270,9 +282,6 @@ main( int argc, char **argv ) int copyoptions = 0; LDAPURLDesc *ludp; - extern char *optarg; - extern int optind; - host = NULL; port = LDAP_PORT; dnsuffix = ""; @@ -291,9 +300,11 @@ main( int argc, char **argv ) 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 printf( "Compile with -DLDAP_DEBUG for debugging\n" ); #endif @@ -333,10 +344,11 @@ 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( "%s( %s, %d )\n", + cldapflg ? "cldap_open" : "ldap_init", host == NULL ? "(null)" : host, port ); if ( cldapflg ) { @@ -344,19 +356,19 @@ main( int argc, char **argv ) 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( cldapflg ? "cldap_open" : "ldap_init" ); + exit( EXIT_FAILURE ); } if ( copyfname != NULL ) { 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; } @@ -401,13 +413,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 ); @@ -427,7 +439,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 ); @@ -435,9 +447,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 ); @@ -473,9 +485,11 @@ main( int argc, char **argv ) #ifdef LDAP_DEBUG getline( line, sizeof(line), stdin, "debug level? " ); 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 printf( "Compile with -DLDAP_DEBUG for debugging\n" ); #endif @@ -532,13 +546,11 @@ main( int argc, char **argv ) if ( cldapflg ) cldap_close( ld ); #endif /* LDAP_CONNECTIONLESS */ -#ifdef LDAP_REFERRALS - if ( !cldapflg ) -#else /* LDAP_REFERRALS */ - if ( !cldapflg && bound ) -#endif /* LDAP_REFERRALS */ + + if ( !cldapflg ) { ldap_unbind( ld ); - exit( 0 ); + } + exit( EXIT_SUCCESS ); break; case 'r': /* result or remove */ @@ -564,7 +576,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 */ @@ -607,7 +619,7 @@ main( int argc, char **argv ) printf( "\nresult: msgid %d\n", res->lm_msgid ); handle_result( ld, res ); - res = NULLMSG; + res = NULL; } #endif /* LDAP_CONNECTIONLESS */ } else { @@ -652,7 +664,7 @@ main( int argc, char **argv ) else { printf( "\nresult: err %d\n", id ); handle_result( ld, res ); - res = NULLMSG; + res = NULL; } free_list( types ); break; @@ -750,7 +762,7 @@ main( int argc, char **argv ) getline( line, sizeof(line), stdin, "sizelimit?" ); ld->ld_sizelimit = atoi( line ); - LDAP_BOOL_ZERO(ld); + LDAP_BOOL_ZERO(&ld->ld_options); #ifdef STR_TRANSLATION getline( line, sizeof(line), stdin, @@ -771,26 +783,24 @@ main( int argc, char **argv ) } #endif /* STR_TRANSLATION */ -#ifdef LDAP_DNS +#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, LDAP_BOOL_DNS); + LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS); } -#endif /* LDAP_DNS */ +#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ -#ifdef LDAP_REFERRALS getline( line, sizeof(line), stdin, "Recognize and chase referrals (0=no, 1=yes)?" ); if ( atoi( line ) != 0 ) { - LDAP_BOOL_SET(ld, LDAP_BOOL_REFERRALS); + LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS); 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 ); } } -#endif /* LDAP_REFERRALS */ break; case 'O': /* set cache options */ @@ -880,7 +890,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" ); } } @@ -893,7 +904,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 ); */ } @@ -901,14 +912,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; @@ -922,17 +933,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; @@ -941,7 +956,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; @@ -952,9 +967,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" ); }