2015-11-18 14:48:14 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import bson
|
2016-02-29 18:51:58 +01:00
|
|
|
import re
|
|
|
|
import smtplib
|
|
|
|
from email.mime.text import MIMEText
|
2015-11-18 14:48:14 +01:00
|
|
|
|
|
|
|
def format_query(query_usr):
|
|
|
|
query_list = []
|
|
|
|
for key, value in query_usr.items():
|
|
|
|
if key == "hostname":
|
|
|
|
qtag = ""
|
|
|
|
else:
|
|
|
|
qtag = "%s:" % key
|
|
|
|
query_list.append("%s%s" % (qtag, value))
|
|
|
|
return " ".join(query_list)
|
|
|
|
|
2016-02-29 18:51:58 +01:00
|
|
|
allowed_filters = (
|
|
|
|
'status',
|
|
|
|
'hood',
|
|
|
|
'community',
|
2017-11-05 19:48:29 +01:00
|
|
|
'nickname',
|
|
|
|
'hardware',
|
|
|
|
'firmware',
|
|
|
|
'mac',
|
|
|
|
'netif',
|
2016-02-29 18:51:58 +01:00
|
|
|
'netmon_id',
|
|
|
|
'hostname',
|
2017-11-05 19:48:29 +01:00
|
|
|
'contact',
|
2016-02-29 18:51:58 +01:00
|
|
|
)
|
2017-11-05 19:48:29 +01:00
|
|
|
|
|
|
|
def query2where(query):
|
|
|
|
s = ""
|
|
|
|
t = []
|
|
|
|
i = 0
|
|
|
|
for k, v in query.items():
|
|
|
|
if not k in allowed_filters:
|
|
|
|
# prevent SQL injection
|
|
|
|
continue
|
|
|
|
if i==0:
|
|
|
|
prefix = " WHERE "
|
|
|
|
else:
|
|
|
|
prefix = " AND "
|
|
|
|
i += 1
|
|
|
|
s += prefix + k + " = %s"
|
|
|
|
t.append(v)
|
|
|
|
return (s,tuple(t))
|
|
|
|
|
2015-11-18 14:48:14 +01:00
|
|
|
def parse_router_list_search_query(args):
|
|
|
|
query_usr = bson.SON()
|
|
|
|
if "q" in args:
|
|
|
|
for word in args["q"].strip().split(" "):
|
2017-11-05 19:48:29 +01:00
|
|
|
if not word:
|
|
|
|
# Case of "q=" without arguments
|
|
|
|
break
|
2015-11-18 14:48:14 +01:00
|
|
|
if not ':' in word:
|
|
|
|
key = "hostname"
|
|
|
|
value = word
|
|
|
|
else:
|
2015-12-09 19:57:15 +01:00
|
|
|
key, value = word.split(':', 1)
|
2015-11-18 14:48:14 +01:00
|
|
|
if key in allowed_filters:
|
|
|
|
query_usr[key] = query_usr.get(key, "") + value
|
|
|
|
query = {}
|
|
|
|
for key, value in query_usr.items():
|
2017-11-05 19:48:29 +01:00
|
|
|
#if value == "EXISTS":
|
|
|
|
# query[key] = {"$exists": True}
|
|
|
|
#elif value == "EXISTS_NOT":
|
|
|
|
# query[key] = {"$exists": False}
|
|
|
|
if key == 'mac':
|
2015-11-18 14:48:14 +01:00
|
|
|
query[key] = value.lower()
|
2017-11-05 19:48:29 +01:00
|
|
|
#elif key == 'netif':
|
|
|
|
# query[key] = {"$regex": value.replace('.', '\.'), "$options": 'i'}
|
2015-11-18 14:48:14 +01:00
|
|
|
elif key == 'hostname':
|
2017-11-05 19:48:29 +01:00
|
|
|
query[key] = value.replace('\\', '')
|
|
|
|
elif key == 'hardware':
|
|
|
|
query[key] = value.replace('\\', '').replace('_', ' ')
|
|
|
|
#elif key == 'netmon_id':
|
|
|
|
# query[key] = int(value)
|
|
|
|
elif key == 'contact':
|
|
|
|
query[key] = value.replace('\\', '')
|
|
|
|
#elif key == 'contact':
|
|
|
|
# if not '\.' in value:
|
|
|
|
# value = re.escape(value)
|
|
|
|
# query[key] = {"$regex": value, "$options": 'i'}
|
|
|
|
#elif value.startswith('!'):
|
|
|
|
# query[key] = {"$ne": value.replace('!', '', 1)}
|
2015-11-18 14:48:14 +01:00
|
|
|
else:
|
|
|
|
query[key] = value
|
|
|
|
return (query, format_query(query_usr))
|
2016-02-29 18:51:58 +01:00
|
|
|
|
|
|
|
def send_email(recipient, subject, content, sender="FFF Monitoring <noreply@monitoring.freifunk-franken.de>"):
|
|
|
|
msg = MIMEText(content)
|
|
|
|
msg['Subject'] = subject
|
|
|
|
msg['From'] = sender
|
|
|
|
msg['To'] = recipient
|
|
|
|
s = smtplib.SMTP('localhost')
|
|
|
|
s.send_message(msg)
|
|
|
|
s.quit()
|
|
|
|
|
|
|
|
def is_authorized(owner, session):
|
2016-09-25 19:46:41 +02:00
|
|
|
if ("user" in session) and (owner == session.get("user")):
|
2016-02-29 18:51:58 +01:00
|
|
|
return True
|
|
|
|
elif session.get("admin"):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|