Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / tests / t_referral.py
1 #!/usr/bin/python
2 from k5test import *
3
4 # Create a pair of realms, where KRBTEST1.COM can authenticate to
5 # REFREALM and has a domain-realm mapping for 'd' pointing to it.
6 drealm = {'domain_realm': {'d': 'REFREALM'}}
7 realm, refrealm = cross_realms(2, xtgts=((0,1),),
8                                args=({'kdc_conf': drealm},
9                                      {'realm': 'REFREALM',
10                                       'create_user': False}),
11                                create_host=False)
12 refrealm.addprinc('a/x.d')
13
14 savefile = os.path.join(realm.testdir, 'ccache.copy')
15 os.rename(realm.ccache, savefile)
16
17 # Get credentials and check that we got a referral to REFREALM.
18 def testref(realm, nametype):
19     shutil.copyfile(savefile, realm.ccache)
20     realm.run(['./gcred', nametype, 'a/x.d'])
21     realm.klist(realm.user_princ, 'a/x.d@REFREALM')
22
23 # Get credentials and check that we get an error, not a referral.
24 def testfail(realm, nametype):
25     shutil.copyfile(savefile, realm.ccache)
26     out = realm.run(['./gcred', nametype, 'a/x.d'], expected_code=1)
27     if 'not found in Kerberos database' not in out:
28         fail('unexpected error')
29
30 # Create a modified KDC environment and restart the KDC.
31 def restart_kdc(realm, kdc_conf):
32     env = realm.special_env('extravars', True, kdc_conf=kdc_conf)
33     realm.stop_kdc()
34     realm.start_kdc(env=env)
35
36 # With no KDC configuration besides [domain_realm], we should get a
37 # referral for a NT-SRV-HST or NT-SRV-INST server name, but not an
38 # NT-UNKNOWN or NT-PRINCIPAL server name.
39 testref(realm, 'srv-hst')
40 testref(realm, 'srv-inst')
41 testfail(realm, 'principal')
42 testfail(realm, 'unknown')
43
44 # With host_based_services matching the first server name component
45 # ("a"), we should get a referral for an NT-UNKNOWN server name.
46 # host_based_services can appear in either [kdcdefaults] or the realm
47 # section, with the realm values supplementing the kdcdefaults values.
48 # NT-SRV-HST server names should be unaffected by host_based_services,
49 # and NT-PRINCIPAL server names shouldn't get a referral regardless.
50 restart_kdc(realm, {'kdcdefaults': {'host_based_services': '*'}})
51 testref(realm, 'unknown')
52 testfail(realm, 'principal')
53 restart_kdc(realm, {'kdcdefaults': {'host_based_services': ['b', 'a,c']}})
54 testref(realm, 'unknown')
55 restart_kdc(realm, {'realms': {'$realm': {'host_based_services': 'a b c'}}})
56 testref(realm, 'unknown')
57 restart_kdc(realm, {'kdcdefaults': {'host_based_services': 'a'},
58                     'realms': {'$realm': {'host_based_services': 'b c'}}})
59 testref(realm, 'unknown')
60 restart_kdc(realm, {'kdcdefaults': {'host_based_services': 'b,c'},
61                     'realms': {'$realm': {'host_based_services': 'a,b'}}})
62 testref(realm, 'unknown')
63 restart_kdc(realm, {'kdcdefaults': {'host_based_services': 'b,c'}})
64 testfail(realm, 'unknown')
65 testref(realm, 'srv-hst')
66
67 # With no_host_referrals matching the first server name component, we
68 # should not get a referral even for NT-SRV-HOST server names
69 restart_kdc(realm, {'kdcdefaults': {'no_host_referral': '*'}})
70 testfail(realm, 'srv-hst')
71 restart_kdc(realm, {'kdcdefaults': {'no_host_referral': ['b', 'a,c']}})
72 testfail(realm, 'srv-hst')
73 restart_kdc(realm, {'realms': {'$realm': {'no_host_referral': 'a b c'}}})
74 testfail(realm, 'srv-hst')
75 restart_kdc(realm, {'kdcdefaults': {'no_host_referral': 'a'},
76                     'realms': {'$realm': {'no_host_referral': 'b c'}}})
77 testfail(realm, 'srv-hst')
78 restart_kdc(realm, {'kdcdefaults': {'no_host_referral': 'b,c'},
79                     'realms': {'$realm': {'no_host_referral': 'a,b'}}})
80 testfail(realm, 'srv-hst')
81 restart_kdc(realm, {'kdcdefaults': {'no_host_referral': 'b,c'}})
82 testref(realm, 'srv-hst')
83
84 # no_host_referrals should override host_based_services for NT-UNKNWON
85 # server names.
86 restart_kdc(realm, {'kdcdefaults': {'no_host_referral': '*',
87                                     'host_based_services': '*'}})
88 testfail(realm, 'unknown')
89
90 realm.stop()
91 refrealm.stop()
92
93 # Regression test for #7483: a KDC should not return a host referral
94 # to its own realm.
95 drealm = {'domain_realm': {'d': 'KRBTEST.COM'}}
96 realm = K5Realm(kdc_conf=drealm, create_host=False)
97 tracefile = os.path.join(realm.testdir, 'trace')
98 realm.run(['env', 'KRB5_TRACE=' + tracefile, './gcred', 'srv-hst', 'a/x.d@'],
99           expected_code=1)
100 f = open(tracefile, 'r')
101 trace = f.read()
102 f.close()
103 if 'back to same realm' in trace:
104     fail('KDC returned referral to service realm')
105 realm.stop()
106
107 # Test client referrals.  Use the test KDB module for KRBTEST1.COM to
108 # simulate referrals since our built-in modules do not support them.
109 # No cross-realm TGTs are necessary.
110 kdcconf = {'realms': {'$realm': {'database_module': 'test'}},
111            'dbmodules': {'test': {'db_library': 'test',
112                                   'alias': {'user': '@KRBTEST2.COM',
113                                             'abc@XYZ': '@KRBTEST2.COM'}}}}
114 r1, r2 = cross_realms(2, xtgts=(),
115                       args=({'kdc_conf': kdcconf, 'create_kdb': False}, None),
116                       create_host=False)
117 r2.addprinc('abc\@XYZ', 'pw')
118 r1.start_kdc()
119 out = r1.kinit('user', expected_code=1)
120 if 'not found in Kerberos database' not in out:
121     fail('Expected error not seen for referral without canonicalize flag')
122 r1.kinit('user', password('user'), ['-C'])
123 r1.klist('user@KRBTEST2.COM', 'krbtgt/KRBTEST2.COM')
124 r1.kinit('abc@XYZ', 'pw', ['-E'])
125 r1.klist('abc\@XYZ@KRBTEST2.COM', 'krbtgt/KRBTEST2.COM')
126
127 success('KDC host referral tests')