summaryrefslogtreecommitdiffstats
path: root/tests-extra/tools/dnstest/knsupdate.py
blob: 24f4a461b3efe3fceb27062bc8368a3a4ac28d39 (plain)
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
#!/usr/bin/env python3

from re import search, findall
from subprocess import Popen, PIPE

from dnstest.context import Context
from dnstest.utils import *
import dnstest.params as params

# Use one instance per zone, don't combine multiple zone updates (could cause UB).
class Knsupdate:
    def __init__(self, zone, tsig=None):
        self.zone = zone
        self.tsig = tsig
        self.output = ""

    def add(self, owner, ttl, rtype, rdata):
        self.output += f"update add {owner}"
        if ttl:
            self.output += f" {ttl}"
        self.output += f" {rtype} {rdata}\n"

    def delete(self, owner, *args):
        self.output += f"update delete {owner}"
        if len(args) >= 1:
            rtype = args[0]
            if rtype != 'ANY':
                self.output += f" {rtype}"
                if len(args) >= 2:
                    rdata = args[1]
                    self.output += f" {rdata}"
        self.output += "\n"

    def present(self, owner, *args):
        if len(args) >= 1:
            self.output += f"prereq yxrrset {owner}"
            rtype = args[0]
            self.output += f" {rtype}"
            if len(args) >= 2:
                rdata = args[1]
                self.output += f" {rdata}"
        else:
            self.output += f"prereq yxdomain {owner}"
        self.output += "\n"

    def absent(self, owner, *args):
        if len(args) >= 1:
            self.output += f"prereq nxrrset {owner}"
            rtype = args[0]
            self.output += f" {rtype}"
        else:
            self.output += f"prereq nxdomain {owner}"
        self.output += "\n"

    def send(self, addr, port, proto):
        # Binary with arguments
        cmdline = [params.knsupdate_bin]

        if proto is Proto.TCP:
            cmdline += ["-T"]
        elif proto is Proto.TLS:
            cmdline += ["-S"]
        elif proto is Proto.QUIC:
            cmdline += ["-Q"]

        if self.tsig:
            cmdline += ["-y", f"{self.tsig.alg}:{self.tsig.name}:{self.tsig.key}"]

        # Mandatory header
        header = f"server {addr} {port}\n"
        header += f"zone {self.zone}\n"

        # Run process
        cmd = Popen(cmdline, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
        (stdout, stderr) = cmd.communicate(header + self.output + "show\nsend\nanswer\nexit\n")

        with open(Context().out_dir + "/knsupdate.out", "a") as outf:
            outf.write(' '.join(cmdline))
            outf.write("\n" + stdout + "\n")
        with open(Context().out_dir + "/knsupdate.err", "a") as errf:
            errf.write(stderr)

        # Parse RCODE
        rcodes = findall(r"status: ([A-Z]+)", stdout)
        if len(rcodes) != 2:
            raise Failed("Failed to parse RCODE")

        rcv_ercode = rcodes[1]

        if rcv_ercode != "SERVFAIL" and "reply verification" in stderr:
            raise Failed("TSIG issues in DDNS")

        self.output = ""
        return rcv_ercode