xref: /freebsd/crypto/krb5/src/tests/gssapi/t_ccselect.py (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert# Copyright (C) 2011 by the Massachusetts Institute of Technology.
2*7f2fe78bSCy Schubert# All rights reserved.
3*7f2fe78bSCy Schubert
4*7f2fe78bSCy Schubert# Export of this software from the United States of America may
5*7f2fe78bSCy Schubert#   require a specific license from the United States Government.
6*7f2fe78bSCy Schubert#   It is the responsibility of any person or organization contemplating
7*7f2fe78bSCy Schubert#   export to obtain such a license before exporting.
8*7f2fe78bSCy Schubert#
9*7f2fe78bSCy Schubert# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10*7f2fe78bSCy Schubert# distribute this software and its documentation for any purpose and
11*7f2fe78bSCy Schubert# without fee is hereby granted, provided that the above copyright
12*7f2fe78bSCy Schubert# notice appear in all copies and that both that copyright notice and
13*7f2fe78bSCy Schubert# this permission notice appear in supporting documentation, and that
14*7f2fe78bSCy Schubert# the name of M.I.T. not be used in advertising or publicity pertaining
15*7f2fe78bSCy Schubert# to distribution of the software without specific, written prior
16*7f2fe78bSCy Schubert# permission.  Furthermore if you modify this software you must label
17*7f2fe78bSCy Schubert# your software as modified software and not distribute it in such a
18*7f2fe78bSCy Schubert# fashion that it might be confused with the original M.I.T. software.
19*7f2fe78bSCy Schubert# M.I.T. makes no representations about the suitability of
20*7f2fe78bSCy Schubert# this software for any purpose.  It is provided "as is" without express
21*7f2fe78bSCy Schubert# or implied warranty.
22*7f2fe78bSCy Schubert
23*7f2fe78bSCy Schubertfrom k5test import *
24*7f2fe78bSCy Schubert
25*7f2fe78bSCy Schubert# Create two independent realms (no cross-realm TGTs).  For the
26*7f2fe78bSCy Schubert# fallback realm tests we need to control the precise server hostname,
27*7f2fe78bSCy Schubert# so turn off DNS canonicalization and shortname qualification.
28*7f2fe78bSCy Schubertconf = {'libdefaults': {'dns_canonicalize_hostname': 'false',
29*7f2fe78bSCy Schubert                        'qualify_shortname': ''}}
30*7f2fe78bSCy Schubertr1 = K5Realm(create_user=False, krb5_conf=conf)
31*7f2fe78bSCy Schubertr2 = K5Realm(create_user=False, krb5_conf=conf, realm='KRBTEST2.COM',
32*7f2fe78bSCy Schubert             portbase=62000, testdir=os.path.join(r1.testdir, 'r2'))
33*7f2fe78bSCy Schubert
34*7f2fe78bSCy Schuberthost1 = 'p:' + r1.host_princ
35*7f2fe78bSCy Schuberthost2 = 'p:' + r2.host_princ
36*7f2fe78bSCy Schubertfoo = 'foo.krbtest.com'
37*7f2fe78bSCy Schubertfoo2 = 'foo.krbtest2.com'
38*7f2fe78bSCy Schubertfoobar = "foo.bar.krbtest.com"
39*7f2fe78bSCy Schubert
40*7f2fe78bSCy Schubert# These strings specify the target as a GSS name.  The resulting
41*7f2fe78bSCy Schubert# principal will have the host-based type, with the referral realm
42*7f2fe78bSCy Schubert# (since k5test realms have no domain-realm mapping by default).
43*7f2fe78bSCy Schubert# krb5_cc_select() will use the fallback realm, which is either the
44*7f2fe78bSCy Schubert# uppercased parent domain, or the default realm if the hostname is a
45*7f2fe78bSCy Schubert# single component.
46*7f2fe78bSCy Schubertgssserver = 'h:host@' + foo
47*7f2fe78bSCy Schubertgssserver2 = 'h:host@' + foo2
48*7f2fe78bSCy Schubertgssserver_bar = 'h:host@' + foobar
49*7f2fe78bSCy Schubertgsslocal = 'h:host@localhost'
50*7f2fe78bSCy Schubert
51*7f2fe78bSCy Schubert# refserver specifies the target as a principal in the referral realm.
52*7f2fe78bSCy Schubert# The principal won't be treated as a host principal by the
53*7f2fe78bSCy Schubert# .k5identity rules since it has unknown type.
54*7f2fe78bSCy Schubertrefserver = 'p:host/' + hostname + '@'
55*7f2fe78bSCy Schubert
56*7f2fe78bSCy Schubert# Verify that we can't get initiator creds with no credentials in the
57*7f2fe78bSCy Schubert# collection.
58*7f2fe78bSCy Schubertr1.run(['./t_ccselect', host1, '-'], expected_code=1,
59*7f2fe78bSCy Schubert       expected_msg='No Kerberos credentials available')
60*7f2fe78bSCy Schubert
61*7f2fe78bSCy Schubert# Make a directory collection and use it for client commands in both realms.
62*7f2fe78bSCy Schubertccdir = os.path.join(r1.testdir, 'cc')
63*7f2fe78bSCy Schubertccname = 'DIR:' + ccdir
64*7f2fe78bSCy Schubertr1.env['KRB5CCNAME'] = ccname
65*7f2fe78bSCy Schubertr2.env['KRB5CCNAME'] = ccname
66*7f2fe78bSCy Schubert
67*7f2fe78bSCy Schubert# Use .k5identity from testdir and not from the tester's homedir.
68*7f2fe78bSCy Schubertr1.env['HOME'] = r1.testdir
69*7f2fe78bSCy Schubertr2.env['HOME'] = r1.testdir
70*7f2fe78bSCy Schubert
71*7f2fe78bSCy Schubert# Create two users in r1 and one in r2.
72*7f2fe78bSCy Schubertalice='alice@KRBTEST.COM'
73*7f2fe78bSCy Schubertbob='bob@KRBTEST.COM'
74*7f2fe78bSCy Schubertzaphod='zaphod@KRBTEST2.COM'
75*7f2fe78bSCy Schubertr1.addprinc(alice, password('alice'))
76*7f2fe78bSCy Schubertr1.addprinc(bob, password('bob'))
77*7f2fe78bSCy Schubertr2.addprinc(zaphod, password('zaphod'))
78*7f2fe78bSCy Schubert
79*7f2fe78bSCy Schubert# Create host principals and keytabs for fallback realm tests.
80*7f2fe78bSCy Schubertif hostname != 'localhost':
81*7f2fe78bSCy Schubert    r1.addprinc('host/localhost')
82*7f2fe78bSCy Schubert    r2.addprinc('host/localhost')
83*7f2fe78bSCy Schubertr1.addprinc('host/' + foo)
84*7f2fe78bSCy Schubertr2.addprinc('host/' + foo2)
85*7f2fe78bSCy Schubertr1.addprinc('host/' + foobar)
86*7f2fe78bSCy Schubertr1.extract_keytab('host/localhost', r1.keytab)
87*7f2fe78bSCy Schubertr2.extract_keytab('host/localhost', r2.keytab)
88*7f2fe78bSCy Schubertr1.extract_keytab('host/' + foo, r1.keytab)
89*7f2fe78bSCy Schubertr2.extract_keytab('host/' + foo2, r2.keytab)
90*7f2fe78bSCy Schubertr1.extract_keytab('host/' + foobar, r1.keytab)
91*7f2fe78bSCy Schubert
92*7f2fe78bSCy Schubert# Get tickets for one user in each realm (zaphod will be primary).
93*7f2fe78bSCy Schubertr1.kinit(alice, password('alice'))
94*7f2fe78bSCy Schubertr2.kinit(zaphod, password('zaphod'))
95*7f2fe78bSCy Schubert
96*7f2fe78bSCy Schubert# Check that we can find a cache for a specified client principal.
97*7f2fe78bSCy Schubertoutput = r1.run(['./t_ccselect', host1, 'p:' + alice])
98*7f2fe78bSCy Schubertif output != (alice + '\n'):
99*7f2fe78bSCy Schubert    fail('alice not chosen when specified')
100*7f2fe78bSCy Schubertoutput = r2.run(['./t_ccselect', host2, 'p:' + zaphod])
101*7f2fe78bSCy Schubertif output != (zaphod + '\n'):
102*7f2fe78bSCy Schubert    fail('zaphod not chosen when specified')
103*7f2fe78bSCy Schubert
104*7f2fe78bSCy Schubert# Check that we can guess a cache based on the service realm.
105*7f2fe78bSCy Schubertoutput = r1.run(['./t_ccselect', host1])
106*7f2fe78bSCy Schubertif output != (alice + '\n'):
107*7f2fe78bSCy Schubert    fail('alice not chosen as default initiator cred for server in r1')
108*7f2fe78bSCy Schubertoutput = r1.run(['./t_ccselect', host1, '-'])
109*7f2fe78bSCy Schubertif output != (alice + '\n'):
110*7f2fe78bSCy Schubert    fail('alice not chosen as default initiator name for server in r1')
111*7f2fe78bSCy Schubertoutput = r2.run(['./t_ccselect', host2])
112*7f2fe78bSCy Schubertif output != (zaphod + '\n'):
113*7f2fe78bSCy Schubert    fail('zaphod not chosen as default initiator cred for server in r1')
114*7f2fe78bSCy Schubertoutput = r2.run(['./t_ccselect', host2, '-'])
115*7f2fe78bSCy Schubertif output != (zaphod + '\n'):
116*7f2fe78bSCy Schubert    fail('zaphod not chosen as default initiator name for server in r1')
117*7f2fe78bSCy Schubert
118*7f2fe78bSCy Schubert# Check that primary cache is used if server realm is unknown.
119*7f2fe78bSCy Schubertoutput = r2.run(['./t_ccselect', refserver])
120*7f2fe78bSCy Schubertif output != (zaphod + '\n'):
121*7f2fe78bSCy Schubert    fail('zaphod not chosen via primary cache for unknown server realm')
122*7f2fe78bSCy Schubertr1.run(['./t_ccselect', gssserver2], expected_code=1)
123*7f2fe78bSCy Schubert# Check ccache selection using a fallback realm.
124*7f2fe78bSCy Schubertoutput = r1.run(['./t_ccselect', gssserver])
125*7f2fe78bSCy Schubertif output != (alice + '\n'):
126*7f2fe78bSCy Schubert    fail('alice not chosen via parent domain fallback')
127*7f2fe78bSCy Schubertoutput = r2.run(['./t_ccselect', gssserver2])
128*7f2fe78bSCy Schubertif output != (zaphod + '\n'):
129*7f2fe78bSCy Schubert    fail('zaphod not chosen via parent domain fallback')
130*7f2fe78bSCy Schubert# Check ccache selection using a fallback realm (default realm).
131*7f2fe78bSCy Schubertoutput = r1.run(['./t_ccselect', gsslocal])
132*7f2fe78bSCy Schubertif output != (alice + '\n'):
133*7f2fe78bSCy Schubert    fail('alice not chosen via default realm fallback')
134*7f2fe78bSCy Schubertoutput = r2.run(['./t_ccselect', gsslocal])
135*7f2fe78bSCy Schubertif output != (zaphod + '\n'):
136*7f2fe78bSCy Schubert    fail('zaphod not chosen via default realm fallback')
137*7f2fe78bSCy Schubert
138*7f2fe78bSCy Schubert# Check that realm ccselect fallback works correctly
139*7f2fe78bSCy Schubertr1.run(['./t_ccselect', gssserver_bar], expected_msg=alice)
140*7f2fe78bSCy Schubertr2.kinit(zaphod, password('zaphod'))
141*7f2fe78bSCy Schubertr1.run(['./t_ccselect', gssserver_bar], expected_msg=alice)
142*7f2fe78bSCy Schubert
143*7f2fe78bSCy Schubert# Get a second cred in r1 (bob will be primary).
144*7f2fe78bSCy Schubertr1.kinit(bob, password('bob'))
145*7f2fe78bSCy Schubert
146*7f2fe78bSCy Schubert# Try some cache selections using .k5identity.
147*7f2fe78bSCy Schubertk5id = open(os.path.join(r1.testdir, '.k5identity'), 'w')
148*7f2fe78bSCy Schubertk5id.write('%s realm=%s\n' % (alice, r1.realm))
149*7f2fe78bSCy Schubertk5id.write('%s service=ho*t host=localhost\n' % zaphod)
150*7f2fe78bSCy Schubertk5id.write('noprinc service=bogus')
151*7f2fe78bSCy Schubertk5id.close()
152*7f2fe78bSCy Schubertoutput = r1.run(['./t_ccselect', host1])
153*7f2fe78bSCy Schubertif output != (alice + '\n'):
154*7f2fe78bSCy Schubert    fail('alice not chosen via .k5identity realm line.')
155*7f2fe78bSCy Schubertoutput = r2.run(['./t_ccselect', gsslocal])
156*7f2fe78bSCy Schubertif output != (zaphod + '\n'):
157*7f2fe78bSCy Schubert    fail('zaphod not chosen via .k5identity service/host line.')
158*7f2fe78bSCy Schubertoutput = r1.run(['./t_ccselect', refserver])
159*7f2fe78bSCy Schubertif output != (bob + '\n'):
160*7f2fe78bSCy Schubert    fail('bob not chosen via primary cache when no .k5identity line matches.')
161*7f2fe78bSCy Schubertr1.run(['./t_ccselect', 'h:bogus@' + foo2], expected_code=1,
162*7f2fe78bSCy Schubert       expected_msg="Can't find client principal noprinc")
163*7f2fe78bSCy Schubert
164*7f2fe78bSCy Schubertsuccess('GSSAPI credential selection tests')
165