Add samba.ensure_third_party_module() function, loading external python modules from...
[samba.git] / lib / dnspython / dns / rdtypes / ANY / DNSKEY.py
1 # Copyright (C) 2004-2007, 2009-2011 Nominum, Inc.
2 #
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
7 #
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16
17 import struct
18
19 import dns.exception
20 import dns.dnssec
21 import dns.rdata
22
23 # flag constants
24 SEP = 0x0001
25 REVOKE = 0x0080
26 ZONE = 0x0100
27
28 class DNSKEY(dns.rdata.Rdata):
29     """DNSKEY record
30
31     @ivar flags: the key flags
32     @type flags: int
33     @ivar protocol: the protocol for which this key may be used
34     @type protocol: int
35     @ivar algorithm: the algorithm used for the key
36     @type algorithm: int
37     @ivar key: the public key
38     @type key: string"""
39
40     __slots__ = ['flags', 'protocol', 'algorithm', 'key']
41
42     def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key):
43         super(DNSKEY, self).__init__(rdclass, rdtype)
44         self.flags = flags
45         self.protocol = protocol
46         self.algorithm = algorithm
47         self.key = key
48
49     def to_text(self, origin=None, relativize=True, **kw):
50         return '%d %d %d %s' % (self.flags, self.protocol, self.algorithm,
51                                 dns.rdata._base64ify(self.key))
52
53     def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
54         flags = tok.get_uint16()
55         protocol = tok.get_uint8()
56         algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
57         chunks = []
58         while 1:
59             t = tok.get().unescape()
60             if t.is_eol_or_eof():
61                 break
62             if not t.is_identifier():
63                 raise dns.exception.SyntaxError
64             chunks.append(t.value)
65         b64 = ''.join(chunks)
66         key = b64.decode('base64_codec')
67         return cls(rdclass, rdtype, flags, protocol, algorithm, key)
68
69     from_text = classmethod(from_text)
70
71     def to_wire(self, file, compress = None, origin = None):
72         header = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
73         file.write(header)
74         file.write(self.key)
75
76     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
77         if rdlen < 4:
78             raise dns.exception.FormError
79         header = struct.unpack('!HBB', wire[current : current + 4])
80         current += 4
81         rdlen -= 4
82         key = wire[current : current + rdlen].unwrap()
83         return cls(rdclass, rdtype, header[0], header[1], header[2],
84                    key)
85
86     from_wire = classmethod(from_wire)
87
88     def _cmp(self, other):
89         hs = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
90         ho = struct.pack("!HBB", other.flags, other.protocol, other.algorithm)
91         v = cmp(hs, ho)
92         if v == 0:
93             v = cmp(self.key, other.key)
94         return v