2 tincctl.c -- Controlling a running tincd
3 Copyright (C) 2007-2009 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.
26 #include "control_common.h"
30 /* The name this program was run with. */
31 char *program_name = NULL;
33 /* If nonzero, display usage information and exit. */
34 bool show_help = false;
36 /* If nonzero, print the version on standard output and exit. */
37 bool show_version = false;
39 /* If nonzero, it will attempt to kill a running tincd and exit. */
42 /* If nonzero, generate public/private keypair for this host/net. */
43 int generate_keys = 0;
45 static char *name = NULL;
46 static char *identname = NULL; /* program name for syslog */
47 static char *controlcookiename = NULL; /* cookie file location */
48 static char controlcookie[1024];
50 char *confbase = NULL;
53 static struct WSAData wsa_state;
56 static struct option const long_options[] = {
57 {"config", required_argument, NULL, 'c'},
58 {"net", required_argument, NULL, 'n'},
59 {"help", no_argument, NULL, 1},
60 {"version", no_argument, NULL, 2},
61 {"controlcookie", required_argument, NULL, 5},
65 static void usage(bool status) {
67 fprintf(stderr, "Try `%s --help\' for more information.\n",
70 printf("Usage: %s [options] command\n\n", program_name);
71 printf("Valid options are:\n"
72 " -c, --config=DIR Read configuration options from DIR.\n"
73 " -n, --net=NETNAME Connect to net NETNAME.\n"
74 " --controlcookie=FILENAME Read control socket from FILENAME.\n"
75 " --help Display this help and exit.\n"
76 " --version Output version information and exit.\n"
78 "Valid commands are:\n"
79 " start Start tincd.\n"
81 " restart Restart tincd.\n"
82 " reload Reload configuration of running tincd.\n"
83 " pid Show PID of currently running tincd.\n"
84 " generate-keys [bits] Generate a new public/private keypair.\n"
85 " dump Dump a list of one of the following things:\n"
86 " nodes - all known nodes in the VPN\n"
87 " edges - all known connections in the VPN\n"
88 " subnets - all known subnets in the VPN\n"
89 " connections - all meta connections with ourself\n"
90 " graph - graph of the VPN in dotty format\n"
91 " purge Purge unreachable nodes\n"
92 " debug N Set debug level\n"
93 " retry Retry all outgoing connections\n"
94 " reload Partial reload of configuration\n"
95 " disconnect NODE Close meta connection with NODE\n"
97 printf("Report bugs to tinc@tinc-vpn.org.\n");
101 static bool parse_options(int argc, char **argv) {
103 int option_index = 0;
105 while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) {
107 case 0: /* long option */
110 case 'c': /* config file */
111 confbase = xstrdup(optarg);
114 case 'n': /* net name given */
115 netname = xstrdup(optarg);
118 case 1: /* show help */
122 case 2: /* show version */
126 case 5: /* open control socket here */
127 controlcookiename = xstrdup(optarg);
142 FILE *ask_and_open(const char *filename, const char *what, const char *mode) {
149 /* Check stdin and stdout */
150 if(isatty(0) && isatty(1)) {
151 /* Ask for a file and/or directory name. */
152 fprintf(stdout, "Please enter a file to save %s to [%s]: ",
156 if(fgets(buf, sizeof buf, stdin) < 0) {
157 fprintf(stderr, "Error while reading stdin: %s\n",
171 if(filename[0] != '\\' && filename[0] != '/' && !strchr(filename, ':')) {
173 if(filename[0] != '/') {
175 /* The directory is a relative path or a filename. */
176 directory = get_current_dir_name();
177 snprintf(buf2, sizeof buf2, "%s/%s", directory, filename);
181 umask(0077); /* Disallow everything for group and other */
183 /* Open it first to keep the inode busy */
185 r = fopen(filename, mode);
188 fprintf(stderr, "Error opening file `%s': %s\n", filename, strerror(errno));
196 Generate a public/private RSA keypair, and ask for a file to store
199 static bool keygen(int bits) {
204 fprintf(stderr, "Generating %d bits keys:\n", bits);
206 if(!rsa_generate(&key, bits, 0x10001)) {
207 fprintf(stderr, "Error during key generation!\n");
210 fprintf(stderr, "Done.\n");
212 xasprintf(&filename, "%s/rsa_key.priv", confbase);
213 f = ask_and_open(filename, "private RSA key", "a");
219 /* Make it unreadable for others. */
220 fchmod(fileno(f), 0600);
224 fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
226 rsa_write_pem_private_key(&key, f);
232 xasprintf(&filename, "%s/hosts/%s", confbase, name);
234 xasprintf(&filename, "%s/rsa_key.pub", confbase);
236 f = ask_and_open(filename, "public RSA key", "a");
242 fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
244 rsa_write_pem_public_key(&key, f);
253 Set all files and paths according to netname
255 static void make_names(void) {
258 char installdir[1024] = "";
259 long len = sizeof installdir;
263 xasprintf(&identname, "tinc.%s", netname);
265 identname = xstrdup("tinc");
268 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
269 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
272 xasprintf(&confbase, "%s/%s", installdir, netname);
274 xasprintf(&confbase, "%s", installdir);
277 if(!controlcookiename)
278 xasprintf(&controlcookiename, "%s/cookie", confbase);
285 if(!controlcookiename)
286 xasprintf(&controlcookiename, "%s/run/%s.cookie", LOCALSTATEDIR, identname);
290 xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
292 fprintf(stderr, "Both netname and configuration directory given, using the latter...\n");
295 xasprintf(&confbase, CONFDIR "/tinc");
299 static bool recvline(int fd, char *line, size_t len) {
300 static char buffer[4096];
301 static size_t blen = 0;
302 char *newline = NULL;
304 while(!(newline = memchr(buffer, '\n', blen))) {
305 int result = recv(fd, buffer + blen, sizeof buffer - blen, 0);
306 if(result == -1 && errno == EINTR)
313 if(newline - buffer >= len)
316 len = newline - buffer;
318 memcpy(line, buffer, len);
320 memmove(buffer, newline + 1, blen - len - 1);
326 static bool sendline(int fd, char *format, ...) {
327 static char buffer[4096];
332 va_start(ap, format);
333 blen = vsnprintf(buffer, sizeof buffer, format, ap);
336 if(blen < 0 || blen >= sizeof buffer)
343 int result = send(fd, p, blen, 0);
344 if(result == -1 && errno == EINTR)
346 else if(result <= 0);
355 int main(int argc, char *argv[], char *envp[]) {
361 program_name = argv[0];
363 if(!parse_options(argc, argv))
369 printf("%s version %s (built %s %s, protocol %d)\n", PACKAGE,
370 VERSION, __DATE__, __TIME__, PROT_CURRENT);
371 printf("Copyright (C) 1998-2009 Ivo Timmermans, Guus Sliepen and others.\n"
372 "See the AUTHORS file for a complete list.\n\n"
373 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
374 "and you are welcome to redistribute it under certain conditions;\n"
375 "see the file COPYING for details.\n");
386 fprintf(stderr, "Not enough arguments.\n");
391 // First handle commands that don't involve connecting to a running tinc daemon.
393 if(!strcasecmp(argv[optind], "generate-keys")) {
394 return !keygen(optind > argc ? atoi(argv[optind + 1]) : 2048);
397 if(!strcasecmp(argv[optind], "start")) {
399 execve(SBINDIR "/tincd", argv, envp);
400 fprintf(stderr, "Could not start tincd: %s", strerror(errno));
405 * Now handle commands that do involve connecting to a running tinc daemon.
406 * Authenticate the server by ensuring the parent directory can be
407 * traversed only by root. Note this is not totally race-free unless all
408 * ancestors are writable only by trusted users, which we don't verify.
411 FILE *f = fopen(controlcookiename, "r");
413 fprintf(stderr, "Could not open control socket cookie file %s: %s\n", controlcookiename, strerror(errno));
416 if(fscanf(f, "%1024s %d %d", controlcookie, &port, &pid) != 3) {
417 fprintf(stderr, "Could not parse control socket cookie file %s\n", controlcookiename);
422 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
423 fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
428 struct sockaddr_in addr;
429 memset(&addr, 0, sizeof addr);
430 addr.sin_family = AF_INET;
431 addr.sin_addr.s_addr = htonl(0x7f000001);
432 addr.sin_port = htons(port);
434 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
436 fprintf(stderr, "Cannot create TCP socket: %s\n", sockstrerror(sockerrno));
441 unsigned long arg = 0;
443 if(ioctlsocket(fd, FIONBIO, &arg) != 0) {
444 fprintf(stderr, "ioctlsocket failed: %s", sockstrerror(sockerrno));
448 if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
450 fprintf(stderr, "Cannot connect to %s: %s\n", controlcookiename, sockstrerror(sockerrno));
456 int code, version, req;
458 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %s %d", &code, data, &version) != 3 || code != 0) {
459 fprintf(stderr, "Cannot read greeting from control socket: %s\n",
460 sockstrerror(sockerrno));
464 sendline(fd, "%d ^%s %d", ID, controlcookie, TINC_CTL_VERSION_CURRENT);
466 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &version, &pid) != 3 || code != 4 || version != TINC_CTL_VERSION_CURRENT) {
467 fprintf(stderr, "Could not fully establish control socket connection\n");
471 if(!strcasecmp(argv[optind], "pid")) {
476 if(!strcasecmp(argv[optind], "stop")) {
477 sendline(fd, "%d %d", CONTROL, REQ_STOP);
478 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_STOP || result) {
479 fprintf(stderr, "Could not stop tinc daemon\n");
485 if(!strcasecmp(argv[optind], "reload")) {
486 sendline(fd, "%d %d", CONTROL, REQ_RELOAD);
487 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RELOAD || result) {
488 fprintf(stderr, "Could not reload tinc daemon\n");
494 if(!strcasecmp(argv[optind], "restart")) {
495 sendline(fd, "%d %d", CONTROL, REQ_RESTART);
496 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RESTART || result) {
497 fprintf(stderr, "Could not restart tinc daemon\n");
503 if(!strcasecmp(argv[optind], "retry")) {
504 sendline(fd, "%d %d", CONTROL, REQ_RETRY);
505 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RETRY || result) {
506 fprintf(stderr, "Could not retry outgoing connections\n");
512 if(!strcasecmp(argv[optind], "dump")) {
513 if(argc < optind + 2) {
514 fprintf(stderr, "Not enough arguments.\n");
519 bool do_graph = false;
522 if(!strcasecmp(argv[optind+1], "nodes"))
523 sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
524 else if(!strcasecmp(argv[optind+1], "edges"))
525 sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
526 else if(!strcasecmp(argv[optind+1], "subnets"))
527 sendline(fd, "%d %d", CONTROL, REQ_DUMP_SUBNETS);
528 else if(!strcasecmp(argv[optind+1], "connections"))
529 sendline(fd, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);
530 else if(!strcasecmp(argv[optind+1], "graph")) {
531 sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
532 sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
535 printf("digraph {\n");
537 fprintf(stderr, "Unknown dump type '%s'.\n", argv[optind+1]);
542 while(recvline(fd, line, sizeof line)) {
543 char node1[4096], node2[4096];
544 int n = sscanf(line, "%d %d %s to %s", &code, &req, node1, node2);
546 if(do_graph && req == REQ_DUMP_NODES)
558 printf("%s\n", line + 5);
560 if(req == REQ_DUMP_NODES)
561 printf(" %s [label = \"%s\"];\n", node1, node1);
563 printf(" %s -> %s;\n", node1, node2);
567 fprintf(stderr, "Error receiving dump\n");
571 if(!strcasecmp(argv[optind], "purge")) {
572 sendline(fd, "%d %d", CONTROL, REQ_PURGE);
573 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_PURGE || result) {
574 fprintf(stderr, "Could not purge tinc daemon\n");
580 if(!strcasecmp(argv[optind], "debug")) {
581 int debuglevel, origlevel;
583 if(argc != optind + 2) {
584 fprintf(stderr, "Invalid arguments.\n");
587 debuglevel = atoi(argv[optind+1]);
589 sendline(fd, "%d %d %d", CONTROL, REQ_SET_DEBUG, debuglevel);
590 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &origlevel) != 3 || code != CONTROL || req != REQ_SET_DEBUG) {
591 fprintf(stderr, "Could not purge tinc daemon\n");
595 fprintf(stderr, "Old level %d, new level %d\n", origlevel, debuglevel);
599 if(!strcasecmp(argv[optind], "connect")) {
600 if(argc != optind + 2) {
601 fprintf(stderr, "Invalid arguments.\n");
604 char *name = argv[optind + 1];
606 sendline(fd, "%d %d %s", CONTROL, REQ_CONNECT, name);
607 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_CONNECT || result) {
608 fprintf(stderr, "Could not connect to %s\n", name);
614 if(!strcasecmp(argv[optind], "disconnect")) {
615 if(argc != optind + 2) {
616 fprintf(stderr, "Invalid arguments.\n");
619 char *name = argv[optind + 1];
621 sendline(fd, "%d %d %s", CONTROL, REQ_DISCONNECT, name);
622 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_DISCONNECT || result) {
623 fprintf(stderr, "Could not disconnect %s\n", name);
629 fprintf(stderr, "Unknown command `%s'.\n", argv[optind]);