Repository navigation
Expand file tree
/
Copy pathappscript.sh
More file actions
executable file
·446 lines (365 loc) · 12.6 KB
/
Copy pathappscript.sh
File metadata and controls
executable file
·446 lines (365 loc) · 12.6 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
#!/bin/sh
#
# Copyright (c) 2026, Jesús Daniel Colmenares Oviedo <dtxdf@disroot.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set -o pipefail
# AppScript version.
VERSION="%%VERSION%%"
# see sysexits(3)
EX_OK=0
EX_USAGE=64
EX_DATAERR=65
EX_NOINPUT=66
# Signals
IGNORED_SIGNALS="SIGALRM SIGVTALRM SIGPROF SIGUSR1 SIGUSR2"
HANDLER_SIGNALS="SIGHUP SIGINT SIGQUIT SIGTERM SIGXCPU SIGXFSZ"
BUILDDIR=
PREFIX="%%PREFIX%%"
SHAREDIR="${PREFIX}/share/appscript"
set -o pipefail
main()
{
local _o
local opt_display_checksum=false
local opt_dereference=false arg_dereference=
local mcmodel="small"
local opt_static=false
local checksum_algo="sha256"
local machine_arch=
local compress_algo="zstd"
local vendorid=
local sign_key=
local target=
local filename="a.AppScript"
local sysroot=
while getopts ":CLMsvA:a:c:I:i:o:S:" _o; do
case "${_o}" in
C)
opt_display_checksum=true
;;
L)
opt_dereference=true
;;
M)
if [ "${mcmodel}" = "small" ]; then
mcmodel="medium"
elif [ "${mcmodel}" = "medium" ]; then
mcmodel="large"
fi
;;
s)
opt_static=true
;;
v)
version
exit ${EX_OK}
;;
A)
checksum_algo="${OPTARG}"
;;
a)
machine_arch="${OPTARG}"
;;
c)
compress_algo="${OPTARG}"
;;
I)
vendorid="${OPTARG}"
;;
i)
sign_key="${OPTARG}"
;;
o)
filename="${OPTARG}"
;;
S)
sysroot="${OPTARG}"
;;
*)
usage
exit ${EX_USAGE}
;;
esac
done
shift $((OPTIND-1))
local directory="$1"
if [ -z "${directory}" ]; then
usage
exit ${EX_USAGE}
fi
case "${compress_algo}" in
gzip|xz|zstd) ;;
*) log_err "Unsupported compression algorithm: ${compress_algo}"; exit ${EX_DATAERR} ;;
esac
case "${checksum_algo}" in
sha256|blake3) ;;
*) log_err "Unsupported checksum algorithm: ${checksum_algo}"; exit ${EX_DATAERR} ;;
esac
local format=
machine_arch="${machine_arch:-`uname -p`}" || exit $?
case "${machine_arch}" in
amd64) format="elf64-x86-64" ;;
aarch64) format="elf64-littleaarch64" ;;
armv7) format="elf32-littlearm" ;;
i386) format="elf32-i386" ;;
riscv64) format="elf64-littleriscv" ;;
powerpc) format="elf32-powerpc" ;;
powerpc64) format="elf64-powerpc" ;;
powerpc64le) format="elf64-powerpcle" ;;
*) log_err "Unsupported arch: ${machine_arch}" ;;
esac
if [ -z "${sysroot}" ]; then
sysroot="${PREFIX}/freebsd-sysroot/${machine_arch}"
# If 'uname -p' is the same as '${machine_arch}', we should use
# '/' which is especially more important when '${opt_static}'
# is 'true'.
if [ ! -d "${sysroot}" ] || [ "${machine_arch}" = "`uname -p`" ]; then
sysroot="/"
fi
fi
atexit_init
BUILDDIR=`mktemp -d -t appscript` || exit $?
if ${opt_dereference}; then
arg_dereference="-L"
fi
local tar_options=
if [ "${compress_algo}" = "zstd" ]; then
tar_options="--options zstd:threads=0"
elif [ "${compress_algo}" = "xz" ]; then
tar_options="--options xz:threads=0"
fi
tar ${arg_dereference} -c ${tar_options} --${compress_algo} \
-C "${directory}" -f "${BUILDDIR}/payload" . || exit $?
local payload_checksum
if [ "${checksum_algo}" = "sha256" ]; then
payload_checksum=`sha256 -q -- "${BUILDDIR}/payload"` || exit $?
elif [ "${checksum_algo}" = "blake3" ]; then
payload_checksum=`b3sum --no-names -- "${BUILDDIR}/payload"` || exit $?
fi
(
cd -- "${BUILDDIR}" &&
objcopy \
--input-target binary \
--output-target "${format}" \
--rename-section .data=.rodata,alloc,load,readonly,contents \
payload payload.o &&
rm -f payload || exit $?
) || exit $?
local static_args=
if ${opt_static}; then
static_args="-static -lbz2 -lz -lprivatezstd -llzma -lmd -lcrypto -lbsdxml -lpthread"
fi
local out="${filename}"
if [ -n "${sign_key}" ] || [ -n "${vendorid}" ]; then
out="${BUILDDIR}/appscript"
fi
clang -O3 -s -pipe -mcmodel="${mcmodel}" --sysroot="${sysroot}" \
-fno-asynchronous-unwind-tables \
-DPAYLOAD_CHECKSUM="\"${payload_checksum}\"" \
-target "${machine_arch}-unknown-freebsd" "${BUILDDIR}/payload.o" \
"${SHAREDIR}/stub.c" -o "${out}" -larchive ${static_args} || exit $?
if [ -n "${vendorid}" ]; then
printf "%s" "${vendorid}" > "${BUILDDIR}/vendorid" || exit $?
objcopy --add-section .vendorid="${BUILDDIR}/vendorid" \
--set-section-flags .vendorid=noload,readonly \
"${out}" "${BUILDDIR}/appscript.vendor" || exit $?
mv -- "${BUILDDIR}/appscript.vendor" "${out}" || exit $?
fi
if [ -n "${sign_key}" ]; then
local checksum
if [ "${checksum_algo}" = "sha256" ]; then
checksum=`sha256 -q -- "${out}"` || exit $?
elif [ "${checksum_algo}" = "blake3" ]; then
checksum=`b3sum --no-names -- "${out}"` || exit $?
fi
if ${opt_display_checksum}; then
printf "%s\n" "${checksum}"
fi
printf "%s" "${checksum}" > "${BUILDDIR}/checksum" || exit $?
signify -S -c "verify with appscript-verify" -s "${sign_key}" -m "${BUILDDIR}/checksum" \
-x "${BUILDDIR}/appscript.sig" || exit $?
echo >> "${out}" || exit $?
echo -n "${checksum_algo}|" >> "${out}" || exit $?
cat -- "${BUILDDIR}/appscript.sig" >> "${out}" || exit $?
mv -f -- "${out}" "${filename}" || exit $?
fi
exit ${EX_OK}
}
atexit_init()
{
trap '' ${IGNORED_SIGNALS}
trap "ERRLEVEL=\$?; cleanup; exit \${ERRLEVEL}" EXIT
trap "cleanup; exit 70" ${HANDLER_SIGNALS}
}
log_err()
{
echo "===> $*" >&2
}
cleanup()
{
trap '' ${HANDLER_SIGNALS} EXIT
if [ -n "${BUILDDIR}" ]; then
rm -rf -- "${BUILDDIR}" > /dev/null 2>&1
fi
trap - ${IGNORED_SIGNALS} ${HANDLER_SIGNALS} EXIT
}
version()
{
echo "${VERSION}"
}
usage()
{
cat << EOF
usage: appscript -v
appscript [-CLMs] [-A <algo>] [-a <arch>] [-c <algo>] [-I <vendorid>]
[-i <sign-key>] [-o <filename>] [-S <sysroot>] <directory>
EOF
}
main_verify()
{
local _o
local opt_print_vendorid=false
local checksum=
local public_key=
while getopts ":PC:p:" _o; do
case "${_o}" in
P)
opt_print_vendorid=true
;;
C)
checksum="${OPTARG}"
;;
p)
public_key="${OPTARG}"
;;
*)
usage_verify
exit ${EX_USAGE}
;;
esac
done
shift $((OPTIND-1))
if [ -n "${public_key}" ] && ${opt_print_vendorid}; then
usage_verify
exit ${EX_USAGE}
fi
local filename="$1"
if [ -z "${filename}" ]; then
usage_verify
exit ${EX_USAGE}
fi
if [ ! -f "${filename}" ]; then
log_err "${filename}: file not found or no read permission."
exit ${EX_NOINPUT}
fi
if ${opt_print_vendorid}; then
atexit_init
local section_info
section_info=`readelf -W -S "${filename}" 2>/dev/null | awk '$2 == ".vendorid" {print toupper($5), toupper($6)}'`
if [ -z "${section_info}" ]; then
log_err "No vendor ID section found."
exit 1
fi
local hex_offset hex_size
hex_offset=`echo "${section_info}" | awk '{print $1}'`
hex_size=`echo "${section_info}" | awk '{print $2}'`
local offset size
offset=`echo "ibase=16; ${hex_offset}" | bc`
size=`echo "ibase=16; ${hex_size}" | bc`
BUILDDIR=`mktemp -d -t appscript` || exit $?
if ! dd if="${filename}" bs=1 skip="${offset}" count="${size}" 2>/dev/null > "${BUILDDIR}/vendorid"; then
log_err "Failed to extract vendor ID section."
exit 1
fi
local vendorid
vendorid=`head -1 -- "${BUILDDIR}/vendorid"` || exit $?
printf "%s\n" "${vendorid}"
else
if [ -z "${public_key}" ]; then
usage_verify
exit ${EX_USAGE}
fi
atexit_init
BUILDDIR=`mktemp -d -t appscript` || exit $?
local checksum_algo
tail -c 256 -- "${filename}" |\
grep -a -A1 -Ee '^(sha256|blake3)|untrusted comment:' > "${BUILDDIR}/appscript.tail"
if [ $? -ne 0 ]; then
tail -c 256 -- "${filename}" |\
grep -a -A1 -Ee '^untrusted comment:' > "${BUILDDIR}/appscript.tail"
if [ $? -ne 0 ]; then
log_err "No signature was found."
exit 1
fi
cp -- "${BUILDDIR}/appscript.tail" "${BUILDDIR}/appscript.sig" || exit $?
# Backward compatibility.
checksum_algo="sha256"
else
cut -s -d"|" -f1 -- "${BUILDDIR}/appscript.tail" > "${BUILDDIR}/appscript.checksum_algo" || exit $?
sed -Ee 's/^(sha256|blake3)\|//' -- "${BUILDDIR}/appscript.tail" > "${BUILDDIR}/appscript.sig" || exit $?
checksum_algo=`head -1 -- "${BUILDDIR}/appscript.checksum_algo"` || exit $?
fi
case "${checksum_algo}" in
sha256|blake3) ;;
*) checksum_algo="sha256" ;;
esac
if [ "${checksum_algo}" = "blake3" ] && ! which -s b3sum; then
log_err "sysutils/b3sum is required to be installed to verify this AppScript."
exit 1
fi
if [ -z "${checksum}" ]; then
local sig_size
sig_size=`stat -f %z -- "${BUILDDIR}/appscript.tail"` || exit $?
local total_size
total_size=`stat -f %z -- "${filename}"` || exit $?
local orig_size
orig_size=$(( total_size - sig_size - 1 ))
if [ "${checksum_algo}" = "sha256" ]; then
checksum=`head -c "${orig_size}" "${filename}" | sha256 -q` || exit $?
elif [ "${checksum_algo}" = "blake3" ]; then
checksum=`head -c "${orig_size}" "${filename}" | b3sum --no-names` || exit $?
fi
fi
printf "%s" "${checksum}" > "${BUILDDIR}/checksum" || exit $?
signify -V -p "${public_key}" -m "${BUILDDIR}/checksum" \
-x "${BUILDDIR}/appscript.sig" || exit $?
fi
exit ${EX_OK}
}
usage_verify()
{
cat << EOF
usage: appscript-verify -P <filename>
appscript-verify [-c <checksum>] -p <public-key> <filename>
EOF
}
self=`basename -- "$0"` || exit $?
case "${self}" in
appscript-verify) main_verify "$@" ;;
*) main "$@" ;;
esac