20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence/*
523230336909d30111cb060b7eb6fc39d23ad174Tinderbox User * Copyright (C) 2000-2018 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence/*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Principal Author: DCL
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <config.h>
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <stdlib.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isc/app.h>
ce2be9b7211ab5bacaa10fe74ef35def3a3f6089David Lawrence#include <isc/buffer.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/commandline.h>
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence#include <isc/file.h>
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington#include <isc/log.h>
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#include <isc/net.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/mem.h>
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont#include <isc/print.h>
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#include <isc/random.h>
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence#include <isc/socket.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isc/stdtime.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/string.h>
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence#include <isc/task.h>
e19501436a92cd48eba2ff47d90fa49c661ec8d8Brian Wellington#include <isc/thread.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/util.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#include <pk11/site.h>
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews
669e9657c731176df235832367f61435f7b83ddfAndreas Gustafsson#include <isccfg/namedconf.h>
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/alist.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/base64.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/cc.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/ccmsg.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/result.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/sexpr.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/types.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/util.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews#include <dns/name.h>
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington#include <bind9/getaddresses.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington#include "util.h"
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
3f96cf3e4f96b36cc1ad2ec7edc5b8e285fced8fBrian Wellington#define SERVERADDRS 10
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
7318a964ece83f748bc7e9814d8c3a61c2b4d946Mark Andrewsconst char *progname;
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrenceisc_boolean_t verbose;
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrence
80b67b3a4f2d9fc7fdd32a50edc67ff189894da2Danny Mayerstatic const char *admin_conffile;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsstatic const char *admin_keyfile;
87983da955bf63128de85d180359bdc418516c3cDavid Lawrencestatic const char *version = VERSION;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic const char *servername = NULL;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonstatic isc_sockaddr_t serveraddrs[SERVERADDRS];
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsstatic isc_sockaddr_t local4, local6;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsstatic isc_boolean_t local4set = ISC_FALSE, local6set = ISC_FALSE;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonstatic int nserveraddrs;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonstatic int currentaddr = 0;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsstatic unsigned int remoteport = 0;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isc_socketmgr_t *socketmgr = NULL;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Huntstatic isc_buffer_t *databuf;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isccc_ccmsg_t ccmsg;
4eb998928b9aef0ceda42d7529980d658138698aEvan Huntstatic isc_uint32_t algorithm;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isccc_region_t secret;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isc_boolean_t failed = ISC_FALSE;
4144efb39046963989ad002cf88a0c195401100aJeremy Reedstatic isc_boolean_t c_flag = ISC_FALSE;
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Huntstatic isc_mem_t *rndc_mctx;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellingtonstatic int sends, recvs, connects;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencestatic char *command;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencestatic char *args;
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellingtonstatic char program[256];
2ba574f329c14376d26d7c0f22c89d7a978a2625Mark Andrewsstatic isc_socket_t *sock = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewsstatic isc_uint32_t serial;
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Huntstatic isc_boolean_t quiet = ISC_FALSE;
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntstatic isc_boolean_t showresult = ISC_FALSE;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonstatic void rndc_startconnect(isc_sockaddr_t *addr, isc_task_t *task);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
debd489a44363870f96f75818e89ec27d3cab736Francis DupontISC_PLATFORM_NORETURN_PRE static void
debd489a44363870f96f75818e89ec27d3cab736Francis Dupontusage(int status) ISC_PLATFORM_NORETURN_POST;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrencestatic void
73a691c373488e4f70387a62462cd8ce0d991705David Lawrenceusage(int status) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence fprintf(stderr, "\
5b79d154014f87b6c54b1ec2d3912c35b02042a1Mark AndrewsUsage: %s [-b address] [-c config] [-s server] [-p port]\n\
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt [-k key-file ] [-y key] [-r] [-V] command\n\
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence\n\
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafssoncommand is one of the following:\n\
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews addzone zone [class [view]] { zone-options }\n\
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman Add zone to given view. Requires allow-new-zones option.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews delzone [-clean] zone [class [view]]\n\
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman Removes zone from given view.\n\
d6a0e00dc3e047f8470b938878926957070def77Mark Andrews dnstap -reopen\n\
19977879caf8579a5fafb0cf3bf1cb983063796cEvan Hunt Close, truncate and re-open the DNSTAP output file.\n\
d6a0e00dc3e047f8470b938878926957070def77Mark Andrews dnstap -roll count\n\
d6a0e00dc3e047f8470b938878926957070def77Mark Andrews Close, rename and re-open the DNSTAP output file(s).\n\
1479200aa05414b2acf33607dbd1682c16f58c51Evan Hunt dumpdb [-all|-cache|-zones|-adb|-bad|-fail] [view ...]\n\
b326d7e3a3a50eb65dd06db007d2fddc62606bbfMark Andrews Dump cache(s) to the dump file (named_dump.db).\n\
5455f30a7532738d750252c00e649890c694ee30Brian Wellington flush Flushes all of the server's caches.\n\
5455f30a7532738d750252c00e649890c694ee30Brian Wellington flush [view] Flushes the server's cache for a view.\n\
60213f2815a7e6584a2285546d05633fa7b6f5b4Mark Andrews flushname name [view]\n\
c30d291128e099a284fa6272b91b2bd64519a209Mark Andrews Flush the given name from the server's cache(s)\n\
6150d3cb666a58d5e3a15275562c9fc5c5b6b2d8Evan Hunt flushtree name [view]\n\
6150d3cb666a58d5e3a15275562c9fc5c5b6b2d8Evan Hunt Flush all names under the given name from the server's cache(s)\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews freeze Suspend updates to all dynamic zones.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews freeze zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Suspend updates to a dynamic zone.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews halt Stop the server without saving pending updates.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews halt -p Stop the server without saving pending updates reporting\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews process id.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews loadkeys zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Update keys without signing immediately.\n\
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews managed-keys refresh [class [view]]\n\
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews Check trust anchor for RFC 5011 key changes\n\
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews managed-keys status [class [view]]\n\
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews Display RFC 5011 managed keys information\n\
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews managed-keys sync [class [view]]\n\
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews Write RFC 5011 managed keys to disk\n\
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman modzone zone [class [view]] { zone-options }\n\
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman Modify a zone's configuration.\n\
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman Requires allow-new-zones option.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews notify zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Resend NOTIFY messages for the zone.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews notrace Set debugging level to 0.\n\
323bb31d7c54078aa62146b3aa946b755cbfd52bMark Andrews nta -dump\n\
323bb31d7c54078aa62146b3aa946b755cbfd52bMark Andrews List all negative trust anchors.\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt nta [-lifetime duration] [-force] domain [view]\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt Set a negative trust anchor, disabling DNSSEC validation\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt for the given domain.\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt Using -lifetime specifies the duration of the NTA, up\n\
629a0159401a6c0d991a78a6d0b90ee84e83668cEvan Hunt to one week.\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt Using -force prevents the NTA from expiring before its\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt full lifetime, even if the domain can validate sooner.\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt nta -remove domain [view]\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt Remove a negative trust anchor, re-enabling validation\n\
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt for the given domain.\n\
4221d9cd1d02311fbf9b5f08a038f5af78b10b4aEvan Hunt querylog [ on | off ]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Enable / disable query logging.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews reconfig Reload configuration file and new zones only.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews recursing Dump the queries that are currently recursing (named.recursing)\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews refresh zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Schedule immediate maintenance for a zone.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews reload Reload configuration file and zones.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews reload zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Reload a single zone.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews retransfer zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Retransfer a single zone without checking serial number.\n\
62ec9fd1681ffae7d6b0d54618599ecf650e3100Mark Andrews scan Scan available network interfaces for changes.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews secroots [view ...]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Write security roots to the secroots file.\n\
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman showzone zone [class [view]]\n\
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaraman Print a zone's configuration.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews sign zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Update zone keys, and sign as needed.\n\
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt signing -clear all zone [class [view]]\n\
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt Remove the private records for all keys that have\n\
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt finished signing the given zone.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews signing -clear <keyid>/<algorithm> zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Remove the private record that indicating the given key\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews has finished signing the given zone.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews signing -list zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews List the private records showing the state of DNSSEC\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews signing in the given zone.\n\
9bd876a683709be588f6fac6781a76fdd57b2f08Mark Andrews signing -nsec3param hash flags iterations salt zone [class [view]]\n\
9bd876a683709be588f6fac6781a76fdd57b2f08Mark Andrews Add NSEC3 chain to zone if already signed.\n\
9bd876a683709be588f6fac6781a76fdd57b2f08Mark Andrews Prime zone with NSEC3 chain if not yet signed.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews signing -nsec3param none zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Remove NSEC3 chains from zone.\n\
fdcfc6bae754ee8f0b43dfd872284a294a8f2fd2Mark Andrews signing -serial <value> zone [class [view]]\n\
fdcfc6bae754ee8f0b43dfd872284a294a8f2fd2Mark Andrews Set the zones's serial to <value>.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews stats Write server statistics to the statistics file.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews status Display status of the server.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews stop Save pending updates to master files and stop the server.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews stop -p Save pending updates to master files and stop the server\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews reporting process id.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews sync [-clean] Dump changes to all dynamic zones to disk, and optionally\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews remove their journal files.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews sync [-clean] zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Dump a single zone's changes to disk, and optionally\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews remove its journal file.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews thaw Enable updates to all dynamic zones and reload them.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews thaw zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Enable updates to a frozen dynamic zone and reload it.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews trace Increment debugging level by one.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews trace level Change the debugging level.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews tsig-delete keyname [view]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Delete a TKEY-negotiated TSIG key.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews tsig-list List all currently active TSIG keys, including both statically\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews configured and TKEY-negotiated keys.\n\
4221d9cd1d02311fbf9b5f08a038f5af78b10b4aEvan Hunt validation [ yes | no | status ] [view]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Enable / disable DNSSEC validation.\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews zonestatus zone [class [view]]\n\
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews Display the current status of a zone.\n\
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence\n\
87983da955bf63128de85d180359bdc418516c3cDavid LawrenceVersion: %s\n",
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence progname, version);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence exit(status);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonget_addresses(const char *host, in_port_t port) {
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington isc_result_t result;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews int found = 0, count;
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews if (*host == '/') {
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews result = isc_sockaddr_frompath(&serveraddrs[nserveraddrs],
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews host);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews if (result == ISC_R_SUCCESS)
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater nserveraddrs++;
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews } else {
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews count = SERVERADDRS - nserveraddrs;
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews result = bind9_getaddresses(host, port,
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews &serveraddrs[nserveraddrs],
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews count, &found);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews nserveraddrs += found;
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews }
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington if (result != ISC_R_SUCCESS)
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington fatal("couldn't get address for '%s': %s",
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington host, isc_result_totext(result));
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington INSIST(nserveraddrs > 0);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonrndc_senddone(isc_task_t *task, isc_event_t *event) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington UNUSED(task);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington sends--;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sevent->result != ISC_R_SUCCESS)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fatal("send failed: %s", isc_result_totext(sevent->result));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_event_free(&event);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews if (sends == 0 && recvs == 0) {
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews isc_socket_detach(&sock);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews isc_task_shutdown(task);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews RUNTIME_CHECK(isc_app_shutdown() == ISC_R_SUCCESS);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonrndc_recvdone(isc_task_t *task, isc_event_t *event) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *response = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *data;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_region_t source;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington char *errormsg = NULL;
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington char *textmsg = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_result_t result;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington recvs--;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence if (ccmsg.result == ISC_R_EOF)
e1d05d323526e7e65df13a6d3dfbec30f6ddb500Brian Wellington fatal("connection to remote host closed\n"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "This may indicate that\n"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "* the remote server is using an older version of"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews " the command protocol,\n"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "* this host is not authorized to connect,\n"
287910778c57d4836a52b03b697c2ef342d0eaa9Francis Dupont "* the clocks are not synchronized, or\n"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "* the key is invalid.");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (ccmsg.result != ISC_R_SUCCESS)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fatal("recv failed: %s", isc_result_totext(ccmsg.result));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington source.rstart = isc_buffer_base(&ccmsg.buffer);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington source.rend = isc_buffer_used(&ccmsg.buffer);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt DO("parse message",
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt isccc_cc_fromwire(&source, &response, algorithm, &secret));
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington data = isccc_alist_lookup(response, "_data");
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrews if (!isccc_alist_alistp(data))
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrews fatal("bad or missing data section in response");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = isccc_cc_lookupstring(data, "err", &errormsg);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (result == ISC_R_SUCCESS) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington failed = ISC_TRUE;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stderr, "%s: '%s' failed: %s\n",
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington progname, command, errormsg);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington else if (result != ISC_R_NOTFOUND)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stderr, "%s: parsing response failed: %s\n",
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington progname, isc_result_totext(result));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington result = isccc_cc_lookupstring(data, "text", &textmsg);
927e4c9fecf448bf3894c68fcaf9dc2f89557f3aEvan Hunt if (result == ISC_R_SUCCESS) {
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt if ((!quiet || failed) && strlen(textmsg) != 0U)
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt fprintf(failed ? stderr : stdout, "%s\n", textmsg);
927e4c9fecf448bf3894c68fcaf9dc2f89557f3aEvan Hunt } else if (result != ISC_R_NOTFOUND)
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington fprintf(stderr, "%s: parsing response failed: %s\n",
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington progname, isc_result_totext(result));
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (showresult) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt isc_result_t eresult;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt result = isccc_cc_lookupuint32(data, "result", &eresult);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (result == ISC_R_SUCCESS)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt printf("%s %u\n", isc_result_toid(eresult), eresult);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt else
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt printf("NONE -1\n");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt }
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_event_free(&event);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&response);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews if (sends == 0 && recvs == 0) {
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews isc_socket_detach(&sock);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews isc_task_shutdown(task);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews RUNTIME_CHECK(isc_app_shutdown() == ISC_R_SUCCESS);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewsstatic void
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrewsrndc_recvnonce(isc_task_t *task, isc_event_t *event) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isccc_sexpr_t *response = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isccc_sexpr_t *_ctrl;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isccc_region_t source;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_result_t result;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_uint32_t nonce;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isccc_sexpr_t *request = NULL;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isccc_time_t now;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_region_t r;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isccc_sexpr_t *data;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_buffer_t b;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews recvs--;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (ccmsg.result == ISC_R_EOF)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews fatal("connection to remote host closed\n"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "This may indicate that\n"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "* the remote server is using an older version of"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews " the command protocol,\n"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "* this host is not authorized to connect,\n"
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt "* the clocks are not synchronized,\n"
bcae9a15c1e9c50a6e6433168d5225b1de89d6b9Evan Hunt "* the key signing algorithm is incorrect, or\n"
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews "* the key is invalid.");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (ccmsg.result != ISC_R_SUCCESS)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews fatal("recv failed: %s", isc_result_totext(ccmsg.result));
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews source.rstart = isc_buffer_base(&ccmsg.buffer);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews source.rend = isc_buffer_used(&ccmsg.buffer);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt DO("parse message",
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt isccc_cc_fromwire(&source, &response, algorithm, &secret));
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews _ctrl = isccc_alist_lookup(response, "_ctrl");
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrews if (!isccc_alist_alistp(_ctrl))
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrews fatal("bad or missing ctrl section in response");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews nonce = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (isccc_cc_lookupuint32(_ctrl, "_nonce", &nonce) != ISC_R_SUCCESS)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews nonce = 0;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_stdtime_get(&now);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews DO("create message", isccc_cc_createmessage(1, NULL, NULL, ++serial,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews now, now + 60, &request));
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews data = isccc_alist_lookup(request, "_data");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (data == NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews fatal("_data section missing");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (isccc_cc_definestring(data, "type", args) == NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews fatal("out of memory");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (nonce != 0) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews _ctrl = isccc_alist_lookup(request, "_ctrl");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (_ctrl == NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews fatal("_ctrl section missing");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (isccc_cc_defineuint32(_ctrl, "_nonce", nonce) == NULL)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews fatal("out of memory");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews }
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_clear(databuf);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt /* Skip the length field (4 bytes) */
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_add(databuf, 4);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt DO("render message",
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isccc_cc_towire(request, &databuf, algorithm, &secret));
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_init(&b, databuf->base, 4);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_putuint32(&b, databuf->used - 4);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt r.base = databuf->base;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt r.length = databuf->used;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isccc_ccmsg_cancelread(&ccmsg);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews DO("schedule recv", isccc_ccmsg_readmessage(&ccmsg, task,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews rndc_recvdone, NULL));
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews recvs++;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews DO("send message", isc_socket_send(sock, &r, task, rndc_senddone,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews NULL));
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews sends++;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_event_free(&event);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isccc_sexpr_free(&response);
35b65c4cdf44dbdcd2121743e9760e2354c1e279Ondřej Surý isccc_sexpr_free(&request);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews return;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews}
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonrndc_connected(isc_task_t *task, isc_event_t *event) {
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews char socktext[ISC_SOCKADDR_FORMATSIZE];
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *request = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *data;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_time_t now;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_region_t r;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_buffer_t b;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_result_t result;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington connects--;
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington if (sevent->result != ISC_R_SUCCESS) {
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews isc_sockaddr_format(&serveraddrs[currentaddr], socktext,
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews sizeof(socktext));
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellington if (sevent->result != ISC_R_CANCELED &&
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews ++currentaddr < nserveraddrs)
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington {
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews notify("connection failed: %s: %s", socktext,
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellington isc_result_totext(sevent->result));
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington isc_socket_detach(&sock);
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellington isc_event_free(&event);
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews rndc_startconnect(&serveraddrs[currentaddr], task);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington return;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington } else
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews fatal("connect failed: %s: %s", socktext,
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington isc_result_totext(sevent->result));
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_stdtime_get(&now);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews DO("create message", isccc_cc_createmessage(1, NULL, NULL, ++serial,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington now, now + 60, &request));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington data = isccc_alist_lookup(request, "_data");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (data == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fatal("_data section missing");
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews if (isccc_cc_definestring(data, "type", "null") == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fatal("out of memory");
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_clear(databuf);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt /* Skip the length field (4 bytes) */
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_add(databuf, 4);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt DO("render message",
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isccc_cc_towire(request, &databuf, algorithm, &secret));
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_init(&b, databuf->base, 4);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_putuint32(&b, databuf->used - 4);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt r.base = databuf->base;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt r.length = databuf->used;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt isccc_ccmsg_init(rndc_mctx, sock, &ccmsg);
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews isccc_ccmsg_setmaxsize(&ccmsg, 1024 * 1024);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington DO("schedule recv", isccc_ccmsg_readmessage(&ccmsg, task,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews rndc_recvnonce, NULL));
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington recvs++;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington DO("send message", isc_socket_send(sock, &r, task, rndc_senddone,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington NULL));
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington sends++;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_event_free(&event);
35b65c4cdf44dbdcd2121743e9760e2354c1e279Ondřej Surý isccc_sexpr_free(&request);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonrndc_startconnect(isc_sockaddr_t *addr, isc_task_t *task) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_result_t result;
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews int pf;
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews isc_sockettype_t type;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington char socktext[ISC_SOCKADDR_FORMATSIZE];
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington isc_sockaddr_format(addr, socktext, sizeof(socktext));
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence notify("using server %s (%s)", servername, socktext);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews pf = isc_sockaddr_pf(addr);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews if (pf == AF_INET || pf == AF_INET6)
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews type = isc_sockettype_tcp;
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews else
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews type = isc_sockettype_unix;
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews DO("create socket", isc_socket_create(socketmgr, pf, type, &sock));
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews switch (isc_sockaddr_pf(addr)) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews case AF_INET:
240e53b13217af266abb3dae8ba103614daf2bf7Mark Andrews DO("bind socket", isc_socket_bind(sock, &local4, 0));
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews break;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews case AF_INET6:
240e53b13217af266abb3dae8ba103614daf2bf7Mark Andrews DO("bind socket", isc_socket_bind(sock, &local6, 0));
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews break;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews default:
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews break;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington DO("connect", isc_socket_connect(sock, addr, task, rndc_connected,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington NULL));
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington connects++;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonstatic void
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonrndc_start(isc_task_t *task, isc_event_t *event) {
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington isc_event_free(&event);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington currentaddr = 0;
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews rndc_startconnect(&serveraddrs[currentaddr], task);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington}
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencestatic void
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsparse_config(isc_mem_t *mctx, isc_log_t *log, const char *keyname,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews cfg_parser_t **pctxp, cfg_obj_t **configp)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence{
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence isc_result_t result;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence const char *conffile = admin_conffile;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *addresses = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *defkey = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *options = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *servers = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *server = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *keys = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *key = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *defport = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *secretobj = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *algorithmobj = NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews cfg_obj_t *config = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *address = NULL;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_listelt_t *elt;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews const char *secretstr;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt const char *algorithmstr;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews static char secretarray[1024];
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews const cfg_type_t *conftype = &cfg_type_rndcconf;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews isc_boolean_t key_only = ISC_FALSE;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_listelt_t *element;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
504f7802d4c9b43db4820f496c4d00e078effa18David Lawrence if (! isc_file_exists(conffile)) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews conffile = admin_keyfile;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews conftype = &cfg_type_rndckey;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
17a00ff54c51f6bdfddd7d9ceaef6f2dcf387cc1Mark Andrews if (c_flag)
17a00ff54c51f6bdfddd7d9ceaef6f2dcf387cc1Mark Andrews fatal("%s does not exist", admin_conffile);
17a00ff54c51f6bdfddd7d9ceaef6f2dcf387cc1Mark Andrews
504f7802d4c9b43db4820f496c4d00e078effa18David Lawrence if (! isc_file_exists(conffile))
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence fatal("neither %s nor %s was found",
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews admin_conffile, admin_keyfile);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews key_only = ISC_TRUE;
4144efb39046963989ad002cf88a0c195401100aJeremy Reed } else if (! c_flag && isc_file_exists(admin_keyfile)) {
4144efb39046963989ad002cf88a0c195401100aJeremy Reed fprintf(stderr, "WARNING: key file (%s) exists, but using "
4144efb39046963989ad002cf88a0c195401100aJeremy Reed "default configuration file (%s)\n",
4144efb39046963989ad002cf88a0c195401100aJeremy Reed admin_keyfile, admin_conffile);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence }
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence DO("create parser", cfg_parser_create(mctx, log, pctxp));
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence /*
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * The parser will output its own errors, so DO() is not used.
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews result = cfg_parse_file(*pctxp, conffile, conftype, &config);
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson if (result != ISC_R_SUCCESS)
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson fatal("could not load rndc configuration");
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (!key_only)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews (void)cfg_map_get(config, "options", &options);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (key_only && servername == NULL)
0a77211c806fa84fd66638b5cccf550c7cd7760dAndreas Gustafsson servername = "127.0.0.1";
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews else if (servername == NULL && options != NULL) {
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *defserverobj = NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews (void)cfg_map_get(options, "default-server", &defserverobj);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (defserverobj != NULL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews servername = cfg_obj_asstring(defserverobj);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (servername == NULL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews fatal("no server specified and no default");
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (!key_only) {
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson (void)cfg_map_get(config, "server", &servers);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (servers != NULL) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews for (elt = cfg_list_first(servers);
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater elt != NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews elt = cfg_list_next(elt))
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews const char *name;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews server = cfg_listelt_value(elt);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews name = cfg_obj_asstring(cfg_map_getname(server));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (strcasecmp(name, servername) == 0)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews break;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews server = NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Look for the name of the key to use.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (keyname != NULL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews ; /* Was set on command line, do nothing. */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews else if (server != NULL) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews DO("get key for server", cfg_map_get(server, "key", &defkey));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews keyname = cfg_obj_asstring(defkey);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews } else if (options != NULL) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews DO("get default key", cfg_map_get(options, "default-key",
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews &defkey));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews keyname = cfg_obj_asstring(defkey);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews } else if (!key_only)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews fatal("no key for server and no default");
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Get the key's definition.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (key_only)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews DO("get key", cfg_map_get(config, "key", &key));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews else {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews DO("get config key list", cfg_map_get(config, "key", &keys));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews for (elt = cfg_list_first(keys);
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater elt != NULL;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews elt = cfg_list_next(elt))
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews key = cfg_listelt_value(elt);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (strcasecmp(cfg_obj_asstring(cfg_map_getname(key)),
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews keyname) == 0)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews break;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (elt == NULL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews fatal("no key definition for name %s", keyname);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews (void)cfg_map_get(key, "secret", &secretobj);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews (void)cfg_map_get(key, "algorithm", &algorithmobj);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (secretobj == NULL || algorithmobj == NULL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews fatal("key must have algorithm and secret");
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews secretstr = cfg_obj_asstring(secretobj);
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt algorithmstr = cfg_obj_asstring(algorithmobj);
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#ifndef PK11_MD5_DISABLE
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt if (strcasecmp(algorithmstr, "hmac-md5") == 0)
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt algorithm = ISCCC_ALG_HMACMD5;
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews else
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews#endif
c40906dfad6dd6e3a3e3c94b8c8847bc9bc064e5Mark Andrews if (strcasecmp(algorithmstr, "hmac-sha1") == 0)
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt algorithm = ISCCC_ALG_HMACSHA1;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt else if (strcasecmp(algorithmstr, "hmac-sha224") == 0)
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt algorithm = ISCCC_ALG_HMACSHA224;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt else if (strcasecmp(algorithmstr, "hmac-sha256") == 0)
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt algorithm = ISCCC_ALG_HMACSHA256;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt else if (strcasecmp(algorithmstr, "hmac-sha384") == 0)
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt algorithm = ISCCC_ALG_HMACSHA384;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt else if (strcasecmp(algorithmstr, "hmac-sha512") == 0)
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt algorithm = ISCCC_ALG_HMACSHA512;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt else
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt fatal("unsupported algorithm: %s", algorithmstr);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews secret.rstart = (unsigned char *)secretarray;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews secret.rend = (unsigned char *)secretarray + sizeof(secretarray);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews DO("decode base64 secret", isccc_base64_decode(secretstr, &secret));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews secret.rend = secret.rstart;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews secret.rstart = (unsigned char *)secretarray;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews /*
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Find the port to connect to.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (remoteport != 0)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews ; /* Was set on command line, do nothing. */
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews else {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (server != NULL)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews (void)cfg_map_get(server, "port", &defport);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (defport == NULL && options != NULL)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson (void)cfg_map_get(options, "default-port", &defport);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews }
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (defport != NULL) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews remoteport = cfg_obj_asuint32(defport);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (remoteport > 65535 || remoteport == 0)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews fatal("port %u out of range", remoteport);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews } else if (remoteport == 0)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews remoteport = NS_CONTROL_PORT;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (server != NULL)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews result = cfg_map_get(server, "addresses", &addresses);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews else
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews result = ISC_R_NOTFOUND;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (result == ISC_R_SUCCESS) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews for (element = cfg_list_first(addresses);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews element != NULL;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews element = cfg_list_next(element))
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_sockaddr_t sa;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews address = cfg_listelt_value(element);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (!cfg_obj_issockaddr(address)) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews unsigned int myport;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews const char *name;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews const cfg_obj_t *obj;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews obj = cfg_tuple_get(address, "name");
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews name = cfg_obj_asstring(obj);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews obj = cfg_tuple_get(address, "port");
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (cfg_obj_isuint32(obj)) {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews myport = cfg_obj_asuint32(obj);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (myport > ISC_UINT16_MAX ||
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews myport == 0)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews fatal("port %u out of range",
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews myport);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews } else
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews myport = remoteport;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (nserveraddrs < SERVERADDRS)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews get_addresses(name, (in_port_t) myport);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews else
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews fprintf(stderr, "too many address: "
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater "%s: dropped\n", name);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews continue;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews sa = *cfg_obj_assockaddr(address);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (isc_sockaddr_getport(&sa) == 0)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_sockaddr_setport(&sa, remoteport);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (nserveraddrs < SERVERADDRS)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews serveraddrs[nserveraddrs++] = sa;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews else {
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews char socktext[ISC_SOCKADDR_FORMATSIZE];
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews isc_sockaddr_format(&sa, socktext,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews sizeof(socktext));
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews fprintf(stderr,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews "too many address: %s: dropped\n",
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews socktext);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (!local4set && server != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews address = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews cfg_map_get(server, "source-address", &address);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (address != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local4 = *cfg_obj_assockaddr(address);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local4set = ISC_TRUE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (!local4set && options != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews address = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews cfg_map_get(options, "default-source-address", &address);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (address != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local4 = *cfg_obj_assockaddr(address);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local4set = ISC_TRUE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (!local6set && server != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews address = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews cfg_map_get(server, "source-address-v6", &address);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (address != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local6 = *cfg_obj_assockaddr(address);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local6set = ISC_TRUE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (!local6set && options != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews address = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews cfg_map_get(options, "default-source-address-v6", &address);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (address != NULL) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local6 = *cfg_obj_assockaddr(address);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local6set = ISC_TRUE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews *configp = config;
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence}
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceint
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrencemain(int argc, char **argv) {
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence isc_result_t result = ISC_R_SUCCESS;
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt isc_boolean_t show_final_mem = ISC_FALSE;
4716e94840921878b26e493576f84afe4fe08752Mark Andrews isc_taskmgr_t *taskmgr = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_task_t *task = NULL;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington isc_log_t *log = NULL;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington isc_logconfig_t *logconfig = NULL;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington isc_logdestination_t logdest;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington cfg_parser_t *pctx = NULL;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington cfg_obj_t *config = NULL;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence const char *keyname = NULL;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews struct in_addr in;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews struct in6_addr in6;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington char *p;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson size_t argslen;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence int ch;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson int i;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington result = isc_file_progname(*argv, program, sizeof(program));
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence if (result != ISC_R_SUCCESS)
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(program, "rndc", 5);
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington progname = program;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews admin_conffile = RNDC_CONFFILE;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews admin_keyfile = RNDC_KEYFILE;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_sockaddr_any(&local4);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_sockaddr_any6(&local6);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson result = isc_app_start();
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson if (result != ISC_R_SUCCESS)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson fatal("isc_app_start() failed: %s", isc_result_totext(result));
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews isc_commandline_errprint = ISC_FALSE;
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt while ((ch = isc_commandline_parse(argc, argv, "b:c:hk:Mmp:qrs:Vy:"))
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer != -1) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence switch (ch) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews case 'b':
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (inet_pton(AF_INET, isc_commandline_argument,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews &in) == 1) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_sockaddr_fromin(&local4, &in, 0);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local4set = ISC_TRUE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews } else if (inet_pton(AF_INET6, isc_commandline_argument,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews &in6) == 1) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews isc_sockaddr_fromin6(&local6, &in6, 0);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews local6set = ISC_TRUE;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews }
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews break;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence case 'c':
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence admin_conffile = isc_commandline_argument;
4144efb39046963989ad002cf88a0c195401100aJeremy Reed c_flag = ISC_TRUE;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence break;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews case 'k':
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews admin_keyfile = isc_commandline_argument;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews break;
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews
5096958739769958dd7a6b69356bf41260033873David Lawrence case 'M':
b39ad8a69bc2859b99c9f5a63d916789b566e470Andreas Gustafsson isc_mem_debugging = ISC_MEM_DEBUGTRACE;
5096958739769958dd7a6b69356bf41260033873David Lawrence break;
5096958739769958dd7a6b69356bf41260033873David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence case 'm':
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence show_final_mem = ISC_TRUE;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence break;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence case 'p':
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington remoteport = atoi(isc_commandline_argument);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (remoteport > 65535 || remoteport == 0)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence fatal("port '%s' out of range",
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence isc_commandline_argument);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence break;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt case 'q':
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt quiet = ISC_TRUE;
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt break;
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt case 'r':
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt showresult = ISC_TRUE;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt break;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence case 's':
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence servername = isc_commandline_argument;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence break;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer case 'V':
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence verbose = ISC_TRUE;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence break;
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence case 'y':
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence keyname = isc_commandline_argument;
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence break;
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence case '?':
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews if (isc_commandline_option != '?') {
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews fprintf(stderr, "%s: invalid argument -%c\n",
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews program, isc_commandline_option);
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews usage(1);
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews }
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt /* FALLTHROUGH */
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews case 'h':
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence usage(0);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence break;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence default:
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews fprintf(stderr, "%s: unhandled option -%c\n",
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews program, isc_commandline_option);
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater exit(1);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence }
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence }
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence argc -= isc_commandline_index;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence argv += isc_commandline_index;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence if (argc < 1)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence usage(1);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews isc_random_get(&serial);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt DO("create memory context", isc_mem_create(0, 0, &rndc_mctx));
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt DO("create socket manager", isc_socketmgr_create(rndc_mctx, &socketmgr));
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt DO("create task manager", isc_taskmgr_create(rndc_mctx, 1, 0, &taskmgr));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington DO("create task", isc_task_create(taskmgr, 0, &task));
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt DO("create logging context", isc_log_create(rndc_mctx, &log, &logconfig));
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington isc_log_setcontext(log);
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington DO("setting log tag", isc_log_settag(logconfig, progname));
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington logdest.file.stream = stderr;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington logdest.file.name = NULL;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington logdest.file.versions = ISC_LOG_ROLLNEVER;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington logdest.file.maximum_size = 0;
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington DO("creating log channel",
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington isc_log_createchannel(logconfig, "stderr",
1d16cf8bb8596c3e4dc1123a5bdf360bf24a272bAutomatic Updater ISC_LOG_TOFILEDESC, ISC_LOG_INFO, &logdest,
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington ISC_LOG_PRINTTAG|ISC_LOG_PRINTLEVEL));
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington DO("enabling log channel", isc_log_usechannel(logconfig, "stderr",
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington NULL, NULL));
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt parse_config(rndc_mctx, log, keyname, &pctx, &config);
d7ba3622ffa20c653ef6c8cfae42d8cd26465b7fBrian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_result_register();
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson command = *argv;
6dc32b43da2c7af5bf460a45a17d03226c162c91Michael Sawyer
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt DO("allocate data buffer",
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt isc_buffer_allocate(rndc_mctx, &databuf, 2048));
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson /*
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson * Convert argc/argv into a space-delimited command string
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson * similar to what the user might enter in interactive mode
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson * (if that were implemented).
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson */
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson argslen = 0;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson for (i = 0; i < argc; i++)
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson argslen += strlen(argv[i]) + 1;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt args = isc_mem_get(rndc_mctx, argslen);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson if (args == NULL)
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson DO("isc_mem_get", ISC_R_NOMEMORY);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson p = args;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson for (i = 0; i < argc; i++) {
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson size_t len = strlen(argv[i]);
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(p, argv[i], len);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson p += len;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson *p++ = ' ';
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson }
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson p--;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson *p++ = '\0';
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson INSIST(p == args + argslen);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews notify("%s", command);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington if (strcmp(command, "restart") == 0)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence fatal("'%s' is not implemented", command);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (nserveraddrs == 0)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews get_addresses(servername, (in_port_t) remoteport);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt DO("post event", isc_app_onrun(rndc_mctx, task, rndc_start, NULL));
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson result = isc_app_run();
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson if (result != ISC_R_SUCCESS)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson fatal("isc_app_run() failed: %s", isc_result_totext(result));
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington if (connects > 0 || sends > 0 || recvs > 0)
2ba574f329c14376d26d7c0f22c89d7a978a2625Mark Andrews isc_socket_cancel(sock, task, ISC_SOCKCANCEL_ALL);
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_task_detach(&task);
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence isc_taskmgr_destroy(&taskmgr);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington isc_socketmgr_destroy(&socketmgr);
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington isc_log_destroy(&log);
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington isc_log_setcontext(NULL);
ff2add63ae297d3c0f925f7479aefc15fd9aec31David Lawrence
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington cfg_obj_destroy(pctx, &config);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington cfg_parser_destroy(&pctx);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt isc_mem_put(rndc_mctx, args, argslen);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington isccc_ccmsg_invalidate(&ccmsg);
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews dns_name_destroy();
d32b13e0be7f01020365c83a0bd36483ace4d7c3Mark Andrews
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt isc_buffer_free(&databuf);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington if (show_final_mem)
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt isc_mem_stats(rndc_mctx, stderr);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt isc_mem_destroy(&rndc_mctx);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (failed)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (1);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (0);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}