Optimize user creation and logging

main
Anthony 2023-11-03 01:12:51 +08:00
parent 7f3baa84fd
commit 200dc954fd
3 changed files with 16 additions and 37 deletions

View File

@ -1,7 +1,5 @@
#!/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
@ -16,10 +14,11 @@ log() {
fi
}
error_exit() {
log "ERROR: $1"
exit $2
}
# Check if user already exists
if id "$1" &>/dev/null; then
echo "{ \"error\": \"User $1 already exists.\" }"
exit 1
fi
# Generate random username and password
USERNAME=$1
@ -33,14 +32,11 @@ ROOT_DIRECTORY="/var/www/webroot/ROOT"
log "Script started"
# Ensure the ROOT_DIRECTORY exists
[ ! -d "$ROOT_DIRECTORY" ] && error_exit "ROOT_DIRECTORY $ROOT_DIRECTORY does not exist." 2
[ ! -d "$ROOT_DIRECTORY" ] && { echo "{ \"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

View File

@ -64,27 +64,17 @@ 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:
- 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
- cmd[cp]:
user: root
commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh ${globals.username} ${globals.password}
- if ("${response.exitStatus}" != "0"):
return: sftpError
- setGlobals:
username: ${fn.extract(response.out, "\"USERNAME\": \"(.*?)\"")}
password: ${fn.extract(response.out, "\"PASSWORD\": \"(.*?)\"")}
- return: sftpSuccess
change_password:
- cmd[cp]:
@ -108,7 +98,7 @@ actions:
list_users:
- cmd[cp]:
user: root
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"
commands: "journalctl -u sshd | grep 'user[0-9]\\{5\\}' | awk '{printf \"Created: %s %s %s Username: %s\\n\", $1, $2, $3, $NF}' | sort -u"
- return: listUsers
responses:
@ -117,7 +107,7 @@ responses:
message: "Installed Successfully"
sftpError:
type: error
message: "Failed to add SFTP user. Please check the server logs for more details."
message: "Failed to add SFTP user. Check the 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}"

View File

@ -1,7 +0,0 @@
#!/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