1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
/*
gvpectrl.C -- the main file for gvpectrl
Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>
2000-2002 Guus Sliepen <guus@sliepen.eu.org>
2003-2016 Marc Lehmann <gvpe@schmorp.de>
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 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 <http://www.gnu.org/licenses/>.
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"
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
#include "pidfile.h"
#include "conf.h"
#include "slog.h"
#include "util.h"
#include "vpn.h"
/* If nonzero, display usage information and exit. */
static int show_help;
/* If nonzero, print the version on standard output and exit. */
static int show_version;
/* If nonzero, it will attempt to kill a running gvpe and exit. */
static int kill_gvpe;
/* 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 single public/private keypair. */
static const char *generate_key;
/* 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-key", required_argument, NULL, 'g'},
{"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)
{
if (status != 0)
fprintf (stderr, _("Try `%s --help\' for more information.\n"), get_identity ());
else
{
printf (_("Usage: %s [option]...\n\n"), get_identity ());
printf (_
(" -c, --config=DIR Read configuration options from DIR.\n"
" -k, --kill[=SIGNAL] Attempt to kill a running gvpe and exit.\n"
" -g, --generate-key=file Generate public/private RSA keypair.\n"
" -G, --generate-keys Generate all public/private RSA keypairs.\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 <gvpe@schmorp.de>.\n"));
}
exit (status);
}
static void
parse_options (int argc, char **argv, char **envp)
{
int r;
int option_index = 0;
while ((r = getopt_long (argc, argv, "c:k::qg:Gs", long_options, &option_index)) != EOF)
{
switch (r)
{
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_key = optarg;
break;
case 'G': /* generate public/private keypairs */
generate_keys = 1;
break;
case 's':
show_config = 1;
break;
case 'q':
quiet = 1;
break;
case '?':
usage (1);
default:
break;
}
}
}
// 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;
default:
fprintf (stderr, "?");
}
break;
default:
fprintf (stderr, "?");
}
return 1;
}
/*
* generate public/private RSA keypairs for all hosts that don't have one.
*/
static int
keygen (const char *pub, const char *priv)
{
FILE *pubf = fopen (pub, "ab");
if (!pubf || fseek (pubf, 0, SEEK_END))
{
perror (pub);
exit (EXIT_FAILURE);
}
if (ftell (pubf))
{
fclose (pubf);
return 1;
}
FILE *privf = fopen (priv, "ab");
/* some libcs are buggy and require an extra seek to the end */
if (!privf || fseek (privf, 0, SEEK_END))
{
perror (priv);
exit (EXIT_FAILURE);
}
if (ftell (privf))
{
fclose (pubf);
fclose (privf);
return 1;
}
RSA *rsa = RSA_new ();
BIGNUM *e = BN_new ();
BN_set_bit (e, 0); BN_set_bit (e, 16); // 0x10001, 65537
#if 0
#if OPENSSL_VERSION_NUMBER < 0x10100000
BN_GENCB cb_100;
BN_GENCB *cb = &cb_100;
#else
BN_GENCB *cb = BN_GENCB_new ();
require (cb);
#endif
BN_GENCB_set (cb, indicator, 0);
require (RSA_generate_key_ex (rsa, RSABITS, e, cb));
#else
require (RSA_generate_key_ex (rsa, RSABITS, e, 0));
#endif
require (PEM_write_RSAPublicKey (pubf, rsa));
require (PEM_write_RSAPrivateKey (privf, rsa, NULL, NULL, 0, NULL, NULL));
fclose (pubf);
fclose (privf);
BN_free (e);
RSA_free (rsa);
return 0;
}
static int
keygen_all ()
{
char *fname;
asprintf (&fname, "%s/pubkey", confbase);
mkdir (fname, 0700);
free (fname);
for (configuration::node_vector::iterator i = conf.nodes.begin (); i != conf.nodes.end (); ++i)
{
conf_node *node = *i;
::thisnode = node->nodename;
char *pub = conf.config_filename ("pubkey/%s", 0);
char *priv = conf.config_filename (conf.prikeyfile, "hostkey");
int status = keygen (pub, priv);
if (status == 0)
{
if (!quiet)
fprintf (stderr, _("generated %d bits key for %s.\n"), RSABITS, node->nodename);
}
else if (status == 1)
fprintf (stderr, _("'%s' keypair already exists, skipping node %s.\n"), pub, node->nodename);
free (priv);
free (pub);
}
return 0;
}
static int
keygen_one (const char *pubname)
{
char *privname;
asprintf (&privname, "%s.privkey", pubname);
int status = keygen (pubname, privname);
if (status == 0)
{
if (!quiet)
fprintf (stderr, _("generated %d bits key as %s.\n"), RSABITS, pubname);
}
else if (status == 1)
{
fprintf (stderr, _("'%s' keypair already exists, not generating key.\n"), pubname);
exit (EXIT_FAILURE);
}
free (privname);
return 0;
}
int
main (int argc, char **argv, char **envp)
{
set_identity (argv[0]);
log_to (LOGTO_STDERR);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
parse_options (argc, argv, envp);
if (show_version)
{
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-2013 Marc Lehmann <gvpe@schmorp.de> 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"
"see the file COPYING for details.\n"));
return 0;
}
if (show_help)
usage (0);
{
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_key)
{
RAND_load_file (conf.seed_dev, SEED_SIZE);
exit (keygen_one (generate_key));
}
if (generate_keys)
{
RAND_load_file (conf.seed_dev, SEED_SIZE);
exit (keygen_all ());
}
if (kill_gvpe)
exit (kill_other (kill_gvpe));
if (show_config)
{
conf.print ();
exit (EXIT_SUCCESS);
}
usage (1);
}
|