From 81b28fe3299f1e3e123bfbdaff53c7c1e6c6bbe7 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Wed, 16 Nov 2005 21:50:51 +0000 Subject: [PATCH] check reponse tags in ldapmodify; other functions rely on client library, which might need further work. Also, remove or silence all deprecated API calls --- clients/tools/common.c | 52 ++++++++++++++++++---------- clients/tools/common.h | 8 ++--- clients/tools/ldapcompare.c | 2 +- clients/tools/ldapdelete.c | 20 +++++------ clients/tools/ldapmodify.c | 69 +++++++++++++++++++++++++++++-------- clients/tools/ldapmodrdn.c | 2 +- clients/tools/ldappasswd.c | 8 ++--- clients/tools/ldapsearch.c | 31 +++++++++++++---- clients/tools/ldapwhoami.c | 14 +++++--- 9 files changed, 141 insertions(+), 65 deletions(-) diff --git a/clients/tools/common.c b/clients/tools/common.c index aa5f692f58..a9d2bcc749 100644 --- a/clients/tools/common.c +++ b/clients/tools/common.c @@ -49,6 +49,18 @@ #include "common.h" +#if !LDAP_DEPRECATED +/* + * NOTE: we declare it here only because we want to keep supporting + * (how long?) ancient, deprecated LDAP_AUTH_KRB* auth methods + */ +LDAP_F( int ) +ldap_bind LDAP_P(( /* deprecated */ + LDAP *ld, + LDAP_CONST char *who, + LDAP_CONST char *passwd, + int authmethod )); +#endif int authmethod = -1; char *binddn = NULL; @@ -189,11 +201,11 @@ NULL } void tool_perror( - char *func, + const char *func, int err, - char *extra, - char *matched, - char *info, + const char *extra, + const char *matched, + const char *info, char **refs ) { fprintf( stderr, "%s: %s (%d)%s\n", @@ -886,12 +898,13 @@ tool_conn_setup( int not, void (*private_setup)( LDAP * ) ) exit( EXIT_FAILURE ); } - if ( use_tls && - ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) - { - ldap_perror( ld, "ldap_start_tls" ); - if ( use_tls > 1 ) { - exit( EXIT_FAILURE ); + if ( use_tls ) { + rc = ldap_start_tls_s( ld, NULL, NULL ); + if ( rc != LDAP_SUCCESS ) { + tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL ); + if ( use_tls > 1 ) { + exit( EXIT_FAILURE ); + } } } } @@ -946,7 +959,8 @@ tool_bind( LDAP *ld ) lutil_sasl_freedefs( defaults ); if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_sasl_interactive_bind_s" ); + tool_perror( "ldap_sasl_interactive_bind_s", + rc, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } #else @@ -955,7 +969,7 @@ tool_bind( LDAP *ld ) exit( EXIT_FAILURE ); #endif } else { - int msgid, err; + int msgid, err, rc; LDAPMessage *result; LDAPControl **ctrls; char msgbuf[256]; @@ -967,19 +981,19 @@ tool_bind( LDAP *ld ) msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod ); if ( msgid == -1 ) { - ldap_perror( ld, "ldap_bind" ); + tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) { - ldap_perror( ld, "ldap_result" ); + tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } - if ( ldap_parse_result( ld, result, &err, &matched, &info, &refs, - &ctrls, 1 ) != LDAP_SUCCESS ) - { - ldap_perror( ld, "ldap_bind parse result" ); + rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs, + &ctrls, 1 ); + if ( rc != LDAP_SUCCESS ) { + tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } @@ -1263,7 +1277,7 @@ tool_check_abandon( LDAP *ld, int msgid ) return -1; case LDAP_REQ_ABANDON: - rc = ldap_abandon( ld, msgid ); + rc = ldap_abandon_ext( ld, msgid, NULL, NULL ); fprintf( stderr, "got interrupt, abandon got %d: %s\n", rc, ldap_err2string( rc ) ); return -1; diff --git a/clients/tools/common.h b/clients/tools/common.h index a0ce71c569..d97265e0f7 100644 --- a/clients/tools/common.h +++ b/clients/tools/common.h @@ -83,11 +83,11 @@ void tool_destroy LDAP_P(( void )); void tool_server_controls LDAP_P(( LDAP *, LDAPControl *, int )); int tool_check_abandon LDAP_P(( LDAP *ld, int msgid )); void tool_perror LDAP_P(( - char *func, + const char *func, int err, - char *extra, - char *matched, - char *info, + const char *extra, + const char *matched, + const char *info, char **refs )); LDAP_END_DECL diff --git a/clients/tools/ldapcompare.c b/clients/tools/ldapcompare.c index 619b7e811a..5f541ba8e6 100644 --- a/clients/tools/ldapcompare.c +++ b/clients/tools/ldapcompare.c @@ -261,7 +261,7 @@ static int docompare( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - ldap_perror( ld, "ldapcompare: ldap_result" ); + tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); return rc; } diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c index a9ca659292..4b1aa7b17a 100644 --- a/clients/tools/ldapdelete.c +++ b/clients/tools/ldapdelete.c @@ -246,7 +246,7 @@ static int dodelete( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - ldap_perror( ld, "ldapdelete: ldap_result" ); + tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); return rc; } @@ -316,7 +316,7 @@ static int deletechildren( rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1, NULL, NULL, NULL, -1, &res ); if ( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_search" ); + tool_perror( "ldap_search", rc, NULL, NULL, NULL, NULL ); return( rc ); } @@ -331,15 +331,15 @@ static int deletechildren( char *dn = ldap_get_dn( ld, e ); if( dn == NULL ) { - ldap_perror( ld, "ldap_prune" ); ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc ); + tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL ); ber_memfree( dn ); return rc; } rc = deletechildren( ld, dn ); if ( rc == -1 ) { - ldap_perror( ld, "ldap_prune" ); + tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL ); ber_memfree( dn ); return rc; } @@ -348,9 +348,9 @@ static int deletechildren( printf( _("\tremoving %s\n"), dn ); } - rc = ldap_delete_s( ld, dn ); + rc = ldap_delete_ext_s( ld, dn, NULL, NULL ); if ( rc == -1 ) { - ldap_perror( ld, "ldap_delete" ); + tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL ); ber_memfree( dn ); return rc; @@ -390,7 +390,7 @@ static int deletechildren( rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1, ctrls, NULL, NULL, -1, &res_se ); if ( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_search" ); + tool_perror( "ldap_search", rc, NULL, NULL, NULL, NULL ); return( rc ); } ber_free( ber, 1 ); @@ -406,8 +406,8 @@ static int deletechildren( char *dn = ldap_get_dn( ld, e ); if( dn == NULL ) { - ldap_perror( ld, "ldap_prune" ); ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc ); + tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL ); ber_memfree( dn ); return rc; } @@ -416,9 +416,9 @@ static int deletechildren( printf( _("\tremoving %s\n"), dn ); } - rc = ldap_delete_s( ld, dn ); + rc = ldap_delete_ext_s( ld, dn, NULL, NULL ); if ( rc == -1 ) { - ldap_perror( ld, "ldap_delete" ); + tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL ); ber_memfree( dn ); return rc; diff --git a/clients/tools/ldapmodify.c b/clients/tools/ldapmodify.c index d1941e3bb8..c96355951a 100644 --- a/clients/tools/ldapmodify.c +++ b/clients/tools/ldapmodify.c @@ -118,7 +118,7 @@ static int dorename LDAP_P(( static int process_response( LDAP *ld, int msgid, - const char *opstr, + int res, const char *dn ); static char *read_one_record LDAP_P(( FILE *fp )); @@ -303,7 +303,7 @@ main( int argc, char **argv ) /* create transaction */ rc = ldap_txn_create_s( ld, &txnCookiep, NULL, NULL ); if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_txn_create_s" ); + tool_perror( "ldap_txn_create_s", rc, NULL, NULL, NULL, NULL ); if( txn > 2 ) return EXIT_FAILURE; txn = 0; } @@ -398,7 +398,7 @@ main( int argc, char **argv ) /* create transaction */ rc = ldap_txn_end_s( ld, &txnCookie, !txnabort, NULL, NULL ); if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_txn_create_s" ); + tool_perror( "ldap_txn_create_s", rc, NULL, NULL, NULL, NULL ); if( txn > 2 ) return EXIT_FAILURE; txn = 0; } @@ -1024,14 +1024,14 @@ domodify( if ( rc != LDAP_SUCCESS ) { /* print error message about failed update including DN */ fprintf( stderr, _("%s: update failed: %s\n"), prog, dn ); - ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" ); + tool_perror( newentry ? "ldap_add" : "ldap_modify", rc, NULL, NULL, NULL, NULL ); goto done; } else if ( verbose ) { printf( _("modify complete\n") ); } rc = process_response( ld, msgid, - newentry ? "ldap_add" : "ldap_modify", dn ); + newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn ); } else { rc = LDAP_SUCCESS; @@ -1056,13 +1056,13 @@ dodelete( rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid ); if ( rc != LDAP_SUCCESS ) { fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn ); - ldap_perror( ld, "ldap_delete" ); + tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL ); goto done; } else if ( verbose ) { printf( _("delete complete") ); } - rc = process_response( ld, msgid, "ldap_delete", dn ); + rc = process_response( ld, msgid, LDAP_RES_DELETE, dn ); } else { rc = LDAP_SUCCESS; @@ -1095,13 +1095,13 @@ dorename( pctrls, NULL, &msgid ); if ( rc != LDAP_SUCCESS ) { fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn ); - ldap_perror( ld, "ldap_modrdn" ); + tool_perror( "ldap_modrdn", rc, NULL, NULL, NULL, NULL ); goto done; } else { printf( _("modrdn completed\n") ); } - rc = process_response( ld, msgid, "ldap_rename", dn ); + rc = process_response( ld, msgid, LDAP_RES_RENAME, dn ); } else { rc = LDAP_SUCCESS; @@ -1112,14 +1112,33 @@ done: return( rc ); } +static const char * +res2str( int res ) { + switch ( res ) { + case LDAP_RES_ADD: + return "ldap_add"; + case LDAP_RES_DELETE: + return "ldap_delete"; + case LDAP_RES_MODIFY: + return "ldap_modify"; + case LDAP_RES_MODRDN: + return "ldap_modrdn"; + default: + assert( 0 ); + } + + return "ldap_unknown"; +} + static int process_response( LDAP *ld, int msgid, - const char *opstr, + int op, const char *dn ) { LDAPMessage *res; - int rc = LDAP_OTHER; + int rc = LDAP_OTHER, + msgtype; struct timeval tv = { 0, 0 }; for ( ; ; ) { @@ -1147,9 +1166,31 @@ static int process_response( } } - if ( ldap_msgtype( res ) != LDAP_RES_INTERMEDIATE ) { - rc = ldap_result2error( ld, res, 1 ); - if( rc != LDAP_SUCCESS ) ldap_perror( ld, opstr ); + msgtype = ldap_msgtype( res ); + if ( msgtype != LDAP_RES_INTERMEDIATE ) { + int err; + char *text = NULL, *matched = NULL, **refs = NULL; + + rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, NULL, 1 ); + if ( rc == LDAP_SUCCESS ) { + rc = err; + } + if ( rc != LDAP_SUCCESS ) { + tool_perror( res2str( op ), rc, NULL, matched, text, refs ); + } else if ( msgtype != op ) { + fprintf( stderr, "%s: msgtype: expected %d got %d\n", + res2str( op ), op, msgtype ); + rc = LDAP_OTHER; + } + if ( text ) { + ldap_memfree( text ); + } + if ( matched ) { + ldap_memfree( matched ); + } + if ( text ) { + ber_memvfree( (void **)refs ); + } return rc; } diff --git a/clients/tools/ldapmodrdn.c b/clients/tools/ldapmodrdn.c index d921fe736d..e7c054b53d 100644 --- a/clients/tools/ldapmodrdn.c +++ b/clients/tools/ldapmodrdn.c @@ -276,7 +276,7 @@ static int domodrdn( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - ldap_perror( ld, "ldapmodrdn: ldap_result" ); + tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); return rc; } diff --git a/clients/tools/ldappasswd.c b/clients/tools/ldappasswd.c index 0acd9cdc13..91137b0c2b 100644 --- a/clients/tools/ldappasswd.c +++ b/clients/tools/ldappasswd.c @@ -317,7 +317,7 @@ main( int argc, char *argv[] ) ber_free( ber, 1 ); if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_extended_operation" ); + tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto done; } @@ -334,7 +334,7 @@ main( int argc, char *argv[] ) rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - ldap_perror( ld, "ldappasswd: ldap_result" ); + tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); return rc; } @@ -346,14 +346,14 @@ main( int argc, char *argv[] ) rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 ); if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_parse_result" ); + tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto done; } rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_parse_extended_result" ); + tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto done; } diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c index 220f613528..e94a56b34f 100644 --- a/clients/tools/ldapsearch.c +++ b/clients/tools/ldapsearch.c @@ -71,6 +71,23 @@ #include "common.h" +#if !LDAP_DEPRECATED +/* + * NOTE: we use this deprecated function only because + * we want ldapsearch to provide some client-side sorting + * capability. + */ +/* from ldap.h */ +typedef int (LDAP_SORT_AD_CMP_PROC) LDAP_P(( /* deprecated */ + LDAP_CONST char *left, + LDAP_CONST char *right )); + +LDAP_F( int ) /* deprecated */ +ldap_sort_entries LDAP_P(( LDAP *ld, + LDAPMessage **chain, + LDAP_CONST char *attr, + LDAP_SORT_AD_CMP_PROC *cmp )); +#endif static int scope = LDAP_SCOPE_SUBTREE; static int deref = -1; @@ -1234,7 +1251,7 @@ static int dosearch( done: if ( rc == -1 ) { - ldap_perror( ld, "ldap_result" ); + tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); return( rc ); } @@ -1295,7 +1312,7 @@ print_entry( rc = ldap_get_entry_controls( ld, entry, &ctrls ); if( rc != LDAP_SUCCESS ) { fprintf(stderr, _("print_entry: %d\n"), rc ); - ldap_perror( ld, "ldap_get_entry_controls" ); + tool_perror( "ldap_get_entry_controls", rc, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } @@ -1393,7 +1410,7 @@ static void print_reference( rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 ); if( rc != LDAP_SUCCESS ) { - ldap_perror(ld, "ldap_parse_reference"); + tool_perror( "ldap_parse_reference", rc, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } @@ -1428,7 +1445,7 @@ static void print_extended( &retoid, &retdata, 0 ); if( rc != LDAP_SUCCESS ) { - ldap_perror(ld, "ldap_parse_extended_result"); + tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } @@ -1466,7 +1483,7 @@ static void print_partial( &retoid, &retdata, &ctrls, 0 ); if( rc != LDAP_SUCCESS ) { - ldap_perror(ld, "ldap_parse_intermediate"); + tool_perror( "ldap_parse_intermediate", rc, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } @@ -1516,7 +1533,7 @@ static int print_result( &err, &matcheddn, &text, &refs, &ctrls, 0 ); if( rc != LDAP_SUCCESS ) { - ldap_perror(ld, "ldap_parse_result"); + tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } @@ -1669,7 +1686,7 @@ parse_page_control( &err, NULL, NULL, NULL, &ctrl, 0 ); if( rc != LDAP_SUCCESS ) { - ldap_perror(ld, "ldap_parse_result"); + tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL ); exit( EXIT_FAILURE ); } diff --git a/clients/tools/ldapwhoami.c b/clients/tools/ldapwhoami.c index e97a8dca11..6cb7d6011f 100644 --- a/clients/tools/ldapwhoami.c +++ b/clients/tools/ldapwhoami.c @@ -161,7 +161,7 @@ main( int argc, char *argv[] ) rc = ldap_whoami( ld, NULL, NULL, &id ); if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_extended_operation" ); + tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } @@ -178,7 +178,7 @@ main( int argc, char *argv[] ) rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - ldap_perror( ld, "ldapwhoami: ldap_result" ); + tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); return rc; } @@ -190,8 +190,12 @@ main( int argc, char *argv[] ) rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 ); - if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_parse_result" ); + if ( rc == LDAP_SUCCESS ) { + rc = code; + } + + if ( rc != LDAP_SUCCESS ) { + tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs ); rc = EXIT_FAILURE; goto skip; } @@ -199,7 +203,7 @@ main( int argc, char *argv[] ) rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); if( rc != LDAP_SUCCESS ) { - ldap_perror( ld, "ldap_parse_result" ); + tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } -- 2.39.5