-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjamf.sh
executable file
·256 lines (216 loc) · 6.85 KB
/
jamf.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
#!/bin/bash
# Paths
GLOBAL_LAUNCH_AGENTS_PATH="/Library/LaunchAgents"
GLOBAL_LAUNCH_DAEMONS_PATH="/Library/LaunchDaemons"
JAMF_MANAGEMENT_PATH="/Library/Application Support/JAMF"
BACKUP_PATH="/Users/Shared/removejamf-restore-resources"
# Files
LAUNCH_AGENTS=(
"com.jamf.management.agent.plist"
)
LAUNCH_DAEMONS=(
"com.jamf.management.daemon.plist"
"com.jamf.management.startup.plist"
"com.jamfsoftware.task.1.plist"
)
JAMF_CONFIG_FILE=".jmf_settings.json"
# Constants
JAMF_CONFIG_JSON='{
"mcxEnabled": false,
"googleBeyondCorpEnabled": false,
"hideRestorePartition": false,
"beaconMonitoring": false,
"blacklist": [],
"applicationUsage": false,
"checkForPoliciesAtLogin": true,
"networkStateChange": false,
"logUserInfoAtLogin": false
}'
# Function to remove agent
# $1: Name of the agent
# $2: Path of the agent
# $3: "agent" or "daemon"
function remove_service() {
local service_name="$1"
local service_path="$2"
local service_type="$3"
# Check if doesn't exist
if [ ! -f "$service_path" ]; then
echo "[❓] $service_type: $service_name does not exist, skipping..."
return
fi
# Try to stop the service
launchctl unload "$service_path"
if [ $? -ne 0 ]; then
echo "[❓] Failed to stop $service_type: $service_name, continuing..."
else
echo "[📦] Stopped $service_type: $service_name"
fi
# Change permissions to delete the service
chmod 777 "$service_path"
if [ $? -ne 0 ]; then
echo "[❓] Failed to change permissions for $service_type: $service_name"
return
fi
# Backup the service
cp "$service_path" "$BACKUP_PATH"
if [ $? -ne 0 ]; then
echo "[❓] Failed to backup $service_type: $service_name"
return
fi
# Remove the service
rm "$service_path"
if [ $? -ne 0 ]; then
echo "[❓] Failed to remove $service_type: $service_name"
return
fi
echo "[🗑] Removed and backed up $service_type: $service_name"
}
# Function to update JAMF Config file
function update_config() {
# Change permissions to delete the config file
chmod 777 "$JAMF_MANAGEMENT_PATH/$JAMF_CONFIG_FILE"
if [ $? -ne 0 ]; then
echo "[❓] Failed to change permissions for config file"
return
fi
# Backup the config file
cp "$JAMF_MANAGEMENT_PATH/$JAMF_CONFIG_FILE" "$BACKUP_PATH"
if [ $? -ne 0 ]; then
echo "[❓] Failed to backup config file"
return
fi
# Write data to JAMF Config file
echo "$JAMF_CONFIG_JSON" > "$JAMF_MANAGEMENT_PATH/$JAMF_CONFIG_FILE"
if [ $? -ne 0 ]; then
echo "[❓] Failed to write to config file"
return
fi
}
# Restore service
# $1: Name of the service
# $2: Path of the service
# $3: "agent" or "daemon"
function restore_service() {
local service_name="$1"
local service_path="$2"
local service_type="$3"
# Check if backup exists
if [ ! -f "$BACKUP_PATH/$service_name" ]; then
echo "[❓] $service_type: $service_name does not exist in backup, skipping..."
return
fi
# Restore service
cp -f "$BACKUP_PATH/$service_name" "$service_path"
if [ $? -ne 0 ]; then
echo "[❓] Failed to restore $service_type: $service_name"
return
fi
# Change permissions
chmod 644 "$service_path"
if [ $? -ne 0 ]; then
echo "[❓] Failed to change permissions for $service_type: $service_name"
return
fi
# Start service
launchctl load "$service_path"
if [ $? -ne 0 ]; then
echo "[❓] Failed to start $service_type: $service_name"
return
fi
echo "[📦] Restored $service_type: $service_name"
}
# Restore JAMF configuration
function restore_jamf_config() {
# Check if backup exists
if [ ! -f "$BACKUP_PATH/$JAMF_CONFIG_FILE" ]; then
echo "[❓] JAMF configuration does not exist in backup, skipping..."
return
fi
# Restore JAMF configuration
cp -f "$BACKUP_PATH/$JAMF_CONFIG_FILE" "$JAMF_MANAGEMENT_PATH/$JAMF_CONFIG_FILE"
if [ $? -ne 0 ]; then
echo "[❓] Failed to restore JAMF configuration"
return
fi
echo "[📦] Restored JAMF configuration"
}
# Prompt to restart Y/N
function prompt_restart() {
read -p "Would you like to restart the system now? (Y/N): " restart
if [ "$restart" == "Y" ] || [ "$restart" == "y" ]; then
echo "[🚀] Restarting system..."
shutdown -r now
else
echo "[👋] Exiting..."
fi
}
# Restore services and configuration
function restore() {
# Check if backup exists
if [ ! -d "$BACKUP_PATH" ]; then
echo "[❓] Backup directory does not exist, exiting..."
exit 1
fi
# Restore services
for agent in "${LAUNCH_AGENTS[@]}"; do
restore_service "$agent" "$GLOBAL_LAUNCH_AGENTS_PATH/$agent" "agent"
done
for daemon in "${LAUNCH_DAEMONS[@]}"; do
restore_service "$daemon" "$GLOBAL_LAUNCH_DAEMONS_PATH/$daemon" "daemon"
done
# Restore JAMF configuration
restore_jamf_config
echo "[📦] Successfully restored JAMF services and configuration."
echo "[🔄] Even though services were successfully restored, it is recommended to restart the system for the changes to take effect."
prompt_restart
}
# Remove services and configuration
function remove() {
# Create backup directory if it does not exist
if [ ! -d "$BACKUP_PATH" ]; then
mkdir "$BACKUP_PATH"
echo "[📁] Created backup folder..."
fi
# Loop through launch agents and daemons
for agent in "${LAUNCH_AGENTS[@]}"; do
remove_service "$agent" "$GLOBAL_LAUNCH_AGENTS_PATH/$agent" "agent"
done
for daemon in "${LAUNCH_DAEMONS[@]}"; do
remove_service "$daemon" "$GLOBAL_LAUNCH_DAEMONS_PATH/$daemon" "daemon"
done
if [ ! -f "$JAMF_MANAGEMENT_PATH/$JAMF_CONFIG_FILE" ]; then
echo "[❓] JAMF Config file does not exist, skipping..."
else
update_config
fi
echo "[🗑] Successfully removed JAMF services and backed them up."
echo "[🔄] Even though services were successfully removed and backed up, it is recommended to restart the system for the changes to take effect."
prompt_restart
}
# Ask user to remove or restore
function main() {
# Check if running in sudo
if [ "$EUID" -ne 0 ]; then
echo "[❓] Please run this script with sudo."
exit 1
fi
echo "This script will remove or restore JAMF services and configuration."
echo "Please choose an option:"
echo "1. Remove JAMF services and configuration"
echo "2. Restore JAMF services and configuration"
read -p "Enter your choice (1/2): " choice
case $choice in
1)
remove
;;
2)
restore
;;
*)
echo "[❓] Invalid choice, exiting..."
;;
esac
}
# Run main function
main