From 274cd6349c6cc0aa3d460fa3b063d9c877b8e9e2 Mon Sep 17 00:00:00 2001 From: Delfer Date: Thu, 31 Mar 2022 19:19:20 +0300 Subject: [PATCH] Fixed user creation when group with the same id already exists (cherry picked from commit 9fd6e070ff6d49b3a0fb4c4009a3072e2f007291) --- README.md | 2 +- start_vsftpd.sh | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 09f7256..84ed65d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ docker run -d \ ## Configuration Environment variables: -- `USERS` - space and `|` separated list (optional, default: `ftp|alpineftp`) +- `USERS` - space and `|` separated list (optional, default: `alpineftp|alpineftp`) - format `name1|password1|[folder1][|uid1] name2|password2|[folder2][|uid2]` - `ADDRESS` - external address witch clients can connect passive ports (optional, should resolve to ftp server ip address) - `MIN_PORT` - minimum port number to be used for passive connections (optional, default `21000`) diff --git a/start_vsftpd.sh b/start_vsftpd.sh index 87d5db3..fce86b8 100755 --- a/start_vsftpd.sh +++ b/start_vsftpd.sh @@ -1,6 +1,6 @@ #!/bin/sh #Remove all ftp users -#grep '/ftp/' /etc/passwd | cut -d':' -f1 | xargs -n1 deluser +grep '/ftp/' /etc/passwd | cut -d':' -f1 | xargs -r -n1 deluser #Create users #USERS='name1|password1|[folder1][|uid1] name2|password2|[folder2][|uid2]' @@ -14,14 +14,15 @@ #Default user 'ftp' with password 'alpineftp' if [ -z "$USERS" ]; then - USERS="amin|alpineftp" + USERS="alpineftp|alpineftp" fi for i in $USERS ; do - NAME=$(echo $i | cut -d'|' -f1) - PASS=$(echo $i | cut -d'|' -f2) + NAME=$(echo $i | cut -d'|' -f1) + GROUP=$NAME + PASS=$(echo $i | cut -d'|' -f2) FOLDER=$(echo $i | cut -d'|' -f3) - UID=$(echo $i | cut -d'|' -f4) + UID=$(echo $i | cut -d'|' -f4) if [ -z "$FOLDER" ]; then FOLDER="/ftp/$NAME" @@ -29,11 +30,16 @@ for i in $USERS ; do if [ ! -z "$UID" ]; then UID_OPT="-u $UID" + #Check if the group with the same ID already exists + GROUP=$(getent group $UID | cut -d: -f1) + if [ ! -z "$GROUP" ]; then + GROUP_OPT="-G $GROUP" + fi fi - echo -e "$PASS\n$PASS" | adduser -h $FOLDER -s /sbin/nologin $UID_OPT $NAME + echo -e "$PASS\n$PASS" | adduser -h $FOLDER -s /sbin/nologin $UID_OPT $GROUP_OPT $NAME mkdir -p $FOLDER - chown $NAME:$NAME $FOLDER + chown $NAME:$GROUP $FOLDER unset NAME PASS FOLDER UID done