1 # Copyright (C) 2003-2007, 2009 Nominum, Inc.
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.
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.
16 """DNS Dynamic Update Support"""
25 class Update(dns.message.Message):
26 def __init__(self, zone, rdclass=dns.rdataclass.IN, keyring=None,
28 """Initialize a new DNS Update object.
30 @param zone: The zone which is being updated.
31 @type zone: A dns.name.Name or string
32 @param rdclass: The class of the zone; defaults to dns.rdataclass.IN.
33 @type rdclass: An int designating the class, or a string whose value
34 is the name of a class.
35 @param keyring: The TSIG keyring to use; defaults to None.
37 @param keyname: The name of the TSIG key to use; defaults to None.
38 The key must be defined in the keyring. If a keyring is specified
39 but a keyname is not, then the key used will be the first key in the
40 keyring. Note that the order of keys in a dictionary is not defined,
41 so applications should supply a keyname when a keyring is used, unless
42 they know the keyring contains only one key.
43 @type keyname: dns.name.Name or string
45 super(Update, self).__init__()
46 self.flags |= dns.opcode.to_flags(dns.opcode.UPDATE)
47 if isinstance(zone, (str, unicode)):
48 zone = dns.name.from_text(zone)
50 if isinstance(rdclass, str):
51 rdclass = dns.rdataclass.from_text(rdclass)
52 self.zone_rdclass = rdclass
53 self.find_rrset(self.question, self.origin, rdclass, dns.rdatatype.SOA,
54 create=True, force_unique=True)
55 if not keyring is None:
56 self.use_tsig(keyring, keyname)
58 def _add_rr(self, name, ttl, rd, deleting=None, section=None):
59 """Add a single RR to the update section."""
62 section = self.authority
64 rrset = self.find_rrset(section, name, self.zone_rdclass, rd.rdtype,
65 covers, deleting, True, True)
68 def _add(self, replace, section, name, *args):
69 """Add records. The first argument is the replace mode. If
70 false, RRs are added to an existing RRset; if true, the RRset
71 is replaced with the specified contents. The second
72 argument is the section to add to. The third argument
73 is always a name. The other arguments can be:
79 - ttl, rdtype, string..."""
81 if isinstance(name, (str, unicode)):
82 name = dns.name.from_text(name, None)
83 if isinstance(args[0], dns.rdataset.Rdataset):
86 self.delete(name, rds.rdtype)
88 self._add_rr(name, rds.ttl, rd, section=section)
91 ttl = int(args.pop(0))
92 if isinstance(args[0], dns.rdata.Rdata):
94 self.delete(name, args[0].rdtype)
96 self._add_rr(name, ttl, rd, section=section)
99 if isinstance(rdtype, str):
100 rdtype = dns.rdatatype.from_text(rdtype)
102 self.delete(name, rdtype)
104 rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s,
106 self._add_rr(name, ttl, rd, section=section)
108 def add(self, name, *args):
109 """Add records. The first argument is always a name. The other
116 - ttl, rdtype, string..."""
117 self._add(False, self.authority, name, *args)
119 def delete(self, name, *args):
120 """Delete records. The first argument is always a name. The other
129 - rdtype, [string...]"""
131 if isinstance(name, (str, unicode)):
132 name = dns.name.from_text(name, None)
134 rrset = self.find_rrset(self.authority, name, dns.rdataclass.ANY,
135 dns.rdatatype.ANY, dns.rdatatype.NONE,
136 dns.rdatatype.ANY, True, True)
137 elif isinstance(args[0], dns.rdataset.Rdataset):
140 self._add_rr(name, 0, rd, dns.rdataclass.NONE)
143 if isinstance(args[0], dns.rdata.Rdata):
145 self._add_rr(name, 0, rd, dns.rdataclass.NONE)
148 if isinstance(rdtype, str):
149 rdtype = dns.rdatatype.from_text(rdtype)
151 rrset = self.find_rrset(self.authority, name,
152 self.zone_rdclass, rdtype,
158 rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s,
160 self._add_rr(name, 0, rd, dns.rdataclass.NONE)
162 def replace(self, name, *args):
163 """Replace records. The first argument is always a name. The other
170 - ttl, rdtype, string...
172 Note that if you want to replace the entire node, you should do
173 a delete of the name followed by one or more calls to add."""
175 self._add(True, self.authority, name, *args)
177 def present(self, name, *args):
178 """Require that an owner name (and optionally an rdata type,
179 or specific rdataset) exists as a prerequisite to the
180 execution of the update. The first argument is always a name.
181 The other arguments can be:
187 - rdtype, string..."""
189 if isinstance(name, (str, unicode)):
190 name = dns.name.from_text(name, None)
192 rrset = self.find_rrset(self.answer, name,
193 dns.rdataclass.ANY, dns.rdatatype.ANY,
194 dns.rdatatype.NONE, None,
196 elif isinstance(args[0], dns.rdataset.Rdataset) or \
197 isinstance(args[0], dns.rdata.Rdata) or \
203 self._add(False, self.answer, name, *args)
206 if isinstance(rdtype, str):
207 rdtype = dns.rdatatype.from_text(rdtype)
208 rrset = self.find_rrset(self.answer, name,
209 dns.rdataclass.ANY, rdtype,
210 dns.rdatatype.NONE, None,
213 def absent(self, name, rdtype=None):
214 """Require that an owner name (and optionally an rdata type) does
215 not exist as a prerequisite to the execution of the update."""
217 if isinstance(name, (str, unicode)):
218 name = dns.name.from_text(name, None)
220 rrset = self.find_rrset(self.answer, name,
221 dns.rdataclass.NONE, dns.rdatatype.ANY,
222 dns.rdatatype.NONE, None,
225 if isinstance(rdtype, str):
226 rdtype = dns.rdatatype.from_text(rdtype)
227 rrset = self.find_rrset(self.answer, name,
228 dns.rdataclass.NONE, rdtype,
229 dns.rdatatype.NONE, None,
232 def to_wire(self, origin=None, max_size=65535):
233 """Return a string containing the update in DNS compressed wire
238 return super(Update, self).to_wire(origin, max_size)