aboutsummaryrefslogtreecommitdiff
path: root/scripts/irdb.py
blob: b03013cfd85006b2fcbac331d7684c3ded7f3914 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# $Id$

import rpki.https, tlslite.api, rpki.config, rpki.resource_set, MySQLdb

def handler(query, path):
  try:
    q_xml = rpki.cms.decode(query, cms_ta)
    print q_xml
    q_elt = lxml.etree.fromstring(q_xml)
    rng.assertValid(q_elt)
    saxer = rpki.left_right.sax_handler()
    lxml.sax.saxify(q_elt, saxer)
    q_msg = saxer.result
    assert instanceof(q_msg, rpki.left_right.msg)
    r_msg = rpki.left_right.msg()
    for q_pdu in q_msg:
      assert isinstance(q_pdu, rpki.left_right.list_resources_elt) and \
             q_pdu.type == "query" and \
             len(q_pdu.resources) == 0

      org_id = q_pdu.child_id
      if org_id is None:
        org_id = q_pdu.self_id
      cur.execute("""SELECT resource_class_id, subject_name
                     FROM registrant, resource_class
                     WHERE registrant.IRBE_mapped_id = '%s'
                     AND   registrant.registrant_id = resource_class.registrant_id
                  """ % org_id)
      resource_classes = cur.fetchall()

      r_pdu = rpki.left_right.list_resources_elt()
      r_pdu.type = "reply"
      r_pdu.self_id = q_pdu.self_id
      r_pdu.child_id = q_pdu.child_id

      # Hmm, I screwed up when I described this table to Tim,
      # valid_until should be on the top-level "registrant" table, not
      # the "resource_class" table.  It's an optional attribute in the
      # XML so maybe just punt it for now.

      for resource_class_id, subject_name in resource_classes:
        resource_class = rpki.left_right.resource_class_elt()
        if subject_name:
          resource_class.subject_name = subject_name

        cur.execute("""SELECT start_as, end_as FROM asn
                       WHERE resource_class_id = '%s'
                    """ % resource_class_id)
        resource_class.as = rpki.resource_set.resource_set_as()
        for b,e = cur.fetchall():
          resource_class.as.append(rpki.resource_set.resource_range_as(b, e))

        cur.execute("""SELECT start_ip, end_ip FROM net
                       WHERE resource_class_id = '%s' AND version = 4
                    """ % resource_class_id)
        resource_class.ipv4 = rpki.resource_set.resource_set_ipv4()
        for b,e = cur.fetchall():
          resource_class.ipv4.append(rpki.resource_set.resource_range_ipv4(b, e))

        cur.execute("""SELECT start_ip, end_ip FROM net
                       WHERE resource_class_id = '%s' AND version = 6
                    """ % resource_class_id)
        resource_class.ipv6 = rpki.resource_set.resource_set_ipv6()
        for b,e = cur.fetchall():
          resource_class.ipv6.append(rpki.resource_set.resource_range_ipv6(b, e))

        r_pdu.resources.append(resource_class)

    assert False, "Not finished"

    return 200, "Something more useful than this string, please"

  except Exception, data:
    # This should generate a <report_error/> PDU, but this will do for initial debugging
    return 500, "Unhandled exception %s" % data

cfg = rpki.config.parser("irbe.conf")
section = "irdb"

db = MySQLdb.connect(user   = cfg.get(section, "username"),
                     db     = cfg.get(section, "database"),
                     passwd = cfg.get(section, "password"))

cur = db.cursor()

cms_ta = cfg.get("cms-peer")

privateKey = rpki.x509.RSA_Keypair(PEM_file = cfg.get(section, "https-key"))

certChain = rpki.x509.X509_chain()
certChain.load_from_PEM(cfg.multiget(section, "https-cert"))

rpki.https.server(privateKey=privateKey, certChain=certChain, handlers={"/" : handler})