From 8be1d467b875a8813359d5203ba5a1897b7ee28d Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Mon, 3 Jul 2000 19:03:22 +0000 Subject: [PATCH] Add reporting of LDAPv3 referrals. Should also report returned controls. Also, as written, the code will behave better in the face of unsolicited noticifications (such as notice of disconnect). However, code needs to be improved to better distinguished such from expected result response. Delete improvements are limited to base object delete. Should be applied to -p[rune] option as well. --- clients/tools/ldapdelete.c | 48 ++++++++++++++++++++++---- clients/tools/ldapmodrdn.c | 59 ++++++++++++++++++++++++++------ clients/tools/ldappasswd.c | 70 ++++++++++++++++++++++++++++++++------ 3 files changed, 150 insertions(+), 27 deletions(-) diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c index 64dc8b39c5..14fb945969 100644 --- a/clients/tools/ldapdelete.c +++ b/clients/tools/ldapdelete.c @@ -435,7 +435,10 @@ static int dodelete( LDAP *ld, const char *dn) { - int rc; + int id; + int rc, code; + char *matcheddn = NULL, *text = NULL, **refs = NULL; + LDAPMessage *res; if ( verbose ) { printf( "%sdeleting entry \"%s\"\n", @@ -451,16 +454,49 @@ static int dodelete( */ if ( prune ) deletechildren( ld, dn ); - rc = ldap_delete_s( ld, dn ); + rc = ldap_delete_ext( ld, dn, NULL, NULL, &id ); if ( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_delete" ); + ldap_perror( ld, "ldap_delete_ext" ); + return rc; } - if ( verbose ) { - printf( "\tremoved\n" ); + rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ); + if ( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_result" ); + return rc; } - return rc; + rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 ); + + if( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_parse_result" ); + return rc; + } + + if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) { + printf( "Result: %s (%d)\n", ldap_err2string( code ), code ); + + if( text && *text ) { + printf( "Additional info: %s\n", text ); + } + + if( matcheddn && *matcheddn ) { + printf( "Matched DN: %s\n", matcheddn ); + } + + if( refs ) { + int i; + for( i=0; refs[i]; i++ ) { + printf("Referral: %s\n", refs[i] ); + } + } + } + + ber_memfree( text ); + ber_memfree( matcheddn ); + ber_memvfree( refs ); + + return code; } /* diff --git a/clients/tools/ldapmodrdn.c b/clients/tools/ldapmodrdn.c index bf49074499..b29e17cd0c 100644 --- a/clients/tools/ldapmodrdn.c +++ b/clients/tools/ldapmodrdn.c @@ -490,7 +490,9 @@ static int domodrdn( char *newSuperior, int remove ) /* flag: remove old RDN */ { - int i; + int rc, code, id; + char *matcheddn=NULL, *text=NULL, **refs=NULL; + LDAPMessage *res; if ( verbose ) { printf( "Renaming \"%s\"\n", dn ); @@ -501,16 +503,51 @@ static int domodrdn( } } - if ( !not ) { - i = ldap_rename2_s( ld, dn, rdn, newSuperior, remove ); - if ( i != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_rename2_s" ); - } else if ( verbose ) { - printf( "modrdn complete\n" ); + if( not ) return LDAP_SUCCESS; + + rc = ldap_rename( ld, dn, rdn, newSuperior, remove, + NULL, NULL, &id ); + + if ( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_rename" ); + return rc; } - } else { - i = LDAP_SUCCESS; - } - return( i ); + rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ); + if ( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_result" ); + return rc; + } + + rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 ); + + if( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_parse_result" ); + return rc; + } + + if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) { + printf( "Result: %s (%d)\n", ldap_err2string( code ), code ); + + if( text && *text ) { + printf( "Additional info: %s\n", text ); + } + + if( matcheddn && *matcheddn ) { + printf( "Matched DN: %s\n", matcheddn ); + } + + if( refs ) { + int i; + for( i=0; refs[i]; i++ ) { + printf("Referral: %s\n", refs[i] ); + } + } + } + + ber_memfree( text ); + ber_memfree( matcheddn ); + ber_memvfree( refs ); + + return code; } diff --git a/clients/tools/ldappasswd.c b/clients/tools/ldappasswd.c index ad084ae8cf..96e898afd2 100644 --- a/clients/tools/ldappasswd.c +++ b/clients/tools/ldappasswd.c @@ -91,8 +91,11 @@ main( int argc, char *argv[] ) LDAP *ld; struct berval *bv = NULL; - char *retoid; - struct berval *retdata; + int id, code; + LDAPMessage *res; + char *matcheddn = NULL, *text = NULL, **refs = NULL; + char *retoid = NULL; + struct berval *retdata = NULL; if (argc == 1) usage (argv[0]); @@ -429,13 +432,43 @@ main( int argc, char *argv[] ) ber_free( ber, 1 ); } - rc = ldap_extended_operation_s( ld, + if ( noupdates ) { + rc = LDAP_SUCCESS; + goto skip; + } + + rc = ldap_extended_operation( ld, LDAP_EXOP_X_MODIFY_PASSWD, bv, - NULL, NULL, - &retoid, &retdata ); + NULL, NULL, &id ); ber_bvfree( bv ); + if( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_extended_operation" ); + ldap_unbind( ld ); + return EXIT_FAILURE; + } + + rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ); + if ( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_result" ); + return rc; + } + + rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 ); + + if( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_parse_result" ); + return rc; + } + + rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); + + if( rc != LDAP_SUCCESS ) { + ldap_perror( ld, "ldap_parse_result" ); + return rc; + } + if( retdata != NULL ) { ber_tag_t tag; char *s; @@ -460,15 +493,32 @@ main( int argc, char *argv[] ) ber_free( ber, 1 ); } - if ( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_extended_operation" ); - ldap_unbind( ld ); - return EXIT_FAILURE; + if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) { + printf( "Result: %s (%d)\n", ldap_err2string( code ), code ); + + if( text && *text ) { + printf( "Additional info: %s\n", text ); + } + + if( matcheddn && *matcheddn ) { + printf( "Matched DN: %s\n", matcheddn ); + } + + if( refs ) { + int i; + for( i=0; refs[i]; i++ ) { + printf("Referral: %s\n", refs[i] ); + } + } } - ldap_memfree( retoid ); + ber_memfree( text ); + ber_memfree( matcheddn ); + ber_memvfree( refs ); + ber_memfree( retoid ); ber_bvfree( retdata ); +skip: /* disconnect from server */ ldap_unbind (ld); -- 2.39.5