2 tincd.c -- the main file for tincd
3 Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>
4 2000-2003 Guus Sliepen <guus@sliepen.eu.org>
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.88 2003/09/25 10:34:16 guus Exp $
25 /* Darwin (MacOS/X) needs the following definition... */
26 #ifndef _P1003_1B_VISIBLE
27 #define _P1003_1B_VISIBLE
30 #ifdef HAVE_SYS_MMAN_H
34 #include <openssl/rand.h>
35 #include <openssl/rsa.h>
36 #include <openssl/pem.h>
37 #include <openssl/evp.h>
53 /* The name this program was run with. */
54 char *program_name = NULL;
56 /* If nonzero, display usage information and exit. */
57 bool show_help = false;
59 /* If nonzero, print the version on standard output and exit. */
60 bool show_version = false;
62 /* If nonzero, it will attempt to kill a running tincd and exit. */
65 /* If nonzero, generate public/private keypair for this host/net. */
66 int generate_keys = 0;
68 /* If nonzero, use null ciphers and skip all key exchanges. */
69 bool bypass_security = false;
71 /* If nonzero, disable swapping for this process. */
72 bool do_mlock = false;
74 /* If nonzero, write log entries to a separate file. */
75 bool use_logfile = false;
77 char *identname = NULL; /* program name for syslog */
78 char *pidfilename = NULL; /* pid file location */
79 char *logfilename = NULL; /* log file location */
80 char **g_argv; /* a copy of the cmdline arguments */
84 static struct option const long_options[] = {
85 {"config", required_argument, NULL, 'c'},
86 {"kill", optional_argument, NULL, 'k'},
87 {"net", required_argument, NULL, 'n'},
88 {"help", no_argument, NULL, 1},
89 {"version", no_argument, NULL, 2},
90 {"no-detach", no_argument, NULL, 'D'},
91 {"generate-keys", optional_argument, NULL, 'K'},
92 {"debug", optional_argument, NULL, 'd'},
93 {"bypass-security", no_argument, NULL, 3},
94 {"mlock", no_argument, NULL, 'L'},
95 {"logfile", optional_argument, NULL, 4},
96 {"pidfile", required_argument, NULL, 5},
101 static struct WSAData wsa_state;
104 static void usage(bool status)
107 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
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 " -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
117 " -L, --mlock Lock tinc into main memory.\n"
118 " --logfile[=FILENAME] Write log entries to a logfile.\n"
119 " --pidfile=FILENAME Write PID to FILENAME.\n"
120 " --help Display this help and exit.\n"
121 " --version Output version information and exit.\n\n"));
122 printf(_("Report bugs to tinc@nl.linux.org.\n"));
126 static bool parse_options(int argc, char **argv)
129 int option_index = 0;
131 while((r = getopt_long(argc, argv, "c:DLd::k::n:K::", long_options, &option_index)) != EOF) {
133 case 0: /* long option */
136 case 'c': /* config file */
137 confbase = xstrdup(optarg);
140 case 'D': /* no detach */
144 case 'L': /* no detach */
148 case 'd': /* inc debug level */
150 debug_level = atoi(optarg);
155 case 'k': /* kill old tincds */
158 if(!strcasecmp(optarg, "HUP"))
160 else if(!strcasecmp(optarg, "TERM"))
161 kill_tincd = SIGTERM;
162 else if(!strcasecmp(optarg, "KILL"))
163 kill_tincd = SIGKILL;
164 else if(!strcasecmp(optarg, "USR1"))
165 kill_tincd = SIGUSR1;
166 else if(!strcasecmp(optarg, "USR2"))
167 kill_tincd = SIGUSR2;
168 else if(!strcasecmp(optarg, "WINCH"))
169 kill_tincd = SIGWINCH;
170 else if(!strcasecmp(optarg, "INT"))
172 else if(!strcasecmp(optarg, "ALRM"))
173 kill_tincd = SIGALRM;
175 kill_tincd = atoi(optarg);
178 fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
185 kill_tincd = SIGTERM;
191 case 'n': /* net name given */
192 netname = xstrdup(optarg);
195 case 'K': /* generate public/private keypair */
197 generate_keys = atoi(optarg);
199 if(generate_keys < 512) {
200 fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
206 generate_keys &= ~7; /* Round it to bytes */
208 generate_keys = 1024;
211 case 1: /* show help */
215 case 2: /* show version */
219 case 3: /* bypass security */
220 bypass_security = true;
223 case 4: /* write log entries to a file */
226 logfilename = xstrdup(optarg);
229 case 5: /* write PID to a file */
230 pidfilename = xstrdup(optarg);
245 /* This function prettyprints the key generation process */
247 static void indicator(int a, int b, void *p)
251 fprintf(stderr, ".");
255 fprintf(stderr, "+");
259 fprintf(stderr, "-");
265 fprintf(stderr, " p\n");
269 fprintf(stderr, " q\n");
273 fprintf(stderr, "?");
278 fprintf(stderr, "?");
283 Generate a public/private RSA keypair, and ask for a file to store
286 static bool keygen(int bits)
293 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
294 rsa_key = RSA_generate_key(bits, 0x10001, indicator, NULL);
297 fprintf(stderr, _("Error during key generation!\n"));
300 fprintf(stderr, _("Done.\n"));
302 asprintf(&filename, "%s/rsa_key.priv", confbase);
303 f = ask_and_open(filename, _("private RSA key"), "a");
309 /* Make it unreadable for others. */
310 fchmod(fileno(f), 0600);
314 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
316 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
320 get_config_string(lookup_config(config_tree, "Name"), &name);
323 asprintf(&filename, "%s/hosts/%s", confbase, name);
325 asprintf(&filename, "%s/rsa_key.pub", confbase);
327 f = ask_and_open(filename, _("public RSA key"), "a");
333 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
335 PEM_write_RSAPublicKey(f, rsa_key);
343 Set all files and paths according to netname
345 static void make_names(void)
349 char installdir[1024] = "";
350 long len = sizeof(installdir);
354 asprintf(&identname, "tinc.%s", netname);
356 identname = xstrdup("tinc");
359 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
360 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
362 asprintf(&logfilename, "%s/log/%s.log", identname);
365 asprintf(&confbase, "%s/%s", installdir, netname);
367 asprintf(&confbase, "%s", installdir);
377 asprintf(&pidfilename, LOCALSTATEDIR "/run/%s.pid", identname);
380 asprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
384 asprintf(&confbase, CONFDIR "/tinc/%s", netname);
386 logger(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
389 asprintf(&confbase, CONFDIR "/tinc");
393 int main(int argc, char **argv)
395 program_name = argv[0];
397 setlocale(LC_ALL, "");
398 bindtextdomain(PACKAGE, LOCALEDIR);
401 if(!parse_options(argc, argv))
407 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
408 VERSION, __DATE__, __TIME__, PROT_CURRENT);
409 printf(_("Copyright (C) 1998-2003 Ivo Timmermans, Guus Sliepen and others.\n"
410 "See the AUTHORS file for a complete list.\n\n"
411 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
412 "and you are welcome to redistribute it under certain conditions;\n"
413 "see the file COPYING for details.\n"));
424 return !kill_other(kill_tincd);
426 openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
428 /* Lock all pages into memory if requested */
432 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
433 logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
437 logger(LOG_ERR, _("mlockall() not supported on this platform!"));
444 init_configuration(&config_tree);
446 /* Slllluuuuuuurrrrp! */
448 RAND_load_file("/dev/urandom", 1024);
450 OpenSSL_add_all_algorithms();
453 read_server_config();
454 return !keygen(generate_keys);
457 if(!read_server_config())
460 if(lzo_init() != LZO_E_OK) {
461 logger(LOG_ERR, _("Error initializing LZO compressor!"));
466 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
467 logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", winerror(GetLastError()));
471 if(!do_detach || !init_service())
472 return main2(argc, argv);
477 int main2(int argc, char **argv)
485 /* Setup sockets and open device. If it doesn't work, don't give up but try again. */
487 while(!setup_network_connections()) {
489 logger(LOG_NOTICE, _("Restarting in %d seconds!"), maxtimeout);
492 logger(LOG_ERR, _("Not restarting."));
497 /* Start main loop. It only exits when tinc is killed. */
499 status = main_loop();
501 /* Shutdown properly. */
503 close_network_connections();
508 logger(LOG_NOTICE, _("Terminating"));
511 remove_pid(pidfilename);