287c72
From cb8c8af56d306267d6964da217c65e129fe83c82 Mon Sep 17 00:00:00 2001
31ace6
From: Robbie Harwood <rharwood@redhat.com>
31ace6
Date: Wed, 26 Feb 2020 18:27:17 -0500
31ace6
Subject: [PATCH] Refresh manually acquired creds from client keytab
31ace6
31ace6
If a client keytab is present but credentials are acquired manually,
31ace6
the credentials would not be refreshed because no refresh_time config
31ace6
var is set in the cache.  Change kg_cred_time_to_refresh() to attempt
31ace6
a refresh from the client keytab on any credentials which will expire
31ace6
in the next 30 seconds.
31ace6
31ace6
[ghudson@mit.edu: adjused code and added test case]
31ace6
31ace6
ticket: 7976
31ace6
(cherry picked from commit 729896467e3c77904666019d6cbbda583ae49b95)
31ace6
(cherry picked from commit 685aada9eae420cb5156ca7b71c2c7614c0b6e2c)
31ace6
---
31ace6
 src/lib/gssapi/krb5/acquire_cred.c  | 14 +++++++++++---
31ace6
 src/tests/gssapi/t_client_keytab.py | 18 ++++++++++++++++++
31ace6
 2 files changed, 29 insertions(+), 3 deletions(-)
31ace6
31ace6
diff --git a/src/lib/gssapi/krb5/acquire_cred.c b/src/lib/gssapi/krb5/acquire_cred.c
31ace6
index acc1868f8..4062f4741 100644
31ace6
--- a/src/lib/gssapi/krb5/acquire_cred.c
31ace6
+++ b/src/lib/gssapi/krb5/acquire_cred.c
31ace6
@@ -557,15 +557,23 @@ set_refresh_time(krb5_context context, krb5_ccache ccache,
31ace6
 krb5_boolean
31ace6
 kg_cred_time_to_refresh(krb5_context context, krb5_gss_cred_id_rec *cred)
31ace6
 {
31ace6
-    krb5_timestamp now;
31ace6
+    krb5_timestamp now, soon;
31ace6
 
31ace6
     if (krb5_timeofday(context, &now))
31ace6
         return FALSE;
31ace6
+    soon = ts_incr(now, 30);
31ace6
     if (cred->refresh_time != 0 && !ts_after(cred->refresh_time, now)) {
31ace6
-        set_refresh_time(context, cred->ccache,
31ace6
-                         ts_incr(cred->refresh_time, 30));
31ace6
+        set_refresh_time(context, cred->ccache, soon);
31ace6
         return TRUE;
31ace6
     }
31ace6
+
31ace6
+    /* If the creds will expire soon, try to refresh even if they weren't
31ace6
+     * acquired with a client keytab. */
31ace6
+    if (ts_after(soon, cred->expire)) {
31ace6
+        set_refresh_time(context, cred->ccache, soon);
31ace6
+        return TRUE;
31ace6
+    }
31ace6
+
31ace6
     return FALSE;
31ace6
 }
31ace6
 
31ace6
diff --git a/src/tests/gssapi/t_client_keytab.py b/src/tests/gssapi/t_client_keytab.py
31ace6
index e474a27c7..7847b3ecd 100755
31ace6
--- a/src/tests/gssapi/t_client_keytab.py
31ace6
+++ b/src/tests/gssapi/t_client_keytab.py
31ace6
@@ -124,4 +124,22 @@ realm.kinit(realm.user_princ, password('user'))
31ace6
 realm.run(['./t_ccselect', phost], env=bad_cktname,
31ace6
           expected_msg=realm.user_princ)
31ace6
 
31ace6
+mark('refresh of manually acquired creds')
31ace6
+
31ace6
+# Test 17: no name/ccache specified, manually acquired creds which
31ace6
+# will expire soon.  Verify that creds are refreshed using the current
31ace6
+# client name, with refresh_time set in the refreshed ccache.
31ace6
+realm.kinit('bob', password('bob'), ['-l', '15s'])
31ace6
+realm.run(['./t_ccselect', phost], expected_msg='bob')
31ace6
+realm.run([klist, '-C'], expected_msg='refresh_time = ')
31ace6
+
31ace6
+# Test 18: no name/ccache specified, manually acquired creds with a
31ace6
+# client principal not present in the client keytab.  A refresh is
31ace6
+# attempted but fails, and an expired ticket error results.
31ace6
+realm.kinit(realm.admin_princ, password('admin'), ['-l', '-1s'])
31ace6
+msgs = ('Getting initial credentials for user/admin@KRBTEST.COM',
31ace6
+        '/Matching credential not found')
31ace6
+realm.run(['./t_ccselect', phost], expected_code=1,
31ace6
+          expected_msg='Ticket expired', expected_trace=msgs)
31ace6
+
31ace6
 success('Client keytab tests')