add-sftp-user/add-sftp.sh

72 lines
2.1 KiB
Bash
Raw Normal View History

2023-10-26 15:54:56 +00:00
#!/bin/bash
LOG_FILE="/home/jelastic/add-sftp-user-addon/logs/script_output.log"
2023-11-01 06:42:52 +00:00
VERBOSE=1 # Set to 1 for verbose mode, 0 for normal mode
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:12:51 +00:00
# Check if user already exists
if id "$1" &>/dev/null; then
echo "{ \"error\": \"User $1 already exists.\" }"
exit 1
fi
# Generate random username and password
2023-10-30 17:09:45 +00:00
USERNAME=$1
PASSWORD=$2
2023-10-26 15:54:56 +00:00
2023-10-30 16:13:29 +00:00
# User's home directory
USER_HOME="/home/sftpusers/$USERNAME"
2023-10-30 16:13:29 +00:00
# The shared directory
ROOT_DIRECTORY="/var/www/webroot/ROOT"
2023-11-01 06:42:52 +00:00
log "Script started"
2023-10-26 15:54:56 +00:00
# Ensure the ROOT_DIRECTORY exists
2023-11-02 17:12:51 +00:00
[ ! -d "$ROOT_DIRECTORY" ] && { echo "{ \"error\": \"ROOT_DIRECTORY $ROOT_DIRECTORY does not exist.\" }"; exit 2; }
# Get the group ownership of the ROOT_DIRECTORY
2023-11-01 06:42:52 +00:00
ROOT_GROUP=$(stat -c '%G' $ROOT_DIRECTORY)
# Create user with their own directory under /home/sftpusers/
mkdir -p $USER_HOME
useradd -d $USER_HOME $USERNAME
2023-10-26 15:54:56 +00:00
echo "$USERNAME:$PASSWORD" | chpasswd
2023-11-01 16:48:25 +00:00
# Log to sftp-users.log
log "User $USERNAME created with home directory $USER_HOME" "/home/jelastic/add-sftp-user-addon/logs/sftp-users.log"
# Create a symlink in the user's directory pointing to the shared ROOT_DIRECTORY
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
# Set user's group to the ROOT_GROUP
usermod -aG $ROOT_GROUP $USERNAME
log "$USERNAME added to group $ROOT_GROUP"
2023-10-26 15:54:56 +00:00
# Adjust permissions and ownership for the user's directory
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
# Adjust permissions and ownership for the ROOT_DIRECTORY
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
# Set the SetGID bit on ROOT_DIRECTORY
chmod g+s $ROOT_DIRECTORY
2023-10-30 13:13:03 +00:00
HOSTNAME=$(hostname -f)
2023-11-01 06:42:52 +00:00
log "Script completed for user $USERNAME with hostname $HOSTNAME"
2023-10-30 17:09:45 +00:00
echo "{ \"USERNAME\": \"$USERNAME\", \"PASSWORD\": \"$PASSWORD\" }"
exit 0