2017-10-19 17:41:41 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Init borg-users .ssh/authorized_keys
|
|
|
|
|
|
|
|
BORG_DATA_DIR=/backup
|
2017-11-01 16:51:51 +00:00
|
|
|
BORG_CMD='cd ${BORG_DATA_DIR}/${client_name}; borg serve --restrict-to-path ${BORG_DATA_DIR}/${client_name}'
|
2017-10-19 17:41:41 +00:00
|
|
|
SSH_KEY_DIR=/sshkeys
|
|
|
|
|
2017-11-01 16:51:51 +00:00
|
|
|
# Parse environment
|
|
|
|
if [ ! -z "${BORG_SERVE_ARGS}" ] ; then
|
|
|
|
BORG_CMD="${BORG_CMD} ${BORG_SERVE_ARGS}"
|
|
|
|
fi
|
|
|
|
|
2017-10-19 17:41:41 +00:00
|
|
|
# add all sshkeys to borg-user's authorized_keys & create repositories
|
|
|
|
echo "########################################################"
|
|
|
|
for dir in BORG_DATA_DIR SSH_KEY_DIR ; do
|
|
|
|
dirpath=$(eval echo '$'$dir)
|
2017-10-20 04:45:14 +00:00
|
|
|
echo " * Testing Volume $dir: $dirpath"
|
2017-10-19 17:41:41 +00:00
|
|
|
if [ ! -d "$dirpath" ] ; then
|
2017-10-20 04:45:14 +00:00
|
|
|
echo "ERROR: $dirpath is no directory!"
|
2017-10-19 17:41:41 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2017-10-20 04:45:14 +00:00
|
|
|
if [ $(find "${SSH_KEY_DIR}/clients" -type f | wc -l) == 0 ] ; then
|
2017-10-19 17:41:41 +00:00
|
|
|
echo "ERROR: No SSH-Pubkey file found in $SSH_KEY_DIR"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
2017-10-20 04:45:14 +00:00
|
|
|
|
2017-10-20 14:29:29 +00:00
|
|
|
# (Create &) Copy SSH-Host-Keys to persistent storage
|
2017-10-20 04:45:14 +00:00
|
|
|
mkdir -p ${SSH_KEY_DIR}/host 2>/dev/null
|
|
|
|
echo " * Checking / Preparing SSH Host-Keys..."
|
2017-10-20 14:29:29 +00:00
|
|
|
|
|
|
|
if [ ! -f /etc/ssh/ssh_host_rsa_key ] ; then
|
|
|
|
echo " ** Creating SSH Hostkeys..."
|
|
|
|
for keytype in ed25519 rsa ; do
|
|
|
|
ssh-keygen -q -f "/etc/ssh/ssh_host_${keytype}_key" -N '' -t $keytype
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2017-10-20 04:45:14 +00:00
|
|
|
for keyfile in ssh_host_rsa_key ssh_host_ed25519_key ; do
|
|
|
|
if [ ! -f "${SSH_KEY_DIR}/host/${keyfile}" ] ; then
|
|
|
|
cp /etc/ssh/${keyfile} "${SSH_KEY_DIR}/host/${keyfile}"
|
|
|
|
fi
|
|
|
|
done
|
2017-10-19 17:41:41 +00:00
|
|
|
echo "########################################################"
|
|
|
|
|
2017-10-20 04:45:14 +00:00
|
|
|
echo " * Starting SSH-Key import..."
|
2017-11-01 16:51:51 +00:00
|
|
|
rm /home/borg/.ssh/authorized_keys &>/dev/null
|
2017-10-20 04:45:14 +00:00
|
|
|
for keyfile in $(find "${SSH_KEY_DIR}/clients" -type f); do
|
2017-10-19 17:41:41 +00:00
|
|
|
client_name=$(basename $keyfile)
|
2017-10-20 04:45:14 +00:00
|
|
|
echo " ** Adding client ${client_name} with repo path ${BORG_DATA_DIR}/${client_name}"
|
2017-10-19 17:41:41 +00:00
|
|
|
mkdir ${BORG_DATA_DIR}/${client_name} 2>/dev/null
|
2017-10-20 04:45:14 +00:00
|
|
|
echo -n "command=\"$(eval echo -n \"${BORG_CMD}\")\" " >> /home/borg/.ssh/authorized_keys
|
2017-10-19 17:41:41 +00:00
|
|
|
cat $keyfile >> /home/borg/.ssh/authorized_keys
|
|
|
|
done
|
|
|
|
|
|
|
|
chown -R borg: /backup
|
|
|
|
chown borg: /home/borg/.ssh/authorized_keys
|
|
|
|
chmod 600 /home/borg/.ssh/authorized_keys
|
|
|
|
|
2017-10-20 04:45:14 +00:00
|
|
|
echo " * Init done!"
|
2017-10-19 17:41:41 +00:00
|
|
|
echo "########################################################"
|
2017-10-20 04:45:14 +00:00
|
|
|
echo " * Starting SSH-Daemon"
|
2017-10-19 17:41:41 +00:00
|
|
|
|
|
|
|
/usr/sbin/sshd -D -e
|