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
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 xasprintf(&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 xasprintf(&filename, "%s/hosts/%s", confbase, name);
230 xasprintf(&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 xasprintf(&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 xasprintf(&logfilename, "%s/log/%s.log", identname);
270 xasprintf(&confbase, "%s/%s", installdir, netname);
272 xasprintf(&confbase, "%s", installdir);
281 if(!controlsocketname)
282 xasprintf(&controlsocketname, "%s/run/%s.control/socket", LOCALSTATEDIR, identname);
286 xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
288 fprintf(stderr, "Both netname and configuration directory given, using the latter...\n");
291 xasprintf(&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, void const *outdata, size_t outdatalen) {
379 if(send_ctl_request(fd, type, outdata, outdatalen, &res_errno,
380 (void**) &buf, &buflen)) {
381 fprintf(stderr, "Error sending request: %s\n", strerror(errno));
386 printf("%*s", (int)buflen, buf);
391 fprintf(stderr, "Server reported error: %s\n", strerror(res_errno));
398 int main(int argc, char *argv[], char *envp[]) {
399 struct sockaddr_un addr;
400 tinc_ctl_greeting_t greeting;
404 program_name = argv[0];
406 if(!parse_options(argc, argv))
412 printf("%s version %s (built %s %s, protocol %d)\n", PACKAGE,
413 VERSION, __DATE__, __TIME__, PROT_CURRENT);
414 printf("Copyright (C) 1998-2009 Ivo Timmermans, Guus Sliepen and others.\n"
415 "See the AUTHORS file for a complete list.\n\n"
416 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
417 "and you are welcome to redistribute it under certain conditions;\n"
418 "see the file COPYING for details.\n");
429 fprintf(stderr, "Not enough arguments.\n");
434 // First handle commands that don't involve connecting to a running tinc daemon.
436 if(!strcasecmp(argv[optind], "generate-keys")) {
437 return !keygen(optind > argc ? atoi(argv[optind + 1]) : 2048);
440 if(!strcasecmp(argv[optind], "start")) {
442 execve(SBINDIR "/tincd", argv, envp);
443 fprintf(stderr, "Could not start tincd: %s", strerror(errno));
448 * Now handle commands that do involve connecting to a running tinc daemon.
449 * Authenticate the server by ensuring the parent directory can be
450 * traversed only by root. Note this is not totally race-free unless all
451 * ancestors are writable only by trusted users, which we don't verify.
455 char *lastslash = strrchr(controlsocketname, '/');
456 if(lastslash != NULL) {
457 /* control socket is not in cwd; stat its parent */
459 result = stat(controlsocketname, &statbuf);
462 result = stat(".", &statbuf);
465 fprintf(stderr, "Unable to check control socket directory permissions: %s\n", strerror(errno));
469 if(statbuf.st_uid != 0 || (statbuf.st_mode & S_IXOTH) != 0 || (statbuf.st_gid != 0 && (statbuf.st_mode & S_IXGRP)) != 0) {
470 fprintf(stderr, "Insecure permissions on control socket directory\n");
474 if(strlen(controlsocketname) >= sizeof addr.sun_path) {
475 fprintf(stderr, "Control socket filename too long!\n");
479 fd = socket(PF_UNIX, SOCK_STREAM, 0);
481 fprintf(stderr, "Cannot create UNIX socket: %s\n", strerror(errno));
485 memset(&addr, 0, sizeof addr);
486 addr.sun_family = AF_UNIX;
487 strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
489 if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
490 fprintf(stderr, "Cannot connect to %s: %s\n", controlsocketname, strerror(errno));
494 if(fullread(fd, &greeting, sizeof greeting) == -1) {
495 fprintf(stderr, "Cannot read greeting from control socket: %s\n",
500 if(greeting.version != TINC_CTL_VERSION_CURRENT) {
501 fprintf(stderr, "Version mismatch: server %d, client %d\n",
502 greeting.version, TINC_CTL_VERSION_CURRENT);
506 if(!strcasecmp(argv[optind], "pid")) {
507 printf("%d\n", greeting.pid);
511 if(!strcasecmp(argv[optind], "stop")) {
512 return send_ctl_request_cooked(fd, REQ_STOP, NULL, 0) != -1;
515 if(!strcasecmp(argv[optind], "reload")) {
516 return send_ctl_request_cooked(fd, REQ_RELOAD, NULL, 0) != -1;
519 if(!strcasecmp(argv[optind], "restart")) {
520 return send_ctl_request_cooked(fd, REQ_RESTART, NULL, 0) != -1;
523 if(!strcasecmp(argv[optind], "dump")) {
524 if(argc < optind + 2) {
525 fprintf(stderr, "Not enough arguments.\n");
530 if(!strcasecmp(argv[optind+1], "nodes")) {
531 return send_ctl_request_cooked(fd, REQ_DUMP_NODES, NULL, 0) != -1;
534 if(!strcasecmp(argv[optind+1], "edges")) {
535 return send_ctl_request_cooked(fd, REQ_DUMP_EDGES, NULL, 0) != -1;
538 if(!strcasecmp(argv[optind+1], "subnets")) {
539 return send_ctl_request_cooked(fd, REQ_DUMP_SUBNETS, NULL, 0) != -1;
542 if(!strcasecmp(argv[optind+1], "connections")) {
543 return send_ctl_request_cooked(fd, REQ_DUMP_CONNECTIONS, NULL, 0) != -1;
546 if(!strcasecmp(argv[optind+1], "graph")) {
547 return send_ctl_request_cooked(fd, REQ_DUMP_GRAPH, NULL, 0) != -1;
550 fprintf(stderr, "Unknown dump type '%s'.\n", argv[optind+1]);
555 if(!strcasecmp(argv[optind], "purge")) {
556 return send_ctl_request_cooked(fd, REQ_PURGE, NULL, 0) != -1;
559 if(!strcasecmp(argv[optind], "debug")) {
562 if(argc != optind + 2) {
563 fprintf(stderr, "Invalid arguments.\n");
566 debuglevel = atoi(argv[optind+1]);
567 return send_ctl_request_cooked(fd, REQ_SET_DEBUG, &debuglevel,
568 sizeof debuglevel) != -1;
571 if(!strcasecmp(argv[optind], "retry")) {
572 return send_ctl_request_cooked(fd, REQ_RETRY, NULL, 0) != -1;
575 if(!strcasecmp(argv[optind], "reload")) {
576 return send_ctl_request_cooked(fd, REQ_RELOAD, NULL, 0) != -1;
579 fprintf(stderr, "Unknown command `%s'.\n", argv[optind]);