This repository has been archived on 2023-10-10. You can view files and clone it, but cannot push or open issues/pull-requests.

71 lines
1.9 KiB
Raw Normal View History

2012-11-14 14:10:39 +00:00
#!/usr/bin/env python
import sys, logging
import tftpy
import re
2012-11-14 14:39:58 +00:00
import signal
2012-11-14 14:10:39 +00:00
LEASES_FILE = "/var/lib/dhcp/dhcpd.leases"
def get_mac(address):
"""Gets MAC address from dhcpd leases based on IP address."""
f = open(LEASES_FILE,"r")
in_lease = False
for line in f:
line = line.split("#")[0]
parts = line.strip().split()
if not parts:
if not in_lease and parts[0] == "lease":
in_lease = parts[1]
field_name = parts[0]
if field_name == "hardware" and in_lease == address:
return parts[2][:-1]
elif field_name == "}":
in_lease = False
return None
file_hooks = {}
def hook_dispatcher(path, remote):
for pattern, hook in file_hooks.iteritems():
match = re.match(pattern, path)
if match:
data = hook(path, match, remote)
if data:
return data
return None
def hook(pattern, match_mac=None):
def decorator(f):
def wrapper(path, match, remote):
if match_mac:
mac = get_mac([0])
if match_mac and mac != match_mac:
return None
return f(path, match, remote)
file_hooks[pattern] = wrapper
return f
return decorator
import hooks
def main():
2012-11-14 14:39:58 +00:00
def sighup_handler(signum, frame):
signal.siginterrupt(signal.SIGHUP, False)
sys.stderr.write("Reloading config...\n")
file_hooks = {}
signal.signal(signal.SIGHUP, sighup_handler)
2012-11-14 14:10:39 +00:00
server = tftpy.TftpServer("/var/khepri/tftp-root/", dyn_file_func=hook_dispatcher)
server.listen("", 5000)
except tftpy.TftpException, err:
sys.stderr.write("%s\n" % str(err))
except KeyboardInterrupt: