2 tincd.c -- the main file for tincd
3 Copyright (C) 1998-2001 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000,2001 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.10.4.45 2001/03/01 21:32:04 guus Exp $
30 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
41 #ifdef HAVE_OPENSSL_RAND_H
42 # include <openssl/rand.h>
47 #ifdef HAVE_OPENSSL_RSA_H
48 # include <openssl/rsa.h>
53 #ifdef HAVE_OPENSSL_ERR_H
54 # include <openssl/err.h>
59 #ifdef HAVE_OPENSSL_PEM_H
60 # include <openssl/pem.h>
79 /* The name this program was run with. */
82 /* If nonzero, display usage information and exit. */
85 /* If nonzero, print the version on standard output and exit. */
86 static int show_version;
88 /* If nonzero, it will attempt to kill a running tincd and exit. */
89 static int kill_tincd = 0;
91 /* If nonzero, generate public/private keypair for this host/net. */
92 static int generate_keys = 0;
94 char *identname; /* program name for syslog */
95 char *pidfilename; /* pid file location */
96 char **g_argv; /* a copy of the cmdline arguments */
97 char **environment; /* A pointer to the environment on
100 static struct option const long_options[] =
102 { "config", required_argument, NULL, 'c' },
103 { "kill", no_argument, NULL, 'k' },
104 { "net", required_argument, NULL, 'n' },
105 { "help", no_argument, &show_help, 1 },
106 { "version", no_argument, &show_version, 1 },
107 { "no-detach", no_argument, &do_detach, 0 },
108 { "generate-keys", optional_argument, NULL, 'K'},
116 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
119 printf(_("Usage: %s [option]...\n\n"), program_name);
120 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
121 " -D, --no-detach Don't fork and detach.\n"
122 " -d Increase debug level.\n"
123 " -k, --kill Attempt to kill a running tincd and exit.\n"
124 " -n, --net=NETNAME Connect to net NETNAME.\n"));
125 printf(_(" -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
126 " --help Display this help and exit.\n"
127 " --version Output version information and exit.\n\n"));
128 printf(_("Report bugs to tinc@nl.linux.org.\n"));
134 parse_options(int argc, char **argv, char **envp)
137 int option_index = 0;
139 while((r = getopt_long(argc, argv, "c:Ddkn:K::", long_options, &option_index)) != EOF)
143 case 0: /* long option */
145 case 'c': /* config file */
146 confbase = xmalloc(strlen(optarg)+1);
147 strcpy(confbase, optarg);
149 case 'D': /* no detach */
152 case 'd': /* inc debug level */
154 debug_lvl = atoi(optarg);
158 case 'k': /* kill old tincds */
161 case 'n': /* net name given */
162 netname = xmalloc(strlen(optarg)+1);
163 strcpy(netname, optarg);
165 case 'K': /* generate public/private keypair */
168 generate_keys = atoi(optarg);
169 if(generate_keys < 512)
171 fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
175 generate_keys &= ~7; /* Round it to bytes */
178 generate_keys = 1024;
188 /* This function prettyprints the key generation process */
190 void indicator(int a, int b, void *p)
195 fprintf(stderr, ".");
198 fprintf(stderr, "+");
201 fprintf(stderr, "-");
207 fprintf(stderr, " p\n");
210 fprintf(stderr, " q\n");
213 fprintf(stderr, "?");
217 fprintf(stderr, "?");
222 Generate a public/private RSA keypair, and ask for a file to store
232 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
233 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
237 fprintf(stderr, _("Error during key generation!"));
241 fprintf(stderr, _("Done.\n"));
243 if(config && (cfg = get_config_val(config, config_name)))
244 asprintf(&filename, "%s/hosts/%s", confbase, cfg->data.ptr);
246 asprintf(&filename, "%s/rsa_key.priv", confbase);
248 if((f = ask_and_safe_open(filename, _("public RSA key"), "a")) == NULL)
252 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
254 PEM_write_RSAPublicKey(f, rsa_key);
258 asprintf(&filename, "%s/rsa_key.priv", confbase);
259 if((f = ask_and_safe_open(filename, _("private RSA key"), "a")) == NULL)
263 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
265 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
273 Set all files and paths according to netname
275 void make_names(void)
280 asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname);
282 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
284 syslog(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
286 asprintf(&identname, "tinc.%s", netname);
291 pidfilename = LOCALSTATEDIR "/run/tinc.pid";
293 asprintf(&confbase, "%s/tinc", CONFDIR);
300 main(int argc, char **argv, char **envp)
302 program_name = argv[0];
304 setlocale (LC_ALL, "");
305 bindtextdomain (PACKAGE, LOCALEDIR);
306 textdomain (PACKAGE);
308 /* Do some intl stuff right now */
310 unknown = _("unknown");
313 parse_options(argc, argv, envp);
317 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
318 printf(_("Copyright (C) 1998-2001 Ivo Timmermans, Guus Sliepen and others.\n"
319 "See the AUTHORS file for a complete list.\n\n"
320 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
321 "and you are welcome to redistribute it under certain conditions;\n"
322 "see the file COPYING for details.\n"));
332 fprintf(stderr, _("You must be root to run this program.\n"));
336 openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
342 /* Slllluuuuuuurrrrp! */
344 RAND_load_file("/dev/urandom", 1024);
348 read_server_config();
349 exit(keygen(generate_keys));
355 if(read_server_config())
361 if(debug_lvl >= DEBUG_ERROR)
362 ERR_load_crypto_strings();
366 if(!setup_network_connections())
372 syslog(LOG_ERR, _("Unrecoverable error"));
377 syslog(LOG_NOTICE, _("Restarting in %d seconds!"), MAXTIMEOUT);
382 syslog(LOG_ERR, _("Not restarting."));