abuild-sign.in 2.59 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1
2
#!/bin/sh

3
# abuild-sign - sign indexes
Natanael Copa's avatar
Natanael Copa committed
4
5
# Copyright (c) 2009 Natanael Copa <ncopa@alpinelinux.org>
#
Leo's avatar
Leo committed
6
# Distributed under GPL-2.0-only
Natanael Copa's avatar
Natanael Copa committed
7
8
#

9
program_version=@VERSION@
10
sharedir=${ABUILD_SHAREDIR:-@sharedir@}
Natanael Copa's avatar
Natanael Copa committed
11

Natanael Copa's avatar
Natanael Copa committed
12
13
if ! [ -f "$sharedir/functions.sh" ]; then
	echo "$sharedir/functions.sh: not found" >&2
Natanael Copa's avatar
Natanael Copa committed
14
	exit 1
15
fi
Natanael Copa's avatar
Natanael Copa committed
16
. "$sharedir/functions.sh"
Natanael Copa's avatar
Natanael Copa committed
17

18
19
gzip=$(command -v pigz || echo gzip)

Dubiousjim's avatar
Dubiousjim committed
20
do_sign() {
Dubiousjim's avatar
Dubiousjim committed
21
	local f i keyname repo
22
	local openssl=$(command -v openssl || echo libressl)
Dubiousjim's avatar
Dubiousjim committed
23

Dubiousjim's avatar
Dubiousjim committed
24
25
26
27
28
29
30
	# we are actually only interested in the name, not the file itself
	keyname=${pubkey##*/}

	for f; do
		i=$(readlink -f $f)
		[ -d "$i" ] && i="$i/APKINDEX.tar.gz"
		repo="${i%/*}"
31
		trap 'die "failed to sign $i"' EXIT
32
33
		set -e
		cd "$repo"
Dubiousjim's avatar
Dubiousjim committed
34
		sig=".SIGN.RSA.$keyname"
35
		$openssl dgst -sha1 -sign "$privkey" -out "$sig" "$i"
kpcyrd's avatar
kpcyrd committed
36
37
38
39
40

		if [ -n "$SOURCE_DATE_EPOCH" ]; then
			touch -h -d "@$SOURCE_DATE_EPOCH" "$sig"
		fi

Dubiousjim's avatar
Dubiousjim committed
41
		tmptargz=$(mktemp)
42
		tar -f - -c "$sig" | abuild-tar --cut | $gzip -n -9 > "$tmptargz"
Dubiousjim's avatar
Dubiousjim committed
43
44
45
		tmpsigned=$(mktemp)
		cat "$tmptargz" "$i" > "$tmpsigned"
		rm -f "$tmptargz" "$sig"
Dubiousjim's avatar
Dubiousjim committed
46
		chmod 644 "$tmpsigned"
Dubiousjim's avatar
Dubiousjim committed
47
		mv "$tmpsigned" "$i"
48
		msg "Signed $i"
49
50
		set +e
		trap - EXIT
Dubiousjim's avatar
Dubiousjim committed
51
52
53
	done
}

Natanael Copa's avatar
Natanael Copa committed
54
usage() {
Jakub Jirutka's avatar
Jakub Jirutka committed
55
56
57
58
59
60
61
62
63
64
65
66
67
	cat >&2 <<-__EOF__
		$program $program_version - sign indexes
		Usage: $program [-k PRIVKEY] [-p PUBKEY] INDEXFILE...
		       $program -e
		Options:
		  -e, --installed    Check only of there exist a private key for signing
		  -k, --private KEY  The private key to use for signing
		  -p, --public KEY   The name of public key. apk add will look for
		                     /etc/apk/keys/KEY
		  -q, --quiet
		  -h, --help         Show this help

	__EOF__
Natanael Copa's avatar
Natanael Copa committed
68
69
}

70
check_installed=false
Natanael Copa's avatar
Natanael Copa committed
71
privkey="$PACKAGER_PRIVKEY"
72
73
pubkey=
quiet=
Natanael Copa's avatar
Natanael Copa committed
74

75
args=$(getopt -o ek:p:qh --long installed,private:,public:,quiet,help -n "$program" -- "$@")
76
77
78
79
80
81
82
if [ $? -ne 0 ]; then
	usage
	exit 2
fi
eval set -- "$args"
while true; do
	case $1 in
83
		-e|--installed) check_installed=true;;
84
85
86
87
88
89
		-k|--private) privkey=$2; shift;;
		-p|--public) pubkey=$2; shift;;
		-q|--quiet) quiet=1;; # suppresses msg
		-h|--help) usage; exit;;
		--) shift; break;;
		*) exit 1;; # getopt error
Natanael Copa's avatar
Natanael Copa committed
90
	esac
91
	shift
Natanael Copa's avatar
Natanael Copa committed
92
done
93
if [ $# -eq 0 ] && ! $check_installed; then
94
95
96
	usage
	exit 2
fi
Natanael Copa's avatar
Natanael Copa committed
97
98

if [ -z "$privkey" ]; then
Jakub Jirutka's avatar
Jakub Jirutka committed
99
100
101
102
103
104
105
	cat >&2 <<-__EOF__
		No private key found. Use 'abuild-keygen' to generate the keys.
		Then you can either:
		  * set the PACKAGER_PRIVKEY in $ABUILD_USERCONF
		    ('abuild-keygen -a' does this for you)
		  * set the PACKAGER_PRIVKEY in $ABUILD_CONF
		  * specify the key with the -k option to $program
106

Jakub Jirutka's avatar
Jakub Jirutka committed
107
	__EOF__
Natanael Copa's avatar
Natanael Copa committed
108
109
110
111
112
113
114
	exit 1
fi

if [ -z "$pubkey" ]; then
	pubkey=${PACKAGER_PUBKEY:-"${privkey}.pub"}
fi

115
116
117
if ! $check_installed; then
	do_sign "$@"
fi