Path: blob/main/crypto/krb5/src/tests/gssapi/t_ccselect.py
34889 views
# Copyright (C) 2011 by the Massachusetts Institute of Technology.1# All rights reserved.23# Export of this software from the United States of America may4# require a specific license from the United States Government.5# It is the responsibility of any person or organization contemplating6# export to obtain such a license before exporting.7#8# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and9# distribute this software and its documentation for any purpose and10# without fee is hereby granted, provided that the above copyright11# notice appear in all copies and that both that copyright notice and12# this permission notice appear in supporting documentation, and that13# the name of M.I.T. not be used in advertising or publicity pertaining14# to distribution of the software without specific, written prior15# permission. Furthermore if you modify this software you must label16# your software as modified software and not distribute it in such a17# fashion that it might be confused with the original M.I.T. software.18# M.I.T. makes no representations about the suitability of19# this software for any purpose. It is provided "as is" without express20# or implied warranty.2122from k5test import *2324# Create two independent realms (no cross-realm TGTs). For the25# fallback realm tests we need to control the precise server hostname,26# so turn off DNS canonicalization and shortname qualification.27conf = {'libdefaults': {'dns_canonicalize_hostname': 'false',28'qualify_shortname': ''}}29r1 = K5Realm(create_user=False, krb5_conf=conf)30r2 = K5Realm(create_user=False, krb5_conf=conf, realm='KRBTEST2.COM',31portbase=62000, testdir=os.path.join(r1.testdir, 'r2'))3233host1 = 'p:' + r1.host_princ34host2 = 'p:' + r2.host_princ35foo = 'foo.krbtest.com'36foo2 = 'foo.krbtest2.com'37foobar = "foo.bar.krbtest.com"3839# These strings specify the target as a GSS name. The resulting40# principal will have the host-based type, with the referral realm41# (since k5test realms have no domain-realm mapping by default).42# krb5_cc_select() will use the fallback realm, which is either the43# uppercased parent domain, or the default realm if the hostname is a44# single component.45gssserver = 'h:host@' + foo46gssserver2 = 'h:host@' + foo247gssserver_bar = 'h:host@' + foobar48gsslocal = 'h:host@localhost'4950# refserver specifies the target as a principal in the referral realm.51# The principal won't be treated as a host principal by the52# .k5identity rules since it has unknown type.53refserver = 'p:host/' + hostname + '@'5455# Verify that we can't get initiator creds with no credentials in the56# collection.57r1.run(['./t_ccselect', host1, '-'], expected_code=1,58expected_msg='No Kerberos credentials available')5960# Make a directory collection and use it for client commands in both realms.61ccdir = os.path.join(r1.testdir, 'cc')62ccname = 'DIR:' + ccdir63r1.env['KRB5CCNAME'] = ccname64r2.env['KRB5CCNAME'] = ccname6566# Use .k5identity from testdir and not from the tester's homedir.67r1.env['HOME'] = r1.testdir68r2.env['HOME'] = r1.testdir6970# Create two users in r1 and one in r2.71alice='[email protected]'72bob='[email protected]'73zaphod='[email protected]'74r1.addprinc(alice, password('alice'))75r1.addprinc(bob, password('bob'))76r2.addprinc(zaphod, password('zaphod'))7778# Create host principals and keytabs for fallback realm tests.79if hostname != 'localhost':80r1.addprinc('host/localhost')81r2.addprinc('host/localhost')82r1.addprinc('host/' + foo)83r2.addprinc('host/' + foo2)84r1.addprinc('host/' + foobar)85r1.extract_keytab('host/localhost', r1.keytab)86r2.extract_keytab('host/localhost', r2.keytab)87r1.extract_keytab('host/' + foo, r1.keytab)88r2.extract_keytab('host/' + foo2, r2.keytab)89r1.extract_keytab('host/' + foobar, r1.keytab)9091# Get tickets for one user in each realm (zaphod will be primary).92r1.kinit(alice, password('alice'))93r2.kinit(zaphod, password('zaphod'))9495# Check that we can find a cache for a specified client principal.96output = r1.run(['./t_ccselect', host1, 'p:' + alice])97if output != (alice + '\n'):98fail('alice not chosen when specified')99output = r2.run(['./t_ccselect', host2, 'p:' + zaphod])100if output != (zaphod + '\n'):101fail('zaphod not chosen when specified')102103# Check that we can guess a cache based on the service realm.104output = r1.run(['./t_ccselect', host1])105if output != (alice + '\n'):106fail('alice not chosen as default initiator cred for server in r1')107output = r1.run(['./t_ccselect', host1, '-'])108if output != (alice + '\n'):109fail('alice not chosen as default initiator name for server in r1')110output = r2.run(['./t_ccselect', host2])111if output != (zaphod + '\n'):112fail('zaphod not chosen as default initiator cred for server in r1')113output = r2.run(['./t_ccselect', host2, '-'])114if output != (zaphod + '\n'):115fail('zaphod not chosen as default initiator name for server in r1')116117# Check that primary cache is used if server realm is unknown.118output = r2.run(['./t_ccselect', refserver])119if output != (zaphod + '\n'):120fail('zaphod not chosen via primary cache for unknown server realm')121r1.run(['./t_ccselect', gssserver2], expected_code=1)122# Check ccache selection using a fallback realm.123output = r1.run(['./t_ccselect', gssserver])124if output != (alice + '\n'):125fail('alice not chosen via parent domain fallback')126output = r2.run(['./t_ccselect', gssserver2])127if output != (zaphod + '\n'):128fail('zaphod not chosen via parent domain fallback')129# Check ccache selection using a fallback realm (default realm).130output = r1.run(['./t_ccselect', gsslocal])131if output != (alice + '\n'):132fail('alice not chosen via default realm fallback')133output = r2.run(['./t_ccselect', gsslocal])134if output != (zaphod + '\n'):135fail('zaphod not chosen via default realm fallback')136137# Check that realm ccselect fallback works correctly138r1.run(['./t_ccselect', gssserver_bar], expected_msg=alice)139r2.kinit(zaphod, password('zaphod'))140r1.run(['./t_ccselect', gssserver_bar], expected_msg=alice)141142# Get a second cred in r1 (bob will be primary).143r1.kinit(bob, password('bob'))144145# Try some cache selections using .k5identity.146k5id = open(os.path.join(r1.testdir, '.k5identity'), 'w')147k5id.write('%s realm=%s\n' % (alice, r1.realm))148k5id.write('%s service=ho*t host=localhost\n' % zaphod)149k5id.write('noprinc service=bogus')150k5id.close()151output = r1.run(['./t_ccselect', host1])152if output != (alice + '\n'):153fail('alice not chosen via .k5identity realm line.')154output = r2.run(['./t_ccselect', gsslocal])155if output != (zaphod + '\n'):156fail('zaphod not chosen via .k5identity service/host line.')157output = r1.run(['./t_ccselect', refserver])158if output != (bob + '\n'):159fail('bob not chosen via primary cache when no .k5identity line matches.')160r1.run(['./t_ccselect', 'h:bogus@' + foo2], expected_code=1,161expected_msg="Can't find client principal noprinc")162163success('GSSAPI credential selection tests')164165166