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