2 tincd.c -- the main file for tincd
3 Copyright (C) 1998-2005 Ivo Timmermans
4 2000-2007 Guus Sliepen <guus@tinc-vpn.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.
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>
38 #include <openssl/engine.h>
55 /* The name this program was run with. */
56 char *program_name = NULL;
58 /* If nonzero, display usage information and exit. */
59 bool show_help = false;
61 /* If nonzero, print the version on standard output and exit. */
62 bool show_version = false;
64 /* If nonzero, generate public/private keypair for this host/net. */
65 int generate_keys = 0;
67 /* If nonzero, use null ciphers and skip all key exchanges. */
68 bool bypass_security = false;
70 /* If nonzero, disable swapping for this process. */
71 bool do_mlock = false;
73 /* If nonzero, write log entries to a separate file. */
74 bool use_logfile = false;
76 char *identname = NULL; /* program name for syslog */
77 char *controlsocketname = NULL; /* control socket location */
78 char *logfilename = NULL; /* log file location */
79 char **g_argv; /* a copy of the cmdline arguments */
83 static struct option const long_options[] = {
84 {"config", required_argument, NULL, 'c'},
85 {"net", required_argument, NULL, 'n'},
86 {"help", no_argument, NULL, 1},
87 {"version", no_argument, NULL, 2},
88 {"no-detach", no_argument, NULL, 'D'},
89 {"generate-keys", optional_argument, NULL, 'K'},
90 {"debug", optional_argument, NULL, 'd'},
91 {"bypass-security", no_argument, NULL, 3},
92 {"mlock", no_argument, NULL, 'L'},
93 {"logfile", optional_argument, NULL, 4},
94 {"controlsocket", required_argument, NULL, 5},
99 static struct WSAData wsa_state;
102 static void usage(bool status)
105 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
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 " -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 " --logfile[=FILENAME] Write log entries to a logfile.\n"
116 " --controlsocket=FILENAME Open control socket at FILENAME.\n"
117 " --help Display this help and exit.\n"
118 " --version Output version information and exit.\n\n"));
119 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
123 static bool parse_options(int argc, char **argv)
126 int option_index = 0;
128 while((r = getopt_long(argc, argv, "c:DLd::n:K::", long_options, &option_index)) != EOF) {
130 case 0: /* long option */
133 case 'c': /* config file */
134 confbase = xstrdup(optarg);
137 case 'D': /* no detach */
141 case 'L': /* no detach */
145 case 'd': /* inc debug level */
147 debug_level = atoi(optarg);
152 case 'n': /* net name given */
153 netname = xstrdup(optarg);
156 case 'K': /* generate public/private keypair */
158 generate_keys = atoi(optarg);
160 if(generate_keys < 512) {
161 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 */
169 generate_keys = 1024;
172 case 1: /* show help */
176 case 2: /* show version */
180 case 3: /* bypass security */
181 bypass_security = true;
184 case 4: /* write log entries to a file */
187 logfilename = xstrdup(optarg);
190 case 5: /* open control socket here */
191 controlsocketname = xstrdup(optarg);
206 /* This function prettyprints the key generation process */
208 static void indicator(int a, int b, void *p)
212 fprintf(stderr, ".");
216 fprintf(stderr, "+");
220 fprintf(stderr, "-");
226 fprintf(stderr, " p\n");
230 fprintf(stderr, " q\n");
234 fprintf(stderr, "?");
239 fprintf(stderr, "?");
244 Generate a public/private RSA keypair, and ask for a file to store
247 static bool keygen(int bits)
254 fprintf(stderr, _("Generating %d bits keys:\n"), bits);
255 rsa_key = RSA_generate_key(bits, 0x10001, indicator, NULL);
258 fprintf(stderr, _("Error during key generation!\n"));
261 fprintf(stderr, _("Done.\n"));
263 asprintf(&filename, "%s/rsa_key.priv", confbase);
264 f = ask_and_open(filename, _("private RSA key"), "a");
270 /* Make it unreadable for others. */
271 fchmod(fileno(f), 0600);
275 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
277 PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
281 get_config_string(lookup_config(config_tree, "Name"), &name);
284 asprintf(&filename, "%s/hosts/%s", confbase, name);
286 asprintf(&filename, "%s/rsa_key.pub", confbase);
288 f = ask_and_open(filename, _("public RSA key"), "a");
294 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
296 PEM_write_RSAPublicKey(f, rsa_key);
304 Set all files and paths according to netname
306 static void make_names(void)
310 char installdir[1024] = "";
311 long len = sizeof(installdir);
315 asprintf(&identname, "tinc.%s", netname);
317 identname = xstrdup("tinc");
320 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
321 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
323 asprintf(&logfilename, "%s/log/%s.log", identname);
326 asprintf(&confbase, "%s/%s", installdir, netname);
328 asprintf(&confbase, "%s", installdir);
337 if(!controlsocketname)
338 asprintf(&controlsocketname, LOCALSTATEDIR "/run/%s.control", identname);
341 asprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
345 asprintf(&confbase, CONFDIR "/tinc/%s", netname);
347 logger(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
350 asprintf(&confbase, CONFDIR "/tinc");
354 int main(int argc, char **argv)
356 program_name = argv[0];
358 setlocale(LC_ALL, "");
359 bindtextdomain(PACKAGE, LOCALEDIR);
362 if(!parse_options(argc, argv))
368 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
369 VERSION, __DATE__, __TIME__, PROT_CURRENT);
370 printf(_("Copyright (C) 1998-2007 Ivo Timmermans, Guus Sliepen and others.\n"
371 "See the AUTHORS file for a complete list.\n\n"
372 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
373 "and you are welcome to redistribute it under certain conditions;\n"
374 "see the file COPYING for details.\n"));
384 openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
387 logger(LOG_ERR, _("Error initializing libevent!"));
394 /* Lock all pages into memory if requested */
398 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
399 logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
403 logger(LOG_ERR, _("mlockall() not supported on this platform!"));
410 init_configuration(&config_tree);
412 /* Slllluuuuuuurrrrp! */
415 RAND_load_file("/dev/urandom", 1024);
417 ENGINE_load_builtin_engines();
418 ENGINE_register_all_complete();
420 OpenSSL_add_all_algorithms();
423 read_server_config();
424 return !keygen(generate_keys);
427 if(!read_server_config())
430 if(lzo_init() != LZO_E_OK) {
431 logger(LOG_ERR, _("Error initializing LZO compressor!"));
436 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
437 logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", winerror(GetLastError()));
441 if(!do_detach || !init_service())
442 return main2(argc, argv);
447 int main2(int argc, char **argv)
455 /* Setup sockets and open device. */
457 if(!setup_network_connections())
460 /* Start main loop. It only exits when tinc is killed. */
462 status = main_loop();
464 /* Shutdown properly. */
466 close_network_connections();
472 logger(LOG_NOTICE, _("Terminating"));