Files
infix/package/skeleton-init-finit/skeleton/usr/bin/ssh-hostkeys
T
Joachim WibergandTobias Waldekranz acd33d4fa5 board/common: drop duplicate sshd.config and ssh-hostkeys
Instead, use the version from package/skeleton-init-finit, including the
ssh-hostkeys (previously ssh-genhostkeys) from the same package, because
it include extensive error handling and logging on failure.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2023-10-09 10:41:46 +02:00

50 lines
897 B
Bash
Executable File

#!/bin/sh
tmp=$(mktemp)
dir=$(dirname "$(grep -r '^HostKey' /etc/ssh/* |tail -1 | awk '{print $2}')")
log()
{
prio=notice
if [ "$1" = "-p" ]; then
prio=$2
shift 2
fi
logger -sik -p "security.$prio" -t sshd "$@"
}
check()
{
if sshd -t >"$tmp" 2>&1; then
log "SSH hostkeys OK, setting ssh-hostkeys condition"
return 0
fi
return 1
}
rc=0
if ! check; then
files=$(awk '/invalid format/{print $6}' "$tmp" | sed 's/.*"\(.*\)".*/\1/')
for file in $files; do
log -p warn "Removing $file: invalid format!"
invalid=yes
rm "$file"
done
if [ -n "$invalid" ]; then
log "Invalid hostkeys detected, regenerating all SSH hostkeys ..."
else
log "Generating SSH hostkeys ..."
fi
if ssh-keygen -A; then
mv /etc/ssh/ssh_host_* "$dir/"
fi
if ! check; then
log -p err "Failed generating SSH hostkeys!"
rc=1
fi
fi
rm "$tmp"
exit $rc