* util/grub-mkfont.c: Migrate to argp.

* util/grub-mklayout.c: Likewise.
	* util/grub-mkpasswd-pbkdf2.c: Likewise.
	* util/grub-mkrelpath.c: Likewise.
	* util/grub-probe.c: Likewise.
	* util/grub-script-check.c: Likewise.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2012-02-03 21:45:43 +01:00
parent 4459819a52
commit 8e1e4e3903
8 changed files with 632 additions and 596 deletions

View file

@ -29,38 +29,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#define _GNU_SOURCE 1
#include <argp.h>
#include "progname.h"
static struct option options[] =
{
{"iteration_count", required_argument, 0, 'c'},
{"buflen", required_argument, 0, 'l'},
{"saltlen", required_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
};
static struct argp_option options[] = {
{"iteration-count", 'c', N_("NUM"), 0, N_("Number of PBKDF2 iterations"), 0},
{"buflen", 'l', N_("NUM"), 0, N_("Length of generated hash"), 0},
{"salt", 's', N_("NUM"), 0, N_("Length of salt"), 0},
{ 0, 0, 0, 0, 0, 0 }
};
static void
usage (int status)
struct arguments
{
if (status)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
printf (_("\
Usage: %s [OPTIONS]\n\
\nOptions:\n\
-c number, --iteration-count=number Number of PBKDF2 iterations\n\
-l number, --buflen=number Length of generated hash\n\
-s number, --salt=number Length of salt\n\
\n\
Report bugs to <%s>.\n"), program_name, PACKAGE_BUGREPORT);
unsigned int count;
unsigned int buflen;
unsigned int saltlen;
};
exit (status);
static error_t
argp_parser (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
char *p;
switch (key)
{
case 'c':
arguments->count = strtoul (arg, NULL, 0);
break;
case 'l':
arguments->buflen = strtoul (arg, NULL, 0);
break;
case 's':
arguments->saltlen = strtoul (arg, NULL, 0);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
options, argp_parser, N_("[OPTIONS]"),
N_("Generate PBKDF2 password hash."),
NULL, NULL, NULL
};
static void
hexify (char *hex, grub_uint8_t *bin, grub_size_t n)
{
@ -85,7 +109,11 @@ hexify (char *hex, grub_uint8_t *bin, grub_size_t n)
int
main (int argc, char *argv[])
{
unsigned int count = 10000, buflen = 64, saltlen = 64;
struct arguments arguments = {
.count = 10000,
.buflen = 64,
.saltlen = 64
};
char *bufhex, *salthex;
gcry_err_code_t gcry_err;
grub_uint8_t *buf, *salt;
@ -98,60 +126,30 @@ main (int argc, char *argv[])
grub_util_init_nls ();
/* Check for options. */
while (1)
if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
{
int c = getopt_long (argc, argv, "c:l:s:hvV", options, 0);
if (c == -1)
break;
switch (c)
{
case 'c':
count = strtoul (optarg, NULL, 0);
break;
case 'l':
buflen = strtoul (optarg, NULL, 0);
break;
case 's':
saltlen = strtoul (optarg, NULL, 0);
break;
case 'h':
usage (0);
return 0;
case 'V':
printf ("%s (%s) %s\n", program_name,
PACKAGE_NAME, PACKAGE_VERSION);
return 0;
default:
usage (1);
return 1;
}
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
exit(1);
}
bufhex = malloc (buflen * 2 + 1);
bufhex = malloc (arguments.buflen * 2 + 1);
if (!bufhex)
grub_util_error (_("out of memory"));
buf = malloc (buflen);
buf = malloc (arguments.buflen);
if (!buf)
{
free (bufhex);
grub_util_error (_("out of memory"));
}
salt = malloc (saltlen);
salt = malloc (arguments.saltlen);
if (!salt)
{
free (bufhex);
free (buf);
grub_util_error (_("out of memory"));
}
salthex = malloc (saltlen * 2 + 1);
salthex = malloc (arguments.saltlen * 2 + 1);
if (!salthex)
{
free (salt);
@ -209,8 +207,8 @@ main (int argc, char *argv[])
fclose (f);
grub_util_error (_("couldn't retrieve random data for salt"));
}
rd = fread (salt, 1, saltlen, f);
if (rd != saltlen)
rd = fread (salt, 1, arguments.saltlen, f);
if (rd != arguments.saltlen)
{
fclose (f);
memset (pass1, 0, sizeof (pass1));
@ -225,34 +223,34 @@ main (int argc, char *argv[])
gcry_err = grub_crypto_pbkdf2 (GRUB_MD_SHA512,
(grub_uint8_t *) pass1, strlen (pass1),
salt, saltlen,
count, buf, buflen);
salt, arguments.saltlen,
arguments.count, buf, arguments.buflen);
memset (pass1, 0, sizeof (pass1));
if (gcry_err)
{
memset (buf, 0, buflen);
memset (bufhex, 0, 2 * buflen);
memset (buf, 0, arguments.buflen);
memset (bufhex, 0, 2 * arguments.buflen);
free (buf);
free (bufhex);
memset (salt, 0, saltlen);
memset (salthex, 0, 2 * saltlen);
memset (salt, 0, arguments.saltlen);
memset (salthex, 0, 2 * arguments.saltlen);
free (salt);
free (salthex);
grub_util_error (_("cryptographic error number %d"), gcry_err);
}
hexify (bufhex, buf, buflen);
hexify (salthex, salt, saltlen);
hexify (bufhex, buf, arguments.buflen);
hexify (salthex, salt, arguments.saltlen);
printf (_("Your PBKDF2 is grub.pbkdf2.sha512.%d.%s.%s\n"),
count, salthex, bufhex);
memset (buf, 0, buflen);
memset (bufhex, 0, 2 * buflen);
arguments.count, salthex, bufhex);
memset (buf, 0, arguments.buflen);
memset (bufhex, 0, 2 * arguments.buflen);
free (buf);
free (bufhex);
memset (salt, 0, saltlen);
memset (salthex, 0, 2 * saltlen);
memset (salt, 0, arguments.saltlen);
memset (salthex, 0, 2 * arguments.saltlen);
free (salt);
free (salthex);