2 tincd.c -- the main file for tincd
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>
4 2000-2002 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.63 2002/09/09 21:25:16 guus Exp $
30 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
42 #include <openssl/rand.h>
43 #include <openssl/rsa.h>
44 #include <openssl/pem.h>
45 #include <openssl/evp.h>
59 /* The name this program was run with. */
62 /* If nonzero, display usage information and exit. */
65 /* If nonzero, print the version on standard output and exit. */
68 /* If nonzero, it will attempt to kill a running tincd and exit. */
71 /* If nonzero, generate public/private keypair for this host/net. */
72 int generate_keys = 0;
74 /* If nonzero, use null ciphers and skip all key exchanges. */
75 int bypass_security = 0;
77 /* If nonzero, disable swapping for this process. */
80 char *identname; /* program name for syslog */
81 char *pidfilename; /* pid file location */
82 char **g_argv; /* a copy of the cmdline arguments */
83 char **environment; /* A pointer to the environment on
86 static struct option const long_options[] = {
87 {"config", required_argument, NULL, 'c'},
88 {"kill", optional_argument, NULL, 'k'},
89 {"net", required_argument, NULL, 'n'},
90 {"help", no_argument, &show_help, 1},
91 {"version", no_argument, &show_version, 1},
92 {"no-detach", no_argument, &do_detach, 0},
93 {"generate-keys", optional_argument, NULL, 'K'},
94 {"debug", optional_argument, NULL, 'd'},
95 {"bypass-security", no_argument, &bypass_security, 1},
96 {"mlock", no_argument, &do_mlock, 1},
100 static void usage(int status)
103 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
106 printf(_("Usage: %s [option]...\n\n"), program_name);
108 (" -c, --config=DIR Read configuration options from DIR.\n"
109 " -D, --no-detach Don't fork and detach.\n"
110 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
111 " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
112 " -n, --net=NETNAME Connect to net NETNAME.\n"
113 " -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
114 " -L, --mlock Lock tinc into main memory.\n"
115 " --help Display this help and exit.\n"
116 " --version Output version information and exit.\n\n"));
117 printf(_("Report bugs to tinc@nl.linux.org.\n"));
123 void parse_options(int argc, char **argv, char **envp)
126 int option_index = 0;
128 while((r = getopt_long(argc, argv, "c:DLd::k::n:K::", long_options, &option_index)) != EOF) {
130 case 0: /* long option */
133 case 'c': /* config file */
134 confbase = xmalloc(strlen(optarg) + 1);
135 strcpy(confbase, optarg);
138 case 'D': /* no detach */
142 case 'L': /* no detach */
146 case 'd': /* inc debug level */
148 debug_lvl = atoi(optarg);
153 case 'k': /* kill old tincds */
155 if(!strcasecmp(optarg, "HUP"))
157 else if(!strcasecmp(optarg, "TERM"))
158 kill_tincd = SIGTERM;
159 else if(!strcasecmp(optarg, "KILL"))
160 kill_tincd = SIGKILL;
161 else if(!strcasecmp(optarg, "USR1"))
162 kill_tincd = SIGUSR1;
163 else if(!strcasecmp(optarg, "USR2"))
164 kill_tincd = SIGUSR2;
165 else if(!strcasecmp(optarg, "WINCH"))
166 kill_tincd = SIGWINCH;
167 else if(!strcasecmp(optarg, "INT"))
169 else if(!strcasecmp(optarg, "ALRM"))
170 kill_tincd = SIGALRM;
172 kill_tincd = atoi(optarg);
177 ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
183 kill_tincd = SIGTERM;
186 case 'n': /* net name given */
187 netname = xmalloc(strlen(optarg) + 1);
188 strcpy(netname, optarg);
191 case 'K': /* generate public/private keypair */
193 generate_keys = atoi(optarg);
195 if(generate_keys < 512) {
198 ("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
203 generate_keys &= ~7; /* Round it to bytes */
205 generate_keys = 1024;
217 /* This function prettyprints the key generation process */
219 void indicator(int a, int b, void *p)
223 fprintf(stderr, ".");
227 fprintf(stderr, "+");
231 fprintf(stderr, "-");
237 fprintf(stderr, " p\n");
241 fprintf(stderr, " q\n");
245 fprintf(stderr, "?");
250 fprintf(stderr, "?");
255 Generate a public/private RSA keypair, and ask for a file to store
265 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
266 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
269 fprintf(stderr, _("Error during key generation!\n"));
272 fprintf(stderr, _("Done.\n"));
274 get_config_string(lookup_config(config_tree, "Name"), &name);
277 asprintf(&filename, "%s/hosts/%s", confbase, name);
279 asprintf(&filename, "%s/rsa_key.pub", confbase);
281 f = ask_and_safe_open(filename, _("public RSA key"), "a");
287 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
289 PEM_write_RSAPublicKey(f, rsa_key);
293 asprintf(&filename, "%s/rsa_key.priv", confbase);
294 f = ask_and_safe_open(filename, _("private RSA key"), "a");
300 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
302 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
310 Set all files and paths according to netname
312 void make_names(void)
316 asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname);
319 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
321 syslog(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
324 asprintf(&identname, "tinc.%s", netname);
327 pidfilename = LOCALSTATEDIR "/run/tinc.pid";
330 asprintf(&confbase, "%s/tinc", CONFDIR);
337 int main(int argc, char **argv, char **envp)
339 program_name = argv[0];
341 setlocale(LC_ALL, "");
342 bindtextdomain(PACKAGE, LOCALEDIR);
346 parse_options(argc, argv, envp);
349 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
350 VERSION, __DATE__, __TIME__, PROT_CURRENT);
351 printf(_("Copyright (C) 1998-2002 Ivo Timmermans, Guus Sliepen and others.\n"
352 "See the AUTHORS file for a complete list.\n\n"
353 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
354 "and you are welcome to redistribute it under certain conditions;\n"
355 "see the file COPYING for details.\n"));
364 openlog("tinc", LOG_CONS, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
366 openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
369 /* Lock all pages into memory if requested */
373 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
374 syslog(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
378 syslog(LOG_ERR, _("mlockall() not supported on this platform!"));
386 init_configuration(&config_tree);
388 /* Slllluuuuuuurrrrp! */
390 RAND_load_file("/dev/urandom", 1024);
392 #ifdef HAVE_SSLEAY_ADD_ALL_ALGORITHMS
393 SSLeay_add_all_algorithms();
395 OpenSSL_add_all_algorithms();
399 read_server_config();
400 exit(keygen(generate_keys));
404 exit(kill_other(kill_tincd));
406 if(read_server_config())
413 if(!setup_network_connections()) {
418 syslog(LOG_ERR, _("Unrecoverable error"));
422 syslog(LOG_NOTICE, _("Restarting in %d seconds!"), maxtimeout);
425 syslog(LOG_ERR, _("Not restarting."));