# $Id$ """ RPKI engine daemon. This is still very much a work in progress. Usage: python rpkid.py [ { -c | --config } configfile ] [ { -h | --help } ] Default configuration file is re.conf, override with --config option. """ import traceback, os, time, getopt, sys import tlslite.api, MySQLdb, xml.sax, lxml.etree, lxml.sax, POW, POW.pkix import rpki.https, rpki.config, rpki.resource_set, rpki.up_down, rpki.left_right, rpki.relaxng import rpki.cms, rpki.exceptions, rpki.x509 def left_right_handler(query, path): try: q_elt = rpki.cms.xml_verify(query, gctx.cms_ta_irbe) rpki.relaxng.left_right.assertValid(q_elt) q_msg = rpki.left_right.sax_handler.saxify(q_elt) r_msg = q_msg.serve_top_level(gctx) r_elt = r_msg.toXML() rpki.relaxng.left_right.assertValid(r_elt) return 200, rpki.cms.xml_sign(r_elt, gctx.cms_key, gctx.cms_certs) except Exception, data: traceback.print_exc() return 500, "Unhandled exception %s" % data def up_down_handler(query, path): try: child_id = path.partition("/up-down/")[2] if not child_id.isdigit(): raise rpki.exceptions.BadContactURL, "Bad path: %s" % path child = rpki.left_right.child_elt.sql_fetch(gctx.db, gctx.cur, long(child_id)) if child is None: raise rpki.exceptions.NotFound, "Could not find child %s" % child_id return 200, child.serve_up_down(gctx, query) except Exception, data: traceback.print_exc() return 400, "Could not process PDU: %s" % data def cronjob_handler(query, path): for s in rpki.left_right.self_elt.sql_fetch_all(gctx): s.client_poll(gctx) #raise rpki.exceptions.NotImplementedYet class global_context(object): """A place to stash various global parameters.""" pass os.environ["TZ"] = "UTC" time.tzset() cfg_file = "re.conf" opts,argv = getopt.getopt(sys.argv[1:], "c:h?", ["config=", "help"]) for o,a in opts: if o in ("-h", "--help", "-?"): print __doc__ sys.exit(0) if o in ("-c", "--config"): cfg_file = a if argv: raise RuntimeError, "Unexpected arguments %s" % argv gctx = global_context() gctx.cfg = rpki.config.parser(cfg_file) gctx.cfg_section = "rpki" gctx.db = MySQLdb.connect(user = gctx.cfg.get(gctx.cfg_section, "sql-username"), db = gctx.cfg.get(gctx.cfg_section, "sql-database"), passwd = gctx.cfg.get(gctx.cfg_section, "sql-password")) gctx.cur = gctx.db.cursor() gctx.cms_ta_irdb = rpki.x509.X509(Auto_file = gctx.cfg.get(gctx.cfg_section, "cms-ta-irdb")) gctx.cms_ta_irbe = rpki.x509.X509(Auto_file = gctx.cfg.get(gctx.cfg_section, "cms-ta-irbe")) gctx.cms_key = rpki.x509.RSA(Auto_file = gctx.cfg.get(gctx.cfg_section, "cms-key")) gctx.cms_certs = rpki.x509.X509_chain(Auto_files = gctx.cfg.multiget(gctx.cfg_section, "cms-cert")) gctx.https_key = rpki.x509.RSA(Auto_file = gctx.cfg.get(gctx.cfg_section, "https-key")) gctx.https_certs = rpki.x509.X509_chain(Auto_files = gctx.cfg.multiget(gctx.cfg_section, "https-cert")) gctx.https_tas = rpki.x509.X509_chain(Auto_files = gctx.cfg.multiget(gctx.cfg_section, "https-ta")) gctx.irdb_url = gctx.cfg.get(gctx.cfg_section, "irdb-url") rpki.https.server(privateKey = gctx.https_key, certChain = gctx.https_certs, port = int(gctx.cfg.get(gctx.cfg_section, "server-port", "4433")), handlers=(("/left-right", left_right_handler), ("/up-down/", up_down_handler), ("/cronjob", cronjob_handler)))