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.29 2000/11/16 17:54:29 zarq 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>
73 /* The name this program was run with. */
76 /* If nonzero, display usage information and exit. */
79 /* If nonzero, print the version on standard output and exit. */
80 static int show_version;
82 /* If nonzero, it will attempt to kill a running tincd and exit. */
83 static int kill_tincd = 0;
85 /* If zero, don't detach from the terminal. */
88 /* If nonzero, generate public/private keypair for this host/net. */
89 static int generate_keys = 0;
91 char *identname; /* program name for syslog */
92 char *pidfilename; /* pid file location */
93 static pid_t ppid; /* pid of non-detached part */
94 char **g_argv; /* a copy of the cmdline arguments */
95 char **environment; /* A pointer to the environment on
98 void cleanup_and_exit(int);
100 int kill_other(void);
101 void make_names(void);
102 RETSIGTYPE parent_exit(int a);
103 void setup_signals(void);
104 int write_pidfile(void);
106 static struct option const long_options[] =
108 { "config", required_argument, NULL, 'c' },
109 { "kill", no_argument, NULL, 'k' },
110 { "net", required_argument, NULL, 'n' },
111 { "help", no_argument, &show_help, 1 },
112 { "version", no_argument, &show_version, 1 },
113 { "no-detach", no_argument, &do_detach, 0 },
114 { "generate-keys", optional_argument, NULL, 'K'},
122 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
125 printf(_("Usage: %s [option]...\n\n"), program_name);
126 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
127 " -D, --no-detach Don't fork and detach.\n"
128 " -d Increase debug level.\n"
129 " -k, --kill Attempt to kill a running tincd and exit.\n"
130 " -n, --net=NETNAME Connect to net NETNAME.\n"));
131 printf(_(" -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
132 " --help Display this help and exit.\n"
133 " --version Output version information and exit.\n\n"));
134 printf(_("Report bugs to tinc@nl.linux.org.\n"));
140 parse_options(int argc, char **argv, char **envp)
143 int option_index = 0;
145 while((r = getopt_long(argc, argv, "c:Ddkn:K::", long_options, &option_index)) != EOF)
149 case 0: /* long option */
151 case 'c': /* config file */
152 confbase = xmalloc(strlen(optarg)+1);
153 strcpy(confbase, optarg);
155 case 'D': /* no detach */
158 case 'd': /* inc debug level */
161 case 'k': /* kill old tincds */
164 case 'n': /* net name given */
165 netname = xmalloc(strlen(optarg)+1);
166 strcpy(netname, optarg);
168 case 'K': /* generate public/private keypair */
171 generate_keys = atoi(optarg);
172 if(generate_keys < 512)
174 fprintf(stderr, _("Invalid argument! BITS must be a number equal to or greater than 512.\n"));
177 generate_keys &= ~7; /* Round it to bytes */
180 generate_keys = 1024;
190 /* This function prettyprints the key generation process */
192 void indicator(int a, int b, void *p)
197 fprintf(stderr, ".");
200 fprintf(stderr, "+");
203 fprintf(stderr, "-");
209 fprintf(stderr, " p\n");
212 fprintf(stderr, " q\n");
215 fprintf(stderr, "?");
219 fprintf(stderr, "?");
223 /* Generate a public/private RSA keypair, and possibly store it into the configuration file. */
229 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
230 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
233 fprintf(stderr, _("Error during key generation!"));
237 fprintf(stderr, _("Done.\n"));
239 fprintf(stderr, _("Please copy the private key to tinc.conf and the\npublic key to your host configuration file:\n\n"));
240 printf("PublicKey = %s\n", BN_bn2hex(rsa_key->n));
241 printf("PrivateKey = %s\n", BN_bn2hex(rsa_key->d));
247 void memory_full(int size)
249 syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exiting."), size);
255 Close network connections, and terminate neatly
257 void cleanup_and_exit(int c)
259 close_network_connections();
261 if(debug_lvl > DEBUG_NOTHING)
262 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
263 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
271 check for an existing tinc for this net, and write pid to pidfile
273 int write_pidfile(void)
277 if((pid = check_pid(pidfilename)))
280 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
283 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
287 /* if it's locked, write-protected, or whatever */
288 if(!write_pid(pidfilename))
295 kill older tincd for this net
301 if(!(pid = read_pid(pidfilename)))
304 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
306 fprintf(stderr, _("No other tincd is running.\n"));
310 errno = 0; /* No error, sometimes errno is only changed on error */
311 /* ESRCH is returned when no process with that pid is found */
312 if(kill(pid, SIGTERM) && errno == ESRCH)
313 fprintf(stderr, _("Removing stale lock file.\n"));
314 remove_pid(pidfilename);
320 Set all files and paths according to netname
322 void make_names(void)
327 asprintf(&pidfilename, "/var/run/tinc.%s.pid", netname);
329 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
331 fprintf(stderr, _("Both netname and configuration directory given, using the latter...\n"));
333 asprintf(&identname, "tinc.%s", netname);
338 pidfilename = "/var/run/tinc.pid";
340 asprintf(&confbase, "%s/tinc", CONFDIR);
347 main(int argc, char **argv, char **envp)
349 program_name = argv[0];
351 setlocale (LC_ALL, "");
352 bindtextdomain (PACKAGE, LOCALEDIR);
353 textdomain (PACKAGE);
355 /* Do some intl stuff right now */
357 unknown = _("unknown");
360 parse_options(argc, argv, envp);
364 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
365 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans, Guus Sliepen and others.\n"
366 "See the AUTHORS file for a complete list.\n\n"
367 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
368 "and you are welcome to redistribute it under certain conditions;\n"
369 "see the file COPYING for details.\n"));
379 fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
387 /* Slllluuuuuuurrrrp! */
389 RAND_load_file("/dev/urandom", 1024);
392 exit(keygen(generate_keys));
397 if(read_server_config())
405 if(debug_lvl >= DEBUG_ERROR)
406 ERR_load_crypto_strings();
410 if(!setup_network_connections())
416 syslog(LOG_ERR, _("Unrecoverable error"));
421 syslog(LOG_NOTICE, _("Restarting in %d seconds!"), MAXTIMEOUT);
426 syslog(LOG_ERR, _("Not restarting."));