-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate-user
183 lines (157 loc) · 6.02 KB
/
create-user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
# -- Single File PHP File Browser - User Management Script --
# This script will create (or remove) a file to store credentials.
# The upload section on index.php will only show up with this file.
# It will create and delete users, and it will hash passwords.
# Tarso Galvão - 19/09/2024 - Debian 12
# github.com/surtarso/single-file-php-file-browser
# Set file to store credentials
# If changed here, it also needs to be changed in index.php
# Global variable: $credentialsFile = './.users'; and vice-versa
credentialsFile=".users"
# Get username and password from arguments
username="$1"
password="$2"
# Bash colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# ------------------------------- FUNCTIONS ----------------------------------
# Function to display an error message and exit the script
function exit_with_error() {
case "$1" in
wrong-args)
echo -e "${YELLOW}Single File PHP File Browser${NC}"
echo -e "User management script usage:"
echo -e "Add user: ${YELLOW}$0 ${GREEN}username password${NC}"
echo -e "Del user: ${YELLOW}$0 ${GREEN}username ${RED}--delete${NC}"
;;
file-create)
echo -e "${RED}Error: Could not create ${YELLOW}'$credentialsFile'${RED}. Check permissions.${NC}"
;;
file-write)
echo -e "${RED}Write Error: Could not write to ${YELLOW}'$credentialsFile'${RED}. Check permissions.${NC}"
;;
file-delete)
echo -e "${RED}Error: Could not remove ${YELLOW}'$credentialsFile'${RED}. Check permissions.${NC}"
;;
file-read)
echo -e "${RED}Read Error: Could not scan for ${YELLOW}'$username'${RED}. Check file permissions.${NC}"
;;
delete-user)
echo -e "${RED}Error: Could not delete ${YELLOW}'$username'${RED}. Check file permissions.${NC}"
;;
username-null)
echo -e "${RED}Error: Username ${YELLOW}'$username'${RED} doesn't exit.${NC}"
;;
username-exists)
echo -e "${RED}Error: Username ${YELLOW}'$username'${RED} already exists.${NC}"
;;
validation-failed)
echo -e "${RED}Error: Invalid credentials! Please use only numbers and letters."
echo -e "Username: Must be 4-20 characters long."
echo -e "Password: Must be 8-20 characters long.${NC}"
;;
hashing-failed)
echo -e "${RED}Error: Could not hash the password. Do you have PHP installed?${NC}"
;;
*)
echo -e "${RED}Error: ${YELLOW}Uh oh... Unknown error${RED}.${NC}"
echo -e "Please create an issue on 'github.com/surtarso/single-file-php-file-browser'"
echo -e "and reproduce the steps taken for this message to show. Thanks! <3"
;;
esac
exit 1
}
# Function to validade credentials
function validate_credentials() {
usernameRegex="^[a-zA-Z0-9_]{4,20}$"
passwordRegex="^[a-zA-Z0-9_]{8,20}$"
if [[ ! "$username" =~ $usernameRegex ]] ||
[[ ! "$password" =~ $passwordRegex ]]; then
return 1
fi
return 0
}
# Function to check if user exists
function user_exists() {
if [[ ! -f "$credentialsFile" ]]; then
return 1
fi
grep -q "^$username:" "$credentialsFile" || exit_with_error file-read
}
# Function to delete empty .users file
function delete_credentials_file() {
if [[ ! -f "$credentialsFile" ]]; then
echo -e "${RED}Error: File ${YELLOW}'$credentialsFile'${RED} not found.${NC}"
return 1
fi
# Scan file for content
while IFS= read -r line; do
if [[ -n "$line" ]]; then
return 0 # Non-empty line found
fi
done <"$credentialsFile"
# If the loop completes without returning, the file is empty, so delete it.
rm -f "$credentialsFile" "$credentialsFile.tmp" 2>/dev/null || exit_with_error file-delete
echo -e "${YELLOW}'$username'${NC} was the last user credential."
echo -e "${YELLOW}Uploads are now disabled.${NC}"
}
# Function to delete a user
function delete_user() {
if ! user_exists; then
exit_with_error username-null
fi
# Get username from arguments and remove from users file
grep -v "^$username:" "$credentialsFile" >"$credentialsFile".tmp &&
mv "$credentialsFile".tmp "$credentialsFile" || exit_with_error delete-user
echo -e "User ${YELLOW}'$username'${NC} deleted."
}
# Function to create the file where credentials will be stored
function create_credentials_file () {
if [[ -f "$credentialsFile" ]]; then
return 0
fi
# If credentials are on the very first line of the file,
# this script is unable to remove it later for some reason.
# So we echo an empty line to overcome this.
echo "" >>"$credentialsFile" || exit_with_error file-create
echo -e "${GREEN}Uploads are now enabled.${NC}"
}
# Function to create a user
function create_user() {
# Hash the password using PHP
hashedPassword=$(php -r "echo password_hash('$password', PASSWORD_DEFAULT);") || exit_with_error hashing-failed
# Append the user and hashed password to the users file
echo "$username:$hashedPassword" >>"$credentialsFile" || exit_with_error file-write
echo -e "User ${YELLOW}'$username'${NC} added."
}
# -------------------------- MAIN LOGIC ---------------------------
# Check for required arguments
if [[ $# -ne 2 ]]; then
exit_with_error wrong-args
fi
# DELETION --------------------------------------------------------
# Check if it's a deletion
if [[ "$password" = "--delete" ]]; then
# Attempt to delete the user
delete_user
# Delete credentials file if empty
delete_credentials_file
exit 0
fi
# CREATION --------------------------------------------------------
# Check if given username already exists
if user_exists; then
exit_with_error username-exists
# Check if given username and password are valid using regex
elif ! validate_credentials; then
exit_with_error validation-failed
else
# Create credentials file if none exists
create_credentials_file
# Attempt to create the user
create_user
exit 0
fi