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.
25 #include <openssl/rand.h>
26 #include <openssl/rsa.h>
27 #include <openssl/pem.h>
28 #include <openssl/evp.h>
29 #include <openssl/engine.h>
36 /* The name this program was run with. */
37 char *program_name = NULL;
39 /* If nonzero, display usage information and exit. */
40 bool show_help = false;
42 /* If nonzero, print the version on standard output and exit. */
43 bool show_version = false;
45 /* If nonzero, it will attempt to kill a running tincd and exit. */
48 /* If nonzero, generate public/private keypair for this host/net. */
49 int generate_keys = 0;
51 static char *identname = NULL; /* program name for syslog */
52 static char *controlsocketname = NULL; /* pid file location */
54 char *confbase = NULL;
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 {"controlsocket", 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 " --controlsocket=FILENAME Open control socket at 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"
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));
190 /* This function prettyprints the key generation process */
192 static void indicator(int a, int b, void *p) {
195 fprintf(stderr, ".");
199 fprintf(stderr, "+");
203 fprintf(stderr, "-");
209 fprintf(stderr, " p\n");
213 fprintf(stderr, " q\n");
217 fprintf(stderr, "?");
222 fprintf(stderr, "?");
227 Generate a public/private RSA keypair, and ask for a file to store
230 static bool keygen(int bits) {
236 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
237 rsa_key = RSA_generate_key(bits, 0x10001, indicator, NULL);
240 fprintf(stderr, _("Error during key generation!\n"));
243 fprintf(stderr, _("Done.\n"));
245 asprintf(&filename, "%s/rsa_key.priv", confbase);
246 f = ask_and_open(filename, _("private RSA key"), "a");
252 /* Make it unreadable for others. */
253 fchmod(fileno(f), 0600);
257 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
259 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
264 asprintf(&filename, "%s/hosts/%s", confbase, name);
266 asprintf(&filename, "%s/rsa_key.pub", confbase);
268 f = ask_and_open(filename, _("public RSA key"), "a");
274 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
276 PEM_write_RSAPublicKey(f, rsa_key);
284 Set all files and paths according to netname
286 static void make_names(void) {
289 char installdir[1024] = "";
290 long len = sizeof(installdir);
294 asprintf(&identname, "tinc.%s", netname);
296 identname = xstrdup("tinc");
299 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
300 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
302 asprintf(&logfilename, "%s/log/%s.log", identname);
305 asprintf(&confbase, "%s/%s", installdir, netname);
307 asprintf(&confbase, "%s", installdir);
316 if(!controlsocketname)
317 asprintf(&controlsocketname, LOCALSTATEDIR "/run/%s.control", identname);
321 asprintf(&confbase, CONFDIR "/tinc/%s", netname);
323 fprintf(stderr, _("Both netname and configuration directory given, using the latter...\n"));
326 asprintf(&confbase, CONFDIR "/tinc");
330 int main(int argc, char *argv[], char *envp[]) {
332 struct sockaddr_un addr;
333 program_name = argv[0];
335 setlocale(LC_ALL, "");
336 bindtextdomain(PACKAGE, LOCALEDIR);
339 if(!parse_options(argc, argv))
345 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
346 VERSION, __DATE__, __TIME__, PROT_CURRENT);
347 printf(_("Copyright (C) 1998-2007 Ivo Timmermans, Guus Sliepen and others.\n"
348 "See the AUTHORS file for a complete list.\n\n"
349 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
350 "and you are welcome to redistribute it under certain conditions;\n"
351 "see the file COPYING for details.\n"));
362 fprintf(stderr, _("Not enough arguments.\n"));
367 // First handle commands that don't involve connecting to a running tinc daemon.
369 if(!strcasecmp(argv[optind], "generate-keys")) {
370 return !keygen(optind > argc ? atoi(argv[optind + 1]) : 1024);
373 if(!strcasecmp(argv[optind], "start")) {
375 execve("tincd", argv, envp);
376 fprintf(stderr, _("Could not start tincd: %s"), strerror(errno));
380 // Now handle commands that do involve connecting to a running tinc daemon.
382 if(strlen(controlsocketname) >= sizeof addr.sun_path) {
383 fprintf(stderr, _("Control socket filename too long!\n"));
387 fd = socket(PF_UNIX, SOCK_STREAM, 0);
389 fprintf(stderr, _("Cannot create UNIX socket: %s\n"), strerror(errno));
393 memset(&addr, 0, sizeof addr);
394 addr.sun_family = AF_UNIX;
395 strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
397 if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
398 fprintf(stderr, _("Cannot connect to %s: %s\n"), controlsocketname, strerror(errno));
403 socklen_t credlen = sizeof cred;
405 if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &credlen) < 0) {
406 fprintf(stderr, _("Could not obtain PID: %s\n"), strerror(errno));
410 if(!strcasecmp(argv[optind], "pid")) {
411 printf("%d\n", cred.pid);
415 if(!strcasecmp(argv[optind], "stop")) {
416 write(fd, "stop\n", 5);
420 if(!strcasecmp(argv[optind], "reload")) {
421 write(fd, "reload\n", 7);
425 if(!strcasecmp(argv[optind], "restart")) {
426 write(fd, "restart\n", 8);
430 fprintf(stderr, _("Unknown command `%s'.\n"), argv[optind]);