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>
37 /* The name this program was run with. */
38 char *program_name = NULL;
40 /* If nonzero, display usage information and exit. */
41 bool show_help = false;
43 /* If nonzero, print the version on standard output and exit. */
44 bool show_version = false;
46 /* If nonzero, it will attempt to kill a running tincd and exit. */
49 /* If nonzero, generate public/private keypair for this host/net. */
50 int generate_keys = 0;
52 char *identname = NULL; /* program name for syslog */
53 char *pidfilename = NULL; /* pid file location */
54 char *controlfilename = NULL; /* pid file location */
55 char *confbase = NULL;
60 static struct option const long_options[] = {
61 {"config", required_argument, NULL, 'c'},
62 {"net", required_argument, NULL, 'n'},
63 {"help", no_argument, NULL, 1},
64 {"version", no_argument, NULL, 2},
65 {"pidfile", required_argument, NULL, 5},
69 static void usage(bool status) {
71 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
74 printf(_("Usage: %s [options] command\n\n"), program_name);
75 printf(_("Valid options are:\n"
76 " -c, --config=DIR Read configuration options from DIR.\n"
77 " -n, --net=NETNAME Connect to net NETNAME.\n"
78 " --pidfile=FILENAME Write PID to FILENAME.\n"
79 " --help Display this help and exit.\n"
80 " --version Output version information and exit.\n"
81 "Valid commands are:\n"
82 " start Start tincd.\n"
84 " restart Restart tincd.\n"
85 " reload Reload configuration of running tincd.\n"
86 " genkey [bits] Generate a new public/private keypair.\n"
87 " dump Dump a list of one of the following things:\n"
88 " nodes - all known nodes in the VPN\n"
89 " edges - all known connections in the VPN\n"
90 " subnets - all known subnets in the VPN\n"
91 " connections - all meta connections with ourself\n"
92 " graph - graph of the VPN in dotty format\n"
94 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
98 static bool parse_options(int argc, char **argv) {
100 int option_index = 0;
102 while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) {
104 case 0: /* long option */
107 case 'c': /* config file */
108 confbase = xstrdup(optarg);
111 case 'n': /* net name given */
112 netname = xstrdup(optarg);
115 case 1: /* show help */
119 case 2: /* show version */
123 case 5: /* write PID to a file */
124 pidfilename = xstrdup(optarg);
139 /* This function prettyprints the key generation process */
141 static void indicator(int a, int b, void *p) {
144 fprintf(stderr, ".");
148 fprintf(stderr, "+");
152 fprintf(stderr, "-");
158 fprintf(stderr, " p\n");
162 fprintf(stderr, " q\n");
166 fprintf(stderr, "?");
171 fprintf(stderr, "?");
176 Generate a public/private RSA keypair, and ask for a file to store
179 static bool keygen(int bits) {
185 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
186 rsa_key = RSA_generate_key(bits, 0x10001, indicator, NULL);
189 fprintf(stderr, _("Error during key generation!\n"));
192 fprintf(stderr, _("Done.\n"));
194 asprintf(&filename, "%s/rsa_key.priv", confbase);
195 f = ask_and_open(filename, _("private RSA key"), "a");
201 /* Make it unreadable for others. */
202 fchmod(fileno(f), 0600);
206 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
208 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
213 asprintf(&filename, "%s/hosts/%s", confbase, name);
215 asprintf(&filename, "%s/rsa_key.pub", confbase);
217 f = ask_and_open(filename, _("public RSA key"), "a");
223 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
225 PEM_write_RSAPublicKey(f, rsa_key);
233 Set all files and paths according to netname
235 static void make_names(void) {
238 char installdir[1024] = "";
239 long len = sizeof(installdir);
243 asprintf(&identname, "tinc.%s", netname);
245 identname = xstrdup("tinc");
248 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
249 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
251 asprintf(&logfilename, "%s/log/%s.log", identname);
254 asprintf(&confbase, "%s/%s", installdir, netname);
256 asprintf(&confbase, "%s", installdir);
266 asprintf(&pidfilename, LOCALSTATEDIR "/run/%s.pid", identname);
268 asprintf(&controlfilename, LOCALSTATEDIR "/run/%s.control", identname);
272 asprintf(&confbase, CONFDIR "/tinc/%s", netname);
274 fprintf(stderr, _("Both netname and configuration directory given, using the latter...\n"));
277 asprintf(&confbase, CONFDIR "/tinc");
281 int main(int argc, char **argv) {
283 struct sockaddr_un addr;
284 program_name = argv[0];
286 setlocale(LC_ALL, "");
287 bindtextdomain(PACKAGE, LOCALEDIR);
290 if(!parse_options(argc, argv))
296 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
297 VERSION, __DATE__, __TIME__, PROT_CURRENT);
298 printf(_("Copyright (C) 1998-2007 Ivo Timmermans, Guus Sliepen and others.\n"
299 "See the AUTHORS file for a complete list.\n\n"
300 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
301 "and you are welcome to redistribute it under certain conditions;\n"
302 "see the file COPYING for details.\n"));
312 if(strlen(controlfilename) >= sizeof addr.sun_path) {
313 fprintf(stderr, _("Control socket filename too long!\n"));
317 fd = socket(PF_UNIX, SOCK_STREAM, 0);
319 fprintf(stderr, _("Cannot create UNIX socket: %s\n"), strerror(errno));
323 memset(&addr, 0, sizeof addr);
324 addr.sun_family = AF_UNIX;
325 strncpy(addr.sun_path, controlfilename, sizeof addr.sun_path - 1);
327 if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
328 fprintf(stderr, _("Cannot connect to %s: %s\n"), controlfilename, strerror(errno));
332 printf("Connected to %s.\n", controlfilename);