forked from goose-ws/docker-cups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-management.bash
More file actions
executable file
·243 lines (230 loc) · 5.19 KB
/
user-management.bash
File metadata and controls
executable file
·243 lines (230 loc) · 5.19 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env bash
#PS4='Line ${LINENO}: '
#set -x
users_file="/etc/cups/users_persistent"
function get_persistent_users {
usersRaw="$(getent group lpadmin)"
usersRaw="${usersRaw#*:*:*:}"
IFS="," read -r -a lpUsers <<<"${usersRaw}"
unset shadowArr
for i in "${lpUsers[@]}"; do
while read -r ii; do
if [[ "${ii%%:*}" == "${i}" ]]; then
shadowArr+=("${ii}")
fi
done < "/etc/shadow"
done
}
function load_persistent_users {
if ! [[ -e "${users_file}" ]]; then
echo "No persistent user file found"
exit 0
fi
shadowPrm="$(stat -c "%a" /etc/shadow)"
shadowOwn="$(stat -c "%u" /etc/shadow)"
shadowGrp="$(stat -c "%g" /etc/shadow)"
while read -r i; do
if id "${i%%:*}" > /dev/null 2>&1; then
echo "Stale user ${i%%:*} already exists, removing"
if userdel "${i%%:*}" > /dev/null 2>&1; then
echo "Removed stale user ${i%%:*} successfully"
else
echo "Unable to remove stale user ${i%%:*}"
fi
fi
if useradd -M -s "/bin/false" "${i%%:*}"; then
echo "User ${i%%:*} imported successfully"
else
echo "Unable to import user ${i%%:*}"
exit 1
fi
if usermod -aG lpadmin "${i%%:*}"; then
echo "User ${i%%:*} added to lpadmin group successfully"
else
echo "Unable to add user ${i%%:*} to lpadmin"
exit 1
fi
while read -r ii; do
if [[ "${i%%:*}" == "${ii%%:*}" ]]; then
newShadowArr+=("${i}")
else
inArray="0"
for iii in "${newShadowArr[@]}"; do
if [[ "${iii%%:*}" == "${ii%%:*}" ]]; then
inArray="1"
fi
done
if [[ "${inArray}" -eq "0" ]]; then
newShadowArr+=("${ii}")
fi
fi
done < "/etc/shadow"
done < "${users_file}"
cp "/etc/shadow" "/etc/shadow.old"
( for i in "${newShadowArr[@]}"; do echo "${i}"; done ) > "/etc/shadow"
chmod "${shadowPrm}" "/etc/shadow"
chown "${shadowOwn}:${shadowGrp}" "/etc/shadow"
importSuccess="0"
while read -r i; do
while read -r ii; do
if [[ "${i}" == "${ii}" ]]; then
echo "Password for user ${i%%:*} imported successfully"
importSuccess="1"
break
fi
done < "/etc/shadow"
if [[ "${importSuccess}" -eq "0" ]]; then
echo "Unable to import password for user ${i%%:*}"
fi
done < "${users_file}"
}
function save_persistent_users {
get_persistent_users
( for i in "${shadowArr[@]}"; do echo "${i}"; done ) > "${users_file}"
shadowPrm="$(stat -c "%a" /etc/shadow)"
shadowOwn="$(stat -c "%u" /etc/shadow)"
shadowGrp="$(stat -c "%g" /etc/shadow)"
chmod "${shadowPrm}" "${users_file}"
chown "${shadowOwn}:${shadowGrp}" "${users_file}"
for i in "${shadowArr[@]}"; do
persistSave="0"
while read -r ii; do
if [[ "${i}" == "${ii}" ]]; then
persistSave="1"
break
fi
done < "${users_file}"
if [[ "${persistSave}" -eq "1" ]]; then
echo "Verified user saved to persistent file: ${i%%:*}"
else
echo "Unable to save user to persistent file: ${i%%:*}"
fi
done
}
function list_users {
get_persistent_users
echo ""
for i in "${lpUsers[@]}"; do
echo " ${i}"
done
}
function add_user {
echo "Please enter the username you would like to add"
read -r -p "> " newUser
if id "${newUser}" > /dev/null 2>&1; then
echo "User already exists"
exit 1
fi
echo ""
echo "Please enter the password for ${newUser} (shown in clear text)"
read -r -p "> " newPassword
if [[ -z "${newPassword}" ]]; then
echo "No password detected"
exit 1
fi
echo ""
echo "Attempting to add new user"
if useradd -M -s "/bin/false" "${newUser}"; then
echo "User created successfully"
else
echo "Unable to create user"
exit 1
fi
if echo "${newUser}:${newPassword}" | chpasswd; then
echo "User password set successfully"
else
echo "Unable to set user password"
exit 1
fi
if usermod -aG lpadmin "${newUser}"; then
echo "User added to lpadmin group successfully"
else
echo "Unable to add user to lpadmin"
exit 1
fi
echo "User added successfully."
}
function pick_user {
get_persistent_users
echo ""
n="0"
for i in "${lpUsers[@]}"; do
echo " [${n}] ${i}"
(( n++ ))
done
echo ""
echo "Please enter the user number"
read -r -p "> " userInput
}
function del_user {
pick_user
echo ""
if [[ -n "${lpUsers[${userInput}]}" ]]; then
if deluser "${lpUsers[${userInput}]}"; then
echo "User ${lpUsers[${userInput}]} removed successfully"
else
echo "Failed to remove user ${lpUsers[${userInput}]}"
fi
else
echo "Invald option"
fi
}
function change_pw {
pick_user
echo ""
if [[ -n "${lpUsers[${userInput}]}" ]]; then
if passwd "${lpUsers[${userInput}]}"; then
echo "Password succesfully changed for user ${lpUsers[${userInput}]}"
else
echo "Failed to change password for user ${lpUsers[${userInput}]}"
fi
else
echo "Invald option"
fi
}
function print_menu {
echo "Choose an option:"
echo ""
echo " [0] Exit"
echo ""
echo " [1] List printer users"
echo " [2] Add a new user"
echo " [3] Remove an existing user"
echo " [4] Change an existing user's password"
echo ""
}
if [[ "${1}" == "--load-persistent-users" ]]; then
load_persistent_users
exit 0
elif [[ "${1}" == "--save-persistent-users" ]]; then
save_persistent_users
exit 0
fi
print_menu
while read -r -p "> " userInput; do
case "${userInput}" in
0)
save_persistent_users
exit 0
;;
1)
list_users
;;
2)
add_user
;;
3)
del_user
;;
4)
change_pw
;;
*)
echo "Invalid option"
;;
esac
echo ""
read -r -p "Press [Enter] to return to main menu"
clear
print_menu
done