Fix compiler warnings.
[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 char controlcookie[65];
32 extern char *controlcookiename;
33
34 static bool control_return(connection_t *c, int type, int error) {
35         return send_request(c, "%d %d %d", CONTROL, type, error);
36 }
37
38 static bool control_ok(connection_t *c, int type) {
39         return control_return(c, type, 0);
40 }
41
42 bool control_h(connection_t *c, char *request) {
43         int type;
44
45         if(!c->status.control || c->allow_request != CONTROL) {
46                 logger(LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
47                 return false;
48         }
49
50         if(sscanf(request, "%*d %d", &type) != 1) {
51                 logger(LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
52                 return false;
53         }
54
55         switch (type) {
56                 case REQ_STOP:
57                         event_loopexit(NULL);
58                         return control_ok(c, REQ_STOP);
59
60                 case REQ_DUMP_NODES:
61                         return dump_nodes(c);
62                         
63                 case REQ_DUMP_EDGES:
64                         return dump_edges(c);
65
66                 case REQ_DUMP_SUBNETS:
67                         return dump_subnets(c);
68
69                 case REQ_DUMP_CONNECTIONS:
70                         return dump_connections(c);
71
72                 case REQ_PURGE:
73                         purge();
74                         return control_ok(c, REQ_PURGE);
75
76                 case REQ_SET_DEBUG: {
77                         int new_level;
78                         if(sscanf(request, "%*d %*d %d", &new_level) != 1)
79                                 return false;
80                         send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
81                         if(new_level >= 0)
82                                 debug_level = new_level;
83                         return true;
84                 }
85
86                 case REQ_RETRY:
87                         retry();
88                         return control_ok(c, REQ_RETRY);
89
90                 case REQ_RELOAD:
91                         logger(LOG_NOTICE, "Got '%s' command", "reload");
92                         int result = reload_configuration();
93                         return control_return(c, REQ_RELOAD, result);
94
95                 default:
96                         return send_request(c, "%d %d", CONTROL, REQ_INVALID);
97         }
98 }
99
100 bool init_control() {
101         randomize(controlcookie, sizeof controlcookie / 2);
102         bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
103         controlcookie[sizeof controlcookie - 1] = 0;
104
105         FILE *f = fopen(controlcookiename, "w");
106         if(!f) {
107                 logger(LOG_ERR, "Cannot write control socket cookie file %s: %s", controlcookiename, strerror(errno));
108                 return false;
109         }
110
111 #ifdef HAVE_FCHMOD
112         fchmod(fileno(f), 0600);
113 #else
114         chmod(controlcookiename, 0600);
115 #endif
116
117         fprintf(f, "%s %s %d\n", controlcookie, myport, getpid());
118         fclose(f);
119
120         return true;
121 }
122
123 void exit_control() {
124         unlink(controlcookiename);
125 }