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