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.80 2003/08/02 20:50:38 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>
52 /* The name this program was run with. */
53 char *program_name = NULL;
55 /* If nonzero, display usage information and exit. */
56 bool show_help = false;
58 /* If nonzero, print the version on standard output and exit. */
59 bool show_version = false;
61 /* If nonzero, it will attempt to kill a running tincd and exit. */
64 /* If nonzero, generate public/private keypair for this host/net. */
65 int generate_keys = 0;
67 /* If nonzero, use null ciphers and skip all key exchanges. */
68 bool bypass_security = false;
70 /* If nonzero, disable swapping for this process. */
71 bool do_mlock = false;
73 /* If nonzero, write log entries to a separate file. */
74 bool use_logfile = false;
76 char *identname = NULL; /* program name for syslog */
77 char *pidfilename = NULL; /* pid file location */
78 char *logfilename = NULL; /* log file location */
79 char **g_argv; /* a copy of the cmdline arguments */
83 static struct option const long_options[] = {
84 {"config", required_argument, NULL, 'c'},
85 {"kill", optional_argument, NULL, 'k'},
86 {"net", required_argument, NULL, 'n'},
87 {"help", no_argument, NULL, 1},
88 {"version", no_argument, NULL, 2},
89 {"no-detach", no_argument, NULL, 'D'},
90 {"generate-keys", optional_argument, NULL, 'K'},
91 {"debug", optional_argument, NULL, 'd'},
92 {"bypass-security", no_argument, NULL, 3},
93 {"mlock", no_argument, NULL, 'L'},
94 {"logfile", optional_argument, NULL, 4},
95 {"pidfile", required_argument, NULL, 5},
100 static struct WSAData wsa_state;
103 static void usage(bool status)
106 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
109 printf(_("Usage: %s [option]...\n\n"), program_name);
110 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
111 " -D, --no-detach Don't fork and detach.\n"
112 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
113 " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
114 " -n, --net=NETNAME Connect to net NETNAME.\n"
115 " -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
116 " -L, --mlock Lock tinc into main memory.\n"
117 " --logfile[=FILENAME] Write log entries to a logfile.\n"
118 " --pidfile=FILENAME Write PID to FILENAME.\n"
119 " --help Display this help and exit.\n"
120 " --version Output version information and exit.\n\n"));
121 printf(_("Report bugs to tinc@nl.linux.org.\n"));
125 static bool parse_options(int argc, char **argv)
128 int option_index = 0;
130 while((r = getopt_long(argc, argv, "c:DLd::k::n:K::", long_options, &option_index)) != EOF) {
132 case 0: /* long option */
135 case 'c': /* config file */
136 confbase = xstrdup(optarg);
139 case 'D': /* no detach */
143 case 'L': /* no detach */
147 case 'd': /* inc debug level */
149 debug_level = atoi(optarg);
154 case 'k': /* kill old tincds */
157 if(!strcasecmp(optarg, "HUP"))
159 else if(!strcasecmp(optarg, "TERM"))
160 kill_tincd = SIGTERM;
161 else if(!strcasecmp(optarg, "KILL"))
162 kill_tincd = SIGKILL;
163 else if(!strcasecmp(optarg, "USR1"))
164 kill_tincd = SIGUSR1;
165 else if(!strcasecmp(optarg, "USR2"))
166 kill_tincd = SIGUSR2;
167 else if(!strcasecmp(optarg, "WINCH"))
168 kill_tincd = SIGWINCH;
169 else if(!strcasecmp(optarg, "INT"))
171 else if(!strcasecmp(optarg, "ALRM"))
172 kill_tincd = SIGALRM;
174 kill_tincd = atoi(optarg);
177 fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
184 kill_tincd = SIGTERM;
190 case 'n': /* net name given */
191 netname = xstrdup(optarg);
194 case 'K': /* generate public/private keypair */
196 generate_keys = atoi(optarg);
198 if(generate_keys < 512) {
199 fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
205 generate_keys &= ~7; /* Round it to bytes */
207 generate_keys = 1024;
210 case 1: /* show help */
214 case 2: /* show version */
218 case 3: /* bypass security */
219 bypass_security = true;
222 case 4: /* write log entries to a file */
225 logfilename = xstrdup(optarg);
228 case 5: /* write PID to a file */
229 pidfilename = xstrdup(optarg);
244 /* This function prettyprints the key generation process */
246 static void indicator(int a, int b, void *p)
250 fprintf(stderr, ".");
254 fprintf(stderr, "+");
258 fprintf(stderr, "-");
264 fprintf(stderr, " p\n");
268 fprintf(stderr, " q\n");
272 fprintf(stderr, "?");
277 fprintf(stderr, "?");
282 Generate a public/private RSA keypair, and ask for a file to store
285 static bool keygen(int bits)
292 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
293 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
296 fprintf(stderr, _("Error during key generation!\n"));
299 fprintf(stderr, _("Done.\n"));
301 asprintf(&filename, "%s/rsa_key.priv", confbase);
302 f = ask_and_safe_open(filename, _("private RSA key"), true, "a");
308 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
310 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
314 get_config_string(lookup_config(config_tree, "Name"), &name);
317 asprintf(&filename, "%s/hosts/%s", confbase, name);
319 asprintf(&filename, "%s/rsa_key.pub", confbase);
321 f = ask_and_safe_open(filename, _("public RSA key"), false, "a");
327 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
329 PEM_write_RSAPublicKey(f, rsa_key);
337 Set all files and paths according to netname
339 static void make_names(void)
342 asprintf(&identname, "tinc.%s", netname);
344 identname = xstrdup("tinc");
347 asprintf(&pidfilename, LOCALSTATEDIR "/run/%s.pid", identname);
350 asprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
354 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
356 logger(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
359 asprintf(&confbase, "%s/tinc", CONFDIR);
363 int main(int argc, char **argv)
365 program_name = argv[0];
367 setlocale(LC_ALL, "");
368 bindtextdomain(PACKAGE, LOCALEDIR);
371 if(!parse_options(argc, argv))
377 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
378 VERSION, __DATE__, __TIME__, PROT_CURRENT);
379 printf(_("Copyright (C) 1998-2003 Ivo Timmermans, Guus Sliepen and others.\n"
380 "See the AUTHORS file for a complete list.\n\n"
381 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
382 "and you are welcome to redistribute it under certain conditions;\n"
383 "see the file COPYING for details.\n"));
394 return !kill_other(kill_tincd);
396 openlogger("tinc", LOGMODE_STDERR);
398 /* Lock all pages into memory if requested */
402 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
403 logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
407 logger(LOG_ERR, _("mlockall() not supported on this platform!"));
414 init_configuration(&config_tree);
416 /* Slllluuuuuuurrrrp! */
418 RAND_load_file("/dev/urandom", 1024);
420 OpenSSL_add_all_algorithms();
423 read_server_config();
424 return !keygen(generate_keys);
427 if(!read_server_config())
430 if(lzo_init() != LZO_E_OK) {
431 logger(LOG_ERR, _("Error initializing LZO compressor!"));
436 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
437 logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", winerror(GetLastError()));
441 if(!do_detach || !init_service())
442 return main2(argc, argv);
447 int main2(int argc, char **argv)
455 if(setup_network_connections()) {
457 status = main_loop();
459 close_network_connections();
464 logger(LOG_NOTICE, _("Terminating"));
468 logger(LOG_ERR, _("Unrecoverable error"));
472 logger(LOG_NOTICE, _("Restarting in %d seconds!"), maxtimeout);
475 logger(LOG_ERR, _("Not restarting."));