summaryrefslogtreecommitdiffstats
path: root/testprogs/blackbox/test_pkinit_simple.sh
blob: 629bfad610a24afbeafbc2e0aa4a6d6bc9376b53 (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
#!/bin/sh
# Blackbox tests for kinit and kerberos integration with smbclient etc
#
# Copyright (C) 2006-2007 Jelmer Vernooij <jelmer@samba.org>
# Copyright (C) 2006-2008 Andrew Bartlett <abartlet@samba.org>
# Copyright (C) 2022      Andreas Schneider <asn@samba.org>

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

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

samba_bindir="${BINDIR}"

samba_tool="${PYTHON} ${samba_bindir}/samba-tool"
wbinfo="${samba_bindir}/wbinfo"

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

samba_kinit=$(system_or_builddir_binary kinit "${BINDIR}" samba4kinit)

unc="//${SERVER}/tmp"

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

USER_PRINCIPAL_NAME="$(echo "${USERNAME}@${REALM}" | tr "[:upper:]" "[:lower:]")"

kbase="$(basename "${samba_kinit}")"
if [ "${kbase}" = "samba4kinit" ]; then
	# HEIMDAL
	X509_USER_IDENTITY="--pk-user=FILE:${PREFIX}/pkinit/USER-${USER_PRINCIPAL_NAME}-cert.pem,${PREFIX}/pkinit/USER-${USER_PRINCIPAL_NAME}-private-key.pem"
	OPTION_RENEWABLE="--renewable"
	OPTION_RENEW_TICKET="--renew"
	OPTION_ENTERPRISE_NAME="--enterprise"
else
	# MIT
	X509_USER_IDENTITY="-X X509_user_identity=FILE:${PREFIX}/pkinit/USER-${USER_PRINCIPAL_NAME}-cert.pem,${PREFIX}/pkinit/USER-${USER_PRINCIPAL_NAME}-private-key.pem"
	OPTION_RENEWABLE="-r 1h"
	OPTION_RENEW_TICKET="-R"
	OPTION_ENTERPRISE_NAME="-E"
fi
OPTION_REQUEST_PAC="--request-pac"

# STEP0:
# Now we set the UF_SMARTCARD_REQUIRED bit
# This means we have a normal enabled account *without* a known password
testit "STEP0 samba-tool user create ${USERNAME} --smartcard-required" \
	"${samba_tool}" user create "${USERNAME}" --smartcard-required ||
	failed=$((failed + 1))

testit_expect_failure "STEP1 kinit with password" \
	kerberos_kinit "${samba_kinit}" "${USERNAME}@${REALM}" "${PASSWORD}" \
	"${OPTION_REQUEST_PAC}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP1 Test login with NTLM" \
	"${smbclient}" "${unc}" -c 'ls' "-U${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP1 Test wbinfo with password" \
	"${wbinfo}" "--authenticate=$DOMAIN/$USERNAME%$PASSWORD" ||
	failed=$((failed + 1))

testit "STEP1 kinit with pkinit (name specified: ${USERNAME})" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${USERNAME}@${REALM}" ||
	failed=$((failed + 1))

testit "STEP1 kinit renew ticket (name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP1 Test login with kerberos ccache (name specified)" \
	'ls' "$unc" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

# OK
testit_expect_failure "STEP1 kinit with pkinit (wrong name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "not${USERNAME}@${REALM}" ||
	failed=$((failed + 1))

testit_expect_failure "STEP1 kinit with pkinit (wrong name specified 2)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${SERVER}@${REALM}" ||
	failed=$((failed + 1))

testit "STEP1 kinit with pkinit (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" \
	"${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit "STEP1 kinit renew ticket (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP1 Test login with kerberos ccache (enterprise name specified)" \
	'ls' "${unc}" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP1 kinit with pkinit (wrong enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" \
	"not${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP1 kinit with pkinit (wrong enterprise name specified 2)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" \
	"${SERVER}@${REALM}" ||
	failed=$((failed + 1))

testit "STEP1 kinit with pkinit (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" ||
	failed=$((failed + 1))
testit "STEP1 kinit renew ticket (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP1 Test login with kerberos ccache (enterprise name in cert)" \
	'ls' "${unc}" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

# STEP2:
# We still have UF_SMARTCARD_REQUIRED, but with a known password
testit "STEP2 samba-tool user setpassword ${USERNAME} --newpassword" \
	"${samba_tool}" user setpassword "${USERNAME}" \
	--newpassword="${PASSWORD}" ||
	failed=$((failed + 1))

testit_expect_failure "STEP2 kinit with password" \
	kerberos_kinit "${samba_kinit}" "${USERNAME}@${REALM}" "${PASSWORD}" \
	"${OPTION_REQUEST_PAC}" ||
	failed=$((failed + 1))
test_smbclient "STEP2 Test login with NTLM" \
	'ls' "$unc" -U"${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP2 Test wbinfo with password" \
	"${wbinfo}" --authenticate="${DOMAIN}/${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))

testit "STEP2 kinit with pkinit (name specified) " \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit "STEP2 kinit renew ticket (name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP2 Test login with kerberos ccache (name specified)" \
	'ls' "$unc" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

testit "STEP2 kinit with pkinit (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" \
	"${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit "STEP2 kinit renew ticket (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP2 Test login with kerberos ccache (enterprise name specified)" \
	'ls' "$unc" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

testit "STEP2 kinit with pkinit (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" ||
	failed=$((failed + 1))
testit "STEP2 kinit renew ticket (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP2 Test login with kerberos ccache (enterprise name in cert)" \
	'ls' "$unc" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

# STEP3:
# The account is a normal account without the UF_SMARTCARD_REQUIRED bit set
testit "STEP3 samba-tool user setpassword ${USERNAME} --clear-smartcard-required" \
	"${samba_tool}" user setpassword "${USERNAME}" \
	--newpassword="${PASSWORD}" --clear-smartcard-required ||
	failed=$((failed + 1))

testit "STEP3 kinit with password" \
	kerberos_kinit "${samba_kinit}" "${USERNAME}@${REALM}" "${PASSWORD}" \
	"${OPTION_REQUEST_PAC}" ||
	failed=$((failed + 1))
test_smbclient "STEP3 Test login with user kerberos ccache" \
	'ls' "$unc" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))
test_smbclient "STEP3 Test login with NTLM" \
	'ls' "$unc" -U"${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))
testit "STEP3 Test wbinfo with password" \
	"${wbinfo}" --authenticate="${DOMAIN}/${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))

testit "STEP3 kinit with pkinit (name specified) " \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit "STEP3 kinit renew ticket (name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP3 Test login with kerberos ccache (name specified)" \
	'ls' "${unc}" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

testit "STEP3 kinit with pkinit (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" \
	"${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit "STEP3 kinit renew ticket (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP3 Test login with kerberos ccache (enterprise name specified)" \
	'ls' "${unc}" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

testit "STEP3 kinit with pkinit (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" ||
	failed=$((failed + 1))
testit "STEP3 kinit renew ticket (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP3 Test login with kerberos ccache (enterprise name in cert)" \
	'ls' "${unc}" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

# STEP4:
# Now we set the UF_SMARTCARD_REQUIRED bit
# This means we have a normal enabled account *without* a known password
testit "STEP4 samba-tool user setpassword $USERNAME --smartcard-required" \
	"${samba_tool}" user setpassword "${USERNAME}" --smartcard-required ||
	failed=$((failed + 1))

testit_expect_failure "STEP4 kinit with password" \
	kerberos_kinit "${samba_kinit}" "${USERNAME}@${REALM}" "${PASSWORD}" \
	"${OPTION_REQUEST_PAC}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP4 Test login with NTLM" \
	"${smbclient}" "${unc}" -c 'ls' -U"${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP4 Test wbinfo with password" \
	"${wbinfo}" --authenticate="${DOMAIN}/${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))

testit "STEP4 kinit with pkinit (name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit "STEP4 kinit renew ticket (name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP4 Test login with kerberos ccache (name specified)" \
	'ls' "$unc" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

testit "STEP4 kinit with pkinit (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" \
	"${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit "STEP4 kinit renew ticket (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP4 Test login with kerberos ccache (enterprise name specified)" \
	'ls' "${unc}" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

testit "STEP4 kinit with pkinit (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" ||
	failed=$((failed + 1))
testit "STEP4 kinit renew ticket (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEW_TICKET}" ||
	failed=$((failed + 1))
test_smbclient "STEP4 Test login with kerberos ccache (enterprise name in cert)" \
	'ls' "${unc}" --use-krb5-ccache="${KRB5CCNAME}" ||
	failed=$((failed + 1))

# STEP5:
# disable the account
testit "STEP5 samba-tool user disable $USERNAME" \
	"${samba_tool}" user disable "${USERNAME}" ||
	failed=$((failed + 1))

testit_expect_failure "STEP5 kinit with password" \
	kerberos_kinit "${samba_kinit}" "${USERNAME}@${REALM}" "${PASSWORD}" \
	"${OPTION_REQUEST_PAC}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP5 Test login with NTLM" \
	"${smbclient}" "${unc}" -c 'ls' -U"${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP5 Test wbinfo with password" \
	"${wbinfo}" --authenticate="${DOMAIN}/${USERNAME}%${PASSWORD}" ||
	failed=$((failed + 1))

testit_expect_failure "STEP5 kinit with pkinit (name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP5 kinit with pkinit (enterprise name specified)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" \
	"${USERNAME}@${REALM}" ||
	failed=$((failed + 1))
testit_expect_failure "STEP5 kinit with pkinit (enterprise name in cert)" \
	"${samba_kinit}" "${OPTION_REQUEST_PAC}" "${OPTION_RENEWABLE}" \
	"${X509_USER_IDENTITY}" "${OPTION_ENTERPRISE_NAME}" ||
	failed=$((failed + 1))

# STEP6:
# cleanup
testit "STEP6 samba-tool user delete ${USERNAME}" \
	"${samba_tool}" user delete "${USERNAME}" ||
	failed=$((failed + 1))

rm -f "${KRB5CCNAME_PATH}"
exit ${failed}