# mini standalone http server die spullen uitdeelt enigsinds zoals de node # factory ze wenst op te halen # # $ python server.py 8000 # # start 'em op poort 8000 import os import BaseHTTPServer from common import * import dhcpd import motd import named import nodelist import penconf import rcnodelocal import resolv import snmpd import sshkeys import dns root = xml_to_obj_hierarchy("nodes.xml") ready_nodes_dtd_hierarchy(root) params = {} os = [ 'FreeBSD' ] versions = [ '5.0', '5.3' ] # generators die node specifiek zijn node_generators = { "dhcpd.conf": dhcpd.gen_dhcpd_conf, "motd": lambda n: motd.gen_motd(n, params['os'], params['version']), "named.conf": named.gen_named_conf, "rc.node.local": lambda n: rcnodelocal.gen_rcnodelocal( n, params['version']), "resolv.conf": resolv.gen_resolv_conf, "snmpd.local.conf": snmpd.gen_snmpd_local_conf, "pen.sh": lambda n: penconf.gen_pen_sh(root, n) } def configfilelist(root = None): return "\n" + ' '.join(node_generators.keys()) def dnsfilelist(root = None): return "\n" + ' '.join(general_generators.keys()) # generators die als parameter de root ("nodes") nemen general_generators = { "authorized_keys": sshkeys.rsakeys_hard, "authorized_keys2": sshkeys.dsakeys_hard, "wleiden.net": dns.wleiden_net, "16.172.in-addr.arpa": dns.interlink_zone, #"authorized_keys": sshkeys.rsakeys_from_xml, #"authorized_keys2": sshkeys.dsakeys_from_xml, "configfilelist": configfilelist, "dnsfilelist": dnsfilelist, "nodelist": nodelist.gen_nodelist } for i in range(17, 32): general_generators["%d.172.in-addr.arpa" % i] = lambda r, i = i: dns.reverse_zone(r, i) class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): clear_header_cache() self.path = self.path[1:] if general_generators.has_key(self.path): self.send_response(200) self.wfile.write("\n" + general_generators[self.path](root)) return # kijk of er parameters zijn gegeven en splits die af in # een dict t = self.path.split('?') self.path = t[0] if len(t) > 1: for p in t[1].split('&'): t = p.split('=') params[t[0]] = t[1] # nu wat validatie if not node_generators.has_key(self.path): self.send_response(404, "Unknown file") return if not params.has_key('node'): self.send_response(404, "Please specify node") return try: node = findnode(root, params['node']) except: self.send_response(404, "Unknown node") return self.send_response(200) if not params.has_key('version'): self.send_response(404, "Plese specify version") return if not params['version'] in versions: self.send_response(404, "Incorrect version number") return if not params.has_key('os'): self.send_response(404, "Plese specify os") return if not params['os'] in os: self.send_response(404, "Incorrect os") return self.wfile.write("\n" + node_generators[self.path](node)) if len(sys.argv) > 1: server_address = ('', int(sys.argv[1])) else: server_address = ('', 8000) httpd = BaseHTTPServer.HTTPServer(server_address, RequestHandler) httpd.serve_forever()