From d7c212e18a87b2c213c57e99b1ea0454a8fd7657 Mon Sep 17 00:00:00 2001 From: Anthony Date: Fri, 3 Nov 2023 01:26:02 +0800 Subject: [PATCH] Revert "Optimize user creation and logging" This reverts commit 200dc954fd8795d878d84c689eb75baf0ac78d66. --- add-sftp.sh | 16 ++++++++++------ manifest.jps | 25 +++++++++++++++++++++++-- scripts/userlogs.sh | 7 +++++++ 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 scripts/userlogs.sh diff --git a/add-sftp.sh b/add-sftp.sh index d8cfba2..43d406c 100644 --- a/add-sftp.sh +++ b/add-sftp.sh @@ -1,5 +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 @@ -14,11 +16,10 @@ log() { fi } -# Check if user already exists -if id "$1" &>/dev/null; then - echo "{ \"error\": \"User $1 already exists.\" }" - exit 1 -fi +error_exit() { + log "ERROR: $1" + exit $2 +} # Generate random username and password USERNAME=$1 @@ -32,11 +33,14 @@ ROOT_DIRECTORY="/var/www/webroot/ROOT" log "Script started" # Ensure the ROOT_DIRECTORY exists -[ ! -d "$ROOT_DIRECTORY" ] && { echo "{ \"error\": \"ROOT_DIRECTORY $ROOT_DIRECTORY does not exist.\" }"; exit 2; } +[ ! -d "$ROOT_DIRECTORY" ] && error_exit "ROOT_DIRECTORY $ROOT_DIRECTORY does not exist." 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 diff --git a/manifest.jps b/manifest.jps index a395f4c..3a21d1e 100644 --- a/manifest.jps +++ b/manifest.jps @@ -64,9 +64,13 @@ menu: loadingText: "Loading users..." action: "list_users" caption: "List Users" + successText: "Users listed successfully!" + logsPath: "/home/jelastic/add-sftp-user-addon/logs/sftp-users.log" + logsNodeGroup: cp actions: add_sftp_user: +<<<<<<< HEAD - cmd[cp]: user: root commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh ${globals.username} ${globals.password} @@ -75,6 +79,23 @@ actions: - setGlobals: username: "${fn.extract(response.out, "\"USERNAME\": \"(.*?)\"")}" password: "${fn.extract(response.out, "\"PASSWORD\": \"(.*?)\"")}" +======= + - 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]: + user: root + commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh ${globals.username} ${globals.password} + - if ("${response.exitStatus}" != "0"): + return: createUserError + - 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 +>>>>>>> parent of 200dc95 (Optimize user creation and logging) - return: sftpSuccess change_password: - cmd[cp]: @@ -98,7 +119,7 @@ actions: list_users: - cmd[cp]: user: root - commands: "journalctl -u sshd | grep 'user[0-9]\\{5\\}' | awk '{printf \"Created: %s %s %s Username: %s\\n\", $1, $2, $3, $NF}' | sort -u" + commands: "grep -oP '^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} - user\\d{5}' /home/litespeed/add-sftp-user-addon/logs/script_output.log | while read line; do printf \"%s\\n\" \"$line\"; done" - return: listUsers responses: @@ -107,7 +128,7 @@ responses: message: "Installed Successfully" sftpError: type: error - message: "Failed to add SFTP user. Check the logs for more details." + message: "Failed to add SFTP user. Please check the server logs for more details." sftpSuccess: type: success message: "Connection Details\n\nSFTP Host: ${globals.sftpHost}\n\nPort: ${globals.sftpPort}\n\nLogin Credentials\n\nUsername: ${globals.username}\n\nPassword: ${globals.password}" diff --git a/scripts/userlogs.sh b/scripts/userlogs.sh new file mode 100644 index 0000000..e1c8c7d --- /dev/null +++ b/scripts/userlogs.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +getent passwd | awk -F: '/^user/ {print $1}' | while read -r user; do + # Attempt to get the user's creation date from the shadow file + creation_date=$(sudo chage -l "$user" | grep 'Last password change' | cut -d: -f2) + echo "$user was created on $creation_date" +done