c0895393017bca513b7eb3d132a8b845b94888a7
[tinc] / src / control.c
1 /*
2     control.c -- Control socket handling.
3     Copyright (C) 2013 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 "conf.h"
22 #include "control.h"
23 #include "control_common.h"
24 #include "logger.h"
25 #include "names.h"
26 #include "net.h"
27 #include "netutl.h"
28 #include "protocol.h"
29 #include "route.h"
30 #include "utils.h"
31 #include "xalloc.h"
32 #include "random.h"
33 #include "pidfile.h"
34
35 char controlcookie[65];
36
37 static bool control_return(connection_t *c, int type, int error) {
38         return send_request(c, "%d %d %d", CONTROL, type, error);
39 }
40
41 static bool control_ok(connection_t *c, int type) {
42         return control_return(c, type, 0);
43 }
44
45 bool control_h(connection_t *c, const char *request) {
46         int type;
47
48         if(!c->status.control || c->allow_request != CONTROL) {
49                 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
50                 return false;
51         }
52
53         if(sscanf(request, "%*d %d", &type) != 1) {
54                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
55                 return false;
56         }
57
58         switch(type) {
59         case REQ_STOP:
60                 event_exit();
61                 return control_ok(c, REQ_STOP);
62
63         case REQ_DUMP_NODES:
64                 return dump_nodes(c);
65
66         case REQ_DUMP_EDGES:
67                 return dump_edges(c);
68
69         case REQ_DUMP_SUBNETS:
70                 return dump_subnets(c);
71
72         case REQ_DUMP_CONNECTIONS:
73                 return dump_connections(c);
74
75         case REQ_PURGE:
76                 purge();
77                 return control_ok(c, REQ_PURGE);
78
79         case REQ_SET_DEBUG: {
80                 int new_level;
81
82                 if(sscanf(request, "%*d %*d %d", &new_level) != 1) {
83                         return false;
84                 }
85
86                 send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
87
88                 if(new_level >= 0) {
89                         debug_level = new_level;
90                 }
91
92                 return true;
93         }
94
95         case REQ_RETRY:
96                 retry();
97                 return control_ok(c, REQ_RETRY);
98
99         case REQ_RELOAD:
100                 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got '%s' command", "reload");
101                 int result = reload_configuration();
102                 return control_return(c, REQ_RELOAD, result);
103
104         case REQ_DISCONNECT: {
105                 char name[MAX_STRING_SIZE];
106                 bool found = false;
107
108                 if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1) {
109                         return control_return(c, REQ_DISCONNECT, -1);
110                 }
111
112                 for list_each(connection_t, other, &connection_list) {
113                         if(strcmp(other->name, name)) {
114                                 continue;
115                         }
116
117                         terminate_connection(other, other->edge);
118                         found = true;
119                 }
120
121                 return control_return(c, REQ_DISCONNECT, found ? 0 : -2);
122         }
123
124         case REQ_DUMP_TRAFFIC:
125                 return dump_traffic(c);
126
127         case REQ_PCAP:
128                 sscanf(request, "%*d %*d %d", &c->outmaclength);
129                 c->status.pcap = true;
130                 pcap = true;
131                 return true;
132
133         case REQ_LOG:
134                 sscanf(request, "%*d %*d %d", &c->outcompression);
135                 c->status.log = true;
136                 logcontrol = true;
137                 return true;
138
139         default:
140                 return send_request(c, "%d %d", CONTROL, REQ_INVALID);
141         }
142 }
143
144 bool init_control(void) {
145         randomize(controlcookie, sizeof(controlcookie) / 2);
146         bin2hex(controlcookie, controlcookie, sizeof(controlcookie) / 2);
147
148         // Get the address and port of the first listening socket
149
150         char *localhost = NULL;
151         sockaddr_t sa = {0};
152         socklen_t len = sizeof(sa);
153
154         // Make sure we have a valid address, and map 0.0.0.0 and :: to 127.0.0.1 and ::1.
155
156         if(getsockname(listen_socket[0].tcp.fd, &sa.sa, &len)) {
157                 xasprintf(&localhost, "127.0.0.1 port %s", myport.tcp);
158         } else {
159                 if(sa.sa.sa_family == AF_INET) {
160                         if(sa.in.sin_addr.s_addr == 0) {
161                                 sa.in.sin_addr.s_addr = htonl(0x7f000001);
162                         }
163                 } else if(sa.sa.sa_family == AF_INET6) {
164                         static const uint8_t zero[16] = {0};
165
166                         if(!memcmp(sa.in6.sin6_addr.s6_addr, zero, sizeof(zero))) {
167                                 sa.in6.sin6_addr.s6_addr[15] = 1;
168                         }
169                 }
170
171                 localhost = sockaddr2hostname(&sa);
172         }
173
174         bool wrote = write_pidfile(controlcookie, localhost);
175         free(localhost);
176
177         if(!wrote) {
178                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno));
179                 return false;
180         }
181
182 #ifndef HAVE_WINDOWS
183         int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
184
185         if(unix_fd < 0) {
186                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(sockerrno));
187                 return false;
188         }
189
190         struct sockaddr_un sa_un = {
191                 .sun_family = AF_UNIX,
192         };
193
194         if(strlen(unixsocketname) >= sizeof(sa_un.sun_path)) {
195                 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket filename %s is too long!", unixsocketname);
196                 return false;
197         }
198
199         strncpy(sa_un.sun_path, unixsocketname, sizeof(sa_un.sun_path));
200
201         if(connect(unix_fd, (struct sockaddr *)&sa_un, sizeof(sa_un)) >= 0) {
202                 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
203                 return false;
204         }
205
206         unlink(unixsocketname);
207
208         mode_t mask = umask(0);
209         umask(mask | 077);
210         int result = bind(unix_fd, (struct sockaddr *)&sa_un, sizeof(sa_un));
211         umask(mask);
212
213         if(result < 0) {
214                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(sockerrno));
215                 return false;
216         }
217
218         if(listen(unix_fd, 3) < 0) {
219                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(sockerrno));
220                 return false;
221         }
222
223         io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
224 #endif
225
226         return true;
227 }
228
229 void exit_control(void) {
230 #ifndef HAVE_WINDOWS
231         unlink(unixsocketname);
232         io_del(&unix_socket);
233         close(unix_socket.fd);
234 #endif
235
236         unlink(pidfilename);
237 }