aboutsummaryrefslogtreecommitdiff
path: root/rpki/publication.py
blob: 1d428bf927d223777d63cc57da140c8761d5fc81 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# $Id$
#
# Copyright (C) 2013--2014  Dragon Research Labs ("DRL")
# Portions copyright (C) 2009--2012  Internet Systems Consortium ("ISC")
# 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 notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND DRL, ISC, AND ARIN DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL DRL,
# ISC, OR 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.

"""
RPKI publication protocol.
"""

import os
import errno
import logging
import rpki.resource_set
import rpki.x509
import rpki.sql
import rpki.exceptions
import rpki.xml_utils
import rpki.http
import rpki.up_down
import rpki.relaxng
import rpki.sundial
import rpki.log

logger = logging.getLogger(__name__)


class publication_namespace(object):
  xmlns = rpki.relaxng.publication.xmlns
  nsmap = rpki.relaxng.publication.nsmap


class base_publication_elt(rpki.xml_utils.base_elt, publication_namespace):
  """
  Base element for publication protocol.  Publish and withdraw PDUs subclass this.
  """

  attributes = ("tag", "uri", "hash")

  tag = None
  uri = None
  der = None
  hash = None
  
  _payload = None

  def __repr__(self):
    return rpki.log.log_repr(self, self.tag, self.uri, self.hash, self.payload)

  @property
  def payload(self):
    if self._payload is None and self.der is not None:
      self._payload = rpki.x509.uri_dispatch(self.uri)(DER = self.der)
    return self._payload

  def raise_if_error(self):
    """
    No-op unless this is a <report_error/> PDU.
    """

    pass


class publish_elt(base_publication_elt):
  """
  <publish/> element.
  """

  element_name = "publish"

  def endElement(self, stack, name, text):
    """
    Handle reading of the object to be published
    """

    assert name == self.element_name, "Unexpected name %s, stack %s" % (name, stack)
    if text:
      self.der = text.decode("base64")
    stack.pop()

  def toXML(self):
    """
    Generate XML element for publishable object.
    """

    elt = self.make_elt()
    if self.der is not None:
      elt.text = self.der.encode("base64")
    return elt

  def serve_action(self, delta):
    """
    Publish an object.
    """

    logger.info("Publishing %s", self.payload.tracking_data(self.uri))
    delta.publish(self.client, self.der, self.uri, self.hash)

    # The rest of this shouldn't happen until after the SQL commit
    filename = self.gctx.uri_to_filename(self.uri)
    filename_tmp = filename + ".tmp"
    dirname = os.path.dirname(filename)
    if not os.path.isdir(dirname):
      os.makedirs(dirname)
    with open(filename_tmp, "wb") as f:
      f.write(self.der)
    os.rename(filename_tmp, filename)


class withdraw_elt(base_publication_elt):
  """
  <withdraw/> element.
  """

  element_name = "withdraw"

  def serve_action(self, delta):
    """
    Withdraw an object, then recursively delete empty directories.
    """

    logger.info("Withdrawing %s", self.uri)
    delta.withdraw(self.client, self.uri, self.hash)

    # The rest of this shouldn't happen until after the SQL commit
    filename = self.gctx.uri_to_filename(self.uri)
    try:
      os.remove(filename)
    except OSError, e:
      if e.errno == errno.ENOENT:
        raise rpki.exceptions.NoObjectAtURI("No object published at %s" % self.uri)
      else:
        raise
    min_path_len = len(self.gctx.publication_base.rstrip("/"))
    dirname = os.path.dirname(filename)
    while len(dirname) > min_path_len:
      try:
        os.rmdir(dirname)
      except OSError:
        break
      else:
        dirname = os.path.dirname(dirname)


class list_elt(base_publication_elt):
  """
  <list/> element.
  """

  element_name = "list"


class report_error_elt(rpki.xml_utils.text_elt, publication_namespace):
  """
  <report_error/> element.
  """

  element_name = "report_error"
  attributes = ("tag", "error_code")
  text_attribute = "error_text"

  error_code = None
  error_text = None

  def __repr__(self):
    return rpki.log.log_repr(self, self.error_code, self.error_text)

  @classmethod
  def from_exception(cls, e, tag = None):
    """
    Generate a <report_error/> element from an exception.
    """

    self = cls()
    self.tag = tag
    self.error_code = e.__class__.__name__
    self.error_text = str(e)
    return self

  def __str__(self):
    s = ""
    if getattr(self, "tag", None) is not None:
      s += "[%s] " % self.tag
    s += self.error_code
    if getattr(self, "error_text", None) is not None:
      s += ": " + self.error_text
    return s

  def raise_if_error(self):
    """
    Raise exception associated with this <report_error/> PDU.
    """

    try:
      e = getattr(rpki.exceptions, self.error_code)
      if issubclass(e, rpki.exceptions.RPKI_Exception):
        raise e(getattr(self, "text", None))
    except (TypeError, AttributeError):
      pass
    raise rpki.exceptions.BadPublicationReply("Unexpected response from pubd: %s" % self)


class msg(rpki.xml_utils.msg, publication_namespace):
  """
  Publication PDU.
  """

  ## @var version
  # Protocol version
  version = int(rpki.relaxng.publication.version)

  ## @var pdus
  # Dispatch table of PDUs for this protocol.
  pdus = dict((x.element_name, x) for x in (publish_elt, withdraw_elt, report_error_elt))


class sax_handler(rpki.xml_utils.sax_handler):
  """
  SAX handler for publication protocol.
  """

  pdu = msg
  name = "msg"
  version = rpki.relaxng.publication.version


class cms_msg(rpki.x509.XML_CMS_object):
  """
  Class to hold a CMS-signed publication PDU.
  """

  encoding = "us-ascii"
  schema = rpki.relaxng.publication
  saxify = sax_handler.saxify