summaryrefslogtreecommitdiffstats
path: root/testprogs/blackbox/test_kinit.sh
blob: d9fb6c4808bc3449b92d148f85b1f4744ba5cdab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/bin/sh
# Blackbox tests for kinit and kerberos integration with smbclient etc
# Copyright (c) Andreas Schneider <asn@samba.org>
# Copyright (C) 2006-2007 Jelmer Vernooij <jelmer@samba.org>
# Copyright (C) 2006-2008 Andrew Bartlett <abartlet@samba.org>

if [ $# -lt 8 ]; then
	cat <<EOF
Usage: test_kinit.sh SERVER USERNAME PASSWORD REALM DOMAIN PREFIX SMBCLIENT CONFIGURATION
EOF
	exit 1
fi

SERVER=$1
USERNAME=$2
PASSWORD=$3
REALM=$4
DOMAIN=$5
PREFIX=$6
smbclient=$7
CONFIGURATION="${8}"
shift 8
failed=0

. "$(dirname "${0}")/subunit.sh"
. "$(dirname "${0}")/common_test_fns.inc"

samba_bindir="$BINDIR"
samba_srcdir="$SRCDIR/source4"
samba_kinit=$(system_or_builddir_binary kinit "${BINDIR}" samba4kinit)
samba_kpasswd=$(system_or_builddir_binary kpasswd "${BINDIR}" samba4kpasswd)
samba_kvno=$(system_or_builddir_binary kvno "${BINDIR}" samba4kvno)

samba_tool="${samba_bindir}/samba-tool"
samba_texpect="${samba_bindir}/texpect"

samba_enableaccount="${samba_tool} user enable"
machineaccountccache="${samba_srcdir}/scripting/bin/machineaccountccache"

ldbmodify=$(system_or_builddir_binary ldbmodify "${BINDIR}")
ldbsearch=$(system_or_builddir_binary ldbsearch "${BINDIR}")

kbase="$(basename "${samba_kinit}")"
if [ "${kbase}" = "samba4kinit" ]; then
	# HEIMDAL
	OPTION_RENEWABLE="--renewable"
	OPTION_RENEW_TICKET="--renew"
	OPTION_ENTERPRISE_NAME="--enterprise"
	OPTION_CANONICALIZATION=""
	OPTION_WINDOWS="--windows"
	OPTION_SERVICE="-S"
else
	# MIT
	OPTION_RENEWABLE="-r 1h"
	OPTION_RENEW_TICKET="-R"
	OPTION_ENTERPRISE_NAME="-E"
	OPTION_CANONICALIZATION="-C"
	OPTION_WINDOWS=""
	OPTION_SERVICE="-S"
fi

TEST_USER="$(mktemp -u kinittest-XXXXXX)"
UNC="//${SERVER}/tmp"

ADMIN_LDBMODIFY_CONFIG="-H ldap://${SERVER} -U${USERNAME}%${PASSWORD}"
export ADMIN_LDBMODIFY_CONFIG

KRB5CCNAME_PATH="${PREFIX}/tmpccache"
KRB5CCNAME="FILE:${KRB5CCNAME_PATH}"
export KRB5CCNAME
rm -rf "${KRB5CCNAME_PATH}"

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=$((failed + 1))

###########################################################
### Test kinit defaults
###########################################################

testit "kinit with password (initial)" \
	kerberos_kinit "${samba_kinit}" "${USERNAME}@${REALM}" "${PASSWORD}" \
	"${OPTION_RENEWABLE}" || \
	failed=$((failed + 1))
test_smbclient "Test login with user kerberos ccache" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

testit "kinit renew ticket (initial)" \
	"${samba_kinit}" ${OPTION_RENEW_TICKET} || \
	failed=$((failed + 1))

test_smbclient "Test login with kerberos ccache (initial)" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

###########################################################
### Test kinit with enterprise principal
###########################################################

testit "kinit with password (enterprise style)" \
	kerberos_kinit "${samba_kinit}" \
	"${USERNAME}@${REALM}" "${PASSWORD}" "${OPTION_ENTERPRISE_NAME}" \
	"${OPTION_RENEWABLE}" || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos ccache (enterprise style)" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

testit "kinit renew ticket (enterprise style)" \
	"${samba_kinit}" ${OPTION_RENEW_TICKET} || \
	failed=$((failed + 1))

test_smbclient "Test login with kerberos ccache (enterprise style)" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

###########################################################
### Tests with kinit windows
###########################################################

# HEIMDAL ONLY
if [ "${kbase}" = "samba4kinit" ]; then
	testit "kinit with password (windows style)" \
		kerberos_kinit "${samba_kinit}" \
		"${USERNAME}@${REALM}" "${PASSWORD}" \
		"${OPTION_RENEWABLE}" "${OPTION_WINDOWS}" || \
		failed=$((failed + 1))

	test_smbclient "Test login with kerberos ccache (windows style)" \
		"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
		failed=$((failed + 1))

	testit "kinit renew ticket (windows style)" \
		"${samba_kinit}" ${OPTION_RENEW_TICKET} || \
		failed=$((failed + 1))

	test_smbclient "Test login with kerberos ccache (windows style)" \
		"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
		failed=$((failed + 1))

	rm -f "${KRB5CCNAME_PATH}"
fi # HEIMDAL ONLY

###########################################################
### Tests with kinit default again
###########################################################

testit "kinit with password (default)" \
	kerberos_kinit "${samba_kinit}" "${USERNAME}@${REALM}" "${PASSWORD}" || \
	failed=$((failed + 1))

testit "check time with kerberos ccache (default)" \
	"${VALGRIND}" "${PYTHON}" "${samba_tool}" time "${SERVER}" \
	"${CONFIGURATION}" --use-krb5-ccache="${KRB5CCNAME}" "$@" || \
	failed=$((failed + 1))

USERPASS="testPass@12%"

testit "add user with kerberos ccache" \
	"${VALGRIND}" "${PYTHON}" "${samba_tool}" user create \
	"${TEST_USER}" "${USERPASS}" \
	"${CONFIGURATION}" --use-krb5-ccache="${KRB5CCNAME}" "$@" || \
	failed=$((failed + 1))

echo "Getting defaultNamingContext"
BASEDN=$(${ldbsearch} --basedn='' -H "ldap://${SERVER}" --scope=base \
	DUMMY=x defaultNamingContext | awk '/defaultNamingContext/ {print $2}')


TEST_UPN="$(mktemp -u test-XXXXXX)@${REALM}"
cat >"${PREFIX}/tmpldbmodify" <<EOF
dn: cn=${TEST_USER},cn=users,${BASEDN}
changetype: modify
add: servicePrincipalName
servicePrincipalName: host/${TEST_USER}
replace: userPrincipalName
userPrincipalName: ${TEST_UPN}
EOF

testit "modify servicePrincipalName and userPrincpalName" \
	"${VALGRIND}" "${ldbmodify}" -H "ldap://${SERVER}" "${PREFIX}/tmpldbmodify" \
	--use-krb5-ccache="${KRB5CCNAME}" "$@" || \
	failed=$((failed + 1))

testit "set user password with kerberos ccache" \
	"${VALGRIND}" "${PYTHON}" "${samba_tool}" user setpassword "${TEST_USER}" \
	--newpassword="${USERPASS}" "${CONFIGURATION}" \
	--use-krb5-ccache="${KRB5CCNAME}" "$@" || \
	failed=$((failed + 1))

testit "enable user with kerberos cache" \
	"${VALGRIND}" "${PYTHON}" "${samba_enableaccount}" "${TEST_USER}" \
	-H "ldap://$SERVER" --use-krb5-ccache="${KRB5CCNAME}" "$@" || \
	failed=$((failed + 1))

testit "kinit with new user password" \
	kerberos_kinit "${samba_kinit}" "${TEST_USER}" "${USERPASS}" || \
	failed=$((failed + 1))

test_smbclient "Test login with new user kerberos ccache" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

###########################################################
### Test kinit after changing password with samba-tool
###########################################################

NEW_USERPASS="testPaSS@34%"
testit "change user password with 'samba-tool user password' (rpc)" \
	"${VALGRIND}" "${PYTHON}" "${samba_tool}" user password \
	-W"${DOMAIN}" -U"${TEST_USER}%${USERPASS}" "${CONFIGURATION}" \
	--newpassword="${NEW_USERPASS}" \
	--use-kerberos=off "$@" || \
	failed=$((failed + 1))

testit "kinit with user password (after rpc password change)" \
	kerberos_kinit "${samba_kinit}" \
	"${TEST_USER}@${REALM}" "${NEW_USERPASS}" || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos (after rpc password change)" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

USERPASS="${NEW_USERPASS}"

rm -f "${KRB5CCNAME_PATH}"

###########################################################
### Test kinit with UPN
###########################################################

testit "kinit with new (NT-Principal style) using UPN" \
	kerberos_kinit "${samba_kinit}" "${TEST_UPN}" "${USERPASS}" || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos ccache from NT UPN" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

testit "kinit with new (enterprise style) using UPN" \
	kerberos_kinit "${samba_kinit}" "${TEST_UPN}" "${USERPASS}" \
	${OPTION_ENTERPRISE_NAME} || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos ccache from enterprise UPN" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

# HEIMDAL ONLY
if [ "${kbase}" = "samba4kinit" ]; then
	testit "kinit with new (windows style) using UPN" \
		kerberos_kinit "${samba_kinit}" "${TEST_UPN}" "${USERPASS}" \
		${OPTION_WINDOWS} || \
		failed=$((failed + 1))

	test_smbclient "Test login with user kerberos ccache with (windows style) UPN" \
		"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
		failed=$((failed + 1))

	rm -f "${KRB5CCNAME_PATH}"
fi # HEIMDAL ONLY

###########################################################
### Tests with SPN
###########################################################

DNSDOMAIN=$(echo "${REALM}" | tr '[:upper:]' '[:lower:]')
testit "kinit with password (SPN)" \
	kerberos_kinit "${samba_kinit}" \
	"http/testupnspn.${DNSDOMAIN}" "${PASSWORD}" || \
	failed=$((failed + 1))

test_smbclient "Test login with kerberos ccache (SPN)" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

###########################################################
### Test kinit with canonicalization
###########################################################

upperusername=$(echo "${USERNAME}" | tr '[:lower:]' '[:upper:]')
testit "kinit with canonicalize and service" \
	kerberos_kinit "${samba_kinit}" "${upperusername}@${REALM}" "${PASSWORD}" \
	${OPTION_CANONICALIZATION} \
	${OPTION_SERVICE} "kadmin/changepw@${REALM}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

###########################################################
### Test kinit with user credentials and changed realm
###########################################################

testit "kinit with password (default)" \
	kerberos_kinit "${samba_kinit}" "${USERNAME}@${REALM}" "${PASSWORD}" || \
	failed=$((failed + 1))

cat >"${PREFIX}/tmpldbmodify" <<EOF
dn: cn=${TEST_USER},cn=users,$BASEDN
changetype: modify
replace: userPrincipalName
userPrincipalName: ${TEST_UPN}.org
EOF

testit "modify userPrincipalName to be a different domain" \
	"${VALGRIND}" "${ldbmodify}" "${ADMIN_LDBMODIFY_CONFIG}" \
	"${PREFIX}/tmpldbmodify" "${PREFIX}/tmpldbmodify" \
	--use-krb5-ccache="${KRB5CCNAME}" "$@" || \
	failed=$((failed + 1))

testit "kinit with new (enterprise style) using UPN" \
	kerberos_kinit "${samba_kinit}" "${TEST_UPN}.org" "${USERPASS}" \
	${OPTION_ENTERPRISE_NAME} || failed=$((failed + 1))

test_smbclient "Test login with user kerberos ccache from enterprise UPN" \
	"ls" "${UNC}" \
	--use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

###########################################################
### Test password change with kpasswd
###########################################################

testit "kinit with user password" \
	kerberos_kinit "${samba_kinit}" "${TEST_USER}@$REALM" "${USERPASS}" || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos ccache" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

NEWUSERPASS=testPaSS@56%

if [ "${kbase}" = "samba4kinit" ]; then
	# HEIMDAL
	cat >"${PREFIX}/tmpkpasswdscript" <<EOF
expect Password
password ${USERPASS}\n
expect New password
send ${NEWUSERPASS}\n
expect Verify password
send ${NEWUSERPASS}\n
expect Success
EOF

else
	# MIT
	cat >"${PREFIX}/tmpkpasswdscript" <<EOF
expect Password for
password ${USERPASS}\n
expect Enter new password
send ${NEWUSERPASS}\n
expect Enter it again
send ${NEWUSERPASS}\n
expect Password changed
EOF
fi

testit "change user password with kpasswd" \
	"${samba_texpect}" "${PREFIX}/tmpkpasswdscript" \
	"${samba_kpasswd}" "${TEST_USER}@$REALM" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

USERPASS="${NEWUSERPASS}"

testit "kinit with user password (after kpasswd)" \
	kerberos_kinit "${samba_kinit}" \
	"${TEST_USER}@${REALM}" "${USERPASS}" || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos ccache (after kpasswd)" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"

###########################################################
### TODO Test set password with kpasswd
###########################################################

# This is not implemented in kpasswd

###########################################################
### Test password expiry
###########################################################

	cat >"${PREFIX}/tmpldbmodify" <<EOF
dn: cn=${TEST_USER},cn=users,${BASEDN}
changetype: modify
replace: pwdLastSet
pwdLastSet: 0
EOF

	NEWUSERPASS=testPaSS@78%

	testit "modify pwdLastSet" \
		"${VALGRIND}" "${ldbmodify}" "${ADMIN_LDBMODIFY_CONFIG}" \
		"${PREFIX}/tmpldbmodify" "${PREFIX}/tmpldbmodify" \
		--use-krb5-ccache="${KRB5CCNAME}" "$@" || \
		failed=$((failed + 1))

if [ "${kbase}" = "samba4kinit" ]; then
	# HEIMDAL branch
	cat >"${PREFIX}/tmpkinituserpassscript" <<EOF
expect ${TEST_USER}@$REALM's Password
send ${USERPASS}\n
expect Password has expired
expect New password
send ${NEWUSERPASS}\n
expect Repeat new password
send ${NEWUSERPASS}\n
EOF
else
	# MIT branch
	cat >"${PREFIX}/tmpkinituserpassscript" <<EOF
expect Password for
send ${USERPASS}\n
expect Password expired.  You must change it now.
expect Enter new password
send ${NEWUSERPASS}\n
expect Enter it again
send ${NEWUSERPASS}\n
EOF

fi # END MIT ONLY

testit "kinit with user password for expired password" \
	"${samba_texpect}" "$PREFIX/tmpkinituserpassscript" \
	"${samba_kinit}" "${TEST_USER}@$REALM" || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos ccache" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

USERPASS="${NEWUSERPASS}"

testit "kinit with user password" \
	kerberos_kinit "${samba_kinit}" \
	"${TEST_USER}@${REALM}" "${USERPASS}" || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos ccache" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

###########################################################
### Test login with lowercase realm
###########################################################

KRB5CCNAME_PATH="$PREFIX/tmpccache"
KRB5CCNAME="FILE:$KRB5CCNAME_PATH"
export KRB5CCNAME

rm -rf "${KRB5CCNAME_PATH}"

testit "kinit with user password" \
	kerberos_kinit "${samba_kinit}" "${TEST_USER}@${REALM}" "${USERPASS}" || \
	failed=$((failed + 1))

lowerrealm=$(echo "${REALM}" | tr '[:upper:]' '[:lower:]')
test_smbclient "Test login with user kerberos lowercase realm" \
	"ls" "${UNC}" --use-kerberos=required \
	-U"${TEST_USER}@${lowerrealm}%${NEWUSERPASS}" || \
	failed=$((failed + 1))

test_smbclient "Test login with user kerberos lowercase realm 2" \
	"ls" "${UNC}" --use-kerberos=required \
	-U"${TEST_USER}@${REALM}%${NEWUSERPASS}" --realm="${lowerrealm}" || \
	failed=$((failed + 1))

testit "del user with kerberos ccache" \
	"${VALGRIND}" "${PYTHON}" "${samba_tool}" user delete \
	"${TEST_USER}" "${CONFIGURATION}" \
	--use-krb5-ccache="${KRB5CCNAME}" "$@" || \
	failed=$((failed + 1))

###########################################################
### Test login with machine account
###########################################################

rm -f "${KRB5CCNAME_PATH}"

testit "kinit with machineaccountccache script" \
	"${PYTHON}" "${machineaccountccache}" "${CONFIGURATION}" \
	"${KRB5CCNAME}" || \
	failed=$((failed + 1))

test_smbclient "Test machine account login with kerberos ccache" \
	"ls" "${UNC}" --use-krb5-ccache="${KRB5CCNAME}" || \
	failed=$((failed + 1))

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=$((failed + 1))

###########################################################
### Test basic s4u2self request
###########################################################

# MIT ONLY
if [ "${kbase}" = "kinit" ]; then

# Use previous acquired machine creds to request a ticket for self.
# We expect it to fail for now.
MACHINE_ACCOUNT="$(hostname -s | tr '[:lower:]' '[:upper:]')\$@${REALM}"

${samba_kvno} -U"${MACHINE_ACCOUNT}" "${MACHINE_ACCOUNT}"

# But we expect the KDC to be up and running still
testit "kinit with machineaccountccache after s4u2self" \
	"${machineaccountccache}" "${CONFIGURATION}" "${KRB5CCNAME}" || \
	failed=$((failed + 1))

fi # END MIT ONLY

### Cleanup

rm -f "${KRB5CCNAME_PATH}"
rm -f "${PREFIX}/tmpkinituserpassscript"
rm -f "${PREFIX}/tmpkinitscript"
rm -f "${PREFIX}/tmpkpasswdscript"

exit $failed