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 #!/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" 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 # Set to 1 for verbose mode, 0 for normal mode
@ -16,10 +14,11 @@ log() {
fi fi
} }
error_exit() { # Check if user already exists
log "ERROR: $1" if id "$1" &>/dev/null; then
exit $2 echo "{ \"error\": \"User $1 already exists.\" }"
} exit 1
fi
# Generate random username and password # Generate random username and password
USERNAME=$1 USERNAME=$1
@ -33,14 +32,11 @@ ROOT_DIRECTORY="/var/www/webroot/ROOT"
log "Script started" log "Script started"
# Ensure the ROOT_DIRECTORY exists # 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 # Get the group ownership of the ROOT_DIRECTORY
ROOT_GROUP=$(stat -c '%G' $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/ # Create user with their own directory under /home/sftpusers/
mkdir -p $USER_HOME mkdir -p $USER_HOME
useradd -d $USER_HOME $USERNAME useradd -d $USER_HOME $USERNAME

View File

@ -64,27 +64,17 @@ menu:
loadingText: "Loading users..." loadingText: "Loading users..."
action: "list_users" action: "list_users"
caption: "List Users" caption: "List Users"
successText: "Users listed successfully!"
logsPath: "/home/jelastic/add-sftp-user-addon/logs/sftp-users.log"
logsNodeGroup: cp
actions: actions:
add_sftp_user: add_sftp_user:
- cmd[cp]: # Use cmd action to check if user exists - cmd[cp]:
commands: id ${globals.username} || echo "User not found" user: root
- if ("${response.out}" == "User not found"): # If user doesn't exist commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh ${globals.username} ${globals.password}
- cmd[cp]: - if ("${response.exitStatus}" != "0"):
user: root return: sftpError
commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh ${globals.username} ${globals.password} - setGlobals:
- if ("${response.exitStatus}" != "0"): username: ${fn.extract(response.out, "\"USERNAME\": \"(.*?)\"")}
return: createUserError password: ${fn.extract(response.out, "\"PASSWORD\": \"(.*?)\"")}
- 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]:
@ -108,7 +98,7 @@ actions:
list_users: list_users:
- cmd[cp]: - cmd[cp]:
user: root 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 - return: listUsers
responses: responses:
@ -117,7 +107,7 @@ responses:
message: "Installed Successfully" message: "Installed Successfully"
sftpError: sftpError:
type: error 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: sftpSuccess:
type: success 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}" 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