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