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.11 by root, Tue Feb 15 13:31:23 2011 UTC vs.
Revision 1.15 by root, Tue Jul 16 16:44:36 2013 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-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
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 */
74static int quiet; 75static int quiet;
75 76
76/* If nonzero, generate public/private keypair for this net. */ 77/* If nonzero, generate public/private keypair for this net. */
77static int generate_keys; 78static int generate_keys;
79
80// output some debugging info, interna constants &c
81static int debug_info;
78 82
79static struct option const long_options[] = 83static struct option const long_options[] =
80{ 84{
81 {"config", required_argument, NULL, 'c'}, 85 {"config", required_argument, NULL, 'c'},
82 {"kill", optional_argument, NULL, 'k'}, 86 {"kill", optional_argument, NULL, 'k'},
83 {"help", no_argument, &show_help, 1}, 87 {"help", no_argument, &show_help, 1},
84 {"version", no_argument, &show_version, 1}, 88 {"version", no_argument, &show_version, 1},
85 {"generate-keys", no_argument, NULL, 'g'}, 89 {"generate-keys", no_argument, NULL, 'g'},
86 {"quiet", no_argument, &quiet, 1}, 90 {"quiet", no_argument, &quiet, 1},
87 {"show-config", no_argument, &show_config, 's'}, 91 {"show-config", no_argument, &show_config, 's'},
92 {"debug-info", no_argument, &debug_info, 1},
88 {NULL, 0, NULL, 0} 93 {NULL, 0, NULL, 0}
89}; 94};
90 95
91static void 96static void
92usage (int status) 97usage (int status)
162 kill_gvpe = SIGTERM; 167 kill_gvpe = SIGTERM;
163 168
164 break; 169 break;
165 170
166 case 'g': /* generate public/private keypair */ 171 case 'g': /* generate public/private keypair */
167 generate_keys = RSA_KEYBITS; 172 generate_keys = RSABITS;
168 break; 173 break;
169 174
170 case 's': 175 case 's':
171 show_config = 1; 176 show_config = 1;
172 break; 177 break;
183 } 188 }
184 } 189 }
185} 190}
186 191
187// this function prettyprints the key generation process 192// this function prettyprints the key generation process
188static void 193static int
189indicator (int a, int b, void *p) 194indicator (int a, int b, BN_GENCB *cb)
190{ 195{
191 if (quiet) 196 if (quiet)
192 return; 197 return 1;
193 198
194 switch (a) 199 switch (a)
195 { 200 {
196 case 0: 201 case 0:
197 fprintf (stderr, "."); 202 fprintf (stderr, ".");
222 break; 227 break;
223 228
224 default: 229 default:
225 fprintf (stderr, "?"); 230 fprintf (stderr, "?");
226 } 231 }
232
233 return 1;
227} 234}
228 235
229/* 236/*
230 * 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.
231 */ 238 */
232static int 239static int
233keygen (int bits) 240keygen (int bits)
234{ 241{
235 RSA *rsa_key; 242 FILE *f, *pubf;
236 FILE *f;
237 char *name = NULL;
238 char *fname; 243 char *fname;
239
240 asprintf (&fname, "%s/hostkeys", confbase);
241 mkdir (fname, 0700);
242 free (fname);
243 244
244 asprintf (&fname, "%s/pubkey", confbase); 245 asprintf (&fname, "%s/pubkey", confbase);
245 mkdir (fname, 0700); 246 mkdir (fname, 0700);
246 free (fname); 247 free (fname);
247 248
248 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)
249 { 250 {
250 conf_node *node = *i; 251 conf_node *node = *i;
251 252
252 asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); 253 ::thisnode = node->nodename;
253 254
255 fname = conf.config_filename (conf.prikeyfile, "hostkey");
256
254 f = fopen (fname, "a"); 257 f = fopen (fname, "ab");
255 258
256 if (!f) 259 /* some libcs are buggy and require an extra seek to the end */
260 if (!f || fseek (f, 0, SEEK_END))
257 { 261 {
258 perror (fname); 262 perror (fname);
259 exit (EXIT_FAILURE); 263 exit (EXIT_FAILURE);
260 } 264 }
261 265
262 if (ftell (f)) 266 if (ftell (f))
263 { 267 {
264 if (!quiet) 268 if (!quiet)
265 fprintf (stderr, "'%s' already exists, skipping this node %d\n", 269 fprintf (stderr, "'%s' already exists, skipping node %s\n", fname, node->nodename);
266 fname, quiet);
267 270
271 free (fname);
268 fclose (f); 272 fclose (f);
269 continue; 273 continue;
270 } 274 }
271 275
272 fprintf (stderr, _("generating %d bits key for %s:\n"), bits,
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); 276 free (fname);
288 277
289 asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); 278 fprintf (stderr, _("generating %d bits key for %s:\n"), bits, node->nodename);
290 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);
291 f = fopen (fname, "a"); 291 pubf = fopen (fname, "wb");
292 if (!f) 292 if (!pubf)
293 { 293 {
294 perror (fname); 294 perror (fname);
295 exit (EXIT_FAILURE); 295 exit (EXIT_FAILURE);
296 } 296 }
297 297
298 free (fname);
299
300 require (PEM_write_RSAPublicKey (pubf, rsa));
301 fclose (pubf);
302
298 require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); 303 require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL));
299 fclose (f); 304 fclose (f);
305
300 free (fname); 306 BN_free (e);
307 RSA_free (rsa);
301 } 308 }
302 309
303 return 0; 310 return 0;
304} 311}
305 312
319 { 326 {
320 printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), 327 printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (),
321 VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); 328 VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR);
322 printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); 329 printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE);
323 printf (_ 330 printf (_
324 ("Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> and others.\n" 331 ("Copyright (C) 2003-2013 Marc Lehmann <gvpe@schmorp.de> and others.\n"
325 "See the AUTHORS file for a complete list.\n\n" 332 "See the AUTHORS file for a complete list.\n\n"
326 "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" 333 "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
327 "and you are welcome to redistribute it under certain conditions;\n" 334 "and you are welcome to redistribute it under certain conditions;\n"
328 "see the file COPYING for details.\n")); 335 "see the file COPYING for details.\n"));
329 336
335 342
336 { 343 {
337 configuration_parser (conf, false, 0, 0); 344 configuration_parser (conf, false, 0, 0);
338 } 345 }
339 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_pad=%d\n", sizeof (((rsa_data *)0)->pad));
355 exit (EXIT_SUCCESS);
356 }
357
340 if (generate_keys) 358 if (generate_keys)
341 { 359 {
342 RAND_load_file ("/dev/urandom", 1024); 360 RAND_load_file (conf.seed_dev, SEED_SIZE);
343 exit (keygen (generate_keys)); 361 exit (keygen (generate_keys));
344 } 362 }
345 363
346 if (kill_gvpe) 364 if (kill_gvpe)
347 exit (kill_other (kill_gvpe)); 365 exit (kill_other (kill_gvpe));
352 exit (EXIT_SUCCESS); 370 exit (EXIT_SUCCESS);
353 } 371 }
354 372
355 usage (1); 373 usage (1);
356} 374}
375

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines