ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/gvpectrl.C
(Generate patch)

Comparing gvpe/src/gvpectrl.C (file contents):
Revision 1.18 by root, Wed Jun 29 22:32:48 2016 UTC vs.
Revision 1.25 by root, Thu Oct 6 03:03:09 2022 UTC

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-2013 Marc Lehmann <gvpe@schmorp.de> 5 2003-2016 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
72static int show_config; 72static int show_config;
73 73
74/* 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 */
75static int quiet; 75static int quiet;
76 76
77/* If nonzero, generate single public/private keypair. */
78static const char *generate_key;
79
77/* If nonzero, generate public/private keypair for this net. */ 80/* If nonzero, generate public/private keypair for this net. */
78static int generate_keys; 81static int generate_keys;
79 82
80// output some debugging info, interna constants &c 83// output some debugging info, interna constants &c
81static int debug_info; 84static int debug_info;
84{ 87{
85 {"config", required_argument, NULL, 'c'}, 88 {"config", required_argument, NULL, 'c'},
86 {"kill", optional_argument, NULL, 'k'}, 89 {"kill", optional_argument, NULL, 'k'},
87 {"help", no_argument, &show_help, 1}, 90 {"help", no_argument, &show_help, 1},
88 {"version", no_argument, &show_version, 1}, 91 {"version", no_argument, &show_version, 1},
92 {"generate-key", required_argument, NULL, 'g'},
89 {"generate-keys", no_argument, NULL, 'g'}, 93 {"generate-keys", no_argument, NULL, 'G'},
90 {"quiet", no_argument, &quiet, 1}, 94 {"quiet", no_argument, &quiet, 1},
91 {"show-config", no_argument, &show_config, 's'}, 95 {"show-config", no_argument, &show_config, 's'},
92 {"debug-info", no_argument, &debug_info, 1}, 96 {"debug-info", no_argument, &debug_info, 1},
93 {NULL, 0, NULL, 0} 97 {NULL, 0, NULL, 0}
94}; 98};
102 { 106 {
103 printf (_("Usage: %s [option]...\n\n"), get_identity ()); 107 printf (_("Usage: %s [option]...\n\n"), get_identity ());
104 printf (_ 108 printf (_
105 (" -c, --config=DIR Read configuration options from DIR.\n" 109 (" -c, --config=DIR Read configuration options from DIR.\n"
106 " -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"
107 " -g, --generate-keys Generate public/private RSA keypair.\n" 112 " -G, --generate-keys Generate all public/private RSA keypairs.\n"
108 " -s, --show-config Display the configuration information.\n" 113 " -s, --show-config Display the configuration information.\n"
109 " -q, --quiet Be quite quiet.\n" 114 " -q, --quiet Be quite quiet.\n"
110 " --help Display this help and exit.\n" 115 " --help Display this help and exit.\n"
111 " --version Output version information and exit.\n\n")); 116 " --version Output version information and exit.\n\n"));
112 printf (_("Report bugs to <gvpe@schmorp.de>.\n")); 117 printf (_("Report bugs to <gvpe@schmorp.de>.\n"));
119parse_options (int argc, char **argv, char **envp) 124parse_options (int argc, char **argv, char **envp)
120{ 125{
121 int r; 126 int r;
122 int option_index = 0; 127 int option_index = 0;
123 128
124 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)
125 { 130 {
126 switch (r) 131 switch (r)
127 { 132 {
128 case 0: /* long option */ 133 case 0: /* long option */
129 break; 134 break;
167 kill_gvpe = SIGTERM; 172 kill_gvpe = SIGTERM;
168 173
169 break; 174 break;
170 175
171 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 */
172 generate_keys = RSABITS; 181 generate_keys = 1;
173 break; 182 break;
174 183
175 case 's': 184 case 's':
176 show_config = 1; 185 show_config = 1;
177 break; 186 break;
235 244
236/* 245/*
237 * 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.
238 */ 247 */
239static int 248static int
240keygen (int bits) 249keygen (const char *pub, const char *priv)
241{ 250{
242 FILE *f, *pubf; 251 /* some libcs are buggy and require an extra seek to the end */
252
253 FILE *pubf = fopen (pub, "ab");
254 if (!pubf || fseek (pubf, 0, SEEK_END))
255 {
256 perror (pub);
257 exit (EXIT_FAILURE);
258 }
259
260 if (ftell (pubf))
261 {
262 fclose (pubf);
263 return 1;
264 }
265
266 FILE *privf = fopen (priv, "ab");
267 if (!privf || fseek (privf, 0, SEEK_END))
268 {
269 perror (priv);
270 exit (EXIT_FAILURE);
271 }
272
273 if (ftell (privf))
274 {
275 fclose (pubf);
276 fclose (privf);
277 return 1;
278 }
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
284#if 0
285#if OPENSSL_VERSION_NUMBER < 0x10100000
286 BN_GENCB cb_100;
287 BN_GENCB *cb = &cb_100;
288#else
289 BN_GENCB *cb = BN_GENCB_new ();
290 require (cb);
291#endif
292
293 BN_GENCB_set (cb, indicator, 0);
294 require (RSA_generate_key_ex (rsa, RSABITS, e, cb));
295#else
296 require (RSA_generate_key_ex (rsa, RSABITS, e, 0));
297#endif
298
299 require (PEM_write_RSAPublicKey (pubf, rsa));
300 require (PEM_write_RSAPrivateKey (privf, rsa, NULL, NULL, 0, NULL, NULL));
301
302 fclose (pubf);
303 fclose (privf);
304
305 BN_free (e);
306 RSA_free (rsa);
307
308 return 0;
309}
310
311static int
312keygen_all ()
313{
243 char *fname; 314 char *fname;
244 315
245 asprintf (&fname, "%s/pubkey", confbase); 316 asprintf (&fname, "%s/pubkey", confbase);
246 mkdir (fname, 0700); 317 mkdir (fname, 0700);
247 free (fname); 318 free (fname);
250 { 321 {
251 conf_node *node = *i; 322 conf_node *node = *i;
252 323
253 ::thisnode = node->nodename; 324 ::thisnode = node->nodename;
254 325
326 char *pub = conf.config_filename ("pubkey/%s", 0);
255 fname = conf.config_filename (conf.prikeyfile, "hostkey"); 327 char *priv = conf.config_filename (conf.prikeyfile, "hostkey");
256 328
257 f = fopen (fname, "ab"); 329 int status = keygen (pub, priv);
258 330
259 /* some libcs are buggy and require an extra seek to the end */ 331 if (status == 0)
260 if (!f || fseek (f, 0, SEEK_END))
261 {
262 perror (fname);
263 exit (EXIT_FAILURE);
264 }
265
266 if (ftell (f))
267 { 332 {
268 if (!quiet) 333 if (!quiet)
269 fprintf (stderr, "'%s' already exists, skipping node %s\n", fname, node->nodename); 334 fprintf (stderr, _("generated %d bits key for %s.\n"), RSABITS, node->nodename);
270
271 free (fname);
272 fclose (f);
273 continue;
274 } 335 }
336 else if (status == 1)
337 fprintf (stderr, _("'%s' keypair already exists, skipping node %s.\n"), pub, node->nodename);
275 338
276 free (fname); 339 free (priv);
277
278 fprintf (stderr, _("generating %d bits key for %s:\n"), bits, node->nodename);
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
284#if OPENSSL_VERSION_NUMBER < 0x10100000
285 BN_GENCB cb;
286 BN_GENCB_set (&cb, indicator, 0);
287#else
288 BN_GENCB *cb = BN_GENCB_new ();
289 BN_GENCB_set (cb, indicator, 0);
290#endif
291
292 require (RSA_generate_key_ex (rsa, bits, e, &cb));
293
294 fprintf (stderr, _("Done.\n"));
295
296 fname = conf.config_filename ("pubkey/%s", 0);
297 pubf = fopen (fname, "wb");
298 if (!pubf) 340 free (pub);
299 { 341 }
300 perror (fname); 342
343 return 0;
344}
345
346static int
347keygen_one (const char *pubname)
348{
349 char *privname;
350
351 asprintf (&privname, "%s.privkey", pubname);
352
353 int status = keygen (pubname, privname);
354
355 if (status == 0)
356 {
357 if (!quiet)
358 fprintf (stderr, _("generated %d bits key as %s.\n"), RSABITS, pubname);
359 }
360 else if (status == 1)
361 {
362 fprintf (stderr, _("'%s' keypair already exists, not generating key.\n"), pubname);
301 exit (EXIT_FAILURE); 363 exit (EXIT_FAILURE);
302 } 364 }
303 365
304 free (fname); 366 free (privname);
305
306 require (PEM_write_RSAPublicKey (pubf, rsa));
307 fclose (pubf);
308
309 require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL));
310 fclose (f);
311
312 BN_free (e);
313 RSA_free (rsa);
314 }
315 367
316 return 0; 368 return 0;
317} 369}
318 370
319int 371int
362 printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6); 414 printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6);
363 printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6); 415 printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6);
364 exit (EXIT_SUCCESS); 416 exit (EXIT_SUCCESS);
365 } 417 }
366 418
419 if (generate_key)
420 {
421 RAND_load_file (conf.seed_dev, SEED_SIZE);
422 exit (keygen_one (generate_key));
423 }
424
367 if (generate_keys) 425 if (generate_keys)
368 { 426 {
369 RAND_load_file (conf.seed_dev, SEED_SIZE); 427 RAND_load_file (conf.seed_dev, SEED_SIZE);
370 exit (keygen (generate_keys)); 428 exit (keygen_all ());
371 } 429 }
372 430
373 if (kill_gvpe) 431 if (kill_gvpe)
374 exit (kill_other (kill_gvpe)); 432 exit (kill_other (kill_gvpe));
375 433

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines