2019-08-14 15:23:14 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
bestip=$(dig +short a tilde.best)
|
|
|
|
|
2019-08-14 17:30:49 +00:00
|
|
|
[! -d /var/lib/byod] && mkdir /var/lib/byod && mkdir /var/lib/byod/user && mkdir /var/lib/byod/domain
|
2019-08-14 16:28:55 +00:00
|
|
|
|
2019-08-14 15:23:14 +00:00
|
|
|
for byod in /home/*/byod; do
|
|
|
|
user=$(stat -c '%U' $byod)
|
|
|
|
domain=$(cat $byod)
|
|
|
|
echo "$user is requesting $domain"
|
2019-08-14 17:18:48 +00:00
|
|
|
if [ -f /var/lib/byod/$domain ]; then
|
2019-08-14 16:28:55 +00:00
|
|
|
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
|
2019-08-14 15:23:14 +00:00
|
|
|
else
|
|
|
|
domainip=$(dig +short a $domain)
|
2019-08-14 17:36:59 +00:00
|
|
|
if [ "$bestip" == "$domainip" ]; then
|
2019-08-14 16:28:55 +00:00
|
|
|
echo "$domain" > /var/lib/byod/user/$user
|
|
|
|
echo "$user" > /var/lib/byod/domain/$domain
|
2019-08-14 15:23:14 +00:00
|
|
|
sed -e "s/<user>/$user/" -e "s/<domain>/$domain/" template.conf \
|
2019-08-14 16:28:55 +00:00
|
|
|
> /etc/nginx/sites-available/byod-$domain.conf
|
|
|
|
ln -s ../sites-available/byod-$domain.conf /etc/nginx/sites-enabled/byod-$domain.conf
|
2019-08-14 15:23:14 +00:00
|
|
|
certbot --nginx -d $domain --non-interactive --redirect
|
|
|
|
else
|
|
|
|
echo "domain's dns not configured correctly"
|
|
|
|
fi
|
|
|
|
fi
|
2019-08-14 10:19:09 +00:00
|
|
|
done
|
2019-08-14 15:23:14 +00:00
|
|
|
|
|
|
|
nginx -t
|
|
|
|
|
2019-08-15 07:32:03 +00:00
|
|
|
if [ "$?" -eq "0" ]; then
|
2019-08-14 16:28:55 +00:00
|
|
|
nginx -s reload
|
|
|
|
else
|
|
|
|
echo "failed to validate new nginx config, see previous error"
|
|
|
|
fi
|