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-03 14:35:15 +00:00
|
|
|
DEBUG=${4:-0} # Set to 1 to enable debug logging, controlled by 4th argument
|
2023-10-30 16:30:57 +00:00
|
|
|
|
2023-11-01 06:42:52 +00:00
|
|
|
log() {
|
2023-11-03 14:35:15 +00:00
|
|
|
local level=${1:-INFO}
|
|
|
|
local message=${2}
|
2023-11-01 06:42:52 +00:00
|
|
|
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
|
2023-11-03 14:35:15 +00:00
|
|
|
echo "$timestamp [$level] $message" | tee -a $LOG_FILE
|
|
|
|
}
|
|
|
|
|
|
|
|
log_error() {
|
|
|
|
log "ERROR" "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
log_warning() {
|
|
|
|
log "WARNING" "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug() {
|
|
|
|
if [ "$DEBUG" -eq 1 ]; then
|
|
|
|
log "DEBUG" "$1"
|
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-11-03 14:35:15 +00:00
|
|
|
SSH_ENABLED=$3
|
|
|
|
|
|
|
|
log "Script started"
|
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)
|
2023-11-03 14:35:15 +00:00
|
|
|
log_warning "Username $USERNAME already exists, generating a new username."
|
2023-11-02 17:48:26 +00:00
|
|
|
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
|
|
|
|
2023-11-03 14:35:15 +00:00
|
|
|
# Create the wp-admins group if it doesn't exist
|
|
|
|
if ! grep -q "^wp-admins:" /etc/group; then
|
|
|
|
if groupadd wp-admins; then
|
|
|
|
log "Group wp-admins created successfully."
|
|
|
|
else
|
|
|
|
log_error "Failed to create group wp-admins."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! useradd -d $USER_HOME $USERNAME; then
|
|
|
|
log_error "Failed to create user $USERNAME."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Enable SSH access if requested
|
|
|
|
if [ "$SSH_ENABLED" == "true" ]; then
|
|
|
|
usermod -s /bin/bash $USERNAME
|
|
|
|
usermod -aG wp-admins $USERNAME # Add user to wp-admins group
|
|
|
|
|
|
|
|
# Ensure WP-CLI is installed
|
|
|
|
if ! command -v wp &> /dev/null; then
|
|
|
|
if curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar &&
|
|
|
|
chmod +x wp-cli.phar &&
|
|
|
|
mv wp-cli.phar /usr/local/bin/wp; then
|
|
|
|
log "WP-CLI installed successfully."
|
|
|
|
else
|
|
|
|
log_error "Failed to install WP-CLI."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2023-10-26 15:54:56 +00:00
|
|
|
|
2023-11-03 14:35:15 +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
|
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-11-02 18:00:37 +00:00
|
|
|
echo "export CREATED_USERNAME=$USERNAME" >> /etc/profile
|
2023-11-03 14:35:15 +00:00
|
|
|
echo "export CREATED_PASSWORD=$PASSWORD" >> /etc/profile
|