2 tincd.c -- the main file for tincd
3 Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000 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.16 2000/10/28 21:05:20 guus Exp $
30 #include <sys/types.h>
34 #include <openssl/rand.h>
35 #include <openssl/rsa.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
54 /* The name this program was run with. */
57 /* If nonzero, display usage information and exit. */
60 /* If nonzero, print the version on standard output and exit. */
61 static int show_version;
63 /* If nonzero, it will attempt to kill a running tincd and exit. */
64 static int kill_tincd = 0;
66 /* If zero, don't detach from the terminal. */
67 static int do_detach = 1;
69 /* If nonzero, generate public/private keypair for this host/net. */
70 static int generate_keys = 0;
72 char *identname; /* program name for syslog */
73 char *pidfilename; /* pid file location */
74 static pid_t ppid; /* pid of non-detached part */
75 char **g_argv; /* a copy of the cmdline arguments */
77 void cleanup_and_exit(int);
80 void make_names(void);
81 RETSIGTYPE parent_exit(int a);
82 void setup_signals(void);
83 int write_pidfile(void);
85 static struct option const long_options[] =
87 { "kill", no_argument, NULL, 'k' },
88 { "net", required_argument, NULL, 'n' },
89 { "help", no_argument, &show_help, 1 },
90 { "version", no_argument, &show_version, 1 },
91 { "no-detach", no_argument, &do_detach, 0 },
92 { "keygen", optional_argument, NULL, 'K'},
100 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
103 printf(_("Usage: %s [option]...\n\n"), program_name);
104 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
105 " -D, --no-detach Don't fork and detach.\n"
106 " -d Increase debug level.\n"
107 " -k, --kill Attempt to kill a running tincd and exit.\n"
108 " -n, --net=NETNAME Connect to net NETNAME.\n"));
109 printf(_(" -K, --keygen[=BITS] Generate public/private RSA keypair.\n"
110 " --help Display this help and exit.\n"
111 " --version Output version information and exit.\n\n"));
112 printf(_("Report bugs to tinc@nl.linux.org.\n"));
118 parse_options(int argc, char **argv, char **envp)
121 int option_index = 0;
124 while((r = getopt_long(argc, argv, "c:Ddkn:K::", long_options, &option_index)) != EOF)
128 case 0: /* long option */
130 case 'c': /* config file */
131 confbase = xmalloc(strlen(optarg)+1);
132 strcpy(confbase, optarg);
134 case 'D': /* no detach */
137 case 'd': /* inc debug level */
140 case 'k': /* kill old tincds */
143 case 'n': /* net name given */
144 netname = xmalloc(strlen(optarg)+1);
145 strcpy(netname, optarg);
147 case 'K': /* generate public/private keypair */
150 generate_keys = atoi(optarg);
151 if(generate_keys < 512)
153 fprintf(stderr, _("Invalid argument! BITS must be a number equal to or greater than 512.\n"));
156 generate_keys &= ~7; /* Round it to bytes */
159 generate_keys = 1024;
169 /* This function prettyprints the key generation process */
171 void indicator(int a, int b, void *p)
176 fprintf(stderr, ".");
179 fprintf(stderr, "+");
182 fprintf(stderr, "-");
188 fprintf(stderr, " p\n");
191 fprintf(stderr, " q\n");
194 fprintf(stderr, "?");
198 fprintf(stderr, "?");
202 /* Generate a public/private RSA keypair, and possibly store it into the configuration file. */
208 fprintf(stderr, _("Seeding the PRNG: please press some keys or move\nthe mouse if this program seems to have halted...\n"));
209 RAND_load_file("/dev/random", 1024); /* OpenSSL PRNG state apparently uses 1024 bytes, but it seems pretty sufficient anyway :) */
211 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
212 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
215 fprintf(stderr, _("Error during key generation!"));
219 fprintf(stderr, _("Done.\n"));
221 fprintf(stderr, _("Please copy the private key to tinc.conf and the\npublic key to your host configuration file:\n\n"));
222 printf("PublicKey = %s\n", BN_bn2hex(rsa_key->n));
223 printf("PrivateKey = %s\n", BN_bn2hex(rsa_key->d));
229 void memory_full(int size)
231 syslog(LOG_ERR, _("Memory exhausted (last is %s:%d) (couldn't allocate %d bytes), exiting."), cp_file, cp_line, size);
236 Detach from current terminal, write pidfile, kill parent
247 if((pid = fork()) < 0)
252 if(pid) /* parent process */
254 signal(SIGTERM, parent_exit);
255 // sleep(600); /* wait 10 minutes */
265 if((fd = open("/dev/tty", O_RDWR)) >= 0)
267 if(ioctl(fd, TIOCNOTTY, NULL))
281 chdir("/"); /* avoid keeping a mointpoint busy */
283 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
285 if(debug_lvl > DEBUG_NOTHING)
286 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
287 VERSION, __DATE__, __TIME__, debug_lvl);
289 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
291 xalloc_fail_func = memory_full;
297 Close network connections, and terminate neatly
299 void cleanup_and_exit(int c)
301 close_network_connections();
303 if(debug_lvl > DEBUG_NOTHING)
304 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
305 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
313 check for an existing tinc for this net, and write pid to pidfile
315 int write_pidfile(void)
319 if((pid = check_pid(pidfilename)))
322 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
325 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
329 /* if it's locked, write-protected, or whatever */
330 if(!write_pid(pidfilename))
337 kill older tincd for this net
343 if(!(pid = read_pid(pidfilename)))
346 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
348 fprintf(stderr, _("No other tincd is running.\n"));
352 errno = 0; /* No error, sometimes errno is only changed on error */
353 /* ESRCH is returned when no process with that pid is found */
354 if(kill(pid, SIGTERM) && errno == ESRCH)
355 fprintf(stderr, _("Removing stale lock file.\n"));
356 remove_pid(pidfilename);
362 Set all files and paths according to netname
364 void make_names(void)
369 asprintf(&pidfilename, "/var/run/tinc.%s.pid", netname);
371 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
373 asprintf(&identname, "tinc.%s", netname);
378 pidfilename = "/var/run/tinc.pid";
380 asprintf(&confbase, "%s/tinc", CONFDIR);
387 main(int argc, char **argv, char **envp)
389 program_name = argv[0];
391 setlocale (LC_ALL, "");
392 bindtextdomain (PACKAGE, LOCALEDIR);
393 textdomain (PACKAGE);
395 /* Do some intl stuff right now */
397 unknown = _("unknown");
399 parse_options(argc, argv, envp);
403 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
404 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans, Guus Sliepen and others.\n"
405 "See the AUTHORS file for a complete list.\n\n"
406 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
407 "and you are welcome to redistribute it under certain conditions;\n"
408 "see the file COPYING for details.\n"));
418 fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
427 exit(keygen(generate_keys));
432 if(read_server_config())
440 /* FIXME: wt* is this suppose to do?
446 if(!setup_network_connections())
452 syslog(LOG_ERR, _("Unrecoverable error"));
457 syslog(LOG_NOTICE, _("Restarting in %d seconds!"), MAXTIMEOUT);
462 syslog(LOG_ERR, _("Aieee! Not restarting."));
469 sigterm_handler(int a)
471 if(debug_lvl > DEBUG_NOTHING)
472 syslog(LOG_NOTICE, _("Got TERM signal"));
477 sigquit_handler(int a)
479 if(debug_lvl > DEBUG_NOTHING)
480 syslog(LOG_NOTICE, _("Got QUIT signal"));
485 sigsegv_square(int a)
487 syslog(LOG_ERR, _("Got another SEGV signal: not restarting"));
492 sigsegv_handler(int a)
494 syslog(LOG_ERR, _("Got SEGV signal"));
499 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
500 signal(SIGSEGV, sigsegv_square);
501 close_network_connections();
503 remove_pid(pidfilename);
504 execvp(g_argv[0], g_argv);
508 syslog(LOG_NOTICE, _("Aieee! Not restarting."));
514 sighup_handler(int a)
516 if(debug_lvl > DEBUG_NOTHING)
517 syslog(LOG_NOTICE, _("Got HUP signal, rereading configuration and restarting"));
522 sigint_handler(int a)
524 if(debug_lvl > DEBUG_NOTHING)
525 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
530 sigusr1_handler(int a)
536 sigusr2_handler(int a)
539 // if(debug_lvl > DEBUG_NOTHING)
540 // syslog(LOG_NOTICE, _("Got USR2 signal, forcing new key generation"));
541 /* FIXME: reprogram this.
549 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
561 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
562 signal(SIGTERM, sigterm_handler);
563 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
564 signal(SIGQUIT, sigquit_handler);
565 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
566 signal(SIGSEGV, sigsegv_handler);
567 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
568 signal(SIGHUP, sighup_handler);
569 signal(SIGPIPE, SIG_IGN);
570 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
571 signal(SIGINT, sigint_handler);
572 signal(SIGUSR1, sigusr1_handler);
573 signal(SIGUSR2, sigusr2_handler);
574 signal(SIGCHLD, SIG_IGN);
577 RETSIGTYPE parent_exit(int a)