Debian Codegraph @

ci/kdc-setup-mit

raw
1 #!/bin/sh
2 #
3 # Build a Kerberos test realm for MIT Kerberos
4 #
5 # This script automates the process of setting up a Kerberos test realm from
6 # scratch suitable for testing pam-krb5. It is primarily intended to be run
7 # from inside CI in a VM or container from the top of the pam-krb5 source
8 # tree, and must be run as root. It expects to be operating on the Debian
9 # MIT Kerberos package.
10 #
11 # Copyright 2014, 2020 Russ Allbery <[email protected]>
12 #
13 # SPDX-License-Identifier: MIT
14
15 set -eux
16
17 # Install the KDC and the OpenSSL command line tool.
18 apt-get install krb5-admin-server krb5-kdc krb5-pkinit openssl
19
20 # Install its configuration files.
21 cp ci/files/mit/extensions.client /etc/krb5kdc/extensions.client
22 cp ci/files/mit/extensions.kdc /etc/krb5kdc/extensions.kdc
23 cp ci/files/mit/kadm5.acl /etc/krb5kdc/kadm5.acl
24 cp ci/files/mit/kdc.conf /etc/krb5kdc/kdc.conf
25 cp ci/files/mit/krb5.conf /etc/krb5.conf
26
27 # Add domain-realm mappings for the local host, since otherwise Heimdal and
28 # MIT Kerberos may attempt to discover the realm of the local domain, and the
29 # DNS server for GitHub Actions has a habit of just not responding and causing
30 # the test to hang.
31 cat <<EOF >>/etc/krb5.conf
32 [domain_realm]
33 $(hostname -f) = MIT.TEST
34 EOF
35
36 # Create the basic KDC.
37 kdb5_util create -s -P 'this is a test master database password'
38
39 # Create and store the keytabs.
40 kadmin.local -q 'add_principal +requires_preauth -randkey test/[email protected]'
41 kadmin.local -q 'ktadd -k tests/config/admin-keytab test/[email protected]'
42 kadmin.local -q 'add_principal +requires_preauth -randkey test/[email protected]'
43 kadmin.local -q 'ktadd -k tests/config/keytab test/[email protected]'
44
45 # Enable anonymous PKINIT.
46 kadmin.local -q 'addprinc -randkey WELLKNOWN/ANONYMOUS'
47
48 # Create a user principal with a known password.
49 password="iceedKaicVevjunwiwyd"
50 kadmin.local -q \
51 "add_principal +requires_preauth -pw $password [email protected]"
52 echo '[email protected]' >tests/config/password
53 echo "$password" >>tests/config/password
54
55 # Create the root CA for PKINIT.
56 openssl genrsa -out /etc/krb5kdc/cakey.pem 2048
57 openssl req -key /etc/krb5kdc/cakey.pem -new -x509 \
58 -out /etc/krb5kdc/cacert.pem -subj "/CN=MIT.TEST CA" -days 3650
59 chmod 755 /etc/krb5kdc
60 chmod 644 /etc/krb5kdc/cacert.pem
61
62 # Create the certificate for the MIT Kerberos KDC.
63 openssl genrsa -out /var/lib/krb5kdc/kdckey.pem 2048
64 openssl req -new -out /var/lib/krb5kdc/kdc.req \
65 -key /var/lib/krb5kdc/kdckey.pem -subj "/CN=MIT.TEST"
66 REALM=MIT.TEST openssl x509 -req -in /var/lib/krb5kdc/kdc.req \
67 -CAkey /etc/krb5kdc/cakey.pem -CA /etc/krb5kdc/cacert.pem \
68 -out /var/lib/krb5kdc/kdc.pem -days 365 \
69 -extfile /etc/krb5kdc/extensions.kdc -extensions kdc_cert \
70 -CAcreateserial
71 rm /var/lib/krb5kdc/kdc.req
72
73 # Create the certificate for the MIT Kerberos client.
74 openssl genrsa -out clientkey.pem 2048
75 openssl req -new -key clientkey.pem -out client.req \
76 -subj "/[email protected]"
77 REALM="MIT.TEST" CLIENT="testuser" openssl x509 \
78 -CAkey /etc/krb5kdc/cakey.pem -CA /etc/krb5kdc/cacert.pem -req \
79 -in client.req -extensions client_cert \
80 -extfile /etc/krb5kdc/extensions.client -days 365 -out client.pem
81 cat client.pem clientkey.pem >tests/config/pkinit-cert
82 rm clientkey.pem client.pem client.req
83 echo '[email protected]' >tests/config/pkinit-principal
84
85 # Fix permissions on all the newly-created files.
86 chmod 644 tests/config/*
87
88 # Restart the MIT Kerberos KDC and services.
89 systemctl stop krb5-kdc krb5-admin-server
90 systemctl start krb5-kdc krb5-admin-server
91
92 # Ensure that the KDC is running.
93 for n in $(seq 1 5); do
94 if echo "$password" | kinit [email protected]; then
95 break
96 fi
97 sleep 1
98 done
99 klist
100 kdestroy
101 kinit -n @MIT.TEST
102 kinit -X X509_user_identity=FILE:tests/config/pkinit-cert [email protected]