Replace the connection_tree with a connection_list.
[tinc] / src / control.c
1 /*
2     control.c -- Control socket handling.
3     Copyright (C) 2012 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 "netutl.h"
30 #include "protocol.h"
31 #include "route.h"
32 #include "splay_tree.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 char controlcookie[65];
37 extern char *pidfilename;
38
39 static bool control_return(connection_t *c, int type, int error) {
40         return send_request(c, "%d %d %d", CONTROL, type, error);
41 }
42
43 static bool control_ok(connection_t *c, int type) {
44         return control_return(c, type, 0);
45 }
46
47 bool control_h(connection_t *c, const char *request) {
48         int type;
49
50         if(!c->status.control || c->allow_request != CONTROL) {
51                 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
52                 return false;
53         }
54
55         if(sscanf(request, "%*d %d", &type) != 1) {
56                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
57                 return false;
58         }
59
60         switch (type) {
61                 case REQ_STOP:
62                         event_loopexit(NULL);
63                         return control_ok(c, REQ_STOP);
64
65                 case REQ_DUMP_NODES:
66                         return dump_nodes(c);
67                         
68                 case REQ_DUMP_EDGES:
69                         return dump_edges(c);
70
71                 case REQ_DUMP_SUBNETS:
72                         return dump_subnets(c);
73
74                 case REQ_DUMP_CONNECTIONS:
75                         return dump_connections(c);
76
77                 case REQ_PURGE:
78                         purge();
79                         return control_ok(c, REQ_PURGE);
80
81                 case REQ_SET_DEBUG: {
82                         int new_level;
83                         if(sscanf(request, "%*d %*d %d", &new_level) != 1)
84                                 return false;
85                         send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
86                         if(new_level >= 0)
87                                 debug_level = new_level;
88                         return true;
89                 }
90
91                 case REQ_RETRY:
92                         retry();
93                         return control_ok(c, REQ_RETRY);
94
95                 case REQ_RELOAD:
96                         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got '%s' command", "reload");
97                         int result = reload_configuration();
98                         return control_return(c, REQ_RELOAD, result);
99
100                 case REQ_DISCONNECT: {
101                         char name[MAX_STRING_SIZE];
102                         connection_t *other;
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(list_node_t *node = connection_list->head, *next; 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                         sscanf(request, "%*d %*d %d", &c->outmaclength);
125                         c->status.pcap = true;
126                         pcap = true;
127                         return true;
128
129                 case REQ_LOG:
130                         sscanf(request, "%*d %*d %d", &c->outcompression);
131                         c->status.log = true;
132                         logcontrol = true;
133                         return true;
134
135                 default:
136                         return send_request(c, "%d %d", CONTROL, REQ_INVALID);
137         }
138 }
139
140 bool init_control(void) {
141         randomize(controlcookie, sizeof controlcookie / 2);
142         bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
143
144         FILE *f = fopen(pidfilename, "w");
145         if(!f) {
146                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno));
147                 return false;
148         }
149
150 #ifdef HAVE_FCHMOD
151         fchmod(fileno(f), 0600);
152 #else
153         chmod(pidfilename, 0600);
154 #endif
155         // Get the address and port of the first listening socket
156
157         char *localhost = NULL;
158         sockaddr_t sa;
159         socklen_t len = sizeof sa;
160
161         // Make sure we have a valid address, and map 0.0.0.0 and :: to 127.0.0.1 and ::1.
162
163         if(getsockname(listen_socket[0].tcp, (struct sockaddr *)&sa, &len)) {
164                 xasprintf(&localhost, "127.0.0.1 port %d", myport);
165         } else {
166                 if(sa.sa.sa_family == AF_INET) {
167                         if(sa.in.sin_addr.s_addr == 0)
168                                 sa.in.sin_addr.s_addr = htonl(0x7f000001);
169                 } else if(sa.sa.sa_family == AF_INET6) {
170                         static const uint8_t zero[16] = {0};
171                         if(!memcmp(sa.in6.sin6_addr.s6_addr, zero, sizeof zero))
172                                 sa.in6.sin6_addr.s6_addr[15] = 1;
173                 }
174
175                 localhost = sockaddr2hostname(&sa);
176         }
177
178         fprintf(f, "%d %s %s\n", (int)getpid(), controlcookie, localhost);
179
180         free(localhost);
181         fclose(f);
182
183         return true;
184 }
185
186 void exit_control(void) {
187         unlink(pidfilename);
188 }