2 tincd.c -- the main file for tincd
3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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.12 2002/04/09 15:26:01 zarq Exp $
30 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
41 #include <openssl/rand.h>
42 #include <openssl/rsa.h>
43 #include <openssl/pem.h>
44 #include <openssl/evp.h>
58 /* The name this program was run with. */
61 /* If nonzero, display usage information and exit. */
64 /* If nonzero, print the version on standard output and exit. */
67 /* If nonzero, it will attempt to kill a running tincd and exit. */
70 /* If nonzero, generate public/private keypair for this host/net. */
71 int generate_keys = 0;
73 /* If nonzero, use null ciphers and skip all key exchanges. */
74 int bypass_security = 0;
76 char *identname; /* program name for syslog */
77 char *pidfilename; /* pid file location */
78 char **g_argv; /* a copy of the cmdline arguments */
79 char **environment; /* A pointer to the environment on
82 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, &show_help, 1 },
88 { "version", no_argument, &show_version, 1 },
89 { "no-detach", no_argument, &do_detach, 0 },
90 { "generate-keys", optional_argument, NULL, 'K'},
91 { "debug", optional_argument, NULL, 'd'},
92 { "bypass-security", no_argument, &bypass_security, 1 },
100 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
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 printf(_(" -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
110 " --help Display this help and exit.\n"
111 " --version Output version information and exit.\n\n"));
112 printf(_("Report bugs to tinc@nl.linux.org.\n"));
118 parse_options(int argc, char **argv, char **envp)
121 int option_index = 0;
123 while((r = getopt_long(argc, argv, "c:Dd::k::n:K::", long_options, &option_index)) != EOF)
127 case 0: /* long option */
129 case 'c': /* config file */
130 confbase = xmalloc(strlen(optarg)+1);
131 strcpy(confbase, optarg);
133 case 'D': /* no detach */
136 case 'd': /* inc debug level */
138 debug_lvl = atoi(optarg);
142 case 'k': /* kill old tincds */
145 if(!strcasecmp(optarg, "HUP"))
147 else if(!strcasecmp(optarg, "TERM"))
148 kill_tincd = SIGTERM;
149 else if(!strcasecmp(optarg, "KILL"))
150 kill_tincd = SIGKILL;
151 else if(!strcasecmp(optarg, "USR1"))
152 kill_tincd = SIGUSR1;
153 else if(!strcasecmp(optarg, "USR2"))
154 kill_tincd = SIGUSR2;
155 else if(!strcasecmp(optarg, "WINCH"))
156 kill_tincd = SIGWINCH;
157 else if(!strcasecmp(optarg, "INT"))
159 else if(!strcasecmp(optarg, "ALRM"))
160 kill_tincd = SIGALRM;
163 kill_tincd = atoi(optarg);
166 fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), optarg);
172 kill_tincd = SIGTERM;
174 case 'n': /* net name given */
175 netname = xmalloc(strlen(optarg)+1);
176 strcpy(netname, optarg);
178 case 'K': /* generate public/private keypair */
181 generate_keys = atoi(optarg);
182 if(generate_keys < 512)
184 fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
188 generate_keys &= ~7; /* Round it to bytes */
191 generate_keys = 1024;
201 /* This function prettyprints the key generation process */
203 void indicator(int a, int b, void *p)
208 fprintf(stderr, ".");
211 fprintf(stderr, "+");
214 fprintf(stderr, "-");
220 fprintf(stderr, " p\n");
223 fprintf(stderr, " q\n");
226 fprintf(stderr, "?");
230 fprintf(stderr, "?");
235 Generate a public/private RSA keypair, and ask for a file to store
245 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
246 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
250 fprintf(stderr, _("Error during key generation!\n"));
254 fprintf(stderr, _("Done.\n"));
256 get_config_string(lookup_config(config_tree, "Name"), &name);
259 asprintf(&filename, "%s/hosts/%s", confbase, name);
261 asprintf(&filename, "%s/rsa_key.pub", confbase);
263 if((f = ask_and_safe_open(filename, _("public RSA key"), "a")) == NULL)
267 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
269 PEM_write_RSAPublicKey(f, rsa_key);
273 asprintf(&filename, "%s/rsa_key.priv", confbase);
274 if((f = ask_and_safe_open(filename, _("private RSA key"), "a")) == NULL)
278 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
280 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
288 Set all files and paths according to netname
290 void make_names(void)
295 asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname);
297 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
299 syslog(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
301 asprintf(&identname, "tinc.%s", netname);
306 pidfilename = LOCALSTATEDIR "/run/tinc.pid";
308 asprintf(&confbase, "%s/tinc", CONFDIR);
315 main(int argc, char **argv, char **envp)
317 program_name = argv[0];
319 setlocale (LC_ALL, "");
320 bindtextdomain (PACKAGE, LOCALEDIR);
321 textdomain (PACKAGE);
324 parse_options(argc, argv, envp);
328 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
329 printf(_("Copyright (C) 1998-2002 Ivo Timmermans, Guus Sliepen and others.\n"
330 "See the AUTHORS file for a complete list.\n\n"
331 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
332 "and you are welcome to redistribute it under certain conditions;\n"
333 "see the file COPYING for details.\n"));
342 openlog("tinc", LOG_CONS, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
344 openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
350 init_configuration(&config_tree);
352 /* Slllluuuuuuurrrrp! */
354 RAND_load_file("/dev/urandom", 1024);
356 #ifdef HAVE_SSLEAY_ADD_ALL_ALGORITHMS
357 SSLeay_add_all_algorithms();
359 OpenSSL_add_all_algorithms();
365 read_server_config();
366 exit(keygen(generate_keys));
370 exit(kill_other(kill_tincd));
372 if(read_server_config())
380 if(!setup_network_connections())
386 syslog(LOG_ERR, _("Unrecoverable error"));
391 syslog(LOG_NOTICE, _("Restarting in %d seconds!"), maxtimeout);
396 syslog(LOG_ERR, _("Not restarting."));