byod/admin.sh

43 lines
1.5 KiB
Bash

#!/bin/sh
bestip=$(dig +short a tilde.best)
[! -d /var/lib/byod] && mkdir /var/lib/byod && mkdir /var/lib/byod/user && mkdir /var/lib/byod/domain
for byod in /home/*/byod; do
user=$(stat -c '%U' $byod)
domain=$(cat $byod)
echo "$user is requesting $domain"
if [ -f /var/lib/byod/$domain ]; 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
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-$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"
fi
fi
done
nginx -t
if [ "$?" -eq "0" ]; then
nginx -s reload
else
echo "failed to validate new nginx config, see previous error"
fi