Update FSF address in files not covered by the merge.
[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 <sys/un.h>
21
22 #include "system.h"
23 #include "conf.h"
24 #include "control.h"
25 #include "control_common.h"
26 #include "graph.h"
27 #include "logger.h"
28 #include "xalloc.h"
29
30 static int control_socket = -1;
31 static struct event control_event;
32 static splay_tree_t *control_socket_tree;
33 extern char *controlsocketname;
34
35 static void handle_control_data(struct bufferevent *event, void *data) {
36         tinc_ctl_request_t req;
37         tinc_ctl_request_t res;
38         struct evbuffer *res_data = NULL;
39         void *req_data;
40
41         if(EVBUFFER_LENGTH(event->input) < sizeof req)
42                 return;
43
44         /* Copy the structure to ensure alignment */
45         memcpy(&req, EVBUFFER_DATA(event->input), sizeof req);
46
47         if(EVBUFFER_LENGTH(event->input) < req.length)
48                 return;
49         req_data = EVBUFFER_DATA(event->input) + sizeof req;
50
51         if(req.length < sizeof req)
52                 goto failure;
53
54         memset(&res, 0, sizeof res);
55         res.type = req.type;
56         res.id = req.id;
57
58         res_data = evbuffer_new();
59         if(res_data == NULL) {
60                 res.res_errno = ENOMEM;
61                 goto respond;
62         }
63
64         if(req.type == REQ_STOP) {
65                 logger(LOG_NOTICE, "Got '%s' command", "stop");
66                 event_loopexit(NULL);
67                 goto respond;
68         }
69
70         if(req.type == REQ_DUMP_NODES) {
71                 logger(LOG_NOTICE, "Got '%s' command", "dump nodes");
72                 res.res_errno = dump_nodes(res_data);
73                 goto respond;
74         }
75
76         if(req.type == REQ_DUMP_EDGES) {
77                 logger(LOG_NOTICE, "Got '%s' command", "dump edges");
78                 res.res_errno = dump_edges(res_data);
79                 goto respond;
80         }
81
82         if(req.type == REQ_DUMP_SUBNETS) {
83                 logger(LOG_NOTICE, "Got '%s' command", "dump subnets");
84                 res.res_errno = dump_subnets(res_data);
85                 goto respond;
86         }
87
88         if(req.type == REQ_DUMP_CONNECTIONS) {
89                 logger(LOG_NOTICE, "Got '%s' command", "dump connections");
90                 res.res_errno = dump_connections(res_data);
91                 goto respond;
92         }
93
94         if(req.type == REQ_DUMP_GRAPH) {
95                 logger(LOG_NOTICE, "Got '%s' command", "dump graph");
96                 res.res_errno = dump_graph(res_data);
97                 goto respond;
98         }
99
100         if(req.type == REQ_PURGE) {
101                 logger(LOG_NOTICE, "Got '%s' command", "purge");
102                 purge();
103                 goto respond;
104         }
105
106         if(req.type == REQ_SET_DEBUG) {
107                 debug_t new_debug_level;
108
109                 logger(LOG_NOTICE, "Got '%s' command", "debug");
110                 if(req.length != sizeof req + sizeof debug_level)
111                         res.res_errno = EINVAL;
112                 else {
113                         memcpy(&new_debug_level, req_data, sizeof new_debug_level);
114                         logger(LOG_NOTICE, "Changing debug level from %d to %d",
115                                    debug_level, new_debug_level);
116                         if(evbuffer_add_printf(res_data,
117                                                                    "Changing debug level from %d to %d\n",
118                                                                    debug_level, new_debug_level) == -1)
119                                 res.res_errno = errno;
120                         debug_level = new_debug_level;
121                 }
122                 goto respond;
123         }
124
125         if(req.type == REQ_RETRY) {
126                 logger(LOG_NOTICE, "Got '%s' command", "retry");
127                 retry();
128                 goto respond;
129         }
130
131         if(req.type == REQ_RELOAD) {
132                 logger(LOG_NOTICE, "Got '%s' command", "reload");
133                 res.res_errno = reload_configuration();
134                 goto respond;
135         }
136
137         logger(LOG_DEBUG, "Malformed control command received");
138         res.res_errno = EINVAL;
139
140 respond:
141         res.length = (sizeof res)
142                                  + ((res_data == NULL) ? 0 : EVBUFFER_LENGTH(res_data));
143         evbuffer_drain(event->input, req.length);
144         if(bufferevent_write(event, &res, sizeof res) == -1)
145                 goto failure;
146         if(res_data != NULL) {
147                 if(bufferevent_write_buffer(event, res_data) == -1)
148                         goto failure;
149                 evbuffer_free(res_data);
150         }
151         return;
152
153 failure:
154         logger(LOG_INFO, "Closing control socket on error");
155         evbuffer_free(res_data);
156         close(event->ev_read.ev_fd);
157         splay_delete(control_socket_tree, event);
158 }
159
160 static void handle_control_error(struct bufferevent *event, short what, void *data) {
161         if(what & EVBUFFER_EOF)
162                 logger(LOG_DEBUG, "Control socket connection closed by peer");
163         else
164                 logger(LOG_DEBUG, "Error while reading from control socket: %s", strerror(errno));
165
166         close(event->ev_read.ev_fd);
167         splay_delete(control_socket_tree, event);
168 }
169
170 static void handle_new_control_socket(int fd, short events, void *data) {
171         int newfd;
172         struct bufferevent *ev;
173         tinc_ctl_greeting_t greeting;
174
175         newfd = accept(fd, NULL, NULL);
176
177         if(newfd < 0) {
178                 logger(LOG_ERR, "Accepting a new connection failed: %s", strerror(errno));
179                 event_del(&control_event);
180                 return;
181         }
182
183         ev = bufferevent_new(newfd, handle_control_data, NULL, handle_control_error, NULL);
184         if(!ev) {
185                 logger(LOG_ERR, "Could not create bufferevent for new control connection: %s", strerror(errno));
186                 close(newfd);
187                 return;
188         }
189
190         memset(&greeting, 0, sizeof greeting);
191         greeting.version = TINC_CTL_VERSION_CURRENT;
192         greeting.pid = getpid();
193         if(bufferevent_write(ev, &greeting, sizeof greeting) == -1) {
194                 logger(LOG_ERR,
195                            "Cannot send greeting for new control connection: %s",
196                            strerror(errno));
197                 bufferevent_free(ev);
198                 close(newfd);
199                 return;
200         }
201
202         bufferevent_enable(ev, EV_READ);
203         splay_insert(control_socket_tree, ev);
204
205         logger(LOG_DEBUG, "Control socket connection accepted");
206 }
207
208 static int control_compare(const struct event *a, const struct event *b) {
209         return a < b ? -1 : a > b ? 1 : 0;
210 }
211
212 bool init_control() {
213         int result;
214         struct sockaddr_un addr;
215         char *lastslash;
216
217         if(strlen(controlsocketname) >= sizeof addr.sun_path) {
218                 logger(LOG_ERR, "Control socket filename too long!");
219                 goto bail;
220         }
221
222         memset(&addr, 0, sizeof addr);
223         addr.sun_family = AF_UNIX;
224         strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
225
226         control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
227
228         if(control_socket < 0) {
229                 logger(LOG_ERR, "Creating UNIX socket failed: %s", strerror(errno));
230                 goto bail;
231         }
232
233         /*
234          * Restrict connections to our control socket by ensuring the parent
235          * directory can be traversed only by root. Note this is not totally
236          * race-free unless all ancestors are writable only by trusted users,
237          * which we don't verify.
238          */
239
240         struct stat statbuf;
241         lastslash = strrchr(controlsocketname, '/');
242         if(lastslash != NULL) {
243                 *lastslash = 0; /* temporarily change controlsocketname to be dir */
244                 if(mkdir(controlsocketname, 0700) < 0 && errno != EEXIST) {
245                         logger(LOG_ERR, "Unable to create control socket directory %s: %s", controlsocketname, strerror(errno));
246                         *lastslash = '/';
247                         goto bail;
248                 }
249
250                 result = stat(controlsocketname, &statbuf);
251                 *lastslash = '/';
252         } else
253                 result = stat(".", &statbuf);
254
255         if(result < 0) {
256                 logger(LOG_ERR, "Examining control socket directory failed: %s", strerror(errno));
257                 goto bail;
258         }
259
260         if(statbuf.st_uid != 0 || (statbuf.st_mode & S_IXOTH) != 0 || (statbuf.st_gid != 0 && (statbuf.st_mode & S_IXGRP)) != 0) {
261                 logger(LOG_ERR, "Control socket directory ownership/permissions insecure.");
262                 goto bail;
263         }
264
265         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
266
267         if(result < 0 && errno == EADDRINUSE) {
268                 result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
269                 if(result < 0) {
270                         logger(LOG_WARNING, "Removing old control socket.");
271                         unlink(controlsocketname);
272                         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
273                 } else {
274                         if(netname)
275                                 logger(LOG_ERR, "Another tincd is already running for net `%s'.", netname);
276                         else
277                                 logger(LOG_ERR, "Another tincd is already running.");
278                         goto bail;
279                 }
280         }
281
282         if(result < 0) {
283                 logger(LOG_ERR, "Can't bind to %s: %s", controlsocketname, strerror(errno));
284                 goto bail;
285         }
286
287         if(listen(control_socket, 3) < 0) {
288                 logger(LOG_ERR, "Can't listen on %s: %s", controlsocketname, strerror(errno));
289                 goto bail;
290         }
291
292         control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)bufferevent_free);
293
294         event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
295         event_add(&control_event, NULL);
296         return true;
297
298 bail:
299         if(control_socket != -1) {
300                 close(control_socket);
301                 control_socket = -1;
302         }
303         return false;
304 }
305
306 void exit_control() {
307         event_del(&control_event);
308         close(control_socket);
309         unlink(controlsocketname);
310 }