Do not try to free NULL pointers.
[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                 case REQ_DISCONNECT: {
96                         char name[MAX_STRING_SIZE];
97                         connection_t *other;
98                         splay_node_t *node, *next;
99                         bool found = false;
100
101                         if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1)
102                                 return control_return(c, REQ_DISCONNECT, -1);
103
104                         for(node = connection_tree->head; node; node = next) {
105                                 next = node->next;
106                                 other = node->data;
107                                 if(strcmp(other->name, name))
108                                         continue;
109                                 terminate_connection(other, other->status.active);
110                                 found = true;
111                         }
112
113                         return control_return(c, REQ_DISCONNECT, found ? 0 : -2);
114                 }
115
116                 default:
117                         return send_request(c, "%d %d", CONTROL, REQ_INVALID);
118         }
119 }
120
121 bool init_control() {
122         randomize(controlcookie, sizeof controlcookie / 2);
123         bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
124         controlcookie[sizeof controlcookie - 1] = 0;
125
126         FILE *f = fopen(controlcookiename, "w");
127         if(!f) {
128                 logger(LOG_ERR, "Cannot write control socket cookie file %s: %s", controlcookiename, strerror(errno));
129                 return false;
130         }
131
132 #ifdef HAVE_FCHMOD
133         fchmod(fileno(f), 0600);
134 #else
135         chmod(controlcookiename, 0600);
136 #endif
137
138         fprintf(f, "%s %s %d\n", controlcookie, myport, getpid());
139         fclose(f);
140
141         return true;
142 }
143
144 void exit_control() {
145         unlink(controlcookiename);
146 }