abuild-sign.in 2.1 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 6 7 8
# Copyright (c) 2009 Natanael Copa <ncopa@alpinelinux.org>
#
# Distributed under GPL-2
#

9
program_version=@VERSION@
10
datadir=@datadir@
Natanael Copa's avatar
Natanael Copa committed
11

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

Dubiousjim's avatar
Dubiousjim committed
18
do_sign() {
Dubiousjim's avatar
Dubiousjim committed
19 20
	local f i keyname repo

Dubiousjim's avatar
Dubiousjim committed
21 22 23 24 25 26 27
	# 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%/*}"
28 29 30
		(
		set -e
		cd "$repo"
Dubiousjim's avatar
Dubiousjim committed
31
		sig=".SIGN.RSA.$keyname"
32
		openssl dgst -sha1 -sign "$privkey" -out "$sig" "$i"
Dubiousjim's avatar
Dubiousjim committed
33 34 35 36 37
		tmptargz=$(mktemp)
		tar -c "$sig" | abuild-tar --cut | gzip -9 > "$tmptargz"
		tmpsigned=$(mktemp)
		cat "$tmptargz" "$i" > "$tmpsigned"
		rm -f "$tmptargz" "$sig"
Dubiousjim's avatar
Dubiousjim committed
38
		chmod 644 "$tmpsigned"
Dubiousjim's avatar
Dubiousjim committed
39
		mv "$tmpsigned" "$i"
40 41
		msg "Signed $i"
		) || die "failed to sign $i"
Dubiousjim's avatar
Dubiousjim committed
42 43 44
	done
}

Natanael Copa's avatar
Natanael Copa committed
45
usage() {
46
	cat >&2 <<__EOF__
47
$program $program_version - sign indexes
48
Usage: $program [-k PRIVKEY] [-p PUBKEY] INDEXFILE...
49 50
Options:
  -k, --private KEY  The private key to use for signing
51 52
  -p, --public KEY   The name of public key. apk add will look for
                     /etc/apk/keys/KEY
53 54 55 56
  -q, --quiet
  -h, --help         Show this help

__EOF__
Natanael Copa's avatar
Natanael Copa committed
57 58 59
}

privkey="$PACKAGER_PRIVKEY"
60 61
pubkey=
quiet=
Natanael Copa's avatar
Natanael Copa committed
62

63
args=`getopt -o k:p:qh --long private:,public:,quiet,help -n "$program" -- "$@"`
64 65 66 67 68 69 70 71 72 73 74 75 76
if [ $? -ne 0 ]; then
	usage
	exit 2
fi
eval set -- "$args"
while true; do
	case $1 in
		-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
77
	esac
78
	shift
Natanael Copa's avatar
Natanael Copa committed
79
done
80 81 82 83
if [ $# -eq 0 ]; then
	usage
	exit 2
fi
Natanael Copa's avatar
Natanael Copa committed
84 85

if [ -z "$privkey" ]; then
86 87 88
	cat >&2 << __EOF__
No private key found. Use 'abuild-keygen' to generate the keys.
Then you can either:
89
  * set the PACKAGER_PRIVKEY in $ABUILD_USERCONF
90
    ('abuild-keygen -a' does this for you)
91
  * set the PACKAGER_PRIVKEY in $ABUILD_CONF
92
  * specify the key with the -k option to $program
93 94

__EOF__
Natanael Copa's avatar
Natanael Copa committed
95 96 97 98 99 100 101
	exit 1
fi

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

Dubiousjim's avatar
Dubiousjim committed
102
do_sign "$@"
103
exit 0