ITS#10371 clients: print actual error code when ldap_result fails

Added a tool_perror2() to avoid some redundant calls.
This commit is contained in:
Howard Chu 2025-07-23 23:18:28 +01:00 committed by Quanah Gibson-Mount
parent 1ba3232418
commit 4e1d9cb687
10 changed files with 32 additions and 26 deletions

View file

@ -410,6 +410,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 )
@ -1529,11 +1542,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 );
@ -1555,15 +1564,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 );
}

View file

@ -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 ));

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -677,8 +677,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;
}

View file

@ -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;
}

View file

@ -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 );
}

View file

@ -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 );
}

View file

@ -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 );
}