1#!/bin/sh
2
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# This Source Code Form is subject to the terms of the Mozilla Public
8# License, v. 2.0.  If a copy of the MPL was not distributed with this
9# file, you can obtain one at https://mozilla.org/MPL/2.0/.
10#
11# See the COPYRIGHT file distributed with this work for additional
12# information regarding copyright ownership.
13
14set -e
15
16# shellcheck source=conf.sh
17. ../conf.sh
18
19dig_with_opts() {
20  "$DIG" +tcp +noadd +nosea +nostat +noquest +nocomm +nocmd +noauth -p "${PORT}" "$@"
21}
22
23dig_with_shortopts() {
24  "$DIG" +tcp +short -p "${PORT}" "$@"
25}
26
27RNDCCMD="$RNDC -c ../_common/rndc.conf -p ${CONTROLPORT} -s"
28
29status=0
30
31echo_i "fetching a.example from ns2's initial configuration"
32dig_with_opts a.example. @10.53.0.2 any >dig.out.ns2.1 || status=1
33
34echo_i "fetching a.example from ns3's initial configuration"
35dig_with_opts a.example. @10.53.0.3 any >dig.out.ns3.1 || status=1
36
37echo_i "copying in new configurations for ns2 and ns3"
38rm -f ns2/named.conf ns3/named.conf ns2/example.db
39cp -f ns2/example2.db ns2/example.db
40copy_setports ns2/named2.conf.in ns2/named.conf
41copy_setports ns3/named2.conf.in ns3/named.conf
42
43echo_i "reloading ns2 and ns3 with rndc"
44nextpart ns2/named.run >/dev/null
45nextpart ns3/named.run >/dev/null
46rndc_reload ns2 10.53.0.2
47rndc_reload ns3 10.53.0.3
48
49echo_i "wait for reload to complete"
50ret=0
51_check_reload() (
52  nextpartpeek ns2/named.run | grep "all zones loaded" >/dev/null \
53    && nextpartpeek ns3/named.run | grep "all zones loaded" >/dev/null \
54    && nextpartpeek ns3/named.run | grep "zone_dump: zone example/IN: enter" >/dev/null
55)
56retry_quiet 10 _check_reload || ret=1
57if [ $ret != 0 ]; then echo_i "failed"; fi
58status=$((status + ret))
59
60echo_i "fetching a.example from ns2's 10.53.0.4, source address 10.53.0.4"
61dig_with_opts -b 10.53.0.4 a.example. @10.53.0.4 any >dig.out.ns4.2 || status=1
62
63echo_i "fetching a.example from ns2's 10.53.0.2, source address 10.53.0.2"
64dig_with_opts -b 10.53.0.2 a.example. @10.53.0.2 any >dig.out.ns2.2 || status=1
65
66echo_i "fetching a.example from ns3's 10.53.0.3, source address defaulted"
67dig_with_opts @10.53.0.3 a.example. any >dig.out.ns3.2 || status=1
68
69echo_i "comparing ns3's initial a.example to one from reconfigured 10.53.0.2"
70digcomp dig.out.ns3.1 dig.out.ns2.2 || status=1
71
72echo_i "comparing ns3's initial a.example to one from reconfigured 10.53.0.3"
73digcomp dig.out.ns3.1 dig.out.ns3.2 || status=1
74
75echo_i "comparing ns2's initial a.example to one from reconfigured 10.53.0.4"
76digcomp dig.out.ns2.1 dig.out.ns4.2 || status=1
77
78echo_i "comparing ns2's initial a.example to one from reconfigured 10.53.0.3"
79echo_i "(should be different)"
80if $PERL ../digcomp.pl dig.out.ns2.1 dig.out.ns3.2 >/dev/null; then
81  echo_i "no differences found.  something's wrong."
82  status=1
83fi
84
85echo_i "updating cloned zone in internal view"
86$NSUPDATE <<EOF
87server 10.53.0.2 ${PORT}
88zone clone
89update add b.clone. 300 in a 10.1.0.3
90send
91EOF
92echo_i "sleeping to allow update to take effect"
93sleep 5
94
95echo_i "verifying update affected both views"
96ret=0
97one=$(dig_with_shortopts -b 10.53.0.2 @10.53.0.2 b.clone a)
98two=$(dig_with_shortopts -b 10.53.0.4 @10.53.0.2 b.clone a)
99if [ "$one" != "$two" ]; then
100  echo_i "'$one' does not match '$two'"
101  ret=1
102fi
103if [ $ret != 0 ]; then echo_i "failed"; fi
104status=$((status + ret))
105
106echo_i "verifying forwarder in cloned zone works"
107ret=0
108one=$(dig_with_shortopts -b 10.53.0.2 @10.53.0.2 child.clone txt)
109two=$(dig_with_shortopts -b 10.53.0.4 @10.53.0.2 child.clone txt)
110three=$(dig_with_shortopts @10.53.0.3 child.clone txt)
111four=$(dig_with_shortopts @10.53.0.5 child.clone txt)
112echo "$three" | grep NS3 >/dev/null || {
113  ret=1
114  echo_i "expected response from NS3 got '$three'"
115}
116echo "$four" | grep NS5 >/dev/null || {
117  ret=1
118  echo_i "expected response from NS5 got '$four'"
119}
120if [ "$one" = "$two" ]; then
121  echo_i "'$one' matches '$two'"
122  ret=1
123fi
124if [ "$one" != "$three" ]; then
125  echo_i "'$one' does not match '$three'"
126  ret=1
127fi
128if [ "$two" != "$four" ]; then
129  echo_i "'$two' does not match '$four'"
130  ret=1
131fi
132if [ $ret != 0 ]; then echo_i "failed"; fi
133status=$((status + ret))
134
135echo_i "verifying inline zones work with views"
136ret=0
137wait_for_signed() {
138  "$DIG" -p "${PORT}" @10.53.0.2 -b 10.53.0.2 +dnssec DNSKEY inline >dig.out.internal
139  "$DIG" -p "${PORT}" @10.53.0.2 -b 10.53.0.5 +dnssec DNSKEY inline >dig.out.external
140  grep "ANSWER: 4," dig.out.internal >/dev/null || return 1
141  grep "ANSWER: 4," dig.out.external >/dev/null || return 1
142  return 0
143}
144retry_quiet 10 wait_for_signed || ret=1
145int=$(awk '$4 == "DNSKEY" { print $8 }' dig.out.internal | sort)
146ext=$(awk '$4 == "DNSKEY" { print $8 }' dig.out.external | sort)
147test "$int" != "$ext" || ret=1
148if [ $ret != 0 ]; then echo_i "failed"; fi
149status=$((status + ret))
150
151echo_i "verifying adding of multiple inline zones followed by reconfiguration works"
152
153[ ! -f ns2/zones.conf ] && touch ns2/zones.conf
154copy_setports ns2/named3.conf.in ns2/named.conf
155
156i=1
157while [ $i -lt 50 ]; do
158  ret=0
159  zone_name=$(printf "example%03d.com" $i)
160
161  # Add a new zone to the configuration.
162  cat >>ns2/zones.conf <<-EOF
163	zone "${zone_name}" {
164	    type primary;
165	    file "db.${zone_name}";
166	    dnssec-dnskey-kskonly yes;
167	    auto-dnssec maintain;
168	    inline-signing yes;
169	};
170	EOF
171
172  # Create a master file for the zone.
173  cat >"ns2/db.${zone_name}" <<-EOF
174	\$TTL 86400
175	@		IN	SOA	localhost. hostmaster.localhost (
176						1612542642 ; serial
177						12H ; refresh
178						1H  ; retry
179						2w  ; expiry
180						1h  ; minimum
181					)
182	@		IN      NS      localhost
183	localhost       IN      A       127.0.0.1
184	EOF
185
186  $KEYGEN -q -Kns2 -fk -aecdsa256 "${zone_name}" >/dev/null
187  $RNDCCMD 10.53.0.2 reconfig || ret=1
188  if [ $ret != 0 ]; then
189    echo_i "failed"
190    break
191  fi
192  i=$((i + 1))
193done
194status=$((status + ret))
195
196echo_i "exit status: $status"
197[ "$status" -eq 0 ] || exit 1
198