Merge from vendor branch SENDMAIL:
[dragonfly.git] / contrib / bind-9.2.4rc7 / bin / named / control.c
1 /*
2  * Copyright (C) 2004  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.4 2004/04/06 01:38: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/util.h>
28
29 #include <dns/result.h>
30
31 #include <isccc/alist.h>
32 #include <isccc/cc.h>
33 #include <isccc/result.h>
34
35 #include <named/control.h>
36 #include <named/log.h>
37 #include <named/server.h>
38
39 static isc_boolean_t
40 command_compare(const char *text, const char *command) {
41         unsigned int commandlen = strlen(command);
42         if (strncasecmp(text, command, commandlen) == 0 &&
43             (text[commandlen] == '\0' ||
44              text[commandlen] == ' ' ||
45              text[commandlen] == '\t'))
46                 return (ISC_TRUE);
47         return (ISC_FALSE);
48 }
49
50 /*
51  * This function is called to process the incoming command
52  * when a control channel message is received.  
53  */
54 isc_result_t
55 ns_control_docommand(isccc_sexpr_t *message, isc_buffer_t *text) {
56         isccc_sexpr_t *data;
57         char *command;
58         isc_result_t result;
59
60         data = isccc_alist_lookup(message, "_data");
61         if (data == NULL) {
62                 /*
63                  * No data section.
64                  */
65                 return (ISC_R_FAILURE);
66         }
67
68         result = isccc_cc_lookupstring(data, "type", &command);
69         if (result != ISC_R_SUCCESS) {
70                 /*
71                  * We have no idea what this is.
72                  */
73                 return (result);
74         }
75
76         isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
77                       NS_LOGMODULE_CONTROL, ISC_LOG_DEBUG(1),
78                       "received control channel command '%s'",
79                       command);
80
81         /*
82          * Compare the 'command' parameter against all known control commands.
83          */
84         if (command_compare(command, NS_COMMAND_RELOAD)) {
85                 result = ns_server_reloadcommand(ns_g_server, command);
86         } else if (command_compare(command, NS_COMMAND_RECONFIG)) {
87                 result = ns_server_reconfigcommand(ns_g_server, command);
88         } else if (command_compare(command, NS_COMMAND_REFRESH)) {
89                 result = ns_server_refreshcommand(ns_g_server, command);
90         } else if (command_compare(command, NS_COMMAND_HALT)) {
91                 ns_server_flushonshutdown(ns_g_server, ISC_FALSE);
92                 isc_app_shutdown();
93                 result = ISC_R_SUCCESS;
94         } else if (command_compare(command, NS_COMMAND_STOP)) {
95                 ns_server_flushonshutdown(ns_g_server, ISC_TRUE);
96                 isc_app_shutdown();
97                 result = ISC_R_SUCCESS;
98         } else if (command_compare(command, NS_COMMAND_DUMPSTATS)) {
99                 result = ns_server_dumpstats(ns_g_server);
100         } else if (command_compare(command, NS_COMMAND_QUERYLOG)) {
101                 result = ns_server_togglequerylog(ns_g_server);
102         } else if (command_compare(command, NS_COMMAND_DUMPDB)) {
103                 ns_server_dumpdb(ns_g_server);
104                 result = ISC_R_SUCCESS;
105         } else if (command_compare(command, NS_COMMAND_TRACE)) {
106                 result = ns_server_setdebuglevel(ns_g_server, command);
107         } else if (command_compare(command, NS_COMMAND_NOTRACE)) {
108                 ns_g_debuglevel = 0;
109                 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
110                 result = ISC_R_SUCCESS;
111         } else if (command_compare(command, NS_COMMAND_FLUSH)) {
112                 result = ns_server_flushcache(ns_g_server, command);
113         } else if (command_compare(command, NS_COMMAND_STATUS)) {
114                 result = ns_server_status(ns_g_server, text);
115         } else if (command_compare(command, NS_COMMAND_NULL)) {
116                 result = ISC_R_SUCCESS;
117         } else {
118                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
119                               NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
120                               "unknown control channel command '%s'",
121                               command);
122                 result = DNS_R_UNKNOWNCOMMAND;
123         }
124
125         return (result);
126 }