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.75 2003/07/22 12:58:34 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>
50 /* The name this program was run with. */
51 char *program_name = NULL;
53 /* If nonzero, display usage information and exit. */
56 /* If nonzero, print the version on standard output and exit. */
59 /* If nonzero, it will attempt to kill a running tincd and exit. */
62 /* If nonzero, generate public/private keypair for this host/net. */
63 int generate_keys = 0;
65 /* If nonzero, use null ciphers and skip all key exchanges. */
66 int bypass_security = 0;
68 /* If nonzero, disable swapping for this process. */
71 /* If nonzero, write log entries to a separate file. */
74 char *identname = NULL; /* program name for syslog */
75 char *pidfilename = NULL; /* pid file location */
76 char *logfilename = NULL; /* log file location */
77 char **g_argv; /* a copy of the cmdline arguments */
78 char **environment; /* A pointer to the environment on
81 static struct option const long_options[] = {
82 {"config", required_argument, NULL, 'c'},
83 {"kill", optional_argument, NULL, 'k'},
84 {"net", required_argument, NULL, 'n'},
85 {"help", no_argument, &show_help, 1},
86 {"version", no_argument, &show_version, 1},
87 {"no-detach", no_argument, &do_detach, 0},
88 {"generate-keys", optional_argument, NULL, 'K'},
89 {"debug", optional_argument, NULL, 'd'},
90 {"bypass-security", no_argument, &bypass_security, 1},
91 {"mlock", no_argument, &do_mlock, 1},
92 {"logfile", optional_argument, NULL, 'F'},
93 {"pidfile", required_argument, NULL, 'P'},
97 static void usage(int status)
100 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
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, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
107 " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
108 " -n, --net=NETNAME Connect to net NETNAME.\n"
109 " -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
110 " -L, --mlock Lock tinc into main memory.\n"
111 " --logfile[=FILENAME] Write log entries to a logfile.\n"
112 " --pidfile=FILENAME Write PID to FILENAME.\n"
113 " --help Display this help and exit.\n"
114 " --version Output version information and exit.\n\n"));
115 printf(_("Report bugs to tinc@nl.linux.org.\n"));
121 static void parse_options(int argc, char **argv, char **envp)
124 int option_index = 0;
126 while((r = getopt_long(argc, argv, "c:DLd::k::n:K::", long_options, &option_index)) != EOF) {
128 case 0: /* long option */
131 case 'c': /* config file */
132 confbase = xstrdup(optarg);
135 case 'D': /* no detach */
139 case 'L': /* no detach */
143 case 'd': /* inc debug level */
145 debug_level = atoi(optarg);
150 case 'k': /* kill old tincds */
152 if(!strcasecmp(optarg, "HUP"))
154 else if(!strcasecmp(optarg, "TERM"))
155 kill_tincd = SIGTERM;
156 else if(!strcasecmp(optarg, "KILL"))
157 kill_tincd = SIGKILL;
158 else if(!strcasecmp(optarg, "USR1"))
159 kill_tincd = SIGUSR1;
160 else if(!strcasecmp(optarg, "USR2"))
161 kill_tincd = SIGUSR2;
162 else if(!strcasecmp(optarg, "WINCH"))
163 kill_tincd = SIGWINCH;
164 else if(!strcasecmp(optarg, "INT"))
166 else if(!strcasecmp(optarg, "ALRM"))
167 kill_tincd = SIGALRM;
169 kill_tincd = atoi(optarg);
172 fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
178 kill_tincd = SIGTERM;
181 case 'n': /* net name given */
182 netname = xstrdup(optarg);
185 case 'K': /* generate public/private keypair */
187 generate_keys = atoi(optarg);
189 if(generate_keys < 512) {
190 fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
195 generate_keys &= ~7; /* Round it to bytes */
197 generate_keys = 1024;
200 case 'F': /* write log entries to a file */
203 logfilename = xstrdup(optarg);
206 case 'P': /* write PID to a file */
207 pidfilename = xstrdup(optarg);
219 /* This function prettyprints the key generation process */
221 static void indicator(int a, int b, void *p)
225 fprintf(stderr, ".");
229 fprintf(stderr, "+");
233 fprintf(stderr, "-");
239 fprintf(stderr, " p\n");
243 fprintf(stderr, " q\n");
247 fprintf(stderr, "?");
252 fprintf(stderr, "?");
257 Generate a public/private RSA keypair, and ask for a file to store
260 static int keygen(int bits)
267 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
268 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
271 fprintf(stderr, _("Error during key generation!\n"));
274 fprintf(stderr, _("Done.\n"));
276 get_config_string(lookup_config(config_tree, "Name"), &name);
279 asprintf(&filename, "%s/hosts/%s", confbase, name);
281 asprintf(&filename, "%s/rsa_key.pub", confbase);
283 f = ask_and_safe_open(filename, _("public RSA key"), "a");
289 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
291 PEM_write_RSAPublicKey(f, rsa_key);
295 asprintf(&filename, "%s/rsa_key.priv", confbase);
296 f = ask_and_safe_open(filename, _("private RSA key"), "a");
302 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
304 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
312 Set all files and paths according to netname
314 static void make_names(void)
317 asprintf(&identname, "tinc.%s", netname);
319 identname = xstrdup("tinc");
322 asprintf(&pidfilename, LOCALSTATEDIR "/run/%s.pid", identname);
325 asprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
329 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
331 logger(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
334 asprintf(&confbase, "%s/tinc", CONFDIR);
338 int main(int argc, char **argv, char **envp)
340 program_name = argv[0];
342 setlocale(LC_ALL, "");
343 bindtextdomain(PACKAGE, LOCALEDIR);
347 parse_options(argc, argv, envp);
351 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
352 VERSION, __DATE__, __TIME__, PROT_CURRENT);
353 printf(_("Copyright (C) 1998-2003 Ivo Timmermans, Guus Sliepen and others.\n"
354 "See the AUTHORS file for a complete list.\n\n"
355 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
356 "and you are welcome to redistribute it under certain conditions;\n"
357 "see the file COPYING for details.\n"));
366 exit(kill_other(kill_tincd));
368 openlogger("tinc", LOGMODE_STDERR);
370 /* Lock all pages into memory if requested */
374 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
375 logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
379 logger(LOG_ERR, _("mlockall() not supported on this platform!"));
386 init_configuration(&config_tree);
388 /* Slllluuuuuuurrrrp! */
390 RAND_load_file("/dev/urandom", 1024);
392 OpenSSL_add_all_algorithms();
395 read_server_config();
396 exit(keygen(generate_keys));
399 if(read_server_config())
402 if(lzo_init() != LZO_E_OK) {
403 logger(LOG_ERR, _("Error initializing LZO compressor!"));
411 if(!setup_network_connections()) {
416 logger(LOG_ERR, _("Unrecoverable error"));
420 logger(LOG_NOTICE, _("Restarting in %d seconds!"), maxtimeout);
423 logger(LOG_ERR, _("Not restarting."));