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.62 2002/09/09 19:40:12 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[] =
88 { "config", required_argument, NULL, 'c' },
89 { "kill", optional_argument, NULL, 'k' },
90 { "net", required_argument, NULL, 'n' },
91 { "help", no_argument, &show_help, 1 },
92 { "version", no_argument, &show_version, 1 },
93 { "no-detach", no_argument, &do_detach, 0 },
94 { "generate-keys", optional_argument, NULL, 'K'},
95 { "debug", optional_argument, NULL, 'd'},
96 { "bypass-security", no_argument, &bypass_security, 1 },
97 { "mlock", no_argument, &do_mlock, 1},
105 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
108 printf(_("Usage: %s [option]...\n\n"), program_name);
109 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
110 " -D, --no-detach Don't fork and detach.\n"
111 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
112 " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
113 " -n, --net=NETNAME Connect to net NETNAME.\n"
114 " -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
115 " -L, --mlock Lock tinc into main memory.\n"
116 " --help Display this help and exit.\n"
117 " --version Output version information and exit.\n\n"));
118 printf(_("Report bugs to tinc@nl.linux.org.\n"));
124 parse_options(int argc, char **argv, char **envp)
127 int option_index = 0;
129 while((r = getopt_long(argc, argv, "c:DLd::k::n:K::", long_options, &option_index)) != EOF)
133 case 0: /* long option */
135 case 'c': /* config file */
136 confbase = xmalloc(strlen(optarg)+1);
137 strcpy(confbase, optarg);
139 case 'D': /* no detach */
142 case 'L': /* no detach */
145 case 'd': /* inc debug level */
147 debug_lvl = atoi(optarg);
151 case 'k': /* kill old tincds */
154 if(!strcasecmp(optarg, "HUP"))
156 else if(!strcasecmp(optarg, "TERM"))
157 kill_tincd = SIGTERM;
158 else if(!strcasecmp(optarg, "KILL"))
159 kill_tincd = SIGKILL;
160 else if(!strcasecmp(optarg, "USR1"))
161 kill_tincd = SIGUSR1;
162 else if(!strcasecmp(optarg, "USR2"))
163 kill_tincd = SIGUSR2;
164 else if(!strcasecmp(optarg, "WINCH"))
165 kill_tincd = SIGWINCH;
166 else if(!strcasecmp(optarg, "INT"))
168 else if(!strcasecmp(optarg, "ALRM"))
169 kill_tincd = SIGALRM;
172 kill_tincd = atoi(optarg);
175 fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), optarg);
181 kill_tincd = SIGTERM;
183 case 'n': /* net name given */
184 netname = xmalloc(strlen(optarg)+1);
185 strcpy(netname, optarg);
187 case 'K': /* generate public/private keypair */
190 generate_keys = atoi(optarg);
191 if(generate_keys < 512)
193 fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
197 generate_keys &= ~7; /* Round it to bytes */
200 generate_keys = 1024;
210 /* This function prettyprints the key generation process */
212 void indicator(int a, int b, void *p)
217 fprintf(stderr, ".");
220 fprintf(stderr, "+");
223 fprintf(stderr, "-");
229 fprintf(stderr, " p\n");
232 fprintf(stderr, " q\n");
235 fprintf(stderr, "?");
239 fprintf(stderr, "?");
244 Generate a public/private RSA keypair, and ask for a file to store
254 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
255 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
259 fprintf(stderr, _("Error during key generation!\n"));
263 fprintf(stderr, _("Done.\n"));
265 get_config_string(lookup_config(config_tree, "Name"), &name);
268 asprintf(&filename, "%s/hosts/%s", confbase, name);
270 asprintf(&filename, "%s/rsa_key.pub", confbase);
272 f = ask_and_safe_open(filename, _("public RSA key"), "a");
278 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
280 PEM_write_RSAPublicKey(f, rsa_key);
284 asprintf(&filename, "%s/rsa_key.priv", confbase);
285 f = ask_and_safe_open(filename, _("private RSA key"), "a");
291 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
293 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
301 Set all files and paths according to netname
303 void make_names(void)
308 asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname);
310 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
312 syslog(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
314 asprintf(&identname, "tinc.%s", netname);
319 pidfilename = LOCALSTATEDIR "/run/tinc.pid";
321 asprintf(&confbase, "%s/tinc", CONFDIR);
328 main(int argc, char **argv, char **envp)
330 program_name = argv[0];
332 setlocale (LC_ALL, "");
333 bindtextdomain (PACKAGE, LOCALEDIR);
334 textdomain (PACKAGE);
337 parse_options(argc, argv, envp);
341 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
342 printf(_("Copyright (C) 1998-2002 Ivo Timmermans, Guus Sliepen and others.\n"
343 "See the AUTHORS file for a complete list.\n\n"
344 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
345 "and you are welcome to redistribute it under certain conditions;\n"
346 "see the file COPYING for details.\n"));
355 openlog("tinc", LOG_CONS, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
357 openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
360 /* Lock all pages into memory if requested */
364 if(mlockall(MCL_CURRENT | MCL_FUTURE))
366 syslog(LOG_ERR, _("System call `%s' failed: %s"), "mlockall", strerror(errno));
369 syslog(LOG_ERR, _("mlockall() not supported on this platform!"));
377 init_configuration(&config_tree);
379 /* Slllluuuuuuurrrrp! */
381 RAND_load_file("/dev/urandom", 1024);
383 #ifdef HAVE_SSLEAY_ADD_ALL_ALGORITHMS
384 SSLeay_add_all_algorithms();
386 OpenSSL_add_all_algorithms();
392 read_server_config();
393 exit(keygen(generate_keys));
397 exit(kill_other(kill_tincd));
399 if(read_server_config())
407 if(!setup_network_connections())
413 syslog(LOG_ERR, _("Unrecoverable error"));
418 syslog(LOG_NOTICE, _("Restarting in %d seconds!"), maxtimeout);
423 syslog(LOG_ERR, _("Not restarting."));