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

View File

@ -53,11 +53,34 @@ onInstall:
actions:
add_sftp_user:
- cmd[cp]:
commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh ${globals.username} ${globals.password}
user: root
- if ("${response.exitStatus}" != "0"):
return: sftpError
- cmd[cp]: # Use cmd action to check if user exists
commands: id ${globals.username} || echo "User not found"
- if ("${response.out}" == "User not found"): # If user doesn't exist
- cmd[cp]:
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
change_password:
- cmd[cp]:
@ -100,6 +123,18 @@ responses:
deleteUserSuccess:
type: success
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:
- settings: sfpform
@ -113,4 +148,14 @@ buttons:
- settings: manageUserForm
action: 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