-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadduser.sh
150 lines (136 loc) · 4.64 KB
/
adduser.sh
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
#!/bin/bash
# ANSI color codes
RED='\033[0;31m'
GOLD='\033[0;33m'
BOLD='\033[1m'
NC='\033[0m' # No Color
info() {
echo -e "${BOLD}${GOLD}[INFO] $1${NC}"
}
warn() {
echo -e "${BOLD}${RED}[WARN] $1${NC}"
}
error() {
echo -e "${BOLD}${RED}[ERROR] $1${NC}"
}
setup_pg_directory() {
if [[ -d "/pg" ]]; then
find /pg -type d -exec chmod 755 {} + 2>/dev/null
find /pg -type f -exec chmod 644 {} + 2>/dev/null
chown -R 1000:1000 /pg 2>/dev/null
fi
}
check_existing_config() {
if [[ -f "/pg/config/username.cfg" ]]; then
if grep -q "username" "/pg/config/username.cfg"; then
info "Existing configuration found in /pg/config/username.cfg. Skipping user setup."
exit 0
fi
else
local existing_user=$(id -nu 1000 2>/dev/null)
if [[ -n "$existing_user" ]]; then
warn "User with UID 1000 detected: $existing_user"
read -p "Would you like to keep this user $existing_user? (y/N): " keep_user
if [[ "$keep_user" == [yY] ]]; then
info "Keeping user $existing_user and storing in configuration."
mkdir -p /pg/config
echo "username=\"$existing_user\"" > /pg/config/username.cfg
chown 1000:1000 /pg/config/username.cfg
chmod 744 /pg/config/username.cfg
info "Configuration file created at /pg/config/username.cfg"
exit 0
fi
fi
fi
}
validate_password() {
local pass="$1"
if [[ ${#pass} -lt 10 ]]; then
return 1
fi
if ! [[ "$pass" =~ [0-9] ]]; then
return 1
fi
return 0
}
remove_1000_user() {
local existing_user=$(id -nu 1000 2>/dev/null)
if [ -n "$existing_user" ]; then
echo "" && warn "User with UID 1000 found: $existing_user"
warn "This will repeat unless user has been created by PlexGuide."
read -p "This user will be removed. Are you sure? (y/N): " confirm
if [[ $confirm == [yY] ]]; then
info "Removing user $existing_user..."
pkill -u "$existing_user" # Kill all processes owned by the user
if userdel -r "$existing_user"; then
info "User $existing_user has been removed successfully."
else
error "Failed to remove user $existing_user. Exiting."
exit 1
fi
else
error "User removal cancelled. Exiting."
exit 1
fi
else
info "No user with UID 1000 found. Proceeding with new user creation."
fi
}
setup_user() {
info "Setting up new user account..."
local username password password_confirm
while true; do
read -rp "Enter username (lowercase letters and numbers only): " username
if [[ "$username" =~ ^[a-z][a-z0-9]*$ ]]; then
if ! id "$username" &>/dev/null; then
break
else
warn "User $username already exists. Please choose a different username."
fi
else
warn "Invalid username."
warn "Lowercase letters and numbers only; starting with a letter."
fi
done
while true; do
while true; do
read -s -rp "Enter password (min 10 characters and at least one number): " password
echo
if validate_password "$password"; then
break
else
warn "Invalid Password!"
warn "Must be at least 10 characters long and contain at least one number."
fi
done
read -s -rp "Confirm password: " password_confirm
echo
if [ "$password" = "$password_confirm" ]; then
break
else
warn "Passwords do not match. Please try again."
fi
done
if useradd -m -s /bin/bash -U -u 1000 "$username"; then
echo "$username:$password" | chpasswd
usermod -aG sudo,video,docker "$username"
info "User $username has been created successfully with UID 1000."
# Create config directory and file
mkdir -p /pg/config
echo "username=\"$username\"" > /pg/config/username.cfg
chown 1000:1000 /pg/config/username.cfg
chmod 744 /pg/config/username.cfg
info "Configuration file created at /pg/config/username.cfg"
else
error "Failed to create user $username with UID 1000."
exit 1
fi
}
# Setup /pg directory permissions in the background
setup_pg_directory &
# Check for existing configuration or UID 1000 user
check_existing_config
# Main execution
remove_1000_user
setup_user
info "User setup complete. New primary user account: $(id -nu 1000)"