2 tincctl.c -- Controlling a running tincd
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
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "control_common.h"
32 /* The name this program was run with. */
33 char *program_name = NULL;
35 /* If nonzero, display usage information and exit. */
36 bool show_help = false;
38 /* If nonzero, print the version on standard output and exit. */
39 bool show_version = false;
41 /* If nonzero, it will attempt to kill a running tincd and exit. */
44 /* If nonzero, generate public/private keypair for this host/net. */
45 int generate_keys = 0;
47 static char *identname = NULL; /* program name for syslog */
48 static char *controlsocketname = NULL; /* pid file location */
50 char *confbase = NULL;
52 static struct option const long_options[] = {
53 {"config", required_argument, NULL, 'c'},
54 {"net", required_argument, NULL, 'n'},
55 {"help", no_argument, NULL, 1},
56 {"version", no_argument, NULL, 2},
57 {"controlsocket", required_argument, NULL, 5},
61 static void usage(bool status) {
63 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
66 printf(_("Usage: %s [options] command\n\n"), program_name);
67 printf(_("Valid options are:\n"
68 " -c, --config=DIR Read configuration options from DIR.\n"
69 " -n, --net=NETNAME Connect to net NETNAME.\n"
70 " --controlsocket=FILENAME Open control socket at FILENAME.\n"
71 " --help Display this help and exit.\n"
72 " --version Output version information and exit.\n"
74 "Valid commands are:\n"
75 " start Start tincd.\n"
77 " restart Restart tincd.\n"
78 " reload Reload configuration of running tincd.\n"
79 " pid Show PID of currently running tincd.\n"
80 " generate-keys [bits] Generate a new public/private keypair.\n"
81 " dump Dump a list of one of the following things:\n"
82 " nodes - all known nodes in the VPN\n"
83 " edges - all known connections in the VPN\n"
84 " subnets - all known subnets in the VPN\n"
85 " connections - all meta connections with ourself\n"
86 " graph - graph of the VPN in dotty format\n"
87 " purge Purge unreachable nodes\n"
88 " debug N Set debug level\n"
89 " retry Retry all outgoing connections\n"
90 " reload Partial reload of configuration\n"
92 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
96 static bool parse_options(int argc, char **argv) {
100 while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) {
102 case 0: /* long option */
105 case 'c': /* config file */
106 confbase = xstrdup(optarg);
109 case 'n': /* net name given */
110 netname = xstrdup(optarg);
113 case 1: /* show help */
117 case 2: /* show version */
121 case 5: /* open control socket here */
122 controlsocketname = xstrdup(optarg);
137 FILE *ask_and_open(const char *filename, const char *what, const char *mode) {
144 /* Check stdin and stdout */
145 if(isatty(0) && isatty(1)) {
146 /* Ask for a file and/or directory name. */
147 fprintf(stdout, _("Please enter a file to save %s to [%s]: "),
151 if(fgets(buf, sizeof buf, stdin) < 0) {
152 fprintf(stderr, _("Error while reading stdin: %s\n"),
166 if(filename[0] != '\\' && filename[0] != '/' && !strchr(filename, ':')) {
168 if(filename[0] != '/') {
170 /* The directory is a relative path or a filename. */
171 directory = get_current_dir_name();
172 snprintf(buf2, sizeof buf2, "%s/%s", directory, filename);
176 umask(0077); /* Disallow everything for group and other */
178 /* Open it first to keep the inode busy */
180 r = fopen(filename, mode);
183 fprintf(stderr, _("Error opening file `%s': %s\n"), filename, strerror(errno));
191 Generate a public/private RSA keypair, and ask for a file to store
194 static bool keygen(int bits) {
200 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
202 if(!rsa_generate(&key, bits, 0x10001)) {
203 fprintf(stderr, _("Error during key generation!\n"));
206 fprintf(stderr, _("Done.\n"));
208 asprintf(&filename, "%s/rsa_key.priv", confbase);
209 f = ask_and_open(filename, _("private RSA key"), "a");
215 /* Make it unreadable for others. */
216 fchmod(fileno(f), 0600);
220 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
222 rsa_write_pem_private_key(&key, f);
228 asprintf(&filename, "%s/hosts/%s", confbase, name);
230 asprintf(&filename, "%s/rsa_key.pub", confbase);
232 f = ask_and_open(filename, _("public RSA key"), "a");
238 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
240 rsa_write_pem_public_key(&key, f);
249 Set all files and paths according to netname
251 static void make_names(void) {
254 char installdir[1024] = "";
255 long len = sizeof installdir;
259 asprintf(&identname, "tinc.%s", netname);
261 identname = xstrdup("tinc");
264 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
265 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
267 asprintf(&logfilename, "%s/log/%s.log", identname);
270 asprintf(&confbase, "%s/%s", installdir, netname);
272 asprintf(&confbase, "%s", installdir);
281 if(!controlsocketname)
282 asprintf(&controlsocketname, "%s/run/%s.control/socket", LOCALSTATEDIR, identname);
286 asprintf(&confbase, CONFDIR "/tinc/%s", netname);
288 fprintf(stderr, _("Both netname and configuration directory given, using the latter...\n"));
291 asprintf(&confbase, CONFDIR "/tinc");
295 static int fullread(int fd, void *data, size_t datalen) {
298 while(len < datalen) {
299 rv = read(fd, data + len, datalen - len);
300 if(rv == -1 && errno == EINTR)
316 static int send_ctl_request(int fd, enum request_type type,
317 void const *outdata, size_t outdatalen,
318 int *res_errno_p, void **indata_p,
319 size_t *indatalen_p) {
320 tinc_ctl_request_t req;
322 struct iovec vector[2] = {
324 {(void*) outdata, outdatalen}
328 if(res_errno_p == NULL)
331 memset(&req, 0, sizeof req);
332 req.length = sizeof req + outdatalen;
336 while((rv = writev(fd, vector, 2)) == -1 && errno == EINTR) ;
340 if(fullread(fd, &req, sizeof req) == -1)
343 if(req.length < sizeof req) {
348 if(req.length > sizeof req) {
349 if(indata_p == NULL) {
354 indata = xmalloc(req.length - sizeof req);
356 if(fullread(fd, indata, req.length - sizeof req) == -1) {
362 if(indatalen_p != NULL)
363 *indatalen_p = req.length - sizeof req;
366 *res_errno_p = req.res_errno;
372 Send a request (with printfs)
374 static int send_ctl_request_cooked(int fd, enum request_type type,
375 void const *outdata, size_t outdatalen)
381 if(send_ctl_request(fd, type, outdata, outdatalen, &res_errno,
382 (void**) &buf, &buflen)) {
383 fprintf(stderr, _("Error sending request: %s\n"), strerror(errno));
388 printf("%*s", (int)buflen, buf);
393 fprintf(stderr, _("Server reported error: %s\n"), strerror(res_errno));
400 int main(int argc, char *argv[], char *envp[]) {
401 struct sockaddr_un addr;
402 tinc_ctl_greeting_t greeting;
406 program_name = argv[0];
408 setlocale(LC_ALL, "");
409 bindtextdomain(PACKAGE, LOCALEDIR);
412 if(!parse_options(argc, argv))
418 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
419 VERSION, __DATE__, __TIME__, PROT_CURRENT);
420 printf(_("Copyright (C) 1998-2007 Ivo Timmermans, Guus Sliepen and others.\n"
421 "See the AUTHORS file for a complete list.\n\n"
422 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
423 "and you are welcome to redistribute it under certain conditions;\n"
424 "see the file COPYING for details.\n"));
435 fprintf(stderr, _("Not enough arguments.\n"));
440 // First handle commands that don't involve connecting to a running tinc daemon.
442 if(!strcasecmp(argv[optind], "generate-keys")) {
443 return !keygen(optind > argc ? atoi(argv[optind + 1]) : 2048);
446 if(!strcasecmp(argv[optind], "start")) {
448 execve(SBINDIR "/tincd", argv, envp);
449 fprintf(stderr, _("Could not start tincd: %s"), strerror(errno));
454 * Now handle commands that do involve connecting to a running tinc daemon.
455 * Authenticate the server by ensuring the parent directory can be
456 * traversed only by root. Note this is not totally race-free unless all
457 * ancestors are writable only by trusted users, which we don't verify.
461 char *lastslash = strrchr(controlsocketname, '/');
462 if(lastslash != NULL) {
463 /* control socket is not in cwd; stat its parent */
465 result = stat(controlsocketname, &statbuf);
468 result = stat(".", &statbuf);
471 fprintf(stderr, _("Unable to check control socket directory permissions: %s\n"), strerror(errno));
475 if(statbuf.st_uid != 0 || (statbuf.st_mode & S_IXOTH) != 0 || (statbuf.st_gid != 0 && (statbuf.st_mode & S_IXGRP)) != 0) {
476 fprintf(stderr, _("Insecure permissions on control socket directory\n"));
480 if(strlen(controlsocketname) >= sizeof addr.sun_path) {
481 fprintf(stderr, _("Control socket filename too long!\n"));
485 fd = socket(PF_UNIX, SOCK_STREAM, 0);
487 fprintf(stderr, _("Cannot create UNIX socket: %s\n"), strerror(errno));
491 memset(&addr, 0, sizeof addr);
492 addr.sun_family = AF_UNIX;
493 strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
495 if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
496 fprintf(stderr, _("Cannot connect to %s: %s\n"), controlsocketname, strerror(errno));
500 if(fullread(fd, &greeting, sizeof greeting) == -1) {
501 fprintf(stderr, _("Cannot read greeting from control socket: %s\n"),
506 if(greeting.version != TINC_CTL_VERSION_CURRENT) {
507 fprintf(stderr, _("Version mismatch: server %d, client %d\n"),
508 greeting.version, TINC_CTL_VERSION_CURRENT);
512 if(!strcasecmp(argv[optind], "pid")) {
513 printf("%d\n", greeting.pid);
517 if(!strcasecmp(argv[optind], "stop")) {
518 return send_ctl_request_cooked(fd, REQ_STOP, NULL, 0) != -1;
521 if(!strcasecmp(argv[optind], "reload")) {
522 return send_ctl_request_cooked(fd, REQ_RELOAD, NULL, 0) != -1;
525 if(!strcasecmp(argv[optind], "restart")) {
526 return send_ctl_request_cooked(fd, REQ_RESTART, NULL, 0) != -1;
529 if(!strcasecmp(argv[optind], "dump")) {
530 if(argc < optind + 2) {
531 fprintf(stderr, _("Not enough arguments.\n"));
536 if(!strcasecmp(argv[optind+1], "nodes")) {
537 return send_ctl_request_cooked(fd, REQ_DUMP_NODES, NULL, 0) != -1;
540 if(!strcasecmp(argv[optind+1], "edges")) {
541 return send_ctl_request_cooked(fd, REQ_DUMP_EDGES, NULL, 0) != -1;
544 if(!strcasecmp(argv[optind+1], "subnets")) {
545 return send_ctl_request_cooked(fd, REQ_DUMP_SUBNETS, NULL, 0) != -1;
548 if(!strcasecmp(argv[optind+1], "connections")) {
549 return send_ctl_request_cooked(fd, REQ_DUMP_CONNECTIONS, NULL, 0) != -1;
552 if(!strcasecmp(argv[optind+1], "graph")) {
553 return send_ctl_request_cooked(fd, REQ_DUMP_GRAPH, NULL, 0) != -1;
556 fprintf(stderr, _("Unknown dump type '%s'.\n"), argv[optind+1]);
561 if(!strcasecmp(argv[optind], "purge")) {
562 return send_ctl_request_cooked(fd, REQ_PURGE, NULL, 0) != -1;
565 if(!strcasecmp(argv[optind], "debug")) {
568 if(argc != optind + 2) {
569 fprintf(stderr, "Invalid arguments.\n");
572 debuglevel = atoi(argv[optind+1]);
573 return send_ctl_request_cooked(fd, REQ_SET_DEBUG, &debuglevel,
574 sizeof debuglevel) != -1;
577 if(!strcasecmp(argv[optind], "retry")) {
578 return send_ctl_request_cooked(fd, REQ_RETRY, NULL, 0) != -1;
581 if(!strcasecmp(argv[optind], "reload")) {
582 return send_ctl_request_cooked(fd, REQ_RELOAD, NULL, 0) != -1;
585 fprintf(stderr, _("Unknown command `%s'.\n"), argv[optind]);