Repository navigation
Expand file tree
/
Copy pathscan.sh
More file actions
executable file
·1770 lines (1616 loc) · 59.9 KB
/
Copy pathscan.sh
File metadata and controls
executable file
·1770 lines (1616 loc) · 59.9 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# scan.sh — install Atomdrift Scan (the `atomscan` CLI).
#
# Generated from templates/install.sh.in and products/scan.conf.
# Do not edit this file: edit one of those and run `make regen`.
#
# curl -fsSL https://install.atomdrift.org/scan.sh | sh
#
# Passing options through a pipe needs `sh -s --`:
#
# curl -fsSL https://install.atomdrift.org/scan.sh | sh -s -- --dir ~/bin --method binary
#
# What it does, in order: work out the platform, pick an install method, fetch a
# release binary (checksum- and provenance-verified), fall back to a source
# build when no binary exists for the platform, then install the optional
# analysis tools that are available from the machine's configured repositories.
#
# Re-running is safe and cheap: an install that is already current is left
# alone, and everything written to the install directory is written atomically.
#
# POSIX sh only — no bashisms, no `local`, no arrays. This has to run on macOS,
# Linux (glibc and musl), FreeBSD, OpenBSD, NetBSD, DragonFly, and illumos,
# where /bin/sh may be dash, ash, busybox, or ksh93. Function-scoped variables
# do not exist here, so each function prefixes its own.
set -eu
REPO="atomdrift-project/scan"
BIN="atomscan"
TAP="atomdrift-project/tap"
# Homebrew formula name inside TAP, empty when this tool has no formula yet.
# Empty disables the brew method rather than offering an install that cannot
# work: the tap is the source of truth for what Homebrew can actually build.
FORMULA="scan"
# Extra tap formulae `brew trust` must cover for FORMULA to evaluate, because
# Homebrew 6 trusts each non-official formula individually rather than by tap.
FORMULA_DEPS="cleave"
DISPLAY="Atomdrift Scan"
READY="Ready to scan"
FOOTER="Installer refreshes models, rules, and bloom filters when possible; future scans update them when stale."
# Closing "Try it" lines, as "label:arguments" — see summary().
EXAMPLES="project:./project
package:purl npm/left-pad@1.3.0
explore:--help"
# Branch a source build may fall back to before the first release exists, empty
# once there is one to resolve. See install_source.
UNRELEASED_REF=""
# Targets published by .github/workflows/release.yml. Anything else takes the
# source path.
TARGETS="
x86_64-unknown-linux-gnu
aarch64-unknown-linux-gnu
x86_64-unknown-linux-musl
aarch64-unknown-linux-musl
arm-unknown-linux-musleabihf
armv7-unknown-linux-musleabihf
loongarch64-unknown-linux-musl
s390x-unknown-linux-gnu
riscv64gc-unknown-linux-gnu
powerpc64le-unknown-linux-gnu
aarch64-apple-darwin
x86_64-apple-darwin
x86_64-unknown-freebsd
x86_64-unknown-openbsd
x86_64-unknown-netbsd
x86_64-unknown-illumos
x86_64-pc-solaris
"
# Settings, overridable by flag or environment.
OPT_VERSION="${ATOMSCAN_VERSION:-}"
OPT_DIR="${ATOMSCAN_INSTALL_DIR:-}"
OPT_METHOD="${ATOMSCAN_METHOD:-auto}"
OPT_TOOLS=1
OPT_UPDATE=1
OPT_FORCE=0
OPT_QUIET=0
[ -n "${ATOMSCAN_NO_TOOLS:-}" ] && OPT_TOOLS=0
[ -n "${ATOMSCAN_NO_UPDATE:-}" ] && OPT_UPDATE=0
UPDATE_COMMAND="update-rules"
# Filled in as we go; declared here so the shape of the script is visible.
HOST_OS="" # uname -s; detect_platform sets it, everything else reads it
SELF="" # this machine's target triple, published or not
TARGET="" # same, but empty unless a release actually carries it
PLATFORM="" # human-readable platform description
METHOD="" # resolved: brew | binary | source
VERSION="" # version being installed, without the leading v
INSTALL_DIR="" # directory the binary lands in
INSTALLED="" # full path of the binary we installed
CHANGED=0 # whether this run actually replaced anything
ALREADY_CURRENT=0 # target version was already installed at the chosen path
INSTALL_ESCALATOR="" # doas | pfexec | sudo, when INSTALL_DIR needs privilege
INSTALL_PRIVILEGED=0 # whether writes to INSTALL_DIR need that escalator
PRIVILEGE_APPROVED=0 # user approved the selected escalator for this run
PRIVILEGE_DENIED=0 # do not ask repeatedly after a declined/noninteractive prompt
BREW_PREFIX="" # Homebrew root, empty when there is no Homebrew
DOWNLOADER="" # curl | wget | fetch | ftp
WGET_MODERN=0 # GNU wget (spider, header dumps) rather than busybox wget
TMP="" # scratch directory, removed on exit
INSTALL_TMP="" # same-directory binary staging file, removed on failure
SOURCE_STAGE="" # staged source clone, removed when an install is interrupted
SOURCE_OLD="" # previous checkout, restored if a staged swap is interrupted
SOURCE_DEST="" # canonical checkout path paired with SOURCE_OLD
usage() {
cat <<EOF
Install Atomdrift Scan — malware and supply-chain analysis for files,
directories, archives, packages, URLs, and processes.
Usage: scan.sh [options]
curl -fsSL https://install.atomdrift.org/scan.sh | sh -s -- [options]
Options:
--version VERSION Install a specific version (default: the latest release).
--dir DIR Install into DIR (default: a suitable directory on PATH).
--method METHOD auto, binary, brew, or source. auto prefers Homebrew where
a formula exists, then a release binary, then source.
--no-tools Skip optional analysis tool installation (rizin, upx, ...).
--no-update Skip any supported post-install model and rules update.
--force Reinstall even when the target version is already there.
--quiet Only report problems.
--help Show this message.
Environment:
ATOMSCAN_VERSION, ATOMSCAN_INSTALL_DIR, ATOMSCAN_METHOD, ATOMSCAN_NO_TOOLS, ATOMSCAN_NO_UPDATE
SCAN_THEME — dark (default) or light.
SCAN_ASCII — force ASCII glyphs instead of the branded Unicode UI.
NO_COLOR — disable colour (any value).
To uninstall: delete the binary whose path this prints${FORMULA:+, or run
\`brew uninstall $TAP/$FORMULA\` if it was installed with Homebrew}.
The default prefers a user-owned directory already on PATH. If none is usable,
it may install into the operating system's local PATH using doas, pfexec, or
sudo (in that preference order).
EOF
}
# valid_version STRING — true for a release identifier we are willing to place
# in a URL or a filesystem path. Applied to resolved tags as well as typed ones:
# a tag arrives from the network and has earned no more trust than an argument.
valid_version() {
case $1 in
'' | *[!0-9A-Za-z._+-]*) return 1 ;;
esac
}
parse_args() {
while [ $# -gt 0 ]; do
case $1 in
--version) [ $# -ge 2 ] || die "--version needs a value"; OPT_VERSION=$2; shift 2 ;;
--version=*) OPT_VERSION=${1#*=}; shift ;;
--dir) [ $# -ge 2 ] || die "--dir needs a value"; OPT_DIR=$2; shift 2 ;;
--dir=*) OPT_DIR=${1#*=}; shift ;;
--method) [ $# -ge 2 ] || die "--method needs a value"; OPT_METHOD=$2; shift 2 ;;
--method=*) OPT_METHOD=${1#*=}; shift ;;
--no-tools) OPT_TOOLS=0; shift ;;
--no-update) OPT_UPDATE=0; shift ;;
--force) OPT_FORCE=1; shift ;;
--quiet | -q) OPT_QUIET=1; shift ;;
--help | -h) usage; exit 0 ;;
*) usage >&2; printf '\nunknown option: %s\n' "$1" >&2; exit 2 ;;
esac
done
case $OPT_METHOD in
auto | binary | brew | source) ;;
*) die "--method must be auto, binary, brew, or source (got '$OPT_METHOD')" ;;
esac
OPT_VERSION=${OPT_VERSION#v}
if [ -n "$OPT_VERSION" ]; then
valid_version "$OPT_VERSION" || die "invalid version '$OPT_VERSION'"
fi
}
# ---------------------------------------------------------------------------
# Style
#
# The theme and glyph switches are atomscan's own environment variables, so the
# installer and the tool it installs agree on how they look.
#
# Follow the Atomdrift website: blue for motion, toxic lime for completion,
# amber for attention, red for failure, and a readable neutral for supporting
# detail. Redirected output and NO_COLOR remain clean plain text.
# ---------------------------------------------------------------------------
setup_style() {
ESC=$(printf '\033')
TTY=0
[ -t 1 ] && TTY=1
if [ "${NO_COLOR+x}" = x ] || [ "$TTY" = 0 ] || [ "${TERM:-dumb}" = dumb ]; then
C_RED='' C_AMBER='' C_GREEN='' C_BRAND=''
C_DIM='' C_BOLD='' C_RESET=''
else
case "${SCAN_THEME:-dark}" in
light | white)
C_RED="${ESC}[38;2;220;38;38m" C_AMBER="${ESC}[38;2;217;119;6m"
C_GREEN="${ESC}[38;2;74;107;15m" C_BRAND="${ESC}[38;2;37;99;235m"
C_DIM="${ESC}[38;2;107;114;128m"
;;
*)
C_RED="${ESC}[38;2;248;113;113m" C_AMBER="${ESC}[38;2;251;191;36m"
C_GREEN="${ESC}[38;2;208;255;0m" C_BRAND="${ESC}[38;2;96;165;250m"
C_DIM="${ESC}[38;2;161;161;170m"
;;
esac
C_BOLD="${ESC}[1m" C_RESET="${ESC}[0m"
fi
# Friendly glyphs in UTF-8 locales, with an ASCII fallback for old consoles
# and minimal environments.
UTF8=0
if [ -z "${SCAN_ASCII:-}" ] && [ "${TERM:-dumb}" != dumb ]; then
case "${LC_ALL:-${LC_CTYPE:-${LANG:-}}}" in
*[Uu][Tt][Ff]-8* | *[Uu][Tt][Ff]8*) UTF8=1 ;;
*) [ "$TTY" = 1 ] && UTF8=1 ;;
esac
fi
if [ "$UTF8" = 1 ]; then
G_SCAN="⚛️" G_STEP="·" G_OK="✓" G_WARN="⚠" G_ERR="✗"
else
G_SCAN="*" G_STEP="-" G_OK="+" G_WARN="!" G_ERR="x"
fi
}
# step LABEL VALUE — one aligned line of progress.
step() {
if [ "$OPT_QUIET" = 0 ]; then
printf ' %s%s%s %s%-11s%s%s\n' "$C_BRAND" "$G_STEP" "$C_RESET" "$C_DIM" "$1" "$C_RESET" "$2"
fi
}
ok() {
if [ "$OPT_QUIET" = 0 ]; then
printf ' %s%s%s %s%-11s%s%s\n' "$C_GREEN" "$G_OK" "$C_RESET" "$C_DIM" "$1" "$C_RESET" "$2"
fi
}
note() {
if [ "$OPT_QUIET" = 0 ]; then
printf ' %s%s%s\n' "$C_DIM" "$1" "$C_RESET"
fi
}
gap() {
[ "$OPT_QUIET" = 1 ] || printf '\n'
}
warn() {
printf ' %s%s%s %s\n' "$C_AMBER" "$G_WARN" "$C_RESET" "$1" >&2
}
die() {
printf '\n %s%s%s %s%s%s\n\n' "$C_RED" "$G_ERR" "$C_RESET" "$C_BOLD" "$1" "$C_RESET" >&2
exit 1
}
banner() {
if [ "$OPT_QUIET" = 1 ]; then
return 0
fi
printf '\n %s%s%s %sInstalling %s%s\n\n' \
"$C_BRAND" "$G_SCAN" "$C_RESET" "$C_BOLD" "$DISPLAY" "$C_RESET"
}
# ---------------------------------------------------------------------------
# Scratch space
# ---------------------------------------------------------------------------
cleanup() {
[ -n "$TMP" ] && rm -rf "$TMP"
if [ -n "$INSTALL_TMP" ]; then
install_run rm -f "$INSTALL_TMP" 2>/dev/null || :
fi
[ -n "$SOURCE_STAGE" ] && rm -rf "$SOURCE_STAGE"
if [ -n "$SOURCE_OLD" ]; then
if [ -n "$SOURCE_DEST" ] && [ ! -e "$SOURCE_DEST" ] && [ ! -L "$SOURCE_DEST" ]; then
# Interrupted mid-swap: put the previous checkout back.
mv "$SOURCE_OLD" "$SOURCE_DEST" 2>/dev/null || :
else
# The new checkout is already in place, so this one is only taking
# up several gigabytes of somebody's cache directory.
rm -rf "$SOURCE_OLD" 2>/dev/null || :
fi
fi
return 0
}
make_tmpdir() {
TMP=$(mktemp -d 2>/dev/null) || TMP=""
if [ -z "$TMP" ]; then
TMP="${TMPDIR:-/tmp}/atomscan-install.$$"
(umask 077 && mkdir "$TMP") || die "cannot create a temporary directory"
fi
}
# ---------------------------------------------------------------------------
# Platform
# ---------------------------------------------------------------------------
# Also the one place `uname -s` is read: everything downstream consults HOST_OS,
# so the whole run agrees on one answer and spends one fork getting it.
detect_platform() {
HOST_OS=$(uname -s 2>/dev/null || echo unknown)
dp_arch=$(uname -m 2>/dev/null || echo unknown)
dp_desc=""
case $dp_arch in
x86_64 | amd64) dp_arch=x86_64 ;;
i86pc) dp_arch=x86_64 ;;
aarch64 | arm64) dp_arch=aarch64 ;;
armv6 | armv6l) dp_arch=arm ;;
armv7 | armv7l | armv8l | armhf) dp_arch=armv7 ;;
loongarch64) dp_arch=loongarch64 ;;
riscv64 | riscv64gc) dp_arch=riscv64gc ;;
ppc64le | powerpc64le) dp_arch=powerpc64le ;;
esac
case $HOST_OS in
Linux)
dp_operating=$(uname -o 2>/dev/null || :)
dp_libc=gnu
case "$dp_operating:${ANDROID_ROOT:-}" in
*[Aa]ndroid* | *:/system*) dp_libc=musl ;;
*)
# musl announces itself only in ldd's usage message, and that ldd
# exits non-zero while printing it.
if ls /lib/ld-musl-* >/dev/null 2>&1 || (ldd --version 2>&1 || :) | grep -qi musl; then
dp_libc=musl
fi
;;
esac
# The ARM32 and LoongArch release artifacts are static musl binaries.
# Select them on any Linux libc: this is both more portable for embedded
# systems (where uClibc remains common) and avoids imposing the cross
# image's glibc floor. ARM32 targets the standard hard-float ABI.
case $dp_arch in
arm | armv7) SELF="$dp_arch-unknown-linux-musleabihf" ;;
loongarch64) SELF="$dp_arch-unknown-linux-musl" ;;
*) SELF="$dp_arch-unknown-linux-$dp_libc" ;;
esac
dp_desc="Linux"
[ "$dp_operating" = Android ] && dp_desc=Android
if [ -r /etc/os-release ]; then
# shellcheck disable=SC1091
dp_pretty=$(. /etc/os-release 2>/dev/null && printf '%s' "${PRETTY_NAME:-}")
[ -n "$dp_pretty" ] && dp_desc=$dp_pretty
fi
;;
Darwin)
# Under Rosetta `uname -m` says x86_64. Install the native binary.
if [ "$(sysctl -n hw.optional.arm64 2>/dev/null || echo 0)" = 1 ]; then
dp_arch=aarch64
fi
SELF="$dp_arch-apple-darwin"
dp_desc="macOS $(sw_vers -productVersion 2>/dev/null || :)"
;;
FreeBSD) SELF="$dp_arch-unknown-freebsd" dp_desc="FreeBSD $(uname -r 2>/dev/null || :)" ;;
OpenBSD) SELF="$dp_arch-unknown-openbsd" dp_desc="OpenBSD $(uname -r 2>/dev/null || :)" ;;
NetBSD) SELF="$dp_arch-unknown-netbsd" dp_desc="NetBSD $(uname -r 2>/dev/null || :)" ;;
DragonFly) SELF="$dp_arch-unknown-dragonfly" dp_desc="DragonFly $(uname -r 2>/dev/null || :)" ;;
Haiku) SELF="$dp_arch-unknown-haiku" dp_desc="Haiku $(uname -r 2>/dev/null || :)" ;;
GNU) SELF="$dp_arch-unknown-hurd-gnu" dp_desc="GNU/Hurd $(uname -r 2>/dev/null || :)" ;;
SunOS)
dp_uname_o=$(uname -o 2>/dev/null || :)
case "$dp_uname_o $(uname -v 2>/dev/null || :)" in
*[Ii]llumos* | *[Oo]mni[Oo][Ss]*) SELF="$dp_arch-unknown-illumos" ;;
*) SELF="$dp_arch-pc-solaris" ;;
esac
dp_desc="$dp_uname_o $(uname -r 2>/dev/null || :)"
;;
CYGWIN* | MINGW* | MSYS* | Windows_NT)
die "on Windows, use install.ps1 instead:
irm https://install.atomdrift.org/scan.ps1 | iex"
;;
*)
SELF="$dp_arch-unknown-$(printf '%s' "$HOST_OS" | tr '[:upper:]' '[:lower:]')"
dp_desc="$HOST_OS $(uname -r 2>/dev/null || :)"
;;
esac
# TARGET is SELF only when a release actually carries it; otherwise the
# source path takes over.
TARGET=""
for dp_t in $TARGETS; do
[ "$dp_t" = "$SELF" ] && TARGET=$SELF
done
# Keep the compiler target internal. Human output should identify the OS
# once, with the architecture as the useful secondary detail.
PLATFORM="${dp_desc% } ${C_DIM}· $dp_arch${C_RESET}"
return 0
}
# ---------------------------------------------------------------------------
# HTTP
#
# curl, wget, fetch, and ftp in that order: OpenBSD and NetBSD ship the last two
# in the base system and nothing else, and both are targets we publish.
# ---------------------------------------------------------------------------
find_downloader() {
for fd_c in curl wget fetch ftp; do
if command -v "$fd_c" >/dev/null 2>&1; then
DOWNLOADER=$fd_c
break
fi
done
[ -n "$DOWNLOADER" ] || die "no HTTP client found — install curl or wget and re-run"
# busybox wget accepts neither --spider nor -S. Ask once instead of
# discovering it mid-install.
if [ "$DOWNLOADER" = wget ] && wget --help 2>&1 | grep -q -- --spider; then
WGET_MODERN=1
fi
return 0
}
# http_get URL OUTFILE — writes the body; non-zero on any HTTP or transport error.
http_get() {
case $DOWNLOADER in
curl) curl -fsSL --proto '=https' --tlsv1.2 --retry 3 --retry-delay 1 -o "$2" "$1" ;;
wget) wget -q -O "$2" "$1" ;;
fetch) fetch -q -o "$2" "$1" ;;
ftp) ftp -o "$2" "$1" ;;
esac
}
# http_ok URL — false only when we can cheaply prove the URL is not there.
#
# Deliberately narrow: only a 404 counts as absent. A HEAD refused for any other
# reason must not be read as "this platform has no binary", or a working release
# would quietly become a twenty-minute source build.
http_ok() {
case $DOWNLOADER in
curl)
ho_code=$(curl -sSLI --proto '=https' --tlsv1.2 -o /dev/null \
-w '%{http_code}' "$1" 2>/dev/null || printf 000)
[ "$ho_code" != 404 ]
;;
wget)
[ "$WGET_MODERN" = 1 ] || return 0
! wget --spider -S "$1" 2>&1 | grep -q ' 404 '
;;
*) : ;;
esac
}
# resolve_latest — the newest release tag.
#
# Read from the redirect that /releases/latest performs, because the REST API is
# rate-limited per IP and CI runners share addresses. The API is the fallback
# for clients that cannot report a redirect target.
resolve_latest() {
rl_url="https://github.com/$REPO/releases/latest"
rl_final=""
case $DOWNLOADER in
curl) rl_final=$(curl -fsSLI --proto '=https' -o /dev/null -w '%{url_effective}' "$rl_url" 2>/dev/null || :) ;;
wget)
if [ "$WGET_MODERN" = 1 ]; then
rl_final=$(wget -q --spider -S "$rl_url" 2>&1 | awk '/^ *Location:/ { print $2 }' | tail -n 1 || :)
fi
;;
esac
case $rl_final in
*/releases/tag/*)
rl_tag=${rl_final##*/tag/}
valid_version "$rl_tag" || return 1
printf '%s' "$rl_tag"
return 0
;;
esac
http_get "https://api.github.com/repos/$REPO/releases/latest" "$TMP/latest.json" 2>/dev/null || return 1
rl_tag=$(sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$TMP/latest.json" | head -n 1)
valid_version "$rl_tag" || return 1
printf '%s' "$rl_tag"
}
file_size() {
# `ls -ln` is the portable stat: GNU wants -c%s, BSD wants -f%z.
# shellcheck disable=SC2012 # the path is ours, in a mode 700 temp dir
fs_n=$(ls -ln "$1" 2>/dev/null | awk 'NR == 1 { print $5 }')
case $fs_n in
'' | *[!0-9]*) fs_n=0 ;;
esac
printf '%s' "$fs_n"
}
human_size() {
awk -v b="$1" 'BEGIN {
if (b >= 1048576) printf "%.1f MB", b / 1048576
else if (b >= 1024) printf "%.0f KB", b / 1024
else printf "%d B", b
}'
}
# download URL OUTFILE LABEL
download() {
dl_url=$1 dl_out=$2 dl_label=$3
http_get "$dl_url" "$dl_out" || return 1
dl_got=$(file_size "$dl_out")
step download "$dl_label $(human_size "$dl_got")"
}
# ---------------------------------------------------------------------------
# Integrity
# ---------------------------------------------------------------------------
# sha256_of FILE — lowercase hex digest, empty when nothing here can produce one.
sha256_of() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{ print $1 }'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{ print $1 }'
elif command -v sha256 >/dev/null 2>&1; then
sha256 -q "$1"
elif command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 "$1" | awk '{ print $NF }'
elif command -v digest >/dev/null 2>&1; then
digest -a sha256 "$1"
elif cksum -a sha256 "$1" >/dev/null 2>&1; then
cksum -a sha256 "$1" | awk '{ print $NF }'
fi
}
# verify_checksum TARBALL SUMSFILE NAME — prints a short digest, non-zero on
# any doubt whatsoever. The caller treats failure as fatal.
#
# The digest travels over the same TLS connection as the archive, so this is a
# corruption and truncation check; verify_provenance below is the trust anchor.
verify_checksum() {
vc_want=$(awk -v want="$3" '
{ n = $2; sub(/^\.\//, "", n); sub(/^\*/, "", n)
if (n == want) { print tolower($1); exit } }' "$2")
if [ -z "$vc_want" ]; then
printf '%s is not listed in SHA256SUMS\n' "$3" >&2
return 1
fi
vc_got=$(sha256_of "$1" | tr '[:upper:]' '[:lower:]')
if [ -z "$vc_got" ]; then
printf 'no sha256 tool found (sha256sum, shasum, openssl)\n' >&2
return 1
fi
if [ "$vc_got" != "$vc_want" ]; then
printf 'expected %s\n got %s\n' "$vc_want" "$vc_got" >&2
return 1
fi
printf '%s' "$vc_got" | cut -c1-12
}
# Signed build provenance, when the GitHub CLI is here to check it. Its absence
# is not an error: this is a stronger check than we can otherwise make, not a
# required one.
verify_provenance() {
command -v gh >/dev/null 2>&1 || return 1
gh attestation verify "$1" --repo "$REPO" >/dev/null 2>&1
}
# Verify the signed release manifest with a standard Sigstore bundle. Tag-push
# releases carry the tag identity; a recovery publish is deliberately limited
# to main by release.yml and carries that identity instead.
verify_sigstore_manifest() {
command -v cosign >/dev/null 2>&1 || return 1
vsm_manifest=$1 vsm_bundle=$2 vsm_version=$3
vsm_tag="https://github.com/$REPO/.github/workflows/release.yml@refs/tags/v$vsm_version"
vsm_main="https://github.com/$REPO/.github/workflows/release.yml@refs/heads/main"
for vsm_identity in "$vsm_tag" "$vsm_main"; do
if cosign verify-blob "$vsm_manifest" \
--bundle "$vsm_bundle" \
--certificate-identity "$vsm_identity" \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
>/dev/null 2>&1; then
printf '%s\n' "$vsm_identity"
return 0
fi
done
return 1
}
# ---------------------------------------------------------------------------
# Install locations
# ---------------------------------------------------------------------------
on_path() {
case ":$PATH:" in
*":$1:"*) return 0 ;;
*) return 1 ;;
esac
}
writable_dir() {
[ -d "$1" ] && [ -w "$1" ]
}
# Prefer the native least-surprising privilege tool.
find_escalator() {
INSTALL_ESCALATOR=""
[ "$(id -u)" = 0 ] && return 1
for fe_cmd in doas pfexec sudo; do
if command -v "$fe_cmd" >/dev/null 2>&1; then
INSTALL_ESCALATOR=$fe_cmd
return 0
fi
done
return 1
}
# install_run COMMAND... — run one step of a write into INSTALL_DIR, escalated
# when that directory needs it. install_binary_file and the exit trap both write
# there, and this is the single place that decides how.
install_run() {
if [ "$INSTALL_PRIVILEGED" = 1 ] && [ -n "$INSTALL_ESCALATOR" ]; then
"$INSTALL_ESCALATOR" "$@"
else
"$@"
fi
}
quote_arg() {
qa_escaped=$(printf '%s' "$1" | sed "s/'/'\\\\''/g")
printf "'%s'" "$qa_escaped"
}
# Ask through the controlling terminal because stdin contains scan.sh when the
# documented `curl | sh` form is used. Approval is remembered for this run;
# every later privileged operation is still printed before it executes.
approve_privilege() {
ap_command=$1
if [ "$PRIVILEGE_APPROVED" = 1 ]; then
step privilege "$ap_command"
return 0
fi
[ "$PRIVILEGE_DENIED" = 0 ] || return 1
if ! (: </dev/tty) 2>/dev/null; then
warn "privilege is required, but no interactive terminal is available"
note "command not run: $ap_command"
PRIVILEGE_DENIED=1
return 1
fi
printf ' %s%s%s %sPermission needed%s\n %s\n Allow %s for this install? [y/N] ' \
"$C_AMBER" "$G_WARN" "$C_RESET" "$C_BOLD" "$C_RESET" "$ap_command" "$INSTALL_ESCALATOR" >/dev/tty
IFS= read -r ap_answer </dev/tty || ap_answer=""
case $ap_answer in
[yY] | [yY][eE][sS])
PRIVILEGE_APPROVED=1
gap
return 0
;;
*)
PRIVILEGE_DENIED=1
warn "permission declined — no privileged changes were made"
return 1
;;
esac
}
# use_install_dir DIR [ALLOW_PRIVILEGE] — use DIR directly when possible,
# otherwise optionally request permission for the preferred privilege tool.
use_install_dir() {
uid_dir=$1 uid_privilege=${2:-1}
if mkdir -p "$uid_dir" 2>/dev/null && writable_dir "$uid_dir"; then
INSTALL_DIR=$uid_dir
INSTALL_PRIVILEGED=0
INSTALL_ESCALATOR=""
return 0
fi
[ "$uid_privilege" = 1 ] || return 1
[ "$(id -u)" = 0 ] && return 1
find_escalator || return 1
uid_cmd=$INSTALL_ESCALATOR
# Selecting an existing protected directory is read-only. Defer the prompt
# until install_binary_file has a file to write; an already-current rerun
# will then finish without asking for unused privilege.
if [ -d "$uid_dir" ]; then
INSTALL_DIR=$uid_dir
INSTALL_PRIVILEGED=1
return 0
fi
uid_display="$uid_cmd mkdir -p $(quote_arg "$uid_dir")"
[ "$PRIVILEGE_APPROVED" = 1 ] || gap
approve_privilege "$uid_display" || return 1
if "$uid_cmd" mkdir -p "$uid_dir" && [ -d "$uid_dir" ]; then
INSTALL_DIR=$uid_dir
INSTALL_PRIVILEGED=1
return 0
fi
warn "$uid_cmd could not prepare $uid_dir"
return 1
}
# A package manager owns its prefix. Directly dropping a binary there would
# bypass its database, so leave those entries to the package-manager method.
managed_path_dir() {
mpd_dir=$1
if [ -n "$BREW_PREFIX" ]; then
case $mpd_dir in
"$BREW_PREFIX" | "$BREW_PREFIX"/*) return 0 ;;
esac
fi
case $mpd_dir in
/nix/store/* | /snap/* | /var/lib/flatpak/*) return 0 ;;
esac
return 1
}
# native_path_dirs OS — ordered, conventional destinations for manual binaries.
native_path_dirs() {
case $1 in
Haiku) printf '%s\n' '/boot/system/non-packaged/bin /usr/local/bin' ;;
SunOS) printf '%s\n' '/opt/atomdrift/bin' ;;
Darwin) printf '%s\n' '/usr/local/bin /opt/local/bin' ;;
NetBSD) printf '%s\n' '/usr/pkg/bin /usr/local/bin' ;;
*) printf '%s\n' '/usr/local/bin' ;;
esac
return 0
}
# Try OS-conventional local directories before following raw PATH order. Every
# REQUIRE_PATH defaults to 1. Non-Linux systems can deliberately select their
# native application prefix even when the user still needs PATH advice.
use_native_path_dir() {
unpd_privilege=${1:-1}
unpd_require_path=${2:-1}
unpd_dirs=$(native_path_dirs "$HOST_OS")
for unpd_dir in $unpd_dirs; do
if [ "$unpd_require_path" = 1 ]; then on_path "$unpd_dir" || continue; fi
managed_path_dir "$unpd_dir" && continue
if use_install_dir "$unpd_dir" "$unpd_privilege"; then
return 0
fi
done
return 1
}
# Follow PATH order without word-splitting directory names. Native local
# directories were already attempted above; core directories such as /usr/bin
# are reached only here.
#
# The trailing colon means every entry is followed by one, so each pass consumes
# exactly one and the loop ends when nothing is left. Consuming before any test
# is what keeps `continue` safe to write here.
use_any_path_dir() {
uap_privilege=${1:-1}
uap_rest="$PATH:"
while [ -n "$uap_rest" ]; do
uap_dir=${uap_rest%%:*}
uap_rest=${uap_rest#*:}
# Relative and empty entries are unsafe escalation targets.
case $uap_dir in /*) ;; *) continue ;; esac
managed_path_dir "$uap_dir" && continue
use_install_dir "$uap_dir" "$uap_privilege" && return 0
done
return 1
}
# Path of the atomscan already on PATH, if any.
current_install() {
command -v "$BIN" 2>/dev/null
}
# installed_version PATH — version string of a binary already on disk.
installed_version() {
[ -x "$1" ] || return 1
iv_out=$("$1" --version 2>/dev/null) || return 1
printf '%s' "$iv_out" | awk '{ print $2; exit }'
}
# Recognize an exact current install without requiring its directory to be
# writable. An idempotent rerun should not need Rust or privilege merely to do
# nothing.
use_current_if_exact() {
[ "$OPT_FORCE" = 0 ] || return 1
[ -n "$VERSION" ] || return 1
ucie_path="" ucie_from_path=0
if [ -n "$OPT_DIR" ]; then
ucie_path="$OPT_DIR/$BIN"
elif [ -n "$INSTALL_DIR" ]; then
ucie_path="$INSTALL_DIR/$BIN"
else
ucie_path=$(current_install || :)
ucie_from_path=1
fi
[ -n "$ucie_path" ] || return 1
[ "$(installed_version "$ucie_path" || :)" = "$VERSION" ] || return 1
ucie_dir=$(dirname "$ucie_path")
if [ "$ucie_from_path" = 1 ]; then
if [ -n "$BREW_PREFIX" ] && [ "${ucie_dir#"$BREW_PREFIX"}" != "$ucie_dir" ]; then
return 1
fi
if [ "$HOST_OS" != Linux ]; then
case $ucie_dir in /bin | /sbin | /usr/bin | /usr/sbin) return 1 ;; esac
fi
fi
INSTALL_DIR=$ucie_dir
INSTALLED=$ucie_path
ALREADY_CURRENT=1
return 0
}
resolve_install_dir() {
INSTALL_PRIVILEGED=0
INSTALL_ESCALATOR=""
if [ -n "$OPT_DIR" ]; then
if use_install_dir "$OPT_DIR"; then
return 0
fi
die "$OPT_DIR is not writable and no privilege tool could prepare it
Prefer --dir \$HOME/.local/bin, or install doas, pfexec, or sudo for a system path."
fi
# Upgrading in place beats installing a second copy that shadows the first.
# Anything Homebrew owns is left to Homebrew.
rid_cur=$(current_install || :)
if [ -n "$rid_cur" ]; then
rid_dir=$(dirname "$rid_cur")
rid_brewed=0
rid_system_core=0
if [ -n "$BREW_PREFIX" ] && [ "${rid_dir#"$BREW_PREFIX"}" != "$rid_dir" ]; then
rid_brewed=1
fi
# Linux distributions may legitimately own /usr/bin installs. On the
# other Unix families, migrate toward the native local/application prefix
# instead of perpetuating an old system-directory choice.
if [ "$HOST_OS" != Linux ]; then
case $rid_dir in /bin | /sbin | /usr/bin | /usr/sbin) rid_system_core=1 ;; esac
fi
if [ "$rid_brewed" = 0 ] && [ "$rid_system_core" = 0 ]; then
if use_install_dir "$rid_dir"; then
return 0
fi
die "the existing $rid_cur cannot be upgraded in place
Re-run with --dir to choose a different destination, or install doas, pfexec, or sudo."
fi
fi
# A PATH entry wins even when its directory has not been created yet. This
# keeps a first install where the user already said they want binaries,
# instead of escalating to a system directory just because ~/.local/bin
# happens not to exist yet.
if [ -n "${HOME:-}" ]; then
if [ "$HOST_OS" = Haiku ]; then
rid_haiku="$HOME/config/non-packaged/bin"
if on_path "$rid_haiku" && mkdir -p "$rid_haiku" 2>/dev/null && writable_dir "$rid_haiku"; then
INSTALL_DIR=$rid_haiku
return 0
fi
fi
for rid_d in "$HOME/.local/bin" "$HOME/bin" "$HOME/.cargo/bin"; do
on_path "$rid_d" || continue
if [ ! -d "$rid_d" ]; then
mkdir -p "$rid_d" 2>/dev/null || continue
fi
if writable_dir "$rid_d"; then
INSTALL_DIR=$rid_d
return 0
fi
done
fi
# Linux honors PATH throughout. Elsewhere, prefer the native application
# prefix even when it needs PATH advice; never fall through to /usr/bin.
if [ "$HOST_OS" = Linux ]; then
use_native_path_dir 0 1 && return 0
use_any_path_dir 0 && return 0
use_native_path_dir 1 1 && return 0
use_any_path_dir 1 && return 0
else
use_native_path_dir 0 0 && return 0
use_any_path_dir 0 && return 0
use_native_path_dir 1 0 && return 0
fi
die "no usable absolute directory was found on PATH
Add \$HOME/.local/bin or \$HOME/bin to PATH, or choose one with --dir."
}
# ibf_fail MESSAGE — die without leaving a staging file behind. Reads ibf_tmp,
# which install_binary_file sets: shell has no locals, so the shared ibf_ prefix
# is the convention marking who owns the variable.
ibf_fail() {
install_run rm -f "$ibf_tmp" 2>/dev/null || :
die "$1"
}
# ibf_approve COMMAND [LABEL] — clear a privileged command with the user, or
# die. Approval given earlier in this run is not asked for again, but every
# privileged command is still printed before it runs. LABEL stands in for the
# progress line when COMMAND is a chain too long to echo in full.
ibf_approve() {
if [ "$PRIVILEGE_APPROVED" = 1 ]; then
step privilege "${2:-$1}"
return 0
fi
gap
approve_privilege "$1" || die "permission is required to install $ibf_dest"
}
# install_binary_file SRC — install SRC at INSTALL_DIR/BIN with mode 0755.
#
# Whichever route is taken, an interrupted install leaves either the old binary
# or the new one in place, never a partial file.
install_binary_file() {
ibf_dest="$INSTALL_DIR/$BIN"
ibf_tmp="$INSTALL_DIR/.$BIN.new.$$"
# Names the escalator in a diagnostic, and is empty when there is not one.
ibf_via=""
if [ "$INSTALL_PRIVILEGED" = 1 ]; then
ibf_via=" with $INSTALL_ESCALATOR"
# BSD and macOS install(1) do the same-directory copy and atomic rename
# themselves; only the flag that asks for it differs. GNU install has no
# such mode, but one direct operation still beats staging by hand, so
# Linux uses it flagless. BusyBox and toybox systems, which may have no
# install applet at all, fall through to the staging path below.
ibf_flag=""
case $HOST_OS in
Darwin | FreeBSD | OpenBSD) ibf_flag=-S ;;
NetBSD) ibf_flag=-r ;;
esac
if { [ -n "$ibf_flag" ] || [ "$HOST_OS" = Linux ] || [ "$HOST_OS" = GNU ]; } &&
command -v install >/dev/null 2>&1; then
ibf_approve "$INSTALL_ESCALATOR install${ibf_flag:+ $ibf_flag} -m 755 $(quote_arg "$1") $(quote_arg "$ibf_dest")"
install_run install ${ibf_flag:+"$ibf_flag"} -m 755 "$1" "$ibf_dest" ||
die "cannot install $ibf_dest$ibf_via"
INSTALLED=$ibf_dest
CHANGED=1
return 0
fi
ibf_approve \
"$INSTALL_ESCALATOR cp $(quote_arg "$1") $(quote_arg "$ibf_tmp") && $INSTALL_ESCALATOR chmod 755 $(quote_arg "$ibf_tmp") && $INSTALL_ESCALATOR mv -f $(quote_arg "$ibf_tmp") $(quote_arg "$ibf_dest")" \
"$INSTALL_ESCALATOR ${C_DIM}(atomic binary install)${C_RESET}"
fi
# Stage inside the destination directory so the rename that follows stays on
# one filesystem, where it is atomic.
INSTALL_TMP=$ibf_tmp
install_run cp "$1" "$ibf_tmp" || ibf_fail "cannot write to $INSTALL_DIR$ibf_via"
install_run chmod 755 "$ibf_tmp" || ibf_fail "cannot make $ibf_tmp executable"
install_run mv -f "$ibf_tmp" "$ibf_dest" || ibf_fail "cannot replace $ibf_dest$ibf_via"
INSTALL_TMP=""
INSTALLED=$ibf_dest
CHANGED=1
}
# ---------------------------------------------------------------------------
# Method: Homebrew
#
# On macOS and Linux it owns upgrades, PATH, and the optional analysis tools.
# The formula builds from source, which is slow, so say so and leave
# `--method binary` one flag away. An empty FORMULA means the tap does not
# carry this tool yet, and every route below declines rather than guessing.
# ---------------------------------------------------------------------------
# Homebrew installs to a fixed prefix and leaves exporting it to a profile
# snippet, which a non-login `curl | sh` never reads. Look in the three prefixes
# it uses before concluding there is no Homebrew: on an image-based system like
# Bluefin, an unseen Homebrew would demote us to rpm-ostree, which cannot
# install anything without a reboot. The prefix is appended rather than
# prepended — brew itself is all we are missing, and every other tool this run
# already resolved should keep the priority it had.
brew_works() {
if ! command -v brew >/dev/null 2>&1; then
for bw_dir in /opt/homebrew/bin /home/linuxbrew/.linuxbrew/bin "${HOME:-}/.linuxbrew/bin"; do
[ -x "$bw_dir/brew" ] || continue
PATH="$PATH:$bw_dir"
export PATH
break
done
fi
command -v brew >/dev/null 2>&1 && brew --version >/dev/null 2>&1
}
# Resolve Homebrew's root into BREW_PREFIX, empty when there is no Homebrew.
#
# The order here is the whole point, which is why it is a function and not two
# lines in main: brew_works is what puts an unexported Homebrew back on PATH, so
# reading the prefix first would report "no Homebrew" on exactly the machines
# that repair exists for. An empty BREW_PREFIX then stops managed_path_dir from
# recognising Homebrew's directories — the very directories the repair has just
# added to PATH, and so to the list of places we might install.
find_brew_prefix() {
BREW_PREFIX=""
brew_works || return 1