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