2023-10-26 15:54:56 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-10-30 16:30:57 +00:00
|
|
|
LOG_FILE="/home/jelastic/add-sftp-user-addon/logs/script_output.log"
|
2023-11-02 17:48:26 +00:00
|
|
|
VERBOSE=1
|
2023-10-30 16:30:57 +00:00
|
|
|
|
2023-11-01 06:42:52 +00:00
|
|
|
log() {
|
2023-11-01 16:48:25 +00:00
|
|
|
local filename=${2:-$LOG_FILE}
|
2023-11-01 06:42:52 +00:00
|
|
|
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
|
2023-11-01 16:48:25 +00:00
|
|
|
local message="$timestamp - $1"
|
2023-11-01 06:42:52 +00:00
|
|
|
if [ "$VERBOSE" -eq 1 ]; then
|
2023-11-01 16:48:25 +00:00
|
|
|
echo "$message" | tee -a $filename
|
|
|
|
else
|
|
|
|
echo "$message" >> $filename
|
2023-11-01 06:42:52 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-11-02 17:48:26 +00:00
|
|
|
# Generate random username
|
|
|
|
generate_username() {
|
|
|
|
echo "user$(shuf -i 10000-99999 -n 1)"
|
2023-11-02 17:26:02 +00:00
|
|
|
}
|
2023-10-30 13:35:32 +00:00
|
|
|
|
2023-10-30 17:09:45 +00:00
|
|
|
USERNAME=$1
|
|
|
|
PASSWORD=$2
|
2023-10-26 15:54:56 +00:00
|
|
|
|
2023-11-02 17:48:26 +00:00
|
|
|
# Check if user already exists, if yes generate a new one
|
|
|
|
while id "$USERNAME" &>/dev/null; do
|
|
|
|
USERNAME=$(generate_username)
|
|
|
|
done
|
|
|
|
|
2023-11-01 10:54:29 +00:00
|
|
|
USER_HOME="/home/sftpusers/$USERNAME"
|
2023-10-30 16:13:29 +00:00
|
|
|
ROOT_DIRECTORY="/var/www/webroot/ROOT"
|
2023-11-02 17:48:26 +00:00
|
|
|
ROOT_GROUP=$(stat -c '%G' $ROOT_DIRECTORY)
|
2023-11-01 06:42:52 +00:00
|
|
|
|
|
|
|
log "Script started"
|
2023-10-26 15:54:56 +00:00
|
|
|
|
2023-11-02 17:48:26 +00:00
|
|
|
[ ! -d "$ROOT_DIRECTORY" ] && { log "ERROR: ROOT_DIRECTORY $ROOT_DIRECTORY does not exist."; exit 2; }
|
2023-11-01 05:22:36 +00:00
|
|
|
|
2023-11-01 10:54:29 +00:00
|
|
|
mkdir -p $USER_HOME
|
|
|
|
useradd -d $USER_HOME $USERNAME
|
2023-10-26 15:54:56 +00:00
|
|
|
echo "$USERNAME:$PASSWORD" | chpasswd
|
|
|
|
|
2023-11-02 17:48:26 +00:00
|
|
|
log "User $USERNAME created with home directory $USER_HOME"
|
2023-11-01 16:48:25 +00:00
|
|
|
|
2023-10-30 16:13:29 +00:00
|
|
|
ln -s $ROOT_DIRECTORY $USER_HOME/ROOT
|
2023-11-01 06:42:52 +00:00
|
|
|
log "Symlink created for $USERNAME pointing to $ROOT_DIRECTORY"
|
2023-10-27 16:19:22 +00:00
|
|
|
|
2023-11-01 06:42:52 +00:00
|
|
|
usermod -aG $ROOT_GROUP $USERNAME
|
|
|
|
log "$USERNAME added to group $ROOT_GROUP"
|
2023-10-26 15:54:56 +00:00
|
|
|
|
2023-11-01 10:54:29 +00:00
|
|
|
chown $USERNAME:$ROOT_GROUP $USER_HOME
|
2023-11-01 16:48:25 +00:00
|
|
|
chmod 750 $USER_HOME
|
2023-11-01 06:42:52 +00:00
|
|
|
|
|
|
|
chown -R :$ROOT_GROUP /var/www/webroot/ROOT/
|
2023-11-01 16:48:25 +00:00
|
|
|
find /var/www/webroot/ROOT/ -type d -exec chmod 770 {} \;
|
|
|
|
find /var/www/webroot/ROOT/ -type f -exec chmod 660 {} \;
|
2023-11-01 06:42:52 +00:00
|
|
|
|
2023-11-01 05:22:36 +00:00
|
|
|
chmod g+s $ROOT_DIRECTORY
|
|
|
|
|
2023-11-02 17:48:26 +00:00
|
|
|
log "Script completed for user $USERNAME"
|
|
|
|
|
|
|
|
# Output the created username and password
|
2023-10-30 17:09:45 +00:00
|
|
|
echo "{ \"USERNAME\": \"$USERNAME\", \"PASSWORD\": \"$PASSWORD\" }"
|