sparse fixup: warning: non-ANSI function declaration of function '...'
[tinc] / src / control.c
1 /*
2     control.c -- Control socket handling.
3     Copyright (C) 2007 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21 #include "crypto.h"
22 #include "conf.h"
23 #include "control.h"
24 #include "control_common.h"
25 #include "graph.h"
26 #include "logger.h"
27 #include "meta.h"
28 #include "net.h"
29 #include "protocol.h"
30 #include "route.h"
31 #include "splay_tree.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 char controlcookie[65];
36 extern char *controlcookiename;
37
38 static bool control_return(connection_t *c, int type, int error) {
39         return send_request(c, "%d %d %d", CONTROL, type, error);
40 }
41
42 static bool control_ok(connection_t *c, int type) {
43         return control_return(c, type, 0);
44 }
45
46 bool control_h(connection_t *c, char *request) {
47         int type;
48
49         if(!c->status.control || c->allow_request != CONTROL) {
50                 logger(LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
51                 return false;
52         }
53
54         if(sscanf(request, "%*d %d", &type) != 1) {
55                 logger(LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
56                 return false;
57         }
58
59         switch (type) {
60                 case REQ_STOP:
61                         event_loopexit(NULL);
62                         return control_ok(c, REQ_STOP);
63
64                 case REQ_DUMP_NODES:
65                         return dump_nodes(c);
66                         
67                 case REQ_DUMP_EDGES:
68                         return dump_edges(c);
69
70                 case REQ_DUMP_SUBNETS:
71                         return dump_subnets(c);
72
73                 case REQ_DUMP_CONNECTIONS:
74                         return dump_connections(c);
75
76                 case REQ_PURGE:
77                         purge();
78                         return control_ok(c, REQ_PURGE);
79
80                 case REQ_SET_DEBUG: {
81                         int new_level;
82                         if(sscanf(request, "%*d %*d %d", &new_level) != 1)
83                                 return false;
84                         send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
85                         if(new_level >= 0)
86                                 debug_level = new_level;
87                         return true;
88                 }
89
90                 case REQ_RETRY:
91                         retry();
92                         return control_ok(c, REQ_RETRY);
93
94                 case REQ_RELOAD:
95                         logger(LOG_NOTICE, "Got '%s' command", "reload");
96                         int result = reload_configuration();
97                         return control_return(c, REQ_RELOAD, result);
98
99                 case REQ_DISCONNECT: {
100                         char name[MAX_STRING_SIZE];
101                         connection_t *other;
102                         splay_node_t *node, *next;
103                         bool found = false;
104
105                         if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1)
106                                 return control_return(c, REQ_DISCONNECT, -1);
107
108                         for(node = connection_tree->head; node; node = next) {
109                                 next = node->next;
110                                 other = node->data;
111                                 if(strcmp(other->name, name))
112                                         continue;
113                                 terminate_connection(other, other->status.active);
114                                 found = true;
115                         }
116
117                         return control_return(c, REQ_DISCONNECT, found ? 0 : -2);
118                 }
119
120                 case REQ_DUMP_TRAFFIC:
121                         return dump_traffic(c);
122
123                 case REQ_PCAP:
124                         c->status.pcap = true;
125                         pcap = true;
126                         return true;
127
128                 default:
129                         return send_request(c, "%d %d", CONTROL, REQ_INVALID);
130         }
131 }
132
133 bool init_control(void) {
134         randomize(controlcookie, sizeof controlcookie / 2);
135         bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
136         controlcookie[sizeof controlcookie - 1] = 0;
137
138         FILE *f = fopen(controlcookiename, "w");
139         if(!f) {
140                 logger(LOG_ERR, "Cannot write control socket cookie file %s: %s", controlcookiename, strerror(errno));
141                 return false;
142         }
143
144 #ifdef HAVE_FCHMOD
145         fchmod(fileno(f), 0600);
146 #else
147         chmod(controlcookiename, 0600);
148 #endif
149
150         fprintf(f, "%s %s %d\n", controlcookie, myport, getpid());
151         fclose(f);
152
153         return true;
154 }
155
156 void exit_control(void) {
157         unlink(controlcookiename);
158 }