ssh: Refactor, bumped to 5.5 (#598)

This commit is contained in:
Franck Nijhof
2019-06-03 11:34:17 +02:00
committed by Pascal Vizeli
parent 91ca564aca
commit 73e4aa8cbb
5 changed files with 167 additions and 34 deletions

View File

@@ -1,47 +1,45 @@
#!/bin/bash
#!/usr/bin/env bashio
set -e
CONFIG_PATH=/data/options.json
KEYS_PATH=/data/host_keys
AUTHORIZED_KEYS=$(jq --raw-output ".authorized_keys[]" $CONFIG_PATH)
PASSWORD=$(jq --raw-output ".password" $CONFIG_PATH)
if [ -n "$AUTHORIZED_KEYS" ]; then
echo "[INFO] Setup authorized_keys"
bashio::log.info "Initializing add-on for use..."
if bashio::config.has_value 'authorized_keys'; then
bashio::log.info "Setup authorized_keys"
mkdir -p ~/.ssh
while read -r line; do
echo "$line" >> ~/.ssh/authorized_keys
done <<< "$AUTHORIZED_KEYS"
done <<< "$(bashio::config 'authorized_keys')"
chmod 600 ~/.ssh/authorized_keys
sed -i s/#PasswordAuthentication.*/PasswordAuthentication\ no/ /etc/ssh/sshd_config
# Unlook account
# Unlock account
PASSWORD="$(strings /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c32)"
echo "root:$PASSWORD" | chpasswd 2&> /dev/null
elif [ -n "$PASSWORD" ]; then
echo "[INFO] Setup password login"
echo "root:${PASSWORD}" | chpasswd 2&> /dev/null
elif bashio::config.has_value 'password'; then
bashio::log.info "Setup password login"
PASSWORD=$(bashio::config 'password')
echo "root:${PASSWORD}" | chpasswd 2&> /dev/null
echo "root:$PASSWORD" | chpasswd 2&> /dev/null
sed -i s/#PasswordAuthentication.*/PasswordAuthentication\ yes/ /etc/ssh/sshd_config
sed -i s/#PermitEmptyPasswords.*/PermitEmptyPasswords\ no/ /etc/ssh/sshd_config
else
echo "[Error] You need to setup a login!"
exit 1
bashio::exit.nok "You need to setup a login!"
fi
# Generate host keys
if [ ! -d "$KEYS_PATH" ]; then
echo "[INFO] Create host keys"
if ! bashio::fs.directory_exists "${KEYS_PATH}"; then
bashio::log.info "Generating host keys..."
mkdir -p "$KEYS_PATH"
ssh-keygen -A
cp -fp /etc/ssh/ssh_host* "$KEYS_PATH/"
mkdir -p "${KEYS_PATH}"
ssh-keygen -A || bashio::exit.nok "Failed to create host keys!"
cp -fp /etc/ssh/ssh_host* "${KEYS_PATH}/"
else
echo "[INFO] Restore host keys"
cp -fp "$KEYS_PATH"/* /etc/ssh/
bashio::log.info "Restoring host keys..."
cp -fp "${KEYS_PATH}"/* /etc/ssh/
fi
# Persist shell history by redirecting .bash_history to /data
@@ -49,8 +47,9 @@ touch /data/.bash_history
chmod 600 /data/.bash_history
ln -s -f /data/.bash_history /root/.bash_history
# Store token for hass.io API
echo "export HASSIO_TOKEN=$HASSIO_TOKEN" >> /root/.bash_profile
# Store token for Hass.io API
echo "export HASSIO_TOKEN=${HASSIO_TOKEN}" >> /root/.bash_profile
# start server
# Start server
bashio::log.info "Starting SSH daemon..."
exec /usr/sbin/sshd -D -e < /dev/null