]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
ITS#10371 clients: print actual error code when ldap_result fails master 782/head
authorHoward Chu <hyc@openldap.org>
Wed, 23 Jul 2025 22:18:28 +0000 (23:18 +0100)
committerQuanah Gibson-Mount <quanah@openldap.org>
Mon, 4 Aug 2025 19:10:38 +0000 (19:10 +0000)
Added a tool_perror2() to avoid some redundant calls.

clients/tools/common.c
clients/tools/common.h
clients/tools/ldapcompare.c
clients/tools/ldapdelete.c
clients/tools/ldapexop.c
clients/tools/ldapmodify.c
clients/tools/ldapmodrdn.c
clients/tools/ldappasswd.c
clients/tools/ldapvc.c
clients/tools/ldapwhoami.c

index 1e428cc372285e3dfc8d212710a0c36a125d5041..5e1ffbbdd5e88b953ccdb7f36f66d5935b65fb81 100644 (file)
@@ -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 );
                }
 
index 2eb2ceff4c6ef0a283a84b3ba43faf573b2af615..1b97689b7bde831dd02cca41cfdb148d7332dc64 100644 (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 ));
index e571600f350ad933b0b08ccac1adb1da37821511..c4fb6d858ab610b2eded1d9a59fa5d3ba27d920f 100644 (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;
                }
 
index f31e5bb3f8b13c82e597bada1a5c20c950287e2a..85d426cbaa1234499dd8a2500de8c885e5ca9428 100644 (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;
                        }
 
index d66f2cfb1fcd61d96e64a2e63b54722ae69f033c..a0f57df9b27b6ce8202322f8537db03ddc9e08d1 100644 (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;
                }
index f6e6b4d777651ee6295ac4c1b5a4ff8f3f0caa1d..630a15a06e9eccac259f72c182851e8517dc5640 100644 (file)
@@ -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;
                }
 
index 1197d3813faf38d7ed643baa08e7b9ec75e808ec..74179b47c2cab65f26864dbaaccd891ab9a22fc2 100644 (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;
                }
 
index cd0650e91499e27d020b883207c49487a4c725a3..03c83c82621ebb785798d7853b95ce33e7f148e6 100644 (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 );
                }
 
index 3608960e7d26802ec4c1401266869857caf33a52..dbb575146b7ea15053324beb7a9e2cddbf03d926 100644 (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 );
                    }
 
index be1f81300a50abd49f72cd22c9007222a8fa64a8..1cd0a842bdd4e79d44b5970e59327ac97d9eb9be 100644 (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 );
                }