· 6 years ago · Jan 04, 2020, 10:50 PM
1
2function echo_with_color() {
3 # Printa ut text med ansi-koder och printf-formatering.
4 # 1:a argumentet är ansi-koderna att applicera på texten
5 # 2:a argumentet är texten att skriva ut
6 printf "\e[$1m"
7 printf $2 "${@:3}"
8 printf "\e[0m"
9}
10
11function handle_errors() {
12 # Runs supplied command and prints out error messages as needed
13 # The 1:st argument is the command to be executed
14 # The 2:nd argument is the message to be displayed on failiure
15 # The 3:rd argument is the message to be displayed on success
16 # Returns the exit code of the command
17 eval $1 &> /tmp/stderr
18 status=$?
19 if [ $status -ne 0 ]
20 then
21 echo_with_color 31 "%s\n" "$2"
22 echo_with_color 31 "%s\n" "The following error message was generated:"
23 cat /tmp/stderr
24 rm /tmp/stderr
25 else
26 echo_with_color 31 "%s\n" "$3"
27 fi
28 return $status
29}
30
31function sanetize_input() {
32 # Cleans input from nasty chars
33 echo "$@" | sed 's/["]/\\"/g' | sed "s/[']/\\\\'/g" | sed 's/[;]/\\;/g' | sed 's/[$]/\\\$/g' | sed 's/[*]/\\*/g' | sed 's/[&]/\\&/g'
34}
35
36function validate_username() {
37 # Makes sure that the supplied string is a calid username
38 echo $1 | egrep "[a-z_][a-z0-9_-]*[$]?" | wc -L
39}
40
41function validate_uid() {
42 # Makes sure that the supplied string is a valid uid
43 echo $1 | egrep -o "[0-9]*" | wc -L
44}
45
46function add_group() {
47 # Adds a group.
48 while true
49 do
50 echo_with_color 31 "%65s" "Group name:"
51 read groupname
52 groupname=$(sanetize_input $groupname)
53 validation=$(validate_username $groupname)
54 if [ "$validation" -ne 0 -a "$validation" -le 32 ] # Make sure that the group name is valid
55 then
56 break
57 fi
58 done
59
60 handle_errors "sudo groupadd $groupname" "Couldn't add group!" "Successfully added group."
61}
62
63function add_user() {
64 # Adds a user.
65
66 # First, ask user for input...
67 while true
68 do
69 echo_with_color 31 "%65s" "Username:"
70 read username
71 username=$(sanetize_input $username)
72 validation=$(validate_username $username)
73 if [ "$validation" -ne 0 -a "$validation" -le 32 ] # Make sure that the username is valid.
74 then
75 break
76 fi
77 done
78
79 echo_with_color 31 "%65s" "User Description/Full Name:"
80 read description
81 description=$(sanetize_input $description)
82
83 echo_with_color 31 "%65s" "Create user group? (Y/n):"
84 read create_user_group
85 create_user_group=$(sanetize_input $create_user_group)
86
87 echo_with_color 31 "%65s" "Other groups to add user to (separated with comma, no space):"
88 read other_groups_to_add
89 other_groups_to_add=$(sanetize_input $other_groups_to_add)
90
91 if [ "$create_user_group" == "n" ]
92 then
93 echo_with_color 31 "%65s" "Main group for user (leave blank for default):"
94 read main_group
95 main_group=$(sanetize_input $main_group)
96 fi
97
98 echo_with_color 31 "%65s" "Create home folder? (Y/n):"
99 read create_home_folder
100 create_home_folder=$(sanetize_input $create_home_folder)
101
102 if [ "$create_home_folder" != "n" ]
103 then
104 echo_with_color 31 "%65s" "Location of home folder (leave blank for default):"
105 read home_folder_dir
106 fi
107
108 echo_with_color 31 "%65s" "User shell (leave blank for default):"
109 read shell
110
111 # ...then construct the command
112
113 command_string="sudo useradd"
114 command_string="$command_string -c \"$description\""
115 if [ "$create_user_group" == "n" ]
116 then
117 command_string="$command_string -N"
118 else
119 command_string="$command_string -U"
120 fi
121 if [ -n "$other_groups_to_add" ]
122 then
123 command_string="$command_string -G \"$other_groups_to_add\""
124 fi
125 if [ -n "$main_group" ]
126 then
127 command_string="$command_string -g $main_group"
128 fi
129 if [ "$create_home_folder" == "n" ]
130 then
131 command_string="$command_string -M"
132 else
133 command_string="$command_string -m"
134 fi
135 if [ -n "$home_folder_dir" ]
136 then
137 command_string="$command_string -d $home_folder_dir"
138 fi
139 if [ -n "$shell" ]
140 then
141 command_string="$command_string -s \"$shell\""
142 fi
143 command_string="$command_string $username"
144
145 handle_errors "$command_string" "Couldn't add user!" "Successfully added user."
146}
147
148function change_passwd() {
149 # Changes the given users password
150
151 username=""
152 while [ -z "$username" ]
153 do
154 echo_with_color 31 "%s" "User to change password of:"
155 read username
156 username=$(sanetize_input $username)
157 done
158
159 # Since we want to see the output of passwd (the prompt), we cant use handle_errors here.
160 sudo passwd "$username"
161
162 if [ $? -ne 0 ]
163 then
164 echo_with_color 31 "%s\n" "Couldn't change the password!"
165 echo_with_color 31 "%s\n" "The following error message was generated:"
166 cat /tmp/stderr
167 rm /tmp/stderr
168 else
169 echo_with_color 31 "%s\n" "Successfully changed password."
170 fi
171}
172
173function delete_passwd() {
174 # Delete the users password
175
176 username=""
177 while [ -z "$username" ]
178 do
179 echo_with_color 31 "%s" "User to remove password from:"
180 read username
181 username=$(sanetize_input $username)
182 done
183
184 handle_errors "sudo passwd -d $username" "Couldn't remove password!" "Successfully removed password."
185}
186
187function mod_user() {
188 # Show a menu with different options, letting the user choose different attributes to change
189
190 while true
191 do
192 echo_with_color 31 "%s" "Username: "
193 read username
194 username=$(sanetize_input $username)
195 validation=$(validate_username $username)
196 userstring=$(egrep "^$username:" /etc/passwd)
197 if [ "$validation" -ne 0 -a "$validation" -le 32 -a -n "$userstring" ] # Make sure the username is valid and that the user exists
198 then
199 break
200 fi
201 done
202
203 while true
204 do
205 # Menu
206 echo_with_color 31 "%s\n" "Selected user: $username"
207 echo_with_color 31 "%s\n" "Change username: u"
208 echo_with_color 31 "%s\n" "Change uid: i"
209 echo_with_color 31 "%s\n" "Change main group: g"
210 echo_with_color 31 "%s\n" "Change description: d"
211 echo_with_color 31 "%s\n" "Change home directory: h"
212 echo_with_color 31 "%s\n" "Change shell: s"
213 echo_with_color 31 "%s\n" "Quit: q"
214 echo_with_color 31 "%s" "Option: "
215 read option
216
217 case "$option" in
218 "u")
219 # Change username
220 while true
221 do
222 echo_with_color 31 "%s" "New username: "
223 read newusername
224 newusername=$(sanetize_input $newusername)
225 validation=$(validate_username $newusername)
226 if [ "$validation" -ne 0 -a "$validation" -le 32 ] # Make sure username is valid
227 then
228 break
229 fi
230 done
231 command_string="sudo usermod -l $newusername $username"
232 handle_errors "$command_string" "Couldn't change username!" "Successfully changed username." && username=$newusername
233 ;;
234 "i")
235 # Change UID
236 while true
237 do
238 echo_with_color 31 "%s" "New UID: "
239 read uid
240 validation=$(validate_uid $uid)
241 if [ "$validation" -ne 0 ] # Make sure UID is valid
242 then
243 break
244 fi
245 done
246 command_string="sudo usermod -u $uid $username"
247 handle_errors "$command_string" "Couldn't change uid!" "Successfully changed uid."
248 ;;
249 "g")
250 # Change primary group
251 echo_with_color 31 "%s" "New main group: "
252 read group
253 group=$(sanetize_input $group)
254 command_string="sudo usermod -g $group $username"
255 handle_errors "$command_string" "Couldn't change main group!" "Successfully changed main group."
256 ;;
257 "d")
258 # Change user commen/description
259 echo_with_color 31 "%s" "New description: "
260 read comment
261 comment=$(sanetize_input $comment)
262 command_string="sudo usermod -c \"$comment\" $username"
263 handle_errors "$command_string" "Couldn't change description!" "Successfully changed description."
264 ;;
265 "h")
266 # Change home directory
267 oldhome=$(egrep "^$username" /etc/passwd | awk -F: '{print $6}')
268 echo_with_color 31 "%s" "New home path: "
269 read newhome
270 newhome=$(sanetize_input $newhome)
271 echo_with_color 31 "%s" "Move contents of old home folder? [Y/n]: "
272 read movehome
273 if [ "$movehome" == "n" ]
274 then
275 command_string="sudo usermod -d \"$newhome\" $username"
276 else
277 command_string="sudo usermod -m -d \"$newhome\" $username" # Add -m flag to move home
278 fi
279 handle_errors "$command_string" "Couldn't change home path!" "Successfully changed home path."
280
281 # If we couldn't move the home dir contents, revert to the original home dir for the user
282 if [ $? -ne 0 -a "$movehome" != "n" ]
283 then
284 echo_with_color 31 "%s\n" "Couldn't move home directory contents, reverting to old home directory."
285 sudo usermod -d "$oldhome" $username
286 fi
287 ;;
288 "s")
289 # Change the users shell
290 while true
291 do
292 echo_with_color 31 "%s" "New shell path: "
293 read shellpath
294 shellpath=$(sanetize_input $shellpath)
295 if [ -e "$shellpath" ] # Make sure the executable exists
296 then
297 break
298 else
299 echo_with_color 31 "%s\n" "No such file."
300 fi
301 done
302 command_string="sudo usermod -s $shellpath $username"
303 handle_errors "$command_string" "Couldn't change shell!" "Successfully changed shell."
304 ;;
305 "q")
306 # Leave the menu
307 break
308 ;;
309 *)
310 echo_with_color 31 "%s\n" "No such option."
311 ;;
312 esac
313 done
314}
315
316function add_user_to_group() {
317 # Add a user to a group
318 username=""
319 while [ -z "$username" ] # Make sure we actually get input
320 do
321 echo_with_color 31 "%s" "Username:"
322 read username
323 username=$(sanetize_input $username)
324 done
325
326 groupname=""
327 while [ -z "$groupname" ]
328 do
329 echo_with_color 31 "%s" "Groupname:"
330 read groupname
331 groupname=$(sanetize_input $groupname)
332 done
333
334 handle_errors "sudo usermod -a -G $groupname $username" "Couldn't add user to group!" "Successfully added user to group."
335}
336
337function remove_user_from_group() {
338 # Remove the a user from a group
339 echo_with_color 31 "%s" "Username:"
340 read username
341 username=$(sanetize_input $username)
342 userstring=$(egrep "^$username:" /etc/passwd)
343 if [ -z "$userstring" -o -z "$username" ] # Make sure we get input, and that the user exists
344 then
345 echo_with_color 31 "%s\n" "User not found!"
346 else
347 usergroups=$(groups $username | sed "s/$username : //") # List of groups the user is part of
348
349 groupname=""
350 while [ -z "$groupname" ] # Make sure we get input
351 do
352 echo_with_color 31 "%s" "Groupname:"
353 read groupname
354 groupname=$(sanetize_input $groupname)
355 done
356
357 if echo "$usergroups" | grep $groupname > /dev/null # Make sure the user is part of the group
358 then
359 newusergroups=$(echo "$usergroups" | sed 's/ /,/g' | sed "s/$groupname//g" | sed 's/,$//' | sed 's/,,/,/') # Remove the group from the users list of groups
360 handle_errors "sudo usermod -G \"$newusergroups\" $username" "Couldn't remove user from group!" "Successfully removed user from group."
361 else
362 echo "The user is not in that group!"
363 fi
364 fi
365}
366
367function remove_group() {
368 # Remove a group
369 echo_with_color 31 "%s" "Group to remove:"
370 read groupname
371 groupname=$(sanetize_input $groupname)
372 groupstring=$(egrep "^$groupname:" /etc/group)
373 if [ -z "$groupstring" -o -z "$groupname" ] # Make sure the group exists
374 then
375 echo_with_color 31 "%s\n" "Group not found!"
376 else
377 gid=$(egrep "^$groupname" /etc/group | awk -F: '{print $3}')
378 if [ "$gid" -gt 1000 ] # Only allow removal of non-system groups
379 then
380 handle_errors "sudo groupdel $groupname" "Couldn't remove group!" "Successfully removed group."
381 else
382 echo "Cannot remove system group!"
383 fi
384 fi
385}
386
387function remove_user() {
388 # Remove a user
389 username=""
390 while [ -z "$username" ] # Make sure we get input
391 do
392 echo_with_color 31 "%s" "User to remove:"
393 read username
394 username=$(sanetize_input $username)
395 done
396
397 echo_with_color 31 "%s" "Remove home directory? (y/N):"
398 read remove_home
399
400 if [ "$remove_home" == y ]
401 then
402 handle_errors "sudo userdel -r $username" "Couldn't remove user and/or home directory!" "Successfully removed user and home directory."
403 else
404 handle_errors "sudo userdel $username" "Couldn't remove user!" "Successfully removed user."
405 fi
406}
407
408function show_groups() {
409 # Show system groups
410 echo_with_color 31 "%s" "Show all groups or only non-system groups? (a/N):"
411 read choice
412 if [ "$choice" == A -o "$choice" == a ] # Show all groups
413 then
414 output=$(echo "Group name: :Gid:Users" | awk -F: '{print $1": "$3": "$4}' /dev/stdin /etc/group | column -s: -t) # Make a nice table with column
415 if [ $(echo "$output" | wc -l) -gt $(tput lines) ] # Show output in pager if it is too large for screen
416 then
417 echo "$output" | less
418 else
419 echo "$output"
420 fi
421 else # Show non-system groups
422 loginusers=$(awk -F: '($3 >= 1000 && $1 != "nobody"){printf $1" "}' /etc/passwd) # List of non-system users
423 groups=$(awk -F: '($3 >= 1000 && $1 != "nogroup"){print}' /etc/group) # List of user-created groups
424
425 # Add the groups that user accounts are members of to the groups list
426 for user in $loginusers
427 do
428 newgroups=`grep $user /etc/group`
429 groups=$groups$'\n'$newgroups
430 done
431
432 groups=$'Group name: :Gid:Users\n'$(echo "$groups" | sort | uniq) # Remove duplicate entries in the list and add titles for the columns
433 output=$(echo "$groups" | awk -F: '{print $1": "$3": "$4}' | column -s: -t)
434 if [ $(echo "$output" | wc -l) -gt $(tput lines) ] # If the output is to big for the screen, show it in a pager
435 then
436 echo "$output" | less
437 else
438 echo "$output"
439 fi
440 fi
441}
442
443function show_group_info() {
444 # Show details about a specific group
445 echo_with_color 31 "%s" "Group name:"
446 read groupname
447 groupname=$(sanetize_input $groupname)
448 groupstring=$(egrep "^$groupname:" /etc/group)
449 if [ -z "$groupstring" -o -z "$groupname" ] # Make sure the group exists
450 then
451 echo_with_color 31 "%s\n" "Group not found!"
452 else
453 egrep "^$groupname" /etc/group | awk -F: '{
454 print "Gid: "$3
455 print "Users: "$4
456 }'
457
458 fi
459}
460
461function show_users() {
462 # Show all users
463 echo_with_color 31 "%s" "Show all users or login users? (a/L):"
464 read choice
465 if [ "$choice" == A -o "$choice" == a ] # Show all users
466 then
467 output=$(echo "Username: :Uid:Gid:Description:Home:Shell" | awk -F: '{print $1": "$3": "$4": "$5": "$6": "$7}' /dev/stdin /etc/passwd | column -s: -t)
468 if [ $(echo "$output" | wc -l) -gt $(tput lines) ] # Show in pager if too large for screen
469 then
470 echo "$output" | less
471 else
472 echo "$output"
473 fi
474 else # Show only non-system users
475 output=$(echo "Username: :Uid:Gid:Description:Home:Shell" | awk -F: '($3 >= 1000 && $1 != "nobody"){print $1": "$3": "$4": "$5": "$6": "$7}' /dev/stdin /etc/passwd | column -s: -t)
476 if [ $(echo "$output" | wc -l) -gt $(tput lines) ] # Show in pager if too large for screen
477 then
478 echo "$output" | less
479 else
480 echo "$output"
481 fi
482 fi
483}
484
485function show_user_info() {
486 # Show info about a particular user
487 echo_with_color 31 "%s" "Username:"
488 read username
489 username=$(sanetize_input $username)
490 userstring=$(egrep "^$username:" /etc/passwd)
491 if [ -z "$userstring" -o -z "$username" ] # Make sure the user exists
492 then
493 echo_with_color 31 "%s\n" "User not found!"
494 else
495 egrep "^$username" /etc/passwd | awk -F: '{
496 print "Uid: "$3
497 print "Gid: "$4
498 print "Description: "$5
499 print "Home Directory: "$6
500 print "Shell: "$7
501 }'
502 echo "Groups: "$(groups $username | sed "s/$username : //")
503
504 fi
505}