]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Make ccache name work for klist/kdestroy -A 681/head
authorGreg Hudson <ghudson@mit.edu>
Tue, 25 Jul 2017 17:16:05 +0000 (13:16 -0400)
committerGreg Hudson <ghudson@mit.edu>
Thu, 27 Jul 2017 14:58:16 +0000 (10:58 -0400)
In klist and kdestroy, if a ccache name is specified, set it as the
default ccache name, simplifying the code and making klist -l, klist
-A, and kdestroy -A can work with a specified ccache name.  Reported
by Robbie Harwood.

ticket: 8602 (new)

src/clients/kdestroy/kdestroy.c
src/clients/klist/klist.c
src/tests/t_ccache.py

index 4d8c6e6f0ecf4fc4634adbdb75e539814a8877ce..0bf83586c14fcabc940717903fa87f5d8e928104 100644 (file)
@@ -137,6 +137,14 @@ main(int argc, char *argv[])
         exit(1);
     }
 
+    if (cache_name != NULL) {
+        code = krb5_cc_set_default_name(context, cache_name);
+        if (code) {
+            com_err(progname, code, _("while setting default cache name"));
+            exit(1);
+        }
+    }
+
     if (all) {
         code = krb5_cccol_cursor_new(context, &cursor);
         if (code) {
@@ -162,18 +170,10 @@ main(int argc, char *argv[])
         return 0;
     }
 
-    if (cache_name != NULL) {
-        code = krb5_cc_resolve(context, cache_name, &cache);
-        if (code != 0) {
-            com_err(progname, code, _("while resolving %s"), cache_name);
-            exit(1);
-        }
-    } else {
-        code = krb5_cc_default(context, &cache);
-        if (code) {
-            com_err(progname, code, _("while getting default ccache"));
-            exit(1);
-        }
+    code = krb5_cc_default(context, &cache);
+    if (code) {
+        com_err(progname, code, _("while resolving ccache"));
+        exit(1);
     }
 
     code = krb5_cc_destroy(context, cache);
index 4763bd3f70893406d8c1c2edae6dd946e9907de9..e9e76d8f3be6e2f0200ea6c013340f80fef5104b 100644 (file)
@@ -69,7 +69,7 @@ static void show_credential(krb5_creds *);
 static void list_all_ccaches(void);
 static int list_ccache(krb5_ccache);
 static void show_all_ccaches(void);
-static void do_ccache_name(char *);
+static void do_ccache(void);
 static int show_ccache(krb5_ccache);
 static int check_ccache(krb5_ccache);
 static void do_keytab(const char *);
@@ -242,12 +242,20 @@ main(int argc, char *argv[])
         exit(1);
     }
 
+    if (name != NULL && mode != KEYTAB) {
+        ret = krb5_cc_set_default_name(context, name);
+        if (ret) {
+            com_err(progname, ret, _("while setting default cache name"));
+            exit(1);
+        }
+    }
+
     if (list_all)
         list_all_ccaches();
     else if (show_all)
         show_all_ccaches();
     else if (mode == DEFAULT || mode == CCACHE)
-        do_ccache_name(name);
+        do_ccache();
     else
         do_keytab(name);
     return 0;
@@ -443,25 +451,16 @@ show_all_ccaches(void)
 }
 
 static void
-do_ccache_name(char *name)
+do_ccache()
 {
     krb5_error_code ret;
     krb5_ccache cache;
 
-    if (name == NULL) {
-        ret = krb5_cc_default(context, &cache);
-        if (ret) {
-            if (!status_only)
-                com_err(progname, ret, _("while getting default ccache"));
-            exit(1);
-        }
-    } else {
-        ret = krb5_cc_resolve(context, name, &cache);
-        if (ret) {
-            if (!status_only)
-                com_err(progname, ret, _("while resolving ccache %s"), name);
-            exit(1);
-        }
+    ret = krb5_cc_default(context, &cache);
+    if (ret) {
+        if (!status_only)
+            com_err(progname, ret, _("while resolving ccache"));
+        exit(1);
     }
     exit(status_only ? check_ccache(cache) : show_ccache(cache));
 }
index 2dcd19102c25949623aed9e23fe70c3b88667708..61d549b7b6a8233ba95c3636f1079919700b5603 100755 (executable)
@@ -57,6 +57,7 @@ realm.addprinc('bob', password('bob'))
 realm.addprinc('carol', password('carol'))
 
 def collection_test(realm, ccname):
+    oldccname = realm.env['KRB5CCNAME']
     realm.env['KRB5CCNAME'] = ccname
 
     realm.run([klist, '-A', '-s'], expected_code=1)
@@ -82,7 +83,7 @@ def collection_test(realm, ccname):
     if '---\nalice@' not in output or output.count('\n') != 4:
         fail('klist -l did not show expected output after re-kinit for alice.')
     realm.kinit('bob', password('bob'))
-    output = realm.run([klist, '-A'])
+    output = realm.run([klist, '-A', ccname])
     if 'bob@' not in output.splitlines()[1] or 'alice@' not in output or \
             'carol' not in output or output.count('Default principal:') != 3:
         fail('klist -A did not show expected output after kinit for bob.')
@@ -90,17 +91,22 @@ def collection_test(realm, ccname):
     output = realm.run([klist, '-l'])
     if '---\ncarol@' not in output or output.count('\n') != 5:
         fail('klist -l did not show expected output after kswitch to carol.')
-    realm.run([kdestroy])
-    output = realm.run([klist, '-l'])
+
+    # Switch to specifying the collection name on the command line
+    # (only works with klist/kdestroy for now, not kinit/kswitch).
+    realm.env['KRB5CCNAME'] = oldccname
+
+    realm.run([kdestroy, '-c', ccname])
+    output = realm.run([klist, '-l', ccname])
     if 'carol@' in output or 'bob@' not in output or output.count('\n') != 4:
         fail('kdestroy failed to remove only primary ccache.')
-    realm.run([klist, '-s'], expected_code=1)
-    realm.run([klist, '-A', '-s'])
-    realm.run([kdestroy, '-A'])
-    output = realm.run([klist, '-l'], expected_code=1)
+    realm.run([klist, '-s', ccname], expected_code=1)
+    realm.run([klist, '-A', '-s', ccname])
+    realm.run([kdestroy, '-A', '-c', ccname])
+    output = realm.run([klist, '-l', ccname], expected_code=1)
     if not output.endswith('---\n') or output.count('\n') != 2:
         fail('kdestroy -a failed to empty cache collection.')
-    realm.run([klist, '-A', '-s'], expected_code=1)
+    realm.run([klist, '-A', '-s', ccname], expected_code=1)
 
 
 collection_test(realm, 'DIR:' + os.path.join(realm.testdir, 'cc'))