aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWojciech Kosior <kwojtus@protonmail.com>2020-06-19 02:58:58 +0200
committerWojciech Kosior <kwojtus@protonmail.com>2020-06-19 02:58:58 +0200
commitdfa1a7d0c3b8d3132453511bf48e9c9eec47f40f (patch)
tree03ddca9721eef2fc1de56616e240ad5dda3f4b5e
parentd8194e0d2cc27c426d1619a26eab5738e3a83fde (diff)
download0tdns-dfa1a7d0c3b8d3132453511bf48e9c9eec47f40f.tar.gz
0tdns-dfa1a7d0c3b8d3132453511bf48e9c9eec47f40f.zip
send emails to users when ip doesn't match
-rw-r--r--0tdns_crontab1
-rw-r--r--db_connection_config.yml3
-rwxr-xr-xinstall.sh5
-rwxr-xr-xsrc/send_emails.py55
-rwxr-xr-xuninstall.sh1
5 files changed, 62 insertions, 3 deletions
diff --git a/0tdns_crontab b/0tdns_crontab
index 859c267..b0eed90 100644
--- a/0tdns_crontab
+++ b/0tdns_crontab
@@ -1,3 +1,4 @@
0 * * * * /usr/sbin/hourly.py # <AUTO_GENERATED_0TDNS_ENTRY> do not remove this '<>' tag
@reboot /usr/bin/rm /var/lib/0tdns/lockfile # <AUTO_GENERATED_0TDNS_ENTRY> do not remove this '<>' tag
15,30,45 * * * * /usr/sbin/check_if_done.py --send-mail # <AUTO_GENERATED_0TDNS_ENTRY> do not remove this '<>' tag
+0 * * * * /usr/bin/send_emails.py # <AUTO_GENERATED_0TDNS_ENTRY> do not remove this '<>' tag \ No newline at end of file
diff --git a/db_connection_config.yml b/db_connection_config.yml
index 5c82751..b66d496 100644
--- a/db_connection_config.yml
+++ b/db_connection_config.yml
@@ -6,4 +6,5 @@ database: "ztdns"
enabled: no
handled_vpns: [1, 2]
parallel_vpns: 20
-private_addresses: ["10.25.25.0 - 10.25.25.59", "10.25.26.0 - 10.25.26.255"] \ No newline at end of file
+private_addresses: ["10.25.25.0 - 10.25.25.59", "10.25.26.0 - 10.25.26.255"]
+send_user_emails: no \ No newline at end of file
diff --git a/install.sh b/install.sh
index 091fd3c..ad71236 100755
--- a/install.sh
+++ b/install.sh
@@ -19,10 +19,11 @@ install -D -m744 src/netns-script "$INSTALL_ROOT"/var/lib/0tdns/netns-script
install -D -m755 src/perform_queries.py "$INSTALL_ROOT"/var/lib/0tdns/perform_queries.py
install -D -m644 db_connection_config.yml "$INSTALL_ROOT"/etc/0tdns/db_connection_config.yml
-# This one would make sense to be executed directly, so it'll go to sbin
-# This happens to also be the script, that gets called by cron
+# Those would make sense to be executed directly, so they'll go to (s)bin
+# Those happen to also be the scripts, that get called by cron
install -D -m744 src/hourly.py "$INSTALL_ROOT"/usr/sbin/hourly.py
install -D -m744 src/check_if_done.py "$INSTALL_ROOT"/usr/sbin/check_if_done.py
+install -D -m744 src/send_emails.py "$INSTALL_ROOT"/usr/bin/send_emails.py
# This one shall be imported from other scripts
install -D -m644 src/ztdnslib.py "$INSTALL_ROOT"/usr/lib/python3/dist-packages/ztdnslib.py
diff --git a/src/send_emails.py b/src/send_emails.py
new file mode 100755
index 0000000..bbbd7b0
--- /dev/null
+++ b/src/send_emails.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python3
+
+from time import gmtime, strftime, time
+
+import smtplib
+
+from ztdnslib import start_db_connection, get_ztdns_config
+
+def sendMail(to, subject, content):
+
+ email = 'FILL THIS FIELD'
+ password = 'FILL THIS FIELD'
+ message = 'Subject: {}\n\n{}'.format(subject, content)
+ mail = smtplib.SMTP('smtp.gmail.com',587)
+ mail.ehlo()
+ mail.starttls()
+ mail.login(email,password)
+ mail.sendmail('zerotdns@gmail.com',to, message)
+ mail.close()
+
+
+config = get_ztdns_config()
+if config['send_user_emails'] not in [True, 'yes']:
+ print('Sending emails disabled in config - exiting')
+ exit()
+
+selectEmailsThatFailured = '''select distinct email from auth_user au
+inner join user_side_subscription uss on au.id = uss.user_id
+inner join user_side_service usservice on uss.service_id = usservice.id
+inner join user_side_responses usr on usservice.id = usr.service_id
+inner join user_side_response usr1 on usr.id = usr1.responses_id
+where usr.result like 'successful' and usr1.returned_ip != usservice."IP" and
+ usr.date = TIMESTAMP WITH TIME ZONE %s'''
+
+connection = start_db_connection(config)
+cursor = connection.cursor()
+
+seconds = time()
+seconds_hour_ago = seconds - 3600
+timestamp_hour_ago = strftime('%Y-%m-%d %H:00%z', gmtime(seconds_hour_ago))
+
+cursor.execute(selectEmailsThatFailured, (timestamp_hour_ago,))
+
+content = 'Uwaga, DNS odpowiedziaƂ niepoprawnym adresem!'
+# to = 'example@site.com'
+subject = '0tdns alert'
+
+for row in cursor.fetchall():
+ to = row[0]
+ if (to != ""):
+ sendMail(to, subject, content)
+
+cursor.close()
+connection.close()
+
diff --git a/uninstall.sh b/uninstall.sh
index 294c151..3bebfbb 100755
--- a/uninstall.sh
+++ b/uninstall.sh
@@ -24,6 +24,7 @@ if [ "x$1" = "x--delete-files" ]; then
rm -r /etc/netns/0tdns*
rm /usr/sbin/hourly.py
rm /usr/sbin/check_if_done.py
+ rm /usr/bin/send_emails.py
rm -r /etc/0tdns
rm /usr/lib/python3/dist-packages/ztdnslib.py
fi