| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
"""DNS Dynamic Update Support""" |
| 19 |
|
| 20 |
from typing import Any, List |
| 21 |
|
| 22 |
import dns.enum |
| 23 |
import dns.exception |
| 24 |
import dns.message |
| 25 |
import dns.name |
| 26 |
import dns.opcode |
| 27 |
import dns.rdata |
| 28 |
import dns.rdataclass |
| 29 |
import dns.rdataset |
| 30 |
import dns.rdatatype |
| 31 |
import dns.rrset |
| 32 |
import dns.tsig |
| 33 |
|
| 34 |
|
| 35 |
class UpdateSection(dns.enum.IntEnum): |
| 36 |
"""Update sections""" |
| 37 |
|
| 38 |
ZONE = 0 |
| 39 |
PREREQ = 1 |
| 40 |
UPDATE = 2 |
| 41 |
ADDITIONAL = 3 |
| 42 |
|
| 43 |
@classmethod |
| 44 |
def _maximum(cls): |
| 45 |
return 3 |
| 46 |
|
| 47 |
|
| 48 |
class UpdateMessage(dns.message.Message): # lgtm[py/missing-equals] |
| 49 |
# ignore the mypy error here as we mean to use a different enum |
| 50 |
_section_enum = UpdateSection # type: ignore |
| 51 |
|
| 52 |
def __init__( |
| 53 |
self, |
| 54 |
zone: dns.name.Name | str | None = None, |
| 55 |
rdclass: dns.rdataclass.RdataClass = dns.rdataclass.IN, |
| 56 |
keyring: Any | None = None, |
| 57 |
keyname: dns.name.Name | None = None, |
| 58 |
keyalgorithm: dns.name.Name | str = dns.tsig.default_algorithm, |
| 59 |
id: int | None = None, |
| 60 |
): |
| 61 |
"""Initialize a new DNS Update object. |
| 62 |
|
| 63 |
See the documentation of the Message class for a complete |
| 64 |
description of the keyring dictionary. |
| 65 |
|
| 66 |
*zone*, a ``dns.name.Name``, ``str``, or ``None``, the zone |
| 67 |
which is being updated. ``None`` should only be used by dnspython's |
| 68 |
message constructors, as a zone is required for the convenience |
| 69 |
methods like ``add()``, ``replace()``, etc. |
| 70 |
|
| 71 |
*rdclass*, an ``int`` or ``str``, the class of the zone. |
| 72 |
|
| 73 |
The *keyring*, *keyname*, and *keyalgorithm* parameters are passed to |
| 74 |
``use_tsig()``; see its documentation for details. |
| 75 |
""" |
| 76 |
super().__init__(id=id) |
| 77 |
self.flags |= dns.opcode.to_flags(dns.opcode.UPDATE) |
| 78 |
if isinstance(zone, str): |
| 79 |
zone = dns.name.from_text(zone) |
| 80 |
self.origin = zone |
| 81 |
rdclass = dns.rdataclass.RdataClass.make(rdclass) |
| 82 |
self.zone_rdclass = rdclass |
| 83 |
if self.origin: |
| 84 |
self.find_rrset( |
| 85 |
self.zone, |
| 86 |
self.origin, |
| 87 |
rdclass, |
| 88 |
dns.rdatatype.SOA, |
| 89 |
create=True, |
| 90 |
force_unique=True, |
| 91 |
) |
| 92 |
if keyring is not None: |
| 93 |
self.use_tsig(keyring, keyname, algorithm=keyalgorithm) |
| 94 |
|
| 95 |
@property |
| 96 |
def zone(self) -> List[dns.rrset.RRset]: |
| 97 |
"""The zone section.""" |
| 98 |
return self.sections[0] |
| 99 |
|
| 100 |
@zone.setter |
| 101 |
def zone(self, v): |
| 102 |
self.sections[0] = v |
| 103 |
|
| 104 |
@property |
| 105 |
def prerequisite(self) -> List[dns.rrset.RRset]: |
| 106 |
"""The prerequisite section.""" |
| 107 |
return self.sections[1] |
| 108 |
|
| 109 |
@prerequisite.setter |
| 110 |
def prerequisite(self, v): |
| 111 |
self.sections[1] = v |
| 112 |
|
| 113 |
@property |
| 114 |
def update(self) -> List[dns.rrset.RRset]: |
| 115 |
"""The update section.""" |
| 116 |
return self.sections[2] |
| 117 |
|
| 118 |
@update.setter |
| 119 |
def update(self, v): |
| 120 |
self.sections[2] = v |
| 121 |
|
| 122 |
def _add_rr(self, name, ttl, rd, deleting=None, section=None): |
| 123 |
"""Add a single RR to the update section.""" |
| 124 |
|
| 125 |
if section is None: |
| 126 |
section = self.update |
| 127 |
covers = rd.covers() |
| 128 |
rrset = self.find_rrset( |
| 129 |
section, name, self.zone_rdclass, rd.rdtype, covers, deleting, True, True |
| 130 |
) |
| 131 |
rrset.add(rd, ttl) |
| 132 |
|
| 133 |
def _add(self, replace, section, name, *args): |
| 134 |
"""Add records. |
| 135 |
|
| 136 |
*replace* is the replacement mode. If ``False``, |
| 137 |
RRs are added to an existing RRset; if ``True``, the RRset |
| 138 |
is replaced with the specified contents. The second |
| 139 |
argument is the section to add to. The third argument |
| 140 |
is always a name. The other arguments can be: |
| 141 |
|
| 142 |
- rdataset... |
| 143 |
|
| 144 |
- ttl, rdata... |
| 145 |
|
| 146 |
- ttl, rdtype, string... |
| 147 |
""" |
| 148 |
|
| 149 |
if isinstance(name, str): |
| 150 |
name = dns.name.from_text(name, None) |
| 151 |
if isinstance(args[0], dns.rdataset.Rdataset): |
| 152 |
for rds in args: |
| 153 |
if replace: |
| 154 |
self.delete(name, rds.rdtype) |
| 155 |
for rd in rds: |
| 156 |
self._add_rr(name, rds.ttl, rd, section=section) |
| 157 |
else: |
| 158 |
args = list(args) |
| 159 |
ttl = int(args.pop(0)) |
| 160 |
if isinstance(args[0], dns.rdata.Rdata): |
| 161 |
if replace: |
| 162 |
self.delete(name, args[0].rdtype) |
| 163 |
for rd in args: |
| 164 |
self._add_rr(name, ttl, rd, section=section) |
| 165 |
else: |
| 166 |
rdtype = dns.rdatatype.RdataType.make(args.pop(0)) |
| 167 |
if replace: |
| 168 |
self.delete(name, rdtype) |
| 169 |
for s in args: |
| 170 |
rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s, self.origin) |
| 171 |
self._add_rr(name, ttl, rd, section=section) |
| 172 |
|
| 173 |
def add(self, name: dns.name.Name | str, *args: Any) -> None: |
| 174 |
"""Add records. |
| 175 |
|
| 176 |
The first argument is always a name. The other |
| 177 |
arguments can be: |
| 178 |
|
| 179 |
- rdataset... |
| 180 |
|
| 181 |
- ttl, rdata... |
| 182 |
|
| 183 |
- ttl, rdtype, string... |
| 184 |
""" |
| 185 |
|
| 186 |
self._add(False, self.update, name, *args) |
| 187 |
|
| 188 |
def delete(self, name: dns.name.Name | str, *args: Any) -> None: |
| 189 |
"""Delete records. |
| 190 |
|
| 191 |
The first argument is always a name. The other |
| 192 |
arguments can be: |
| 193 |
|
| 194 |
- *empty* |
| 195 |
|
| 196 |
- rdataset... |
| 197 |
|
| 198 |
- rdata... |
| 199 |
|
| 200 |
- rdtype, [string...] |
| 201 |
""" |
| 202 |
|
| 203 |
if isinstance(name, str): |
| 204 |
name = dns.name.from_text(name, None) |
| 205 |
if len(args) == 0: |
| 206 |
self.find_rrset( |
| 207 |
self.update, |
| 208 |
name, |
| 209 |
dns.rdataclass.ANY, |
| 210 |
dns.rdatatype.ANY, |
| 211 |
dns.rdatatype.NONE, |
| 212 |
dns.rdataclass.ANY, |
| 213 |
True, |
| 214 |
True, |
| 215 |
) |
| 216 |
elif isinstance(args[0], dns.rdataset.Rdataset): |
| 217 |
for rds in args: |
| 218 |
for rd in rds: |
| 219 |
self._add_rr(name, 0, rd, dns.rdataclass.NONE) |
| 220 |
else: |
| 221 |
largs = list(args) |
| 222 |
if isinstance(largs[0], dns.rdata.Rdata): |
| 223 |
for rd in largs: |
| 224 |
self._add_rr(name, 0, rd, dns.rdataclass.NONE) |
| 225 |
else: |
| 226 |
rdtype = dns.rdatatype.RdataType.make(largs.pop(0)) |
| 227 |
if len(largs) == 0: |
| 228 |
self.find_rrset( |
| 229 |
self.update, |
| 230 |
name, |
| 231 |
self.zone_rdclass, |
| 232 |
rdtype, |
| 233 |
dns.rdatatype.NONE, |
| 234 |
dns.rdataclass.ANY, |
| 235 |
True, |
| 236 |
True, |
| 237 |
) |
| 238 |
else: |
| 239 |
for s in largs: |
| 240 |
rd = dns.rdata.from_text( |
| 241 |
self.zone_rdclass, |
| 242 |
rdtype, |
| 243 |
s, # type: ignore[arg-type] |
| 244 |
self.origin, |
| 245 |
) |
| 246 |
self._add_rr(name, 0, rd, dns.rdataclass.NONE) |
| 247 |
|
| 248 |
def replace(self, name: dns.name.Name | str, *args: Any) -> None: |
| 249 |
"""Replace records. |
| 250 |
|
| 251 |
The first argument is always a name. The other |
| 252 |
arguments can be: |
| 253 |
|
| 254 |
- rdataset... |
| 255 |
|
| 256 |
- ttl, rdata... |
| 257 |
|
| 258 |
- ttl, rdtype, string... |
| 259 |
|
| 260 |
Note that if you want to replace the entire node, you should do |
| 261 |
a delete of the name followed by one or more calls to add. |
| 262 |
""" |
| 263 |
|
| 264 |
self._add(True, self.update, name, *args) |
| 265 |
|
| 266 |
def present(self, name: dns.name.Name | str, *args: Any) -> None: |
| 267 |
"""Require that an owner name (and optionally an rdata type, |
| 268 |
or specific rdataset) exists as a prerequisite to the |
| 269 |
execution of the update. |
| 270 |
|
| 271 |
The first argument is always a name. |
| 272 |
The other arguments can be: |
| 273 |
|
| 274 |
- rdataset... |
| 275 |
|
| 276 |
- rdata... |
| 277 |
|
| 278 |
- rdtype, string... |
| 279 |
""" |
| 280 |
|
| 281 |
if isinstance(name, str): |
| 282 |
name = dns.name.from_text(name, None) |
| 283 |
if len(args) == 0: |
| 284 |
self.find_rrset( |
| 285 |
self.prerequisite, |
| 286 |
name, |
| 287 |
dns.rdataclass.ANY, |
| 288 |
dns.rdatatype.ANY, |
| 289 |
dns.rdatatype.NONE, |
| 290 |
None, |
| 291 |
True, |
| 292 |
True, |
| 293 |
) |
| 294 |
elif ( |
| 295 |
isinstance(args[0], dns.rdataset.Rdataset) |
| 296 |
or isinstance(args[0], dns.rdata.Rdata) |
| 297 |
or len(args) > 1 |
| 298 |
): |
| 299 |
if not isinstance(args[0], dns.rdataset.Rdataset): |
| 300 |
# Add a 0 TTL |
| 301 |
largs = list(args) |
| 302 |
largs.insert(0, 0) # type: ignore[arg-type] |
| 303 |
self._add(False, self.prerequisite, name, *largs) |
| 304 |
else: |
| 305 |
self._add(False, self.prerequisite, name, *args) |
| 306 |
else: |
| 307 |
rdtype = dns.rdatatype.RdataType.make(args[0]) |
| 308 |
self.find_rrset( |
| 309 |
self.prerequisite, |
| 310 |
name, |
| 311 |
dns.rdataclass.ANY, |
| 312 |
rdtype, |
| 313 |
dns.rdatatype.NONE, |
| 314 |
None, |
| 315 |
True, |
| 316 |
True, |
| 317 |
) |
| 318 |
|
| 319 |
def absent( |
| 320 |
self, |
| 321 |
name: dns.name.Name | str, |
| 322 |
rdtype: dns.rdatatype.RdataType | str | None = None, |
| 323 |
) -> None: |
| 324 |
"""Require that an owner name (and optionally an rdata type) does |
| 325 |
not exist as a prerequisite to the execution of the update.""" |
| 326 |
|
| 327 |
if isinstance(name, str): |
| 328 |
name = dns.name.from_text(name, None) |
| 329 |
if rdtype is None: |
| 330 |
self.find_rrset( |
| 331 |
self.prerequisite, |
| 332 |
name, |
| 333 |
dns.rdataclass.NONE, |
| 334 |
dns.rdatatype.ANY, |
| 335 |
dns.rdatatype.NONE, |
| 336 |
None, |
| 337 |
True, |
| 338 |
True, |
| 339 |
) |
| 340 |
else: |
| 341 |
rdtype = dns.rdatatype.RdataType.make(rdtype) |
| 342 |
self.find_rrset( |
| 343 |
self.prerequisite, |
| 344 |
name, |
| 345 |
dns.rdataclass.NONE, |
| 346 |
rdtype, |
| 347 |
dns.rdatatype.NONE, |
| 348 |
None, |
| 349 |
True, |
| 350 |
True, |
| 351 |
) |
| 352 |
|
| 353 |
def _get_one_rr_per_rrset(self, value): |
| 354 |
# Updates are always one_rr_per_rrset |
| 355 |
return True |
| 356 |
|
| 357 |
def _parse_rr_header(self, section, name, rdclass, rdtype): # pyright: ignore |
| 358 |
deleting = None |
| 359 |
empty = False |
| 360 |
if section == UpdateSection.ZONE: |
| 361 |
if ( |
| 362 |
dns.rdataclass.is_metaclass(rdclass) |
| 363 |
or rdtype != dns.rdatatype.SOA |
| 364 |
or self.zone |
| 365 |
): |
| 366 |
raise dns.exception.FormError |
| 367 |
else: |
| 368 |
if not self.zone: |
| 369 |
raise dns.exception.FormError |
| 370 |
if rdclass in (dns.rdataclass.ANY, dns.rdataclass.NONE): |
| 371 |
deleting = rdclass |
| 372 |
rdclass = self.zone[0].rdclass |
| 373 |
empty = ( |
| 374 |
deleting == dns.rdataclass.ANY or section == UpdateSection.PREREQ |
| 375 |
) |
| 376 |
return (rdclass, rdtype, deleting, empty) |
| 377 |
|
| 378 |
|
| 379 |
# backwards compatibility |
| 380 |
Update = UpdateMessage |
| 381 |
|
| 382 |
### BEGIN generated UpdateSection constants |
| 383 |
|
| 384 |
ZONE = UpdateSection.ZONE |
| 385 |
PREREQ = UpdateSection.PREREQ |
| 386 |
UPDATE = UpdateSection.UPDATE |
| 387 |
ADDITIONAL = UpdateSection.ADDITIONAL |
| 388 |
|
| 389 |
### END generated UpdateSection constants |