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, use null ciphers and skip all key exchanges. */
65 bool bypass_security = false;
67 /* If nonzero, disable swapping for this process. */
68 bool do_mlock = false;
70 /* If nonzero, write log entries to a separate file. */
71 bool use_logfile = false;
73 char *identname = NULL; /* program name for syslog */
74 char *controlsocketname = NULL; /* control socket location */
75 char *logfilename = NULL; /* log file location */
76 char **g_argv; /* a copy of the cmdline arguments */
80 static struct option const long_options[] = {
81 {"config", required_argument, NULL, 'c'},
82 {"net", required_argument, NULL, 'n'},
83 {"help", no_argument, NULL, 1},
84 {"version", no_argument, NULL, 2},
85 {"no-detach", no_argument, NULL, 'D'},
86 {"debug", optional_argument, NULL, 'd'},
87 {"bypass-security", no_argument, NULL, 3},
88 {"mlock", no_argument, NULL, 'L'},
89 {"logfile", optional_argument, NULL, 4},
90 {"controlsocket", required_argument, NULL, 5},
95 static struct WSAData wsa_state;
98 static void usage(bool status)
101 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
104 printf(_("Usage: %s [option]...\n\n"), program_name);
105 printf(_( " -c, --config=DIR Read configuration options from DIR.\n"
106 " -D, --no-detach Don't fork and detach.\n"
107 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
108 " -n, --net=NETNAME Connect to net NETNAME.\n"
109 " -L, --mlock Lock tinc into main memory.\n"
110 " --logfile[=FILENAME] Write log entries to a logfile.\n"
111 " --controlsocket=FILENAME Open control socket at FILENAME.\n"
112 " --bypass-security Disables meta protocol security, for debugging.\n"
113 " --help Display this help and exit.\n"
114 " --version Output version information and exit.\n\n"));
115 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
119 static bool parse_options(int argc, char **argv)
122 int option_index = 0;
124 while((r = getopt_long(argc, argv, "c:DLd::n:", long_options, &option_index)) != EOF) {
126 case 0: /* long option */
129 case 'c': /* config file */
130 confbase = xstrdup(optarg);
133 case 'D': /* no detach */
137 case 'L': /* no detach */
141 case 'd': /* inc debug level */
143 debug_level = atoi(optarg);
148 case 'n': /* net name given */
149 netname = xstrdup(optarg);
152 case 1: /* show help */
156 case 2: /* show version */
160 case 3: /* bypass security */
161 bypass_security = true;
164 case 4: /* write log entries to a file */
167 logfilename = xstrdup(optarg);
170 case 5: /* open control socket here */
171 controlsocketname = xstrdup(optarg);
187 Set all files and paths according to netname
189 static void make_names(void)
193 char installdir[1024] = "";
194 long len = sizeof(installdir);
198 asprintf(&identname, "tinc.%s", netname);
200 identname = xstrdup("tinc");
203 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
204 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
206 asprintf(&logfilename, "%s/log/%s.log", identname);
209 asprintf(&confbase, "%s/%s", installdir, netname);
211 asprintf(&confbase, "%s", installdir);
220 if(!controlsocketname)
221 asprintf(&controlsocketname, LOCALSTATEDIR "/run/%s.control", identname);
224 asprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
228 asprintf(&confbase, CONFDIR "/tinc/%s", netname);
230 logger(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
233 asprintf(&confbase, CONFDIR "/tinc");
237 int main(int argc, char **argv)
239 program_name = argv[0];
241 setlocale(LC_ALL, "");
242 bindtextdomain(PACKAGE, LOCALEDIR);
245 if(!parse_options(argc, argv))
251 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
252 VERSION, __DATE__, __TIME__, PROT_CURRENT);
253 printf(_("Copyright (C) 1998-2007 Ivo Timmermans, Guus Sliepen and others.\n"
254 "See the AUTHORS file for a complete list.\n\n"
255 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
256 "and you are welcome to redistribute it under certain conditions;\n"
257 "see the file COPYING for details.\n"));
267 openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
270 logger(LOG_ERR, _("Error initializing libevent!"));
277 /* Lock all pages into memory if requested */
281 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
282 logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
286 logger(LOG_ERR, _("mlockall() not supported on this platform!"));
293 init_configuration(&config_tree);
295 /* Slllluuuuuuurrrrp! */
298 RAND_load_file("/dev/urandom", 1024);
300 ENGINE_load_builtin_engines();
301 ENGINE_register_all_complete();
303 OpenSSL_add_all_algorithms();
305 if(!read_server_config())
308 if(lzo_init() != LZO_E_OK) {
309 logger(LOG_ERR, _("Error initializing LZO compressor!"));
314 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
315 logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", winerror(GetLastError()));
319 if(!do_detach || !init_service())
320 return main2(argc, argv);
325 int main2(int argc, char **argv)
333 /* Setup sockets and open device. */
335 if(!setup_network_connections())
338 /* Start main loop. It only exits when tinc is killed. */
340 status = main_loop();
342 /* Shutdown properly. */
344 close_network_connections();
350 logger(LOG_NOTICE, _("Terminating"));