Merge branch 'master' into 1.1
[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 "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 "protocol.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 static int control_socket = -1;
32 static struct event control_event;
33 static splay_tree_t *control_socket_tree;
34 char controlcookie[65];
35 extern char *controlcookiename;
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, char *request) {
46         int type;
47
48         if(!c->status.control || c->allow_request != CONTROL) {
49                 logger(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(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_loopexit(NULL);
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                         if(sscanf(request, "%*d %*d %d", &new_level) != 1)
82                                 return false;
83                         send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
84                         if(new_level >= 0)
85                                 debug_level = new_level;
86                         return true;
87                 }
88
89                 case REQ_RETRY:
90                         retry();
91                         return control_ok(c, REQ_RETRY);
92
93                 case REQ_RELOAD:
94                         logger(LOG_NOTICE, "Got '%s' command", "reload");
95                         int result = reload_configuration();
96                         return control_return(c, REQ_RELOAD, result);
97
98                 default:
99                         return send_request(c, "%d %d", CONTROL, REQ_INVALID);
100         }
101 }
102
103 bool init_control() {
104         randomize(controlcookie, sizeof controlcookie / 2);
105         bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
106         controlcookie[sizeof controlcookie - 1] = 0;
107
108         FILE *f = fopen(controlcookiename, "w");
109         if(!f) {
110                 logger(LOG_ERR, "Cannot write control socket cookie file %s: %s", controlcookiename, strerror(errno));
111                 return false;
112         }
113
114 #ifdef HAVE_FCHMOD
115         fchmod(f, 0600);
116 #else
117         chmod(controlcookiename, 0600);
118 #endif
119
120         fprintf(f, "%s %s %d\n", controlcookie, myport, getpid());
121         fclose(f);
122
123         return true;
124 }
125
126 void exit_control() {
127         unlink(controlcookiename);
128 }