rndc.c revision 1a69a1a78cfaa86f3b68bbc965232b7876d4da2a
551e0d486d45ce379fe6bfdd64945342850a0835Tinderbox User/*
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Copyright (C) 2000 Internet Software Consortium.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Permission to use, copy, modify, and distribute this software for any
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * purpose with or without fee is hereby granted, provided that the above
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * copyright notice and this permission notice appear in all copies.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
f41f183f628a148860a6d1f0070208cddd45b0c6Bob Halley */
f41f183f628a148860a6d1f0070208cddd45b0c6Bob Halley
f41f183f628a148860a6d1f0070208cddd45b0c6Bob Halley/* $Id: rndc.c,v 1.11 2000/05/08 14:33:19 tale Exp $ */
f41f183f628a148860a6d1f0070208cddd45b0c6Bob Halley
9a2127f8335bad323451d7825119cd9f72e32464Mark Andrews/*
9a2127f8335bad323451d7825119cd9f72e32464Mark Andrews * Principal Author: DCL
9a2127f8335bad323451d7825119cd9f72e32464Mark Andrews */
9a2127f8335bad323451d7825119cd9f72e32464Mark Andrews
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <config.h>
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckb#include <stdlib.h>
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Hunt
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <isc/commandline.h>
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley#include <isc/mem.h>
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley#include <isc/socket.h>
c2bc56dc65b4b103a5600565680eb5f33fa4c90bMark Andrews#include <isc/string.h>
51917258dbb23cfe6069ae1cf2b7fc5aefc1e0c2Bob Halley#include <isc/task.h>
c6b3c1984caa362359862e063e7b70dfb306db7eMark Andrews#include <isc/util.h>
155a0ce6e3c9cef51b63789c34d83d9143ce01fdMark Andrews
e71905610c72f474a2943934a48f43121c79c939Evan Hunt#include <dns/confndc.h>
e71905610c72f474a2943934a48f43121c79c939Evan Hunt
6d5dcd0dc9bdbd679282b1ffc47987d24c3a1346Bob Halley#include <named/omapi.h>
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
155a0ce6e3c9cef51b63789c34d83d9143ce01fdMark Andrewschar *progname;
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halleychar *conffile = "/etc/rndc.conf";
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halleyisc_mem_t *mctx;
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrewstypedef struct ndc_object {
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews OMAPI_OBJECT_PREAMBLE;
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews} ndc_object_t;
c45abd11bb68005d8a5c56c14b16f4184c3e057dMichael Graff
c45abd11bb68005d8a5c56c14b16f4184c3e057dMichael Graffstatic ndc_object_t ndc_g_ndc;
a685d9c3b608eb768d237f20e8e8b1c5d3902146David Lawrencestatic omapi_objecttype_t *ndc_type;
feb08cecf318bba330437cfcb725f48d999804d8James Brister
7e9ccec63889f2aabee59b94307be492a77fa93bMark Andrews/*
76117ff568dc788ed24937cfea916a18db285960Mark Andrews * Send a control command to the server.
7e9ccec63889f2aabee59b94307be492a77fa93bMark Andrews */
aaaf8d4f4873d21e55c3ffb4f656203d08339865Mark Andrewsstatic isc_result_t
76117ff568dc788ed24937cfea916a18db285960Mark Andrewssend_command(omapi_object_t *manager, char *command) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt omapi_object_t *message = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(manager != NULL && command != NULL);
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
79f0eedd657d75889009226aa7ce23eeac28df5cMark Andrews /*
65c4736d9c0ebc6d9b1d991593b55566909da9cdBrian Wellington * Create a new message object to store the information that will
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews * be sent to the server.
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews */
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt result = omapi_message_create(&message);
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt if (result != ISC_R_SUCCESS)
fc80027fb54b501cdd88461bf879d078259e0226David Lawrence return (result);
a8783019814daa36dd57afe3f527462822834c3bEvan Hunt
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt /*
911ed0d3bee586b3bec42cb6c376d5cc6c0e1e24Brian Wellington * Specify the OPEN operation, with the UPDATE option if requested.
a00f9e2f50675bd43cc6a9fe2669709162a2ccb4Evan Hunt */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = omapi_object_setinteger(message, "op", OMAPI_OP_OPEN);
e334405421979688f2d838805ac67ee47bd62976Mark Andrews
911ed0d3bee586b3bec42cb6c376d5cc6c0e1e24Brian Wellington if (result == ISC_R_SUCCESS)
0cfb24736841b3e98bb25853229a0efabab88bddEvan Hunt result = omapi_object_setboolean(message, "update", ISC_TRUE);
0cfb24736841b3e98bb25853229a0efabab88bddEvan Hunt
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews /*
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Tell the server the type of the object being opened; it needs
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * to know this so that it can apply the proper object methods
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt * for lookup/setvalue.
3916872f379457fe344afb02398a009701c5016aEvan Hunt */
ce1f5b8d0ae5936fd187c1f414ff12a7e3b0aa37Andreas Gustafsson if (result == ISC_R_SUCCESS)
a69070d8fab55dbc63ba9f96c9d3e34f0ea9119aMark Andrews result = omapi_object_setstring(message, "type",
5c6762838c9e423753723ec2a963164e3d66ac32Mark Andrews NS_OMAPI_CONTROL);
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt
5c6762838c9e423753723ec2a963164e3d66ac32Mark Andrews /*
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt * Associate the ndc object with the message, so that it will have its
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt * values stuffed in the message. Without it, the OPEN operation will
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews * fail because there is no name/value pair to use as a key for looking
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley * up the desired object at the server; this is true even though the
fc80027fb54b501cdd88461bf879d078259e0226David Lawrence * particular object being accessed on the server does not need a key
7e9ccec63889f2aabee59b94307be492a77fa93bMark Andrews * to be found.
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews *
7e9ccec63889f2aabee59b94307be492a77fa93bMark Andrews * This object will also have its signal handler called with a
76117ff568dc788ed24937cfea916a18db285960Mark Andrews * "status" signal that sends the result of the operation on the
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * server.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = omapi_object_setobject(message, "object",
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews (omapi_object_t *)&ndc_g_ndc);
494576ce20cfd98d74955698cf8f7b37dce2f740Mark Andrews
76117ff568dc788ed24937cfea916a18db285960Mark Andrews /*
494576ce20cfd98d74955698cf8f7b37dce2f740Mark Andrews * Create a generic object to be the outer object for ndc_g_ndc, to
84dc4b3e7eea3e9c8fafa5f4fd632a51ee8b356fMukund Sivaraman * handle the job of storing the command and stuffing it into the
8a896bc645a6236e9a47e33fb57510a451009a90Mark Andrews * message.
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt *
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt * XXXDCL provide API so client does not need to refer to the
a8783019814daa36dd57afe3f527462822834c3bEvan Hunt * outer member -- does not even need to know about how the whole
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt * outer/inner thing works.
911ed0d3bee586b3bec42cb6c376d5cc6c0e1e24Brian Wellington */
ffafab1328b8d6a6d2e9476d35e7de07e70b6c9dMark Andrews if (result == ISC_R_SUCCESS)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = omapi_object_create(&ndc_g_ndc.outer, NULL, 0);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
0cfb24736841b3e98bb25853229a0efabab88bddEvan Hunt /*
0cfb24736841b3e98bb25853229a0efabab88bddEvan Hunt * Set the command being sent.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
3cd88f71b01833d5c2474638854dfa5c4244a22aBrian Wellington if (result == ISC_R_SUCCESS)
55e5c51e661e23e24573db84114a3837817745c9Evan Hunt result = omapi_object_setboolean((omapi_object_t *)&ndc_g_ndc,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews command, ISC_TRUE);
ce1f5b8d0ae5936fd187c1f414ff12a7e3b0aa37Andreas Gustafsson
a69070d8fab55dbc63ba9f96c9d3e34f0ea9119aMark Andrews if (result == ISC_R_SUCCESS) {
18d110413cf6416eb339c169b99159d09f690da1Brian Wellington /*
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt * Add the new message to the list of known messages. When the
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt * server's response comes back, the client will verify that
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt * the response was for a message it really sent.
4fd3e3482c7e4ce01e2cf28f13e0152c8e50d746Mark Andrews */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt omapi_message_register(message);
4fd3e3482c7e4ce01e2cf28f13e0152c8e50d746Mark Andrews
4fd3e3482c7e4ce01e2cf28f13e0152c8e50d746Mark Andrews /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Deliver the message to the server and await its
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley * response.
d2b5fe92a4755ce67f2dd6050955782b4fd6b3f3Mark Andrews */
d2b5fe92a4755ce67f2dd6050955782b4fd6b3f3Mark Andrews result = omapi_message_send(message, manager);
d2b5fe92a4755ce67f2dd6050955782b4fd6b3f3Mark Andrews }
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley /*
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt * Free the generic object and the message.
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt */
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley if (ndc_g_ndc.outer != NULL)
74cb99072c4b0ebd2ccafcfa284288fa760f7a1aMark Andrews omapi_object_dereference(&ndc_g_ndc.outer);
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley
7829fad4093f2c1985b1efb7cea00287ff015d2bckb omapi_message_unregister(message);
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Hunt omapi_object_dereference(&message);
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley return (result);
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley}
139154bd9894495042f4e748f554e9c8b98b7598Bob Halley
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley/*
193738b819e3c699f9edd18864a6810fcfcec855Andreas Gustafsson * The signal handler gets the "status" signals when the server's response
fd35453765c6494d077edca44f4d998af5fd4cfaDavid Lawrence * is processed. It also gets the "updated" signal after all the values
fd35453765c6494d077edca44f4d998af5fd4cfaDavid Lawrence * from the server have been incorporated via ndc_setvalue.
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley */
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halleystatic isc_result_t
74cb99072c4b0ebd2ccafcfa284288fa760f7a1aMark Andrewsndc_signalhandler(omapi_object_t *handle, const char *name, va_list ap) {
9f7d51ee3290e2a064d71016a6bd555b47134a7cMark Andrews ndc_object_t *ndc;
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley omapi_value_t *tv;
dbb012765c735ee0d82dedb116cdc7cf18957814Evan Hunt isc_region_t region;
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley isc_result_t result;
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley REQUIRE(handle->type == ndc_type);
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley ndc = (ndc_object_t *)handle;
922e6a3c2ac4ef900dd9dc99f0cc137f18372583Andreas Gustafsson
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley if (strcmp(name, "status") == 0) {
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley /*
f687e639f00a31e0884fb2c95a627b10b50c364bEvan Hunt * "status" is signalled with the result of the message's
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley * operation.
4880900113e4d0f17884f3fbdbf4c302e3f9ea91Bob Halley */
fd35453765c6494d077edca44f4d998af5fd4cfaDavid Lawrence ndc->waitresult = va_arg(ap, isc_result_t);
c8a42d6d81ff2fd9ad7fac94d4b85323554493c0Bob Halley
4fd3e3482c7e4ce01e2cf28f13e0152c8e50d746Mark Andrews if (ndc->waitresult != ISC_R_SUCCESS) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews fprintf(stderr, "%s: operation failed: %s",
6ef838d427c1ea858d2f09194a369134babf8b98Mark Andrews progname, isc_result_totext(ndc->waitresult));
6ef838d427c1ea858d2f09194a369134babf8b98Mark Andrews
6ef838d427c1ea858d2f09194a369134babf8b98Mark Andrews tv = va_arg(ap, omapi_value_t *);
6ef838d427c1ea858d2f09194a369134babf8b98Mark Andrews if (tv != NULL) {
6d831cd0baf7dd2c133f7f9cbf295f3d80cf63a7David Lawrence omapi_value_getregion(tv, &region);
6d831cd0baf7dd2c133f7f9cbf295f3d80cf63a7David Lawrence fprintf(stderr, " (%.*s)",
dac2799ea19735503a4d27f517d821aea210e594Andreas Gustafsson (int)region.length, region.base);
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews fprintf(stderr, "\n");
6d7585b3e948dd31bf78321d83ac0e024f44a71bMark Andrews }
6d5dcd0dc9bdbd679282b1ffc47987d24c3a1346Bob Halley
6d7585b3e948dd31bf78321d83ac0e024f44a71bMark Andrews /*
6d7585b3e948dd31bf78321d83ac0e024f44a71bMark Andrews * Even if the waitresult was not ISC_R_SUCCESS, the processing
6d5dcd0dc9bdbd679282b1ffc47987d24c3a1346Bob Halley * by the function still was.
6d7585b3e948dd31bf78321d83ac0e024f44a71bMark Andrews */
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley result = ISC_R_SUCCESS;
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley } else if (strcmp(name, "updated") == 0) {
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews /*
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley * Nothing to do, really.
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley */
4fd3e3482c7e4ce01e2cf28f13e0152c8e50d746Mark Andrews result = ISC_R_SUCCESS;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
6d5dcd0dc9bdbd679282b1ffc47987d24c3a1346Bob Halley } else {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Pass any unknown signal any internal object.
118394ef2ec7cef253c55359a3d70d202ddc2fa0Mark Andrews * (This normally does not happen; there is no
118394ef2ec7cef253c55359a3d70d202ddc2fa0Mark Andrews * inner object, nor anything else being signalled.)
bb556a9bf3a15c7160fa4f1b71773ce12b1bedf6Bob Halley */
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt fprintf(stderr, "%s: ndc_signalhandler: unknown signal: %s",
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley progname, name);
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley result = omapi_object_passsignal(handle, name, ap);
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley }
fe37278859bce157001cca14cdfa3a761b0d5012Mark Andrews
1471a520e59ac12b413afa8d8b3158a87af97aa6Bob Halley return (result);
494576ce20cfd98d74955698cf8f7b37dce2f740Mark Andrews}
798c83548f89091e3994ca5a9c8c7e9caaf89d10Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrewsstatic void
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Huntusage(void) {
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt fprintf(stderr, "\
b66b333f59cf51ef87f973084a5023acd9317fb2Evan HuntUsage: %s [-c config] [-s server] [-p port] [-m] command [command ...]\n\
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt\n\
b66b333f59cf51ef87f973084a5023acd9317fb2Evan HuntWhere command is one of the following for named:\n\
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt\n\
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt *status Display ps(1) status of named.\n\
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt *dumpdb Dump database and cache to /var/tmp/named_dump.db.\n\
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt reload Reload configuration file and zones.\n\
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews *stats Dump statistics to /var/tmp/named.stats.\n\
*trace Increment debugging level by one.\n\
*notrace Set debugging level to 0.\n\
*querylog Toggle query logging.\n\
*stop Stop the server.\n\
*restart Restart the server.\n\
\n\
* == not yet implemented\n",
progname);
}
#undef DO
#define DO(name, function) \
do { \
if (result == ISC_R_SUCCESS) { \
result = function; \
if (result != ISC_R_SUCCESS) { \
fprintf(stderr, "%s: %s: %s\n", progname, \
name, isc_result_totext(result)); \
exit(1); \
} \
} \
} while (0)
int
main(int argc, char **argv) {
isc_boolean_t show_final_mem = ISC_FALSE;
isc_result_t result = ISC_R_SUCCESS;
isc_socketmgr_t *socketmgr = NULL;
isc_taskmgr_t *taskmgr = NULL;
omapi_object_t *omapimgr = NULL;
dns_c_ndcctx_t *config = NULL;
dns_c_ndcopts_t *configopts = NULL;
dns_c_ndcserver_t *server = NULL;
dns_c_kdeflist_t *keys = NULL;
dns_c_kdef_t *key = NULL;
char *command;
const char *servername = NULL, *keyname = NULL;
const char *host = NULL, *secret = NULL;
unsigned int port = NS_OMAPI_PORT;
unsigned int algorithm;
int ch;
progname = strrchr(*argv, '/');
if (progname != NULL)
progname++;
else
progname = *argv;
while ((ch = isc_commandline_parse(argc, argv, "c:mp:s:")) != -1) {
switch (ch) {
case 'c':
conffile = isc_commandline_argument;
break;
case 'm':
show_final_mem = ISC_TRUE;
break;
case 'p':
port = atoi(isc_commandline_argument);
if (port > 65535) {
fprintf(stderr, "%s: port out of range\n",
progname);
exit(1);
}
break;
case 's':
servername = isc_commandline_argument;
break;
case '?':
usage();
exit(1);
break;
default:
fprintf(stderr, "%s: unexpected error parsing "
"command arguments: got %c\n", progname, ch);
exit(1);
break;
}
}
argc -= isc_commandline_index;
argv += isc_commandline_index;
if (argc < 1) {
usage();
exit(1);
}
DO("create memory context", isc_mem_create(0, 0, &mctx));
DO("create socket manager", isc_socketmgr_create(mctx, &socketmgr));
DO("create task manager", isc_taskmgr_create(mctx, 1, 0, &taskmgr));
DO("parse configuration", dns_c_ndcparseconf(conffile, mctx, &config));
(void)dns_c_ndcctx_getoptions(config, &configopts);
if (servername == NULL)
result = dns_c_ndcopts_getdefserver(configopts, &servername);
if (servername != NULL)
result = dns_c_ndcctx_getserver(config, servername, &server);
else {
fprintf(stderr, "%s: no server specified and no default\n",
progname);
exit (1);
}
if (server != NULL)
DO("get key for server", dns_c_ndcserver_getkey(server,
&keyname));
else if (configopts != NULL)
DO("get default key",
dns_c_ndcopts_getdefkey(configopts, &keyname));
else {
fprintf(stderr, "%s: no key for server and no default\n",
progname);
exit(1);
}
DO("get config key list", dns_c_ndcctx_getkeys(config, &keys));
DO("get key definition", dns_c_kdeflist_find(keys, keyname, &key));
/* XXX need methods for structure access? */
INSIST(key->secret != NULL);
INSIST(key->algorithm != NULL);
secret = key->secret;
if (strcasecmp(key->algorithm, "hmac-md5") == 0)
algorithm = OMAPI_AUTH_HMACMD5;
else {
fprintf(stderr, "%s: unsupported algorithm: %s\n",
progname, key->algorithm);
exit(1);
}
if (server != NULL)
(void)dns_c_ndcserver_gethost(server, &host);
if (host == NULL)
host = servername;
DO("initialize omapi", omapi_lib_init(mctx, taskmgr, socketmgr));
DO("register omapi object",
omapi_object_register(&ndc_type, "ndc",
NULL, /* setvalue */
NULL, /* getvalue */
NULL, /* destroy */
ndc_signalhandler,
NULL, /* stuffvalues */
NULL, /* lookup */
NULL, /* create */
NULL)); /* remove */
/*
* Initialize the static ndc_g_ndc variable (normally this is done
* by omapi_object_create on a dynamic variable).
*/
ndc_g_ndc.refcnt = 1;
ndc_g_ndc.type = ndc_type;
DO("register local authenticator",
omapi_auth_register(keyname, secret, algorithm));
DO("create protocol manager", omapi_object_create(&omapimgr, NULL, 0));
DO("connect", omapi_protocol_connect(omapimgr, host, port, NULL));
DO("send remote authenticator",
omapi_auth_use(omapimgr, keyname, algorithm));
/*
* Preload the waitresult as successful.
*/
ndc_g_ndc.waitresult = ISC_R_SUCCESS;
while ((command = *++argv) != NULL &&
result == ISC_R_SUCCESS &&
ndc_g_ndc.waitresult == ISC_R_SUCCESS) {
if (strcmp(command, "dumpdb") == 0) {
result = ISC_R_NOTIMPLEMENTED;
} else if (strcmp(command, "notrace") == 0) {
result = ISC_R_NOTIMPLEMENTED;
} else if (strcmp(command, "querylog") == 0 ||
strcmp(command, "qrylog") == 0) {
result = ISC_R_NOTIMPLEMENTED;
} else if (strcmp(command, "reload") == 0) {
result = send_command(omapimgr, command);
} else if (strcmp(command, "restart") == 0) {
result = ISC_R_NOTIMPLEMENTED;
} else if (strcmp(command, "stats") == 0) {
result = ISC_R_NOTIMPLEMENTED;
} else if (strcmp(command, "status") == 0) {
result = ISC_R_NOTIMPLEMENTED;
} else if (strcmp(command, "stop") == 0) {
result = ISC_R_NOTIMPLEMENTED;
} else if (strcmp(command, "trace") == 0) {
result = ISC_R_NOTIMPLEMENTED;
}
if (result == ISC_R_NOTIMPLEMENTED)
fprintf(stderr, "%s: '%s' is not yet implemented\n",
progname, command);
else if (result != ISC_R_SUCCESS)
fprintf(stderr, "%s: protocol failure: %s\n",
progname, isc_result_totext(result));
else if (ndc_g_ndc.waitresult != ISC_R_SUCCESS)
fprintf(stderr, "%s: %s command failure: %s\n",
progname, command,
isc_result_totext(ndc_g_ndc.waitresult));
else
fprintf(stdout, "%s: %s command successful\n",
progname, command);
}
/*
* Close the connection and wait to be disconnected. The connection
* is only still open if the protocol object is still attached
* to the omapimgr.
*/
if (omapimgr != NULL) {
omapi_protocol_disconnect(omapimgr, OMAPI_CLEAN_DISCONNECT);
/*
* Free the protocol manager.
*/
omapi_object_dereference(&omapimgr);
}
isc_socketmgr_destroy(&socketmgr);
isc_taskmgr_destroy(&taskmgr);
omapi_lib_destroy();
if (mctx != NULL) {
if (show_final_mem)
isc_mem_stats(mctx, stderr);
isc_mem_destroy(&mctx);
}
if (result != ISC_R_SUCCESS || ndc_g_ndc.waitresult != ISC_R_SUCCESS)
exit(1);
return (0);
}