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