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-2011 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 |
… | |
… | |
44 | #include <sys/stat.h> |
44 | #include <sys/stat.h> |
45 | #include <sys/types.h> |
45 | #include <sys/types.h> |
46 | #include <unistd.h> |
46 | #include <unistd.h> |
47 | #include <signal.h> |
47 | #include <signal.h> |
48 | |
48 | |
|
|
49 | #include <openssl/bn.h> |
49 | #include <openssl/rand.h> |
50 | #include <openssl/rand.h> |
50 | #include <openssl/rsa.h> |
51 | #include <openssl/rsa.h> |
51 | #include <openssl/pem.h> |
52 | #include <openssl/pem.h> |
52 | #include <openssl/evp.h> |
53 | #include <openssl/evp.h> |
53 | |
54 | |
… | |
… | |
71 | static int show_config; |
72 | static int show_config; |
72 | |
73 | |
73 | /* If nonzero, do not output anything but warnings/errors/very unusual conditions */ |
74 | /* If nonzero, do not output anything but warnings/errors/very unusual conditions */ |
74 | static int quiet; |
75 | static int quiet; |
75 | |
76 | |
|
|
77 | /* If nonzero, generate single public/private keypair. */ |
|
|
78 | static const char *generate_key; |
|
|
79 | |
76 | /* If nonzero, generate public/private keypair for this net. */ |
80 | /* If nonzero, generate public/private keypair for this net. */ |
77 | static int generate_keys; |
81 | static int generate_keys; |
|
|
82 | |
|
|
83 | // output some debugging info, interna constants &c |
|
|
84 | static int debug_info; |
78 | |
85 | |
79 | static struct option const long_options[] = |
86 | static struct option const long_options[] = |
80 | { |
87 | { |
81 | {"config", required_argument, NULL, 'c'}, |
88 | {"config", required_argument, NULL, 'c'}, |
82 | {"kill", optional_argument, NULL, 'k'}, |
89 | {"kill", optional_argument, NULL, 'k'}, |
83 | {"help", no_argument, &show_help, 1}, |
90 | {"help", no_argument, &show_help, 1}, |
84 | {"version", no_argument, &show_version, 1}, |
91 | {"version", no_argument, &show_version, 1}, |
|
|
92 | {"generate-key", required_argument, NULL, 'g'}, |
85 | {"generate-keys", no_argument, NULL, 'g'}, |
93 | {"generate-keys", no_argument, NULL, 'G'}, |
86 | {"quiet", no_argument, &quiet, 1}, |
94 | {"quiet", no_argument, &quiet, 1}, |
87 | {"show-config", no_argument, &show_config, 's'}, |
95 | {"show-config", no_argument, &show_config, 's'}, |
|
|
96 | {"debug-info", no_argument, &debug_info, 1}, |
88 | {NULL, 0, NULL, 0} |
97 | {NULL, 0, NULL, 0} |
89 | }; |
98 | }; |
90 | |
99 | |
91 | static void |
100 | static void |
92 | usage (int status) |
101 | usage (int status) |
… | |
… | |
97 | { |
106 | { |
98 | printf (_("Usage: %s [option]...\n\n"), get_identity ()); |
107 | printf (_("Usage: %s [option]...\n\n"), get_identity ()); |
99 | printf (_ |
108 | printf (_ |
100 | (" -c, --config=DIR Read configuration options from DIR.\n" |
109 | (" -c, --config=DIR Read configuration options from DIR.\n" |
101 | " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n" |
110 | " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n" |
|
|
111 | " -g, --generate-key=file Generate public/private RSA keypair.\n" |
102 | " -g, --generate-keys Generate public/private RSA keypair.\n" |
112 | " -G, --generate-keys Generate all public/private RSA keypairs.\n" |
103 | " -s, --show-config Display the configuration information.\n" |
113 | " -s, --show-config Display the configuration information.\n" |
104 | " -q, --quiet Be quite quiet.\n" |
114 | " -q, --quiet Be quite quiet.\n" |
105 | " --help Display this help and exit.\n" |
115 | " --help Display this help and exit.\n" |
106 | " --version Output version information and exit.\n\n")); |
116 | " --version Output version information and exit.\n\n")); |
107 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
117 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
… | |
… | |
114 | parse_options (int argc, char **argv, char **envp) |
124 | parse_options (int argc, char **argv, char **envp) |
115 | { |
125 | { |
116 | int r; |
126 | int r; |
117 | int option_index = 0; |
127 | int option_index = 0; |
118 | |
128 | |
119 | while ((r = getopt_long (argc, argv, "c:k::qgs", long_options, &option_index)) != EOF) |
129 | while ((r = getopt_long (argc, argv, "c:k::qg:Gs", long_options, &option_index)) != EOF) |
120 | { |
130 | { |
121 | switch (r) |
131 | switch (r) |
122 | { |
132 | { |
123 | case 0: /* long option */ |
133 | case 0: /* long option */ |
124 | break; |
134 | break; |
… | |
… | |
162 | kill_gvpe = SIGTERM; |
172 | kill_gvpe = SIGTERM; |
163 | |
173 | |
164 | break; |
174 | break; |
165 | |
175 | |
166 | case 'g': /* generate public/private keypair */ |
176 | case 'g': /* generate public/private keypair */ |
|
|
177 | generate_key = optarg; |
|
|
178 | break; |
|
|
179 | |
|
|
180 | case 'G': /* generate public/private keypairs */ |
167 | generate_keys = RSA_KEYBITS; |
181 | generate_keys = 1; |
168 | break; |
182 | break; |
169 | |
183 | |
170 | case 's': |
184 | case 's': |
171 | show_config = 1; |
185 | show_config = 1; |
172 | break; |
186 | break; |
… | |
… | |
183 | } |
197 | } |
184 | } |
198 | } |
185 | } |
199 | } |
186 | |
200 | |
187 | // this function prettyprints the key generation process |
201 | // this function prettyprints the key generation process |
188 | static void |
202 | static int |
189 | indicator (int a, int b, void *p) |
203 | indicator (int a, int b, BN_GENCB *cb) |
190 | { |
204 | { |
191 | if (quiet) |
205 | if (quiet) |
192 | return; |
206 | return 1; |
193 | |
207 | |
194 | switch (a) |
208 | switch (a) |
195 | { |
209 | { |
196 | case 0: |
210 | case 0: |
197 | fprintf (stderr, "."); |
211 | fprintf (stderr, "."); |
… | |
… | |
222 | break; |
236 | break; |
223 | |
237 | |
224 | default: |
238 | default: |
225 | fprintf (stderr, "?"); |
239 | fprintf (stderr, "?"); |
226 | } |
240 | } |
|
|
241 | |
|
|
242 | return 1; |
227 | } |
243 | } |
228 | |
244 | |
229 | /* |
245 | /* |
230 | * generate public/private RSA keypairs for all hosts that don't have one. |
246 | * generate public/private RSA keypairs for all hosts that don't have one. |
231 | */ |
247 | */ |
232 | static int |
248 | static int |
233 | keygen (int bits) |
249 | keygen (const char *pub, const char *priv) |
234 | { |
250 | { |
235 | RSA *rsa_key; |
251 | |
236 | FILE *f; |
252 | FILE *pubf = fopen (pub, "ab"); |
237 | char *name = NULL; |
253 | if (!pubf || fseek (pubf, 0, SEEK_END)) |
|
|
254 | { |
|
|
255 | perror (pub); |
|
|
256 | exit (EXIT_FAILURE); |
|
|
257 | } |
|
|
258 | |
|
|
259 | if (ftell (pubf)) |
|
|
260 | { |
|
|
261 | fclose (pubf); |
|
|
262 | return 1; |
|
|
263 | } |
|
|
264 | |
|
|
265 | FILE *privf = fopen (priv, "ab"); |
|
|
266 | |
|
|
267 | /* some libcs are buggy and require an extra seek to the end */ |
|
|
268 | if (!privf || fseek (privf, 0, SEEK_END)) |
|
|
269 | { |
|
|
270 | perror (priv); |
|
|
271 | exit (EXIT_FAILURE); |
|
|
272 | } |
|
|
273 | |
|
|
274 | if (ftell (privf)) |
|
|
275 | { |
|
|
276 | fclose (pubf); |
|
|
277 | fclose (privf); |
|
|
278 | return 1; |
|
|
279 | } |
|
|
280 | |
|
|
281 | RSA *rsa = RSA_new (); |
|
|
282 | BIGNUM *e = BN_new (); |
|
|
283 | BN_set_bit (e, 0); BN_set_bit (e, 16); // 0x10001, 65537 |
|
|
284 | |
|
|
285 | #if 0 |
|
|
286 | #if OPENSSL_VERSION_NUMBER < 0x10100000 |
|
|
287 | BN_GENCB cb_100; |
|
|
288 | BN_GENCB *cb = &cb_100; |
|
|
289 | #else |
|
|
290 | BN_GENCB *cb = BN_GENCB_new (); |
|
|
291 | require (cb); |
|
|
292 | #endif |
|
|
293 | |
|
|
294 | BN_GENCB_set (cb, indicator, 0); |
|
|
295 | require (RSA_generate_key_ex (rsa, RSABITS, e, cb)); |
|
|
296 | #else |
|
|
297 | require (RSA_generate_key_ex (rsa, RSABITS, e, 0)); |
|
|
298 | #endif |
|
|
299 | |
|
|
300 | require (PEM_write_RSAPublicKey (pubf, rsa)); |
|
|
301 | require (PEM_write_RSAPrivateKey (privf, rsa, NULL, NULL, 0, NULL, NULL)); |
|
|
302 | |
|
|
303 | fclose (pubf); |
|
|
304 | fclose (privf); |
|
|
305 | |
|
|
306 | BN_free (e); |
|
|
307 | RSA_free (rsa); |
|
|
308 | |
|
|
309 | return 0; |
|
|
310 | } |
|
|
311 | |
|
|
312 | static int |
|
|
313 | keygen_all () |
|
|
314 | { |
238 | char *fname; |
315 | char *fname; |
239 | |
|
|
240 | asprintf (&fname, "%s/hostkeys", confbase); |
|
|
241 | mkdir (fname, 0700); |
|
|
242 | free (fname); |
|
|
243 | |
316 | |
244 | asprintf (&fname, "%s/pubkey", confbase); |
317 | asprintf (&fname, "%s/pubkey", confbase); |
245 | mkdir (fname, 0700); |
318 | mkdir (fname, 0700); |
246 | free (fname); |
319 | free (fname); |
247 | |
320 | |
248 | for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) |
321 | for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i) |
249 | { |
322 | { |
250 | conf_node *node = *i; |
323 | conf_node *node = *i; |
251 | |
324 | |
252 | asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); |
325 | ::thisnode = node->nodename; |
253 | |
326 | |
254 | f = fopen (fname, "a"); |
327 | char *pub = conf.config_filename ("pubkey/%s", 0); |
|
|
328 | char *priv = conf.config_filename (conf.prikeyfile, "hostkey"); |
255 | |
329 | |
256 | if (!f) |
330 | int status = keygen (pub, priv); |
257 | { |
|
|
258 | perror (fname); |
|
|
259 | exit (EXIT_FAILURE); |
|
|
260 | } |
|
|
261 | |
331 | |
262 | if (ftell (f)) |
332 | if (status == 0) |
263 | { |
333 | { |
264 | if (!quiet) |
334 | if (!quiet) |
265 | fprintf (stderr, "'%s' already exists, skipping this node %d\n", |
335 | fprintf (stderr, _("generated %d bits key for %s.\n"), RSABITS, node->nodename); |
266 | fname, quiet); |
|
|
267 | |
|
|
268 | fclose (f); |
|
|
269 | continue; |
|
|
270 | } |
336 | } |
|
|
337 | else if (status == 1) |
|
|
338 | fprintf (stderr, _("'%s' keypair already exists, skipping node %s.\n"), pub, node->nodename); |
271 | |
339 | |
272 | fprintf (stderr, _("generating %d bits key for %s:\n"), bits, |
340 | free (priv); |
273 | node->nodename); |
|
|
274 | |
|
|
275 | rsa_key = RSA_generate_key (bits, 0xFFFF, indicator, NULL); |
|
|
276 | |
|
|
277 | if (!rsa_key) |
|
|
278 | { |
|
|
279 | fprintf (stderr, _("error during key generation!\n")); |
|
|
280 | return -1; |
|
|
281 | } |
|
|
282 | else |
|
|
283 | fprintf (stderr, _("Done.\n")); |
|
|
284 | |
|
|
285 | require (PEM_write_RSAPublicKey (f, rsa_key)); |
|
|
286 | fclose (f); |
|
|
287 | free (fname); |
341 | free (pub); |
288 | |
|
|
289 | asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); |
|
|
290 | |
|
|
291 | f = fopen (fname, "a"); |
|
|
292 | if (!f) |
|
|
293 | { |
|
|
294 | perror (fname); |
|
|
295 | exit (EXIT_FAILURE); |
|
|
296 | } |
|
|
297 | |
|
|
298 | require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); |
|
|
299 | fclose (f); |
|
|
300 | free (fname); |
|
|
301 | } |
342 | } |
302 | |
343 | |
303 | return 0; |
344 | return 0; |
|
|
345 | } |
|
|
346 | |
|
|
347 | static int |
|
|
348 | keygen_one (const char *pubname) |
|
|
349 | { |
|
|
350 | char *privname; |
|
|
351 | |
|
|
352 | asprintf (&privname, "%s.key", pubname); |
|
|
353 | |
|
|
354 | int status = keygen (pubname, privname); |
|
|
355 | |
|
|
356 | if (status == 0) |
|
|
357 | { |
|
|
358 | if (!quiet) |
|
|
359 | fprintf (stderr, _("generated %d bits key as %s.\n"), RSABITS, pubname); |
|
|
360 | } |
|
|
361 | else if (status == 1) |
|
|
362 | { |
|
|
363 | fprintf (stderr, _("'%s' keypair already exists, not generating key.\n"), pubname); |
|
|
364 | exit (EXIT_FAILURE); |
|
|
365 | } |
|
|
366 | |
|
|
367 | free(privname); |
304 | } |
368 | } |
305 | |
369 | |
306 | int |
370 | int |
307 | main (int argc, char **argv, char **envp) |
371 | main (int argc, char **argv, char **envp) |
308 | { |
372 | { |
… | |
… | |
319 | { |
383 | { |
320 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
384 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
321 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
385 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
322 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
386 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
323 | printf (_ |
387 | printf (_ |
324 | ("Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
388 | ("Copyright (C) 2003-2013 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
325 | "See the AUTHORS file for a complete list.\n\n" |
389 | "See the AUTHORS file for a complete list.\n\n" |
326 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
390 | "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
327 | "and you are welcome to redistribute it under certain conditions;\n" |
391 | "and you are welcome to redistribute it under certain conditions;\n" |
328 | "see the file COPYING for details.\n")); |
392 | "see the file COPYING for details.\n")); |
329 | |
393 | |
… | |
… | |
335 | |
399 | |
336 | { |
400 | { |
337 | configuration_parser (conf, false, 0, 0); |
401 | configuration_parser (conf, false, 0, 0); |
338 | } |
402 | } |
339 | |
403 | |
|
|
404 | if (debug_info) |
|
|
405 | { |
|
|
406 | printf ("cipher_nid=%d\n", EVP_CIPHER_nid (CIPHER ())); |
|
|
407 | printf ("mac_nid=%d\n", EVP_MD_type (MAC_DIGEST ())); |
|
|
408 | printf ("auth_nid=%d\n", EVP_MD_type (AUTH_DIGEST ())); |
|
|
409 | printf ("sizeof_auth_data=%d\n", sizeof (auth_data)); |
|
|
410 | printf ("sizeof_rsa_data=%d\n", sizeof (rsa_data)); |
|
|
411 | printf ("sizeof_rsa_data_extra_auth=%d\n", sizeof (((rsa_data *)0)->extra_auth)); |
|
|
412 | printf ("raw_overhead=%d\n", VPE_OVERHEAD); |
|
|
413 | printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6); |
|
|
414 | printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6); |
|
|
415 | exit (EXIT_SUCCESS); |
|
|
416 | } |
|
|
417 | |
|
|
418 | if (generate_key) |
|
|
419 | { |
|
|
420 | RAND_load_file (conf.seed_dev, SEED_SIZE); |
|
|
421 | exit (keygen_one (generate_key)); |
|
|
422 | } |
|
|
423 | |
340 | if (generate_keys) |
424 | if (generate_keys) |
341 | { |
425 | { |
342 | RAND_load_file ("/dev/urandom", 1024); |
426 | RAND_load_file (conf.seed_dev, SEED_SIZE); |
343 | exit (keygen (generate_keys)); |
427 | exit (keygen_all ()); |
344 | } |
428 | } |
345 | |
429 | |
346 | if (kill_gvpe) |
430 | if (kill_gvpe) |
347 | exit (kill_other (kill_gvpe)); |
431 | exit (kill_other (kill_gvpe)); |
348 | |
432 | |
… | |
… | |
352 | exit (EXIT_SUCCESS); |
436 | exit (EXIT_SUCCESS); |
353 | } |
437 | } |
354 | |
438 | |
355 | usage (1); |
439 | usage (1); |
356 | } |
440 | } |
|
|
441 | |