Repository navigation
Expand file tree
/
Copy pathbash-fallback.sh
More file actions
executable file
·274 lines (244 loc) · 7.59 KB
/
Copy pathbash-fallback.sh
File metadata and controls
executable file
·274 lines (244 loc) · 7.59 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
#!/usr/bin/env bash
# bash-fallback.sh — minimal macOS application audit, no Go required.
#
# Produces a report.json that conforms to the osaat.report/v1 schema,
# so the Go binary (and `osaat diff`) can consume the output. Use this
# when you need a record of what's installed on a Mac *before* you
# bootstrap Homebrew / Go / osaat itself — for example, on a fresh
# machine you're about to migrate to.
#
# Limitations vs. the Go binary:
# - macOS only.
# - No license extraction (use the Go binary for that).
# - No age encryption.
# - No PDF / Markdown / TXT / CSV / HTML reporters.
# - No `mas` / `brew` cross-reference; source is filesystem-derived only.
# - No SHA256SUMS file.
#
# Compatibility: Bash 3.2 (the default `/bin/bash` on macOS), POSIX
# tools (awk, sed, grep, stat, du), and macOS-specific `mdls`,
# `codesign`, `hostname`, `uname`, `xattr`.
#
# Usage:
# ./bash-fallback.sh # write to stdout
# ./bash-fallback.sh --out <dir> # write <dir>/report.json
# ./bash-fallback.sh --pretty # pretty-print (default)
# ./bash-fallback.sh --help
set -euo pipefail
LC_ALL=C
export LC_ALL
readonly VERSION="1.0"
readonly SCHEMA="osaat.report/v1"
usage() {
cat <<'EOF'
osaat bash-fallback — minimal macOS application audit.
Usage:
bash-fallback.sh [--out <dir>] [--help]
Flags:
--out <dir> Write <dir>/report.json instead of stdout.
--help, -h Show this message.
The output is osaat.report/v1 compatible and can be consumed by:
osaat diff <old.json> bash-fallback-report.json
EOF
}
OUT_DIR=""
while [[ $# -gt 0 ]]; do
case "$1" in
--out)
if [[ $# -lt 2 ]]; then echo "--out requires an argument" >&2; exit 2; fi
OUT_DIR="$2"; shift 2 ;;
--help|-h)
usage; exit 0 ;;
--)
shift; break ;;
-*)
echo "unknown flag: $1" >&2
usage >&2
exit 2 ;;
*)
echo "unexpected positional argument: $1" >&2
exit 2 ;;
esac
done
if [[ "$(uname -s)" != "Darwin" ]]; then
echo "bash-fallback.sh only runs on macOS. Use the Go binary on other platforms." >&2
exit 1
fi
# json_escape stdin → stdout. Escapes \, ", control chars, and newlines.
# Intentionally simple — relies on the limited character set of plist
# values (mostly ASCII strings).
json_escape() {
local s="$1"
s="${s//\\/\\\\}"
s="${s//\"/\\\"}"
s="${s// /\\t}"
s="${s//$'\n'/\\n}"
s="${s//$'\r'/\\r}"
printf '%s' "$s"
}
# read_mdls <path> <attribute> → echoes value or empty string for "(null)".
read_mdls() {
local val
val=$(mdls -name "$2" -raw "$1" 2>/dev/null || true)
if [[ "$val" == "(null)" ]]; then
printf ''
else
printf '%s' "$val"
fi
}
# epoch_to_rfc3339 <epoch> → echoes UTC RFC 3339 timestamp.
epoch_to_rfc3339() {
local epoch="$1"
if [[ -z "$epoch" ]]; then return; fi
# macOS date(1) uses -r for "from epoch".
date -u -r "$epoch" "+%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || true
}
# signing_status <app_path> → echoes signed | ad_hoc | unsigned | unknown.
signing_status() {
local path="$1"
local out
out=$(codesign -dv --verbose=4 "$path" 2>&1 || true)
case "$out" in
*"not signed at all"*) printf 'unsigned' ;;
*"Signature=adhoc"*) printf 'ad_hoc' ;;
*"Authority="*) printf 'signed' ;;
*) printf 'unknown' ;;
esac
}
# whereFroms_url <app_path> → echoes the first URL from kMDItemWhereFroms.
whereFroms_url() {
local path="$1"
local raw
raw=$(mdls -name kMDItemWhereFroms "$path" 2>/dev/null || true)
printf '%s\n' "$raw" | grep -Eo 'https?://[^"]+' | head -n 1 || true
}
# emit_record_json — print one JSON object for a record, with field
# omission when a value is empty. Trailing comma handling is left to
# the caller (we just print the object).
emit_record_json() {
local name="$1" bundle_id="$2" version="$3" source="$4" path="$5"
local size_bytes="$6" installed_at="$7" signing="$8" download_url="$9"
printf ' {\n'
printf ' "name": "%s"' "$(json_escape "$name")"
if [[ -n "$bundle_id" ]]; then
printf ',\n "bundle_id": "%s"' "$(json_escape "$bundle_id")"
fi
if [[ -n "$version" ]]; then
printf ',\n "version": "%s"' "$(json_escape "$version")"
fi
printf ',\n "source": "%s"' "$(json_escape "$source")"
if [[ -n "$download_url" ]]; then
printf ',\n "download_url": "%s"' "$(json_escape "$download_url")"
fi
if [[ -n "$path" ]]; then
printf ',\n "path": "%s"' "$(json_escape "$path")"
fi
if [[ -n "$size_bytes" && "$size_bytes" != "0" ]]; then
printf ',\n "size_bytes": %s' "$size_bytes"
fi
if [[ -n "$installed_at" ]]; then
printf ',\n "installed_at": "%s"' "$installed_at"
fi
if [[ -n "$signing" && "$signing" != "unknown" ]]; then
printf ',\n "signing_status": "%s"' "$signing"
fi
printf '\n }'
}
# process_app — emit one record JSON object to fd 3 (the buffered
# records list). Called per .app bundle.
process_app() {
local app_path="$1" default_source="$2"
local name bundle_id version
name=$(read_mdls "$app_path" kMDItemDisplayName)
if [[ -z "$name" ]]; then
name="$(basename "$app_path" .app)"
fi
# Some apps set kMDItemDisplayName to include ".app"; strip it.
name="${name%.app}"
bundle_id=$(read_mdls "$app_path" kMDItemCFBundleIdentifier)
version=$(read_mdls "$app_path" kMDItemVersion)
local size_kb size_bytes=0
size_kb=$(du -sk "$app_path" 2>/dev/null | awk '{print $1}')
if [[ -n "$size_kb" ]]; then
size_bytes=$(( size_kb * 1024 ))
fi
local epoch installed_at
epoch=$(stat -f %m "$app_path" 2>/dev/null || true)
installed_at=$(epoch_to_rfc3339 "$epoch")
local signing
signing=$(signing_status "$app_path")
local download_url
download_url=$(whereFroms_url "$app_path")
emit_record_json "$name" "$bundle_id" "$version" \
"$default_source" "$app_path" "$size_bytes" "$installed_at" \
"$signing" "$download_url"
}
# scan_root walks one application root and emits each .app inside it.
# COUNT and FIRST track JSON comma placement.
COUNT=0
FIRST=1
scan_root() {
local root="$1" default_source="$2"
[[ -d "$root" ]] || return 0
for app in "$root"/*.app; do
[[ -d "$app" ]] || continue
if [[ $FIRST -eq 1 ]]; then
FIRST=0
else
printf ',\n'
fi
process_app "$app" "$default_source"
COUNT=$(( COUNT + 1 ))
done
}
# Header → stdout (or the file via redirection at the end).
emit_header() {
local arch
arch=$(uname -m)
case "$arch" in
x86_64) arch=amd64 ;;
aarch64) arch=arm64 ;;
esac
local hn
hn=$(hostname -s)
printf '{\n'
printf ' "schema": "%s",\n' "$SCHEMA"
printf ' "generated_at": "%s",\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
printf ' "host": {\n'
printf ' "hostname": "%s",\n' "$(json_escape "$hn")"
printf ' "os": "darwin",\n'
printf ' "arch": "%s"\n' "$arch"
printf ' },\n'
printf ' "tool": {\n'
printf ' "name": "osaat-bash-fallback",\n'
printf ' "version": "%s",\n' "$VERSION"
printf ' "commit": ""\n'
printf ' },\n'
printf ' "records": [\n'
}
emit_footer() {
if [[ $COUNT -gt 0 ]]; then
printf '\n'
fi
printf ' ]\n'
printf '}\n'
}
# Build the full document.
build_report() {
emit_header
scan_root "/Applications" "unknown"
scan_root "/Applications/Utilities" "unknown"
scan_root "$HOME/Applications" "unknown"
scan_root "/System/Applications" "system"
scan_root "/System/Applications/Utilities" "system"
emit_footer
printf 'osaat bash-fallback: %d apps audited.\n' "$COUNT" >&2
}
if [[ -n "$OUT_DIR" ]]; then
mkdir -p "$OUT_DIR"
out_path="$OUT_DIR/report.json"
build_report > "$out_path"
printf 'wrote %s\n' "$out_path"
else
build_report
fi