]> git.sur5r.net Git - openldap/commitdiff
check reponse tags in ldapmodify; other functions rely on client library, which might...
authorPierangelo Masarati <ando@openldap.org>
Wed, 16 Nov 2005 21:50:51 +0000 (21:50 +0000)
committerPierangelo Masarati <ando@openldap.org>
Wed, 16 Nov 2005 21:50:51 +0000 (21:50 +0000)
clients/tools/common.c
clients/tools/common.h
clients/tools/ldapcompare.c
clients/tools/ldapdelete.c
clients/tools/ldapmodify.c
clients/tools/ldapmodrdn.c
clients/tools/ldappasswd.c
clients/tools/ldapsearch.c
clients/tools/ldapwhoami.c

index aa5f692f582acb98c60476497237c794eea878e0..a9d2bcc749f8538803bc7c61aeb59d251af109be 100644 (file)
 
 #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;
index a0ce71c56918fc711b920d1cfcb5aeea27dc2a2a..d97265e0f7a17c5733f79682c8f98a7de85eb2a8 100644 (file)
@@ -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
index 619b7e811a574d56c94631b75ef71d041c517377..5f541ba8e62074ce24f100b81f8975fd168eed99 100644 (file)
@@ -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;
                }
 
index a9ca65929253d586dbb0f1bfd26a0b9d554cf707..4b1aa7b17ac92c41b3d518d688d9e626ce1a1f4c 100644 (file)
@@ -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;
 
index d1941e3bb8937d32619d210b46b2e11442452d4c..c96355951accdd7b296315c6f0a03d8d194b1105 100644 (file)
@@ -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;
        }
 
index d921fe736d8c95b0daca7d70f17e3d997f24ed2b..e7c054b53d9c45109557c53d9e25704cdee28072 100644 (file)
@@ -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;
                }
 
index 0acd9cdc13122a2f5e47cb83563755a54e920132..91137b0c2b4b4509e6dba093be5f57d25b83fb7d 100644 (file)
@@ -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;
        }
index 220f613528977dfdf768ef9cbb0bcbc0648c57bf..e94a56b34f0b6747ea6a3d7dd5a03cd597cde4f6 100644 (file)
 
 #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 );
        }
 
index e97a8dca114616204b0faf9395c4136fd8e4c246..6cb7d6011f710875d1ebdd46e43860c401408b29 100644 (file)
@@ -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;
        }