testprogs: Use random usernames for kinit tests
[bbaumbach/samba-autobuild/.git] / testprogs / blackbox / test_kinit_mit.sh
1 #!/bin/sh
2 # Blackbox tests for kinit and kerberos integration with smbclient etc
3 # Copyright (c) 2015-2016 Andreas Schneider <asn@samba.org>
4
5 if [ $# -lt 5 ]; then
6         cat <<EOF
7 Usage: test_kinit.sh SERVER USERNAME PASSWORD REALM DOMAIN PREFIX SMBCLIENT
8 EOF
9         exit 1
10 fi
11
12 SERVER=$1
13 USERNAME=$2
14 PASSWORD=$3
15 REALM=$4
16 DOMAIN=$5
17 PREFIX=$6
18 smbclient=$7
19 shift 7
20 failed=0
21
22 samba_bindir="$BINDIR"
23 samba_srcdir="$SRCDIR/source4"
24 samba_kinit=kinit
25 samba_kdestroy=kdestroy
26 samba_kpasswd=kpasswd
27 samba_kvno=kvno
28
29 samba_tool="$samba_bindir/samba-tool"
30 samba_texpect="$samba_bindir/texpect"
31
32 samba_enableaccount="$samba_tool user enable"
33 machineaccountccache="$samba_srcdir/scripting/bin/machineaccountccache"
34
35 . $(dirname $0)/subunit.sh
36 . "$(dirname "${0}")/common_test_fns.inc"
37
38 ldbmodify=$(system_or_builddir_binary ldbmodify "${BINDIR}")
39 ldbsearch=$(system_or_builddir_binary ldbsearch "${BINDIR}")
40
41 test_smbclient()
42 {
43         name="$1"
44         cmd="$2"
45         shift
46         shift
47         echo "test: $name"
48         $VALGRIND $smbclient $CONFIGURATION //$SERVER/tmp -c "$cmd" "$@"
49         status=$?
50         if [ x$status = x0 ]; then
51                 echo "success: $name"
52         else
53                 echo "failure: $name"
54         fi
55         return $status
56 }
57
58 TEST_USER="$(mktemp -u kinittestuserXXXXXX)"
59
60 ADMIN_LDBMODIFY_CONFIG="-H ldap://$SERVER -U$USERNAME%$PASSWORD"
61 export ADMIN_LDBMODIFY_CONFIG
62
63 KRB5CCNAME_PATH="$PREFIX/tmpccache"
64 KRB5CCNAME="FILE:$KRB5CCNAME_PATH"
65 ADMIN_KRB5CCNAME="FILE:$KRB5CCNAME_PATH"
66 export KRB5CCNAME
67 rm -rf $KRB5CCNAME_PATH
68
69 testit "reset password policies beside of minimum password age of 0 days" $VALGRIND $PYTHON $samba_tool domain passwordsettings set $ADMIN_LDBMODIFY_CONFIG --complexity=default --history-length=default --min-pwd-length=default --min-pwd-age=0 --max-pwd-age=default || failed=$(expr $failed + 1)
70
71 cat >$PREFIX/tmpkinitscript <<EOF
72 expect Password for
73 send ${PASSWORD}\n
74 EOF
75
76 ###########################################################
77 ### Test kinit defaults
78 ###########################################################
79
80 testit "kinit with password" $samba_texpect $PREFIX/tmpkinitscript $samba_kinit $USERNAME@$REALM || failed=$(expr $failed + 1)
81 test_smbclient "Test login with user kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
82
83 testit "kinit renew ticket" $samba_kinit -R || failed=$(expr $failed + 1)
84 test_smbclient "Test login with kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
85
86 $samba_kdestroy
87
88 ###########################################################
89 ### Test kinit with enterprice principal
90 ###########################################################
91
92 testit "kinit with password (enterprise style)" $samba_texpect $PREFIX/tmpkinitscript $samba_kinit -E $USERNAME@$REALM || failed=$(expr $failed + 1)
93 test_smbclient "Test login with user kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
94
95 # This does not work with MIT Kerberos 1.14 or older
96 testit "kinit renew ticket (enterprise style)" $samba_kinit -R || failed=$(expr $failed + 1)
97 test_smbclient "Test login with kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
98
99 $samba_kdestroy
100
101 ###########################################################
102 ### Tests with kinit default again
103 ###########################################################
104
105 testit "kinit with password" $samba_texpect $PREFIX/tmpkinitscript $samba_kinit $USERNAME@$REALM || failed=$(expr $failed + 1)
106 testit "check time with kerberos ccache" $VALGRIND $PYTHON $samba_tool time $SERVER $CONFIGURATION --use-krb5-ccache="${KRB5CCNAME}" "$@" || failed=$(expr $failed + 1)
107
108 USERPASS="testPass@12%"
109
110 testit "add user with kerberos ccache" $VALGRIND $PYTHON $samba_tool user create ${TEST_USER} $USERPASS $CONFIGURATION --use-krb5-ccache="${KRB5CCNAME}" "$@" || failed=$(expr $failed + 1)
111
112 echo "Getting defaultNamingContext"
113 BASEDN=$($ldbsearch $options --basedn='' -H ldap://$SERVER --scope=base DUMMY=x defaultNamingContext | grep defaultNamingContext | awk '{print $2}')
114
115 cat >$PREFIX/tmpldbmodify <<EOF
116 dn: cn=${TEST_USER},cn=users,$BASEDN
117 changetype: modify
118 add: servicePrincipalName
119 servicePrincipalName: host/${TEST_USER}
120 replace: userPrincipalName
121 userPrincipalName: nettest@$REALM
122 EOF
123
124 testit "modify servicePrincipalName and userPrincpalName" $VALGRIND $ldbmodify -H ldap://$SERVER $PREFIX/tmpldbmodify --use-krb5-ccache="${KRB5CCNAME}" "$@" || failed=$(expr $failed + 1)
125
126 testit "set user password with kerberos ccache" $VALGRIND $PYTHON $samba_tool user setpassword ${TEST_USER} --newpassword=$USERPASS $CONFIGURATION --use-krb5-ccache="${KRB5CCNAME}" "$@" || failed=$(expr $failed + 1)
127
128 testit "enable user with kerberos cache" $VALGRIND $PYTHON $samba_enableaccount ${TEST_USER} -H ldap://$SERVER --use-krb5-ccache="${KRB5CCNAME}" "$@" || failed=$(expr $failed + 1)
129
130 ###########################################################
131 ### Test kinit with canonicalization
132 ###########################################################
133
134 upperusername=$(echo $USERNAME | tr '[a-z]' '[A-Z]')
135 testit "kinit with canonicalize" $samba_texpect $PREFIX/tmpkinitscript $samba_kinit -C $upperusername@$REALM -S kadmin/changepw@$REALM || failed=$(expr $failed + 1)
136
137 $samba_kdestroy
138
139 ###########################################################
140 ### Test kinit with user credentials
141 ###########################################################
142
143 KRB5CCNAME_PATH="$PREFIX/tmpuserccache"
144 KRB5CCNAME="FILE:$KRB5CCNAME_PATH"
145 export KRB5CCNAME
146
147 rm -f $KRB5CCNAME_PATH
148
149 cat >$PREFIX/tmpkinituserpassscript <<EOF
150 expect Password for
151 send ${USERPASS}\n
152 EOF
153
154 testit "kinit with user password" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit ${TEST_USER}@$REALM || failed=$(expr $failed + 1)
155 test_smbclient "Test login with user kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
156
157 ### Change password
158
159 NEWUSERPASS="testPaSS@34%"
160 testit "change user password with 'samba-tool user password' (rpc)" $VALGRIND $PYTHON $samba_tool user password -W$DOMAIN -U${TEST_USER}%$USERPASS $CONFIGURATION --use-kerberos=off --newpassword=$NEWUSERPASS "$@" || failed=$(expr $failed + 1)
161
162 cat >$PREFIX/tmpkinituserpassscript <<EOF
163 expect Password for
164 send ${NEWUSERPASS}\n
165 EOF
166
167 testit "kinit with new user password" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit ${TEST_USER}@$REALM || failed=$(expr $failed + 1)
168 test_smbclient "Test login with user kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
169
170 $samba_kdestroy
171
172 ###########################################################
173 ### Test kinit with user credentials in special formats
174 ###########################################################
175
176 testit "kinit with new (NT-Principal style) using UPN" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit nettest@$REALM || failed=$(expr $failed + 1)
177 test_smbclient "Test login with user kerberos ccache from NT UPN" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
178
179 $samba_kdestroy
180
181 testit "kinit with new (enterprise style) using UPN" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit -E nettest@$REALM || failed=$(expr $failed + 1)
182 test_smbclient "Test login with user kerberos ccache from enterprise UPN" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
183
184 $samba_kdestroy
185
186 ###########################################################
187 ### Test kinit with user credentials and changed realm
188 ###########################################################
189
190 cat >$PREFIX/tmpldbmodify <<EOF
191 dn: cn=${TEST_USER},cn=users,$BASEDN
192 changetype: modify
193 replace: userPrincipalName
194 userPrincipalName: nettest@$REALM.org
195 EOF
196
197 testit "modify userPrincipalName to be a different domain" $VALGRIND $ldbmodify $ADMIN_LDBMODIFY_CONFIG $PREFIX/tmpldbmodify $PREFIX/tmpldbmodify --use-krb5-ccache="${KRB5CCNAME}" "$@" || failed=$(expr $failed + 1)
198
199 testit "kinit with new (enterprise style) using UPN" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit -E nettest@$REALM.org || failed=$(expr $failed + 1)
200 test_smbclient "Test login with user kerberos ccache from enterprise UPN" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
201
202 $samba_kdestroy
203
204 ###########################################################
205 ### Test password change with kpasswd
206 ###########################################################
207
208 testit "kinit with user password" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit ${TEST_USER}@$REALM || failed=$(expr $failed + 1)
209 test_smbclient "Test login with user kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
210
211 USERPASS=$NEWUSERPASS
212 NEWUSERPASS=testPaSS@56%
213
214 cat >$PREFIX/tmpkpasswdscript <<EOF
215 expect Password for
216 password ${USERPASS}\n
217 expect Enter new password
218 send ${NEWUSERPASS}\n
219 expect Enter it again
220 send ${NEWUSERPASS}\n
221 expect Password changed
222 EOF
223
224 testit "change user password with kpasswd" $samba_texpect $PREFIX/tmpkpasswdscript $samba_kpasswd ${TEST_USER}@$REALM || failed=$(expr $failed + 1)
225
226 $samba_kdestroy
227
228 USERPASS=$NEWUSERPASS
229 cat >$PREFIX/tmpkinituserpassscript <<EOF
230 expect Password for
231 send ${USERPASS}\n
232 EOF
233
234 testit "kinit with user password" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit ${TEST_USER}@$REALM || failed=$(expr $failed + 1)
235 test_smbclient "Test login with user kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
236
237 $samba_kdestroy
238
239 ###########################################################
240 ### TODO Test set password with kpasswd
241 ###########################################################
242
243 # This is not implemented in kpasswd
244
245 ###########################################################
246 ### Test password expiry
247 ###########################################################
248
249 cat >$PREFIX/tmpldbmodify <<EOF
250 dn: cn=${TEST_USER},cn=users,$BASEDN
251 changetype: modify
252 replace: pwdLastSet
253 pwdLastSet: 0
254 EOF
255
256 USERPASS=$NEWUSERPASS
257 NEWUSERPASS=testPaSS@911%
258
259 testit "modify pwdLastSet" $VALGRIND $ldbmodify $ADMIN_LDBMODIFY_CONFIG $PREFIX/tmpldbmodify $PREFIX/tmpldbmodify --use-krb5-ccache="${KRB5CCNAME}" "$@" || failed=$(expr $failed + 1)
260
261 cat >$PREFIX/tmpkinituserpassscript <<EOF
262 expect Password for
263 send ${USERPASS}\n
264 expect Password expired.  You must change it now.
265 expect Enter new password
266 send ${NEWUSERPASS}\n
267 expect Enter it again
268 send ${NEWUSERPASS}\n
269 EOF
270
271 testit "kinit (MIT) with user password for expired password" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit ${TEST_USER}@$REALM || failed=$(expr $failed + 1)
272 test_smbclient "Test login with user kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
273
274 USERPASS=$NEWUSERPASS
275 cat >$PREFIX/tmpkinituserpassscript <<EOF
276 expect Password for
277 send ${USERPASS}\n
278 EOF
279
280 testit "kinit with user password" $samba_texpect $PREFIX/tmpkinituserpassscript $samba_kinit ${TEST_USER}@$REALM || failed=$(expr $failed + 1)
281 test_smbclient "Test login with user kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
282
283 ###########################################################
284 ### Test login with lowercase realm
285 ###########################################################
286
287 KRB5CCNAME_PATH="$PREFIX/tmpccache"
288 KRB5CCNAME="FILE:$KRB5CCNAME_PATH"
289 export KRB5CCNAME
290
291 rm -rf $KRB5CCNAME_PATH
292
293 lowerrealm=$(echo $REALM | tr '[A-Z]' '[a-z]')
294 test_smbclient "Test login with user kerberos lowercase realm" 'ls' --use-kerberos=required -U${TEST_USER}@$lowerrealm%$NEWUSERPASS || failed=$(expr $failed + 1)
295 test_smbclient "Test login with user kerberos lowercase realm 2" 'ls' --use-kerberos=required -U${TEST_USER}@$REALM%$NEWUSERPASS --realm=$lowerrealm || failed=$(expr $failed + 1)
296
297 testit "del user with kerberos ccache" $VALGRIND $PYTHON $samba_tool user delete ${TEST_USER} $CONFIGURATION --use-krb5-ccache="${KRB5CCNAME}" "$@" || failed=$(expr $failed + 1)
298
299 ###########################################################
300 ### Test login with machine account
301 ###########################################################
302
303 rm -f $KRB5CCNAME_PATH
304 testit "kinit with machineaccountccache script" $PYTHON $machineaccountccache $CONFIGURATION $KRB5CCNAME || failed=$(expr $failed + 1)
305 test_smbclient "Test machine account login with kerberos ccache" 'ls' --use-krb5-ccache=$KRB5CCNAME || failed=$(expr $failed + 1)
306
307 testit "reset password policies" $VALGRIND $PYTHON $samba_tool domain passwordsettings set $ADMIN_LDBMODIFY_CONFIG --complexity=default --history-length=default --min-pwd-length=default --min-pwd-age=default --max-pwd-age=default || failed=$(expr $failed + 1)
308
309 ###########################################################
310 ### Test basic s4u2self request
311 ###########################################################
312
313 # Use previous acquired machine creds to request a ticket for self.
314 # We expect it to fail for now.
315 MACHINE_ACCOUNT="$(hostname -s | tr [a-z] [A-Z])\$@$REALM"
316 $samba_kvno -U$MACHINE_ACCOUNT $MACHINE_ACCOUNT
317 # But we expect the KDC to be up and running still
318 testit "kinit with machineaccountccache after s4u2self" $machineaccountccache $CONFIGURATION $KRB5CCNAME || failed=$(expr $failed + 1)
319
320 ### Cleanup
321
322 $samba_kdestroy
323
324 rm -f $KRB5CCNAME_PATH
325 rm -f $PREFIX/tmpkinituserpassscript
326 rm -f $PREFIX/tmpkinitscript
327 rm -f $PREFIX/tmpkpasswdscript
328 exit $failed