aboutsummaryrefslogtreecommitdiff
path: root/rpkid/rpki/pubd.py
blob: 513c1c3ed99b986efc1064140d01dbd5c68d1237 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
RPKI publication engine.

Usage: python pubd.py [ { -c | --config } configfile ]
                      [ { -h | --help } ]
                      [ { -p | --profile } outputfile ]

$Id$

Copyright (C) 2009--2010  Internet Systems Consortium ("ISC")

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

Portions copyright (C) 2007--2008  American Registry for Internet Numbers ("ARIN")

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND ARIN DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS.  IN NO EVENT SHALL ARIN BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""

import os, time, getopt, sys, re
import rpki.resource_set, rpki.up_down, rpki.x509, rpki.sql
import rpki.http, rpki.config, rpki.exceptions, rpki.relaxng
import rpki.log, rpki.publication

class main(object):
  """
  Main program for pubd.
  """

  def __init__(self):

    os.environ["TZ"] = "UTC"
    time.tzset()

    self.cfg_file = None
    self.profile = False

    opts, argv = getopt.getopt(sys.argv[1:], "c:dhp:?", ["config=", "debug", "help"])
    for o, a in opts:
      if o in ("-h", "--help", "-?"):
        print __doc__
        sys.exit(0)
      elif o in ("-c", "--config"):
        self.cfg_file = a
      elif o in ("-d", "--debug"):
        rpki.log.use_syslog = False
      elif o in ("-p", "--profile"):
        self.profile = a
    if argv:
      raise rpki.exceptions.CommandParseFailure, "Unexpected arguments %s" % argv

    rpki.log.init("pubd")

    if self.profile:
      import cProfile
      cProfile.run("self.main()", self.profile)
    else:
      self.main()

  def main(self):

    self.cfg = rpki.config.parser(self.cfg_file, "pubd")

    if self.profile:
      rpki.log.info("Running in profile mode with output to %s" % self.profile)

    self.cfg.set_global_flags()

    self.sql = rpki.sql.session(self.cfg)

    self.bpki_ta   = rpki.x509.X509(Auto_update = self.cfg.get("bpki-ta"))
    self.irbe_cert = rpki.x509.X509(Auto_update = self.cfg.get("irbe-cert"))
    self.pubd_cert = rpki.x509.X509(Auto_update = self.cfg.get("pubd-cert"))
    self.pubd_key  = rpki.x509.RSA( Auto_update = self.cfg.get("pubd-key"))

    self.http_server_host = self.cfg.get("server-host", "")
    self.http_server_port = int(self.cfg.get("server-port", "4434"))

    self.publication_base = self.cfg.get("publication-base", "publication/")

    self.publication_multimodule = self.cfg.getboolean("publication-multimodule", False)

    rpki.http.server(
      host                          = self.http_server_host,
      port                          = self.http_server_port,
      handlers                      = (("/control", self.control_handler),
                                       ("/client/", self.client_handler)))

  def handler_common(self, query, client, cb, certs, crl = None):
    """
    Common PDU handler code.
    """

    def done(r_msg):
      reply = rpki.publication.cms_msg().wrap(r_msg, self.pubd_key, self.pubd_cert, crl)
      self.sql.sweep()
      cb(reply)

    q_msg = rpki.publication.cms_msg(DER = query).unwrap(certs)
    q_msg.serve_top_level(self, client, done)

  def control_handler(self, query, path, cb):
    """
    Process one PDU from the IRBE.
    """

    def done(body):
      cb(200, body = body)

    rpki.log.trace()
    try:
      self.sql.ping()
      self.handler_common(query, None, done, (self.bpki_ta, self.irbe_cert))
    except (rpki.async.ExitNow, SystemExit):
      raise
    except Exception, data:
      rpki.log.traceback()
      cb(500, reason = "Unhandled exception %s" % data)

  client_url_regexp = re.compile("/client/([-A-Z0-9_/]+)$", re.I)

  def client_handler(self, query, path, cb):
    """
    Process one PDU from a client.
    """

    def done(body):
      cb(200, body = body)

    rpki.log.trace()
    try:
      self.sql.ping()
      match = self.client_url_regexp.search(path)
      if match is None:
        raise rpki.exceptions.BadContactURL, "Bad path: %s" % path
      client_handle = match.group(1)
      client = rpki.publication.client_elt.sql_fetch_where1(self, "client_handle = %s", (client_handle,))
      if client is None:
        raise rpki.exceptions.ClientNotFound, "Could not find client %s" % client_handle
      config = rpki.publication.config_elt.fetch(self)
      if config is None or config.bpki_crl is None:
        raise rpki.exceptions.CMSCRLNotSet
      self.handler_common(query, client, done, (self.bpki_ta, client.bpki_cert, client.bpki_glue), config.bpki_crl)
    except (rpki.async.ExitNow, SystemExit):
      raise
    except Exception, data:
      rpki.log.traceback()
      cb(500, reason = "Could not process PDU: %s" % data)