Path: blob/main/crypto/krb5/src/tests/gssapi/t_client_keytab.py
34889 views
from k5test import *12# Set up a basic realm and a client keytab containing two user principals.3# Point HOME at realm.testdir for tests using .k5identity.4realm = K5Realm(get_creds=False)5bob = 'bob@' + realm.realm6phost = 'p:' + realm.host_princ7puser = 'p:' + realm.user_princ8pbob = 'p:' + bob9gssserver = 'h:host@' + hostname10realm.env['HOME'] = realm.testdir11realm.addprinc(bob, password('bob'))12realm.extract_keytab(realm.user_princ, realm.client_keytab)13realm.extract_keytab(bob, realm.client_keytab)1415# Test 1: no name/cache specified, pick first principal from client keytab16realm.run(['./t_ccselect', phost], expected_msg=realm.user_princ)17realm.run([kdestroy])1819# Test 2: no name/cache specified, pick principal from k5identity20k5idname = os.path.join(realm.testdir, '.k5identity')21k5id = open(k5idname, 'w')22k5id.write('%s service=host host=%s\n' % (bob, hostname))23k5id.close()24realm.run(['./t_ccselect', gssserver], expected_msg=bob)25os.remove(k5idname)26realm.run([kdestroy])2728# Test 3: no name/cache specified, default ccache has name but no creds29realm.run(['./ccinit', realm.ccache, bob])30realm.run(['./t_ccselect', phost], expected_msg=bob)31# Leave tickets for next test.3233# Test 4: name specified, non-collectable default cache doesn't match34msg = 'Principal in credential cache does not match desired name'35realm.run(['./t_ccselect', phost, puser], expected_code=1, expected_msg=msg)36realm.run([kdestroy])3738# Test 5: name specified, nonexistent default cache39realm.run(['./t_ccselect', phost, pbob], expected_msg=bob)40# Leave tickets for next test.4142# Test 6: name specified, matches default cache, time to refresh43realm.run(['./ccrefresh', realm.ccache, '1'])44realm.run(['./t_ccselect', phost, pbob], expected_msg=bob)45out = realm.run(['./ccrefresh', realm.ccache])46if int(out) < 1000:47fail('Credentials apparently not refreshed')48realm.run([kdestroy])4950# Test 7: empty ccache specified, pick first principal from client keytab51realm.run(['./t_imp_cred', phost])52realm.klist(realm.user_princ)53realm.run([kdestroy])5455# Test 8: ccache specified with name but no creds; name not in client keytab56realm.run(['./ccinit', realm.ccache, realm.host_princ])57realm.run(['./t_imp_cred', phost], expected_code=1,58expected_msg='Credential cache is empty')59realm.run([kdestroy])6061# Test 9: ccache specified with name but no creds; name in client keytab62realm.run(['./ccinit', realm.ccache, bob])63realm.run(['./t_imp_cred', phost])64realm.klist(bob)65# Leave tickets for next test.6667# Test 10: ccache specified with creds, time to refresh68realm.run(['./ccrefresh', realm.ccache, '1'])69realm.run(['./t_imp_cred', phost])70realm.klist(bob)71out = realm.run(['./ccrefresh', realm.ccache])72if int(out) < 1000:73fail('Credentials apparently not refreshed')74realm.run([kdestroy])7576# Test 11: gss_import_cred_from with client_keytab value77store_keytab = os.path.join(realm.testdir, 'store_keytab')78os.rename(realm.client_keytab, store_keytab)79realm.run(['./t_credstore', '-i', 'p:' + realm.user_princ, 'client_keytab',80store_keytab])81realm.klist(realm.user_princ)82os.rename(store_keytab, realm.client_keytab)8384# Use a cache collection for the remaining tests.85ccdir = os.path.join(realm.testdir, 'cc')86ccname = 'DIR:' + ccdir87os.mkdir(ccdir)88realm.env['KRB5CCNAME'] = ccname8990# Test 12: name specified, matching cache in collection with no creds91bobcache = os.path.join(ccdir, 'tktbob')92realm.run(['./ccinit', bobcache, bob])93realm.run(['./t_ccselect', phost, pbob], expected_msg=bob)94# Leave tickets for next test.9596# Test 13: name specified, matching cache in collection, time to refresh97realm.run(['./ccrefresh', bobcache, '1'])98realm.run(['./t_ccselect', phost, pbob], expected_msg=bob)99out = realm.run(['./ccrefresh', bobcache])100if int(out) < 1000:101fail('Credentials apparently not refreshed')102realm.run([kdestroy, '-A'])103104# Test 14: name specified, collection has default for different principal105realm.kinit(realm.user_princ, password('user'))106realm.run(['./t_ccselect', phost, pbob], expected_msg=bob)107msg = 'Default principal: %s\n' % realm.user_princ108realm.run([klist], expected_msg=msg)109realm.run([kdestroy, '-A'])110111# Test 15: name specified, collection has no default cache112realm.run(['./t_ccselect', phost, pbob], expected_msg=bob)113# Make sure the tickets we acquired didn't become the default114realm.run([klist], expected_code=1, expected_msg='No credentials cache found')115realm.run([kdestroy, '-A'])116117# Test 16: default client keytab cannot be resolved, but valid118# credentials exist in ccache.119conf = {'libdefaults': {'default_client_keytab_name': '%{'}}120bad_cktname = realm.special_env('bad_cktname', False, krb5_conf=conf)121del bad_cktname['KRB5_CLIENT_KTNAME']122realm.kinit(realm.user_princ, password('user'))123realm.run(['./t_ccselect', phost], env=bad_cktname,124expected_msg=realm.user_princ)125126mark('refresh of manually acquired creds')127128# Test 17: no name/ccache specified, manually acquired creds which129# will expire soon. Verify that creds are refreshed using the current130# client name, with refresh_time set in the refreshed ccache.131realm.kinit('bob', password('bob'), ['-l', '15s'])132realm.run(['./t_ccselect', phost], expected_msg='bob')133realm.run([klist, '-C'], expected_msg='refresh_time = ')134135# Test 18: no name/ccache specified, manually acquired creds with a136# client principal not present in the client keytab. A refresh is137# attempted but fails, and an expired ticket error results.138realm.kinit(realm.admin_princ, password('admin'), ['-l', '-10s'])139msgs = ('Getting initial credentials for user/[email protected]',140'/Matching credential not found')141realm.run(['./t_ccselect', phost], expected_code=1,142expected_msg='Ticket expired', expected_trace=msgs)143realm.run([kdestroy, '-A'])144145# Test 19: host-based initiator name146mark('host-based initiator name')147hsvc = 'h:svc@' + hostname148svcprinc = 'svc/%s@%s' % (hostname, realm.realm)149realm.addprinc(svcprinc)150realm.extract_keytab(svcprinc, realm.client_keytab)151# On the first run we match against the keytab while getting tickets,152# substituting the default realm.153msgs = ('/Can\'t find client principal svc/%s@ in' % hostname,154'Getting initial credentials for svc/%s@' % hostname,155'Found entries for %s in keytab' % svcprinc,156'Retrieving %s from FILE:%s' % (svcprinc, realm.client_keytab),157'Storing %s -> %s in' % (svcprinc, realm.krbtgt_princ),158'Retrieving %s -> %s from' % (svcprinc, realm.krbtgt_princ),159'authenticator for %s -> %s' % (svcprinc, realm.host_princ))160realm.run(['./t_ccselect', phost, hsvc], expected_trace=msgs)161# On the second run we match against the collection.162msgs = ('Matching svc/%s@ in collection with result: 0' % hostname,163'Getting credentials %s -> %s' % (svcprinc, realm.host_princ),164'authenticator for %s -> %s' % (svcprinc, realm.host_princ))165realm.run(['./t_ccselect', phost, hsvc], expected_trace=msgs)166realm.run([kdestroy, '-A'])167168# Test 20: host-based initiator name with fallback169mark('host-based fallback initiator name')170canonname = canonicalize_hostname(hostname)171if canonname != hostname:172hfsvc = 'h:fsvc@' + hostname173canonprinc = 'fsvc/%s@%s' % (canonname, realm.realm)174realm.addprinc(canonprinc)175realm.extract_keytab(canonprinc, realm.client_keytab)176msgs = ('/Can\'t find client principal fsvc/%s@ in' % hostname,177'Found entries for %s in keytab' % canonprinc,178'authenticator for %s -> %s' % (canonprinc, realm.host_princ))179realm.run(['./t_ccselect', phost, hfsvc], expected_trace=msgs)180msgs = ('Matching fsvc/%s@ in collection with result: 0' % hostname,181'Getting credentials %s -> %s' % (canonprinc, realm.host_princ))182realm.run(['./t_ccselect', phost, hfsvc], expected_trace=msgs)183realm.run([kdestroy, '-A'])184else:185skipped('GSS initiator name fallback test',186'%s does not canonicalize to a different name' % hostname)187188success('Client keytab tests')189190191