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