1 | /* |
1 | /* |
2 | gvpectrl.C -- the main file for gvpectrl |
2 | gvpectrl.C -- the main file for gvpectrl |
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-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 |
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> |
… | |
… | |
34 | #include <sys/stat.h> |
44 | #include <sys/stat.h> |
35 | #include <sys/types.h> |
45 | #include <sys/types.h> |
36 | #include <unistd.h> |
46 | #include <unistd.h> |
37 | #include <signal.h> |
47 | #include <signal.h> |
38 | |
48 | |
|
|
49 | #include <openssl/bn.h> |
39 | #include <openssl/rand.h> |
50 | #include <openssl/rand.h> |
40 | #include <openssl/rsa.h> |
51 | #include <openssl/rsa.h> |
41 | #include <openssl/pem.h> |
52 | #include <openssl/pem.h> |
42 | #include <openssl/evp.h> |
53 | #include <openssl/evp.h> |
43 | |
54 | |
… | |
… | |
58 | static int kill_gvpe; |
69 | static int kill_gvpe; |
59 | |
70 | |
60 | /* If nonzero, it will attempt to kill a running gvpe and exit. */ |
71 | /* If nonzero, it will attempt to kill a running gvpe and exit. */ |
61 | static int show_config; |
72 | static int show_config; |
62 | |
73 | |
|
|
74 | /* If nonzero, do not output anything but warnings/errors/very unusual conditions */ |
|
|
75 | static int quiet; |
|
|
76 | |
63 | /* If nonzero, generate public/private keypair for this net. */ |
77 | /* If nonzero, generate public/private keypair for this net. */ |
64 | static int generate_keys; |
78 | static int generate_keys; |
65 | |
79 | |
|
|
80 | // output some debugging info, interna constants &c |
|
|
81 | static int debug_info; |
|
|
82 | |
66 | static struct option const long_options[] = |
83 | static struct option const long_options[] = |
67 | { |
84 | { |
68 | {"config", required_argument, NULL, 'c'}, |
85 | {"config", required_argument, NULL, 'c'}, |
69 | {"kill", optional_argument, NULL, 'k'}, |
86 | {"kill", optional_argument, NULL, 'k'}, |
70 | {"help", no_argument, &show_help, 1}, |
87 | {"help", no_argument, &show_help, 1}, |
71 | {"version", no_argument, &show_version, 1}, |
88 | {"version", no_argument, &show_version, 1}, |
72 | {"generate-keys", no_argument, NULL, 'g'}, |
89 | {"generate-keys", no_argument, NULL, 'g'}, |
|
|
90 | {"quiet", no_argument, &quiet, 1}, |
73 | {"show-config", no_argument, &show_config, 's'}, |
91 | {"show-config", no_argument, &show_config, 's'}, |
|
|
92 | {"debug-info", no_argument, &debug_info, 1}, |
74 | {NULL, 0, NULL, 0} |
93 | {NULL, 0, NULL, 0} |
75 | }; |
94 | }; |
76 | |
95 | |
77 | static void |
96 | static void |
78 | usage (int status) |
97 | usage (int status) |
79 | { |
98 | { |
80 | if (status != 0) |
99 | if (status != 0) |
… | |
… | |
85 | printf (_ |
104 | printf (_ |
86 | (" -c, --config=DIR Read configuration options from DIR.\n" |
105 | (" -c, --config=DIR Read configuration options from DIR.\n" |
87 | " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n" |
106 | " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n" |
88 | " -g, --generate-keys Generate public/private RSA keypair.\n" |
107 | " -g, --generate-keys Generate public/private RSA keypair.\n" |
89 | " -s, --show-config Display the configuration information.\n" |
108 | " -s, --show-config Display the configuration information.\n" |
|
|
109 | " -q, --quiet Be quite quiet.\n" |
90 | " --help Display this help and exit.\n" |
110 | " --help Display this help and exit.\n" |
91 | " --version Output version information and exit.\n\n")); |
111 | " --version Output version information and exit.\n\n")); |
92 | printf (_("Report bugs to <vpe@plan9.de>.\n")); |
112 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
93 | } |
113 | } |
94 | |
114 | |
95 | exit (status); |
115 | exit (status); |
96 | } |
116 | } |
97 | |
117 | |
98 | void |
118 | static void |
99 | parse_options (int argc, char **argv, char **envp) |
119 | parse_options (int argc, char **argv, char **envp) |
100 | { |
120 | { |
101 | int r; |
121 | int r; |
102 | int option_index = 0; |
122 | int option_index = 0; |
103 | |
123 | |
104 | while ((r = |
124 | while ((r = getopt_long (argc, argv, "c:k::qgs", long_options, &option_index)) != EOF) |
105 | getopt_long (argc, argv, "c:k::gs", long_options, |
|
|
106 | &option_index)) != EOF) |
|
|
107 | { |
125 | { |
108 | switch (r) |
126 | switch (r) |
109 | { |
127 | { |
110 | case 0: /* long option */ |
128 | case 0: /* long option */ |
111 | break; |
129 | break; |
112 | |
130 | |
113 | case 'c': /* config file */ |
131 | case 'c': /* config file */ |
114 | confbase = strdup (optarg); |
132 | confbase = strdup (optarg); |
115 | break; |
133 | break; |
116 | |
134 | |
117 | case 'k': /* kill old gvpes */ |
135 | case 'k': /* kill old gvpes */ |
118 | if (optarg) |
136 | if (optarg) |
119 | { |
137 | { |
120 | if (!strcasecmp (optarg, "HUP")) |
138 | if (!strcasecmp (optarg, "HUP")) |
121 | kill_gvpe = SIGHUP; |
139 | kill_gvpe = SIGHUP; |
122 | else if (!strcasecmp (optarg, "TERM")) |
140 | else if (!strcasecmp (optarg, "TERM")) |
123 | kill_gvpe = SIGTERM; |
141 | kill_gvpe = SIGTERM; |
124 | else if (!strcasecmp (optarg, "KILL")) |
142 | else if (!strcasecmp (optarg, "KILL")) |
125 | kill_gvpe = SIGKILL; |
143 | kill_gvpe = SIGKILL; |
126 | else if (!strcasecmp (optarg, "USR1")) |
144 | else if (!strcasecmp (optarg, "USR1")) |
127 | kill_gvpe = SIGUSR1; |
145 | kill_gvpe = SIGUSR1; |
128 | else if (!strcasecmp (optarg, "USR2")) |
146 | else if (!strcasecmp (optarg, "USR2")) |
129 | kill_gvpe = SIGUSR2; |
147 | kill_gvpe = SIGUSR2; |
130 | else if (!strcasecmp (optarg, "INT")) |
148 | else if (!strcasecmp (optarg, "INT")) |
131 | kill_gvpe = SIGINT; |
149 | kill_gvpe = SIGINT; |
132 | else if (!strcasecmp (optarg, "ALRM")) |
150 | else if (!strcasecmp (optarg, "ALRM")) |
133 | kill_gvpe = SIGALRM; |
151 | kill_gvpe = SIGALRM; |
134 | else |
152 | else |
135 | { |
153 | { |
136 | kill_gvpe = atoi (optarg); |
154 | kill_gvpe = atoi (optarg); |
137 | |
155 | |
138 | if (!kill_gvpe) |
156 | if (!kill_gvpe) |
139 | { |
157 | { |
140 | fprintf (stderr, |
158 | fprintf (stderr, |
141 | _ |
159 | _ |
142 | ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), |
160 | ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), |
143 | optarg); |
161 | optarg); |
144 | usage (1); |
162 | usage (1); |
145 | } |
163 | } |
146 | } |
164 | } |
147 | } |
165 | } |
148 | else |
166 | else |
149 | kill_gvpe = SIGTERM; |
167 | kill_gvpe = SIGTERM; |
150 | |
168 | |
151 | break; |
169 | break; |
152 | |
170 | |
153 | case 'g': /* generate public/private keypair */ |
171 | case 'g': /* generate public/private keypair */ |
154 | generate_keys = RSA_KEYBITS; |
172 | generate_keys = RSABITS; |
155 | break; |
173 | break; |
156 | |
174 | |
157 | case 's': |
175 | case 's': |
158 | show_config = 1; |
176 | show_config = 1; |
159 | break; |
177 | break; |
160 | |
178 | |
|
|
179 | case 'q': |
|
|
180 | quiet = 1; |
|
|
181 | break; |
|
|
182 | |
161 | case '?': |
183 | case '?': |
162 | usage (1); |
184 | usage (1); |
163 | |
185 | |
164 | default: |
186 | default: |
165 | break; |
187 | break; |
166 | } |
188 | } |
167 | } |
189 | } |
168 | } |
190 | } |
169 | |
191 | |
170 | /* This function prettyprints the key generation process */ |
192 | // this function prettyprints the key generation process |
171 | |
193 | static int |
172 | void |
|
|
173 | indicator (int a, int b, void *p) |
194 | indicator (int a, int b, BN_GENCB *cb) |
174 | { |
195 | { |
|
|
196 | if (quiet) |
|
|
197 | return 1; |
|
|
198 | |
175 | switch (a) |
199 | switch (a) |
176 | { |
200 | { |
177 | case 0: |
201 | case 0: |
178 | fprintf (stderr, "."); |
202 | fprintf (stderr, "."); |
179 | break; |
203 | break; |
180 | |
204 | |
181 | case 1: |
205 | case 1: |
182 | fprintf (stderr, "+"); |
206 | fprintf (stderr, "+"); |
183 | break; |
207 | break; |
184 | |
208 | |
185 | case 2: |
209 | case 2: |
186 | fprintf (stderr, "-"); |
210 | fprintf (stderr, "-"); |
187 | break; |
211 | break; |
188 | |
212 | |
189 | case 3: |
213 | case 3: |
190 | switch (b) |
214 | switch (b) |
191 | { |
215 | { |
192 | case 0: |
216 | case 0: |
193 | fprintf (stderr, " p\n"); |
217 | fprintf (stderr, " p\n"); |
194 | break; |
218 | break; |
195 | |
219 | |
196 | case 1: |
220 | case 1: |
197 | fprintf (stderr, " q\n"); |
221 | fprintf (stderr, " q\n"); |
198 | break; |
222 | break; |
199 | |
223 | |
200 | default: |
224 | default: |
201 | fprintf (stderr, "?"); |
225 | fprintf (stderr, "?"); |
202 | } |
226 | } |
203 | break; |
227 | break; |
204 | |
228 | |
205 | default: |
229 | default: |
206 | fprintf (stderr, "?"); |
230 | fprintf (stderr, "?"); |
207 | } |
231 | } |
|
|
232 | |
|
|
233 | return 1; |
208 | } |
234 | } |
209 | |
235 | |
210 | /* |
236 | /* |
211 | * generate public/private RSA keypairs for all hosts that don't have one. |
237 | * generate public/private RSA keypairs for all hosts that don't have one. |
212 | */ |
238 | */ |
213 | int |
239 | static int |
214 | keygen (int bits) |
240 | keygen (int bits) |
215 | { |
241 | { |
216 | RSA *rsa_key; |
242 | FILE *f, *pubf; |
217 | FILE *f; |
|
|
218 | char *name = NULL; |
|
|
219 | char *fname; |
243 | char *fname; |
220 | |
|
|
221 | asprintf (&fname, "%s/hostkeys", confbase); |
|
|
222 | mkdir (fname, 0700); |
|
|
223 | free (fname); |
|
|
224 | |
244 | |
225 | asprintf (&fname, "%s/pubkey", confbase); |
245 | asprintf (&fname, "%s/pubkey", confbase); |
226 | mkdir (fname, 0700); |
246 | mkdir (fname, 0700); |
227 | free (fname); |
247 | free (fname); |
228 | |
248 | |
229 | for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) |
249 | for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) |
230 | { |
250 | { |
231 | conf_node *node = *i; |
251 | conf_node *node = *i; |
232 | |
252 | |
233 | asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
253 | ::thisnode = node->nodename; |
234 | |
254 | |
|
|
255 | fname = conf.config_filename (conf.prikeyfile, "hostkey"); |
|
|
256 | |
235 | f = fopen (fname, "a"); |
257 | f = fopen (fname, "ab"); |
236 | |
258 | |
237 | if (!f) |
259 | /* some libcs are buggy and require an extra seek to the end */ |
|
|
260 | if (!f || fseek (f, 0, SEEK_END)) |
238 | { |
261 | { |
239 | perror (fname); |
262 | perror (fname); |
240 | exit (EXIT_FAILURE); |
263 | exit (EXIT_FAILURE); |
241 | } |
264 | } |
242 | |
265 | |
243 | if (ftell (f)) |
266 | if (ftell (f)) |
244 | { |
267 | { |
|
|
268 | if (!quiet) |
245 | fprintf (stderr, "'%s' already exists, skipping this node\n", |
269 | fprintf (stderr, "'%s' already exists, skipping node %s\n", fname, node->nodename); |
246 | fname); |
270 | |
|
|
271 | free (fname); |
247 | fclose (f); |
272 | fclose (f); |
248 | continue; |
273 | continue; |
249 | } |
274 | } |
250 | |
275 | |
251 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, |
|
|
252 | node->nodename); |
|
|
253 | |
|
|
254 | rsa_key = RSA_generate_key (bits, 0xFFFF, indicator, NULL); |
|
|
255 | |
|
|
256 | if (!rsa_key) |
|
|
257 | { |
|
|
258 | fprintf (stderr, _("error during key generation!\n")); |
|
|
259 | return -1; |
|
|
260 | } |
|
|
261 | else |
|
|
262 | fprintf (stderr, _("Done.\n")); |
|
|
263 | |
|
|
264 | require (PEM_write_RSAPublicKey (f, rsa_key)); |
|
|
265 | fclose (f); |
|
|
266 | free (fname); |
276 | free (fname); |
267 | |
277 | |
268 | asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); |
278 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, node->nodename); |
269 | |
279 | |
|
|
280 | RSA *rsa = RSA_new (); |
|
|
281 | BIGNUM *e = BN_new (); |
|
|
282 | BN_set_bit (e, 0); BN_set_bit (e, 16); // 0x10001, 65537 |
|
|
283 | BN_GENCB cb; |
|
|
284 | BN_GENCB_set (&cb, indicator, 0); |
|
|
285 | |
|
|
286 | require (RSA_generate_key_ex (rsa, bits, e, &cb)); |
|
|
287 | |
|
|
288 | fprintf (stderr, _("Done.\n")); |
|
|
289 | |
|
|
290 | fname = conf.config_filename ("pubkey/%s", 0); |
270 | f = fopen (fname, "a"); |
291 | pubf = fopen (fname, "wb"); |
271 | if (!f) |
292 | if (!pubf) |
272 | { |
293 | { |
273 | perror (fname); |
294 | perror (fname); |
274 | exit (EXIT_FAILURE); |
295 | exit (EXIT_FAILURE); |
275 | } |
296 | } |
276 | |
297 | |
|
|
298 | free (fname); |
|
|
299 | |
|
|
300 | require (PEM_write_RSAPublicKey (pubf, rsa)); |
|
|
301 | fclose (pubf); |
|
|
302 | |
277 | require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); |
303 | require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL)); |
278 | fclose (f); |
304 | fclose (f); |
|
|
305 | |
279 | free (fname); |
306 | BN_free (e); |
|
|
307 | RSA_free (rsa); |
280 | } |
308 | } |
281 | |
309 | |
282 | return 0; |
310 | return 0; |
283 | } |
311 | } |
284 | |
312 | |
… | |
… | |
294 | |
322 | |
295 | parse_options (argc, argv, envp); |
323 | parse_options (argc, argv, envp); |
296 | |
324 | |
297 | if (show_version) |
325 | if (show_version) |
298 | { |
326 | { |
299 | printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), |
327 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
300 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
328 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
301 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
329 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
302 | printf (_ |
330 | printf (_ |
303 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
331 | ("Copyright (C) 2003-2013 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
304 | "See the AUTHORS file for a complete list.\n\n" |
332 | "See the AUTHORS file for a complete list.\n\n" |
305 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
333 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
306 | "and you are welcome to redistribute it under certain conditions;\n" |
334 | "and you are welcome to redistribute it under certain conditions;\n" |
307 | "see the file COPYING for details.\n")); |
335 | "see the file COPYING for details.\n")); |
308 | |
336 | |
… | |
… | |
310 | } |
338 | } |
311 | |
339 | |
312 | if (show_help) |
340 | if (show_help) |
313 | usage (0); |
341 | usage (0); |
314 | |
342 | |
315 | conf.read_config (false); |
343 | { |
|
|
344 | configuration_parser (conf, false, 0, 0); |
|
|
345 | } |
|
|
346 | |
|
|
347 | if (debug_info) |
|
|
348 | { |
|
|
349 | printf ("cipher_nid=%d\n", EVP_CIPHER_nid (CIPHER ())); |
|
|
350 | printf ("mac_nid=%d\n", EVP_MD_type (MAC_DIGEST ())); |
|
|
351 | printf ("auth_nid=%d\n", EVP_MD_type (AUTH_DIGEST ())); |
|
|
352 | printf ("sizeof_auth_data=%d\n", sizeof (auth_data)); |
|
|
353 | printf ("sizeof_rsa_data=%d\n", sizeof (rsa_data)); |
|
|
354 | printf ("sizeof_rsa_data_extra_auth=%d\n", sizeof (((rsa_data *)0)->extra_auth)); |
|
|
355 | printf ("raw_overhead=%d\n", VPE_OVERHEAD); |
|
|
356 | printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6); |
|
|
357 | printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6); |
|
|
358 | exit (EXIT_SUCCESS); |
|
|
359 | } |
316 | |
360 | |
317 | if (generate_keys) |
361 | if (generate_keys) |
318 | { |
362 | { |
319 | RAND_load_file ("/dev/urandom", 1024); |
363 | RAND_load_file (conf.seed_dev, SEED_SIZE); |
320 | exit (keygen (generate_keys)); |
364 | exit (keygen (generate_keys)); |
321 | } |
365 | } |
322 | |
366 | |
323 | if (kill_gvpe) |
367 | if (kill_gvpe) |
324 | exit (kill_other (kill_gvpe)); |
368 | exit (kill_other (kill_gvpe)); |
… | |
… | |
329 | exit (EXIT_SUCCESS); |
373 | exit (EXIT_SUCCESS); |
330 | } |
374 | } |
331 | |
375 | |
332 | usage (1); |
376 | usage (1); |
333 | } |
377 | } |
|
|
378 | |