xref: /freebsd/crypto/krb5/src/tests/t_localauth.py (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubertfrom k5test import *
2*7f2fe78bSCy Schubert
3*7f2fe78bSCy Schubert# Unfortunately, we can't reliably test the k5login module.  We can control
4*7f2fe78bSCy Schubert# the directory where k5login files are read, but we can't suppress the UID
5*7f2fe78bSCy Schubert# validity check, which might fail in some filesystems for a .k5login file
6*7f2fe78bSCy Schubert# we create.
7*7f2fe78bSCy Schubertconf = {'plugins': {'localauth': { 'disable': 'k5login'}}}
8*7f2fe78bSCy Schubertrealm = K5Realm(create_kdb=False, krb5_conf=conf)
9*7f2fe78bSCy Schubert
10*7f2fe78bSCy Schubertdef test_an2ln(env, aname, result, msg):
11*7f2fe78bSCy Schubert    out = realm.run(['./localauth', aname], env=env)
12*7f2fe78bSCy Schubert    if out != result + '\n':
13*7f2fe78bSCy Schubert        fail(msg)
14*7f2fe78bSCy Schubert
15*7f2fe78bSCy Schubertdef test_an2ln_err(env, aname, err, msg):
16*7f2fe78bSCy Schubert    realm.run(['./localauth', aname], env=env, expected_code=1,
17*7f2fe78bSCy Schubert              expected_msg=err)
18*7f2fe78bSCy Schubert
19*7f2fe78bSCy Schubertdef test_userok(env, aname, lname, ok, msg):
20*7f2fe78bSCy Schubert    out = realm.run(['./localauth', aname, lname], env=env)
21*7f2fe78bSCy Schubert    if ((ok and out != 'yes\n') or
22*7f2fe78bSCy Schubert        (not ok and out != 'no\n')):
23*7f2fe78bSCy Schubert        fail(msg)
24*7f2fe78bSCy Schubert
25*7f2fe78bSCy Schubert# The default an2ln method works only in the default realm, and works
26*7f2fe78bSCy Schubert# for a single-component principal or a two-component principal where
27*7f2fe78bSCy Schubert# the second component is the default realm.
28*7f2fe78bSCy Schubertmark('default')
29*7f2fe78bSCy Schuberttest_an2ln(None, 'user@KRBTEST.COM', 'user', 'default rule 1')
30*7f2fe78bSCy Schuberttest_an2ln(None, 'user/KRBTEST.COM@KRBTEST.COM', 'user', 'default rule 2')
31*7f2fe78bSCy Schuberttest_an2ln_err(None, 'user/KRBTEST.COM/x@KRBTEST.COM', 'No translation',
32*7f2fe78bSCy Schubert               'default rule (3)')
33*7f2fe78bSCy Schuberttest_an2ln_err(None, 'user/X@KRBTEST.COM', 'No translation',
34*7f2fe78bSCy Schubert               'default rule comp mismatch')
35*7f2fe78bSCy Schuberttest_an2ln_err(None, 'user@X', 'No translation', 'default rule realm mismatch')
36*7f2fe78bSCy Schubert
37*7f2fe78bSCy Schubert# auth_to_local_names matches ignore the realm but are case-sensitive.
38*7f2fe78bSCy Schubertmark('auth_to_local_names')
39*7f2fe78bSCy Schubertconf_names1 = {'realms': {'$realm': {'auth_to_local_names': {'user': 'abcd'}}}}
40*7f2fe78bSCy Schubertnames1 = realm.special_env('names1', False, conf_names1)
41*7f2fe78bSCy Schuberttest_an2ln(names1, 'user@KRBTEST.COM', 'abcd', 'auth_to_local_names match')
42*7f2fe78bSCy Schuberttest_an2ln(names1, 'user@X', 'abcd', 'auth_to_local_names out-of-realm match')
43*7f2fe78bSCy Schuberttest_an2ln(names1, 'x@KRBTEST.COM', 'x', 'auth_to_local_names mismatch')
44*7f2fe78bSCy Schuberttest_an2ln(names1, 'User@KRBTEST.COM', 'User', 'auth_to_local_names case')
45*7f2fe78bSCy Schubert
46*7f2fe78bSCy Schubert# auth_to_local_names values must be in the default realm's section.
47*7f2fe78bSCy Schubertconf_names2 = {'realms': {'X': {'auth_to_local_names': {'user': 'abcd'}}}}
48*7f2fe78bSCy Schubertnames2 = realm.special_env('names2', False, conf_names2)
49*7f2fe78bSCy Schuberttest_an2ln_err(names2, 'user@X', 'No translation',
50*7f2fe78bSCy Schubert               'auth_to_local_names section mismatch')
51*7f2fe78bSCy Schubert
52*7f2fe78bSCy Schubert# Return a realm environment containing an auth_to_local value (or list).
53*7f2fe78bSCy Schubertdef a2l_realm(name, values):
54*7f2fe78bSCy Schubert    conf = {'realms': {'$realm': {'auth_to_local': values}}}
55*7f2fe78bSCy Schubert    return realm.special_env(name, False, conf)
56*7f2fe78bSCy Schubert
57*7f2fe78bSCy Schubert# Test explicit use of default method.
58*7f2fe78bSCy Schubertmark('explicit default')
59*7f2fe78bSCy Schubertauth1 = a2l_realm('auth1', 'DEFAULT')
60*7f2fe78bSCy Schuberttest_an2ln(auth1, 'user@KRBTEST.COM', 'user', 'default rule')
61*7f2fe78bSCy Schubert
62*7f2fe78bSCy Schubert# Test some invalid auth_to_local values.
63*7f2fe78bSCy Schubertmark('auth_to_local invalid')
64*7f2fe78bSCy Schubertauth2 = a2l_realm('auth2', 'RULE')
65*7f2fe78bSCy Schuberttest_an2ln_err(auth2, 'user@X', 'Improper format', 'null rule')
66*7f2fe78bSCy Schubertauth3 = a2l_realm('auth3', 'UNRECOGNIZED:stuff')
67*7f2fe78bSCy Schuberttest_an2ln_err(auth3, 'user@X', 'Improper format', 'null rule')
68*7f2fe78bSCy Schubert
69*7f2fe78bSCy Schubert# An empty rule has the default selection string (unparsed principal
70*7f2fe78bSCy Schubert# without realm) and no match or substitutions.
71*7f2fe78bSCy Schubertmark('rule (empty)')
72*7f2fe78bSCy Schubertrule1 = a2l_realm('rule1', 'RULE:')
73*7f2fe78bSCy Schuberttest_an2ln(rule1, 'user@KRBTEST.COM', 'user', 'empty rule')
74*7f2fe78bSCy Schuberttest_an2ln(rule1, 'user@X', 'user', 'empty rule (foreign realm)')
75*7f2fe78bSCy Schuberttest_an2ln(rule1, 'a/b/c@X', 'a/b/c', 'empty rule (multi-component)')
76*7f2fe78bSCy Schubert
77*7f2fe78bSCy Schubert# Test explicit selection string.  Also test that the default method
78*7f2fe78bSCy Schubert# is suppressed when auth_to_local values are present.
79*7f2fe78bSCy Schubertmark('rule (selection string)')
80*7f2fe78bSCy Schubertrule2 = a2l_realm('rule2', 'RULE:[2:$$0.$$2.$$1]')
81*7f2fe78bSCy Schuberttest_an2ln(rule2, 'aaron/burr@REALM', 'REALM.burr.aaron', 'selection string')
82*7f2fe78bSCy Schuberttest_an2ln_err(rule2, 'user@KRBTEST.COM', 'No translation', 'suppress default')
83*7f2fe78bSCy Schubert
84*7f2fe78bSCy Schubert# Test match string.
85*7f2fe78bSCy Schubertmark('rule (match string)')
86*7f2fe78bSCy Schubertrule3 = a2l_realm('rule3', 'RULE:(.*tail)')
87*7f2fe78bSCy Schuberttest_an2ln(rule3, 'withtail@X', 'withtail', 'rule match 1')
88*7f2fe78bSCy Schuberttest_an2ln(rule3, 'x/withtail@X', 'x/withtail', 'rule match 2')
89*7f2fe78bSCy Schuberttest_an2ln_err(rule3, 'tails@X', 'No translation', 'rule anchor mismatch')
90*7f2fe78bSCy Schubert
91*7f2fe78bSCy Schubert# Test substitutions.
92*7f2fe78bSCy Schubertmark('rule (substitutions)')
93*7f2fe78bSCy Schubertrule4 = a2l_realm('rule4', 'RULE:s/birds/bees/')
94*7f2fe78bSCy Schuberttest_an2ln(rule4, 'thebirdsbirdsbirds@X', 'thebeesbirdsbirds', 'subst 1')
95*7f2fe78bSCy Schubertrule5 = a2l_realm('rule4', 'RULE:s/birds/bees/g  s/bees/birds/')
96*7f2fe78bSCy Schuberttest_an2ln(rule4, 'the/birdsbirdsbirds@x', 'the/birdsbeesbees', 'subst 2')
97*7f2fe78bSCy Schubert
98*7f2fe78bSCy Schubert# Test a bunch of auth_to_local values and rule features in combination.
99*7f2fe78bSCy Schubertmark('rule (combo)')
100*7f2fe78bSCy Schubertcombo = a2l_realm('combo', ['RULE:[1:$$1-$$0](fred.*)s/-/ /g',
101*7f2fe78bSCy Schubert                            'DEFAULT',
102*7f2fe78bSCy Schubert                            'RULE:[3:$$1](z.*z)'])
103*7f2fe78bSCy Schuberttest_an2ln(combo, 'fred@X', 'fred X', 'combo 1')
104*7f2fe78bSCy Schuberttest_an2ln(combo, 'fred-too@X', 'fred too X', 'combo 2')
105*7f2fe78bSCy Schuberttest_an2ln(combo, 'fred@KRBTEST.COM', 'fred KRBTEST.COM', 'combo 3')
106*7f2fe78bSCy Schuberttest_an2ln(combo, 'user@KRBTEST.COM', 'user', 'combo 4')
107*7f2fe78bSCy Schuberttest_an2ln(combo, 'zazz/b/c@X', 'zazz', 'combo 5')
108*7f2fe78bSCy Schuberttest_an2ln_err(combo, 'a/b@KRBTEST.COM', 'No translation', 'combo 6')
109*7f2fe78bSCy Schubert
110*7f2fe78bSCy Schubert# Test the an2ln userok method with the combo environment.
111*7f2fe78bSCy Schubertmark('userok (an2ln)')
112*7f2fe78bSCy Schuberttest_userok(combo, 'fred@X', 'fred X', True, 'combo userok 1')
113*7f2fe78bSCy Schuberttest_userok(combo, 'user@KRBTEST.COM', 'user', True, 'combo userok 2')
114*7f2fe78bSCy Schuberttest_userok(combo, 'user@KRBTEST.COM', 'X', False, 'combo userok 3')
115*7f2fe78bSCy Schuberttest_userok(combo, 'a/b@KRBTEST.COM', 'a/b', False, 'combo userok 4')
116*7f2fe78bSCy Schubert
117*7f2fe78bSCy Schubertmark('test modules')
118*7f2fe78bSCy Schubert
119*7f2fe78bSCy Schubert# Register the two test modules and set up some auth_to_local and
120*7f2fe78bSCy Schubert# auth_to_local_names entries.
121*7f2fe78bSCy Schubertmodpath = os.path.join(buildtop, 'plugins', 'localauth', 'test',
122*7f2fe78bSCy Schubert                       'localauth_test.so')
123*7f2fe78bSCy Schubertconf = {'plugins': {'localauth': { 'module': [
124*7f2fe78bSCy Schubert                'test1:' + modpath,
125*7f2fe78bSCy Schubert                'test2:' + modpath]}},
126*7f2fe78bSCy Schubert        'realms': {'$realm': {'auth_to_local': [
127*7f2fe78bSCy Schubert                'RULE:(test/rulefirst)s/.*/rule/',
128*7f2fe78bSCy Schubert                'TYPEA',
129*7f2fe78bSCy Schubert                'DEFAULT',
130*7f2fe78bSCy Schubert                'TYPEB:resid']},
131*7f2fe78bSCy Schubert                   'auth_to_local_names': {'test/a/b': 'name'}}}
132*7f2fe78bSCy Schubertmod = realm.special_env('mod', False, conf)
133*7f2fe78bSCy Schubert
134*7f2fe78bSCy Schubert# test1's untyped an2ln method should come before the names method, mapping
135*7f2fe78bSCy Schubert# test/a/b@X to its realm name (superseding auth_to_local_names).
136*7f2fe78bSCy Schuberttest_an2ln(mod, 'test/a/b@X', 'X', 'mod untyped an2ln')
137*7f2fe78bSCy Schubert
138*7f2fe78bSCy Schubert# Match the auth_to_local values in order.  test2's TYPEA should map
139*7f2fe78bSCy Schubert# test/notrule to its second component, and its TYPEB should map
140*7f2fe78bSCy Schubert# anything which gets there to the residual string.
141*7f2fe78bSCy Schuberttest_an2ln(mod, 'test/rulefirst@X', 'rule', 'mod auth_to_local 1')
142*7f2fe78bSCy Schuberttest_an2ln(mod, 'test/notrule', 'notrule', 'mod auth_to_local 2')
143*7f2fe78bSCy Schuberttest_an2ln(mod, 'user@KRBTEST.COM', 'user', 'mod auth_to_local 3')
144*7f2fe78bSCy Schuberttest_an2ln(mod, 'xyz@X', 'resid', 'mod auth_to_local 4')
145*7f2fe78bSCy Schubert
146*7f2fe78bSCy Schubert# test2's userok module should succeed when the number of components
147*7f2fe78bSCy Schubert# is equal to the length of the local name, should pass if the first
148*7f2fe78bSCy Schubert# component is 'pass', and should reject otherwise.
149*7f2fe78bSCy Schuberttest_userok(mod, 'a/b/c/d@X', 'four', True, 'mod userok 1')
150*7f2fe78bSCy Schuberttest_userok(mod, 'x/y/z@X', 'four', False, 'mod userok 2')
151*7f2fe78bSCy Schuberttest_userok(mod, 'pass@KRBTEST.COM', 'pass', True, 'mod userok 3')
152*7f2fe78bSCy Schuberttest_userok(mod, 'user@KRBTEST.COM', 'user', False, 'mod userok 4')
153*7f2fe78bSCy Schubert
154*7f2fe78bSCy Schubertsuccess('krb5_kuserok and krb5_aname_to_localname tests')
155