From 31780eb14db9e7ef5cb7f7988a041825fd68453d Mon Sep 17 00:00:00 2001 From: user Date: Wed, 14 Aug 2019 18:28:55 +0200 Subject: [PATCH] One config per domain + /var/lib/byod/ to store this info --- admin.sh | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/admin.sh b/admin.sh index e49043b..794270e 100644 --- a/admin.sh +++ b/admin.sh @@ -2,20 +2,30 @@ bestip=$(dig +short a tilde.best) +[! -d /var/lib/byod] && mkdir /var/lib/byod + for byod in /home/*/byod; do user=$(stat -c '%U' $byod) domain=$(cat $byod) echo "$user is requesting $domain" - if [ -s /etc/nginx/sites-enabled/byod-$user-$domain.conf ]; then - echo "byod already configured for $user at $domain" - continue + if [ -s /etc/nginx/sites-enabled/byod-$domain.conf ]; then + echo "byod already configured for $domain" + previousUser=$(cat /var/lib/byod/user/$user) + if [! "$user" -eq "$previousUser"]; then + error="ERROR: The domain $domain was already reclaimed by $previousUser" + echo "$error" + mail=$(printf "From: root@tilde.best\nTo: $user\nSubject: BYOD failed to add domain $domain\n$error\n.\n") + echo "$mail" | sendmail -t + continue + fi else domainip=$(dig +short a $domain) - if [ "$bestip" == "$domainip" ] - then + if [ "$bestip" == "$domainip"]; then + echo "$domain" > /var/lib/byod/user/$user + echo "$user" > /var/lib/byod/domain/$domain sed -e "s//$user/" -e "s//$domain/" template.conf \ - > /etc/nginx/sites-available/byod-$user-$domain.conf - ln -s ../sites-available/byod-$user-$domain.conf /etc/nginx/sites-enabled/byod-$user-$domain.conf + > /etc/nginx/sites-available/byod-$domain.conf + ln -s ../sites-available/byod-$domain.conf /etc/nginx/sites-enabled/byod-$domain.conf certbot --nginx -d $domain --non-interactive --redirect else echo "domain's dns not configured correctly" @@ -24,5 +34,9 @@ for byod in /home/*/byod; do done nginx -t -systemctl reload nginx +if ["$?" -eq "0"]; then + nginx -s reload +else + echo "failed to validate new nginx config, see previous error" +fi