Added more debugging and error handling

main
Anthony 2023-11-01 18:54:29 +08:00
parent ca6338613c
commit 52509d143b
2 changed files with 66 additions and 27 deletions

View File

@ -13,7 +13,7 @@ log() {
error_exit() { error_exit() {
log "ERROR: $1" log "ERROR: $1"
exit 1 exit $2
} }
# Generate random username and password # Generate random username and password
@ -21,7 +21,7 @@ USERNAME=$1
PASSWORD=$2 PASSWORD=$2
# User's home directory # User's home directory
USER_HOME="/home/$USERNAME" USER_HOME="/home/sftpusers/$USERNAME"
# The shared directory # The shared directory
ROOT_DIRECTORY="/var/www/webroot/ROOT" ROOT_DIRECTORY="/var/www/webroot/ROOT"
@ -29,11 +29,10 @@ log "Script started"
# Ensure the ROOT_DIRECTORY exists # Ensure the ROOT_DIRECTORY exists
if [ ! -d "$ROOT_DIRECTORY" ]; then if [ ! -d "$ROOT_DIRECTORY" ]; then
error_exit "ROOT_DIRECTORY $ROOT_DIRECTORY does not exist." error_exit "ROOT_DIRECTORY $ROOT_DIRECTORY does not exist." 2
fi fi
# Get the owner and group of the ROOT_DIRECTORY # Get the group ownership of the ROOT_DIRECTORY
ROOT_OWNER=$(stat -c '%U' $ROOT_DIRECTORY)
ROOT_GROUP=$(stat -c '%G' $ROOT_DIRECTORY) ROOT_GROUP=$(stat -c '%G' $ROOT_DIRECTORY)
# Check if user already exists # Check if user already exists
@ -42,37 +41,31 @@ if id "$USERNAME" &>/dev/null; then
exit 3 exit 3
fi fi
# Create user with their own home directory # Create user with their own directory under /home/sftpusers/
useradd -m $USERNAME mkdir -p $USER_HOME
useradd -d $USER_HOME $USERNAME
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
error_exit "Failed to create user $USERNAME." error_exit "Failed to create user $USERNAME." 4
fi fi
echo "$USERNAME:$PASSWORD" | chpasswd echo "$USERNAME:$PASSWORD" | chpasswd
# Create a symlink in the user's home directory pointing to the shared ROOT_DIRECTORY # Create a symlink in the user's directory pointing to the shared ROOT_DIRECTORY
ln -s $ROOT_DIRECTORY $USER_HOME/ROOT ln -s $ROOT_DIRECTORY $USER_HOME/ROOT
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
error_exit "Failed to create symlink for $USERNAME." error_exit "Failed to create symlink for $USERNAME." 5
fi fi
log "Symlink created for $USERNAME pointing to $ROOT_DIRECTORY" log "Symlink created for $USERNAME pointing to $ROOT_DIRECTORY"
# Set user's group to the ROOT_GROUP # Set user's group to the ROOT_GROUP
usermod -aG $ROOT_GROUP $USERNAME usermod -aG $ROOT_GROUP $USERNAME
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
error_exit "Failed to modify groups for $USERNAME." error_exit "Failed to modify groups for $USERNAME." 6
fi fi
log "$USERNAME added to group $ROOT_GROUP" log "$USERNAME added to group $ROOT_GROUP"
# Check if the user-specific directory already exists # Adjust permissions and ownership for the user's directory
USER_SPECIFIC_DIR="$ROOT_DIRECTORY/$USERNAME" chown $USERNAME:$ROOT_GROUP $USER_HOME
if [ ! -d "$USER_SPECIFIC_DIR" ]; then chmod 750 $USER_HOME # Owner has rwx, group has r-x, others have no permissions
# Create a user-specific directory inside ROOT_DIRECTORY
mkdir $USER_SPECIFIC_DIR
fi
# Adjust permissions and ownership for the user-specific directory
chown $USERNAME:$ROOT_GROUP $USER_SPECIFIC_DIR
chmod 750 $USER_SPECIFIC_DIR # Owner has rwx, group has r-x, others have no permissions
# Adjust permissions and ownership for the ROOT_DIRECTORY # Adjust permissions and ownership for the ROOT_DIRECTORY
chown -R :$ROOT_GROUP /var/www/webroot/ROOT/ chown -R :$ROOT_GROUP /var/www/webroot/ROOT/
@ -85,3 +78,4 @@ chmod g+s $ROOT_DIRECTORY
HOSTNAME=$(hostname -f) HOSTNAME=$(hostname -f)
log "Script completed for user $USERNAME with hostname $HOSTNAME" log "Script completed for user $USERNAME with hostname $HOSTNAME"
echo "{ \"USERNAME\": \"$USERNAME\", \"PASSWORD\": \"$PASSWORD\" }" echo "{ \"USERNAME\": \"$USERNAME\", \"PASSWORD\": \"$PASSWORD\" }"
exit 0

View File

@ -53,11 +53,34 @@ onInstall:
actions: actions:
add_sftp_user: add_sftp_user:
- cmd[cp]: - cmd[cp]: # Use cmd action to check if user exists
commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh ${globals.username} ${globals.password} commands: id ${globals.username} || echo "User not found"
user: root - if ("${response.out}" == "User not found"): # If user doesn't exist
- if ("${response.exitStatus}" != "0"): - cmd[cp]:
return: sftpError commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh ${globals.username} ${globals.password}
user: root
- switch ("${response.exitStatus}"):
0:
return: sftpSuccess
2:
return: rootDirectoryError
3:
return: userExistsError
4:
return: createUserError
5:
return: symlinkError
6:
return: modifyGroupError
default:
return: unknownError
- else:
- setGlobals:
username: "user${fn.random(10000,99999)}"
- cmd[cp]: # Check again with the new username
commands: id ${globals.username} || echo "User not found"
- if ("${response.out}" != "User not found"):
return: userExistsError
- return: sftpSuccess - return: sftpSuccess
change_password: change_password:
- cmd[cp]: - cmd[cp]:
@ -100,6 +123,18 @@ responses:
deleteUserSuccess: deleteUserSuccess:
type: success type: success
message: "User ${settings.manage_username} deleted successfully." message: "User ${settings.manage_username} deleted successfully."
rootDirectoryError:
type: error
message: "ROOT_DIRECTORY does not exist. Please check the server logs for more details."
symlinkError:
type: error
message: "Failed to create symlink for the user. Check logs for details."
modifyGroupError:
type: error
message: "Failed to modify groups for the user. Check logs for details."
unknownError:
type: error
message: "An unknown error occurred. Please check the server logs for more details."
buttons: buttons:
- settings: sfpform - settings: sfpform
@ -114,3 +149,13 @@ buttons:
action: delete_user action: delete_user
caption: Delete User caption: Delete User
confirmText: "Are you sure you want to delete this user?" confirmText: "Are you sure you want to delete this user?"
onUninstall:
- cmd[cp]:
commands:
- rm -vf /home/jelastic/add-sftp-user-addon/add-sftp.sh
- rm -vf /home/jelastic/add-sftp-user-addon/logs/script_output.log
- rmdir -v /home/jelastic/add-sftp-user-addon/logs/
- rmdir -v /home/jelastic/add-sftp-user-addon/
user: root