diff --git a/dhcp-hosts.dpto2 b/dhcp-hosts.dpto2 index b980259..b887dca 100644 --- a/dhcp-hosts.dpto2 +++ b/dhcp-hosts.dpto2 @@ -11,7 +11,7 @@ ff:ff:ff:ff:ff:fe,10.6.122.10 ff:ff:ff:ff:ff:fe,10.6.122.11 ff:ff:ff:ff:ff:fe,10.6.122.12 ff:ff:ff:ff:ff:fe,10.6.122.13 -# { "reserved-by": "WiFi dpto2", "locked": true } +# {"locked": true, "reserved-by": "WiFi dpto2"} ff:ff:ff:ff:ff:fe,10.6.122.14 ff:ff:ff:ff:ff:fe,10.6.122.15 ff:ff:ff:ff:ff:fe,10.6.122.16 @@ -94,14 +94,16 @@ ff:ff:ff:ff:ff:fe,10.6.122.92 ff:ff:ff:ff:ff:fe,10.6.122.93 ff:ff:ff:ff:ff:fe,10.6.122.94 ff:ff:ff:ff:ff:fe,10.6.122.95 -# { "reserved-by": "Andy" } +# {"reserved-by": "Andy"} 00:23:54:8c:65:13,10.6.122.96,set:client0023548c6513 +# {"reserved-by": "Somoza"} ff:ff:ff:ff:ff:fe,10.6.122.97 ff:ff:ff:ff:ff:fe,10.6.122.98 +# {"reserved-by": "Zelda"} ff:ff:ff:ff:ff:fe,10.6.122.99 ff:ff:ff:ff:ff:fe,10.6.122.100 -# { "reserved-by": "Andy" } -aa:bb:cc:dd:ee:ff,10.6.122.101,set:clientaabbccddeeff +# {"reserved-by": "Andy"} +12:24:42:23:24:45,10.6.122.101,set:client122442232445 ff:ff:ff:ff:ff:fe,10.6.122.102 ff:ff:ff:ff:ff:fe,10.6.122.103 ff:ff:ff:ff:ff:fe,10.6.122.104 @@ -240,4 +242,3 @@ ff:ff:ff:ff:ff:fe,10.6.122.251 ff:ff:ff:ff:ff:fe,10.6.122.252 ff:ff:ff:ff:ff:fe,10.6.122.253 ff:ff:ff:ff:ff:fe,10.6.122.254 -ff:ff:ff:ff:ff:fe,10.6.122.255 diff --git a/dhcp-opts.dpto2 b/dhcp-opts.dpto2 index cad2290..b60d303 100644 --- a/dhcp-opts.dpto2 +++ b/dhcp-opts.dpto2 @@ -1,3 +1,2 @@ -tag:client5cf9ffffffff,option:router,10.0.0.1 -tag:clientaabbccddeeff,option:router,10.6.122.96 -tag:client0023548c6513,option:router,10.6.122.2 +tag:client0023548c6513,option:router,10.6.122.96 +tag:client122442232445,option:router,10.6.122.2 diff --git a/main.py b/main.py index d7d7829..05320f2 100755 --- a/main.py +++ b/main.py @@ -46,7 +46,10 @@ exclusive_lock = Lock(lock_file) shared_lock = SharedLock(lock_file) def reload_freeradius(): - call("./reload_freeradius") + call("./sendhup freeradius") + +def reload_dnsmasq(): + call("./sendhup dnsmasq") def delete_user(deluser): f = open(users_file) @@ -303,31 +306,45 @@ def parse_macs(macs): return r def write_dhcp_files(ipmap): + timestamp = time.time() tempfile = "{}.{}".format(dhcp_hosts_file, timestamp) - f = open(tempfile,"w") - prefix = ipmap["prefix"] - for i in range(1,255): - ip = ipmap[i] - if ip.dhcp_pool: - continue - if ip.reserved_by or ip.locked: - meta = OrderedDict() - if ip.reserved_by: - meta['reserved-by'] = ip.reserved_by - if ip.locked: - meta['locked'] = True - print("# " + json.dumps(meta)) - if len(ip.macs) == 0: - macs = impossible_mac - else: - macs = ",".join(ip.macs) - tag = "" - if ip.gw and len(ip.macs) > 0: - tag = ",set:client" + ip.macs[0].replace(":","").lower() - print("{},{}.{}{}".format(macs,prefix,i,tag)) - f.close() + with open(tempfile,"w") as f: + prefix = ipmap["prefix"] + gws = {} + for i in range(1,255): + ip = ipmap[i] + if ip.dhcp_pool: + continue + if ip.reserved_by or ip.locked: + meta = OrderedDict() + if ip.reserved_by: + meta['reserved-by'] = ip.reserved_by + if ip.locked: + meta['locked'] = True + print("# " + json.dumps(meta), file=f) + if len(ip.macs) == 0: + macs = impossible_mac + else: + macs = ",".join(ip.macs) + settag = "" + if ip.gw and len(ip.macs) > 0: + tag = "client" + ip.macs[0].replace(":","").lower() + settag = ",set:" + tag + gws[tag] = ip.gw + print("{},{}.{}{}".format(macs,prefix,i,settag), file=f) shutil.move(tempfile, dhcp_hosts_file) + shutil.copy(dhcp_hosts_file, tempfile) + # Write custom gateways file + tempfile = "{}.{}".format(dhcp_opts_file, timestamp) + with open(tempfile,"w") as f: + for tag in gws: + print( + "tag:{},option:router,{}".format(tag,gws[tag]), + file=f, + ) + shutil.move(tempfile, dhcp_opts_file) + shutil.copy(dhcp_opts_file, tempfile) @app.route("/ip/", methods=['GET', 'POST']) @login_required @@ -347,7 +364,7 @@ def ip(last_byte): dhcp_pool = request.form.get('dhcp-client', '') == 'pool' macs = parse_macs(request.form.get('dhcp-client-mac', '')) gw = request.form.get('dhcp-gw','')[:15] - gw = gw if ip_re.match(gw) else '' + gw = gw if ip_re.match(gw) else None with exclusive_lock: ipmap = load_ips() @@ -360,6 +377,7 @@ def ip(last_byte): gw=gw, ) write_dhcp_files(ipmap) + reload_dnsmasq() return redirect(url_for('ips')) else: diff --git a/reload_freeradius.c b/reload_freeradius.c deleted file mode 100644 index 637c45c..0000000 --- a/reload_freeradius.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main() -{ - //system("/etc/init.d/freeradius reload"); - system("pkill -HUP freeradius"); -} diff --git a/sendhup.cpp b/sendhup.cpp new file mode 100644 index 0000000..aceb10f --- /dev/null +++ b/sendhup.cpp @@ -0,0 +1,13 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc < 2) + return 0; + std::ostringstream s; + s << "pkill -HUP " << argv[1]; + //system("/etc/init.d/freeradius reload"); + system(s.str().c_str()); +}