From 7fed52f01887bf9e127cd9f9d9e61aa761dee90a Mon Sep 17 00:00:00 2001 From: Rodrigo Aiala Feijolo Rodrigues Date: Mon, 20 Nov 2017 17:26:27 -0300 Subject: [PATCH 1/5] Update entrypoint --- entrypoint | 2 ++ 1 file changed, 2 insertions(+) diff --git a/entrypoint b/entrypoint index 62b58df..2ee659c 100755 --- a/entrypoint +++ b/entrypoint @@ -172,6 +172,8 @@ if [ -d /etc/sftp.d ]; then if [ -x "$f" ]; then log "Running $f ..." $f + elif + log "Will not run $f because the file is not executable. Maybe you should run chmod +x $f outside your container." fi done unset f From 6ba0edae230caf5c2de98760df03ee3ed769f848 Mon Sep 17 00:00:00 2001 From: Hollin Wilkins Date: Wed, 27 Dec 2017 00:33:10 -0800 Subject: [PATCH 2/5] Make sure to reset chpasswdOptions. --- entrypoint | 2 ++ 1 file changed, 2 insertions(+) diff --git a/entrypoint b/entrypoint index 62b58df..7e904e0 100755 --- a/entrypoint +++ b/entrypoint @@ -45,6 +45,8 @@ function createUser() { if [ "${args[2]}" == "e" ]; then chpasswdOptions="-e" index=1 + else + chpasswdOptions="" fi uid="${args[$[$index+2]]}"; validateArg "UID" "$uid" "$reUid" || return 1 From 9405974cc9dadcfa0636eec5d6c6a33a453321d0 Mon Sep 17 00:00:00 2001 From: Adrian Dvergsdal Date: Thu, 4 Jan 2018 19:22:15 +0100 Subject: [PATCH 3/5] Tidy up variables in createUser() --- entrypoint | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/entrypoint b/entrypoint index 7e904e0..471dcc2 100755 --- a/entrypoint +++ b/entrypoint @@ -37,29 +37,28 @@ function createUser() { log "Parsing user data: \"$@\"" IFS=':' read -a args <<< $@ - index=0 + + skipIndex=0 + chpasswdOptions="" + useraddOptions="--no-user-group" user="${args[0]}"; validateArg "username" "$user" "$reUser" || return 1 pass="${args[1]}"; validateArg "password" "$pass" "$rePass" || return 1 if [ "${args[2]}" == "e" ]; then chpasswdOptions="-e" - index=1 - else - chpasswdOptions="" + skipIndex=1 fi - uid="${args[$[$index+2]]}"; validateArg "UID" "$uid" "$reUid" || return 1 - gid="${args[$[$index+3]]}"; validateArg "GID" "$gid" "$reGid" || return 1 - dir="${args[$[$index+4]]}"; validateArg "dirs" "$dir" "$reDir" || return 1 + uid="${args[$[$skipIndex+2]]}"; validateArg "UID" "$uid" "$reUid" || return 1 + gid="${args[$[$skipIndex+3]]}"; validateArg "GID" "$gid" "$reGid" || return 1 + dir="${args[$[$skipIndex+4]]}"; validateArg "dirs" "$dir" "$reDir" || return 1 if getent passwd $user > /dev/null; then log "WARNING: User \"$user\" already exists. Skipping." return 0 fi - useraddOptions="--no-user-group" - if [ -n "$uid" ]; then useraddOptions="$useraddOptions --non-unique --uid $uid" fi From a2863269a0e3d2eed3bf535a5ced5f9df98d2d6d Mon Sep 17 00:00:00 2001 From: Adrian Dvergsdal Date: Thu, 4 Jan 2018 19:32:05 +0100 Subject: [PATCH 4/5] Remove unused stdin args and parse args from env --- README.md | 11 ++++++----- entrypoint | 15 +++++++-------- tests/run | 9 +++++++++ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 41bb226..f129907 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,14 @@ This is an automated build linked with the [debian](https://hub.docker.com/_/deb # Usage -- Required: define users in command arguments or in file mounted as `/etc/sftp/users.conf` - (syntax: `user:pass[:e][:uid[:gid[:dir1[,dir2]...]]]...`). +- Define users in (1) command arguments, (2) `SFTP_USERS` environment variable + or (3) in file mounted as `/etc/sftp/users.conf` (syntax: + `user:pass[:e][:uid[:gid[:dir1[,dir2]...]]] ...`, see below for examples) - Set UID/GID manually for your users if you want them to make changes to your mounted volumes with permissions matching your host filesystem. - - Add directory names at the end, if you want to create them under the user's - home directory. Perfect when you just want a fast way to upload something. -- Optional (but recommended): mount volumes. + - Directory names at the end will be created under user's home directory with + write permission, if they aren't already present. +- Mount volumes - The users are chrooted to their home directory, so you can mount the volumes in separate directories inside the user's home directory (/home/user/**mounted-directory**) or just mount the whole **/home** directory. diff --git a/entrypoint b/entrypoint index 471dcc2..4737e97 100755 --- a/entrypoint +++ b/entrypoint @@ -131,18 +131,17 @@ if [ ! -f "$userConfFinalPath" ]; then cat "$userConfPath" | grep -v -E "$reArgSkip" > "$userConfFinalPath" fi - # Append users from STDIN to final config - # DEPRECATED on 2017-10-08, DO NOT USE - # TODO: Remove code after 6-12 months - if [ ! -t 0 ]; then - while IFS= read -r user || [[ -n "$user" ]]; do + if $startSshd; then + # Append users from arguments to final config + for user in "$@"; do echo "$user" >> "$userConfFinalPath" done fi - if $startSshd; then - # Append users from arguments to final config - for user in "$@"; do + if [ -n "$SFTP_USERS" ]; then + # Append users from environment variable to final config + usersFromEnv=($SFTP_USERS) # as array + for user in "${usersFromEnv[@]}"; do echo "$user" >> "$userConfFinalPath" done fi diff --git a/tests/run b/tests/run index 0453473..ddc2978 100755 --- a/tests/run +++ b/tests/run @@ -52,10 +52,12 @@ function beforeTest() { $sudo docker run \ -v "$tmpDir/users:/etc/sftp/users.conf:ro" \ -v "$scriptDir/id_rsa.pub":/home/test/.ssh/keys/id_rsa.pub:ro \ + -v "$scriptDir/id_rsa.pub":/home/userFromEnv/.ssh/keys/id_rsa.pub:ro \ -v "$scriptDir/id_rsa.pub":/home/user.with.dot/.ssh/keys/id_rsa.pub:ro \ -v "$tmpDir":/home/test/share \ --name "$sftpContainerName" \ --expose 22 \ + -e "SFTP_USERS=userFromEnv::$(id -u):$(id -g) userFromEnv2::$(id -u):$(id -g)" \ -d "$sftpImageName" \ > "$redirect" @@ -145,6 +147,13 @@ function testUserWithDotLogin() { assertReturn $? 0 } +function testLoginUsingUserFromEnv() { + $skipAllTests && skip && return 0 + + runSftpCommands "$sftpContainerName" "userFromEnv" "exit" + assertReturn $? 0 +} + function testWritePermission() { $skipAllTests && skip && return 0 From 5427a7d59be7e63dfef41de2e43432a6add1cbb5 Mon Sep 17 00:00:00 2001 From: Adrian Dvergsdal Date: Fri, 5 Jan 2018 13:14:29 +0100 Subject: [PATCH 5/5] Fix if-else syntax and change log message --- entrypoint | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint b/entrypoint index c1fe624..bff3652 100755 --- a/entrypoint +++ b/entrypoint @@ -172,8 +172,8 @@ if [ -d /etc/sftp.d ]; then if [ -x "$f" ]; then log "Running $f ..." $f - elif - log "Will not run $f because the file is not executable. Maybe you should run chmod +x $f outside your container." + else + log "Could not run $f, because it's missing execute permission (+x)." fi done unset f