Finish crypto wrapping. Also provide wrappers for OpenSSL.
[tinc] / src / tincd.c
1 /*
2     tincd.c -- the main file for tincd
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2007 Guus Sliepen <guus@tinc-vpn.org>
5
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.
10
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.
15
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.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 /* Darwin (MacOS/X) needs the following definition... */
26 #ifndef _P1003_1B_VISIBLE
27 #define _P1003_1B_VISIBLE
28 #endif
29
30 #ifdef HAVE_SYS_MMAN_H
31 #include <sys/mman.h>
32 #endif
33
34 #include LZO1X_H
35
36 #include <getopt.h>
37
38 #include "conf.h"
39 #include "control.h"
40 #include "crypto.h"
41 #include "device.h"
42 #include "logger.h"
43 #include "net.h"
44 #include "netutl.h"
45 #include "process.h"
46 #include "protocol.h"
47 #include "utils.h"
48 #include "xalloc.h"
49
50 /* The name this program was run with. */
51 char *program_name = NULL;
52
53 /* If nonzero, display usage information and exit. */
54 bool show_help = false;
55
56 /* If nonzero, print the version on standard output and exit.  */
57 bool show_version = false;
58
59 /* If nonzero, use null ciphers and skip all key exchanges. */
60 bool bypass_security = false;
61
62 /* If nonzero, disable swapping for this process. */
63 bool do_mlock = false;
64
65 /* If nonzero, write log entries to a separate file. */
66 bool use_logfile = false;
67
68 char *identname = NULL;                         /* program name for syslog */
69 char *controlsocketname = NULL;                 /* control socket location */
70 char *logfilename = NULL;                       /* log file location */
71 char **g_argv;                                  /* a copy of the cmdline arguments */
72
73 static int status;
74
75 static struct option const long_options[] = {
76         {"config", required_argument, NULL, 'c'},
77         {"net", required_argument, NULL, 'n'},
78         {"help", no_argument, NULL, 1},
79         {"version", no_argument, NULL, 2},
80         {"no-detach", no_argument, NULL, 'D'},
81         {"debug", optional_argument, NULL, 'd'},
82         {"bypass-security", no_argument, NULL, 3},
83         {"mlock", no_argument, NULL, 'L'},
84         {"logfile", optional_argument, NULL, 4},
85         {"controlsocket", required_argument, NULL, 5},
86         {NULL, 0, NULL, 0}
87 };
88
89 #ifdef HAVE_MINGW
90 static struct WSAData wsa_state;
91 #endif
92
93 static void usage(bool status)
94 {
95         if(status)
96                 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
97                                 program_name);
98         else {
99                 printf(_("Usage: %s [option]...\n\n"), program_name);
100                 printf(_("  -c, --config=DIR                     Read configuration options from DIR.\n"
101                                 "  -D, --no-detach               Don't fork and detach.\n"
102                                 "  -d, --debug[=LEVEL]           Increase debug level or set it to LEVEL.\n"
103                                 "  -n, --net=NETNAME             Connect to net NETNAME.\n"
104                                 "  -L, --mlock                   Lock tinc into main memory.\n"
105                                 "      --logfile[=FILENAME]      Write log entries to a logfile.\n"
106                                 "      --controlsocket=FILENAME  Open control socket at FILENAME.\n"
107                                 "      --help                    Display this help and exit.\n"
108                                 "      --version                 Output version information and exit.\n\n"));
109                 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
110         }
111 }
112
113 static bool parse_options(int argc, char **argv)
114 {
115         int r;
116         int option_index = 0;
117
118         while((r = getopt_long(argc, argv, "c:DLd::n:", long_options, &option_index)) != EOF) {
119                 switch (r) {
120                         case 0:                         /* long option */
121                                 break;
122
123                         case 'c':                               /* config file */
124                                 confbase = xstrdup(optarg);
125                                 break;
126
127                         case 'D':                               /* no detach */
128                                 do_detach = false;
129                                 break;
130
131                         case 'L':                               /* no detach */
132                                 do_mlock = true;
133                                 break;
134
135                         case 'd':                               /* inc debug level */
136                                 if(optarg)
137                                         debug_level = atoi(optarg);
138                                 else
139                                         debug_level++;
140                                 break;
141
142                         case 'n':                               /* net name given */
143                                 netname = xstrdup(optarg);
144                                 break;
145
146                         case 1:                                 /* show help */
147                                 show_help = true;
148                                 break;
149
150                         case 2:                                 /* show version */
151                                 show_version = true;
152                                 break;
153
154                         case 3:                                 /* bypass security */
155                                 bypass_security = true;
156                                 break;
157
158                         case 4:                                 /* write log entries to a file */
159                                 use_logfile = true;
160                                 if(optarg)
161                                         logfilename = xstrdup(optarg);
162                                 break;
163
164                         case 5:                                 /* open control socket here */
165                                 controlsocketname = xstrdup(optarg);
166                                 break;
167
168                         case '?':
169                                 usage(true);
170                                 return false;
171
172                         default:
173                                 break;
174                 }
175         }
176
177         return true;
178 }
179
180 /*
181   Set all files and paths according to netname
182 */
183 static void make_names(void)
184 {
185 #ifdef HAVE_MINGW
186         HKEY key;
187         char installdir[1024] = "";
188         long len = sizeof(installdir);
189 #endif
190
191         if(netname)
192                 asprintf(&identname, "tinc.%s", netname);
193         else
194                 identname = xstrdup("tinc");
195
196 #ifdef HAVE_MINGW
197         if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
198                 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
199                         if(!logfilename)
200                                 asprintf(&logfilename, "%s/log/%s.log", identname);
201                         if(!confbase) {
202                                 if(netname)
203                                         asprintf(&confbase, "%s/%s", installdir, netname);
204                                 else
205                                         asprintf(&confbase, "%s", installdir);
206                         }
207                 }
208                 RegCloseKey(key);
209                 if(*installdir)
210                         return;
211         }
212 #endif
213
214         if(!controlsocketname)
215                 asprintf(&controlsocketname, LOCALSTATEDIR "/run/%s.control", identname);
216
217         if(!logfilename)
218                 asprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
219
220         if(netname) {
221                 if(!confbase)
222                         asprintf(&confbase, CONFDIR "/tinc/%s", netname);
223                 else
224                         logger(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
225         } else {
226                 if(!confbase)
227                         asprintf(&confbase, CONFDIR "/tinc");
228         }
229 }
230
231 int main(int argc, char **argv)
232 {
233         program_name = argv[0];
234
235         setlocale(LC_ALL, "");
236         bindtextdomain(PACKAGE, LOCALEDIR);
237         textdomain(PACKAGE);
238
239         if(!parse_options(argc, argv))
240                 return 1;
241         
242         make_names();
243
244         if(show_version) {
245                 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
246                            VERSION, __DATE__, __TIME__, PROT_CURRENT);
247                 printf(_("Copyright (C) 1998-2007 Ivo Timmermans, Guus Sliepen and others.\n"
248                                 "See the AUTHORS file for a complete list.\n\n"
249                                 "tinc comes with ABSOLUTELY NO WARRANTY.  This is free software,\n"
250                                 "and you are welcome to redistribute it under certain conditions;\n"
251                                 "see the file COPYING for details.\n"));
252
253                 return 0;
254         }
255
256         if(show_help) {
257                 usage(false);
258                 return 0;
259         }
260
261         openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
262
263         if(!event_init()) {
264                 logger(LOG_ERR, _("Error initializing libevent!"));
265                 return 1;
266         }
267
268         if(!init_control())
269                 return 1;
270
271         /* Lock all pages into memory if requested */
272
273         if(do_mlock)
274 #ifdef HAVE_MLOCKALL
275                 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
276                         logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
277                                    strerror(errno));
278 #else
279         {
280                 logger(LOG_ERR, _("mlockall() not supported on this platform!"));
281 #endif
282                 return -1;
283         }
284
285         g_argv = argv;
286
287         init_configuration(&config_tree);
288
289         /* Slllluuuuuuurrrrp! */
290
291         srand(time(NULL));
292         crypto_init();
293
294         if(!read_server_config())
295                 return 1;
296
297         if(lzo_init() != LZO_E_OK) {
298                 logger(LOG_ERR, _("Error initializing LZO compressor!"));
299                 return 1;
300         }
301
302 #ifdef HAVE_MINGW
303         if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
304                 logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", winerror(GetLastError()));
305                 return 1;
306         }
307
308         if(!do_detach || !init_service())
309                 return main2(argc, argv);
310         else
311                 return 1;
312 }
313
314 int main2(int argc, char **argv)
315 {
316 #endif
317
318         if(!detach())
319                 return 1;
320                 
321
322         /* Setup sockets and open device. */
323
324         if(!setup_network_connections())
325                 goto end;
326
327         /* Start main loop. It only exits when tinc is killed. */
328
329         status = main_loop();
330
331         /* Shutdown properly. */
332
333         close_network_connections();
334
335         ifdebug(CONNECTIONS)
336                 dump_device_stats();
337
338 end:
339         logger(LOG_NOTICE, _("Terminating"));
340
341 #ifndef HAVE_MINGW
342         exit_control();
343 #endif
344
345         crypto_exit();
346
347         return status;
348 }