f21cbd9
#!/usr/bin/bash
f21cbd9
set -eu -o pipefail
f21cbd9
# Detect existing non-conforming host keys and perform the permissions migration
f21cbd9
# https://fedoraproject.org/wiki/Changes/SSHKeySignSuidBit
f21cbd9
#
f21cbd9
# Example output looks like:
f21cbd9
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
f21cbd9
# @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
f21cbd9
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
f21cbd9
# Permissions 0640 for '/etc/ssh/ssh_host_rsa_key' are too open.
f21cbd9
# It is required that your private key files are NOT accessible by others.
f21cbd9
# This private key will be ignored.
f21cbd9
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
f21cbd9
# @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
f21cbd9
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
f21cbd9
# Permissions 0640 for '/etc/ssh/ssh_host_ecdsa_key' are too open.
f21cbd9
# It is required that your private key files are NOT accessible by others.
f21cbd9
# This private key will be ignored.
f21cbd9
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
f21cbd9
# @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
f21cbd9
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
f21cbd9
# Permissions 0640 for '/etc/ssh/ssh_host_ed25519_key' are too open.
f21cbd9
# It is required that your private key files are NOT accessible by others.
f21cbd9
# This private key will be ignored.
f21cbd9
# gsisshd: no hostkeys available -- exiting.
f21cbd9
#
f21cbd9
output="$(gsisshd -T 2>&1 || true)" # expected to fail
f21cbd9
while read line; do
f21cbd9
    if [[ $line =~ ^Permissions\ [0-9]+\ for\ \'(.*)\'\ are\ too\ open. ]]; then
f21cbd9
        keyfile=${BASH_REMATCH[1]}
f21cbd9
        echo $line
f21cbd9
        echo -e "\t-> changing permissions on $keyfile"
f21cbd9
        chmod --verbose g-r $keyfile
f21cbd9
        chown --verbose root:root $keyfile
f21cbd9
    fi
f21cbd9
done <<< "$output"