Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / bind-9.3 / bin / named / control.c
1 /*
2  * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2001-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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: control.c,v 1.7.2.2.2.14 2005/04/29 01:04:47 marka Exp $ */
19
20 #include <config.h>
21
22 #include <string.h>
23
24 #include <isc/app.h>
25 #include <isc/event.h>
26 #include <isc/mem.h>
27 #include <isc/timer.h>
28 #include <isc/util.h>
29
30 #include <dns/result.h>
31
32 #include <isccc/alist.h>
33 #include <isccc/cc.h>
34 #include <isccc/result.h>
35
36 #include <named/control.h>
37 #include <named/log.h>
38 #include <named/os.h>
39 #include <named/server.h>
40 #ifdef HAVE_LIBSCF
41 #include <named/ns_smf_globals.h>
42 #endif
43
44 static isc_boolean_t
45 command_compare(const char *text, const char *command) {
46         unsigned int commandlen = strlen(command);
47         if (strncasecmp(text, command, commandlen) == 0 &&
48             (text[commandlen] == '\0' ||
49              text[commandlen] == ' ' ||
50              text[commandlen] == '\t'))
51                 return (ISC_TRUE);
52         return (ISC_FALSE);
53 }
54
55 /*
56  * This function is called to process the incoming command
57  * when a control channel message is received.  
58  */
59 isc_result_t
60 ns_control_docommand(isccc_sexpr_t *message, isc_buffer_t *text) {
61         isccc_sexpr_t *data;
62         char *command;
63         isc_result_t result;
64 #ifdef HAVE_LIBSCF
65         ns_smf_want_disable = 0;
66 #endif
67
68         data = isccc_alist_lookup(message, "_data");
69         if (data == NULL) {
70                 /*
71                  * No data section.
72                  */
73                 return (ISC_R_FAILURE);
74         }
75
76         result = isccc_cc_lookupstring(data, "type", &command);
77         if (result != ISC_R_SUCCESS) {
78                 /*
79                  * We have no idea what this is.
80                  */
81                 return (result);
82         }
83
84         isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
85                       NS_LOGMODULE_CONTROL, ISC_LOG_DEBUG(1),
86                       "received control channel command '%s'",
87                       command);
88
89         /*
90          * Compare the 'command' parameter against all known control commands.
91          */
92         if (command_compare(command, NS_COMMAND_RELOAD)) {
93                 result = ns_server_reloadcommand(ns_g_server, command, text);
94         } else if (command_compare(command, NS_COMMAND_RECONFIG)) {
95                 result = ns_server_reconfigcommand(ns_g_server, command);
96         } else if (command_compare(command, NS_COMMAND_REFRESH)) {
97                 result = ns_server_refreshcommand(ns_g_server, command, text);
98         } else if (command_compare(command, NS_COMMAND_RETRANSFER)) {
99                 result = ns_server_retransfercommand(ns_g_server, command);
100         } else if (command_compare(command, NS_COMMAND_HALT)) {
101 #ifdef HAVE_LIBSCF
102                 /*
103                  * If we are managed by smf(5), AND in chroot, then
104                  * we cannot connect to the smf repository, so just
105                  * return with an appropriate message back to rndc.
106                  */
107                 if (ns_smf_got_instance == 1 && ns_smf_chroot == 1) {
108                         result = ns_smf_add_message(text);
109                         return (result);
110                 }
111                 /*
112                  * If we are managed by smf(5) but not in chroot,
113                  * try to disable ourselves the smf way.
114                  */
115                 if (ns_smf_got_instance == 1 && ns_smf_chroot == 0)
116                         ns_smf_want_disable = 1;
117                 /*
118                  * If ns_smf_got_instance = 0, ns_smf_chroot
119                  * is not relevant and we fall through to
120                  * isc_app_shutdown below.
121                  */
122 #endif
123                 ns_server_flushonshutdown(ns_g_server, ISC_FALSE);
124                 ns_os_shutdownmsg(command, text);
125                 isc_app_shutdown();
126                 result = ISC_R_SUCCESS;
127         } else if (command_compare(command, NS_COMMAND_STOP)) {
128 #ifdef HAVE_LIBSCF
129                 if (ns_smf_got_instance == 1 && ns_smf_chroot == 1) {
130                         result = ns_smf_add_message(text);
131                         return (result);
132                 }
133                 if (ns_smf_got_instance == 1 && ns_smf_chroot == 0)
134                         ns_smf_want_disable = 1;
135 #endif
136                 ns_server_flushonshutdown(ns_g_server, ISC_TRUE);
137                 ns_os_shutdownmsg(command, text);
138                 isc_app_shutdown();
139                 result = ISC_R_SUCCESS;
140         } else if (command_compare(command, NS_COMMAND_DUMPSTATS)) {
141                 result = ns_server_dumpstats(ns_g_server);
142         } else if (command_compare(command, NS_COMMAND_QUERYLOG)) {
143                 result = ns_server_togglequerylog(ns_g_server);
144         } else if (command_compare(command, NS_COMMAND_DUMPDB)) {
145                 ns_server_dumpdb(ns_g_server, command);
146                 result = ISC_R_SUCCESS;
147         } else if (command_compare(command, NS_COMMAND_TRACE)) {
148                 result = ns_server_setdebuglevel(ns_g_server, command);
149         } else if (command_compare(command, NS_COMMAND_NOTRACE)) {
150                 ns_g_debuglevel = 0;
151                 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
152                 result = ISC_R_SUCCESS;
153         } else if (command_compare(command, NS_COMMAND_FLUSH)) {
154                 result = ns_server_flushcache(ns_g_server, command);
155         } else if (command_compare(command, NS_COMMAND_FLUSHNAME)) {
156                 result = ns_server_flushname(ns_g_server, command);
157         } else if (command_compare(command, NS_COMMAND_STATUS)) {
158                 result = ns_server_status(ns_g_server, text);
159         } else if (command_compare(command, NS_COMMAND_FREEZE)) {
160                 result = ns_server_freeze(ns_g_server, ISC_TRUE, command);
161         } else if (command_compare(command, NS_COMMAND_UNFREEZE) ||
162                    command_compare(command, NS_COMMAND_THAW)) {
163                 result = ns_server_freeze(ns_g_server, ISC_FALSE, command);
164         } else if (command_compare(command, NS_COMMAND_RECURSING)) {
165                 result = ns_server_dumprecursing(ns_g_server);
166         } else if (command_compare(command, NS_COMMAND_NULL)) {
167                 result = ISC_R_SUCCESS;
168         } else {
169                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
170                               NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
171                               "unknown control channel command '%s'",
172                               command);
173                 result = DNS_R_UNKNOWNCOMMAND;
174         }
175
176         return (result);
177 }