2018-09-25 01:31:37 +02:00
|
|
|
#!/bin/bash
|
2016-04-06 12:32:01 +02:00
|
|
|
set -e
|
2017-04-27 22:58:43 +02:00
|
|
|
set -o pipefail
|
2016-04-06 12:32:01 +02:00
|
|
|
|
|
|
|
[ "$DEBUG" == 'true' ] && set -x
|
|
|
|
|
|
|
|
DAEMON=sshd
|
|
|
|
HOSTKEY=/etc/ssh/ssh_host_ed25519_key
|
|
|
|
|
|
|
|
# create the host key if not already created
|
2018-09-25 01:31:37 +02:00
|
|
|
if [[ ! -f "${HOSTKEY}" ]]; then
|
2016-04-06 12:32:01 +02:00
|
|
|
ssh-keygen -A
|
|
|
|
fi
|
|
|
|
|
2018-09-25 01:31:37 +02:00
|
|
|
mkdir -p "${HOME}/.ssh"
|
|
|
|
# shellcheck disable=SC1091
|
2016-04-06 19:21:51 +02:00
|
|
|
source /etc/profile
|
2018-09-25 01:31:37 +02:00
|
|
|
[ "$PUBKEY" ] && echo "$PUBKEY" > "${HOME}/.ssh/authorized_keys"
|
2016-04-06 12:32:01 +02:00
|
|
|
|
2018-09-25 01:31:37 +02:00
|
|
|
chown -R git:git "${HOME}"
|
|
|
|
chmod -R 755 "${HOME}"
|
2016-04-06 19:21:51 +02:00
|
|
|
|
2016-04-06 12:32:01 +02:00
|
|
|
# Fix permissions, if writable
|
2018-09-25 01:31:37 +02:00
|
|
|
if [[ -w "${HOME}/.ssh" ]]; then
|
|
|
|
chown git:git "${HOME}/.ssh" && chmod 700 "${HOME}/.ssh/"
|
2016-04-06 12:32:01 +02:00
|
|
|
fi
|
2018-09-25 01:31:37 +02:00
|
|
|
if [[ -w "${HOME}/.ssh/authorized_keys" ]]; then
|
|
|
|
chown git:git "${HOME}/.ssh/authorized_keys"
|
|
|
|
chmod 600 "${HOME}/.ssh/authorized_keys"
|
2016-04-06 12:32:01 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Warn if no config
|
2018-09-25 01:31:37 +02:00
|
|
|
if [[ ! -e "${HOME}/.ssh/authorized_keys" ]]; then
|
2016-04-06 12:32:01 +02:00
|
|
|
echo "WARNING: No SSH authorized_keys found for git"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set the default shell
|
2018-09-25 01:31:37 +02:00
|
|
|
mkdir -p "${HOME}/git-shell-commands"
|
|
|
|
cat > "${HOME}/git-shell-commands/no-interactive-login" <<\EOF
|
2016-04-06 12:32:01 +02:00
|
|
|
#!/bin/sh
|
|
|
|
printf '%s\n' "Hi $USER! You've successfully authenticated, but I do not"
|
|
|
|
printf '%s\n' "provide interactive shell access."
|
|
|
|
exit 128
|
|
|
|
EOF
|
2018-09-25 01:31:37 +02:00
|
|
|
chmod +x "${HOME}/git-shell-commands/no-interactive-login"
|
2016-04-06 12:32:01 +02:00
|
|
|
|
|
|
|
stop() {
|
|
|
|
echo "Received SIGINT or SIGTERM. Shutting down $DAEMON"
|
|
|
|
# Get PID
|
2018-09-25 01:31:37 +02:00
|
|
|
pid=$(cat "/var/run/${DAEMON}/${DAEMON}.pid")
|
2016-04-06 12:32:01 +02:00
|
|
|
# Set TERM
|
|
|
|
kill -SIGTERM "${pid}"
|
|
|
|
# Wait for exit
|
|
|
|
wait "${pid}"
|
|
|
|
# All done.
|
|
|
|
echo "Done."
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:30:25 +02:00
|
|
|
# shellcheck disable=SC2145
|
2018-09-25 19:29:41 +02:00
|
|
|
echo "Running $@"
|
2018-09-25 01:31:37 +02:00
|
|
|
if [[ "$(basename "$1")" == "$DAEMON" ]]; then
|
2016-04-06 12:32:01 +02:00
|
|
|
trap stop SIGINT SIGTERM
|
2018-09-25 01:31:37 +02:00
|
|
|
# shellcheck disable=SC2068
|
2016-04-06 12:32:01 +02:00
|
|
|
$@ &
|
|
|
|
pid="$!"
|
2018-09-25 01:31:37 +02:00
|
|
|
mkdir -p "/var/run/${DAEMON}" && echo "${pid}" > "/var/run/${DAEMON}/${DAEMON}.pid"
|
2016-04-06 12:32:01 +02:00
|
|
|
wait "${pid}" && exit $?
|
|
|
|
else
|
|
|
|
exec "$@"
|
|
|
|
fi
|