2 control.c -- Control socket handling.
3 Copyright (C) 2013 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.
23 #include "control_common.h"
35 char controlcookie[65];
37 static bool control_return(connection_t *c, int type, int error) {
38 return send_request(c, "%d %d %d", CONTROL, type, error);
41 static bool control_ok(connection_t *c, int type) {
42 return control_return(c, type, 0);
45 bool control_h(connection_t *c, const char *request) {
48 if(!c->status.control || c->allow_request != CONTROL) {
49 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
53 if(sscanf(request, "%*d %d", &type) != 1) {
54 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
61 return control_ok(c, REQ_STOP);
69 case REQ_DUMP_SUBNETS:
70 return dump_subnets(c);
72 case REQ_DUMP_CONNECTIONS:
73 return dump_connections(c);
77 return control_ok(c, REQ_PURGE);
82 if(sscanf(request, "%*d %*d %d", &new_level) != 1) {
86 send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
89 debug_level = new_level;
97 return control_ok(c, REQ_RETRY);
100 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got '%s' command", "reload");
101 int result = reload_configuration();
102 return control_return(c, REQ_RELOAD, result);
104 case REQ_DISCONNECT: {
105 char name[MAX_STRING_SIZE];
108 if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1) {
109 return control_return(c, REQ_DISCONNECT, -1);
112 for list_each(connection_t, other, &connection_list) {
113 if(strcmp(other->name, name)) {
117 terminate_connection(other, other->edge);
121 return control_return(c, REQ_DISCONNECT, found ? 0 : -2);
124 case REQ_DUMP_TRAFFIC:
125 return dump_traffic(c);
128 sscanf(request, "%*d %*d %d", &c->outmaclength);
129 c->status.pcap = true;
134 int level = 0, colorize = 0;
135 sscanf(request, "%*d %*d %d %d", &level, &colorize);
136 c->log_level = CLAMP(level, DEBUG_UNSET, DEBUG_SCARY_THINGS);
137 c->status.log = true;
138 c->status.log_color = colorize;
144 return send_request(c, "%d %d", CONTROL, REQ_INVALID);
148 bool init_control(void) {
149 randomize(controlcookie, sizeof(controlcookie) / 2);
150 bin2hex(controlcookie, controlcookie, sizeof(controlcookie) / 2);
152 // Get the address and port of the first listening socket
154 char *localhost = NULL;
156 socklen_t len = sizeof(sa);
158 // Make sure we have a valid address, and map 0.0.0.0 and :: to 127.0.0.1 and ::1.
160 if(getsockname(listen_socket[0].tcp.fd, &sa.sa, &len)) {
161 xasprintf(&localhost, "127.0.0.1 port %s", myport.tcp);
163 if(sa.sa.sa_family == AF_INET) {
164 if(sa.in.sin_addr.s_addr == 0) {
165 sa.in.sin_addr.s_addr = htonl(0x7f000001);
167 } else if(sa.sa.sa_family == AF_INET6) {
168 static const uint8_t zero[16] = {0};
170 if(!memcmp(sa.in6.sin6_addr.s6_addr, zero, sizeof(zero))) {
171 sa.in6.sin6_addr.s6_addr[15] = 1;
175 localhost = sockaddr2hostname(&sa);
178 bool wrote = write_pidfile(controlcookie, localhost);
182 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno));
187 int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
190 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(sockerrno));
194 struct sockaddr_un sa_un = {
195 .sun_family = AF_UNIX,
198 if(strlen(unixsocketname) >= sizeof(sa_un.sun_path)) {
199 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket filename %s is too long!", unixsocketname);
203 strncpy(sa_un.sun_path, unixsocketname, sizeof(sa_un.sun_path));
205 if(connect(unix_fd, (struct sockaddr *)&sa_un, sizeof(sa_un)) >= 0) {
206 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
210 unlink(unixsocketname);
212 mode_t mask = umask(0);
214 int result = bind(unix_fd, (struct sockaddr *)&sa_un, sizeof(sa_un));
218 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(sockerrno));
222 if(listen(unix_fd, 3) < 0) {
223 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(sockerrno));
227 io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
233 void exit_control(void) {
235 unlink(unixsocketname);
236 io_del(&unix_socket);
237 close(unix_socket.fd);