-
Notifications
You must be signed in to change notification settings - Fork 0
/
000_enrolment.sh
executable file
·382 lines (343 loc) · 14.8 KB
/
000_enrolment.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
#!/bin/bash
# Enrolment script used at various companies
# Author: James Smith - [email protected]
# Version 3.6.0
/usr/bin/defaults write /Library/Management/management_info.plist enrol_initial_policy_start "$(date +%s)"
working_dir="/private/var/tmp"
identifier_prefix="com.github.smithjw"
enrolment_base_string="${identifier_prefix}.mac.swiftEnrolment"
enrolment_script="${working_dir}/${enrolment_base_string}.sh"
enrolment_launchdaemon="/Library/LaunchDaemons/${enrolment_base_string}.plist"
post_enrolment_launchdaemon="/Library/LaunchDaemons/${enrolment_base_string}_post.plist"
jamf_binary="/usr/local/jamf/bin/jamf"
fde_setup_binary="/usr/bin/fdesetup"
self_service_branding_icon_name="brandingimage.png"
self_service_branding_icon_location="/Library/Management/images"
self_service_branding_icon_url="https://COMPANY.jamfcloud.com/api/v1/branding-images/download/9"
dialog_app="/usr/local/bin/dialog"
dialog_command_file="${working_dir}/dialog.log"
dialog_icon="/Library/Management/images/company_logo.png"
dialog_title="COMPANY Device Enrolment"
dialog_title_complete="You're all done!"
dialog_message="We're just installing up a few apps and configuring a few System Settings before you get started. \n\n This process should take about 10 minutes to complete. \n\n "
dialog_message_testing="This is usually where swiftDialog would quit, and the user logged out. \n\nHowever, testing_mode is enabled and FileVault deferred status is on."
dialog_message_final="We've finished installing up all the default apps required to get you started. \n\nIn a few moments you'll see a new dialog that will walk you through the remaining setup steps."
dialog_status_initial="Initial Configuration Starting..."
dialog_status_complete="Configuration Complete!"
log_folder="/private/var/log"
log_name="management.log"
dialog_cmd=(
-p
--title "$dialog_title"
--iconsize 200
--width 70%
--height 70%
--position centre
--button1disabled
--progress 30
--progresstext "$dialog_status_initial"
--blurscreen
)
#########################################################################################
# Policy Array to determine what's installed
#########################################################################################
policy_array=('
{
"steps": [
{
"listitem": "Installing management tools...",
"icon": "SF=terminal,colour=auto,weight=medium",
"trigger_list": [
{
"trigger": "install-Python",
"path": "/usr/local/bin/managed_python3"
},
{
"trigger": "install-Nudge",
"path": "/Applications/Utilities/Nudge.app/Contents/Info.plist"
},
{
"trigger": "install-Outset",
"path": "/usr/local/outset/outset"
}
]
},
{
"listitem": "Configuring Single Sign-On...",
"icon": "SF=person.crop.square.filled.and.at.rectangle,colour=auto,weight=medium",
"trigger_list": [
{
"trigger": "install-nomad",
"path": "/Applications/NoMAD.app/Contents/Info.plist"
},
{
"trigger": "install-Microsoft_Company_Portal",
"path": "/Applications/Company Portal.app/Contents/Info.plist"
}
]
},
{
"listitem": "Configuring network settings...",
"icon": "SF=network.badge.shield.half.filled,colour=auto,weight=medium",
"trigger_list": [
{
"trigger": "configure-networkprefsaccess",
"path": ""
},
{
"trigger": "configure-proxy",
"path": ""
}
]
},
{
"listitem": "Installing collaboration tools...",
"icon": "SF=bubble.left.and.bubble.right,colour=auto,weight=medium",
"trigger_list": [
{
"trigger": "install-Slack",
"path": "/Applications/Slack.app/Contents/Info.plist"
},
{
"trigger": "install-Microsoft_Teams",
"path": "/Applications/Microsoft Teams.app/Contents/Info.plist"
}
]
},
{
"listitem": "Installing browsers...",
"icon": "SF=safari,colour=auto,weight=medium",
"trigger_list": [
{
"trigger": "install-Microsoft_Edge",
"path": "/Applications/Microsoft Edge.app/Contents/Info.plist"
}
]
},
{
"listitem": "Configuring account access...",
"icon": "SF=lock.square,colour=auto,weight=medium",
"trigger_list": [
{
"trigger": "install-Privileges",
"path": "/Applications/Privileges.app/Contents/Info.plist"
},
{
"trigger": "install-macOSLAPS",
"path": "/usr/local/laps/macOSLAPS"
},
{
"trigger": "remove-admin",
"path": ""
}
]
},
{
"listitem": "Making things pretty...",
"icon": "SF=sparkles.tv,colour=auto,weight=medium",
"trigger_list": [
{
"trigger": "install-desktoppr",
"path": "/usr/local/bin/desktoppr"
},
{
"trigger": "configure-default-appearance",
"path": ""
}
]
},
{
"listitem": "Submitting Inital Computer Inventory...",
"icon": "SF=tray.and.arrow.up.fill,colour=auto,weight=medium",
"trigger_list": [
{
"trigger": "recon",
"path": ""
}
]
}
]
}
')
#########################################################################################
# Bash functions used later on
#########################################################################################
echo_logger() {
# echo_logger version 1.1
log_folder="${log_folder:=/private/var/log}"
/bin/mkdir -p "$log_folder"
echo -e "$(date +'%Y-%m-%d %T%z') - ${log_prefix:+$log_prefix }${1}" | /usr/bin/tee -a "$log_folder/${log_name:=management.log}"
}
echo_logger_heading() {
echo_logger "#########################################################################"
echo_logger "$1"
echo_logger "#########################################################################"
}
dialog_update() {
echo_logger "DIALOG: $1"
# shellcheck disable=2001
echo "$1" >> "$dialog_command_file"
}
get_json_value() {
JSON="$1" /usr/bin/osascript -l 'JavaScript' \
-e 'const env = $.NSProcessInfo.processInfo.environment.objectForKey("JSON").js' \
-e "JSON.parse(env).$2"
}
run_jamf_trigger() {
trigger="$1"
if [ "$testing_mode" = true ]; then
echo_logger "TESTING: $trigger"
/bin/sleep 1
elif [ "$trigger" == "recon" ]; then
echo_logger "RUNNING: $jamf_binary $trigger"
"$jamf_binary" "$trigger"
else
echo_logger "RUNNING: $jamf_binary policy -event $trigger"
"$jamf_binary" policy -event "$trigger"
fi
}
# Run script with -t to enable testing_mode
while getopts "t" o; do
case "${o}" in
t)
echo_logger "TESTING: Testing mode enabled"
echo_logger "TESTING: Jamf Policies will not be run"
echo_logger "TESTING: No management files will be written"
testing_mode=true
;;
*)
;;
esac
done
#########################################################################################
# Policy kickoff
#########################################################################################
echo_logger_heading "Start of _enrolment.sh"
# Check if Dialog is running
if ! /usr/bin/pgrep -qx "Dialog"; then
echo_logger "INFO: Dialog isn't running, launching now"
"$dialog_app" "${dialog_cmd[@]}" & /bin/sleep 1
else
echo_logger "INFO: Dialog is running"
echo_logger "INFO: Dialog Process: $(/usr/bin/pgrep -lx Dialog)"
dialog_update "title: $dialog_title"
dialog_update "progresstext: $dialog_status_initial"
fi
dialog_update "progress: complete"
logged_in_user=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
logged_in_user_home=$(dscl . read /Users/"$logged_in_user" NFSHomeDirectory | awk '{print $2}')
logged_in_user_uid=$(id -u "$logged_in_user")
echo_logger "INFO: User details:"
echo_logger "INFO: logged_in_user: $logged_in_user"
echo_logger "INFO: logged_in_user_home: $logged_in_user_home"
echo_logger "INFO: logged_in_user_uid: $logged_in_user_uid"
self_service_custom_icon="$logged_in_user_home/Library/Application Support/com.jamfsoftware.selfservice.mac/Documents/Images/brandingimage.png"
self_service_path=$( /usr/bin/defaults read /Library/Preferences/com.jamfsoftware.jamf.plist self_service_app_path )
if /usr/bin/curl -JLo "$self_service_branding_icon_name" --create-dirs --output-dir "$self_service_branding_icon_location" "$self_service_branding_icon_url"; then
self_service_branding_icon="$self_service_branding_icon_location/$self_service_branding_icon_name"
if [[ -f "$self_service_branding_icon" ]]; then
dialog_overlayicon="$self_service_branding_icon"
fi
elif [[ -f "$self_service_custom_icon" ]]; then
dialog_overlayicon="$self_service_custom_icon"
else
dialog_overlayicon="$self_service_path"
fi
echo_logger "INFO: self_service_branding_icon: $self_service_branding_icon"
#########################################################################################
# Main Script Logic
#########################################################################################
# Iterate through policy_array json to construct the list for swiftDialog
dialog_step_length=$(get_json_value "${policy_array[*]}" "steps.length")
for (( i=0; i<dialog_step_length; i++ )); do
listitem=$(get_json_value "${policy_array[*]}" "steps[$i].listitem")
list_item_array+=("$listitem")
done
# Updating swiftDialog with the list of items
dialog_update "overlayicon: $dialog_overlayicon"
dialog_update "icon: default"
dialog_update "icon: $dialog_icon"
dialog_update "message: $dialog_message"
dialog_update "progresstext: "
list_item_string=${list_item_array[*]/%/,}
dialog_update "list: ${list_item_string%?}"
for (( i=0; i<dialog_step_length; i++ )); do
dialog_update "listitem: index: $i, status: pending, statustext: Pending..."
done
# The ${array_name[*]/%/,} expansion will combine all items within the array adding a "," character at the end
# To add a character to the start, use "/#/" instead of the "/%/"
if [ "$testing_mode" = true ]; then /bin/sleep 2; fi
# This for loop will iterate over each distinct step in the policy_array array
for (( i=0; i<dialog_step_length; i++ )); do
# Creating initial variables
listitem=$(get_json_value "${policy_array[*]}" "steps[$i].listitem")
icon=$(get_json_value "${policy_array[*]}" "steps[$i].icon")
progresstext=$(get_json_value "${policy_array[*]}" "steps[$i].progresstext")
trigger_list_length=$(get_json_value "${policy_array[*]}" "steps[$i].trigger_list.length")
# If there's a value in the variable, update running swiftDialog
# if [[ -n "$listitem" ]]; then dialog_update "listitem: $listitem: wait"; fi
if [[ -n "$listitem" ]]; then dialog_update "listitem: index: $i, status: wait, statustext: Downloading..."; fi
if [[ -n "$icon" ]]; then dialog_update "icon: $icon"; fi
if [[ -n "$progresstext" ]]; then dialog_update "progresstext: $progresstext"; fi
if [[ -n "$trigger_list_length" ]]; then
for (( j=0; j<trigger_list_length; j++ )); do
# Setting variables within the trigger_list
trigger=$(get_json_value "${policy_array[*]}" "steps[$i].trigger_list[$j].trigger")
path=$(get_json_value "${policy_array[*]}" "steps[$i].trigger_list[$j].path")
# If the path variable has a value, check if that path exists on disk
if [[ -f "$path" ]]; then
echo_logger "INFO: $path exists, moving on"
else
run_jamf_trigger "$trigger"
fi
done
fi
if [[ -n "$listitem" ]]; then dialog_update "listitem: index: $i, status: success, statustext: Installed"; fi
done
dialog_update "title: $dialog_title_complete"
dialog_update "list: clear"
dialog_update "overlayicon: clear"
dialog_update "height: 400"
dialog_update "width: 600"
dialog_update "icon: $dialog_icon"
dialog_update "progresstext: $dialog_status_complete"
dialog_update "button1: enable"
dialog_update "button1text: Continue"
#########################################################################################
# Post Enrolment Setup
#########################################################################################
echo_logger "INFO: Configuring post_enrolment_script"
/bin/mv "${working_dir}/${enrolment_base_string}_post.plist" "${post_enrolment_launchdaemon}"
/bin/chmod 644 "${post_enrolment_launchdaemon}"
/usr/sbin/chown root:wheel "${post_enrolment_launchdaemon}"
#########################################################################################
# Script Cleanup
#########################################################################################
if [ "$testing_mode" = true ]; then
echo_logger "TESTING: Displaying message and exiting out now"
dialog_update "message: $dialog_message_testing"
/bin/sleep 5
dialog_update "quit:"
else
echo_logger "Writing enrol_initial_policy_end into management_info.plist"
/usr/bin/defaults write /Library/Management/management_info.plist enrol_initial_policy_end "$(date +%s)"
filevault_status=$($fde_setup_binary status | grep "Deferred" | cut -d ' ' -f6)
if [ "$filevault_status" = "active" ]; then
echo_logger "INFO: FileVault is deferred, logging out now"
dialog_update "quit:"
/bin/launchctl bootout user/"$logged_in_user_uid"
else
echo_logger "INFO: FileVault is already enabled, moving on"
dialog_update "message: $dialog_message_final"
fi
fi
if /bin/launchctl bootstrap system "${post_enrolment_launchdaemon}"; then
echo_logger "INFO: post_enrolment_launchdaemon launched, exiting script"
echo_logger "INFO: swiftEnrolment dialog will remain until user_walkthrough has begun"
else
echo_logger "WARNING: post_enrolment_launchdaemon didn't launch correctly"
echo_logger "WARNING: Quitting swiftEnrolment dialog"
/bin/sleep 2
dialog_update "quit:"
fi
exit 0