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