1 | /* |
1 | /* |
2 | vped.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-2005 Marc Lehmann <gvpe@schmorp.de> |
5 | 2003-2011 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 |
9 | GVPE is free software; you can redistribute it and/or modify it |
10 | it under the terms of the GNU General Public License as published by |
10 | under the terms of the GNU General Public License as published by the |
11 | the Free Software Foundation; either version 2 of the License, or |
11 | Free Software Foundation; either version 3 of the License, or (at your |
12 | (at your option) any later version. |
12 | option) any later version. |
13 | |
13 | |
14 | This program is distributed in the hope that it will be useful, |
14 | This program is distributed in the hope that it will be useful, but |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
17 | GNU General Public License for more details. |
17 | Public License for more details. |
18 | |
18 | |
19 | You should have received a copy of the GNU General Public License |
19 | You should have received a copy of the GNU General Public License along |
20 | along with gvpe; if not, write to the Free Software |
20 | with this program; if not, see <http://www.gnu.org/licenses/>. |
21 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | |
|
|
22 | Additional permission under GNU GPL version 3 section 7 |
|
|
23 | |
|
|
24 | If you modify this Program, or any covered work, by linking or |
|
|
25 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
26 | version of that library), containing parts covered by the terms of the |
|
|
27 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
28 | additional permission to convey the resulting work. Corresponding |
|
|
29 | Source for a non-source form of such a combination shall include the |
|
|
30 | source code for the parts of OpenSSL used as well as that of the |
|
|
31 | covered work. |
22 | */ |
32 | */ |
23 | |
33 | |
24 | #include "config.h" |
34 | #include "config.h" |
25 | |
35 | |
26 | #include <cstdio> |
36 | #include <cstdio> |
… | |
… | |
41 | #endif |
51 | #endif |
42 | |
52 | |
43 | #include <openssl/err.h> |
53 | #include <openssl/err.h> |
44 | #include <openssl/rand.h> |
54 | #include <openssl/rand.h> |
45 | |
55 | |
|
|
56 | #include "gettext.h" |
46 | #include "pidfile.h" |
57 | #include "pidfile.h" |
47 | |
58 | |
48 | #include "conf.h" |
59 | #include "conf.h" |
49 | #include "slog.h" |
60 | #include "slog.h" |
50 | #include "util.h" |
61 | #include "util.h" |
51 | #include "vpn.h" |
62 | #include "vpn.h" |
52 | #include "iom.h" |
63 | #include "ev_cpp.h" |
53 | |
64 | |
54 | static loglevel llevel = L_NONE; |
65 | static loglevel llevel = L_NONE; |
55 | |
66 | |
56 | /* If nonzero, display usage information and exit. */ |
67 | /* If nonzero, display usage information and exit. */ |
57 | static int show_help; |
68 | static int show_help; |
… | |
… | |
64 | |
75 | |
65 | /* If zero, don't detach from the terminal. */ |
76 | /* If zero, don't detach from the terminal. */ |
66 | static int do_detach = 1; |
77 | static int do_detach = 1; |
67 | |
78 | |
68 | static struct option const long_options[] = |
79 | static struct option const long_options[] = |
69 | { |
80 | { |
70 | {"config", required_argument, NULL, 'c'}, |
81 | {"config", required_argument, NULL, 'c'}, |
71 | {"help", no_argument, &show_help, 1}, |
82 | {"help", no_argument, &show_help, 1}, |
72 | {"version", no_argument, &show_version, 1}, |
83 | {"version", no_argument, &show_version, 1}, |
73 | {"no-detach", no_argument, &do_detach, 0}, |
84 | {"no-detach", no_argument, &do_detach, 0}, |
74 | {"log-level", required_argument, NULL, 'l'}, |
85 | {"log-level", required_argument, NULL, 'l'}, |
75 | {"mlock", no_argument, &do_mlock, 1}, |
86 | {"mlock", no_argument, &do_mlock, 1}, |
76 | {NULL, 0, NULL, 0} |
87 | {NULL, 0, NULL, 0} |
77 | }; |
88 | }; |
78 | |
89 | |
79 | static void |
90 | static void |
80 | usage (int status) |
91 | usage (int status) |
81 | { |
92 | { |
82 | if (status != 0) |
93 | if (status != 0) |
… | |
… | |
86 | printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
97 | printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
87 | printf (_ |
98 | printf (_ |
88 | (" -c, --config=DIR Read configuration options from DIR.\n" |
99 | (" -c, --config=DIR Read configuration options from DIR.\n" |
89 | " -D, --no-detach Don't fork and detach.\n" |
100 | " -D, --no-detach Don't fork and detach.\n" |
90 | " -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
101 | " -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
91 | " -L, --mlock Lock tinc into main memory.\n" |
102 | " -L, --mlock Lock gvpe into main memory.\n" |
92 | " --help Display this help and exit.\n" |
103 | " --help Display this help and exit.\n" |
93 | " --version Output version information and exit.\n\n")); |
104 | " --version Output version information and exit.\n\n")); |
94 | printf (_("Report bugs to <vpe@plan9.de>.\n")); |
105 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
95 | } |
106 | } |
96 | |
107 | |
97 | exit (status); |
108 | exit (status); |
98 | } |
109 | } |
99 | |
110 | |
100 | void |
111 | static void |
101 | parse_options (int argc, char **argv, char **envp) |
112 | parse_options (int argc, char **argv, char **envp) |
102 | { |
113 | { |
103 | int r; |
114 | int r; |
104 | int option_index = 0; |
115 | int option_index = 0; |
105 | |
116 | |
106 | while ((r = getopt_long (argc, argv, "-c:DLl:", long_options, &option_index)) != EOF) |
117 | while ((r = getopt_long (argc, argv, "c:DLl:", long_options, &option_index)) != EOF) |
107 | { |
118 | { |
108 | switch (r) |
119 | switch (r) |
109 | { |
120 | { |
110 | case 0: /* long option */ |
121 | case 0: /* long option */ |
111 | break; |
122 | break; |
112 | |
123 | |
113 | case 1: /* this node name */ |
|
|
114 | thisnode = strdup (optarg); |
|
|
115 | break; |
|
|
116 | |
|
|
117 | case 'c': /* config file */ |
124 | case 'c': /* config file */ |
118 | confbase = strdup (optarg); |
125 | confbase = strdup (optarg); |
119 | break; |
126 | break; |
120 | |
127 | |
121 | case 'D': /* no detach */ |
128 | case 'D': /* no detach */ |
… | |
… | |
142 | break; |
149 | break; |
143 | } |
150 | } |
144 | } |
151 | } |
145 | } |
152 | } |
146 | |
153 | |
147 | /* |
|
|
148 | Close network connections, and terminate neatly |
154 | // close network connections, and terminate neatly |
149 | */ |
155 | static void |
150 | void cleanup_and_exit(int c) |
156 | cleanup_and_exit (int c) |
151 | { |
157 | { |
152 | network.shutdown_all (); |
158 | network.shutdown_all (); |
153 | |
159 | |
154 | if (conf.pidfilename) |
160 | if (conf.pidfilename) |
155 | remove_pid (conf.pidfilename); |
161 | remove_pid (conf.pidfilename); |
… | |
… | |
157 | slog (L_INFO, _("terminating with exit code %d"), c); |
163 | slog (L_INFO, _("terminating with exit code %d"), c); |
158 | |
164 | |
159 | exit (c); |
165 | exit (c); |
160 | } |
166 | } |
161 | |
167 | |
162 | /* |
|
|
163 | Signal handlers. |
168 | // signal handlers |
164 | */ |
169 | static RETSIGTYPE |
165 | RETSIGTYPE |
|
|
166 | sigterm_handler (int a) |
170 | sigterm_handler (int a) |
167 | { |
171 | { |
168 | network.events |= vpn::EVENT_SHUTDOWN; |
172 | network.events |= vpn::EVENT_SHUTDOWN; |
169 | network.event.start (0); |
173 | network.event.start (); |
170 | } |
174 | } |
171 | |
175 | |
172 | RETSIGTYPE |
176 | static RETSIGTYPE |
173 | sighup_handler (int a) |
177 | sighup_handler (int a) |
174 | { |
178 | { |
175 | network.events |= vpn::EVENT_RECONNECT; |
179 | network.events |= vpn::EVENT_RECONNECT; |
176 | network.event.start (0); |
180 | network.event.start (); |
177 | } |
181 | } |
178 | |
182 | |
179 | RETSIGTYPE |
183 | static RETSIGTYPE |
180 | sigusr1_handler (int a) |
184 | sigusr1_handler (int a) |
181 | { |
185 | { |
182 | network.dump_status (); |
186 | network.dump_status (); |
183 | } |
187 | } |
184 | |
188 | |
185 | RETSIGTYPE |
189 | static RETSIGTYPE |
186 | sigusr2_handler (int a) |
190 | sigusr2_handler (int a) |
187 | { |
191 | { |
188 | } |
192 | } |
189 | |
193 | |
190 | void |
194 | static void |
191 | setup_signals (void) |
195 | setup_signals (void) |
192 | { |
196 | { |
193 | struct sigaction act; |
197 | struct sigaction act; |
194 | |
198 | |
195 | sigfillset (&act.sa_mask); |
199 | sigfillset (&act.sa_mask); |
196 | act.sa_flags = 0; |
200 | act.sa_flags = 0; |
197 | |
201 | |
198 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
202 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
199 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
203 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
200 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
204 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
201 | act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
|
|
202 | act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
205 | act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
203 | act.sa_flags = SA_RESETHAND; |
206 | act.sa_flags = SA_RESETHAND; |
204 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
207 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
205 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
208 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
206 | } |
209 | } |
… | |
… | |
218 | bindtextdomain (PACKAGE, LOCALEDIR); |
221 | bindtextdomain (PACKAGE, LOCALEDIR); |
219 | textdomain (PACKAGE); |
222 | textdomain (PACKAGE); |
220 | |
223 | |
221 | parse_options (argc, argv, envp); |
224 | parse_options (argc, argv, envp); |
222 | |
225 | |
|
|
226 | argc -= optind; |
|
|
227 | argv += optind; |
|
|
228 | |
223 | if (show_version) |
229 | if (show_version) |
224 | { |
230 | { |
225 | printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), |
231 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
226 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
232 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
227 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
233 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
228 | printf (_ |
234 | printf (_ |
229 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
235 | ("Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
230 | "See the AUTHORS file for a complete list.\n\n" |
236 | "See the AUTHORS file for a complete list.\n\n" |
231 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
237 | "GVPE comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
232 | "and you are welcome to redistribute it under certain conditions;\n" |
238 | "and you are welcome to redistribute it under certain conditions;\n" |
233 | "see the file COPYING for details.\n")); |
239 | "see the file COPYING for details.\n")); |
234 | |
240 | |
235 | return 0; |
241 | return 0; |
236 | } |
242 | } |
… | |
… | |
246 | if (do_mlock) |
252 | if (do_mlock) |
247 | if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
253 | if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
248 | slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
254 | slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
249 | #endif |
255 | #endif |
250 | |
256 | |
251 | conf.read_config (true); |
257 | if (argc >= 1) |
|
|
258 | { |
|
|
259 | thisnode = *argv++; |
|
|
260 | argc--; |
|
|
261 | } |
|
|
262 | |
|
|
263 | if (!ev_default_loop (0)) |
|
|
264 | { |
|
|
265 | slog (L_ERR, _("unable to initialise the event loop (bad $LIBEV_METHODS?)")); |
|
|
266 | exit (EXIT_FAILURE); |
|
|
267 | } |
|
|
268 | |
|
|
269 | { |
|
|
270 | configuration_parser (conf, true, argc, argv); |
|
|
271 | } |
252 | |
272 | |
253 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
273 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
254 | |
274 | |
255 | RAND_load_file ("/dev/urandom", 1024); |
275 | RAND_load_file ("/dev/urandom", 1024); |
256 | |
276 | |
257 | if (!THISNODE) |
277 | if (!THISNODE) |
258 | { |
278 | { |
259 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, specify the nodename when starting vped."), |
279 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, specify the nodename when starting gvpe."), |
260 | thisnode ? thisnode : "<unset>"); |
280 | thisnode ? thisnode : "<unset>"); |
261 | exit (EXIT_FAILURE); |
281 | exit (EXIT_FAILURE); |
262 | } |
282 | } |
263 | |
283 | |
264 | if (detach (do_detach)) |
284 | if (detach (do_detach)) |
265 | exit (EXIT_SUCCESS); |
285 | exit (EXIT_SUCCESS); |
266 | |
286 | |
267 | setup_signals (); |
287 | setup_signals (); |
268 | |
288 | |
269 | if (!network.setup ()) |
289 | if (!network.setup ()) |
|
|
290 | if (network.drop_privileges ()) |
270 | { |
291 | { |
271 | io_manager::loop (); |
292 | ev_run (EV_DEFAULT_ 0); |
272 | cleanup_and_exit (EXIT_FAILURE); |
293 | cleanup_and_exit (EXIT_FAILURE); |
273 | } |
294 | } |
274 | |
295 | |
275 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
296 | slog (L_CRIT, _("unrecoverable error while setting up network, exiting.")); |
276 | cleanup_and_exit (EXIT_FAILURE); |
297 | cleanup_and_exit (EXIT_FAILURE); |
277 | } |
298 | } |
278 | |
299 | |