This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathconfigure_user.sh
325 lines (288 loc) · 15.7 KB
/
configure_user.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env bash
###############################################################
### Anarchy Linux Install Script
### configure_user.sh
###
### Copyright (C) 2017 Dylan Schacht
###
### By: Dylan Schacht (deadhead)
### Email: [email protected]
### Webpage: https://anarchylinux.org
###
### Any questions, comments, or bug reports may be sent to above
### email address. Enjoy, and keep on using Arch.
###
### License: GPL v2.0
###############################################################
set_user() {
while (true) # Begin user menu while loop
do
op_title="$user_op_msg"
## Create main user dialog menu from users in $tmp_passwd include root user
user=$(dialog --extra-button --extra-label "$edit" --ok-button "$new_user" --cancel-button "$done_msg" --menu "$user_menu_msg" 13 55 4 \
$(while IFS= read -r i ; do
echo "$i $(<"$tmp_passwd" cut -d: -f1,2 | grep -w $i | cut -d: -f2)"
done <<<"$(sort "$tmp_passwd" | cut -d: -f1)") \
"root" "$root_sh" 3>&1 1>&2 2>&3)
## Check exit status of main user dialog menu
case "$?" in
1) ## if user selects cancel
break
;;
0) ## if user selects add new user
while (true)
do
## prompt user for username
user=$(dialog --cancel-button "$cancel" --ok-button "$ok" --inputbox "\n$user_msg1" 12 55 "" 3>&1 1>&2 2>&3)
if [ "$?" -gt "0" ]; then
break
elif [ -z "$user" ]; then
dialog --ok-button "$ok" --msgbox "\n$user_err_msg2" 10 60
elif (grep "^$user:" "$tmp_passwd" &>/dev/null); then
dialog --ok-button "$ok" --msgbox "\n$user_err_msg1" 10 60
elif (<<<"$user" grep "^[0-9]\|[A-Z]\|[]:/?#@\!\$&'()*+,;=%[]" &> /dev/null); then
dialog --ok-button "$ok" --msgbox "\n$user_err_msg" 10 60
else
while (true)
do
full_user=$(dialog --cancel-button "$cancel" --ok-button "$ok" --inputbox "\n$user_msg2" 12 55 3>&1 1>&2 2>&3)
if [ "$?" != "0" ]; then
break
elif (<<<"$full_user" grep "," &> /dev/null) then
dialog --ok-button "$ok" --msgbox "\n$fulluser_err_msg" 10 60
elif $(cut -d: -f1,4 <"$tmp_passwd" | grep -w "$user" | cut -d: -f2 | grep -w "$full_user"); then
dialog --ok-button "$ok" --msgbox "\n$fulluser_err_msg1" 10 60
else
set_password
echo "$(date -u "+%F %H:%M") : Added user: $user" >> "$log"
if (dialog --yes-button "$yes" --no-button "$no" --yesno "\n$sudo_var" 10 60) then
sudo_user=yes
else
sudo_user=no
fi
echo "${user}:${sh}:${sudo_user}:${full_user}:${pass_crypt}" >> "$tmp_passwd"
if "$menu_enter" ; then
add_user
fi
break
fi
done
break
fi
done
;;
*)
while (true)
do
op_title="$user_op_msg1"
user_sh=$(grep -w "$user" <"$tmp_passwd" | cut -d: -f2)
full_user=$(grep -w "$user" <"$tmp_passwd" | cut -d: -f4)
pass_crypt=$(grep -w "$user" <"$tmp_passwd" | cut -d: -f5)
sudo_user=$(grep -w "$user" <"$tmp_passwd" | cut -d: -f3)
if (grep -w "$user" <"$tmp_passwd" | cut -d: -f3 | grep "yes" &>/dev/null); then
sudo="$yes"
else
sudo="$no"
fi
source "$lang_file"
paswd=$(<"$tmp_passwd" grep -v "$user")
if [ "$user" == "root" ]; then
user_sh="$root_sh"
sudo="$yes"
full_user="superuser"
source "$lang_file"
user_edit=$(dialog --ok-button "$select" --cancel-button "$back" --menu "$user_edit_var" 15 55 2 \
"$change_pass" "->" \
"$change_sh" "->" 3>&1 1>&2 2>&3)
else
user_edit=$(dialog --ok-button "$select" --cancel-button "$back" --menu "$user_edit_var" 17 65 5 \
"$change_pass" "->" \
"$change_sh" "->" \
"$change_su" "->" \
"$change_fn" "->" \
"$del_user" "->" 3>&1 1>&2 2>&3)
fi
case "$user_edit" in
"$change_pass")
set_password
if "$menu_enter" ; then
input="$(echo "$pass_crypt" | openssl enc -aes-256-cbc -a -d -salt -pass pass:"$ssl_key")"
(printf "$input\n$input" | arch-chroot "$ARCH" passwd "$user") &> /dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2passwd $user\Zn" load
echo -e "${paswd}\n${user}:${user_sh}:${sudo_user}:${full_user}:${pass_crypt}" > "$tmp_passwd"
elif [ "$user" == "root" ]; then
root_crypt="${pass_crypt}"
else
echo -e "${paswd}\n${user}:${user_sh}:${sudo_user}:${full_user}:${pass_crypt}" > "$tmp_passwd"
fi
;;
"$change_sh")
user_sh=$(dialog --ok-button "$select" --cancel-button "$cancel" --menu "$user_shell_var" 15 55 6 \
"bash" "$shell5" \
"dash" "$shell0" \
"fish" "$shell1" \
"mksh" "$shell2" \
"tcsh" "$shell3" \
"zsh" "$shell4" 3>&1 1>&2 2>&3)
if [ "$?" -eq "0" ]; then
if ! (<<<"$base_install" grep "$user_sh" &>/dev/null); then
base_install+="$user_sh "
fi
case "$user_sh" in
zsh) user_sh=/usr/bin/zsh
;;
fish) user_sh=/bin/bash
;;
*) user_sh=/bin/"$user_sh"
;;
esac
fi
if "$menu_enter" ; then
arch-chroot "$ARCH" chsh "$user" -s "$user_sh" &>/dev/null
if [ "$user" != "root" ]; then
echo -e "${paswd}\n${user}:${user_sh}:${sudo_user}:${full_user}:${pass_crypt}" > "$tmp_passwd"
fi
elif [ "$user" == "root" ]; then
root_sh="$user_sh"
else
echo -e "${paswd}\n${user}:${user_sh}:${sudo_user}:${full_user}:${pass_crypt}" > "$tmp_passwd"
fi
;;
"$change_su")
if [ "$sudo" == "$yes" ]; then
if (dialog --defaultno --yes-button "$yes" --no-button "$no" --yesno "\n$sudo_var1" 10 60) then
sudo_user=no
fi
else
if (dialog --yes-button "$yes" --no-button "$no" --yesno "\n$sudo_var" 10 60) then
sudo_user=yes
fi
fi
if "$menu_enter" ; then
(sed -i '/%wheel ALL=(ALL) ALL/s/^#//' $ARCH/etc/sudoers
arch-chroot "$ARCH" usermod -a -G wheel "$user") &> /dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2usermod -a -G wheel $user\Zn" load
fi
echo -e "${paswd}\n${user}:${user_sh}:${sudo_user}:${full_user}:${pass_crypt}" > "$tmp_passwd"
;;
"$change_fn")
while (true)
do
full_user=$(dialog --cancel-button "$cancel" --ok-button "$ok" --inputbox "\n$user_msg2" 12 55 3>&1 1>&2 2>&3)
if [ "$?" != "0" ]; then
break
elif (<<<"$full_user" grep "," &> /dev/null) then
dialog --ok-button "$ok" --msgbox "\n$fulluser_err_msg" 10 60
else
if "$menu_enter" ; then
if [ "$full_user" == "" ]; then
full_user="$user"
fi
arch-chroot "$ARCH" chfn -f "$full_user" "$user" &>/dev/null
fi
echo -e "${paswd}\n${user}:${user_sh}:${sudo_user}:${full_user}:${pass_crypt}" > "$tmp_passwd"
break
fi
done
;;
"$del_user")
if (dialog --defaultno --yes-button "$yes" --no-button "$no" --yesno "\n$deluser_var" 10 60) then
if "$menu_enter" ; then
arch-chroot "$ARCH" userdel --remove "$user" &>/dev/null
fi
echo -e "${paswd}" > "$tmp_passwd"
break
else
echo -e "${paswd}\n${user}:${user_sh}:${sudo_user}:${full_user}:${pass_crypt}" > "$tmp_passwd"
break
fi
;;
*) if [ "$user" != "root" ]; then
echo -e "${paswd}\n${user}:${user_sh}:${sudo_user}:${full_user}:${pass_crypt}" > "$tmp_passwd"
fi
break
;;
esac
done
;;
esac
done
}
set_hostname() {
op_title="$host_op_msg"
while (true) ## Begin set hostname loop
do ## Prompt user to enter hostname check for starting with numbers or containg special char
hostname=$(dialog --ok-button "$ok" --nocancel --inputbox "\n$host_msg" 12 55 "anarchy" 3>&1 1>&2 2>&3 | sed 's/ //g')
if (<<<$hostname grep "^[0-9]\|[\[\$\!\'\"\`\\|%&#@()+=<>~;:/?.,^{}]\|]" &> /dev/null); then
dialog --ok-button "$ok" --msgbox "\n$host_err_msg" 10 60
else
break
fi
done
user=root
set_password
root_sh="$sh"
root_crypt="$pass_crypt"
}
set_password() {
source "$lang_file"
op_title="$passwd_op_msg"
while [ "$input" != "$input_chk" ]
do
input=$(dialog --nocancel --clear --insecure --passwordbox "$user_var0" 11 55 --stdout)
input_chk=$(dialog --nocancel --clear --insecure --passwordbox "$user_var1" 11 55 --stdout)
if [ -z "$input" ]; then
dialog --ok-button "$ok" --msgbox "\n$passwd_msg0" 10 55
input_chk=default
elif [ "$input" != "$input_chk" ]; then
dialog --ok-button "$ok" --msgbox "\n$passwd_msg1" 10 55
fi
done
pass_crypt="$(echo "$input" | openssl enc -aes-256-cbc -a -salt -pass pass:"$ssl_key")"
sleep 1 &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2encrypt passwd\Zn" load
unset input input_chk ; input_chk=default
op_title="$user_op_msg"
}
add_user() {
if "$menu_enter" ; then
if [ "$full_user" == "" ]; then
arch-chroot "$ARCH" useradd -m -G audio,network,power,storage,optical -s "$sh" "$user" &>/dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2useradd $user\Zn" load
else
arch-chroot "$ARCH" useradd -m -G audio,network,power,storage,optical -c "$full_name" -s "$sh" "$user" &>/dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2useradd $user\Zn" load
fi
input="$(echo "$pass_crypt" | openssl enc -aes-256-cbc -a -d -salt -pass pass:"$ssl_key")"
(printf "$input\n$input" | arch-chroot "$ARCH" passwd "$user") &> /dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2passwd $user\Zn" load
unset input
echo "$(date -u "+%F %H:%M") : Password set: $user" >> "$log"
if [ "$sudo_user" == "yes" ]; then
(sed -i '/%wheel ALL=(ALL) ALL/s/^#//' $ARCH/etc/sudoers
arch-chroot "$ARCH" usermod -a -G wheel "$user") &> /dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2usermod -a -G wheel $user\Zn" load
fi
else
while IFS= read -r i ; do
if [ "$(<<<"$i" cut -d: -f4)" == "" ]; then
arch-chroot "$ARCH" useradd -m -G audio,network,power,storage,optical -s "$(<<<"$i" cut -d: -f2)" "$(<<<"$i" cut -d: -f1)" &>/dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2useradd $(<<<"$i" cut -d: -f1)\Zn" load
else
arch-chroot "$ARCH" useradd -m -G audio,network,power,storage,optical -c "$(<<<"$i" cut -d: -f4)" -s "$(<<<"$i" cut -d: -f2)" "$(<<<"$i" cut -d: -f1)" &>/dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2useradd $(<<<"$i" cut -d: -f1)\Zn" load
fi
input="$(<<<"$i" cut -d: -f5 | openssl enc -aes-256-cbc -a -d -salt -pass pass:"$ssl_key")"
(printf "$input\n$input" | arch-chroot "$ARCH" passwd "$(<<<"$i" cut -d: -f1)") &> /dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2passwd $(<<<"$i" cut -d: -f1)\Zn" load
unset input
echo "$(date -u "+%F %H:%M") : Password set: $(<<<"$i" cut -d: -f1)" >> "$log"
if [ "$(<<<"$i" cut -d: -f3)" == "yes" ]; then
(sed -i '/%wheel ALL=(ALL) ALL/s/^#//' $ARCH/etc/sudoers
arch-chroot "$ARCH" usermod -a -G wheel "$(<<<"$i" cut -d: -f1)") &> /dev/null &
pid=$! pri=0.1 msg="$wait_load \n\n \Z1> \Z2usermod -a -G wheel $(<<<"$i" cut -d: -f1)\Zn" load
fi
done <<<$(sort "$tmp_passwd")
fi
}
# vim: ai:ts=4:sw=4:et