2 tincd.c -- the main file for tincd
3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: tincd.c,v 1.16 2002/05/02 11:50:07 zarq Exp $
30 #include <sys/types.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
41 #include <openssl/rand.h>
42 #include <openssl/rsa.h>
43 #include <openssl/pem.h>
44 #include <openssl/evp.h>
54 #include "callbacks.h"
55 #include "read_conf.h"
65 /* The name this program was run with. */
68 /* If nonzero, display usage information and exit. */
71 /* If nonzero, print the version on standard output and exit. */
74 /* If nonzero, it will attempt to kill a running tincd and exit. */
77 /* If nonzero, generate public/private keypair for this host/net. */
78 int generate_keys = 0;
80 /* If nonzero, use null ciphers and skip all key exchanges. */
81 int bypass_security = 0;
83 char *identname; /* program name for syslog */
84 char *pidfilename; /* pid file location */
85 char **g_argv; /* a copy of the cmdline arguments */
86 char **environment; /* A pointer to the environment on
89 static struct option const long_options[] =
91 { "config", required_argument, NULL, 'c' },
92 { "kill", optional_argument, NULL, 'k' },
93 { "net", required_argument, NULL, 'n' },
94 { "help", no_argument, &show_help, 1 },
95 { "version", no_argument, &show_version, 1 },
96 { "no-detach", no_argument, &do_detach, 0 },
97 { "generate-keys", optional_argument, NULL, 'K'},
98 { "debug", optional_argument, NULL, 'd'},
99 { "bypass-security", no_argument, &bypass_security, 1 },
107 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
110 printf(_("Usage: %s [option]...\n\n"), program_name);
111 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
112 " -D, --no-detach Don't fork and detach.\n"
113 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
114 " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
115 " -n, --net=NETNAME Connect to net NETNAME.\n"));
116 printf(_(" -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
117 " --help Display this help and exit.\n"
118 " --version Output version information and exit.\n\n"));
119 printf(_("Report bugs to tinc@nl.linux.org.\n"));
125 parse_options(int argc, char **argv, char **envp)
128 int option_index = 0;
130 while((r = getopt_long(argc, argv, "c:Dd::k::n:K::", long_options, &option_index)) != EOF)
134 case 0: /* long option */
136 case 'c': /* config file */
137 confbase = xmalloc(strlen(optarg)+1);
138 strcpy(confbase, optarg);
140 case 'D': /* no detach */
143 case 'd': /* inc debug level */
145 debug_lvl = atoi(optarg);
149 case 'k': /* kill old tincds */
152 if(!strcasecmp(optarg, "HUP"))
154 else if(!strcasecmp(optarg, "TERM"))
155 kill_tincd = SIGTERM;
156 else if(!strcasecmp(optarg, "KILL"))
157 kill_tincd = SIGKILL;
158 else if(!strcasecmp(optarg, "USR1"))
159 kill_tincd = SIGUSR1;
160 else if(!strcasecmp(optarg, "USR2"))
161 kill_tincd = SIGUSR2;
162 else if(!strcasecmp(optarg, "WINCH"))
163 kill_tincd = SIGWINCH;
164 else if(!strcasecmp(optarg, "INT"))
166 else if(!strcasecmp(optarg, "ALRM"))
167 kill_tincd = SIGALRM;
170 kill_tincd = atoi(optarg);
173 fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), optarg);
179 kill_tincd = SIGTERM;
181 case 'n': /* net name given */
182 netname = xmalloc(strlen(optarg)+1);
183 strcpy(netname, optarg);
185 case 'K': /* generate public/private keypair */
188 generate_keys = atoi(optarg);
189 if(generate_keys < 512)
191 fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
195 generate_keys &= ~7; /* Round it to bytes */
198 generate_keys = 1024;
208 /* This function prettyprints the key generation process */
210 void indicator(int a, int b, void *p)
215 fprintf(stderr, ".");
218 fprintf(stderr, "+");
221 fprintf(stderr, "-");
227 fprintf(stderr, " p\n");
230 fprintf(stderr, " q\n");
233 fprintf(stderr, "?");
237 fprintf(stderr, "?");
243 Generate a public/private RSA keypair, and ask for a file to store
253 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
254 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
258 fprintf(stderr, _("Error during key generation!\n"));
262 fprintf(stderr, _("Done.\n"));
264 get_config_string(lookup_config(config_tree, "Name"), &name);
267 asprintf(&filename, "%s/hosts/%s", confbase, name);
269 asprintf(&filename, "%s/rsa_key.pub", confbase);
271 if((f = ask_and_safe_open(filename, _("public RSA key"), "a")) == NULL)
275 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
277 PEM_write_RSAPublicKey(f, rsa_key);
281 asprintf(&filename, "%s/rsa_key.priv", confbase);
282 if((f = ask_and_safe_open(filename, _("private RSA key"), "a")) == NULL)
286 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
288 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
297 Set all files and paths according to netname
299 void make_names(void)
304 asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname);
306 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
308 log(0, TLOG_INFO, _("Both netname and configuration directory given, using the latter..."));
310 asprintf(&identname, "tinc.%s", netname);
315 pidfilename = LOCALSTATEDIR "/run/tinc.pid";
317 asprintf(&confbase, "%s/tinc", CONFDIR);
324 main(int argc, char **argv, char **envp)
326 program_name = argv[0];
328 setlocale (LC_ALL, "");
329 bindtextdomain (PACKAGE, LOCALEDIR);
330 textdomain (PACKAGE);
333 parse_options(argc, argv, envp);
337 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
338 printf(_("Copyright (C) 1998-2002 Ivo Timmermans, Guus Sliepen and others.\n"
339 "See the AUTHORS file for a complete list.\n\n"
340 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
341 "and you are welcome to redistribute it under certain conditions;\n"
342 "see the file COPYING for details.\n"));
350 log_add_hook(log_default);
355 init_configuration(&config_tree);
357 /* Slllluuuuuuurrrrp! */
360 RAND_load_file("/dev/urandom", 1024);
362 #ifdef HAVE_SSLEAY_ADD_ALL_ALGORITHMS
363 SSLeay_add_all_algorithms();
365 OpenSSL_add_all_algorithms();
371 read_server_config();
372 exit(keygen(generate_keys));
377 exit(kill_other(kill_tincd));
379 if(read_server_config())
389 if(!setup_network_connections())
395 log(0, TLOG_ERROR, _("Unrecoverable error"));
400 log(0, TLOG_NOTICE, _("Restarting in %d seconds!"), maxtimeout);
405 log(0, TLOG_ERROR, _("Not restarting."));