| 1 | #!/bin/sh |
| 2 | |
| 3 | # sign indexes |
| 4 | # Copyright (c) 2009 Natanael Copa <[email protected]> |
| 5 | # |
| 6 | # Distributed under GPL-2 |
| 7 | # |
| 8 | # Depends on: busybox utilities, fakeroot, |
| 9 | # |
| 10 | |
| 11 | abuild_ver=@VERSION@ |
| 12 | sysconfdir=@sysconfdir@ |
| 13 | |
| 14 | abuild_conf=${ABUILD_CONF:-"$sysconfdir/abuild.conf"} |
| 15 | abuild_home=${ABUILD_USERDIR:-"$HOME/.abuild"} |
| 16 | abuild_userconf=${ABUILD_USERCONF:-"$abuild_home/abuild.conf"} |
| 17 | |
| 18 | die() { |
| 19 | echo "$@" >&2 |
| 20 | exit 1 |
| 21 | } |
| 22 | |
| 23 | usage() { |
| 24 | echo "abuild-sign $abuild_ver" |
| 25 | echo "usage: abuild-sign [-hq] [-k PRIVKEY] [-p PUBKEY] INDEXFILE..." |
| 26 | echo "options:" |
| 27 | echo " -h Show this help" |
| 28 | echo " -k The private key to use for signing" |
| 29 | echo " -p The name of public key. apk add will look for /etc/apk/keys/PUBKEY" |
| 30 | exit 1 |
| 31 | } |
| 32 | |
| 33 | # read config |
| 34 | [ -f "$abuild_conf" ] && . "$abuild_conf" |
| 35 | |
| 36 | # read user config if exists |
| 37 | [ -f "$abuild_userconf" ] && . "$abuild_userconf" |
| 38 | |
| 39 | privkey="$PACKAGER_PRIVKEY" |
| 40 | |
| 41 | while getopts "hk:p:q" opt; do |
| 42 | case $opt in |
| 43 | h) usage;; |
| 44 | k) privkey=$OPTARG;; |
| 45 | p) pubkey=$OPTARG;; |
| 46 | q) quiet=yes;; |
| 47 | esac |
| 48 | done |
| 49 | shift $(( $OPTIND - 1)) |
| 50 | |
| 51 | if [ -z "$privkey" ]; then |
| 52 | echo "No private key found. Use 'abuild-keygen' to generate the keys" |
| 53 | echo "Then you can either:" |
| 54 | echo " 1. set the PACKAGER_PRIVKEY in $abuild_userconf" |
| 55 | echo " (Note that 'abuild-keygen -a' does this for you)" |
| 56 | echo " 2. set the PACKAGER_PRIVKEY in $abuild_conf" |
| 57 | echo " 3. specify the key with the -k option" |
| 58 | echo "" |
| 59 | exit 1 |
| 60 | fi |
| 61 | |
| 62 | if [ -z "$pubkey" ]; then |
| 63 | pubkey=${PACKAGER_PUBKEY:-"${privkey}.pub"} |
| 64 | fi |
| 65 | |
| 66 | # we are actually only interested in the name, not the file itself |
| 67 | keyname=${pubkey##*/} |
| 68 | |
| 69 | for f in "$@"; do |
| 70 | i=$(readlink -f $f) |
| 71 | [ -d "$i" ] && i="$i/APKINDEX.tar.gz" |
| 72 | repo="${i%/*}" |
| 73 | cd "$repo" || die "Failed to sign $i" |
| 74 | sig=".SIGN.RSA.$keyname" |
| 75 | openssl dgst -sha1 -sign "$privkey" -out "$sig" "$i" || die "Failed to sign $i" |
| 76 | tmptargz=$(mktemp) |
| 77 | tar -c "$sig" | abuild-tar --cut | gzip -9 > "$tmptargz" |
| 78 | tmpsigned=$(mktemp) |
| 79 | cat "$tmptargz" "$i" > "$tmpsigned" |
| 80 | rm -f "$tmptargz" "$sig" |
| 81 | mv "$tmpsigned" "$i" |
| 82 | chmod 644 "$i" |
| 83 | if [ -z "$quiet" ]; then |
| 84 | echo "Signed $i" |
| 85 | fi |
| 86 | done |
| 87 | |
| 88 | exit 0 |