--- gvpe/src/gvpectrl.C 2008/08/11 16:02:16 1.8 +++ gvpe/src/gvpectrl.C 2013/07/19 18:18:28 1.17 @@ -2,7 +2,7 @@ gvpectrl.C -- the main file for gvpectrl Copyright (C) 1998-2002 Ivo Timmermans 2000-2002 Guus Sliepen - 2003-2008 Marc Lehmann + 2003-2013 Marc Lehmann This file is part of GVPE. @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -76,17 +77,21 @@ /* If nonzero, generate public/private keypair for this net. */ static int generate_keys; +// output some debugging info, interna constants &c +static int debug_info; + static struct option const long_options[] = - { - {"config", required_argument, NULL, 'c'}, - {"kill", optional_argument, NULL, 'k'}, - {"help", no_argument, &show_help, 1}, - {"version", no_argument, &show_version, 1}, - {"generate-keys", no_argument, NULL, 'g'}, - {"quiet", no_argument, &quiet, 1}, - {"show-config", no_argument, &show_config, 's'}, - {NULL, 0, NULL, 0} - }; +{ + {"config", required_argument, NULL, 'c'}, + {"kill", optional_argument, NULL, 'k'}, + {"help", no_argument, &show_help, 1}, + {"version", no_argument, &show_version, 1}, + {"generate-keys", no_argument, NULL, 'g'}, + {"quiet", no_argument, &quiet, 1}, + {"show-config", no_argument, &show_config, 's'}, + {"debug-info", no_argument, &debug_info, 1}, + {NULL, 0, NULL, 0} +}; static void usage (int status) @@ -110,7 +115,7 @@ exit (status); } -void +static void parse_options (int argc, char **argv, char **envp) { int r; @@ -120,128 +125,123 @@ { switch (r) { - case 0: /* long option */ - break; + case 0: /* long option */ + break; - case 'c': /* config file */ - confbase = strdup (optarg); - break; - - case 'k': /* kill old gvpes */ - if (optarg) - { - if (!strcasecmp (optarg, "HUP")) - kill_gvpe = SIGHUP; - else if (!strcasecmp (optarg, "TERM")) - kill_gvpe = SIGTERM; - else if (!strcasecmp (optarg, "KILL")) - kill_gvpe = SIGKILL; - else if (!strcasecmp (optarg, "USR1")) - kill_gvpe = SIGUSR1; - else if (!strcasecmp (optarg, "USR2")) - kill_gvpe = SIGUSR2; - else if (!strcasecmp (optarg, "INT")) - kill_gvpe = SIGINT; - else if (!strcasecmp (optarg, "ALRM")) - kill_gvpe = SIGALRM; - else - { - kill_gvpe = atoi (optarg); - - if (!kill_gvpe) - { - fprintf (stderr, - _ - ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), - optarg); - usage (1); - } - } - } - else - kill_gvpe = SIGTERM; - - break; - - case 'g': /* generate public/private keypair */ - generate_keys = RSA_KEYBITS; - break; - - case 's': - show_config = 1; - break; - - case 'q': - quiet = 1; - break; + case 'c': /* config file */ + confbase = strdup (optarg); + break; + + case 'k': /* kill old gvpes */ + if (optarg) + { + if (!strcasecmp (optarg, "HUP")) + kill_gvpe = SIGHUP; + else if (!strcasecmp (optarg, "TERM")) + kill_gvpe = SIGTERM; + else if (!strcasecmp (optarg, "KILL")) + kill_gvpe = SIGKILL; + else if (!strcasecmp (optarg, "USR1")) + kill_gvpe = SIGUSR1; + else if (!strcasecmp (optarg, "USR2")) + kill_gvpe = SIGUSR2; + else if (!strcasecmp (optarg, "INT")) + kill_gvpe = SIGINT; + else if (!strcasecmp (optarg, "ALRM")) + kill_gvpe = SIGALRM; + else + { + kill_gvpe = atoi (optarg); + + if (!kill_gvpe) + { + fprintf (stderr, + _ + ("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"), + optarg); + usage (1); + } + } + } + else + kill_gvpe = SIGTERM; + + break; + + case 'g': /* generate public/private keypair */ + generate_keys = RSABITS; + break; + + case 's': + show_config = 1; + break; + + case 'q': + quiet = 1; + break; - case '?': - usage (1); + case '?': + usage (1); - default: - break; + default: + break; } } } -/* This function prettyprints the key generation process */ - -void -indicator (int a, int b, void *p) +// this function prettyprints the key generation process +static int +indicator (int a, int b, BN_GENCB *cb) { if (quiet) - return; + return 1; switch (a) { - case 0: - fprintf (stderr, "."); - break; - - case 1: - fprintf (stderr, "+"); - break; - - case 2: - fprintf (stderr, "-"); - break; - - case 3: - switch (b) - { - case 0: - fprintf (stderr, " p\n"); - break; - - case 1: - fprintf (stderr, " q\n"); - break; + case 0: + fprintf (stderr, "."); + break; + + case 1: + fprintf (stderr, "+"); + break; + + case 2: + fprintf (stderr, "-"); + break; + + case 3: + switch (b) + { + case 0: + fprintf (stderr, " p\n"); + break; + + case 1: + fprintf (stderr, " q\n"); + break; + + default: + fprintf (stderr, "?"); + } + break; - default: - fprintf (stderr, "?"); - } - break; - - default: - fprintf (stderr, "?"); + default: + fprintf (stderr, "?"); } + + return 1; } /* * generate public/private RSA keypairs for all hosts that don't have one. */ -int +static int keygen (int bits) { - RSA *rsa_key; - FILE *f; - char *name = NULL; + FILE *f, *pubf; char *fname; - asprintf (&fname, "%s/hostkeys", confbase); - mkdir (fname, 0700); - free (fname); - asprintf (&fname, "%s/pubkey", confbase); mkdir (fname, 0700); free (fname); @@ -250,11 +250,14 @@ { conf_node *node = *i; - asprintf (&fname, "%s/pubkey/%s", confbase, node->nodename); + ::thisnode = node->nodename; + + fname = conf.config_filename (conf.prikeyfile, "hostkey"); - f = fopen (fname, "a"); + f = fopen (fname, "ab"); - if (!f) + /* some libcs are buggy and require an extra seek to the end */ + if (!f || fseek (f, 0, SEEK_END)) { perror (fname); exit (EXIT_FAILURE); @@ -263,42 +266,45 @@ if (ftell (f)) { if (!quiet) - fprintf (stderr, "'%s' already exists, skipping this node %d\n", - fname, quiet); + fprintf (stderr, "'%s' already exists, skipping node %s\n", fname, node->nodename); + free (fname); fclose (f); continue; } - fprintf (stderr, _("generating %d bits key for %s:\n"), bits, - node->nodename); + free (fname); - rsa_key = RSA_generate_key (bits, 0xFFFF, indicator, NULL); + fprintf (stderr, _("generating %d bits key for %s:\n"), bits, node->nodename); - if (!rsa_key) - { - fprintf (stderr, _("error during key generation!\n")); - return -1; - } - else - fprintf (stderr, _("Done.\n")); + RSA *rsa = RSA_new (); + BIGNUM *e = BN_new (); + BN_set_bit (e, 0); BN_set_bit (e, 16); // 0x10001, 65537 + BN_GENCB cb; + BN_GENCB_set (&cb, indicator, 0); - require (PEM_write_RSAPublicKey (f, rsa_key)); - fclose (f); - free (fname); + require (RSA_generate_key_ex (rsa, bits, e, &cb)); - asprintf (&fname, "%s/hostkeys/%s", confbase, node->nodename); + fprintf (stderr, _("Done.\n")); - f = fopen (fname, "a"); - if (!f) + fname = conf.config_filename ("pubkey/%s", 0); + pubf = fopen (fname, "wb"); + if (!pubf) { perror (fname); exit (EXIT_FAILURE); } - require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); - fclose (f); free (fname); + + require (PEM_write_RSAPublicKey (pubf, rsa)); + fclose (pubf); + + require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL)); + fclose (f); + + BN_free (e); + RSA_free (rsa); } return 0; @@ -322,7 +328,7 @@ VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); printf (_ - ("Copyright (C) 2003 Marc Lehmann and others.\n" + ("Copyright (C) 2003-2013 Marc Lehmann and others.\n" "See the AUTHORS file for a complete list.\n\n" "vpe comes with ABSOLUTELY NO WARRANTY. This is free software,\n" "and you are welcome to redistribute it under certain conditions;\n" @@ -338,9 +344,23 @@ configuration_parser (conf, false, 0, 0); } + if (debug_info) + { + printf ("cipher_nid=%d\n", EVP_CIPHER_nid (CIPHER ())); + printf ("mac_nid=%d\n", EVP_MD_type (MAC_DIGEST ())); + printf ("auth_nid=%d\n", EVP_MD_type (AUTH_DIGEST ())); + printf ("sizeof_auth_data=%d\n", sizeof (auth_data)); + printf ("sizeof_rsa_data=%d\n", sizeof (rsa_data)); + printf ("sizeof_rsa_data_extra_auth=%d\n", sizeof (((rsa_data *)0)->extra_auth)); + printf ("raw_overhead=%d\n", VPE_OVERHEAD); + printf ("vpn_overhead=%d\n", VPE_OVERHEAD + 6 + 6); + printf ("udp_overhead=%d\n", UDP_OVERHEAD + VPE_OVERHEAD + 6 + 6); + exit (EXIT_SUCCESS); + } + if (generate_keys) { - RAND_load_file ("/dev/urandom", 1024); + RAND_load_file (conf.seed_dev, SEED_SIZE); exit (keygen (generate_keys)); } @@ -355,3 +375,4 @@ usage (1); } +