40 lines
1.3 KiB
Bash
40 lines
1.3 KiB
Bash
#!/bin/bash
|
|
|
|
echo "Script started" >> /home/jelastic/add-sftp-user-addon/script_output.log
|
|
|
|
# Generate random username and password
|
|
USERNAME="user$(shuf -i 10000-99999 -n 1)"
|
|
PASSWORD=$(openssl rand -base64 12)
|
|
|
|
# User's home directory
|
|
USER_HOME="/home/$USERNAME"
|
|
# The shared directory
|
|
ROOT_DIRECTORY="/var/www/webroot/ROOT"
|
|
# Get the group ownership of the ROOT_DIRECTORY
|
|
ROOT_GROUP=$(stat -c '%G' $ROOT_DIRECTORY)
|
|
|
|
# Check if user already exists
|
|
if id "$USERNAME" &>/dev/null; then
|
|
echo "User $USERNAME already exists." >> /home/jelastic/add-sftp-user-addon/script_output.log
|
|
exit 1
|
|
fi
|
|
|
|
# Create user with their own home directory
|
|
useradd -m $USERNAME
|
|
if [ $? -ne 0 ]; then
|
|
echo "Failed to create user $USERNAME." >> /home/jelastic/add-sftp-user-addon/script_output.log
|
|
exit 1
|
|
fi
|
|
echo "$USERNAME:$PASSWORD" | chpasswd
|
|
|
|
# Create a symlink in the user's home directory pointing to the shared ROOT_DIRECTORY
|
|
ln -s $ROOT_DIRECTORY $USER_HOME/ROOT
|
|
|
|
# Set user's group to the ROOT_GROUP and any other groups as needed (e.g., root)
|
|
usermod -aG $ROOT_GROUP,root $USERNAME
|
|
|
|
HOSTNAME=$(hostname -f)
|
|
echo "Script completed for user $USERNAME with hostname $HOSTNAME" >> /home/jelastic/add-sftp-user-addon/script_output.log
|
|
echo "USERNAME:$USERNAME"
|
|
echo "PASSWORD:$PASSWORD"
|