--- gvpe/src/gvpectrl.C 2005/04/26 00:55:56 1.4 +++ gvpe/src/gvpectrl.C 2013/07/05 10:04:22 1.13 @@ -2,23 +2,33 @@ gvpectrl.C -- the main file for gvpectrl Copyright (C) 1998-2002 Ivo Timmermans 2000-2002 Guus Sliepen - 2003-2005 Marc Lehmann + 2003-2013 Marc Lehmann This file is part of GVPE. - GVPE is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with gvpe; if not, write to the Free Software - Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + GVPE is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, see . + + Additional permission under GNU GPL version 3 section 7 + + If you modify this Program, or any covered work, by linking or + combining it with the OpenSSL project's OpenSSL library (or a modified + version of that library), containing parts covered by the terms of the + OpenSSL or SSLeay licenses, the licensors of this Program grant you + additional permission to convey the resulting work. Corresponding + Source for a non-source form of such a combination shall include the + source code for the parts of OpenSSL used as well as that of the + covered work. */ #include "config.h" @@ -36,6 +46,7 @@ #include #include +#include #include #include #include @@ -60,19 +71,23 @@ /* If nonzero, it will attempt to kill a running gvpe and exit. */ static int show_config; +/* If nonzero, do not output anything but warnings/errors/very unusual conditions */ +static int quiet; + /* If nonzero, generate public/private keypair for this net. */ static int generate_keys; 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'}, - {"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'}, + {NULL, 0, NULL, 0} +}; static void usage (int status) @@ -87,133 +102,139 @@ " -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n" " -g, --generate-keys Generate public/private RSA keypair.\n" " -s, --show-config Display the configuration information.\n" + " -q, --quiet Be quite quiet.\n" " --help Display this help and exit.\n" " --version Output version information and exit.\n\n")); - printf (_("Report bugs to .\n")); + printf (_("Report bugs to .\n")); } exit (status); } -void +static void parse_options (int argc, char **argv, char **envp) { int r; int option_index = 0; - while ((r = - getopt_long (argc, argv, "c:k::gs", long_options, - &option_index)) != EOF) + while ((r = getopt_long (argc, argv, "c:k::qgs", long_options, &option_index)) != EOF) { 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 '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 '?': - 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 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; char *fname; @@ -234,7 +255,8 @@ f = fopen (fname, "a"); - 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); @@ -242,8 +264,10 @@ if (ftell (f)) { - fprintf (stderr, "'%s' already exists, skipping this node\n", - fname); + if (!quiet) + fprintf (stderr, "'%s' already exists, skipping this node %d\n", + fname, quiet); + fclose (f); continue; } @@ -251,17 +275,17 @@ fprintf (stderr, _("generating %d bits key for %s:\n"), bits, node->nodename); - rsa_key = RSA_generate_key (bits, 0xFFFF, indicator, NULL); + 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); - if (!rsa_key) - { - fprintf (stderr, _("error during key generation!\n")); - return -1; - } - else - fprintf (stderr, _("Done.\n")); + require (RSA_generate_key_ex (rsa, bits, e, &cb)); - require (PEM_write_RSAPublicKey (f, rsa_key)); + fprintf (stderr, _("Done.\n")); + + require (PEM_write_RSAPublicKey (f, rsa)); fclose (f); free (fname); @@ -274,9 +298,12 @@ exit (EXIT_FAILURE); } - require (PEM_write_RSAPrivateKey (f, rsa_key, NULL, NULL, 0, NULL, NULL)); + require (PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL)); fclose (f); free (fname); + + BN_free (e); + RSA_free (rsa); } return 0; @@ -296,11 +323,11 @@ if (show_version) { - printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), + printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), 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"