1 | /* |
1 | /* |
2 | gvpe.C -- the main file for gvpe |
2 | gvpe.C -- the main file for gvpe |
3 | Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
3 | Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
4 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
4 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
5 | 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
5 | 2003-2013 Marc Lehmann <gvpe@schmorp.de> |
6 | |
6 | |
7 | This file is part of GVPE. |
7 | This file is part of GVPE. |
8 | |
8 | |
9 | GVPE is free software; you can redistribute it and/or modify it |
9 | GVPE is free software; you can redistribute it and/or modify it |
10 | under the terms of the GNU General Public License as published by the |
10 | under the terms of the GNU General Public License as published by the |
… | |
… | |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <fcntl.h> |
41 | #include <fcntl.h> |
42 | #include <getopt.h> |
42 | #include <getopt.h> |
43 | #include <signal.h> |
43 | #include <signal.h> |
44 | #include <sys/types.h> |
44 | #include <sys/types.h> |
|
|
45 | #include <sys/stat.h> |
45 | #include <unistd.h> |
46 | #include <unistd.h> |
46 | #include <signal.h> |
47 | #include <signal.h> |
47 | #include <termios.h> |
48 | #include <termios.h> |
48 | |
49 | |
49 | #if HAVE_SYS_MMAN_H |
50 | #if HAVE_SYS_MMAN_H |
… | |
… | |
75 | |
76 | |
76 | /* If zero, don't detach from the terminal. */ |
77 | /* If zero, don't detach from the terminal. */ |
77 | static int do_detach = 1; |
78 | static int do_detach = 1; |
78 | |
79 | |
79 | static struct option const long_options[] = |
80 | static struct option const long_options[] = |
80 | { |
81 | { |
81 | {"config", required_argument, NULL, 'c'}, |
82 | {"config", required_argument, NULL, 'c'}, |
82 | {"help", no_argument, &show_help, 1}, |
83 | {"help", no_argument, &show_help, 1}, |
83 | {"version", no_argument, &show_version, 1}, |
84 | {"version", no_argument, &show_version, 1}, |
84 | {"no-detach", no_argument, &do_detach, 0}, |
85 | {"no-detach", no_argument, &do_detach, 0}, |
85 | {"log-level", required_argument, NULL, 'l'}, |
86 | {"log-level", required_argument, NULL, 'l'}, |
86 | {"mlock", no_argument, &do_mlock, 1}, |
87 | {"mlock", no_argument, &do_mlock, 1}, |
87 | {NULL, 0, NULL, 0} |
88 | {NULL, 0, NULL, 0} |
88 | }; |
89 | }; |
89 | |
90 | |
90 | static void |
91 | static void |
91 | usage (int status) |
92 | usage (int status) |
92 | { |
93 | { |
93 | if (status != 0) |
94 | if (status != 0) |
… | |
… | |
97 | printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
98 | printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
98 | printf (_ |
99 | printf (_ |
99 | (" -c, --config=DIR Read configuration options from DIR.\n" |
100 | (" -c, --config=DIR Read configuration options from DIR.\n" |
100 | " -D, --no-detach Don't fork and detach.\n" |
101 | " -D, --no-detach Don't fork and detach.\n" |
101 | " -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
102 | " -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
102 | " -L, --mlock Lock tinc into main memory.\n" |
103 | " -L, --mlock Lock gvpe into main memory.\n" |
103 | " --help Display this help and exit.\n" |
104 | " --help Display this help and exit.\n" |
104 | " --version Output version information and exit.\n\n")); |
105 | " --version Output version information and exit.\n\n")); |
105 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
106 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
106 | } |
107 | } |
107 | |
108 | |
108 | exit (status); |
109 | exit (status); |
109 | } |
110 | } |
110 | |
111 | |
111 | void |
112 | static void |
112 | parse_options (int argc, char **argv, char **envp) |
113 | parse_options (int argc, char **argv, char **envp) |
113 | { |
114 | { |
114 | int r; |
115 | int r; |
115 | int option_index = 0; |
116 | int option_index = 0; |
116 | |
117 | |
… | |
… | |
149 | break; |
150 | break; |
150 | } |
151 | } |
151 | } |
152 | } |
152 | } |
153 | } |
153 | |
154 | |
154 | /* |
|
|
155 | Close network connections, and terminate neatly |
155 | // close network connections, and terminate neatly |
156 | */ |
156 | static void |
157 | void cleanup_and_exit(int c) |
157 | cleanup_and_exit (int c) |
158 | { |
158 | { |
159 | network.shutdown_all (); |
159 | network.shutdown_all (); |
160 | |
160 | |
161 | if (conf.pidfilename) |
161 | if (conf.pidfilename) |
162 | remove_pid (conf.pidfilename); |
162 | remove_pid (conf.pidfilename); |
… | |
… | |
164 | slog (L_INFO, _("terminating with exit code %d"), c); |
164 | slog (L_INFO, _("terminating with exit code %d"), c); |
165 | |
165 | |
166 | exit (c); |
166 | exit (c); |
167 | } |
167 | } |
168 | |
168 | |
169 | /* |
|
|
170 | Signal handlers. |
169 | // signal handlers |
171 | */ |
170 | static RETSIGTYPE |
172 | RETSIGTYPE |
|
|
173 | sigterm_handler (int a) |
171 | sigterm_handler (int a) |
174 | { |
172 | { |
175 | network.events |= vpn::EVENT_SHUTDOWN; |
173 | network.events |= vpn::EVENT_SHUTDOWN; |
176 | network.event.start (); |
174 | network.event.start (); |
177 | } |
175 | } |
178 | |
176 | |
179 | RETSIGTYPE |
177 | static RETSIGTYPE |
180 | sighup_handler (int a) |
178 | sighup_handler (int a) |
181 | { |
179 | { |
182 | network.events |= vpn::EVENT_RECONNECT; |
180 | network.events |= vpn::EVENT_RECONNECT; |
183 | network.event.start (); |
181 | network.event.start (); |
184 | } |
182 | } |
185 | |
183 | |
186 | RETSIGTYPE |
184 | static RETSIGTYPE |
187 | sigusr1_handler (int a) |
185 | sigusr1_handler (int a) |
188 | { |
186 | { |
189 | network.dump_status (); |
187 | network.dump_status (); |
190 | } |
188 | } |
191 | |
189 | |
192 | RETSIGTYPE |
190 | static RETSIGTYPE |
193 | sigusr2_handler (int a) |
191 | sigusr2_handler (int a) |
194 | { |
192 | { |
195 | } |
193 | } |
196 | |
194 | |
197 | void |
195 | static void |
198 | setup_signals (void) |
196 | setup_signals (void) |
199 | { |
197 | { |
200 | struct sigaction act; |
198 | struct sigaction act; |
201 | |
199 | |
202 | sigfillset (&act.sa_mask); |
200 | sigfillset (&act.sa_mask); |
203 | act.sa_flags = 0; |
201 | act.sa_flags = 0; |
204 | |
202 | |
205 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
203 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
206 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
204 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
207 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
205 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
208 | act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
|
|
209 | act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
206 | act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
210 | act.sa_flags = SA_RESETHAND; |
207 | act.sa_flags = SA_RESETHAND; |
211 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
208 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
212 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
209 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
213 | } |
210 | } |
214 | |
211 | |
|
|
212 | static int rand_fd; |
|
|
213 | |
|
|
214 | // antique C++ requires external linkage :/ |
|
|
215 | void |
|
|
216 | reseed_rng (ev::timer &w, int revents) |
|
|
217 | { |
|
|
218 | char buf [SEED_SIZE]; |
|
|
219 | int n = read (rand_fd, buf, sizeof (buf)); |
|
|
220 | |
|
|
221 | if (n > 0) |
|
|
222 | RAND_seed (buf, n); |
|
|
223 | } |
|
|
224 | |
|
|
225 | static void |
|
|
226 | setup_rng (void) |
|
|
227 | { |
|
|
228 | if (!*conf.seed_dev) |
|
|
229 | return; |
|
|
230 | |
|
|
231 | #ifndef O_BINARY |
|
|
232 | # define O_BINARY 0 |
|
|
233 | #endif |
|
|
234 | #ifndef O_NONBLOCK |
|
|
235 | # define O_NONBLOCK 0 |
|
|
236 | #endif |
|
|
237 | |
|
|
238 | rand_fd = open (conf.seed_dev, O_RDONLY | O_NONBLOCK | O_BINARY); |
|
|
239 | |
|
|
240 | if (rand_fd < 0) |
|
|
241 | { |
|
|
242 | slog (L_ERR, _("unable to open seed device '%s': %s, exiting."), conf.seed_dev, strerror (errno)); |
|
|
243 | exit (EXIT_FAILURE); |
|
|
244 | } |
|
|
245 | |
|
|
246 | static ev::timer reseed_timer; |
|
|
247 | |
|
|
248 | if (conf.reseed) |
|
|
249 | { |
|
|
250 | reseed_timer.set<reseed_rng> (); |
|
|
251 | reseed_timer.set (conf.reseed, conf.reseed); |
|
|
252 | reseed_timer.start (EV_DEFAULT); |
|
|
253 | } |
|
|
254 | |
|
|
255 | reseed_rng (reseed_timer, 0); |
|
|
256 | } |
|
|
257 | |
215 | int |
258 | int |
216 | main (int argc, char **argv, char **envp) |
259 | main (int argc, char **argv, char **envp) |
217 | { |
260 | { |
218 | ERR_load_crypto_strings (); // we have the RAM |
261 | ERR_load_crypto_strings (); // we have the RAM |
219 | |
262 | |
… | |
… | |
234 | { |
277 | { |
235 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
278 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
236 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
279 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
237 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
280 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
238 | printf (_ |
281 | printf (_ |
239 | ("Copyright (C) 2003 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
282 | ("Copyright (C) 2003-2011 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
240 | "See the AUTHORS file for a complete list.\n\n" |
283 | "See the AUTHORS file for a complete list.\n\n" |
241 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
284 | "GVPE comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
242 | "and you are welcome to redistribute it under certain conditions;\n" |
285 | "and you are welcome to redistribute it under certain conditions;\n" |
243 | "see the file COPYING for details.\n")); |
286 | "see the file COPYING for details.\n")); |
244 | |
287 | |
245 | return 0; |
288 | return 0; |
246 | } |
289 | } |
… | |
… | |
274 | configuration_parser (conf, true, argc, argv); |
317 | configuration_parser (conf, true, argc, argv); |
275 | } |
318 | } |
276 | |
319 | |
277 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
320 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
278 | |
321 | |
279 | RAND_load_file ("/dev/urandom", 1024); |
322 | setup_rng (); |
280 | |
323 | |
281 | if (!THISNODE) |
324 | if (!THISNODE) |
282 | { |
325 | { |
283 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, specify the nodename when starting gvpe."), |
326 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, specify the nodename when starting gvpe."), |
284 | thisnode ? thisnode : "<unset>"); |
327 | thisnode ? thisnode : "<unset>"); |
… | |
… | |
289 | exit (EXIT_SUCCESS); |
332 | exit (EXIT_SUCCESS); |
290 | |
333 | |
291 | setup_signals (); |
334 | setup_signals (); |
292 | |
335 | |
293 | if (!network.setup ()) |
336 | if (!network.setup ()) |
|
|
337 | if (network.drop_privileges ()) |
294 | { |
338 | { |
295 | ev_loop (EV_DEFAULT_ 0); |
339 | ev_run (EV_DEFAULT_ 0); |
296 | cleanup_and_exit (EXIT_FAILURE); |
340 | cleanup_and_exit (EXIT_FAILURE); |
297 | } |
341 | } |
298 | |
342 | |
299 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
343 | slog (L_CRIT, _("unrecoverable error while setting up network, exiting.")); |
300 | cleanup_and_exit (EXIT_FAILURE); |
344 | cleanup_and_exit (EXIT_FAILURE); |
301 | } |
345 | } |
302 | |
346 | |