diff --git a/Dockerfile b/Dockerfile index bb5637ce..c15fc3be 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,15 +6,20 @@ MAINTAINER Adrian Dvergsdal [atmoz.net] # - OpenSSH needs /var/run/sshd to run # - Remove generic host keys, entrypoint generates unique keys RUN apt-get update && \ - apt-get -y install openssh-server && \ + apt-get -y install openssh-server cron && \ rm -rf /var/lib/apt/lists/* && \ mkdir -p /var/run/sshd && \ rm -f /etc/ssh/ssh_host_*key* +COPY files/refresh-users-cron /etc/cron.d/ COPY files/sshd_config /etc/ssh/sshd_config COPY files/create-sftp-user /usr/local/bin/ +COPY files/add-new-users /usr/local/bin/ COPY files/entrypoint / +RUN chmod 0644 /etc/cron.d/refresh-users-cron +RUN crontab /etc/cron.d/refresh-users-cron + EXPOSE 22 ENTRYPOINT ["/entrypoint"] diff --git a/files/add-new-users b/files/add-new-users new file mode 100755 index 00000000..b73abc7c --- /dev/null +++ b/files/add-new-users @@ -0,0 +1,25 @@ +#!/bin/bash + +SHELL=/bin/bash +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +export SHELL +export PATH + +reArgSkip='^([[:blank:]]*#.*|[[:blank:]]*)$' # comment or empty line + +# Paths +userConfPath="/etc/sftp/users.conf" + +if [ ! -f "$userConfPath" ]; then + exit 0 +fi + +USERS=$(grep -v -E "$reArgSkip" < "$userConfPath") + +# Check that we have users in config +if [ "$(echo "$USERS" | wc -l)" -gt 0 ]; then + # Import users from final conf file + while IFS= read -r user || [[ -n "$user" ]]; do + create-sftp-user "$user" + done < <(printf '%s\n' "$USERS") +fi diff --git a/files/create-sftp-user b/files/create-sftp-user index 5df16b77..674756f7 100755 --- a/files/create-sftp-user +++ b/files/create-sftp-user @@ -81,13 +81,15 @@ else fi # Add SSH keys to authorized_keys with valid permissions +mkdir -p "/home/$user/.ssh/" +touch "/home/$user/.ssh/authorized_keys" if [ -d "/home/$user/.ssh/keys" ]; then for publickey in "/home/$user/.ssh/keys"/*; do cat "$publickey" >> "/home/$user/.ssh/authorized_keys" done - chown "$uid" "/home/$user/.ssh/authorized_keys" - chmod 600 "/home/$user/.ssh/authorized_keys" fi +chown "$uid" "/home/$user/.ssh/authorized_keys" +chmod 600 "/home/$user/.ssh/authorized_keys" # Make sure dirs exists if [ -n "$dir" ]; then diff --git a/files/entrypoint b/files/entrypoint index 8730452d..8723ef84 100755 --- a/files/entrypoint +++ b/files/entrypoint @@ -86,6 +86,9 @@ if [ -d /etc/sftp.d ]; then unset f fi +log "Executing cron" +exec /usr/sbin/cron & + if $startSshd; then log "Executing sshd" exec /usr/sbin/sshd -D -e diff --git a/files/refresh-users-cron b/files/refresh-users-cron new file mode 100644 index 00000000..365f174e --- /dev/null +++ b/files/refresh-users-cron @@ -0,0 +1,3 @@ +* * * * * /usr/local/bin/add-new-users +# An empty line is required at the end of this file for a valid cron file. +