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