One config per domain + /var/lib/byod/ to store this info #1

Manually merged
den merged 1 commits from southerntofu/byod:master into master 2019-08-14 16:54:36 +00:00
1 changed files with 22 additions and 8 deletions

View File

@ -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>/$user/" -e "s/<domain>/$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