ixfr_init.sh.in 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2011 Internet Software Consortium.
  4. #
  5. # Permission to use, copy, modify, and/or distribute this software for any
  6. # purpose with or without fee is hereby granted, provided that the above
  7. # copyright notice and this permission notice appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. # PERFORMANCE OF THIS SOFTWARE.
  16. # \file
  17. # This file should be run by all IXFR tests before doing anything else. It
  18. # includes the main configuration script to set the environment variables as
  19. # well as defining useful shell subroutines.
  20. . @abs_top_builddir@/tests/system/conf.sh
  21. # Useful symbols used in the IXFR tests.
  22. # Short-hand for getting SOA - just supply address of the server
  23. DIG_SOA="$DIG +norecurse +short -p $DNS_PORT example. SOA"
  24. # All IXFR tests use a BIND 9 server serving a BIND 10 client. These have the
  25. # smae name and use the same address in all tests.
  26. SERVER_NAME=ns1
  27. SERVER_IP=10.53.0.1 # BIND 9
  28. CLIENT_NAME=nsx2
  29. CLIENT_IP=10.53.0.2 # BIND 10
  30. # \brief Check Arguments
  31. #
  32. # Most functions take the name of nameserver as the first argument and its IP
  33. # address as the second. This function is passed "$*" and just checks that
  34. # both $1 and $2 are defined.
  35. #
  36. # \arg $* Arguments passed to caller
  37. #
  38. # \return status 0 => $1 and $2 are defined, 1 => they are not.
  39. check_name_ip() {
  40. if [ "$1" = "" ];
  41. then
  42. echo "R:FAIL name of server not supplied"
  43. return 1
  44. fi
  45. if [ "$2" = "" ];
  46. then
  47. echo "R:FAIL IP address of server not supplied"
  48. return 1
  49. fi
  50. return 0
  51. }
  52. # \brief Perform RNDC Command
  53. #
  54. # Controls the BIND 9 IXFR server. Called do_rndc (instead of rndc) to avoid
  55. # confusion if rndc itself is in the search path.
  56. #
  57. # \arg $1 - Name of the server (ns1, nsx2 etc.)
  58. # \arg $2 - IP address of the server
  59. # \arg $* - Command to execute (which may be multiple tokens)
  60. #
  61. # \return 0 on success, 1 on failure (in which case an error message will
  62. # have been output).
  63. do_rndc () {
  64. # If the following checks fail, the code is wrong.
  65. check_name_ip $*
  66. if [ $? -ne 0 ];
  67. then
  68. echo "R:FAIL do_rndc - name or ip address of server not supplied"
  69. return 1
  70. fi
  71. name=$1
  72. shift
  73. ip=$1
  74. shift
  75. if [ "$1" = "" ];
  76. then
  77. echo "R:FAIL do_rndc - rndc command not supplied"
  78. return 1
  79. fi
  80. $RNDC -c $SYSTEM_TOP/common/rndc.conf -s $ip -p $RNDC_PORT $* 2>&1 \
  81. | sed "s/^/I:$name /"
  82. }
  83. # \brief Wait for update
  84. #
  85. # Given a serial number and a server, poll the nameserver until the SOA serial
  86. # number is different from that given. The poll takes place every five seconds
  87. # for a minute.
  88. #
  89. # \arg $1 - Name of the server
  90. # \arg $2 - IP address of the server
  91. # \arg $3 - Serial number to check against
  92. #
  93. # \return 0 if the serial number is different (requires another poll to obtain
  94. # it), 1 if the serial number has not changed after one minute.
  95. wait_for_update() {
  96. # If the following checks fail, the code is wrong.
  97. check_name_ip $*
  98. if [ $? -ne 0 ];
  99. then
  100. echo "R:FAIL wait_for_update - name or ip address of system not supplied"
  101. return 1
  102. fi
  103. name=$1
  104. shift
  105. ip=$1
  106. shift
  107. serial=$1
  108. if [ "$serial" = "" ];
  109. then
  110. echo "R:FAIL wait_for_update - serial number not supplied"
  111. return 1
  112. fi
  113. # Now poll the server looking for the new serial number
  114. echo "I:$name waiting for SOA serial to change from $serial"
  115. for i in 1 2 3 4 5 6 7 8 9 10 11 12
  116. do
  117. if [ $i -gt 1 ];
  118. then
  119. sleep 5
  120. fi
  121. new_serial=`$DIG_SOA @$ip | $AWK '{print $3}'`
  122. if [ "$new_serial" != "$serial" ];
  123. then
  124. echo "I:$name SOA serial was at $serial, now at $new_serial"
  125. return 0
  126. fi
  127. done
  128. echo "R:$name FAIL serial number has not updated"
  129. return 1
  130. }
  131. # \brief Update server zone
  132. #
  133. # Reloads the example. zone in the BIND 9 IXFR server and waits a maximum of
  134. # one minute for it to be served.
  135. #
  136. # \arg $1 - Name of the server (ns1, nsx2 etc.)
  137. # \arg $2 - IP address of the server
  138. # \arg $3 - Zone file to load
  139. # \arg $* - Command to execute (which may be multiple tokens)
  140. #
  141. # \return 0 on success, 1 on failure (for which an error message will have
  142. # been output).
  143. update_server_zone() {
  144. # If the following checks fail, the code is wrong.
  145. check_name_ip $*
  146. if [ $? -ne 0 ];
  147. then
  148. echo "R:FAIL update_server_zone - name or ip address of server not supplied"
  149. return 1
  150. fi
  151. name=$1
  152. shift
  153. ip=$1
  154. shift
  155. file=$1
  156. shift
  157. if [ "$file" = "" ];
  158. then
  159. echo "R:FAIL update_server_zone - new zone file not supplied"
  160. return 1
  161. fi
  162. if [ ! -e $file ];
  163. then
  164. echo "R:FAIL update_server_zone - zone file does not exist: $file"
  165. return 1
  166. fi
  167. old_serial=`$DIG_SOA @$ip | $AWK '{print $3}'`
  168. echo "I:$name IXFR server loading $file"
  169. cp $file $name/db.example
  170. do_rndc $name $ip reload
  171. if [ $? -ne 0 ];
  172. then
  173. return 1 # Message will have already been output
  174. fi
  175. wait_for_update $name $ip $old_serial
  176. if [ $? -ne 0 ];
  177. then
  178. echo "R:$name FAIL IXFR server did not update zone after reload"
  179. return 1
  180. fi
  181. new_serial=`$DIG_SOA @$ip | $AWK '{print $3}'`
  182. return 0
  183. }
  184. # \brief Compare client and server SOAs
  185. #
  186. # Checks the SOAs of two systems and reports if they are not equal.
  187. #
  188. # \arg $1 Name of the IXFR server
  189. # \arg $2 IP of the IXFR server
  190. # \arg $3 Name of the IXFR client
  191. # \arg $4 IP of the IXFR client
  192. #
  193. # \return 0 if the systems have the same SOA, 1 if not. In the latter case,
  194. # an error will be output.
  195. compare_soa() {
  196. # If the following checks fail, the code is wrong.
  197. check_name_ip $*
  198. if [ $? -ne 0 ];
  199. then
  200. echo "R:FAIL compare_soa - name or ip address of server not supplied"
  201. return 1
  202. fi
  203. server_name=$1
  204. shift
  205. server_ip=$1
  206. shift
  207. check_name_ip $*
  208. if [ $? -ne 0 ];
  209. then
  210. echo "R:FAIL compare_soa - name or ip address of client not supplied"
  211. return 1
  212. fi
  213. client_name=$1
  214. shift
  215. client_ip=$1
  216. shift
  217. client_serial=`$DIG_SOA @$client_ip | $AWK '{print $3}'`
  218. server_serial=`$DIG_SOA @$server_ip | $AWK '{print $3}'`
  219. if [ "$client_serial" != "$server_serial" ];
  220. then
  221. echo "R:FAIL client $client_name serial $client_serial not same as server $server_name serial $server_serial"
  222. return 1
  223. fi
  224. return 0
  225. }
  226. # \brief Compare client and server zones
  227. #
  228. # Checks the zones of two systems and reports if they are not identical.
  229. #
  230. # The check is simplistic. Each zone is listed via "dig", after which comment
  231. # lines, blank lines and spaces/tabs are removed, and the result sorted. The
  232. # output from each system is then compared. They should be identical.
  233. #
  234. # \arg $1 Name of the IXFR server
  235. # \arg $2 IP of the IXFR server
  236. # \arg $3 Name of the IXFR client
  237. # \arg $4 IP of the IXFR client
  238. #
  239. # \return 0 if the zones are the same, 1 if not.
  240. compare_zones() {
  241. # If the following checks fail, the code is wrong.
  242. check_name_ip $*
  243. if [ $? -ne 0 ];
  244. then
  245. echo "R:FAIL compare_zones - name or ip address of server not supplied"
  246. return 1
  247. fi
  248. server_name=$1
  249. shift
  250. server_ip=$1
  251. shift
  252. check_name_ip $*
  253. if [ $? -ne 0 ];
  254. then
  255. echo "R:FAIL compare_zones - name or ip address of client not supplied"
  256. return 1
  257. fi
  258. client_name=$1
  259. shift
  260. client_ip=$1
  261. shift
  262. $DIG @$client_ip -p $DNS_PORT example. axfr | grep -v '^;' | grep -v '^$' \
  263. | sed -e 's/ //g' -e 's/\t//g' | sort > client.dig
  264. $DIG @$server_ip -p $DNS_PORT example. axfr | grep -v '^;' | grep -v '^$' \
  265. | sed -e 's/ //g' -e 's/\t//g' | sort > server.dig
  266. diff client.dig server.dig
  267. if [ $? -eq 0 ];
  268. then
  269. echo "I:client and server zones identical"
  270. else
  271. echo "R:FAIL client $client_name zone not same as server $server_name zone"
  272. return 1
  273. fi
  274. return 0
  275. }