-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.sh
More file actions
executable file
·389 lines (331 loc) · 9.57 KB
/
uninstall.sh
File metadata and controls
executable file
·389 lines (331 loc) · 9.57 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
#!/bin/sh
set -eu
PATH="/usr/bin:/bin:/usr/sbin:/sbin"
export PATH
default_app_dir="/Applications"
default_bin_dir="$HOME/.local/bin"
default_skills_dir="$HOME/.agents/skills"
app_dir="${AGENT_SECRET_APP_DIR:-$default_app_dir}"
bin_dir="${AGENT_SECRET_BIN_DIR:-$default_bin_dir}"
skills_dir="${AGENT_SECRET_SKILLS_DIR:-$default_skills_dir}"
remove_audit_logs="${AGENT_SECRET_REMOVE_AUDIT_LOGS:-0}"
no_stop_daemon="${AGENT_SECRET_NO_STOP_DAEMON:-0}"
allow_custom_uninstall_paths="${AGENT_SECRET_ALLOW_CUSTOM_UNINSTALL_PATHS:-0}"
force_remove_untrusted_app="${AGENT_SECRET_FORCE_REMOVE_UNTRUSTED_APP:-0}"
expected_team_id="B6L7QLWTZW"
expected_app_bundle_id="com.kovyrin.agent-secret"
expected_daemon_bundle_id="com.kovyrin.agent-secret.daemon"
codesign_path="/usr/bin/codesign"
default_support_dir="$HOME/Library/Application Support/agent-secret"
default_audit_dir="$HOME/Library/Logs/agent-secret"
app_support_dir="${AGENT_SECRET_SUPPORT_DIR-$default_support_dir}"
audit_dir="${AGENT_SECRET_AUDIT_DIR-$default_audit_dir}"
fail() {
echo "agent-secret uninstall: $*" >&2
exit 1
}
case "$force_remove_untrusted_app" in
0 | 1) ;;
*)
fail "AGENT_SECRET_FORCE_REMOVE_UNTRUSTED_APP must be 0 or 1"
;;
esac
plist_value() {
plist="$1"
key="$2"
/usr/libexec/PlistBuddy -c "Print :$key" "$plist" 2>/dev/null
}
bundle_identifier_matches() {
bundle="$1"
expected="$2"
plist="$bundle/Contents/Info.plist"
[ -f "$plist" ] || return 1
got="$(plist_value "$plist" CFBundleIdentifier)" || return 1
[ "$got" = "$expected" ]
}
codesign_team_id() {
path="$1"
details="$("$codesign_path" -dv --verbose=4 "$path" 2>&1)" || return 1
printf '%s\n' "$details" |
/usr/bin/awk -F= '$1 == "TeamIdentifier" { print $2; found = 1; exit } END { if (!found) exit 1 }'
}
team_id_matches() {
path="$1"
team_id="$(codesign_team_id "$path")" || return 1
[ "$team_id" = "$expected_team_id" ]
}
existing_app_is_trusted() {
daemon_app="$target_app/Contents/Library/Helpers/AgentSecretDaemon.app"
cli="$target_app/Contents/Resources/bin/agent-secret"
[ -x "$cli" ] || return 1
[ -x /usr/libexec/PlistBuddy ] || return 1
[ -x "$codesign_path" ] || return 1
bundle_identifier_matches "$target_app" "$expected_app_bundle_id" || return 1
[ -d "$daemon_app" ] || return 1
bundle_identifier_matches "$daemon_app" "$expected_daemon_bundle_id" || return 1
"$codesign_path" --verify --deep --strict "$target_app" >/dev/null 2>&1 || return 1
team_id_matches "$target_app" || return 1
team_id_matches "$daemon_app" || return 1
team_id_matches "$cli" || return 1
}
stop_existing_daemon() {
if [ "$no_stop_daemon" = "1" ]; then
return
fi
existing="$target_app/Contents/Resources/bin/agent-secret"
if [ ! -d "$target_app" ]; then
return
fi
if existing_app_is_trusted; then
"$existing" daemon stop >/dev/null 2>&1 || true
else
echo "agent-secret uninstall: skipping daemon stop because existing Agent Secret.app could not be verified" >&2
fi
}
remove_app_bundle() {
echo "Removing $target_app"
if [ ! -d "$target_app" ]; then
return
fi
if existing_app_is_trusted; then
rm -rf "$target_app"
return
fi
if [ "$force_remove_untrusted_app" = "1" ]; then
echo "agent-secret uninstall: force-removing unverified Agent Secret.app: $target_app" >&2
rm -rf "$target_app"
return
fi
echo "agent-secret uninstall: leaving unverified Agent Secret.app in place: $target_app" >&2
}
remove_cli_link() {
if [ ! -L "$cli_link" ]; then
if [ -e "$cli_link" ]; then
echo "agent-secret uninstall: leaving non-symlink $cli_link in place" >&2
fi
return
fi
target="$(readlink "$cli_link")"
case "$target" in
*"Agent Secret.app/Contents/Resources/bin/agent-secret")
rm -f "$cli_link"
;;
*)
echo "agent-secret uninstall: leaving unrelated symlink $cli_link -> $target in place" >&2
;;
esac
}
remove_skill_link() {
if [ ! -L "$skill_link" ]; then
if [ -e "$skill_link" ]; then
echo "agent-secret uninstall: leaving non-symlink $skill_link in place" >&2
fi
return
fi
target="$(readlink "$skill_link")"
case "$target" in
*"Agent Secret.app/Contents/Resources/skills/agent-secret")
rm -f "$skill_link"
;;
*)
echo "agent-secret uninstall: leaving unrelated symlink $skill_link -> $target in place" >&2
;;
esac
}
strip_trailing_slashes() {
value="$1"
while [ "$value" != "/" ] && [ "${value%/}" != "$value" ]; do
value="${value%/}"
done
printf '%s\n' "$value"
}
require_custom_path_guard() {
label="$1"
path="$2"
default_path="$3"
if [ "$path" = "$default_path" ]; then
return
fi
if [ "$allow_custom_uninstall_paths" = "1" ]; then
return
fi
fail "$label path override requires AGENT_SECRET_ALLOW_CUSTOM_UNINSTALL_PATHS=1: $path"
}
is_system_root_alias() {
case "$1" in
/etc | /tmp | /var)
return 0
;;
*)
return 1
;;
esac
}
reject_symlinked_parent_dirs() {
label="$1"
path="$2"
current="${path%/*}"
if [ "$current" = "$path" ] || [ "$current" = "" ]; then
return
fi
while [ "$current" != "/" ]; do
if [ -L "$current" ] && ! is_system_root_alias "$current"; then
fail "$label path must not contain symlinked parent directories: $current"
fi
next="${current%/*}"
if [ "$next" = "$current" ] || [ "$next" = "" ]; then
current="/"
else
current="$next"
fi
done
}
deepest_existing_path() {
path="$1"
current="$path"
while [ ! -e "$current" ]; do
next="${current%/*}"
if [ "$next" = "$current" ] || [ "$next" = "" ]; then
current="/"
break
fi
current="$next"
done
printf '%s\n' "$current"
}
default_user_destination_allows_symlinks() {
label="$1"
path="$2"
case "$label:$path" in
"bin:$default_bin_dir" | "skills:$default_skills_dir") ;;
*)
return 1
;;
esac
existing="$(deepest_existing_path "$path")"
[ -d "$existing" ] || return 1
home_physical="$(cd "$HOME" && pwd -P)" || return 1
existing_physical="$(cd "$existing" && pwd -P)" || return 1
case "$existing_physical" in
"$home_physical" | "$home_physical"/*)
return 0
;;
*)
return 1
;;
esac
}
validate_agent_secret_dir() {
label="$1"
path="$(strip_trailing_slashes "$2")"
default_path="$(strip_trailing_slashes "$3")"
require_custom_path_guard "$label" "$path" "$default_path"
if [ "$path" = "" ]; then
fail "$label path is empty"
fi
case "$path" in
/*) ;;
*)
fail "$label path must be absolute: $path"
;;
esac
case "$path" in
"/" | "$HOME")
fail "$label path is too broad: $path"
;;
*/../* | */.. | */./* | */.)
fail "$label path must not contain dot segments: $path"
;;
esac
leaf="${path##*/}"
if [ "$leaf" != "agent-secret" ]; then
fail "$label path must end with agent-secret: $path"
fi
reject_symlinked_parent_dirs "$label" "$path"
if [ -L "$path" ]; then
fail "$label path must not be a symlink: $path"
fi
if [ -e "$path" ] && [ ! -d "$path" ]; then
fail "$label path is not a directory: $path"
fi
}
validate_destination_dir() {
label="$1"
path="$(strip_trailing_slashes "$2")"
default_path="$(strip_trailing_slashes "$3")"
require_custom_path_guard "$label" "$path" "$default_path"
if [ "$path" = "" ]; then
fail "$label path is empty"
fi
case "$path" in
/*) ;;
*)
fail "$label path must be absolute: $path"
;;
esac
case "$path" in
"/" | "$HOME")
fail "$label path is too broad: $path"
;;
*/../* | */.. | */./* | */.)
fail "$label path must not contain dot segments: $path"
;;
esac
if ! default_user_destination_allows_symlinks "$label" "$path"; then
reject_symlinked_parent_dirs "$label" "$path"
if [ -L "$path" ]; then
fail "$label path must not be a symlink: $path"
fi
fi
if [ -e "$path" ] && [ ! -d "$path" ]; then
fail "$label path is not a directory: $path"
fi
}
remove_known_support_dir() {
validate_agent_secret_dir "support" "$app_support_dir" "$default_support_dir"
echo "Removing $app_support_dir"
if [ ! -d "$app_support_dir" ]; then
return
fi
rm -f "$app_support_dir/agent-secretd.sock"
if ! rmdir "$app_support_dir" 2>/dev/null; then
echo "agent-secret uninstall: leaving non-empty support directory $app_support_dir in place" >&2
fi
}
remove_known_audit_dir() {
validate_agent_secret_dir "audit" "$audit_dir" "$default_audit_dir"
echo "Removing $audit_dir"
if [ ! -d "$audit_dir" ]; then
return
fi
rm -f "$audit_dir/audit.jsonl"
if ! rmdir "$audit_dir" 2>/dev/null; then
echo "agent-secret uninstall: leaving non-empty audit directory $audit_dir in place" >&2
fi
}
validate_uninstall_paths() {
validate_destination_dir "app" "$app_dir" "$default_app_dir"
validate_destination_dir "bin" "$bin_dir" "$default_bin_dir"
validate_destination_dir "skills" "$skills_dir" "$default_skills_dir"
validate_agent_secret_dir "support" "$app_support_dir" "$default_support_dir"
if [ "$remove_audit_logs" = "1" ]; then
validate_agent_secret_dir "audit" "$audit_dir" "$default_audit_dir"
fi
app_dir="$(strip_trailing_slashes "$app_dir")"
bin_dir="$(strip_trailing_slashes "$bin_dir")"
skills_dir="$(strip_trailing_slashes "$skills_dir")"
}
validate_uninstall_paths
target_app="$app_dir/Agent Secret.app"
cli_link="$bin_dir/agent-secret"
skill_link="$skills_dir/agent-secret"
stop_existing_daemon
remove_cli_link
remove_skill_link
remove_app_bundle
remove_known_support_dir
if [ "$remove_audit_logs" = "1" ]; then
remove_known_audit_dir
else
echo "Leaving audit logs in place: $audit_dir"
fi