conf_server/add_domain_with_acme.sh

119 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
. ./myserver.conf
check_ssl_folder()
{
[ -d "$FOLDER_CONF/ssl" ] || mkdir -p "$FOLDER_CONF/ssl"
}
gen_nginx_acme_conf(){
domain=$1
alt_domain=$2
nginx_conf_file="/etc/nginx/sites-enabled/$domain"
[ ! -f $nginx_conf_file ] || rm $nginx_conf_file;
mkdir /var/www/htdocs/$domain
rcctl check nginx
if [ $? == 0 ]; then
cat > $nginx_conf_file <<EOF
server {
listen 80;
server_name $alt_domain $domain;
include snippets/acme-challenge.conf;
root /htdocs/$domain;
}
EOF
rcctl reload nginx
else
echo "Service NGINX not running"
exit 1
fi
}
# Generate part of acme client for the domain
gen_acme_client_conf(){
domain=$1
alt_domain=$2
acme_conf_file="$FOLDER_CONF/ssl/acme-client-$1.conf"
# If the file exist, do nothing
if [ -f $acme_conf_file ]
then
echo "Domain already configured !";
exit 1;
fi
if [ "$alt_domain" == "" ]; then
cat >> $acme_conf_file <<EOF
domain $domain {
domain key "/etc/ssl/private/$domain.key"
domain full chain certificate "/etc/ssl/$domain.crt"
sign with letsencrypt
}
EOF
else
cat >> $acme_conf_file <<EOF
domain $domain {
alternative names { $alt_domain }
domain key "/etc/ssl/private/$domain.key"
domain full chain certificate "/etc/ssl/$domain.crt"
sign with letsencrypt
}
EOF
fi
echo "on est passé dans gen_acme_client_conf "
}
add_acme_domain_to_conf(){
domain="domain $1 {"
acme_conf="$FOLDER_CONF/ssl/acme-client-$1.conf"
sed "/$domain/,/}/d" /etc/acme-client.conf > /tmp/acme-client.conf
cp -v /etc/acme-client.conf /etc/acme-client.conf.old
cat $acme_conf >> /tmp/acme-client.conf
cp -v /tmp/acme-client.conf /etc/acme-client.conf
}
install_utils(){
[ -f /usr/local/bin/renew_https_certificate ] || cp -v utils/renew_https_certificate /usr/local/bin/renew_https_certificate
chmod u+x /usr/local/bin/renew_https_certificate
}
get_certificate()
{
domain=$1
/usr/local/bin/renew_https_certificate "$domain"
}
usage()
{
print "This program ask 2 arguments : \n"
print "First is domain name the second is list of alternatives domains with \" \" \n"
print "\t $0 domain.tld \"a.domain.tld b.domain.tld c.domain.tld\""
}
if [ -z $1 ] || [ $1 == "-h" ] || [ $1 == "--help" ];
then
usage
exit 3;
fi
domain=$1
alt_domain=$2
check_ssl_folder
gen_acme_client_conf $domain $alt_domain
gen_nginx_acme_conf $domain $alt_domain
add_acme_domain_to_conf $domain
install_utils
get_certificate $domain