-
Notifications
You must be signed in to change notification settings - Fork 1
/
allow_rev2.py
executable file
·156 lines (121 loc) · 3.93 KB
/
allow_rev2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
import sys
import re
import os
import os.path, time, datetime
#if( len(sys.argv) < 1 ):
# sys.stderr.write("Usage: allw.py dhcp.leases");
# sys.exit(1)
from model import mac_status, DHCPLease, UserGroup
in_iface = 'eth1'
out_iface = 'eth0'
debug = False
def from_dhcp(dhcp_lease):
with open(dhcp_lease, "r") as fd:
st = fd.read().split("\n")
ends = ''
starts = ''
mac = ''
rules = []
for line in st:
if not line or line.startswith("#"):
continue;
if line == '}' and lease:
now = datetime.datetime.utcnow()
if(ends > now): # or debug):
rules.append(DHCPLease(ip=lease, mac=mac, starts=starts, ends=ends))
lease = None
continue
m = re.search(r'lease\s*([0-9\.]+)\s*\{', line)
if m:
lease = m.groups()[0]
continue
m = re.search(r'\s+([a-z\s\-]+)\s+\"?(.+?)?\"?\;', line)
if m:
k, v = m.groups()
if k == "hardware ethernet":
mac = v
elif k == "ends":
ends = datetime.datetime.strptime(v, "%w %Y/%m/%d %H:%M:%S")
elif k == "starts":
starts = datetime.datetime.strptime(v, "%w %Y/%m/%d %H:%M:%S")
return rules
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client['iasa-wifi']
user = db.user
class Group():
def __init__(self, name):
self.name = name
self.access = "ACCEPT"
def mac_status(doc):
if(doc == None):
jump = Group('guest')
else:
jump = Group(doc['group'])
return jump
def make_script(dhcp):
print("make_script")
templ = [
('filter', "-A INPUT --src {0} -m mac --mac-source {1} -j {2}"),
('filter', "-A FORWARD --src {0} -m mac --mac-source {1} -j {2}"),
('mangle', "-A FORWARD --src {0} -j {2}"),
('mangle', "-A FORWARD --dst {0} -j {2}")
]
iptb = {"mangle": [], "filter": []}
i = 0
for lease in dhcp:
doc = user.find_one({"devices": {"mac": lease.mac}})
jump = mac_status(doc)
# print(lease.mac, jump, jump.name, jump.access)
iptb['mangle'] += ["-A FORWARD --src {0} -j {2}".format(lease.ip, lease.mac, jump.name)]
iptb['mangle'] += ["-A FORWARD --dst {0} -j {2}".format(lease.ip, lease.mac, jump.name)]
iptb['filter'] += ["-A allow-inet --src {0} -m mac --mac-source {1} -j {2}".format(lease.ip, lease.mac, jump.access)]
script = ''
script += '*mangle\n'
script += '-F FORWARD\n'
script += '\n'.join(list(set(iptb['mangle'])))
script += '\nCOMMIT\n'
script += '*filter\n'
script += ':allow-inet - [0:0]\n'
script += '\n'.join(list(set(iptb['filter']))) + '\n'
script += 'COMMIT\n'
# print(script)
return script
def apply_script(script):
print("apply_script")
fd = os.popen("sudo iptables-restore --noflush", "w")
fd.write(script)
fd.close()
def save_dhcp(dhcp, last):
print("save_dhcp")
last = datetime.datetime.utcfromtimestamp(last - 3)
#print("Last: ", last)
for lease in dhcp:
#print(lease.starts, last)
if(lease.starts >= last):
#print("save")
lease.save()
def main():
dhcp_lease = sys.argv[1]
global debug
if(len(sys.argv) == 3 and sys.argv[2] == "debug"):
print("DEBUG mode")
debug = True
lastmtime = 3
while 1:
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(dhcp_lease)
if lastmtime == mtime:
time.sleep(1)
continue
dhcp = from_dhcp(dhcp_lease)
save_dhcp(dhcp, lastmtime)
script = make_script(dhcp)
if(debug):
print(script)
apply_script(script)
lastmtime = mtime
print ("last modified: %s" % mtime)
#return
if __name__ == "__main__":
main()