forked from mm2270/JamfProScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_SelfService_Plug-in.sh
417 lines (311 loc) · 10.6 KB
/
create_SelfService_Plug-in.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
#!/bin/bash
## Script: create_SelfService_Plug-in.sh
## Author: Mike Morales
##
## Change log:
## 2015-08-06:
## Added lines to process several user passed variables to ensure they conform to xml standards
## 2015-01-12:
## Changed to use sips to check image aspect ratio and pre-convert image (to tmp file) to 128x128 pixels to use for base64 encoding
function createPlugIn ()
{
echo "Starting Plug-in creation..."
echo "Checking for existing Plug-ins..."
sleep 0.5
## Process some of the passed strings to make sure they conform to xml standards
URL=$(echo "$URL" | sed -e 's/"/"/g;s/&/&/g;s/>/>/g;s/</</g' | sed "s/'/'/g")
title=$(echo "$title" | sed -e 's/"/"/g;s/&/&/g;s/>/>/g;s/</</g' | sed "s/'/'/g")
subtitle=$(echo "$subtitle" | sed -e 's/"/"/g;s/&/&/g;s/>/>/g;s/</</g' | sed "s/'/'/g")
if [ -d "/Library/Application Support/JAMF/" ]; then
## Check for an existing Self Service Plug-ins directory
if [ -d "/Library/Application Support/JAMF/Self Service/Plug-ins" ]; then
pluginFolderExists="yes"
destDir="/Library/Application Support/JAMF/Self Service/Plug-ins"
## Capture a list of any installed plug-ins
installedPlugIns=$(ls "/Library/Application Support/JAMF/Self Service/Plug-ins" | sed 's/.plist//g' | sort -g | awk '$1 > 999 {print}')
## If any are in the 1000+ range, determine the next available ID
if [ ! -z "$installedPlugIns" ]; then
lastID=$(ls "/Library/Application Support/JAMF/Self Service/Plug-ins" | sed 's/.plist//g' | sort -g | awk '$1 > 999 {print}' | tail -1)
nextID=$((lastID+1))
else
## If none are in the 1000+ range, set the ID to 1000
nextID="1000"
fi
else
## If there is no Plug-Ins folder, set the ID to 1000
nextID="1000"
echo "The Self Service Plug-ins folder doesn't exist on this Mac. Creating it now..."
mkdir -p "/Library/Application Support/JAMF/Self Service/Plug-ins"
chown root:admin "/Library/Application Support/JAMF/Self Service/Plug-ins"
chmod -R 755 "/Library/Application Support/JAMF/Self Service/Plug-ins"
pluginFolderExists="yes"
destDir="/Library/Application Support/JAMF/Self Service/Plug-ins"
sleep 0.5
fi
else
## The 'JAMF' directory does not exist on this Mac. Therefore, its an un-enrolled system
echo -e "No '/Library/Application Support/JAMF' directory was found on this Mac.\nThe Plug-in will be saved to your Desktop."
nextID="1000"
destDir="/Users/$loggedInUser/Desktop"
sleep 0.5
fi
## If an image was chosen, convert it to binary data
if [ ! -z "$ICON" ]; then
## Resize to 128 x 128 max height and width if the image is larger in either dimension
if [[ "$imageH" || "$imageW" -gt 128 ]]; then
sips -z 128 128 2>&1 >/dev/null "$ICON" -o "/private/tmp/$ICON_NAME" 2>&1 >/dev/null
IMAGE_PATH="/private/tmp/$ICON_NAME"
else
IMAGE_PATH="$ICON"
fi
echo "Converting image file to binary data..."
sleep 0.5
imageData=$(cat "$IMAGE_PATH" | base64)
fi
id="$nextID"
url="$URL"
title="$TITLE"
subtitle="$SUBTITLE"
priority="$PRIORITY"
openInBrowser="$OPENINBROWSER"
icon="$imageData"
echo "Creating $TITLE Plug-in..."
sleep 0.5
## Create the Plug-in plist file
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>id</key>
<string>$id</string>
<key>version</key>
<integer>1</integer>
<key>url</key>
<string>$url</string>
<key>title</key>
<string>$title</string>
<key>priority</key>
<string>$priority</string>
<key>subtitle</key>
<string>$subtitle</string>
<key>openInBrowserAutomatically</key>
<$openInBrowser/>
<key>image</key>
<data>$imageData</data>
</dict>
</plist>" > "${destDir}/${id}.plist"
if [ "$?" == "0" ]; then
if [ "$pluginFolderExists" == "yes" ]; then
echo -e "The Plug-in has been saved to the location: ${destDir}/${id}.plist. Self Service should now show the Plug-in\n"
## Clean up the tmp image file
rm "/private/tmp/$ICON_NAME" 2>/dev/null
exit 0
else
echo -e "The Plug-in has been saved to the location: ${destDir}/${id}.plist. If necessary, you can package this plug-in plist and deploy it to other Macs.\n"
## Clean up the tmp image file
rm "/private/tmp/$ICON_NAME" 2>/dev/null
exit 0
fi
else
echo "We ran into an error while creating the Plug-in.
However, its possible the Plug-in was successfully saved. If its not showing up in Self Service,
try running this script again to create it."
## Clean up the tmp image file
rm "/private/tmp/$ICON_NAME" 2>/dev/null
exit 1
fi
}
function finalSteps ()
{
if [ -z "$SUBTITLE" ]; then
SUBTITLE_PRINT="* None chosen *"
else
SUBTITLE_PRINT="$SUBTITLE"
fi
if [ -z "$ICON" ]; then
ICON_PRINT="* None chosen *"
else
ICON_PRINT="$ICON"
fi
finalText="Looks like we have everything we need.
Check the settings you chose below and press Enter or Return to create the Self Service URL Plug-in:
URL: $URL
Title: $TITLE
Description: $SUBTITLE_PRINT
Priority: $PRIORITY
Icon: $ICON_PRINT
Open in browser: $OPENINBROWSER
If everything above looks good, press 'Enter' or 'Return' to create the Plug-in.
If you would like to start over, type in REDO and press Enter or Return"
echo "$finalText"
read PROCESS
if [ -z "$PROCESS" ]; then
echo -e "Processing...\n"
createPlugIn
elif [[ "$PROCESS" == "REDO" || "redo" ]]; then
echo -e "Starting over...\n"
sleep 0.5
askForURL
fi
}
function askBrowserPref ()
{
browserPrefText="Step 6: Choose if you would like the Plug-in to open in a default browser, or load into Self Service.
Type in \"yes\" for opening in an external browser, or press Enter/Return to have it open in Self Service."
echo "$browserPrefText"
read BROWSER
shopt -s nocasematch
if [[ ! -z "$BROWSER" ]] && [[ "$BROWSER" == "yes" ]]; then
OPENINBROWSER="true"
echo -e "Setting chosen was: \"Open in browser\" Continuing...\n"
shopt -u nocasematch
sleep 0.5
finalSteps
else
OPENINBROWSER="false"
echo -e "Setting chosen was: \"Open in Self Service\" Continuing...\n"
sleep 0.5
finalSteps
fi
}
function askForIcon ()
{
if [ -z "$iconText" ]; then
iconText="Step 5: Drag and drop an image file from the Finder to be used for the Plug-in icon (Optional but Recommended) and press Enter/Return
Preferred format is PNG at 128x128 pixels, but can also accept .GIF, .TIF or .JPG formats.
Also, square images (pixel dimensions) work best, otherwise the icon will get squished disportionately within Self Service.app."
fi
echo "$iconText"
read ICON
if [ -z "$ICON" ]; then
echo -e "Icon selected: None. This Self Service Plug-in will be created without an icon. Continuing...\n"
askBrowserPref
else
## Test to make sure we have a GIF, PNG, TIFF or JPEG file for the image
imageFormat=$(sips -g format "$ICON" | awk '/format/{print $NF}')
## Get the name of just the image file
ICON_NAME=$(basename "$ICON")
case "$imageFormat" in
png|tiff|gif|jpeg)
imageH=$(sips -g pixelHeight "$ICON" | awk '{getline; print $NF}')
imageW=$(sips -g pixelWidth "$ICON" | awk '{getline; print $NF}')
if [[ "$imageH" -ne "$imageW" ]]; then
echo "*** NOTE: This image is not in a square aspect ratio. The image may become distorted when converted ***"
fi
echo -e "Icon selected: $ICON_NAME. Continuing...\n"
askBrowserPref ;;
*)
iconText="The image ${ICON_NAME} appears to be in $imageFormat format. This is not one of the accepted formats. Please use only a png, gif, tif or jpeg and try again:"
askForIcon ;;
esac
fi
}
function askForPriority ()
{
if [ -z "$priorityText" ]; then
priorityText="Step 4: Enter a numeric value (1-20) for the priority of the Plug-in (Optional) and press Enter or Return
Lower values mean the Plug-in will appear before others in the Self Service sidebar.
Note that you can leave a default value of 5 by pressing Enter or Return:"
fi
echo "$priorityText"
read PRIORITY
if [ -z "$PRIORITY" ]; then
echo -e "No value assigned. Using a default of \"5\"\n"
PRIORITY="5"
sleep 0.5
askForIcon
else
## Test to make sure we received an integer value
test=$(echo "$PRIORITY / $PRIORITY" | bc)
if [ "$test" == "1" ]; then
if [[ "$PRIORITY" -gt 0 ]] && [[ "$PRIORITY" -lt 21 ]]; then
echo -e "Value entered: \"$PRIORITY\" Continuing...\n"
sleep 0.5
askForIcon
else
priorityText="Oops! We can only accept a number value between 1-20. Please try again, or press Enter or Return to use the default value:"
askForPriority
fi
else
priorityText="Oops! We can only accept a number value between 1-20. No letters or punctuation. Please try again, or press Enter or Return to use the default value:"
askForPriority
fi
fi
}
function askForSubtitle ()
{
subTitleText="Step 3: Enter a description for the Plug-in (Optional), then Press Enter or Return
(Note: If you don't want a description, simply press Enter or Return to continue:"
echo "$subTitleText"
read SUBTITLE
if [ -z "$SUBTITLE" ]; then
echo -e "No description was specified. Continuing...\n"
sleep 0.5
askForPriority
else
echo -e "Description entered: \"$SUBTITLE\" Continuing...\n"
sleep 0.5
askForPriority
fi
}
function askForTitle ()
{
if [ -z "$titleText" ]; then
titleText="Step 2: Enter a title for the Plug-in (Required), then press Enter or Return:"
fi
echo "$titleText"
read TITLE
if [ -z "$TITLE" ]; then
titleText="Oops! A Title is required! Enter a title for the Plug-in, then press Enter or Return:"
askForTitle
else
echo -e "Title entered: \"$TITLE\" Continuing...\n"
sleep 0.5
askForSubtitle
fi
}
function askForURL ()
{
if [ -z "$urlText" ]; then
urlText="Step 1: Enter a URL to use for the Plug-in (Required), then press Enter or Return:"
fi
echo "$urlText"
read URL
if [ -z "$URL" ]; then
urlText="Oops! A URL is required! Please enter a URL and press Enter or Return:"
askForURL
else
echo -e "URL entered was: \"$URL\" Continuing...\n"
sleep 0.5
askForTitle
fi
}
function initialUsage ()
{
startMessage="This script will guide you in generating a Self Service URL Plug-in.
By default, new Self Service URL plug-ins are generated with an ID in the 1000 plus range
so as to avoid any conflict with Plug-ins that may be set up on your JSS.
When ready to get started, just press 'Enter' or 'Return'. Otherwise, type 'exit' and press 'Return' to exit the script."
echo "$startMessage"
read BEGIN
if [ "$BEGIN" == "" ]; then
echo -e "Starting...\n"
sleep 0.5
askForURL
elif [ "$BEGIN" == "exit" ]; then
echo "Exiting. Good-bye!"
exit 0
else
initialUsage
fi
}
function checkForRoot ()
{
if [[ $EUID -ne 0 ]]; then
echo -e "This script must be run as root. Please use 'sudo /path/to/script.sh' and try again\n"
exit 1
else
initialUsage
fi
}
checkForRoot