| 1 |
#!/bin/bash |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
set -eu |
| 5 |
|
| 6 |
# shellcheck disable=SC2034 # prefix= is set because the default @sysconfdir@ contains "${prefix}" |
| 7 |
prefix="@prefix@" |
| 8 |
COCKPIT_CONFIG="@sysconfdir@/cockpit" |
| 9 |
COCKPIT_WS_CERTS_D="${COCKPIT_CONFIG}/ws-certs.d" |
| 10 |
COCKPIT_RUNTIME_DIR="/run/cockpit" |
| 11 |
|
| 12 |
install_cert() { |
| 13 |
local destination="${COCKPIT_WS_CERTS_D}/$1" |
| 14 |
mv -Z "$1" "${destination}" |
| 15 |
|
| 16 |
# The certificate should be world-readable |
| 17 |
chmod a+r "${destination}" |
| 18 |
|
| 19 |
# Force flush to disk for embedded devices |
| 20 |
sync "${destination}" |
| 21 |
} |
| 22 |
|
| 23 |
install_key() { |
| 24 |
local destination="${COCKPIT_WS_CERTS_D}/$1" |
| 25 |
mv -Z "$1" "${destination}" |
| 26 |
|
| 27 |
# Force flush to disk for embedded devices |
| 28 |
sync "${destination}" |
| 29 |
} |
| 30 |
|
| 31 |
selfsign_sscg() { |
| 32 |
sscg --quiet \ |
| 33 |
--lifetime "${DAYS}" \ |
| 34 |
--cert-key-file "${KEYFILE}" \ |
| 35 |
--cert-file "${CERTFILE}" \ |
| 36 |
--ca-file "${CA_FILE}" \ |
| 37 |
--hostname "${HOSTNAME}" \ |
| 38 |
--organization "${MACHINE_ID:-unspecified}" \ |
| 39 |
--subject-alt-name localhost \ |
| 40 |
--subject-alt-name IP:127.0.0.1/255.255.255.255 |
| 41 |
} |
| 42 |
|
| 43 |
selfsign_openssl() { |
| 44 |
openssl req -x509 \ |
| 45 |
-days "${DAYS}" \ |
| 46 |
-newkey rsa \ |
| 47 |
-keyout "${KEYFILE}" \ |
| 48 |
-keyform PEM \ |
| 49 |
-nodes \ |
| 50 |
-out "${CERTFILE}" \ |
| 51 |
-outform PEM \ |
| 52 |
-subj "${MACHINE_ID:+/O=${MACHINE_ID}}/CN=${HOSTNAME}" \ |
| 53 |
-addext "subjectAltName=IP:127.0.0.1,DNS:localhost" \ |
| 54 |
-addext "basicConstraints = critical,CA:TRUE" \ |
| 55 |
-addext "keyUsage = critical,digitalSignature,cRLSign,keyCertSign,keyEncipherment,keyAgreement" \ |
| 56 |
-addext "extendedKeyUsage = serverAuth" |
| 57 |
} |
| 58 |
|
| 59 |
cmd_selfsign() { |
| 60 |
# Common variables used by both methods |
| 61 |
local MACHINE_ID |
| 62 |
if [ -e /etc/machine-id ]; then |
| 63 |
MACHINE_ID="$(tr -d -c '[:xdigit:]' < /etc/machine-id)" |
| 64 |
fi |
| 65 |
local HOSTNAME="${HOSTNAME:-$(hostname)}" |
| 66 |
local CERTFILE="0-self-signed.cert" |
| 67 |
local KEYFILE="0-self-signed.key" |
| 68 |
local CA_FILE="0-self-signed-ca.pem" |
| 69 |
|
| 70 |
# do not stomp over a partial key -- the admin tried to do something wrong |
| 71 |
if [ -e "${COCKPIT_WS_CERTS_D}/${KEYFILE}" ] && [ ! -e "${COCKPIT_WS_CERTS_D}/${CERTFILE}" ]; then |
| 72 |
echo "Error: Found $KEYFILE but no $CERTFILE. Please remove the key file first." >&2 |
| 73 |
exit 1 |
| 74 |
fi |
| 75 |
|
| 76 |
mkdir -pZ "$COCKPIT_WS_CERTS_D" |
| 77 |
|
| 78 |
# We renew certificates up to 30 days before expiry, so give ourselves a |
| 79 |
# year, plus 30 days. The maximum is variously mentioned to be 397 or 398. |
| 80 |
local DAYS=395 |
| 81 |
|
| 82 |
# If sscg fails, try openssl |
| 83 |
selfsign_sscg || selfsign_openssl |
| 84 |
|
| 85 |
# Install the files and set permissions ($CA_FILE is only created by sscg) |
| 86 |
test ! -e "${CA_FILE}" || install_cert "${CA_FILE}" |
| 87 |
install_cert "${CERTFILE}" |
| 88 |
install_key "${KEYFILE}" |
| 89 |
} |
| 90 |
|
| 91 |
cmd_ipa_request() { |
| 92 |
local USER="$1" |
| 93 |
|
| 94 |
# IPA operations require auth; read password from stdin to avoid quoting issues |
| 95 |
# if kinit fails, we can't handle this setup, exit cleanly |
| 96 |
kinit "${USER}@${REALM}" || exit 0 |
| 97 |
|
| 98 |
# ensure this gets run with a non-C locale; ipa fails otherwise |
| 99 |
if [ "$(sh -c 'eval `locale`; echo $LC_CTYPE')" = 'C' ]; then |
| 100 |
export LC_CTYPE=C.UTF-8 |
| 101 |
fi |
| 102 |
|
| 103 |
# create a kerberos Service Principal Name for cockpit-ws, unless already present |
| 104 |
ipa service-show "${SERVICE}" || \ |
| 105 |
ipa service-add --ok-as-delegate=true --ok-to-auth-as-delegate=true --force "${SERVICE}" |
| 106 |
|
| 107 |
# add cockpit-ws key, unless already present |
| 108 |
klist -k "${KEYTAB}" | grep -qF "${SERVICE}" || \ |
| 109 |
ipa-getkeytab -p "HTTP/${HOST}" -k "${KEYTAB}" |
| 110 |
|
| 111 |
# request the certificate and put it into our certificate directory, so that auto-refresh works |
| 112 |
mkdir -pZ "$COCKPIT_WS_CERTS_D" |
| 113 |
ipa-getcert request -f "${COCKPIT_WS_CERTS_D}/10-ipa.cert" -k "${COCKPIT_WS_CERTS_D}/10-ipa.key" -K "HTTP/${HOST}" -m 640 -o root:root -M 644 -w -v |
| 114 |
} |
| 115 |
|
| 116 |
cmd_ipa_cleanup() { |
| 117 |
# clean up keytab |
| 118 |
if [ -e "${KEYTAB}" ]; then |
| 119 |
ipa-rmkeytab -k "${KEYTAB}" -p "${SERVICE}" |
| 120 |
fi |
| 121 |
|
| 122 |
# clean up certificate; support both "copy" and "direct" modes from cmd_ipa_request() |
| 123 |
if [ -e "${COCKPIT_WS_CERTS_D}/10-ipa.key" ]; then |
| 124 |
rm "${COCKPIT_WS_CERTS_D}/10-ipa.cert" "${COCKPIT_WS_CERTS_D}/10-ipa.key" |
| 125 |
ipa-getcert stop-tracking -f "${COCKPIT_WS_CERTS_D}/10-ipa.cert" -k "${COCKPIT_WS_CERTS_D}/10-ipa.key" || \ |
| 126 |
ipa-getcert stop-tracking -f /run/cockpit/certificate-helper/10-ipa.cert -k /run/cockpit/certificate-helper/10-ipa.key |
| 127 |
fi |
| 128 |
} |
| 129 |
|
| 130 |
cmd_ipa() { |
| 131 |
local REALM="$2" |
| 132 |
|
| 133 |
local HOST |
| 134 |
HOST="$(hostname -f)" |
| 135 |
local SERVICE="HTTP/${HOST}@${REALM}" |
| 136 |
local KEYTAB="${COCKPIT_CONFIG}/krb5.keytab" |
| 137 |
|
| 138 |
# use a temporary keytab to avoid interfering with the system one |
| 139 |
export KRB5CCNAME=/run/cockpit/keytab-setup |
| 140 |
|
| 141 |
# not an IPA setup? cannot handle this |
| 142 |
if [ -z "$(which ipa)" ]; then |
| 143 |
echo 'ipa must be installed for this command' |
| 144 |
exit 1 |
| 145 |
fi |
| 146 |
|
| 147 |
case "$1" in |
| 148 |
request) |
| 149 |
cmd_ipa_request "$3" |
| 150 |
;; |
| 151 |
cleanup) |
| 152 |
cmd_ipa_cleanup |
| 153 |
;; |
| 154 |
*) |
| 155 |
echo 'unknown subcommand' |
| 156 |
exit 1 |
| 157 |
;; |
| 158 |
esac |
| 159 |
} |
| 160 |
|
| 161 |
main() { |
| 162 |
# ipa-getkeytab needs root to create the file, same for cert installation |
| 163 |
if [ "$(id -u)" != "0" ]; then |
| 164 |
echo 'must be run as root' |
| 165 |
exit 1 |
| 166 |
fi |
| 167 |
|
| 168 |
# Create a private working directory |
| 169 |
mkdir -p "${COCKPIT_RUNTIME_DIR}" |
| 170 |
WORKDIR="${COCKPIT_RUNTIME_DIR}/certificate-helper" |
| 171 |
mkdir -m 700 "${WORKDIR}" # we expect that not to have existed |
| 172 |
trap 'exit' INT QUIT PIPE TERM |
| 173 |
trap 'rm -rf "${WORKDIR}"' EXIT |
| 174 |
cd "${WORKDIR}" |
| 175 |
|
| 176 |
# Dispatch subcommand |
| 177 |
case "$1" in |
| 178 |
selfsign) |
| 179 |
cmd_selfsign |
| 180 |
;; |
| 181 |
ipa) |
| 182 |
shift |
| 183 |
cmd_ipa "$@" |
| 184 |
;; |
| 185 |
*) |
| 186 |
echo 'unknown subcommand' |
| 187 |
exit 1 |
| 188 |
;; |
| 189 |
esac |
| 190 |
} |
| 191 |
|
| 192 |
main "$@" |