control.c revision 296611
1/*
2 * Copyright (C) 2004-2007, 2009-2013  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001-2003  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id$ */
19
20/*! \file */
21
22#include <config.h>
23
24
25#include <isc/app.h>
26#include <isc/event.h>
27#include <isc/mem.h>
28#include <isc/string.h>
29#include <isc/timer.h>
30#include <isc/util.h>
31
32#include <dns/result.h>
33
34#include <isccc/alist.h>
35#include <isccc/cc.h>
36#include <isccc/result.h>
37
38#include <named/control.h>
39#include <named/log.h>
40#include <named/os.h>
41#include <named/server.h>
42#ifdef HAVE_LIBSCF
43#include <named/ns_smf_globals.h>
44#endif
45
46static isc_boolean_t
47command_compare(const char *text, const char *command) {
48	unsigned int commandlen = strlen(command);
49	if (strncasecmp(text, command, commandlen) == 0 &&
50	    (text[commandlen] == '\0' ||
51	     text[commandlen] == ' ' ||
52	     text[commandlen] == '\t'))
53		return (ISC_TRUE);
54	return (ISC_FALSE);
55}
56
57/*%
58 * This function is called to process the incoming command
59 * when a control channel message is received.
60 */
61isc_result_t
62ns_control_docommand(isccc_sexpr_t *message, isc_buffer_t *text) {
63	isccc_sexpr_t *data;
64	char *command = NULL;
65	isc_result_t result;
66	int log_level;
67#ifdef HAVE_LIBSCF
68	ns_smf_want_disable = 0;
69#endif
70
71	data = isccc_alist_lookup(message, "_data");
72	if (!isccc_alist_alistp(data)) {
73		/*
74		 * No data section.
75		 */
76		return (ISC_R_FAILURE);
77	}
78
79	result = isccc_cc_lookupstring(data, "type", &command);
80	if (result != ISC_R_SUCCESS) {
81		/*
82		 * We have no idea what this is.
83		 */
84		return (result);
85	}
86
87	/*
88	 * Compare the 'command' parameter against all known control commands.
89	 */
90	if (command_compare(command, NS_COMMAND_NULL) ||
91	    command_compare(command, NS_COMMAND_STATUS)) {
92		log_level = ISC_LOG_DEBUG(1);
93	} else {
94		log_level = ISC_LOG_INFO;
95	}
96	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
97		      NS_LOGMODULE_CONTROL, log_level,
98		      "received control channel command '%s'",
99		      command);
100
101	if (command_compare(command, NS_COMMAND_RELOAD)) {
102		result = ns_server_reloadcommand(ns_g_server, command, text);
103	} else if (command_compare(command, NS_COMMAND_RECONFIG)) {
104		result = ns_server_reconfigcommand(ns_g_server, command);
105	} else if (command_compare(command, NS_COMMAND_REFRESH)) {
106		result = ns_server_refreshcommand(ns_g_server, command, text);
107	} else if (command_compare(command, NS_COMMAND_RETRANSFER)) {
108		result = ns_server_retransfercommand(ns_g_server,
109						     command, text);
110	} else if (command_compare(command, NS_COMMAND_HALT)) {
111#ifdef HAVE_LIBSCF
112		/*
113		 * If we are managed by smf(5), AND in chroot, then
114		 * we cannot connect to the smf repository, so just
115		 * return with an appropriate message back to rndc.
116		 */
117		if (ns_smf_got_instance == 1 && ns_smf_chroot == 1) {
118			result = ns_smf_add_message(text);
119			return (result);
120		}
121		/*
122		 * If we are managed by smf(5) but not in chroot,
123		 * try to disable ourselves the smf way.
124		 */
125		if (ns_smf_got_instance == 1 && ns_smf_chroot == 0)
126			ns_smf_want_disable = 1;
127		/*
128		 * If ns_smf_got_instance = 0, ns_smf_chroot
129		 * is not relevant and we fall through to
130		 * isc_app_shutdown below.
131		 */
132#endif
133		/* Do not flush master files */
134		ns_server_flushonshutdown(ns_g_server, ISC_FALSE);
135		ns_os_shutdownmsg(command, text);
136		isc_app_shutdown();
137		result = ISC_R_SUCCESS;
138	} else if (command_compare(command, NS_COMMAND_STOP)) {
139		/*
140		 * "stop" is the same as "halt" except it does
141		 * flush master files.
142		 */
143#ifdef HAVE_LIBSCF
144		if (ns_smf_got_instance == 1 && ns_smf_chroot == 1) {
145			result = ns_smf_add_message(text);
146			return (result);
147		}
148		if (ns_smf_got_instance == 1 && ns_smf_chroot == 0)
149			ns_smf_want_disable = 1;
150#endif
151		ns_server_flushonshutdown(ns_g_server, ISC_TRUE);
152		ns_os_shutdownmsg(command, text);
153		isc_app_shutdown();
154		result = ISC_R_SUCCESS;
155	} else if (command_compare(command, NS_COMMAND_DUMPSTATS)) {
156		result = ns_server_dumpstats(ns_g_server);
157	} else if (command_compare(command, NS_COMMAND_QUERYLOG)) {
158		result = ns_server_togglequerylog(ns_g_server, command);
159	} else if (command_compare(command, NS_COMMAND_DUMPDB)) {
160		ns_server_dumpdb(ns_g_server, command);
161		result = ISC_R_SUCCESS;
162	} else if (command_compare(command, NS_COMMAND_SECROOTS)) {
163		result = ns_server_dumpsecroots(ns_g_server, command);
164	} else if (command_compare(command, NS_COMMAND_TRACE)) {
165		result = ns_server_setdebuglevel(ns_g_server, command);
166	} else if (command_compare(command, NS_COMMAND_NOTRACE)) {
167		ns_g_debuglevel = 0;
168		isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
169		result = ISC_R_SUCCESS;
170	} else if (command_compare(command, NS_COMMAND_FLUSH)) {
171		result = ns_server_flushcache(ns_g_server, command);
172	} else if (command_compare(command, NS_COMMAND_FLUSHNAME)) {
173		result = ns_server_flushnode(ns_g_server, command, ISC_FALSE);
174	} else if (command_compare(command, NS_COMMAND_FLUSHTREE)) {
175		result = ns_server_flushnode(ns_g_server, command, ISC_TRUE);
176	} else if (command_compare(command, NS_COMMAND_STATUS)) {
177		result = ns_server_status(ns_g_server, text);
178	} else if (command_compare(command, NS_COMMAND_TSIGLIST)) {
179		result = ns_server_tsiglist(ns_g_server, text);
180	} else if (command_compare(command, NS_COMMAND_TSIGDELETE)) {
181		result = ns_server_tsigdelete(ns_g_server, command, text);
182	} else if (command_compare(command, NS_COMMAND_FREEZE)) {
183		result = ns_server_freeze(ns_g_server, ISC_TRUE, command,
184					  text);
185	} else if (command_compare(command, NS_COMMAND_UNFREEZE) ||
186		   command_compare(command, NS_COMMAND_THAW)) {
187		result = ns_server_freeze(ns_g_server, ISC_FALSE, command,
188					  text);
189	} else if (command_compare(command, NS_COMMAND_SYNC)) {
190		result = ns_server_sync(ns_g_server, command, text);
191	} else if (command_compare(command, NS_COMMAND_RECURSING)) {
192		result = ns_server_dumprecursing(ns_g_server);
193	} else if (command_compare(command, NS_COMMAND_TIMERPOKE)) {
194		result = ISC_R_SUCCESS;
195		isc_timermgr_poke(ns_g_timermgr);
196	} else if (command_compare(command, NS_COMMAND_NULL)) {
197		result = ISC_R_SUCCESS;
198	} else if (command_compare(command, NS_COMMAND_NOTIFY)) {
199		result = ns_server_notifycommand(ns_g_server, command, text);
200	} else if (command_compare(command, NS_COMMAND_VALIDATION)) {
201		result = ns_server_validation(ns_g_server, command);
202	} else if (command_compare(command, NS_COMMAND_SIGN) ||
203		   command_compare(command, NS_COMMAND_LOADKEYS)) {
204		result = ns_server_rekey(ns_g_server, command, text);
205	} else if (command_compare(command, NS_COMMAND_ADDZONE)) {
206		result = ns_server_add_zone(ns_g_server, command);
207	} else if (command_compare(command, NS_COMMAND_DELZONE)) {
208		result = ns_server_del_zone(ns_g_server, command, text);
209	} else if (command_compare(command, NS_COMMAND_SIGNING)) {
210		result = ns_server_signing(ns_g_server, command, text);
211	} else {
212		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
213			      NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
214			      "unknown control channel command '%s'",
215			      command);
216		result = DNS_R_UNKNOWNCOMMAND;
217	}
218
219	return (result);
220}
221