freedombone/src/freedombone-addcert

235 lines
6.9 KiB
Plaintext
Raw Normal View History

#!/bin/bash
2015-04-04 14:07:00 +02:00
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# A script for creating self-signed certificates on Debian
# License
# =======
#
# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-11-27 12:42:16 +01:00
PROJECT_NAME='freedombone'
2015-11-27 17:52:23 +01:00
export TEXTDOMAIN=${PROJECT_NAME}-addcert
2015-11-27 12:42:16 +01:00
export TEXTDOMAINDIR="/usr/share/locale"
HOSTNAME=
2015-11-17 23:21:40 +01:00
LETSENCRYPT_HOSTNAME=
COUNTRY_CODE="US"
AREA="Free Speech Zone"
LOCATION="Freedomville"
ORGANISATION="Freedombone"
UNIT="Freedombone Unit"
2015-06-14 16:56:46 +02:00
EXTENSIONS=""
NODH=
2015-10-27 15:38:05 +01:00
DH_KEYLENGTH=2048
2015-11-17 23:21:40 +01:00
INSTALL_DIR=/root/build
LETSENCRYPT_SERVER='https://acme-v01.api.letsencrypt.org/directory'
function show_help {
echo ''
2015-11-27 16:29:43 +01:00
echo $'freedombone-addcert -h [hostname] -c [country code] -a [area] -l [location]'
echo $' -o [organisation] -u [unit] --ca "" --nodh ""'
echo ''
2015-11-27 16:29:43 +01:00
echo $'Creates a self-signed certificate for the given hostname'
echo ''
2015-11-27 16:29:43 +01:00
echo $' --help Show help'
echo $' -h --hostname [name] Hostname'
echo $' -e --letsencrypt [hostname] Hostname to use with Lets Encrypt'
echo $' -s --server [url] Lets Encrypt server URL'
echo $' -c --country [code] Optional country code (eg. US, GB, etc)'
echo $' -a --area [description] Optional area description'
echo $' -l --location [locn] Optional location name'
echo $' -o --organisation [name] Optional organisation name'
echo $' -u --unit [name] Optional unit name'
echo $' --dhkey [bits] DH key length in bits'
echo $' --nodh "" Do not calculate DH params'
echo $' --ca "" Certificate authority cert'
echo ''
2015-04-04 14:07:00 +02:00
exit 0
}
while [[ $# > 1 ]]
do
key="$1"
case $key in
--help)
show_help
;;
-h|--hostname)
shift
HOSTNAME="$1"
;;
2015-11-17 23:21:40 +01:00
-e|--letsencrypt)
shift
LETSENCRYPT_HOSTNAME="$1"
;;
-s|--server)
shift
LETSENCRYPT_SERVER="$1"
;;
-c|--country)
shift
COUNTRY_CODE="$1"
;;
-a|--area)
shift
AREA="$1"
;;
-l|--location)
shift
LOCATION="$1"
;;
-o|--organisation)
shift
ORGANISATION="$1"
;;
-u|--unit)
shift
UNIT="$1"
;;
2015-06-14 16:56:46 +02:00
--ca)
2015-06-19 23:54:32 +02:00
shift
2015-06-14 16:56:46 +02:00
EXTENSIONS="-extensions v3_ca"
2015-06-19 23:08:28 +02:00
ORGANISATION="Freedombone-CA"
2015-06-14 16:56:46 +02:00
;;
--nodh)
2015-06-19 23:54:32 +02:00
shift
NODH="true"
;;
2015-08-15 10:10:00 +02:00
--dhkey)
shift
DH_KEYLENGTH=${1}
;;
*)
# unknown option
;;
esac
shift
done
if [ ! $HOSTNAME ]; then
2015-11-17 23:21:40 +01:00
if [ ! $LETSENCRYPT_HOSTNAME ]; then
2015-11-27 16:29:43 +01:00
echo $'No hostname specified'
2015-11-17 23:21:40 +01:00
exit 5748
fi
fi
if ! which openssl > /dev/null ;then
2015-11-27 16:29:43 +01:00
echo $"$0: openssl is not installed, exiting" 1>&2
exit 5689
fi
2015-11-17 23:21:40 +01:00
if [ ! -d /etc/ssl/mycerts ]; then
mkdir /etc/ssl/mycerts
2015-06-19 23:08:28 +02:00
fi
2015-11-17 23:21:40 +01:00
if [ $LETSENCRYPT_HOSTNAME ]; then
CERTFILE=$LETSENCRYPT_HOSTNAME
if [ ! -d $INSTALL_DIR ]; then
mkdir -p $INSTALL_DIR
fi
cd $INSTALL_DIR
# obtain the repo
if [ ! -d $INSTALL_DIR/letsencrypt ]; then
git clone https://github.com/letsencrypt/letsencrypt
if [ ! -d $INSTALL_DIR/letsencrypt ]; then
exit 76283
fi
else
cd $INSTALL_DIR/letsencrypt
git stash
git pull
fi
cd $INSTALL_DIR/letsencrypt
# TODO this requires user interaction - is there a non-interactive mode?
./letsencrypt-auto certonly --server $LETSENCRYPT_SERVER --standalone -d $LETSENCRYPT_HOSTNAME
if [ ! "$?" = "0" ]; then
2015-11-27 16:29:43 +01:00
echo $"Failed to install letsencrypt for domain $LETSENCRYPT_HOSTNAME"
2015-11-17 23:21:40 +01:00
exit 63216
fi
# replace some legacy filenames
if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt ]; then
mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
fi
if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt ]; then
mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
fi
sed -i "s|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem|g" /etc/nginx/sites-available/$LETSENCRYPT_HOSTNAME
sed -i "s|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem|g" /etc/nginx/sites-available/$LETSENCRYPT_HOSTNAME
2015-11-27 12:42:16 +01:00
2015-11-17 23:21:40 +01:00
# link the private key
if [ -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key ]; then
if [ ! -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key.old ]; then
mv /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key.old
fi
fi
ln -s /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/privkey.pem /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key
# link the public key
if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem ]; then
if [ ! -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem.old ]; then
mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem.old
fi
fi
ln -s /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/fullchain.pem /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
cp /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/fullchain.pem /etc/ssl/mycerts/${LETSENCRYPT_HOSTNAME}.pem
else
CERTFILE=$HOSTNAME
if [[ $ORGANISATION == "Freedombone-CA" ]]; then
CERTFILE="ca-$HOSTNAME"
fi
openssl req -x509 $EXTENSIONS -nodes -days 3650 -sha256 \
2015-06-19 23:54:32 +02:00
-subj "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$HOSTNAME" \
-newkey rsa:4096 -keyout /etc/ssl/private/$CERTFILE.key \
-out /etc/ssl/certs/$CERTFILE.crt
2015-11-17 23:21:40 +01:00
chmod 400 /etc/ssl/private/$CERTFILE.key
chmod 640 /etc/ssl/certs/$CERTFILE.crt
cp /etc/ssl/certs/$CERTFILE.crt /etc/ssl/mycerts
fi
2015-11-17 23:21:40 +01:00
# generate DH params
if [ ! $NODH ]; then
if [ ! -f /etc/ssl/certs/$CERTFILE.dhparam ]; then
openssl dhparam -check -text -dsaparam $DH_KEYLENGTH -out /etc/ssl/certs/$CERTFILE.dhparam
2015-11-17 23:21:40 +01:00
chmod 640 /etc/ssl/certs/$CERTFILE.dhparam
fi
fi
2015-11-17 23:21:40 +01:00
if [ -f /etc/init.d/nginx ]; then
/etc/init.d/nginx reload
fi
# Create a bundle of your certificates
2015-11-17 23:21:40 +01:00
cat /etc/ssl/mycerts/*.crt /etc/ssl/mycerts/*.pem > /etc/ssl/freedombone-bundle.crt
tar -czvf /etc/ssl/freedombone-certs.tar.gz /etc/ssl/mycerts/*.crt /etc/ssl/mycerts/*.pem
exit 0