2 control.c -- Control socket handling.
3 Copyright (C) 2007 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
24 #include "control_common.h"
31 char controlcookie[65];
32 extern char *controlcookiename;
34 static bool control_return(connection_t *c, int type, int error) {
35 return send_request(c, "%d %d %d", CONTROL, type, error);
38 static bool control_ok(connection_t *c, int type) {
39 return control_return(c, type, 0);
42 bool control_h(connection_t *c, char *request) {
45 if(!c->status.control || c->allow_request != CONTROL) {
46 logger(LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
50 if(sscanf(request, "%*d %d", &type) != 1) {
51 logger(LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
58 return control_ok(c, REQ_STOP);
66 case REQ_DUMP_SUBNETS:
67 return dump_subnets(c);
69 case REQ_DUMP_CONNECTIONS:
70 return dump_connections(c);
74 return control_ok(c, REQ_PURGE);
78 if(sscanf(request, "%*d %*d %d", &new_level) != 1)
80 send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
82 debug_level = new_level;
88 return control_ok(c, REQ_RETRY);
91 logger(LOG_NOTICE, "Got '%s' command", "reload");
92 int result = reload_configuration();
93 return control_return(c, REQ_RELOAD, result);
95 case REQ_DISCONNECT: {
96 char name[MAX_STRING_SIZE];
98 splay_node_t *node, *next;
101 if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1)
102 return control_return(c, REQ_DISCONNECT, -1);
104 for(node = connection_tree->head; node; node = next) {
107 if(strcmp(other->name, name))
109 terminate_connection(other, other->status.active);
113 return control_return(c, REQ_DISCONNECT, found ? 0 : -2);
117 return send_request(c, "%d %d", CONTROL, REQ_INVALID);
121 bool init_control() {
122 randomize(controlcookie, sizeof controlcookie / 2);
123 bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
124 controlcookie[sizeof controlcookie - 1] = 0;
126 FILE *f = fopen(controlcookiename, "w");
128 logger(LOG_ERR, "Cannot write control socket cookie file %s: %s", controlcookiename, strerror(errno));
133 fchmod(fileno(f), 0600);
135 chmod(controlcookiename, 0600);
138 fprintf(f, "%s %s %d\n", controlcookie, myport, getpid());
144 void exit_control() {
145 unlink(controlcookiename);