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.14 2000/10/20 19:46:58 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>
53 /* The name this program was run with. */
56 /* If nonzero, display usage information and exit. */
59 /* If nonzero, print the version on standard output and exit. */
60 static int show_version;
62 /* If nonzero, it will attempt to kill a running tincd and exit. */
63 static int kill_tincd = 0;
65 /* If zero, don't detach from the terminal. */
66 static int do_detach = 1;
68 /* If nonzero, generate public/private keypair for this host/net. */
69 static int generate_keys = 0;
71 char *identname; /* program name for syslog */
72 char *pidfilename; /* pid file location */
73 static pid_t ppid; /* pid of non-detached part */
74 char **g_argv; /* a copy of the cmdline arguments */
76 void cleanup_and_exit(int);
79 void make_names(void);
80 RETSIGTYPE parent_exit(int a);
81 void setup_signals(void);
82 int write_pidfile(void);
84 static struct option const long_options[] =
86 { "kill", no_argument, NULL, 'k' },
87 { "net", required_argument, NULL, 'n' },
88 { "help", no_argument, &show_help, 1 },
89 { "version", no_argument, &show_version, 1 },
90 { "no-detach", no_argument, &do_detach, 0 },
91 { "keygen", optional_argument, NULL, 'K'},
99 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
102 printf(_("Usage: %s [option]...\n\n"), program_name);
103 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
104 " -D, --no-detach Don't fork and detach.\n"
105 " -d Increase debug level.\n"
106 " -k, --kill Attempt to kill a running tincd and exit.\n"
107 " -n, --net=NETNAME Connect to net NETNAME.\n"));
108 printf(_(" -K, --keygen[=BITS] Generate public/private RSA keypair.\n"
109 " --help Display this help and exit.\n"
110 " --version Output version information and exit.\n\n"));
111 printf(_("Report bugs to tinc@nl.linux.org.\n"));
117 parse_options(int argc, char **argv, char **envp)
120 int option_index = 0;
123 while((r = getopt_long(argc, argv, "c:Ddkn:K::", long_options, &option_index)) != EOF)
127 case 0: /* long option */
129 case 'c': /* config file */
130 confbase = xmalloc(strlen(optarg)+1);
131 strcpy(confbase, optarg);
133 case 'D': /* no detach */
136 case 'd': /* inc debug level */
139 case 'k': /* kill old tincds */
142 case 'n': /* net name given */
143 netname = xmalloc(strlen(optarg)+1);
144 strcpy(netname, optarg);
146 case 'K': /* generate public/private keypair */
149 generate_keys = atoi(optarg);
150 if(generate_keys < 512)
152 fprintf(stderr, _("Invalid argument! BITS must be a number equal to or greater than 512.\n"));
155 generate_keys &= ~7; /* Round it to bytes */
158 generate_keys = 1024;
168 /* This function prettyprints the key generation process */
170 void indicator(int a, int b, void *p)
175 fprintf(stderr, ".");
178 fprintf(stderr, "+");
181 fprintf(stderr, "-");
187 fprintf(stderr, " p\n");
190 fprintf(stderr, " q\n");
193 fprintf(stderr, "?");
197 fprintf(stderr, "?");
201 /* Generate a public/private RSA keypair, and possibly store it into the configuration file. */
207 fprintf(stderr, _("Seeding the PRNG: please press some keys or move\nthe mouse if this program seems to have halted...\n"));
208 RAND_load_file("/dev/random", 1024); /* OpenSSL PRNG state apparently uses 1024 bytes, but it seems pretty sufficient anyway :) */
210 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
211 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
214 fprintf(stderr, _("Error during key generation!"));
218 fprintf(stderr, _("Done.\n"));
220 fprintf(stderr, _("Please copy the private key to tinc.conf and the\npublic key to your host configuration file:\n\n"));
221 printf("PublicKey = %s\n", BN_bn2hex(rsa_key->n));
222 printf("PrivateKey = %s\n", BN_bn2hex(rsa_key->d));
228 void memory_full(int size)
230 syslog(LOG_ERR, _("Memory exhausted (last is %s:%d) (couldn't allocate %d bytes), exiting."), cp_file, cp_line, size);
235 Detach from current terminal, write pidfile, kill parent
246 if((pid = fork()) < 0)
251 if(pid) /* parent process */
253 signal(SIGTERM, parent_exit);
254 // sleep(600); /* wait 10 minutes */
264 if((fd = open("/dev/tty", O_RDWR)) >= 0)
266 if(ioctl(fd, TIOCNOTTY, NULL))
280 chdir("/"); /* avoid keeping a mointpoint busy */
282 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
285 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
286 VERSION, __DATE__, __TIME__, debug_lvl);
288 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION, debug_lvl);
290 xalloc_fail_func = memory_full;
296 Close network connections, and terminate neatly
298 void cleanup_and_exit(int c)
300 close_network_connections();
303 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
304 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
312 check for an existing tinc for this net, and write pid to pidfile
314 int write_pidfile(void)
318 if((pid = check_pid(pidfilename)))
321 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
324 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
328 /* if it's locked, write-protected, or whatever */
329 if(!write_pid(pidfilename))
336 kill older tincd for this net
342 if(!(pid = read_pid(pidfilename)))
345 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
347 fprintf(stderr, _("No other tincd is running.\n"));
351 errno = 0; /* No error, sometimes errno is only changed on error */
352 /* ESRCH is returned when no process with that pid is found */
353 if(kill(pid, SIGTERM) && errno == ESRCH)
354 fprintf(stderr, _("Removing stale lock file.\n"));
355 remove_pid(pidfilename);
361 Set all files and paths according to netname
363 void make_names(void)
368 asprintf(&pidfilename, "/var/run/tinc.%s.pid", netname);
370 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
372 asprintf(&identname, "tinc.%s", netname);
377 pidfilename = "/var/run/tinc.pid";
379 asprintf(&confbase, "%s/tinc", CONFDIR);
386 main(int argc, char **argv, char **envp)
388 program_name = argv[0];
390 setlocale (LC_ALL, "");
391 bindtextdomain (PACKAGE, LOCALEDIR);
392 textdomain (PACKAGE);
394 /* Do some intl stuff right now */
396 unknown = _("unknown");
398 parse_options(argc, argv, envp);
402 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
403 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans, Guus Sliepen and others.\n"
404 "See the AUTHORS file for a complete list.\n\n"
405 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
406 "and you are welcome to redistribute it under certain conditions;\n"
407 "see the file COPYING for details.\n"));
417 fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
426 exit(keygen(generate_keys));
431 if(read_server_config())
439 /* FIXME: wt* is this suppose to do?
445 if(!setup_network_connections())
451 syslog(LOG_ERR, _("Unrecoverable error"));
456 syslog(LOG_NOTICE, _("Restarting in %d seconds!"), MAXTIMEOUT);
461 syslog(LOG_ERR, _("Aieee! Not restarting."));
468 sigterm_handler(int a)
471 syslog(LOG_NOTICE, _("Got TERM signal"));
476 sigquit_handler(int a)
479 syslog(LOG_NOTICE, _("Got QUIT signal"));
484 sigsegv_square(int a)
486 syslog(LOG_ERR, _("Got another SEGV signal: not restarting"));
491 sigsegv_handler(int a)
493 syslog(LOG_ERR, _("Got SEGV signal"));
498 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
499 signal(SIGSEGV, sigsegv_square);
500 close_network_connections();
502 remove_pid(pidfilename);
503 execvp(g_argv[0], g_argv);
507 syslog(LOG_NOTICE, _("Aieee! Not restarting."));
513 sighup_handler(int a)
516 syslog(LOG_NOTICE, _("Got HUP signal, rereading configuration and restarting"));
521 sigint_handler(int a)
524 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
529 sigusr1_handler(int a)
535 sigusr2_handler(int a)
538 syslog(LOG_NOTICE, _("Got USR2 signal, forcing new key generation"));
539 /* FIXME: reprogram this.
547 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
559 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
560 signal(SIGTERM, sigterm_handler);
561 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
562 signal(SIGQUIT, sigquit_handler);
563 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
564 signal(SIGSEGV, sigsegv_handler);
565 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
566 signal(SIGHUP, sighup_handler);
567 signal(SIGPIPE, SIG_IGN);
568 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
569 signal(SIGINT, sigint_handler);
570 signal(SIGUSR1, sigusr1_handler);
571 signal(SIGUSR2, sigusr2_handler);
572 signal(SIGCHLD, SIG_IGN);
575 RETSIGTYPE parent_exit(int a)