2009-07-22 09:46:24 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# generate signing keys
|
|
|
|
# Copyright (c) 2009 Natanael Copa <ncopa@alpinelinux.org>
|
|
|
|
#
|
|
|
|
# Distributed under GPL-2
|
|
|
|
#
|
|
|
|
# Depends on: busybox utilities, fakeroot,
|
|
|
|
#
|
|
|
|
|
|
|
|
abuild_ver=@VERSION@
|
|
|
|
sysconfdir=@sysconfdir@
|
|
|
|
|
|
|
|
abuild_conf=${ABUILD_CONF:-"$sysconfdir/abuild.conf"}
|
|
|
|
abuild_home=${ABUILD_USERDIR:-"$HOME/.abuild"}
|
|
|
|
abuild_userconf=${ABUILD_USERCONF:-"$abuild_home/abuild.conf"}
|
|
|
|
|
2010-12-20 13:33:45 +00:00
|
|
|
# echo message unless quite mode
|
|
|
|
msg() {
|
|
|
|
[ -n "$quiet" ] && return 0
|
|
|
|
echo "$@"
|
|
|
|
}
|
|
|
|
|
2010-12-20 13:22:29 +00:00
|
|
|
# ask for privkey unless non-interactive mode
|
|
|
|
# returns value in global $privkey
|
|
|
|
get_privkey_file() {
|
|
|
|
privkey="$abuild_home/$default_name.rsa"
|
|
|
|
[ "$non_interactive" = "yes" ] && return 0
|
|
|
|
echo "Generating public/private rsa key pair for abuild"
|
|
|
|
echo -n "Enter file in which to save the key ($abuild_home/$default_name.rsa): "
|
|
|
|
|
|
|
|
read line
|
|
|
|
if [ -n "$line" ]; then
|
|
|
|
privkey="$line"
|
|
|
|
fi
|
|
|
|
}
|
2009-07-22 14:16:58 +00:00
|
|
|
|
2010-12-20 13:33:45 +00:00
|
|
|
# print usage and exit
|
2009-07-22 14:16:58 +00:00
|
|
|
usage() {
|
|
|
|
echo "abuild-keygen $abuild_ver"
|
|
|
|
echo "usage: abuild-keygen [-ih]"
|
|
|
|
echo "options:"
|
2009-07-23 06:38:52 +00:00
|
|
|
echo " -a Set PACKAGER_PRIVKEY=<generated key> in $abuild_userconf"
|
2009-07-22 14:16:58 +00:00
|
|
|
echo " -i Install public key into /etc/apk/keys using sudo"
|
|
|
|
echo " -h Show this help"
|
2010-12-20 13:22:29 +00:00
|
|
|
echo " -n Non-interactive. Use defaults"
|
2010-12-20 13:33:45 +00:00
|
|
|
echo " -q Quiet mode"
|
2009-07-22 14:16:58 +00:00
|
|
|
echo ""
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2009-07-22 09:46:24 +00:00
|
|
|
# read config
|
|
|
|
[ -f "$abuild_conf" ] && . "$abuild_conf"
|
|
|
|
|
|
|
|
# read user config if exists
|
|
|
|
[ -f "$abuild_userconf" ] && . "$abuild_userconf"
|
|
|
|
|
|
|
|
emailaddr=${PACKAGER##*<}
|
|
|
|
emailaddr=${emailaddr%%>*}
|
|
|
|
|
|
|
|
# if PACKAGER does not contain a valid email address, then ask git
|
|
|
|
if [ -z "$emailaddr" ] || [ "${emailaddr##*@}" = "$emailaddr" ]; then
|
|
|
|
emailaddr=$(git config --get user.email 2>/dev/null)
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$emailaddr" ]; then
|
2009-07-24 19:12:56 +00:00
|
|
|
default_name="$emailaddr-$(printf "%x" $(date +%s))"
|
2009-07-22 09:46:24 +00:00
|
|
|
else
|
2009-07-24 19:12:56 +00:00
|
|
|
default_name="$USER-$(printf "%x" $(date +%s))"
|
2009-07-22 09:46:24 +00:00
|
|
|
fi
|
|
|
|
|
2010-12-20 13:33:45 +00:00
|
|
|
while getopts "ahinq" opt; do
|
2009-07-22 14:16:58 +00:00
|
|
|
case $opt in
|
2009-07-23 06:38:52 +00:00
|
|
|
a) append_config=yes;;
|
2009-07-22 14:16:58 +00:00
|
|
|
h) usage;;
|
2010-12-20 13:22:29 +00:00
|
|
|
i) install_pubkey=yes;;
|
|
|
|
n) non_interactive=yes;;
|
2010-12-20 13:33:45 +00:00
|
|
|
q) quiet=-quiet;;
|
2009-07-22 14:16:58 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( $OPTIND - 1))
|
|
|
|
|
2009-07-22 09:46:24 +00:00
|
|
|
mkdir -p "$abuild_home"
|
|
|
|
|
2010-12-20 13:22:29 +00:00
|
|
|
get_privkey_file
|
2009-07-22 09:46:24 +00:00
|
|
|
pubkey="$privkey.pub"
|
|
|
|
|
|
|
|
# generate the private key in a subshell with stricter umask
|
|
|
|
(
|
2009-07-23 06:38:52 +00:00
|
|
|
umask 0007
|
2009-07-22 09:46:24 +00:00
|
|
|
openssl genrsa -out "$privkey" 2048
|
|
|
|
)
|
|
|
|
openssl rsa -in "$privkey" -pubout -out "$pubkey"
|
|
|
|
|
2009-07-22 14:16:58 +00:00
|
|
|
|
|
|
|
if [ -n "$install_pubkey" ]; then
|
2010-12-20 13:33:45 +00:00
|
|
|
msg "Installing $pubkey to /etc/apk/keys..."
|
2009-07-22 14:16:58 +00:00
|
|
|
sudo mkdir -p /etc/apk/keys
|
|
|
|
sudo cp -i "$pubkey" /etc/apk/keys/
|
|
|
|
else
|
|
|
|
|
2010-12-20 13:33:45 +00:00
|
|
|
msg ""
|
|
|
|
msg "You'll need to install $pubkey into "
|
|
|
|
msg "/etc/apk/keys to be able to install packages and repositories signed with"
|
|
|
|
msg "$privkey"
|
2009-07-22 14:16:58 +00:00
|
|
|
fi
|
|
|
|
|
2009-07-23 06:38:52 +00:00
|
|
|
if [ -n "$append_config" ]; then
|
|
|
|
if [ -f "$abuild_userconf" ]; then
|
|
|
|
# comment out the existing values
|
|
|
|
sed -i -e 's/^\(PACKAGER_PRIVKEY=.*\)/\#\1/' "$abuild_userconf"
|
|
|
|
fi
|
|
|
|
echo "PACKAGER_PRIVKEY=\"$privkey\"" >> "$abuild_userconf"
|
|
|
|
else
|
2010-12-20 13:33:45 +00:00
|
|
|
msg ""
|
|
|
|
msg "You might want add following line to $abuild_userconf:"
|
|
|
|
msg ""
|
|
|
|
msg "PACKAGER_PRIVKEY=\"$privkey\""
|
|
|
|
msg ""
|
2009-07-23 06:38:52 +00:00
|
|
|
fi
|
|
|
|
|
2010-12-20 13:33:45 +00:00
|
|
|
msg ""
|
|
|
|
msg "Please remember to make a safe backup of your private key:"
|
|
|
|
msg "$privkey"
|
|
|
|
msg ""
|
2009-07-22 09:46:24 +00:00
|
|
|
|