MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- $Id$
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include <sys/un.h>
-
#include "system.h"
+#include "crypto.h"
#include "conf.h"
#include "control.h"
#include "control_common.h"
+#include "graph.h"
#include "logger.h"
+#include "protocol.h"
+#include "utils.h"
#include "xalloc.h"
-static int control_socket = -1;
-static struct event control_event;
-static splay_tree_t *control_socket_tree;
-extern char *controlsocketname;
-
-static void handle_control_data(struct bufferevent *event, void *data) {
- tinc_ctl_request_t req;
- size_t size;
- tinc_ctl_request_t res;
- struct evbuffer *res_data = NULL;
-
- if(EVBUFFER_LENGTH(event->input) < sizeof(tinc_ctl_request_t))
- return;
-
- /* Copy the structure to ensure alignment */
- memcpy(&req, EVBUFFER_DATA(event->input), sizeof(tinc_ctl_request_t));
-
- if(EVBUFFER_LENGTH(event->input) < req.length)
- return;
-
- if(req.length < sizeof(tinc_ctl_request_t))
- goto failure;
-
- memset(&res, 0, sizeof res);
- res.type = req.type;
- res.id = req.id;
-
- res_data = evbuffer_new();
- if (res_data == NULL) {
- res.res_errno = ENOMEM;
- goto respond;
- }
-
- if(req.type == REQ_STOP) {
- logger(LOG_NOTICE, _("Got '%s' command"), "stop");
- event_loopexit(NULL);
- goto respond;
- }
+char controlcookie[65];
+extern char *controlcookiename;
- if(req.type == REQ_DUMP_NODES) {
- logger(LOG_NOTICE, _("Got '%s' command"), "dump nodes");
- res.res_errno = dump_nodes(res_data);
- goto respond;
- }
-
- if(req.type == REQ_DUMP_EDGES) {
- logger(LOG_NOTICE, _("Got '%s' command"), "dump edges");
- res.res_errno = dump_edges(res_data);
- goto respond;
- }
-
- if(req.type == REQ_DUMP_SUBNETS) {
- logger(LOG_NOTICE, _("Got '%s' command"), "dump subnets");
- res.res_errno = dump_subnets(res_data);
- goto respond;
- }
-
- if(req.type == REQ_DUMP_CONNECTIONS) {
- logger(LOG_NOTICE, _("Got '%s' command"), "dump connections");
- res.res_errno = dump_connections(res_data);
- goto respond;
- }
-
- if(req.type == REQ_DUMP_GRAPH) {
- logger(LOG_NOTICE, _("Got '%s' command"), "dump graph");
- res.res_errno = dump_graph(res_data);
- goto respond;
- }
-
- logger(LOG_DEBUG, _("Malformed control command received"));
- res.res_errno = EINVAL;
-
-respond:
- res.length = (sizeof res)
- + ((res_data == NULL) ? 0 : EVBUFFER_LENGTH(res_data));
- evbuffer_drain(event->input, req.length);
- if(bufferevent_write(event, &res, sizeof res) == -1)
- goto failure;
- if(res_data != NULL) {
- if(bufferevent_write_buffer(event, res_data) == -1)
- goto failure;
- evbuffer_free(res_data);
- }
- return;
-
-failure:
- logger(LOG_INFO, _("Closing control socket on error"));
- evbuffer_free(res_data);
- close(event->ev_read.ev_fd);
- splay_delete(control_socket_tree, event);
+static bool control_return(connection_t *c, int type, int error) {
+ return send_request(c, "%d %d %d", CONTROL, type, error);
}
-static void handle_control_error(struct bufferevent *event, short what, void *data) {
- if(what & EVBUFFER_EOF)
- logger(LOG_DEBUG, _("Control socket connection closed by peer"));
- else
- logger(LOG_DEBUG, _("Error while reading from control socket: %s"), strerror(errno));
-
- close(event->ev_read.ev_fd);
- splay_delete(control_socket_tree, event);
+static bool control_ok(connection_t *c, int type) {
+ return control_return(c, type, 0);
}
-static void handle_new_control_socket(int fd, short events, void *data) {
- int newfd;
- struct bufferevent *ev;
- tinc_ctl_greeting_t greeting;
-
- newfd = accept(fd, NULL, NULL);
+bool control_h(connection_t *c, char *request) {
+ int type;
- if(newfd < 0) {
- logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
- event_del(&control_event);
- return;
- }
-
- ev = bufferevent_new(newfd, handle_control_data, NULL, handle_control_error, NULL);
- if(!ev) {
- logger(LOG_ERR, _("Could not create bufferevent for new control connection: %s"), strerror(errno));
- close(newfd);
- return;
+ if(!c->status.control || c->allow_request != CONTROL) {
+ logger(LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
+ return false;
}
- memset(&greeting, 0, sizeof greeting);
- greeting.version = TINC_CTL_VERSION_CURRENT;
- if(bufferevent_write(ev, &greeting, sizeof greeting) == -1) {
- logger(LOG_ERR,
- _("Cannot send greeting for new control connection: %s"),
- strerror(errno));
- bufferevent_free(ev);
- close(newfd);
- return;
+ if(sscanf(request, "%*d %d", &type) != 1) {
+ logger(LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
+ return false;
}
- bufferevent_enable(ev, EV_READ);
- splay_insert(control_socket_tree, ev);
-
- logger(LOG_DEBUG, _("Control socket connection accepted"));
-}
-
-static int control_compare(const struct event *a, const struct event *b) {
- return a < b ? -1 : a > b ? 1 : 0;
-}
+ switch (type) {
+ case REQ_STOP:
+ event_loopexit(NULL);
+ return control_ok(c, REQ_STOP);
-bool init_control() {
- int result;
- struct sockaddr_un addr;
+ case REQ_DUMP_NODES:
+ return dump_nodes(c);
+
+ case REQ_DUMP_EDGES:
+ return dump_edges(c);
- if(strlen(controlsocketname) >= sizeof addr.sun_path) {
- logger(LOG_ERR, _("Control socket filename too long!"));
- return false;
- }
+ case REQ_DUMP_SUBNETS:
+ return dump_subnets(c);
- memset(&addr, 0, sizeof addr);
- addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
+ case REQ_DUMP_CONNECTIONS:
+ return dump_connections(c);
- control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
+ case REQ_PURGE:
+ purge();
+ return control_ok(c, REQ_PURGE);
- if(control_socket < 0) {
- logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
- return false;
- }
+ case REQ_SET_DEBUG: {
+ int new_level;
+ if(sscanf(request, "%*d %*d %d", &new_level) != 1)
+ return false;
+ send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
+ if(new_level >= 0)
+ debug_level = new_level;
+ return true;
+ }
- //unlink(controlsocketname);
- result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
-
- if(result < 0 && errno == EADDRINUSE) {
- result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
- if(result < 0) {
- logger(LOG_WARNING, _("Removing old control socket."));
- unlink(controlsocketname);
- result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
- } else {
- close(control_socket);
- if(netname)
- logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
- else
- logger(LOG_ERR, _("Another tincd is already running."));
- return false;
+ case REQ_RETRY:
+ retry();
+ return control_ok(c, REQ_RETRY);
+
+ case REQ_RELOAD:
+ logger(LOG_NOTICE, "Got '%s' command", "reload");
+ int result = reload_configuration();
+ return control_return(c, REQ_RELOAD, result);
+
+ case REQ_DISCONNECT: {
+ char name[MAX_STRING_SIZE];
+ connection_t *other;
+ splay_node_t *node, *next;
+ bool found = false;
+
+ if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1)
+ return control_return(c, REQ_DISCONNECT, -1);
+
+ for(node = connection_tree->head; node; node = next) {
+ next = node->next;
+ other = node->data;
+ if(strcmp(other->name, name))
+ continue;
+ terminate_connection(other, other->status.active);
+ found = true;
+ }
+
+ return control_return(c, REQ_DISCONNECT, found ? 0 : -2);
}
- }
- if(result < 0) {
- logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
- close(control_socket);
- return false;
+ default:
+ return send_request(c, "%d %d", CONTROL, REQ_INVALID);
}
+}
+
+bool init_control() {
+ randomize(controlcookie, sizeof controlcookie / 2);
+ bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
+ controlcookie[sizeof controlcookie - 1] = 0;
- if(listen(control_socket, 3) < 0) {
- logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
- close(control_socket);
+ FILE *f = fopen(controlcookiename, "w");
+ if(!f) {
+ logger(LOG_ERR, "Cannot write control socket cookie file %s: %s", controlcookiename, strerror(errno));
return false;
}
- control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)bufferevent_free);
+#ifdef HAVE_FCHMOD
+ fchmod(fileno(f), 0600);
+#else
+ chmod(controlcookiename, 0600);
+#endif
- event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
- event_add(&control_event, NULL);
+ fprintf(f, "%s %s %d\n", controlcookie, myport, getpid());
+ fclose(f);
return true;
}
void exit_control() {
- event_del(&control_event);
- close(control_socket);
- unlink(controlsocketname);
+ unlink(controlcookiename);
}