#!/usr/bin/env python import sys, logging import tftpy import re import signal 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: continue if not in_lease and parts[0] == "lease": in_lease = parts[1] continue 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(): def sighup_handler(signum, frame): signal.siginterrupt(signal.SIGHUP, False) sys.stderr.write("Reloading config...\n") sys.stderr.flush() file_hooks = {} reload(hooks) signal.signal(signal.SIGHUP, sighup_handler) server = tftpy.TftpServer("/var/khepri/tftp-root/", dyn_file_func=hook_dispatcher) try: server.listen("10.8.1.1", 5000) except tftpy.TftpException, err: sys.stderr.write("%s\n" % str(err)) sys.exit(1) except KeyboardInterrupt: pass