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.
27 #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 *identname = NULL; /* program name for syslog */
46 static char *controlsocketname = NULL; /* pid file location */
48 char *confbase = NULL;
50 static struct option const long_options[] = {
51 {"config", required_argument, NULL, 'c'},
52 {"net", required_argument, NULL, 'n'},
53 {"help", no_argument, NULL, 1},
54 {"version", no_argument, NULL, 2},
55 {"controlsocket", required_argument, NULL, 5},
59 static void usage(bool status) {
61 fprintf(stderr, "Try `%s --help\' for more information.\n",
64 printf("Usage: %s [options] command\n\n", program_name);
65 printf("Valid options are:\n"
66 " -c, --config=DIR Read configuration options from DIR.\n"
67 " -n, --net=NETNAME Connect to net NETNAME.\n"
68 " --controlsocket=FILENAME Open control socket at FILENAME.\n"
69 " --help Display this help and exit.\n"
70 " --version Output version information and exit.\n"
72 "Valid commands are:\n"
73 " start Start tincd.\n"
75 " restart Restart tincd.\n"
76 " reload Reload configuration of running tincd.\n"
77 " pid Show PID of currently running tincd.\n"
78 " generate-keys [bits] Generate a new public/private keypair.\n"
79 " dump Dump a list of one of the following things:\n"
80 " nodes - all known nodes in the VPN\n"
81 " edges - all known connections in the VPN\n"
82 " subnets - all known subnets in the VPN\n"
83 " connections - all meta connections with ourself\n"
84 " graph - graph of the VPN in dotty format\n"
85 " purge Purge unreachable nodes\n"
86 " debug N Set debug level\n"
87 " retry Retry all outgoing connections\n"
88 " reload Partial reload of configuration\n"
90 printf("Report bugs to tinc@tinc-vpn.org.\n");
94 static bool parse_options(int argc, char **argv) {
98 while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) {
100 case 0: /* long option */
103 case 'c': /* config file */
104 confbase = xstrdup(optarg);
107 case 'n': /* net name given */
108 netname = xstrdup(optarg);
111 case 1: /* show help */
115 case 2: /* show version */
119 case 5: /* open control socket here */
120 controlsocketname = xstrdup(optarg);
135 FILE *ask_and_open(const char *filename, const char *what, const char *mode) {
142 /* Check stdin and stdout */
143 if(isatty(0) && isatty(1)) {
144 /* Ask for a file and/or directory name. */
145 fprintf(stdout, "Please enter a file to save %s to [%s]: ",
149 if(fgets(buf, sizeof buf, stdin) < 0) {
150 fprintf(stderr, "Error while reading stdin: %s\n",
164 if(filename[0] != '\\' && filename[0] != '/' && !strchr(filename, ':')) {
166 if(filename[0] != '/') {
168 /* The directory is a relative path or a filename. */
169 directory = get_current_dir_name();
170 snprintf(buf2, sizeof buf2, "%s/%s", directory, filename);
174 umask(0077); /* Disallow everything for group and other */
176 /* Open it first to keep the inode busy */
178 r = fopen(filename, mode);
181 fprintf(stderr, "Error opening file `%s': %s\n", filename, strerror(errno));
189 Generate a public/private RSA keypair, and ask for a file to store
192 static bool keygen(int bits) {
198 fprintf(stderr, "Generating %d bits keys:\n", bits);
200 if(!rsa_generate(&key, bits, 0x10001)) {
201 fprintf(stderr, "Error during key generation!\n");
204 fprintf(stderr, "Done.\n");
206 xasprintf(&filename, "%s/rsa_key.priv", confbase);
207 f = ask_and_open(filename, "private RSA key", "a");
213 /* Make it unreadable for others. */
214 fchmod(fileno(f), 0600);
218 fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
220 rsa_write_pem_private_key(&key, f);
226 xasprintf(&filename, "%s/hosts/%s", confbase, name);
228 xasprintf(&filename, "%s/rsa_key.pub", confbase);
230 f = ask_and_open(filename, "public RSA key", "a");
236 fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
238 rsa_write_pem_public_key(&key, f);
247 Set all files and paths according to netname
249 static void make_names(void) {
252 char installdir[1024] = "";
253 long len = sizeof installdir;
257 xasprintf(&identname, "tinc.%s", netname);
259 identname = xstrdup("tinc");
262 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
263 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
265 xasprintf(&logfilename, "%s/log/%s.log", identname);
268 xasprintf(&confbase, "%s/%s", installdir, netname);
270 xasprintf(&confbase, "%s", installdir);
279 if(!controlsocketname)
280 xasprintf(&controlsocketname, "%s/run/%s.control/socket", LOCALSTATEDIR, identname);
284 xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
286 fprintf(stderr, "Both netname and configuration directory given, using the latter...\n");
289 xasprintf(&confbase, CONFDIR "/tinc");
293 static int fullread(int fd, void *data, size_t datalen) {
296 while(len < datalen) {
297 rv = read(fd, data + len, datalen - len);
298 if(rv == -1 && errno == EINTR)
314 static int send_ctl_request(int fd, enum request_type type,
315 void const *outdata, size_t outdatalen,
316 int *res_errno_p, void **indata_p,
317 size_t *indatalen_p) {
318 tinc_ctl_request_t req;
320 struct iovec vector[2] = {
322 {(void*) outdata, outdatalen}
326 if(res_errno_p == NULL)
329 memset(&req, 0, sizeof req);
330 req.length = sizeof req + outdatalen;
334 while((rv = writev(fd, vector, 2)) == -1 && errno == EINTR) ;
338 if(fullread(fd, &req, sizeof req) == -1)
341 if(req.length < sizeof req) {
346 if(req.length > sizeof req) {
347 if(indata_p == NULL) {
352 indata = xmalloc(req.length - sizeof req);
354 if(fullread(fd, indata, req.length - sizeof req) == -1) {
360 if(indatalen_p != NULL)
361 *indatalen_p = req.length - sizeof req;
364 *res_errno_p = req.res_errno;
370 Send a request (with printfs)
372 static int send_ctl_request_cooked(int fd, enum request_type type, void const *outdata, size_t outdatalen) {
377 if(send_ctl_request(fd, type, outdata, outdatalen, &res_errno,
378 (void**) &buf, &buflen)) {
379 fprintf(stderr, "Error sending request: %s\n", strerror(errno));
384 printf("%*s", (int)buflen, buf);
389 fprintf(stderr, "Server reported error: %s\n", strerror(res_errno));
396 int main(int argc, char *argv[], char *envp[]) {
397 struct sockaddr_un addr;
398 tinc_ctl_greeting_t greeting;
402 program_name = argv[0];
404 if(!parse_options(argc, argv))
410 printf("%s version %s (built %s %s, protocol %d)\n", PACKAGE,
411 VERSION, __DATE__, __TIME__, PROT_CURRENT);
412 printf("Copyright (C) 1998-2009 Ivo Timmermans, Guus Sliepen and others.\n"
413 "See the AUTHORS file for a complete list.\n\n"
414 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
415 "and you are welcome to redistribute it under certain conditions;\n"
416 "see the file COPYING for details.\n");
427 fprintf(stderr, "Not enough arguments.\n");
432 // First handle commands that don't involve connecting to a running tinc daemon.
434 if(!strcasecmp(argv[optind], "generate-keys")) {
435 return !keygen(optind > argc ? atoi(argv[optind + 1]) : 2048);
438 if(!strcasecmp(argv[optind], "start")) {
440 execve(SBINDIR "/tincd", argv, envp);
441 fprintf(stderr, "Could not start tincd: %s", strerror(errno));
446 * Now handle commands that do involve connecting to a running tinc daemon.
447 * Authenticate the server by ensuring the parent directory can be
448 * traversed only by root. Note this is not totally race-free unless all
449 * ancestors are writable only by trusted users, which we don't verify.
453 char *lastslash = strrchr(controlsocketname, '/');
454 if(lastslash != NULL) {
455 /* control socket is not in cwd; stat its parent */
457 result = stat(controlsocketname, &statbuf);
460 result = stat(".", &statbuf);
463 fprintf(stderr, "Unable to check control socket directory permissions: %s\n", strerror(errno));
467 if(statbuf.st_uid != 0 || (statbuf.st_mode & S_IXOTH) != 0 || (statbuf.st_gid != 0 && (statbuf.st_mode & S_IXGRP)) != 0) {
468 fprintf(stderr, "Insecure permissions on control socket directory\n");
472 if(strlen(controlsocketname) >= sizeof addr.sun_path) {
473 fprintf(stderr, "Control socket filename too long!\n");
477 fd = socket(PF_UNIX, SOCK_STREAM, 0);
479 fprintf(stderr, "Cannot create UNIX socket: %s\n", strerror(errno));
483 memset(&addr, 0, sizeof addr);
484 addr.sun_family = AF_UNIX;
485 strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
487 if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
488 fprintf(stderr, "Cannot connect to %s: %s\n", controlsocketname, strerror(errno));
492 if(fullread(fd, &greeting, sizeof greeting) == -1) {
493 fprintf(stderr, "Cannot read greeting from control socket: %s\n",
498 if(greeting.version != TINC_CTL_VERSION_CURRENT) {
499 fprintf(stderr, "Version mismatch: server %d, client %d\n",
500 greeting.version, TINC_CTL_VERSION_CURRENT);
504 if(!strcasecmp(argv[optind], "pid")) {
505 printf("%d\n", greeting.pid);
509 if(!strcasecmp(argv[optind], "stop")) {
510 return send_ctl_request_cooked(fd, REQ_STOP, NULL, 0) != -1;
513 if(!strcasecmp(argv[optind], "reload")) {
514 return send_ctl_request_cooked(fd, REQ_RELOAD, NULL, 0) != -1;
517 if(!strcasecmp(argv[optind], "restart")) {
518 return send_ctl_request_cooked(fd, REQ_RESTART, NULL, 0) != -1;
521 if(!strcasecmp(argv[optind], "dump")) {
522 if(argc < optind + 2) {
523 fprintf(stderr, "Not enough arguments.\n");
528 if(!strcasecmp(argv[optind+1], "nodes")) {
529 return send_ctl_request_cooked(fd, REQ_DUMP_NODES, NULL, 0) != -1;
532 if(!strcasecmp(argv[optind+1], "edges")) {
533 return send_ctl_request_cooked(fd, REQ_DUMP_EDGES, NULL, 0) != -1;
536 if(!strcasecmp(argv[optind+1], "subnets")) {
537 return send_ctl_request_cooked(fd, REQ_DUMP_SUBNETS, NULL, 0) != -1;
540 if(!strcasecmp(argv[optind+1], "connections")) {
541 return send_ctl_request_cooked(fd, REQ_DUMP_CONNECTIONS, NULL, 0) != -1;
544 if(!strcasecmp(argv[optind+1], "graph")) {
545 return send_ctl_request_cooked(fd, REQ_DUMP_GRAPH, NULL, 0) != -1;
548 fprintf(stderr, "Unknown dump type '%s'.\n", argv[optind+1]);
553 if(!strcasecmp(argv[optind], "purge")) {
554 return send_ctl_request_cooked(fd, REQ_PURGE, NULL, 0) != -1;
557 if(!strcasecmp(argv[optind], "debug")) {
560 if(argc != optind + 2) {
561 fprintf(stderr, "Invalid arguments.\n");
564 debuglevel = atoi(argv[optind+1]);
565 return send_ctl_request_cooked(fd, REQ_SET_DEBUG, &debuglevel,
566 sizeof debuglevel) != -1;
569 if(!strcasecmp(argv[optind], "retry")) {
570 return send_ctl_request_cooked(fd, REQ_RETRY, NULL, 0) != -1;
573 if(!strcasecmp(argv[optind], "reload")) {
574 return send_ctl_request_cooked(fd, REQ_RELOAD, NULL, 0) != -1;
577 fprintf(stderr, "Unknown command `%s'.\n", argv[optind]);