From: Howard Chu Date: Wed, 23 Jul 2025 22:18:28 +0000 (+0100) Subject: ITS#10371 clients: print actual error code when ldap_result fails X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=87b3f2074211c1472a1b67ccd56a57f75ad59838;p=thirdparty%2Fopenldap.git ITS#10371 clients: print actual error code when ldap_result fails Added a tool_perror2() to avoid some redundant calls. --- diff --git a/clients/tools/common.c b/clients/tools/common.c index 1e428cc372..5e1ffbbdd5 100644 --- a/clients/tools/common.c +++ b/clients/tools/common.c @@ -424,6 +424,19 @@ void tool_perror( } } +int tool_perror2( + LDAP *ld, + const char *func ) +{ + int err; + char *msg = NULL; + + ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void*)&err ); + ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg ); + tool_perror( func, err, NULL, NULL, msg, NULL ); + ldap_memfree( msg ); + return err; +} void tool_args( int argc, char **argv ) @@ -1552,11 +1565,7 @@ tool_bind( LDAP *ld ) ldap_msgfree( result ); if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) { - ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void*)&err ); - ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info ); - tool_perror( "ldap_sasl_interactive_bind", - err, NULL, NULL, info, NULL ); - ldap_memfree( info ); + err = tool_perror2( ld, "ldap_sasl_interactive_bind" ); tool_exit( ld, err ); } } while ( rc == LDAP_SASL_BIND_IN_PROGRESS ); @@ -1578,15 +1587,14 @@ tool_bind( LDAP *ld ) /* simple bind */ rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd, sctrlsp, NULL, &msgid ); - if ( msgid == -1 ) { - tool_perror( "ldap_sasl_bind(SIMPLE)", rc, - NULL, NULL, NULL, NULL ); + if ( rc == -1 ) { + rc = tool_perror2( ld, "ldap_sasl_bind(SIMPLE)" ); tool_exit( ld, rc ); } rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ); if ( rc == -1 ) { - tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL ); + tool_perror2( ld, "ldap_result" ); tool_exit( ld, LDAP_LOCAL_ERROR ); } diff --git a/clients/tools/common.h b/clients/tools/common.h index 2eb2ceff4c..1b97689b7b 100644 --- a/clients/tools/common.h +++ b/clients/tools/common.h @@ -130,6 +130,9 @@ void tool_perror LDAP_P(( const char *matched, const char *info, char **refs )); +int tool_perror2 LDAP_P(( + LDAP *ld, + const char *func )); void tool_print_ctrls LDAP_P(( LDAP *ld, LDAPControl **ctrls )); int tool_write_ldif LDAP_P(( int type, char *name, char *value, ber_len_t vallen )); int tool_is_oid LDAP_P(( const char *s )); diff --git a/clients/tools/ldapcompare.c b/clients/tools/ldapcompare.c index e571600f35..c4fb6d858a 100644 --- a/clients/tools/ldapcompare.c +++ b/clients/tools/ldapcompare.c @@ -302,7 +302,7 @@ static int docompare( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + rc = tool_perror2( ld, "ldap_result" ); return rc; } diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c index f31e5bb3f8..85d426cbaa 100644 --- a/clients/tools/ldapdelete.c +++ b/clients/tools/ldapdelete.c @@ -269,7 +269,7 @@ retry:; rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + rc = tool_perror2( ld, "ldap_result" ); return rc; } @@ -405,8 +405,7 @@ more:; char *dn = ldap_get_dn( ld, e ); if( dn == NULL ) { - ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc ); - tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL ); + rc = tool_perror2( ld, "ldap_prune" ); goto leave; } diff --git a/clients/tools/ldapexop.c b/clients/tools/ldapexop.c index d66f2cfb1f..a0f57df9b2 100644 --- a/clients/tools/ldapexop.c +++ b/clients/tools/ldapexop.c @@ -101,7 +101,7 @@ main( int argc, char *argv[] ) rc = ldap_whoami( ld, NULL, NULL, &id ); if ( rc != LDAP_SUCCESS ) { - tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); + tool_perror( "ldap_whoami", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } @@ -156,7 +156,7 @@ main( int argc, char *argv[] ) rc = ldap_refresh( ld, &dn, ttl, NULL, NULL, &id ); if ( rc != LDAP_SUCCESS ) { - tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); + tool_perror( "ldap_refresh", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } @@ -221,7 +221,7 @@ main( int argc, char *argv[] ) rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + tool_perror2( ld, "ldap_result" ); rc = EXIT_FAILURE; goto skip; } diff --git a/clients/tools/ldapmodify.c b/clients/tools/ldapmodify.c index f6e6b4d777..630a15a06e 100644 --- a/clients/tools/ldapmodify.c +++ b/clients/tools/ldapmodify.c @@ -697,8 +697,7 @@ static int process_response( } if ( rc == -1 ) { - ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc ); - tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + rc = tool_perror2( ld, "ldap_result" ); return rc; } diff --git a/clients/tools/ldapmodrdn.c b/clients/tools/ldapmodrdn.c index 1197d3813f..74179b47c2 100644 --- a/clients/tools/ldapmodrdn.c +++ b/clients/tools/ldapmodrdn.c @@ -278,7 +278,7 @@ static int domodrdn( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + rc = tool_perror2( ld, "ldap_result" ); return rc; } diff --git a/clients/tools/ldappasswd.c b/clients/tools/ldappasswd.c index cd0650e914..03c83c8262 100644 --- a/clients/tools/ldappasswd.c +++ b/clients/tools/ldappasswd.c @@ -348,7 +348,7 @@ main( int argc, char *argv[] ) rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + rc = tool_perror2( ld, "ldap_result" ); tool_exit( ld, rc ); } diff --git a/clients/tools/ldapvc.c b/clients/tools/ldapvc.c index 3608960e7d..dbb575146b 100644 --- a/clients/tools/ldapvc.c +++ b/clients/tools/ldapvc.c @@ -396,10 +396,7 @@ main( int argc, char *argv[] ) ldap_msgfree(res); if (ldap_result(ld, msgid, LDAP_MSG_ALL, NULL, &res) == -1 || !res) { - ldap_get_option(ld, LDAP_OPT_RESULT_CODE, (void*) &rc); - ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*) &text); - tool_perror( "ldap_verify_credentials_interactive", rc, NULL, NULL, text, NULL); - ldap_memfree(text); + rc = tool_perror2( ld, "ldap_verify_credentials_interactive" ); tool_exit(ld, rc); } } while (rc == LDAP_SASL_BIND_IN_PROGRESS); @@ -441,7 +438,7 @@ main( int argc, char *argv[] ) rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + rc = tool_perror2( ld, "ldap_result" ); tool_exit( ld, rc ); } diff --git a/clients/tools/ldapwhoami.c b/clients/tools/ldapwhoami.c index be1f81300a..1cd0a842bd 100644 --- a/clients/tools/ldapwhoami.c +++ b/clients/tools/ldapwhoami.c @@ -159,7 +159,7 @@ main( int argc, char *argv[] ) rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { - tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); + rc = tool_perror2( ld, "ldap_result" ); tool_exit( ld, rc ); }