/
CreateUserAccount.sh
2266 lines (2041 loc) · 62.5 KB
/
CreateUserAccount.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
##########################################
# Create User Account Script
# Josh Harvey | josh[at]macjeezy.com
# GitHub - github.com/therealmacjeezy
# JAMFnation - therealmacjeezy
# Created: July 6 2017
# Updated: August 16 2017
##########################################
############################# Licensing ###############################
# Copyright 2017, Joshua Harvey
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
############################### Usage #################################
# sudo createuser.sh [-a | -h | -c]
########################## Revision History ###########################
# 08-16-2017 Added an option to create and delete a Sharing Only
# account (Version 1.2)
# 08-11-2017 Custom pictures can now be used when the package option
# is selected. Added an option to exit the script after
# the package is created, instead of automatically
# creating the account (Version 1.1).
# 08-06-2017 Added option to export a user to a package and the
# option to password protect that package as a zip
# 08-05-2017 Added section for FileVault (Reset Password Section),
# Created user lists that appear when you need to select
# a user, fixed issues in the disable/enable user section
# 07-19-2017 Added Getopts section
# 07-06-2017 Created
#######################################################################
########################### Text Formatting ###########################
addStyle() {
esc="\033"
# Text Colors
TXTblack="${esc}[30m"; TXTred="${esc}[31m"; TXTgreen="${esc}[32m";
TXTyellow="${esc}[33m"; TXTblue="${esc}[34m"; TXTpurple="${esc}[35m";
TXTcyan="${esc}[36m"; TXTwhite="${esc}[37m"; TXTlightgreen="${esc}[92m";
TXTpink="${esc}[95m";
# Background Colors
BGblack="${esc}[40m"; BGred="${esc}[41m"; BGgreen="${esc}[42m";
BGyellow="${esc}[43m"; BGblue="${esc}[44m"; BGpurple="${esc}[45m";
BGcyan="${esc}[46m"; BGwhite="${esc}[47m"; BGgray="${esc}[100m";
BGlightgreen="${esc}[102m";
# Additional Styles
boldon="${esc}[1m"; boldoff="${esc}[22m";
italicon="${esc}[3m"; italicoff="${esc}[23m";
underlineon="${esc}[4m"; underlineoff="${esc}[24m";
inverton="${esc}[7m"; invertoff="${esc}[27m";
blinkon="${esc}[5m"; blinkoff="${esc}[25m";
hideon="${esc}[8m"; hideoff="${esc}[28m";
# Reset Style
reset="${esc}[0m"
}
addStyle
#######################################################################
#### Generate UID
# Creates a UID in the 503 to 550 range that will appear in the Account Preferences pane
makeUID500=`awk -v min=503 -v max=550 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'`
# Creates a UID in the 480 to 499 range that will NOT appear in the Account Preferences pane
makeUID400=`awk -v min=480 -v max=499 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'`
scriptTitle() {
printf "${boldon}${BGlightgreen}############################################################\n"
printf " Create User Account Script | Copyright: Josh Harvey [2017] \n"
printf "############################################################${reset}\n"
printf "\n"
}
showCopyright() {
scriptTitle
echo "
${boldon}Copyright 2017, Joshua Harvey${boldoff}
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License at <http://www.gnu.org/licenses/> for
more details.
================================================================
${boldon}Contact${boldoff}
> ${boldon}E-Mail: ${boldoff}josh[at]macjeezy.com
> ${boldon}GitHub: ${boldoff}github.com/therealmacjeezy
> ${boldon}JAMFnation: ${boldoff}therealmacjeezy
================================================================
"
}
scriptHelp() {
clear
scriptTitle
echo "${boldon}Create User Account Script${boldoff} - ${boldon} Version: ${boldoff}1.1${reset}
${boldon}Summary${reset}
This script will allow a user to create various account types along with perform select account management tasks.
${boldon}Useage${reset}
${boldon}CreateUserAccount.sh ${boldoff} [ -a | -c | -h ]
${boldon}-a${boldoff} - Administrator Management Mode
${boldon}-c${boldoff} - Show Copyright Section
${boldon}-h${boldoff} - Usage / Help (This Page)
${boldon}Main Menu Options: ${boldoff}
- Create Standard Account
- Create Admin Account
- ${italicon}${boldon}Account Options:${italicoff}${boldoff}
- Hidden Account
(${boldon}Location:${boldoff} /var/username/ | ${boldon}UID:${boldoff} below 500)
- Non Hidden Account
(${boldon}Location:${boldoff} /Users/username/ | ${boldon}UID:${boldoff} above 500)
- Create Sharing Only Account [new]
- Account Management
${boldon}Account Management Options: ${boldoff}
- Disable Standard User Account
- Enable Standard User Account
- Reset Standard User Password
- Delete Standard User Account
- Delete Sharing Only Account [new]
- Admin: Disable Account ${italicon}${TXTblue}(-a Required)${reset}
- Admin: Enable Account ${italicon}${TXTblue}(-a Required)${reset}
- Admin: Reset Password ${italicon}${TXTblue}(-a Required)${reset}
- Admin: Delete Account ${italicon}${TXTblue}(-a Required)${reset}
"
}
#### Package Creation Section ####
# Gets the current logged in user and sets it as a variable
theUser=$(who | grep "console" | cut -d" " -f1)
# Function to create the package and postscripts for the user account
pkgOptions() {
mkPkg() {
# Creates the directory for the postinstall script
mkdir -p /Users/"$theUser"/Desktop/scripts/
# Allows the directory to be read/write/executed by all users
chmod ugo+rwx /Users/"$theUser"/Desktop/scripts/
# Takes the user creation lines and exports them into a postinstall script
printf "#!/bin/bash\n" > /Users/"$theUser"/Desktop/scripts/postinstall
printf "sudo dscl . -create /Users/"$userName"\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
printf "sudo dscl . -create /Users/"$userName" UserShell "$userShell"\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
printf 'sudo dscl . -create /Users/'"$userName"' RealName '"\"$userRealName\""'\n' >> /Users/"$theUser"/Desktop/scripts/postinstall
printf "sudo dscl . -create /Users/"$userName" UniqueID "$makeUID"\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
printf "sudo dscl . -create /Users/"$userName" PrimaryGroupID "$getID"\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
printf "sudo dscl . -create /Users/"$userName" NFSHomeDirectory "$homeDir"\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
printf "sudo dscl . passwd /Users/"$userName" "$userPass"\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
printf "sudo mkdir -p "$homeDir"\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
if [[ "$customPictureOption" == "true" ]];
then
cp -R "$userIcon" /Users/"$theUser"/Desktop/scripts/img01.png
sudo mkdir /Library/User\ Pictures/Custom
printf "sudo cp -R img01.png /Library/User\ Pictures/Custom/img01.png\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
printf "sudo dscl . -create /Users/"$userName" Picture /Library/User\ Pictures/Custom/img01.png\n" >> /Users/"$theUser"/Desktop/scripts/postinstall
else
printf 'sudo dscl . -create /Users/'"$userName"' Picture '\""$userIcon"\"'\n' >> /Users/"$theUser"/Desktop/scripts/postinstall
fi
#printf 'sudo dscl . -create /Users/'"$userName"' Picture '\""$userIcon"\"'\n' >> /Users/"$theUser"/Desktop/scripts/postinstall
# Makes the postinstall script executable
chmod a+x /Users/"$theUser"/Desktop/scripts/postinstall
# Builds the package without a payload and just the postscript
sudo pkgbuild --identifier com.therealmacjeezy.createuseraccount --nopayload --scripts "/Users/"$theUser"/Desktop/scripts" "/Users/"$theUser"/Desktop/CreateUserAccount.pkg"
# Removes the directory that stored the postinstall script
sudo rm -rf /Users/"$theUser"/Desktop/scripts/
}
makeUser() {
# Prompt that will ask the user if they still want to create the user account or exit the script. This option will be provided if they choose to create a package of the user account
clear
scriptTitle
printf "${boldon}Do you still want to create the user account on this computer? (Y/N): ${boldoff}"
read continueScript
tput cuu1; tput cr; tput el;
case $continueScript in
Y|y)
echo "Beginning User Creation.."
;;
N|n)
printf "${boldon}Exiting Script.. ${boldoff}\n"
sleep 1
clear
exit 0
;;
esac
}
# Prompt that gives you the option to convert the package into a password protected zip for additonal security
printf "${boldon}Would you like to password protect the package? ${boldoff}\n"
printf "${italicon}${TXTyellow}${boldon}Note: This will convert the package to a password protected zip${reset}\n"
printf "${boldon}Add Password? (Y/N): ${boldoff}"
read addPass
tput cuu1; tput cr; tput el;
case $addPass in
Y|y)
# Until loop that will ensure the passwords match
until [[ "$passVerified" == "yes" ]]; do
printf "${boldon}Enter a password to use:${boldoff}\n"
read -s pkgPass
tput cuu1; tput cr; tput el;
printf "Please Re-Enter The Password:\n"
read -s pkgPassVerify
tput cuu1; tput cr; tput el;
if [[ "$pkgPass" == "$pkgPassVerify" ]];
then
passVerified="yes"
else
printf "${TXTred}The Passwords you entered do not match.${reset}\n"
printf "${boldon}Please Re-Enter the password..${boldoff}\n"
sleep 1
tput cuu1; tput cr; tput el;
tput cuu1; tput cr; tput el;
fi
done
# Runs the function to create the package
mkPkg
# Changes the directory to the user's desktop so the zip gets created onto their Desktop
cd /Users/"$theUser"/Desktop
# Creates the zip file using the password the user entered above
zip -e CreateUserAccount.pkg.zip CreateUserAccount.pkg --password "$pkgPass"
# Removes the package from the Desktop so only the zip file is there
rm -rf /Users/"$theUser"/Desktop/CreateUserAccount.pkg
;;
N|n)
# Runs the function to create the package
mkPkg
;;
esac
}
createPKG() {
printf "Would you like to export this user to a package for later use? (Y/N):\n"
read pkgToo
tput cuu1; tput cr; tput el;
case $pkgToo in
Y|y)
pkgOptions
makeUser
;;
N|n)
#break
;;
esac
}
## End Package Option
getSharing() {
# Get list of all user accounts with the system accounts filtered out
listUsers=$(dscl . -list /Users | grep -v "^_.*" | awk '{print $1}')
# Count the number of user accounts
countUsers=$(echo "$listUsers" | wc -l)
# Create starting count for the user array
lineNumber="1"
# Creates an item in the array for every user.
for i in $(seq 1 "$countUsers"); do
# Uses the numbering variable set above with the head command to print each user line by line
listUsers=$(dscl . -list /Users | grep -v "^_.*" | awk '{print $1}' | head -"$lineNumber" | tail -1)
# Checks to see if the account is able to login or if their shell is set to /usr/bin/false
userShell=$(dscl . -read /Users/"$listUsers" NFSHomeDirectory | grep -w "/dev/null")
# If the account has the default shell to allow logins, the user is added to the array
if [[ ! -z "$userShell" ]];
then
userList+=("$listUsers")
fi
# Increases the numbering variable
let "lineNumber++"
done
# A for loop that will check to see if the users in the array created above are part of the admin group. If they aren't an admin, they will get added to the standard users array.
for i in "${userList[@]}"; do
# Variable to check to see if the user is a member of the admin group
adminCheck=$(dsmemberutil checkmembership -U "$i" -G admin | grep "is a member")
# If the variable above is empty, that means the user is not a memeber of the admin group and gets added to the standard users array
if [[ -z "$adminCheck" ]];
then
sharingUser+=("$i")
fi
done
# Adds an option to quit to the end of the standard users array
sharingUser+=("QUIT")
# Creates a starting count for error checking
errorCount="0"
pickUser() {
# Function to allow the user to verify the user they have selected and allows them to pick another one if it's not correct
verifyUser() {
clear
scriptTitle
printf "You have selected the user ${boldon}$usr ${boldoff}..\nIs this correct (y/n)? "
read useOption
case $useOption in
Y|y)
# Sets the variable that is used to verify the user has been verifed and set to be used in the next part of the script
selectionComplete="true"
# Sets the variable that is used in the until loop to skip the user selection menu
userPicked="yes"
clear
break
;;
N|n)
clear
scriptTitle
# Sets the variable that will bring the user back to the user selection menu to pick a different user
userPicked="no"
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of attempts reached. Exiting"
sleep .5
exit 1
fi
break
;;
*)
echo "Error Found: Invalid Entry"
sleep .5
tput cuu1; tput cr; tput el;
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of errors reached. Exiting"
sleep .5
exit 1
fi
;;
esac
}
PS3="Please select a user: "
until [[ "$userPicked" == "yes" ]]; do
select usr in "${sharingUser[@]}"; do
tput cuu1; tput cr; tput el;
if [[ -z "$usr" ]];
then
echo "Error Found: Invalid Entry"
sleep .5
tput cuu1; tput cr; tput el;
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of errors reached. Exiting"
sleep .5
exit 1
fi
elif [[ "$usr" == "QUIT" ]];
then
echo "Exiting.."
sleep .5
clear
exit 0
else
# Sets the variable that is used in the until loop to skip the user selection menu
userPicked="yes"
tput cuu1; tput cr; tput el;
# Calls the function to verify the user that has been selected
verifyUser
fi
done
done
}
# A while loop that will call the pickUser function if it sees the variable for selectionComplete isn't set to true
while [[ "$selectionComplete" != "true" ]]; do
pickUser
done
# If statement that sets the setUser variable with the user that has been selected above
if [[ "$selectionComplete" == true ]];
then
setUser="$usr"
fi
}
getStandard() {
# Get list of all user accounts with the system accounts filtered out
listUsers=$(dscl . -list /Users | grep -v "^_.*" | awk '{print $1}')
# Count the number of user accounts
countUsers=$(echo "$listUsers" | wc -l)
# Create starting count for the user array
lineNumber="1"
# Creates an item in the array for every user.
for i in $(seq 1 "$countUsers"); do
# Uses the numbering variable set above with the head command to print each user line by line
listUsers=$(dscl . -list /Users | grep -v "^_.*" | awk '{print $1}' | head -"$lineNumber" | tail -1)
# Checks to see if the account is able to login or if their shell is set to /usr/bin/false
userShell=$(dscl . -read /Users/"$listUsers" UserShell | grep -w "/bin/bash")
# If the account has the default shell to allow logins, the user is added to the array
if [[ ! -z "$userShell" ]];
then
userList+=("$listUsers")
fi
# Increases the numbering variable
let "lineNumber++"
done
# A for loop that will check to see if the users in the array created above are part of the admin group. If they aren't an admin, they will get added to the standard users array.
for i in "${userList[@]}"; do
# Variable to check to see if the user is a member of the admin group
adminCheck=$(dsmemberutil checkmembership -U "$i" -G admin | grep "is a member")
# If the variable above is empty, that means the user is not a memeber of the admin group and gets added to the standard users array
if [[ -z "$adminCheck" ]];
then
standardUser+=("$i")
fi
done
# Adds an option to quit to the end of the standard users array
standardUser+=("QUIT")
# Creates a starting count for error checking
errorCount="0"
pickUser() {
# Function to allow the user to verify the user they have selected and allows them to pick another one if it's not correct
verifyUser() {
clear
scriptTitle
printf "You have selected the user ${boldon}$usr ${boldoff}..\nIs this correct (y/n)? "
read useOption
case $useOption in
Y|y)
# Sets the variable that is used to verify the user has been verifed and set to be used in the next part of the script
selectionComplete="true"
# Sets the variable that is used in the until loop to skip the user selection menu
userPicked="yes"
clear
break
;;
N|n)
clear
scriptTitle
# Sets the variable that will bring the user back to the user selection menu to pick a different user
userPicked="no"
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of attempts reached. Exiting"
sleep .5
exit 1
fi
break
;;
*)
echo "Error Found: Invalid Entry"
sleep .5
tput cuu1; tput cr; tput el;
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of errors reached. Exiting"
sleep .5
exit 1
fi
;;
esac
}
PS3="Please select a user: "
until [[ "$userPicked" == "yes" ]]; do
select usr in "${standardUser[@]}"; do
tput cuu1; tput cr; tput el;
if [[ -z "$usr" ]];
then
echo "Error Found: Invalid Entry"
sleep .5
tput cuu1; tput cr; tput el;
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of errors reached. Exiting"
sleep .5
exit 1
fi
elif [[ "$usr" == "QUIT" ]];
then
echo "Exiting.."
sleep .5
clear
exit 0
else
# Sets the variable that is used in the until loop to skip the user selection menu
userPicked="yes"
tput cuu1; tput cr; tput el;
# Calls the function to verify the user that has been selected
verifyUser
fi
done
done
}
# A while loop that will call the pickUser function if it sees the variable for selectionComplete isn't set to true
while [[ "$selectionComplete" != "true" ]]; do
pickUser
done
# If statement that sets the setUser variable with the user that has been selected above
if [[ "$selectionComplete" == true ]];
then
setUser="$usr"
fi
}
getAdmin() {
# Get list of all user accounts with the system accounts filtered out
listUsers=$(dscl . -list /Users | grep -v "^_.*" | awk '{print $1}')
# Count the number of user accounts
countUsers=$(echo "$listUsers" | wc -l)
# Create starting count for the user array
lineNumber="1"
# Creates an item in the array for every user.
for i in $(seq 1 "$countUsers"); do
# Uses the numbering variable set above with the head command to print each user line by line
listUsers=$(dscl . -list /Users | grep -v "^_.*" | awk '{print $1}' | head -"$lineNumber" | tail -1)
# Checks to see if the account is able to login or if their shell is set to /usr/bin/false
userShell=$(dscl . -read /Users/"$listUsers" UserShell | grep -w "/bin/bash")
# If the account has the default shell to allow logins, the user is added to the array
if [[ ! -z "$userShell" ]];
then
userList+=("$listUsers")
fi
# Increases the numbering variable
let "lineNumber++"
done
# A for loop that will check to see if the users in the array created above are part of the admin group. If they are an admin, they will get added to the admin users array.
for i in "${userList[@]}"; do
# Variable to check to see if the user is a member of the admin group
adminCheck=$(dsmemberutil checkmembership -U "$i" -G admin | grep "is a member")
# If the variable above is not empty, that means the user is a memeber of the admin group and gets added to the admin users array
if [[ ! -z "$adminCheck" ]];
then
adminUser+=("$i")
fi
done
# Adds an option to quit to the end of the admin users array
adminUser+=("QUIT")
# Creates a starting count for error checking
errorCount="0"
pickUser() {
# Function to allow the user to verify the user they have selected and allows them to pick another one if it's not correct
verifyUser() {
clear
scriptTitle
printf "You have selected the user ${boldon}$usr ${boldoff}..\nIs this correct (y/n)?: "
read useOption
case $useOption in
Y|y)
# Sets the variable that is used to verify the user has been verifed and set to be used in the next part of the script
selectionComplete="true"
# Sets the variable that is used in the until loop to skip the user selection menu
userPicked="yes"
clear
break
;;
N|n)
clear
scriptTitle
# Sets the variable that will bring the user back to the user selection menu to pick a different user
userPicked="no"
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of attempts reached. Exiting"
sleep .5
exit 1
fi
break
;;
*)
echo "Error Found: Invalid Entry"
sleep .5
tput cuu1; tput cr; tput el;
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of errors reached. Exiting"
sleep .5
exit 1
fi
;;
esac
}
PS3="Please select a user: "
until [[ "$userPicked" == "yes" ]]; do
#printf "User List\n"
select usr in ${adminUser[@]}; do
tput cuu1; tput cr; tput el;
if [[ -z "$usr" ]];
then
echo "Error Found: Invalid Entry"
sleep .5
tput cuu1; tput cr; tput el;
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of errors reached. Exiting"
sleep .5
exit 1
fi
elif [[ "$usr" == "QUIT" ]];
then
echo "Exiting.."
sleep .5
clear
exit 0
else
# Sets the variable that is used in the until loop to skip the user selection menu
userPicked="yes"
tput cuu1; tput cr; tput el;
# Calls the function to verify the user that has been selected
verifyUser
fi
done
done
}
# A while loop that will call the pickUser function if it sees the variable for selectionComplete isn't set to true
while [[ "$selectionComplete" != "true" ]]; do
pickUser
done
# If statement that sets the setUser variable with the user that has been selected above
if [[ "$selectionComplete" == true ]];
then
setUser="$usr"
fi
}
# Gets the list of FileVault users
getFVUserList() {
# Get list of all user accounts with the system accounts filtered out
listFVUsers=$(sudo fdesetup list | sed 's/[,].*//')
# Count the number of user accounts
countFVUsers=$(echo "$listFVUsers" | wc -l)
# Create starting count for the user array
lineNumberFV="1"
# Creates an item in the array for every user.
for i in $(seq 1 "$countFVUsers"); do
# Uses the numbering variable set above with the head command to print each user line by line
listFVUsers=$(sudo fdesetup list | sed 's/[,].*//' | head -"$lineNumberFV" | tail -1)
# Adds the FileVault user to the array
FVUserList+=("$listFVUsers")
# Increases the numbering variable
let "lineNumberFV++"
done
# Adds an option to quit to the end of the admin users array
FVUserList+=("QUIT")
# Creates a starting count for error checking
errorCount="0"
pickFVUser() {
# Function to allow the user to verify the user they have selected and allows them to pick another one if it's not correct
verifyFVUser() {
clear
scriptTitle
printf "You have selected the user ${boldon}$FVusr ${boldoff}..\nIs this correct (y/n)?: "
read useOption
case $useOption in
Y|y)
# Sets the variable that is used to verify the user has been verifed and set to be used in the next part of the script
FVselectionComplete="true"
# Sets the variable that is used in the until loop to skip the user selection menu
FVuserPicked="yes"
clear
break
;;
N|n)
clear
scriptTitle
# Sets the variable that will bring the user back to the user selection menu to pick a different user
FVuserPicked="no"
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of attempts reached. Exiting"
sleep .5
exit 1
fi
break
;;
*)
echo "Error Found: Invalid Entry"
sleep .5
tput cuu1; tput cr; tput el;
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of errors reached. Exiting"
sleep .5
exit 1
fi
;;
esac
}
printf "FileVault Users\n"
PS3="Please select a user: "
until [[ "$FVuserPicked" == "yes" ]]; do
#printf "User List\n"
select FVusr in ${FVUserList[@]}; do
tput cuu1; tput cr; tput el;
if [[ -z "$FVusr" ]];
then
echo "Error Found: Invalid Entry"
sleep .5
tput cuu1; tput cr; tput el;
# Increases the variable for error checking
let "errorCount++"
# Exits the script if the max number of errors has been reached
if [[ "$errorCount" == "3" ]];
then
echo "Max number of errors reached. Exiting"
sleep .5
exit 1
fi
elif [[ "$FVusr" == "QUIT" ]];
then
echo "Exiting.."
sleep .5
clear
exit 0
else
# Sets the variable that is used in the until loop to skip the user selection menu
FVuserPicked="yes"
tput cuu1; tput cr; tput el;
# Calls the function to verify the user that has been selected
verifyFVUser
fi
done
done
}
# A while loop that will call the pickUser function if it sees the variable for selectionComplete isn't set to true
while [[ "$FVselectionComplete" != "true" ]]; do
pickFVUser
done
# If statement that sets the setUser variable with the user that has been selected above
if [[ "$FVselectionComplete" == true ]];
then
fvUserOld="$FVusr"
fi
}
adminCheck() {
if groups $setUser | grep -q -w admin;
then
echo "Error Found: Access Denied"
exit 1
adminUser=true
else
adminUser=false
fi
}
# Function to disable a user account
disableUser() {
# Verify the username variable isn't empty before continuing
if [[ -z "$setUser" ]];
then
echo "Required Field Missing: Username not chosen."
echo "Exiting.."
sleep 1
clear
exit
fi
# Check to see if the user account is already disabled
disableCheck=$(dscl . -read /Users/$setUser AuthenticationAuthority | grep -w ';DisabledUser;')
if [[ -z "$disableCheck" ]];
then
echo "$setUser is not already disabled. Continuing.."
sudo dscl . -append /Users/$setUser AuthenticationAuthority \;DisabledUser\;
else
echo "$setUser is already disabled. Exiting.."
sleep .5
exit 1
fi
verifyOption=$(dscl . -read /Users/$setUser AuthenticationAuthority | grep -w ';DisabledUser;')
if [[ ! -z "$verifyOption" ]];
then
echo "${boldon}${blinkon}$setUser has been disabled.${reset}"
sleep 1
clear
exit 0
else
echo "Unable to disable the account for $setUser."
sleep 1
exit 1
fi
}
# Function to delete a user account
deleteUser() {
# Verify the username variable isn't empty before continuing
if [[ -z "$setUser" ]];
then
echo "Required Field Missing: Username not chosen."
echo "Exiting.."
sleep 1
clear
exit
fi
# Creates the variable to set the error number count
errorsFound="0"
# Verifies the user exists once more before deleting the account to add another layer of validation
userCheck=$(dscl . -list /Users | grep -w "$setUser")
# If statement to verify and validate the user account does exist and will confirm with the user once more if they want to delete the account
if [[ ! -z "$userCheck" ]];
then
scriptTitle
echo "Found account for $setUser .."
# Asks the user once more if they want to delete the account for the selected user
printf "${boldon}Are you sure you want to delete the account for $setUser?\n${TXTred}${blinkon}This action cannot be undone.${reset}\n"
sleep 1
printf "${boldon}Confirm Deletion?${boldoff} (y/n): "
read verifyDelete
tput cuu1; tput cr; tput el;
case $verifyDelete in
Y|y)
echo "Deleting account for $setUser .."
# Deletes the user from dscl
sudo dscl . -delete /Users/"$setUser"
# Deletes the user's home directory
if [[ -z "$sharingAccount" ]];
then
sudo rm -rf /Users/"$setUser"/
fi
# Sets the variable to verify the actions performed above to true
verifyAction=true
;;
N|n)
echo "Deletion for ${setUser}'s account halted.."
echo "Exiting.."
sleep .5
exit 0
;;
*)
echo "Error Found: Invalid Entry"
# Increases the number of errors found
let "errorsFound++"
# If statement to exit the script if the max number of errors has been reached (2)
if [[ "$errorsFound" == "2" ]];
then
echo "Max Number of Errors Reached. Exiting.."
sleep .5
clear
exit 1
fi
;;
esac
fi
# If statement to verify the account was able to be deleted
if [[ "$verifyAction" == "true" ]];
then
# Searches for the user's account
userSearch=`dscl . -list /Users | grep -w "$setUser"`
if [[ -z "$userSearch" ]];
then
echo "The account for $setUser has been successfully deleted."
sleep 1
clear
exit 0
else
echo "The account for $setUser was unable to be deleted."
sleep 1
clear
exit 1
fi
fi
}
# Function to enable a user account
enableUser() {
# Verify the username variable isn't empty before continuing
if [[ -z "$setUser" ]];
then
echo "Required Field Missing: Username not chosen."
echo "Exiting.."
sleep 1
clear
exit
fi
# Check to see if the user account is already disabled
disableCheck=$(dscl . -read /Users/$setUser AuthenticationAuthority | grep -w ';DisabledUser;')
if [[ -z "$disableCheck" ]];
then
echo "$setUser is not disabled. Exiting.."
sleep 1
exit 0
else
echo "$setUser is currently disabled. Continuing.."
sudo dscl . -delete /Users/$setUser AuthenticationAuthority \;DisabledUser\;
fi
verifyOption=$(dscl . -read /Users/$setUser AuthenticationAuthority | grep -w ';DisabledUser;')
if [[ -z "$verifyOption" ]];
then
echo "${boldon}${blinkon}$setUser has been enabled.${reset}"
sleep 1
clear
exit 0
else
echo "Unable to enable the account for $setUser."
sleep 1
exit 1
fi
}
# Function to reset a user's password
resetPassword() {
# Verify the username variable isn't empty before continuing
if [[ -z "$setUser" ]];
then
echo "Required Field Missing: Username not chosen."
echo "Exiting.."
sleep 1
clear
exit
fi
# Function to change the user's password
getPW() {
scriptTitle
# Creates variable based on if the user wants the password to be visible while they enter it
printf "Do you want the password to be visible while being entered? (Y/N): "