Test extract response out
parent
f862cc4758
commit
7990e7ecf5
43
add-sftp.sh
43
add-sftp.sh
|
@ -1,9 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e # Exit immediately if a command exits with a non-zero status
|
||||
|
||||
LOG_FILE="/home/jelastic/add-sftp-user-addon/logs/script_output.log"
|
||||
VERBOSE=1 # Set to 1 for verbose mode, 0 for normal mode
|
||||
VERBOSE=1
|
||||
|
||||
log() {
|
||||
local filename=${2:-$LOG_FILE}
|
||||
|
@ -16,60 +14,49 @@ log() {
|
|||
fi
|
||||
}
|
||||
|
||||
error_exit() {
|
||||
log "ERROR: $1"
|
||||
exit $2
|
||||
# Generate random username
|
||||
generate_username() {
|
||||
echo "user$(shuf -i 10000-99999 -n 1)"
|
||||
}
|
||||
|
||||
# Generate random username and password
|
||||
USERNAME=$1
|
||||
PASSWORD=$2
|
||||
|
||||
# User's home directory
|
||||
# Check if user already exists, if yes generate a new one
|
||||
while id "$USERNAME" &>/dev/null; do
|
||||
USERNAME=$(generate_username)
|
||||
done
|
||||
|
||||
USER_HOME="/home/sftpusers/$USERNAME"
|
||||
# The shared directory
|
||||
ROOT_DIRECTORY="/var/www/webroot/ROOT"
|
||||
ROOT_GROUP=$(stat -c '%G' $ROOT_DIRECTORY)
|
||||
|
||||
log "Script started"
|
||||
|
||||
# Ensure the ROOT_DIRECTORY exists
|
||||
[ ! -d "$ROOT_DIRECTORY" ] && error_exit "ROOT_DIRECTORY $ROOT_DIRECTORY does not exist." 2
|
||||
[ ! -d "$ROOT_DIRECTORY" ] && { log "ERROR: ROOT_DIRECTORY $ROOT_DIRECTORY does not exist."; exit 2; }
|
||||
|
||||
# Get the group ownership of the ROOT_DIRECTORY
|
||||
ROOT_GROUP=$(stat -c '%G' $ROOT_DIRECTORY)
|
||||
|
||||
# Check if user already exists
|
||||
id "$USERNAME" &>/dev/null && error_exit "User $USERNAME already exists." 3
|
||||
|
||||
# Create user with their own directory under /home/sftpusers/
|
||||
mkdir -p $USER_HOME
|
||||
useradd -d $USER_HOME $USERNAME
|
||||
echo "$USERNAME:$PASSWORD" | chpasswd
|
||||
|
||||
# Log to sftp-users.log
|
||||
log "User $USERNAME created with home directory $USER_HOME" "/home/jelastic/add-sftp-user-addon/logs/sftp-users.log"
|
||||
log "User $USERNAME created with home directory $USER_HOME"
|
||||
|
||||
# Create a symlink in the user's directory pointing to the shared ROOT_DIRECTORY
|
||||
ln -s $ROOT_DIRECTORY $USER_HOME/ROOT
|
||||
log "Symlink created for $USERNAME pointing to $ROOT_DIRECTORY"
|
||||
|
||||
# Set user's group to the ROOT_GROUP
|
||||
usermod -aG $ROOT_GROUP $USERNAME
|
||||
log "$USERNAME added to group $ROOT_GROUP"
|
||||
|
||||
# Adjust permissions and ownership for the user's directory
|
||||
chown $USERNAME:$ROOT_GROUP $USER_HOME
|
||||
chmod 750 $USER_HOME
|
||||
|
||||
# Adjust permissions and ownership for the ROOT_DIRECTORY
|
||||
chown -R :$ROOT_GROUP /var/www/webroot/ROOT/
|
||||
find /var/www/webroot/ROOT/ -type d -exec chmod 770 {} \;
|
||||
find /var/www/webroot/ROOT/ -type f -exec chmod 660 {} \;
|
||||
|
||||
# Set the SetGID bit on ROOT_DIRECTORY
|
||||
chmod g+s $ROOT_DIRECTORY
|
||||
|
||||
HOSTNAME=$(hostname -f)
|
||||
log "Script completed for user $USERNAME with hostname $HOSTNAME"
|
||||
log "Script completed for user $USERNAME"
|
||||
|
||||
# Output the created username and password
|
||||
echo "{ \"USERNAME\": \"$USERNAME\", \"PASSWORD\": \"$PASSWORD\" }"
|
||||
exit 0
|
||||
|
|
Loading…
Reference in New Issue