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 " generate-keys [bits] Generate a new public/private keypair.\n"
84 " dump Dump a list of one of the following things:\n"
85 " nodes - all known nodes in the VPN\n"
86 " edges - all known connections in the VPN\n"
87 " subnets - all known subnets in the VPN\n"
88 " connections - all meta connections with ourself\n"
89 " graph - graph of the VPN in dotty format\n"
91 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
95 static bool parse_options(int argc, char **argv) {
99 while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) {
101 case 0: /* long option */
104 case 'c': /* config file */
105 confbase = xstrdup(optarg);
108 case 'n': /* net name given */
109 netname = xstrdup(optarg);
112 case 1: /* show help */
116 case 2: /* show version */
120 case 5: /* open control socket here */
121 controlsocketname = xstrdup(optarg);
136 FILE *ask_and_open(const char *filename, const char *what, const char *mode) {
143 /* Check stdin and stdout */
144 if(isatty(0) && isatty(1)) {
145 /* Ask for a file and/or directory name. */
146 fprintf(stdout, _("Please enter a file to save %s to [%s]: "),
150 if(fgets(buf, sizeof buf, stdin) < 0) {
151 fprintf(stderr, _("Error while reading stdin: %s\n"),
165 if(filename[0] != '\\' && filename[0] != '/' && !strchr(filename, ':')) {
167 if(filename[0] != '/') {
169 /* The directory is a relative path or a filename. */
170 directory = get_current_dir_name();
171 snprintf(buf2, sizeof buf2, "%s/%s", directory, filename);
175 umask(0077); /* Disallow everything for group and other */
177 /* Open it first to keep the inode busy */
179 r = fopen(filename, mode);
182 fprintf(stderr, _("Error opening file `%s': %s\n"), filename, strerror(errno));
189 /* This function prettyprints the key generation process */
191 static void indicator(int a, int b, void *p) {
194 fprintf(stderr, ".");
198 fprintf(stderr, "+");
202 fprintf(stderr, "-");
208 fprintf(stderr, " p\n");
212 fprintf(stderr, " q\n");
216 fprintf(stderr, "?");
221 fprintf(stderr, "?");
226 Generate a public/private RSA keypair, and ask for a file to store
229 static bool keygen(int bits) {
235 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
236 rsa_key = RSA_generate_key(bits, 0x10001, indicator, NULL);
239 fprintf(stderr, _("Error during key generation!\n"));
242 fprintf(stderr, _("Done.\n"));
244 asprintf(&filename, "%s/rsa_key.priv", confbase);
245 f = ask_and_open(filename, _("private RSA key"), "a");
251 /* Make it unreadable for others. */
252 fchmod(fileno(f), 0600);
256 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
258 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
263 asprintf(&filename, "%s/hosts/%s", confbase, name);
265 asprintf(&filename, "%s/rsa_key.pub", confbase);
267 f = ask_and_open(filename, _("public RSA key"), "a");
273 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
275 PEM_write_RSAPublicKey(f, rsa_key);
283 Set all files and paths according to netname
285 static void make_names(void) {
288 char installdir[1024] = "";
289 long len = sizeof(installdir);
293 asprintf(&identname, "tinc.%s", netname);
295 identname = xstrdup("tinc");
298 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
299 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
301 asprintf(&logfilename, "%s/log/%s.log", identname);
304 asprintf(&confbase, "%s/%s", installdir, netname);
306 asprintf(&confbase, "%s", installdir);
315 if(!controlsocketname)
316 asprintf(&controlsocketname, LOCALSTATEDIR "/run/%s.control", identname);
320 asprintf(&confbase, CONFDIR "/tinc/%s", netname);
322 fprintf(stderr, _("Both netname and configuration directory given, using the latter...\n"));
325 asprintf(&confbase, CONFDIR "/tinc");
329 int main(int argc, char **argv) {
331 struct sockaddr_un addr;
332 program_name = argv[0];
334 setlocale(LC_ALL, "");
335 bindtextdomain(PACKAGE, LOCALEDIR);
338 if(!parse_options(argc, argv))
344 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
345 VERSION, __DATE__, __TIME__, PROT_CURRENT);
346 printf(_("Copyright (C) 1998-2007 Ivo Timmermans, Guus Sliepen and others.\n"
347 "See the AUTHORS file for a complete list.\n\n"
348 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
349 "and you are welcome to redistribute it under certain conditions;\n"
350 "see the file COPYING for details.\n"));
361 fprintf(stderr, _("Not enough arguments.\n"));
366 if(!strcasecmp(argv[optind], "generate-keys")) {
367 return !keygen(optind > argc ? atoi(argv[optind + 1]) : 1024);
370 if(strlen(controlsocketname) >= sizeof addr.sun_path) {
371 fprintf(stderr, _("Control socket filename too long!\n"));
375 fd = socket(PF_UNIX, SOCK_STREAM, 0);
377 fprintf(stderr, _("Cannot create UNIX socket: %s\n"), strerror(errno));
381 memset(&addr, 0, sizeof addr);
382 addr.sun_family = AF_UNIX;
383 strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
385 if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
386 fprintf(stderr, _("Cannot connect to %s: %s\n"), controlsocketname, strerror(errno));
390 printf("Connected to %s.\n", controlsocketname);