Repository navigation
Expand file tree
/
Copy pathMainLibrary.sh
More file actions
3033 lines (2494 loc) · 94.8 KB
/
Copy pathMainLibrary.sh
File metadata and controls
3033 lines (2494 loc) · 94.8 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
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/zsh
#
# Main Library
#
# by: Scott Kendall
#
# Written: 01/03/2023
# Last updated: 09/16/2026
#
# Script Purpose: Main Library containing all of my commonly used functions.
#
# 1.0 - Initial
# 1.1 - Code optimization
# 1.1 - Changed the get_nic_info logic
# 1.2 - renamed all JAMF functions to start with JAMF_.....
# 1.3 - Changed JAMF function names to be more descriptive
# 1.4 - Added listitem, textbox, checkbox and dropdown functions
# 1.5 - Reworked top section for better idea of what can be modified
# New create_log_directory check routine that parses the path and checks the directory structure
# 1.6 - Add several new MS Graph API routines
# 1.7 - Add more JAMF API Libraries for Static Group modifications & Checking to see which version of SS/SS+ is being used
# 1.8 - Added option to move some of the "defaults" to a plist file / Also used the variable SCRIPT_for temp files creation & log file cname
# 1.9 - Add more JAMF functions
# Add Check for focus mode
# 1.10 -Added static group functions
# 1.11 -Removed dependencies of using systemprofiler command and use sysctl instead
# Add RunAsUser & check_logged_in_user command
# Changed create_infobox_message to use new OS & version variables
# 1.12 -Added function "admin_user" to detect if user is admin or not.
# Changed logging functions to only record if a user is admin
# 1.13 -Added more JAMF API calls
# 1.14 -Optimized Common section
# Added functions for check_display_sleep and logged_in_user
# Added JAMF DDM libraries
# 1.15 - Add functions to detect touchID, enable TouchID & enable pluginkit extensions
# 2.0 - Updated SD Version requirements to 3.1.0
# Added ability to set subtitle, color, and padding from defaults file
# 2.1 - Updates based on MS Copilot review
# 2.2 - Add JAMF functions for macOS Branding
######################################################################################################
#
# Global "Common" variables
#
######################################################################################################
#set -x
SCRIPT_NAME=""
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
MAIN_PID=$$
FREE_DISK_SPACE=$(($( /usr/sbin/diskutil info / | /usr/bin/grep "Free Space" | /usr/bin/awk '{print $6}' | /usr/bin/cut -c 2- ) / 1024 / 1024 / 1024 ))
MACOS_NAME=$(sw_vers -productName)
MACOS_VERSION=$(sw_vers -productVersion)
MAC_RAM=$(($(sysctl -n hw.memsize) / 1024**3))" GB"
MAC_CPU=$(sysctl -n machdep.cpu.brand_string)
ICON_FILES="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/"
# Swift Dialog version requirements
SW_DIALOG="/usr/local/bin/dialog"
MIN_SD_REQUIRED_VERSION="3.1.0"
[[ -e "${SW_DIALOG}" ]] && SD_VERSION=$( ${SW_DIALOG} --version) || SD_VERSION="0.0.0"
HOUR=$(date +%H)
case $HOUR in
0[0-9]|1[0-1]) GREET="morning" ;;
1[2-7]) GREET="afternoon" ;;
*) GREET="evening" ;;
esac
SD_DIALOG_GREETING="Good $GREET"
###################################################
#
# App Specific variables (Feel free to change these)
#
###################################################
# See if there is a "defaults" file...if so, read in the contents
DEFAULTS_DIR="/Library/Managed Preferences/com.gianteaglescript.defaults.plist"
echo "Setting Default values"
SUPPORT_DIR=$(defaults read "$DEFAULTS_DIR" SupportFiles 2>/dev/null) || SUPPORT_DIR="/Library/Application Support/GiantEagle"
SD_BANNER_IMAGE=$(defaults read "$DEFAULTS_DIR" BannerImage 2>/dev/null) || SD_BANNER_IMAGE="GE_SD_BannerImage.png"
BANNER_TEXT_PADDING=$(defaults read "$DEFAULTS_DIR" BannerPadding 2>/dev/null) || BANNER_TEXT_PADDING=10
BANNER_SUBTITLE=$(defaults read "$DEFAULTS_DIR" BannerSubtitle 2>/dev/null) || BANNER_SUBTITLE=""
BANNER_TEXT_COLOR=$(defaults read "$DEFAULTS_DIR" TitleFontColor 2>/dev/null) || BANNER_TEXT_COLOR="white"
[[ -e $SUPPORT_DIR/$SD_BANNER_IMAGE ]] && SD_BANNER_IMAGE="$SUPPORT_DIR/$SD_BANNER_IMAGE"
# Log files location
LOG_FILE="${SUPPORT_DIR}/logs/${SCRIPT_NAME}.log"
# Display items (banner / icon)
SD_WINDOW_TITLE=""
SD_ICON_FILE="https://images.crunchbase.com/image/upload/c_pad,h_170,w_170,f_auto,b_white,q_auto:eco,dpr_1/vhthjpy7kqryjxorozdk"
OVERLAY_ICON="/System/Applications/App Store.app"
SUPPORT_FILE_INSTALL_POLICY="install_SymFiles"
DIALOG_INSTALL_POLICY="install_SwiftDialog"
JQ_INSTALL_POLICY="install_jq"
APP_EXTENSIONS=("com.microsoft.CompanyPortalMac.ssoextension"
"com.microsoft.CompanyPortalMac.Mac-Autofill-Extension")
##################################################
#
# Passed in variables
#
#################################################
JAMF_LOGGED_IN_USER=${3:-"$LOGGED_IN_USER"} # Passed in by JAMF automatically
SD_FIRST_NAME="${(C)${JAMF_LOGGED_IN_USER%%.*}}"
CLIENT_ID=${4} # user name for JAMF Pro
CLIENT_SECRET=${5}
[[ ${#CLIENT_ID} -gt 30 ]] && JAMF_TOKEN="new" || JAMF_TOKEN="classic" #Determine with JAMF credentials we are using
####################################################################################################
#
# Functions
#
####################################################################################################
function admin_user ()
{
[[ $UID -eq 0 ]] && return 0 || return 1
}
function create_log_directory ()
{
# Ensure that the log directory and the log files exist. If they
# do not then create them and set the permissions.
#
# RETURN: None
# If the log directory doesn't exist - create it and set the permissions (using zsh parameter expansion to get directory)
local LOG_DIR="${LOG_FILE%/*}"
admin_user || return 0
if [[ ! -d "$LOG_DIR" ]]; then
mkdir -p "$LOG_DIR" || {print -u2 "ERROR: Unable to create log directory: ${LOG_DIR}"; return 1; }
fi
chmod 755 "$LOG_DIR" || {print -u2 "ERROR: Unable to set permissions on: ${LOG_DIR}"; return 1; }
# If the log file does not exist - create it and set the permissions
if [[ ! -f "$LOG_FILE" ]]; then
touch "$LOG_FILE" || {print -u2 "ERROR: Unable to create log file: ${LOG_FILE}"; return 1;}
fi
chmod 640 "$LOG_FILE" || {print -u2 "ERROR: Unable to secure log file: ${LOG_FILE}"; return 1;}
return 0
}
function logMe ()
{
# Basic two pronged logging function that will log like this:
#
# 20231204 12:00:00: Some message here
#
# This function logs both to STDOUT/STDERR and a file
# The log file is set by the $LOG_FILE variable.
# if the user is an admin, it will write to the logfile, otherwise it will just echo to the screen
#
# RETURN: None
if admin_user; then
echo "$(/bin/date '+%Y-%m-%d %H:%M:%S'): ${1}" | /usr/bin/tee -a "${LOG_FILE}"
else
echo "$(/bin/date '+%Y-%m-%d %H:%M:%S'): ${1}"
fi
}
function check_swift_dialog_install ()
{
local SD_VERSION
logMe "Ensuring that SwiftDialog is installed..."
if [[ ! -x "$SW_DIALOG" ]]; then
logMe "SwiftDialog is missing. Attempting installation."
if ! install_swift_dialog || [[ ! -x "$SW_DIALOG" ]]; then
logMe "ERROR: SwiftDialog installation failed." >&2
return 1
fi
fi
if ! SD_VERSION=$("$SW_DIALOG" --version ); then #2>/dev/null); then
logMe "ERROR: Unable to determine SwiftDialog version." >&2
return 1
fi
if [[ -z "$SD_VERSION" ]]; then
logMe "ERROR: SwiftDialog returned an empty version." >&2
return 1
fi
if ! is-at-least "$MIN_SD_REQUIRED_VERSION" "$SD_VERSION"; then
logMe "SwiftDialog ${SD_VERSION} is outdated. Attempting update."
if ! install_swift_dialog; then
logMe "ERROR: SwiftDialog update failed." >&2
return 1
fi
if ! SD_VERSION=$("$SW_DIALOG" --version 2>/dev/null); then
logMe "ERROR: Unable to read SwiftDialog version after update." >&2
return 1
fi
if ! is-at-least "$MIN_SD_REQUIRED_VERSION" "$SD_VERSION"; then
logMe "ERROR: SwiftDialog remains below the required version." >&2
return 1
fi
fi
logMe "SwiftDialog version ${SD_VERSION} is available."
return 0
}
function install_swift_dialog ()
{
if [[ ! -x /usr/local/bin/jamf ]]; then
logMe "ERROR: Jamf binary not found. Cannot install Swift Dialog."
cleanup_and_exit 1
fi
/usr/local/bin/jamf policy -event "${DIALOG_INSTALL_POLICY}"
local jamf_exit="$?"
if [[ "$jamf_exit" -ne 0 ]]; then
logMe "ERROR: Jamf policy failed while installing Swift Dialog. Exit code: $jamf_exit"
cleanup_and_exit 1
fi
if [[ ! -x "${SW_DIALOG}" ]]; then
logMe "ERROR: Swift Dialog still missing after install attempt."
cleanup_and_exit 1
fi
}
function check_support_files ()
{
if [[ ! -e "$SD_BANNER_IMAGE" ]] && [[ "$SD_BANNER_IMAGE" =~ \.(jpg|png|heic)$ ]]; then
if ! /usr/local/bin/jamf policy -event "$SUPPORT_FILE_INSTALL_POLICY"
then
logMe "WARNING: Support-file installation failed." >&2
fi
fi
if ! command -v jq >/dev/null 2>&1; then
if ! /usr/local/bin/jamf policy -event "$JQ_INSTALL_POLICY"; then
logMe "ERROR: jq installation policy failed." >&2
return 1
fi
fi
if ! command -v jq >/dev/null 2>&1; then
logMe "ERROR: jq remains unavailable after installation." >&2
return 1
fi
return 0
}
function initialize_user_context ()
{
LOGGED_IN_USER=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')
if [[ -z "$LOGGED_IN_USER" || "$LOGGED_IN_USER" == "loginwindow" ]]; then
printf '%s\n' "INFO: No interactive user is logged in."
return 1
fi
if ! USER_UID=$(id -u "$LOGGED_IN_USER"); then
printf '%s\n' "ERROR: Unable to resolve UID for ${LOGGED_IN_USER}." >&2
return 1
fi
if ! USER_DIR=$(dscl . -read "/Users/${LOGGED_IN_USER}" NFSHomeDirectory | awk '{ print $2 }'); then
printf '%s\n' "ERROR: Unable to resolve home directory for ${LOGGED_IN_USER}." >&2
return 1
fi
if [[ -z "$USER_DIR" || ! -d "$USER_DIR" ]]; then
printf '%s\n' "ERROR: Invalid home directory for ${LOGGED_IN_USER}: ${USER_DIR}" >&2
return 1
fi
Jamf_LOGGED_IN_USER="${Jamf_PARAMETER_USER:-$LOGGED_IN_USER}"
SD_FIRST_NAME="${(C)${Jamf_LOGGED_IN_USER%%.*}}"
CSV_PATH="${USER_DIR}/Desktop/DDM Data Dump for "
DDM_CROSS_REF_FILE="${USER_DIR}/Documents/DDMCrossRef.csv"
return 0
}
function create_infobox_message ()
{
################################
#
# Swift Dialog InfoBox message construct
#
################################
SD_INFO_BOX_MSG="## System Info ##<br>"
SD_INFO_BOX_MSG+="${MAC_CPU}<br>"
SD_INFO_BOX_MSG+="{serialnumber}<br>"
SD_INFO_BOX_MSG+="${MAC_RAM} RAM<br>"
SD_INFO_BOX_MSG+="${FREE_DISK_SPACE}GB Available<br>"
SD_INFO_BOX_MSG+="${MACOS_NAME} ${MACOS_VERSION}<br>"
}
function check_logged_in_user ()
{
# PURPOSE: Make sure there is a logged in user
# RETURN: None
# EXPECTED: $LOGGED_IN_USER
if [[ -z "$LOGGED_IN_USER" ]] || [[ "$LOGGED_IN_USER" == "loginwindow" ]]; then
logMe "INFO: No user logged in, exiting"
cleanup_and_exit 0
else
logMe "INFO: User $LOGGED_IN_USER is logged in"
fi
}
function check_display_sleep ()
{
# PURPOSE: Determine if the mac is asleep or awake.
# RETURN: will return 0 if awake, otherwise will return 1
# EXPECTED: None
local sleepval=$(pmset -g systemstate | tail -1 | awk '{print $4}')
local retval=0
logMe "INFO: Checking sleep status"
[[ "$sleepval" == "4" ]] && logMe "INFO: System appears to be awake" || { logMe "INFO: System appears to be asleep, will pause notifications"; retval=1; }
return $retval
}
function dialog_cmd ()
{
# Serialize SwiftDialog command-file writes from background workers.
local command="$1"
local attempts=0
while ! mkdir "$DIALOG_LOCK_DIR" 2>/dev/null; do
sleep 0.02
(( attempts++ ))
if (( attempts >= 500 )); then
logMe "ERROR: Timed out waiting for dialog command lock" >&2
return 1
fi
done
{
if ! printf '%s\n' "$command" >> "$DIALOG_COMMAND_FILE"; then
logMe "ERROR: Unable to write SwiftDialog command: $command" >&2
return 1
fi
} always {
rmdir "$DIALOG_LOCK_DIR" 2>/dev/null
}
return 0
}
function cleanup_files ()
{
# Perform a clean-up on all of the temp files that were created at run-time
(( ZSH_SUBSHELL == 0 )) || return 0
[[ "$$" == "$MAIN_PID" ]] || return 0
local file
for file in \
"$JSON_DIALOG_BLOB" \
"$DIALOG_COMMAND_FILE" \
"$TMP_FILE_STORAGE"
do
[[ -n "$file" && -e "$file" ]] && rm -f -- "$file"
done
[[ -n "$RESULTS_DIR" && -d "$RESULTS_DIR" ]] && rm -rf -- "$RESULTS_DIR"
[[ -n "$CSV_LOCK_DIR" && -d "$CSV_LOCK_DIR" ]] && rmdir "$CSV_LOCK_DIR" 2>/dev/null
[[ -n "$DIALOG_LOCK_DIR" && -d "$DIALOG_LOCK_DIR" ]] && rmdir "$DIALOG_LOCK_DIR" 2>/dev/null
}
function cleanup_and_exit ()
{
local exit_code="${1:-0}"
trap - EXIT
cleanup_files
exit "$exit_code"
}
trap 'cleanup_and_exit 130' INT
trap 'cleanup_and_exit 143' TERM
trap 'cleanup_files' EXIT
function check_for_sudo ()
{
# Ensures that script is run as ROOT
if ! admin_user; then
MainDialogBody=(
--message "In order for this script to function properly, it must be run as an admin user!"
--ontop
--icon computer
--overlayicon "$STOP_ICON"
--bannerimage "${SD_BANNER_IMAGE}"
--bannertitle "${SD_WINDOW_TITLE}"
--titlefont shadow=1
--button1text "OK"
)
"${SW_DIALOG}" "${MainDialogBody[@]}" 2>/dev/null
cleanup_and_exit 1
fi
}
function make_temp_files ()
{
JSON_DIALOG_BLOB=$(mktemp "/var/tmp/${SCRIPT_NAME}_json.XXXXX") || {
logMe "ERROR: Unable to create SwiftDialog JSON file" >&2
return 1
}
DIALOG_COMMAND_FILE=$(mktemp "/var/tmp/${SCRIPT_NAME}_cmd.XXXXX") || {
logMe "ERROR: Unable to create SwiftDialog command file" >&2
return 1
}
TMP_FILE_STORAGE=$(mktemp "/var/tmp/${SCRIPT_NAME}_storage.XXXXX") || {
logMe "ERROR: Unable to create temporary storage file" >&2
return 1
}
RESULTS_DIR=$(mktemp -d "/var/tmp/${SCRIPT_NAME}_results.XXXXX") || {
logMe "ERROR: Unable to create results counter directory" >&2
return 1
}
chmod 700 "$RESULTS_DIR" || {
logMe "ERROR: Unable to secure results counter directory" >&2
return 1
}
CSV_LOCK_DIR="/var/tmp/${SCRIPT_NAME}.${MAIN_PID}.csv.lock"
DIALOG_LOCK_DIR="/var/tmp/${SCRIPT_NAME}.${MAIN_PID}.dialog.lock"
if ! /usr/sbin/chown "$USER_UID" \
"$JSON_DIALOG_BLOB" \
"$DIALOG_COMMAND_FILE"
then
logMe "ERROR: Unable to set temporary dialog-file ownership" >&2
return 1
fi
if ! chmod 600 \
"$JSON_DIALOG_BLOB" \
"$DIALOG_COMMAND_FILE"
then
logMe "ERROR: Unable to secure temporary dialog files" >&2
return 1
fi
if ! /usr/sbin/chown root:wheel "$TMP_FILE_STORAGE"; then
logMe "ERROR: Unable to set temporary storage ownership" >&2
return 1
fi
if ! chmod 600 "$TMP_FILE_STORAGE"; then
logMe "ERROR: Unable to secure temporary storage file" >&2
return 1
fi
return 0
}
function welcomemsg ()
{
message=""
MainDialogBody=(
--message "$SD_DIALOG_GREETING $SD_FIRST_NAME. $message"
--titlefont shadow=1
--ontop
--icon "${SD_ICON_FILE}"
--overlayicon "${OVERLAY_ICON}"
--bannerimage "${SD_BANNER_IMAGE}"
--bannertitle "${SD_WINDOW_TITLE}"
--infobox "${SD_INFO_BOX_MSG}"
--helpmessage ""
--width 920
--ignorednd
--quitkey 0
--button1text "OK"
--button2text "Create Ticket"
)
# Example of appending items to the display array
# [[ ! -z "${SD_IMAGE_TO_DISPLAY}" ]] && MainDialogBody+=(--height 520 --image "${SD_IMAGE_TO_DISPLAY}")
temp=$("${SW_DIALOG}" "${MainDialogBody[@]}" 2>/dev/null)
returnCode=$?
[[ "$returnCode" == "2" ]] && {logMe "Cancel..."; break; }
# Examples of how to extra data from returned string
search_type=$(echo $temp | plutil -extract "SelectedOption" 'raw' -)
computer_id=$(echo $temp | plutil -extract "Device" 'raw' -)
reason=$(echo $temp | plutil -extract "Reason" 'raw' -)
}
function check_focus_status ()
{
# PURPOSE: Check to see if the user is in focus mode
# RETURN: in focus mode (Off/On)
# EXPECTED: FOCUS_FILE is the location of FocusMode settings
# PARAMETERS: None
local results="off"
if [[ -f "$FOCUS_FILE" ]] && grep -q '"storeAssertionRecords"' "$FOCUS_FILE" 2>/dev/null; then
results="on"
fi
echo $results
}
function touch_id_status ()
{
local hw="Absent"
retval="$hw"
local enrolled="false"
local bioCount="0"
# --- Detect Touch ID–capable hardware (internal or external) ---
bioOutput=$(ioreg -l 2>/dev/null)
# Check for the device entry indicating hardware presence
if [[ $bioOutput == *"+-o AppleBiometricSensor"* ]]; then
hw="Present"
else
# Fallback: Parse IOKitDiagnostics for class instance count
if [[ $bioOutput =~ '"AppleBiometricSensor"=([0-9]+)' && ${match[1]} -gt 0 ]]; then
hw="Present"
# Fallback: Magic Keyboard with Touch ID
elif system_profiler SPUSBDataType 2>/dev/null | grep -q "Magic Keyboard.*Touch ID"; then
hw="Present"
fi
fi
if [[ "${hw}" == "Present" ]]; then
# Enrollment check
bioCount=$(runAsUser bioutil -c 2>/dev/null | awk '/biometric template/{print $3}' | grep -Eo '^[0-9]+$' || echo "0")
[[ "${bioCount}" -gt 0 ]] && enrolled="true"
[[ "${enrolled}" == "true" ]] && retval="Enabled" || retval="Not enabled"
fi
echo "$retval"
}
function force_touch_id ()
{
# PURPOSE: Forces touchID registration
# RETURN: 0 if successful, 1 if aborted
# EXPECTED: TOUCH_ID_STATUS = Status of TouchID sensor
# PARAMETERS: None
while true; do
open "x-apple.systempreferences:com.apple.Touch-ID-Settings.extension"
"${SW_DIALOG}" \
--title "Touch ID Required" \
--message "Touch ID needs to be enabled on your system. Please add at least one fingerprint. Close this window when you are done adding your fingerprint." \
--icon "SF=touchid,colour=auto" \
--style mini \
--position "topright" \
--button1text "Close" \
--button2text "Abort" \
--quitkey 0 \
--ontop \
buttonpress=$?
TOUCH_ID_STATUS=$(touch_id_status)
[[ $TOUCH_ID_STATUS == "Enabled" || $buttonpress == 2 ]] && break
done
killall "System Settings" >/dev/null 2>&1
# Set the status code
[[ $buttonpress == 2 ]] && return 1 || return 0
}
function enable_app_extension ()
{
# PURPOSE: Enable the auto fill extension for TouchID
# check each extension listed in the array to see if it is enabled in PlugKit
# RETURN: None
# EXPECTED: APP_EXTENSIONS array of extensions to check / enable
# PARAMETERS: None
#
for extension in "${APP_EXTENSIONS[@]}"; do
logMe "Checking for extension: $extension"
results=$(runAsUser pluginkit -m | grep "${extension}")
# Check if extension exists
if [[ -z $results ]]; then
logMe "Error: Extension not found: ${extension}"
logMe "Skipping..."
continue
fi
logMe "Extension found: $extension"
# Check if the extension is enabled
if [[ $(echo $results | awk '{print $1}') == "+" ]]; then
logMe "INFO: $extension is already enabled"
else
logMe "WARNING: $extension is not enabled. Enabling now..."
runAsUser pluginkit -e use -i "${extension}"
logMe "INFO: $extension has been enabled"
fi
done
}
function display_failure_message ()
{
MainDialogBody=(
--bannerimage "${SD_BANNER_IMAGE}"
--bannertitle "${SD_WINDOW_TITLE}"
--subtitle "${BANNER_SUBTITLE}"
--titlefont "shadow=1,color=${BANNER_TEXT_COLOR},offset=${BANNER_TEXT_PADDING}"
--message "**Problems retrieving Jamf Info**<br><br>Error Message: $1"
--icon "${SD_ICON_FILE}"
--overlayicon warning
--iconsize 128
--messagefont name=Arial,size=17
--button1text "OK"
--ontop
--moveable
)
"$SW_DIALOG" "${MainDialogBody[@]}" 2>/dev/null
buttonpress=$?
}
####################################################################################################
#
# Main Script
#
####################################################################################################
autoload 'is-at-least'
check_for_sudo
if ! initialize_user_context; then
cleanup_and_exit 0
fi
if ! create_log_directory; then
cleanup_and_exit 1
fi
if ! check_swift_dialog_install; then
cleanup_and_exit 1
fi
if ! check_support_files; then
cleanup_and_exit 1
fi
if ! make_temp_files; then
cleanup_and_exit 1
fi
[[ ${#CLIENT_ID} -gt 30 ]] && JAMF_TOKEN="new" || JAMF_TOKEN="classic" #Determine with Jamf credentials we are using
create_infobox_message
if ! Jamf_check_connection; then
cleanup_and_exit 1
fi
if ! Jamf_check_credentials; then
cleanup_and_exit 1
fi
if ! Jamf_get_server; then
cleanup_and_exit 1
fi
OVERLAY_ICON=$(Jamf_which_self_service)
welcomemsg
exit 0
function update_display_list ()
{
# setopt -s nocasematch
# This function updates the Swift Dialog list display with easy to implement parameter passing...
# The Swift Dialog native structure is very strict with the command structure...this routine makes
# it easier to implement
#
# Param list
#
# $1 - Action to be done ("Create", "Add", "Change", "Clear", "Info", "Show", "Done", "Update")
# ${2} - Affected item (2nd field in JSON Blob listitem entry)
# ${3} - Icon status "wait, success, fail, error, pending or progress"
# ${4} - Status Text
# $5 - Progress Text (shown below progress bar)
# $6 - Progress amount
# increment - increments the progress by one
# reset - resets the progress bar to 0
# complete - maxes out the progress bar
# If an integer value is sent, this will move the progress bar to that value of steps
# the GLOB :l converts any incoming parameter into lowercase
case "${1:l}" in
"create" )
# Remove commands from any previous progress dialog.
if ! : > "$DIALOG_COMMAND_FILE"; then
logMe "ERROR: Unable to reset SwiftDialog command file" >&2
return 1
fi
DIALOG_PROCESS=""
if ! jq -e . "$JSON_DIALOG_BLOB" >/dev/null 2>&1; then
logMe "ERROR: Constructed SwiftDialog JSON is invalid" >&2
return 1
fi
"$SW_DIALOG" --progress --jsonfile "$JSON_DIALOG_BLOB" --commandfile "$DIALOG_COMMAND_FILE" &
DIALOG_PROCESS=$!
if [[ -z "$DIALOG_PROCESS" ]]; then
logMe "ERROR: Unable to capture the SwiftDialog process ID" >&2
return 1
fi
return 0
;;
"add" )
# Add an item to the list
#
# $2 name of item
# $3 Icon status "wait, success, fail, error, pending or progress"
# $4 Optional status text
dialog_cmd "listitem: add, title: ${2}, status: ${3}, statustext: ${4}"
;;
"buttonaction" )
# Change button 1 action
dialog_cmd 'button1action: "'${2}'"'
;;
"buttonchange" )
# change text of button 1
dialog_cmd "button1text: ${2}"
;;
"buttondisable" )
# disable button 1
dialog_cmd "button1: disable"
;;
"buttonenable" )
# Enable button 1
dialog_cmd "button1: enable"
;;
"update" | "change" )
#
# Increment the progress bar by ${2} amount
#
# change the list item status and increment the progress bar
dialog_cmd "listitem: title: ${3}, status: ${5}, statustext: ${4}"
[[ -n "$6" ]] && dialog_cmd "progress: ${6}"
;;
"clear" )
# Clear the list and show an optional message
dialog_cmd "list: clear"
dialog_cmd "message: ${2}"
;;
"delete" )
# Delete item from list
dialog_cmd "listitem: delete, title: ${2}"
;;
"destroy" )
# Kill the progress bar and clean up
dialog_cmd "quit:"
;;
"done" )
# Complete the progress bar and clean up
dialog_cmd "progress: complete"
dialog_cmd "progresstext: $5"
;;
"icon" )
# set / clear the icon, pass <nil> if you want to clear the icon
[[ -z ${2} ]] && dialog_cmd "icon: none" || dialog_cmd "icon: ${2}"
;;
"image" )
# Display an image and show an optional message
dialog_cmd "image: ${2}"
[[ -n ${3} ]] && dialog_cmd "progresstext: $5"
;;
"infobox" )
# Show text message
dialog_cmd "infobox: ${2}"
;;
"infotext" )
# Show text message
dialog_cmd "infotext: ${2}"
;;
"show" )
# Activate the dialog box
dialog_cmd "activate:"
;;
"title" )
# Set / Clear the title, pass <nil> to clear the title
[[ -z ${2} ]] && dialog_cmd "title: none:" || dialog_cmd "title: ${2}"
;;
"progress" )
# Increment the progress bar by static amount ($6)
# Display the progress bar text ($5)
dialog_cmd "progress: ${6}"
dialog_cmd "progresstext: ${5}"
;;
esac
}
function unload_and_delete_daemon ()
{
# Unloads the launch daemon from launchctl
#
# RETURNS: None
/bin/launchctl unload -wF "${LAUNCH_DAEMON_PATH:r}"
/bin/rm -f "${LAUNCH_DAEMON_PATH}"
}
function runAsUser ()
{
launchctl asuser "$USER_UID" sudo -iu "$LOGGED_IN_USER" open "$@"
}
function array_contains ()
{
# Purpose: Scan for item inside an arry
# Paramters: #1 - Pass entire array
# #2 - Item to look for in array
# Returns: Return 0 if element exists in array
local match="$1"
shift
local item
for item in "$@"; do
[[ "$item" == "$match" ]] && return 0
done
return 1
}
function read_lockfile_pid ()
{
# Reads a file which contains a PID.
#
# RETURNS: A string representation of a PID or nothing if
# the file is empty or doesnt exist
[[ ! -e ${LOCKFILE_PATH} ]] && return
pid=$(cat "${LOCKFILE_PATH}")
echo $pid
}
function clear_lock_file ()
{
# Remove the lock file - we use this if we exit for a deferral and d
# not want to clean up other files
#
# RETURNS: None
/bin/rm -f "${LOCKFILE_PATH}"
}
function set_lock_file_with_pid ()
{
printf $$ > "${LOCKFILE_PATH}"
}
function get_nic_info ()
{
declare sname
declare sdev
declare sip
adapter=""
currentIPAddress=""
wifiName=""
# Get all active intefaces, its name & ip address
while read -r line; do
sname=$(echo "$line" | awk -F "(, )|(: )|[)]" '{print $2}' | awk '{print $1}')
sdev=$(echo "$line" | awk -F "(, )|(: )|[)]" '{print $4}')
sip=$(ipconfig getifaddr $sdev)
[[ -z $sip ]] && continue
currentIPAddress+="$(ipconfig getifaddr "$sdev") | "
adapter+="$sname | "
done <<< "$(networksetup -listnetworkserviceorder | grep 'Hardware Port')"
adapter=${adapter::-3}
currentIPAddress=${currentIPAddress::-3}
wifiName=$(sudo wdutil info | grep "SSID" | head -1 | awk -F ":" '{print $2}' | xargs)
}
function extract_xml_data ()
{
declare -a retval
# PURPOSE: extract an XML string from the passed string
# RETURN: parsed XML string
# PARAMETERS: $1 - XML "blob"
# $2 - String to extract
# EXPECTED: None
retval=$(echo "$1" | xmllint --xpath "//$2/text()" - 2>/dev/null)
printf '%s\n' "$retval"
}
function convert_to_hex ()
{
local input="$1"
local length="${#input}"
local result=""
for (( i = 0; i <= length; i++ )); do
local char="${input[i]}"
if [[ "$char" =~ [^a-zA-Z0-9] ]]; then
hex=$(printf '%x' "'$char")
result+="%$hex"
else
result+="$char"
fi
done
echo "$result"
}
function execute_in_parallel ()
{
# PURPOSE: Execute items in parallel for faster processing
local process_type="$1"
shift
local -a ids=("$@")
local -a worker_pids=()
local ID
local pid
local completed_pid
local numberOfComputers=${#ids[@]}
local completed_count=0
local progress=0
local worker_failures=0
(( numberOfComputers == 0 )) && return 0
for ID in "${ids[@]}"; do
if [[ "$process_type" == "blueprint" ]]; then
process_blueprint_computer "$ID" &