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.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: pokey.c,v 1.2 2002/04/13 11:21: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>
47 #include <glade/glade.h>
53 #include "interface.h"
63 /* The name this program was run with. */
66 /* If nonzero, display usage information and exit. */
69 /* If nonzero, print the version on standard output and exit. */
72 /* If nonzero, it will attempt to kill a running tincd and exit. */
75 /* If nonzero, generate public/private keypair for this host/net. */
76 int generate_keys = 0;
78 /* If nonzero, use null ciphers and skip all key exchanges. */
79 int bypass_security = 0;
81 char *identname; /* program name for syslog */
82 char *pidfilename; /* pid file location */
83 char **g_argv; /* a copy of the cmdline arguments */
84 char **environment; /* A pointer to the environment on startup */
90 static struct option const long_options[] =
92 { "config", required_argument, NULL, 'c' },
93 { "kill", optional_argument, NULL, 'k' },
94 { "net", required_argument, NULL, 'n' },
95 { "help", no_argument, &show_help, 1 },
96 { "version", no_argument, &show_version, 1 },
97 { "no-detach", no_argument, &do_detach, 0 },
98 { "generate-keys", optional_argument, NULL, 'K'},
99 { "debug", optional_argument, NULL, 'd'},
100 { "bypass-security", no_argument, &bypass_security, 1 },
108 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
111 printf(_("Usage: %s [option]...\n\n"), program_name);
112 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
113 " -D, --no-detach Don't fork and detach.\n"
114 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
115 " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
116 " -n, --net=NETNAME Connect to net NETNAME.\n"));
117 printf(_(" -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
118 " --help Display this help and exit.\n"
119 " --version Output version information and exit.\n\n"));
120 printf(_("Report bugs to tinc@nl.linux.org.\n"));
126 parse_options(int argc, char **argv, char **envp)
129 int option_index = 0;
131 while((r = getopt_long(argc, argv, "c:Dd::k::n:K::", long_options, &option_index)) != EOF)
135 case 0: /* long option */
137 case 'c': /* config file */
138 confbase = xmalloc(strlen(optarg)+1);
139 strcpy(confbase, optarg);
141 case 'D': /* no detach */
144 case 'd': /* inc debug level */
146 debug_lvl = atoi(optarg);
150 case 'k': /* kill old tincds */
151 kill_tincd = optarg?atoi(optarg):SIGTERM;
153 case 'n': /* net name given */
154 netname = xmalloc(strlen(optarg)+1);
155 strcpy(netname, optarg);
157 case 'K': /* generate public/private keypair */
160 generate_keys = atoi(optarg);
161 if(generate_keys < 512)
163 fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
167 generate_keys &= ~7; /* Round it to bytes */
170 generate_keys = 1024;
180 /* This function prettyprints the key generation process */
182 void indicator(int a, int b, void *p)
187 fprintf(stderr, ".");
190 fprintf(stderr, "+");
193 fprintf(stderr, "-");
199 fprintf(stderr, " p\n");
202 fprintf(stderr, " q\n");
205 fprintf(stderr, "?");
209 fprintf(stderr, "?");
214 Generate a public/private RSA keypair, and ask for a file to store
224 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
225 rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
229 fprintf(stderr, _("Error during key generation!\n"));
233 fprintf(stderr, _("Done.\n"));
235 get_config_string(lookup_config(config_tree, "Name"), &name);
238 asprintf(&filename, "%s/hosts/%s", confbase, name);
240 asprintf(&filename, "%s/rsa_key.pub", confbase);
242 if((f = ask_and_safe_open(filename, _("public RSA key"), "a")) == NULL)
246 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
248 PEM_write_RSAPublicKey(f, rsa_key);
252 asprintf(&filename, "%s/rsa_key.priv", confbase);
253 if((f = ask_and_safe_open(filename, _("private RSA key"), "a")) == NULL)
257 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
259 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
267 Set all files and paths according to netname
269 void make_names(void)
274 asprintf(&pidfilename, LOCALSTATEDIR "/run/tinc.%s.pid", netname);
276 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
279 _("Both netname and configuration directory given, using the latter..."));
281 asprintf(&identname, "tinc.%s", netname);
286 pidfilename = LOCALSTATEDIR "/run/tinc.pid";
288 asprintf(&confbase, "%s/tinc", CONFDIR);
295 main(int argc, char **argv, char **envp)
297 char *fake_argv[] = { argv[0], NULL };
298 program_name = argv[0];
300 setlocale (LC_ALL, "");
301 bindtextdomain (PACKAGE, LOCALEDIR);
302 textdomain (PACKAGE);
305 parse_options(argc, argv, envp);
309 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
310 printf(_("Copyright (C) 1998-2002 Ivo Timmermans, Guus Sliepen and others.\n"
311 "See the AUTHORS file for a complete list.\n\n"
312 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
313 "and you are welcome to redistribute it under certain conditions;\n"
314 "see the file COPYING for details.\n"));
322 log_add_hook(log_default);
324 gnome_init("Pokey", "0.0", 1, fake_argv);
328 xml = glade_xml_new("pokey.glade", "MainWindow");
332 fprintf(stderr, _("Something bad happened while creating the interface.\n"));
339 init_configuration(&config_tree);
341 /* Slllluuuuuuurrrrp! */
343 RAND_load_file("/dev/urandom", 1024);
345 #ifdef HAVE_SSLEAY_ADD_ALL_ALGORITHMS
346 SSLeay_add_all_algorithms();
348 OpenSSL_add_all_algorithms();
352 if(read_server_config())
359 fprintf(stderr, _("Could not setup all necessary interface elements.\n"));
363 if(!setup_network_connections())
370 _("Unrecoverable error"));