* [PATCH 1/5] mcookie: use lib/randutils
@ 2014-03-08 21:43 Sami Kerola
2014-03-08 21:43 ` [PATCH 2/5] mcookie: allow --file option be defined multiple times Sami Kerola
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Sami Kerola @ 2014-03-08 21:43 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
The mcookie should reuse existing code, and there is definitely no need
to prefer /dev/random for this utility. See reference for explanation
about later statement.
References: http://www.2uo.de/myths-about-urandom/
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
include/randutils.h | 1 +
lib/randutils.c | 19 ++++++++++++++++
misc-utils/Makemodule.am | 1 +
misc-utils/mcookie.1 | 45 +++++++++++++++-----------------------
misc-utils/mcookie.c | 57 +++++++++---------------------------------------
5 files changed, 48 insertions(+), 75 deletions(-)
diff --git a/include/randutils.h b/include/randutils.h
index dec5e35..17e2a02 100644
--- a/include/randutils.h
+++ b/include/randutils.h
@@ -8,5 +8,6 @@
extern int random_get_fd(void);
extern void random_get_bytes(void *buf, size_t nbytes);
+extern const char *random_tell_source(void);
#endif
diff --git a/lib/randutils.c b/lib/randutils.c
index 68bb0e1..7c49fd7 100644
--- a/lib/randutils.c
+++ b/lib/randutils.c
@@ -108,6 +108,25 @@ void random_get_bytes(void *buf, size_t nbytes)
return;
}
+
+/*
+ * Tell source of randomness.
+ */
+const char *random_tell_source(void)
+{
+ const char *random_sources[] = {
+ "/dev/urandom",
+ "/dev/random",
+ "glibc pseudo-random functions"
+ };
+
+ if (!access(random_sources[0], R_OK))
+ return random_sources[0];
+ if (!access(random_sources[1], R_OK))
+ return random_sources[1];
+ return random_sources[2];
+}
+
#ifdef TEST_PROGRAM
int main(int argc __attribute__((__unused__)),
char *argv[] __attribute__((__unused__)))
diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am
index 05407de..f31e4e1 100644
--- a/misc-utils/Makemodule.am
+++ b/misc-utils/Makemodule.am
@@ -32,6 +32,7 @@ look_SOURCES = misc-utils/look.c
usrbin_exec_PROGRAMS += mcookie
dist_man_MANS += misc-utils/mcookie.1
mcookie_SOURCES = misc-utils/mcookie.c lib/md5.c
+mcookie_LDADD = $(LDADD) libcommon.la
usrbin_exec_PROGRAMS += namei
dist_man_MANS += misc-utils/namei.1
diff --git a/misc-utils/mcookie.1 b/misc-utils/mcookie.1
index fc7e030..df6137f 100644
--- a/misc-utils/mcookie.1
+++ b/misc-utils/mcookie.1
@@ -1,6 +1,6 @@
.\" mcookie.1 --
.\" Public Domain 1995 Rickard E. Faith (faith@cs.unc.edu)
-.TH MCOOKIE 1 "June 2011" "util-linux" "User Commands"
+.TH MCOOKIE 1 "March 2014" "util-linux" "User Commands"
.SH NAME
mcookie \- generate magic cookies for xauth
.SH SYNOPSIS
@@ -15,23 +15,21 @@ xauth add :0 . `mcookie`
.RE
.PP
The "random" number generated is actually the output of the MD5 message
-digest fed with various pieces of random information: the current time, the
-process id, the parent process id, and optionally the contents of an input
-file. and several bytes of information from the first of the following
-devices which is present:
-.IR /dev/random ,
+digest fed with random information from one of the sources
.IR /dev/urandom ,
-files in
-.IR /proc ,
-.IR /dev/audio .
+.IR /dev/random ,
+or
+.I "glibc pseudo-random functions"
+in this preference order.
.SH OPTIONS
.TP
\fB\-f\fR, \fB\-\-file\fR=\fIFILE\fR
-Use file as a macig cookie seed. When file is defined as `-' character
-input is read from stdin.
+Use additional file as a macig cookie random seed. When file is defined
+as '-' character input is read from stdin.
.TP
\fB\-v\fR, \fB\-\-verbose\fR
-Explain what is being done.
+Inform where randomness originated, with amount of entropy read from each
+source.
.TP
\fB\-V\fR, \fB\-\-version\fR
Display version information and exit.
@@ -39,27 +37,18 @@ Display version information and exit.
\fB\-h\fR, \fB\-\-help\fR
Display help text and exit.
.SH BUGS
-The entropy in the generated 128-bit is probably quite small (and,
-therefore, vulnerable to attack) unless a non-pseudorandom number generator
-is used (e.g.,
-.I /dev/random
-under Linux).
-.PP
-It is assumed that none of the devices opened will block.
+It is assumed that none of the randomness sources will block.
.SH FILES
-.I /dev/random
-.br
.I /dev/urandom
.br
-.I /dev/audio
-.br
-.I /proc/stat
-.br
-.I /proc/loadavg
+.I /dev/random
.SH "SEE ALSO"
.BR X (1),
.BR xauth (1),
-.BR md5sum (1)
+.BR md5sum (1),
+.BR rand (3)
.SH AVAILABILITY
The mcookie command is part of the util-linux package and is available from
-ftp://ftp.kernel.org/pub/linux/utils/util-linux/.
+.UR ftp://\:ftp.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
+Linux Kernel Archive
+.UE .
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index d7af3c2..63eeb5b 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -22,6 +22,7 @@
#include "md5.h"
#include "nls.h"
#include "closestream.h"
+#include "randutils.h"
#include <fcntl.h>
#include <getopt.h>
@@ -31,21 +32,11 @@
#include <sys/time.h>
#include <unistd.h>
-#define BUFFERSIZE 4096
-
-struct rngs {
- const char *path;
- int minlength, maxlength;
-} rngs[] = {
- {"/dev/random", 16, 16}, /* 16 bytes = 128 bits suffice */
- {"/proc/interrupts", 0, 0},
- {"/proc/slabinfo", 0, 0},
- {"/proc/stat", 0, 0},
- {"/dev/urandom", 32, 64},
+enum {
+ BUFFERSIZE = 4096,
+ RAND_BYTES = 128
};
-#define RNGS (sizeof(rngs)/sizeof(struct rngs))
-
/* The basic function to hash a file */
static off_t hash_file(struct MD5Context *ctx, int fd)
{
@@ -83,15 +74,11 @@ int main(int argc, char **argv)
size_t i;
struct MD5Context ctx;
unsigned char digest[MD5LENGTH];
- unsigned char buf[BUFFERSIZE];
+ unsigned char buf[RAND_BYTES];
int fd;
int c;
- pid_t pid;
char *file = NULL;
int verbose = 0;
- int r;
- struct timeval tv;
- struct timezone tz;
static const struct option longopts[] = {
{"file", required_argument, NULL, 'f'},
@@ -125,13 +112,6 @@ int main(int argc, char **argv)
}
MD5Init(&ctx);
- gettimeofday(&tv, &tz);
- MD5Update(&ctx, (unsigned char *) &tv, sizeof(tv));
-
- pid = getppid();
- MD5Update(&ctx, (unsigned char *) &pid, sizeof(pid));
- pid = getpid();
- MD5Update(&ctx, (unsigned char *) &pid, sizeof(pid));
if (file) {
int count = 0;
@@ -158,28 +138,11 @@ int main(int argc, char **argv)
}
}
- for (i = 0; i < RNGS; i++) {
- if ((fd = open(rngs[i].path, O_RDONLY | O_NONBLOCK)) >= 0) {
- int count = sizeof(buf);
-
- if (rngs[i].maxlength && count > rngs[i].maxlength)
- count = rngs[i].maxlength;
- r = read(fd, buf, count);
- if (r > 0)
- MD5Update(&ctx, buf, r);
- else
- r = 0;
- close(fd);
- if (verbose)
- fprintf(stderr,
- P_("Got %d byte from %s\n",
- "Got %d bytes from %s\n", r),
- r, rngs[i].path);
- if (rngs[i].minlength && r >= rngs[i].minlength)
- break;
- } else if (verbose)
- warn(_("cannot open %s"), rngs[i].path);
- }
+ random_get_bytes(&buf, RAND_BYTES);
+ MD5Update(&ctx, buf, RAND_BYTES);
+ if (verbose)
+ fprintf(stderr,
+ _("Got %d bytes from %s\n"), RAND_BYTES, random_tell_source());
MD5Final(digest, &ctx);
for (i = 0; i < MD5LENGTH; i++)
--
1.9.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/5] mcookie: allow --file option be defined multiple times
2014-03-08 21:43 [PATCH 1/5] mcookie: use lib/randutils Sami Kerola
@ 2014-03-08 21:43 ` Sami Kerola
2014-03-08 21:43 ` [PATCH 3/5] mcookie: use same variable type consistently Sami Kerola
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Sami Kerola @ 2014-03-08 21:43 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Earlier only the last option argument took effect.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/mcookie.c | 73 +++++++++++++++++++++++++++++++---------------------
1 file changed, 44 insertions(+), 29 deletions(-)
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index 63eeb5b..41f5a0c 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -23,6 +23,7 @@
#include "nls.h"
#include "closestream.h"
#include "randutils.h"
+#include "xalloc.h"
#include <fcntl.h>
#include <getopt.h>
@@ -69,15 +70,45 @@ static void __attribute__((__noreturn__)) usage(FILE * out)
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
+static void randomness_from_files(char **files, int nfiles,
+ struct MD5Context *ctx, int verbose)
+{
+ int fd, i;
+ int count = 0;
+
+ for (i = 0; i < nfiles; i++) {
+ if (files[i][0] == '-' && !files[i][1])
+ fd = STDIN_FILENO;
+ else
+ fd = open(files[i], O_RDONLY);
+
+ if (fd < 0) {
+ warn(_("cannot open %s"), files[i]);
+ } else {
+ count = hash_file(ctx, fd);
+ if (verbose)
+ fprintf(stderr,
+ P_("Got %d byte from %s\n",
+ "Got %d bytes from %s\n", count),
+ count, files[i]);
+
+ if (fd != STDIN_FILENO)
+ if (close(fd))
+ err(EXIT_FAILURE,
+ _("closing %s failed"), files[i]);
+ }
+ }
+}
+
int main(int argc, char **argv)
{
size_t i;
struct MD5Context ctx;
unsigned char digest[MD5LENGTH];
unsigned char buf[RAND_BYTES];
- int fd;
+ char **files = NULL;
+ int nfiles;
int c;
- char *file = NULL;
int verbose = 0;
static const struct option longopts[] = {
@@ -93,6 +124,13 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
+ MD5Init(&ctx);
+
+ if (2 < argc) {
+ files = xmalloc(sizeof(char *) * argc);
+ nfiles = 0;
+ }
+
while ((c =
getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
switch (c) {
@@ -100,7 +138,8 @@ int main(int argc, char **argv)
verbose = 1;
break;
case 'f':
- file = optarg;
+ files[nfiles] = optarg;
+ nfiles++;
break;
case 'V':
printf(UTIL_LINUX_VERSION);
@@ -111,32 +150,8 @@ int main(int argc, char **argv)
usage(stderr);
}
- MD5Init(&ctx);
-
- if (file) {
- int count = 0;
-
- if (file[0] == '-' && !file[1])
- fd = STDIN_FILENO;
- else
- fd = open(file, O_RDONLY);
-
- if (fd < 0) {
- warn(_("cannot open %s"), file);
- } else {
- count = hash_file(&ctx, fd);
- if (verbose)
- fprintf(stderr,
- P_("Got %d byte from %s\n",
- "Got %d bytes from %s\n", count),
- count, file);
-
- if (fd != STDIN_FILENO)
- if (close(fd))
- err(EXIT_FAILURE,
- _("closing %s failed"), file);
- }
- }
+ randomness_from_files(files, nfiles, &ctx, verbose);
+ free(files);
random_get_bytes(&buf, RAND_BYTES);
MD5Update(&ctx, buf, RAND_BYTES);
--
1.9.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/5] mcookie: use same variable type consistently
2014-03-08 21:43 [PATCH 1/5] mcookie: use lib/randutils Sami Kerola
2014-03-08 21:43 ` [PATCH 2/5] mcookie: allow --file option be defined multiple times Sami Kerola
@ 2014-03-08 21:43 ` Sami Kerola
2014-03-08 21:43 ` [PATCH 4/5] mcookie: use control structure, and fix usage() Sami Kerola
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Sami Kerola @ 2014-03-08 21:43 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/mcookie.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index 41f5a0c..9167440 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -39,9 +39,9 @@ enum {
};
/* The basic function to hash a file */
-static off_t hash_file(struct MD5Context *ctx, int fd)
+static size_t hash_file(struct MD5Context *ctx, int fd)
{
- off_t count = 0;
+ size_t count = 0;
ssize_t r;
unsigned char buf[BUFFERSIZE];
@@ -74,7 +74,7 @@ static void randomness_from_files(char **files, int nfiles,
struct MD5Context *ctx, int verbose)
{
int fd, i;
- int count = 0;
+ size_t count = 0;
for (i = 0; i < nfiles; i++) {
if (files[i][0] == '-' && !files[i][1])
@@ -88,8 +88,8 @@ static void randomness_from_files(char **files, int nfiles,
count = hash_file(ctx, fd);
if (verbose)
fprintf(stderr,
- P_("Got %d byte from %s\n",
- "Got %d bytes from %s\n", count),
+ P_("Got %zu byte from %s\n",
+ "Got %zu bytes from %s\n", count),
count, files[i]);
if (fd != STDIN_FILENO)
--
1.9.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 4/5] mcookie: use control structure, and fix usage()
2014-03-08 21:43 [PATCH 1/5] mcookie: use lib/randutils Sami Kerola
2014-03-08 21:43 ` [PATCH 2/5] mcookie: allow --file option be defined multiple times Sami Kerola
2014-03-08 21:43 ` [PATCH 3/5] mcookie: use same variable type consistently Sami Kerola
@ 2014-03-08 21:43 ` Sami Kerola
2014-03-08 21:43 ` [PATCH 5/5] mcookie: add --max-size option Sami Kerola
2014-03-26 11:12 ` [PATCH 1/5] mcookie: use lib/randutils Karel Zak
4 siblings, 0 replies; 6+ messages in thread
From: Sami Kerola @ 2014-03-08 21:43 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/mcookie.c | 95 +++++++++++++++++++++++++++-------------------------
1 file changed, 49 insertions(+), 46 deletions(-)
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index 9167440..31f33e4 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -38,78 +38,82 @@ enum {
RAND_BYTES = 128
};
+struct mcookie_control {
+ struct MD5Context ctx;
+ char **files;
+ int nfiles;
+ int fd;
+ unsigned int
+ verbose:1;
+};
+
/* The basic function to hash a file */
-static size_t hash_file(struct MD5Context *ctx, int fd)
+static size_t hash_file(struct mcookie_control *ctl)
{
size_t count = 0;
ssize_t r;
unsigned char buf[BUFFERSIZE];
- while ((r = read(fd, buf, sizeof(buf))) > 0) {
- MD5Update(ctx, buf, r);
+ while ((r = read(ctl->fd, buf, sizeof(buf))) > 0) {
+ MD5Update(&(ctl->ctx), buf, r);
count += r;
}
/* Separate files with a null byte */
buf[0] = '\0';
- MD5Update(ctx, buf, 1);
+ MD5Update(&(ctl->ctx), buf, 1);
return count;
}
static void __attribute__((__noreturn__)) usage(FILE * out)
{
- fputs(_("\nUsage:\n"), out);
- fprintf(out,
- _(" %s [options]\n"), program_invocation_short_name);
-
- fputs(_("\nOptions:\n"), out);
- fputs(_(" -f, --file <file> use file as a cookie seed\n"
- " -v, --verbose explain what is being done\n"
- " -V, --version output version information and exit\n"
- " -h, --help display this help and exit\n\n"), out);
-
+ fputs(USAGE_HEADER, out);
+ fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
+ fputs(USAGE_OPTIONS, out);
+ fputs(_(" -f, --file <file> use file as a cookie seed\n"), out);
+ fputs(_(" -v, --verbose explain what is being done\n"), out);
+ fputs(USAGE_SEPARATOR, out);
+ fputs(USAGE_HELP, out);
+ fputs(USAGE_VERSION, out);
+ fprintf(out, USAGE_MAN_TAIL("mcookie(1)"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
-static void randomness_from_files(char **files, int nfiles,
- struct MD5Context *ctx, int verbose)
+static void randomness_from_files(struct mcookie_control *ctl)
{
- int fd, i;
+ int i;
size_t count = 0;
- for (i = 0; i < nfiles; i++) {
- if (files[i][0] == '-' && !files[i][1])
- fd = STDIN_FILENO;
+ for (i = 0; i < ctl->nfiles; i++) {
+ if (ctl->files[i][0] == '-' && !ctl->files[i][1])
+ ctl->fd = STDIN_FILENO;
else
- fd = open(files[i], O_RDONLY);
+ ctl->fd = open(ctl->files[i], O_RDONLY);
- if (fd < 0) {
- warn(_("cannot open %s"), files[i]);
+ if (ctl->fd < 0) {
+ warn(_("cannot open %s"), ctl->files[i]);
} else {
- count = hash_file(ctx, fd);
- if (verbose)
+ count = hash_file(ctl);
+ if (ctl->verbose)
fprintf(stderr,
P_("Got %zu byte from %s\n",
"Got %zu bytes from %s\n", count),
- count, files[i]);
+ count, ctl->files[i]);
- if (fd != STDIN_FILENO)
- if (close(fd))
+ if (ctl->fd != STDIN_FILENO)
+ if (close(ctl->fd))
err(EXIT_FAILURE,
- _("closing %s failed"), files[i]);
+ _("closing %s failed"), ctl->files[i]);
}
}
}
int main(int argc, char **argv)
{
+ struct mcookie_control ctl;
size_t i;
- struct MD5Context ctx;
unsigned char digest[MD5LENGTH];
unsigned char buf[RAND_BYTES];
- char **files = NULL;
- int nfiles;
int c;
- int verbose = 0;
static const struct option longopts[] = {
{"file", required_argument, NULL, 'f'},
@@ -124,22 +128,21 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
- MD5Init(&ctx);
+ memset(&ctl, 0, sizeof(ctl));
+ MD5Init(&(ctl.ctx));
- if (2 < argc) {
- files = xmalloc(sizeof(char *) * argc);
- nfiles = 0;
- }
+ if (2 < argc)
+ ctl.files = xmalloc(sizeof(char *) * argc);
while ((c =
getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
switch (c) {
case 'v':
- verbose = 1;
+ ctl.verbose = 1;
break;
case 'f':
- files[nfiles] = optarg;
- nfiles++;
+ ctl.files[ctl.nfiles] = optarg;
+ ctl.nfiles++;
break;
case 'V':
printf(UTIL_LINUX_VERSION);
@@ -150,16 +153,16 @@ int main(int argc, char **argv)
usage(stderr);
}
- randomness_from_files(files, nfiles, &ctx, verbose);
- free(files);
+ randomness_from_files(&ctl);
+ free(ctl.files);
random_get_bytes(&buf, RAND_BYTES);
- MD5Update(&ctx, buf, RAND_BYTES);
- if (verbose)
+ MD5Update(&(ctl.ctx), buf, RAND_BYTES);
+ if (ctl.verbose)
fprintf(stderr,
_("Got %d bytes from %s\n"), RAND_BYTES, random_tell_source());
- MD5Final(digest, &ctx);
+ MD5Final(digest, &(ctl.ctx));
for (i = 0; i < MD5LENGTH; i++)
printf("%02x", digest[i]);
putchar('\n');
--
1.9.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 5/5] mcookie: add --max-size option
2014-03-08 21:43 [PATCH 1/5] mcookie: use lib/randutils Sami Kerola
` (2 preceding siblings ...)
2014-03-08 21:43 ` [PATCH 4/5] mcookie: use control structure, and fix usage() Sami Kerola
@ 2014-03-08 21:43 ` Sami Kerola
2014-03-26 11:12 ` [PATCH 1/5] mcookie: use lib/randutils Karel Zak
4 siblings, 0 replies; 6+ messages in thread
From: Sami Kerola @ 2014-03-08 21:43 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Just in case someone wants to add entropy from device with invocation
demonstrated below.
$ mcookie --file /dev/urandom --max-size 64k
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/mcookie.1 | 15 +++++++++++++++
misc-utils/mcookie.c | 29 ++++++++++++++++++++++++-----
2 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/misc-utils/mcookie.1 b/misc-utils/mcookie.1
index df6137f..1b6ac76 100644
--- a/misc-utils/mcookie.1
+++ b/misc-utils/mcookie.1
@@ -27,6 +27,21 @@ in this preference order.
Use additional file as a macig cookie random seed. When file is defined
as '-' character input is read from stdin.
.TP
+\fB\-m\fR, \fB\-\-max\-size\fR=\fInumber\fR
+Read form
+.I FILE
+only
+.I number
+of bytes. This option is meant to be used when reading additional
+randomness from a device.
+.IP
+The
+.I number
+argument may be followed by the multiplicative suffixes KiB=1024,
+MiB=1024*1024, and so on for GiB, TiB, PiB, EiB, ZiB and YiB (the "iB" is
+optional, e.g., "K" has the same meaning as "KiB") or the suffixes
+KB=1000, MB=1000*1000, and so on for GB, TB, PB, EB, ZB and YB.
+.TP
\fB\-v\fR, \fB\-\-verbose\fR
Inform where randomness originated, with amount of entropy read from each
source.
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index 31f33e4..caa1b8f 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -23,6 +23,7 @@
#include "nls.h"
#include "closestream.h"
#include "randutils.h"
+#include "strutils.h"
#include "xalloc.h"
#include <fcntl.h>
@@ -43,6 +44,7 @@ struct mcookie_control {
char **files;
int nfiles;
int fd;
+ uint64_t maxsz;
unsigned int
verbose:1;
};
@@ -50,11 +52,22 @@ struct mcookie_control {
/* The basic function to hash a file */
static size_t hash_file(struct mcookie_control *ctl)
{
- size_t count = 0;
- ssize_t r;
+ size_t count = 0, read_sz;
+ ssize_t r = 1;
unsigned char buf[BUFFERSIZE];
-
- while ((r = read(ctl->fd, buf, sizeof(buf))) > 0) {
+ int stop = 0;
+
+ read_sz = sizeof(buf);
+ while (!stop) {
+ if (ctl->maxsz) {
+ if (ctl->maxsz <= read_sz) {
+ read_sz = ctl->maxsz;
+ stop = 1;
+ } else
+ ctl->maxsz -= read_sz;
+ }
+ if ((r = read(ctl->fd, buf, read_sz)) == 0)
+ break;
MD5Update(&(ctl->ctx), buf, r);
count += r;
}
@@ -70,6 +83,7 @@ static void __attribute__((__noreturn__)) usage(FILE * out)
fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
fputs(USAGE_OPTIONS, out);
fputs(_(" -f, --file <file> use file as a cookie seed\n"), out);
+ fputs(_(" -m, --max-size <num> limit how much is read from seed files\n"), out);
fputs(_(" -v, --verbose explain what is being done\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(USAGE_HELP, out);
@@ -117,6 +131,7 @@ int main(int argc, char **argv)
static const struct option longopts[] = {
{"file", required_argument, NULL, 'f'},
+ {"max-size", required_argument, NULL, 'm'},
{"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
@@ -135,7 +150,7 @@ int main(int argc, char **argv)
ctl.files = xmalloc(sizeof(char *) * argc);
while ((c =
- getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
+ getopt_long(argc, argv, "f:m:vVh", longopts, NULL)) != -1)
switch (c) {
case 'v':
ctl.verbose = 1;
@@ -144,6 +159,10 @@ int main(int argc, char **argv)
ctl.files[ctl.nfiles] = optarg;
ctl.nfiles++;
break;
+ case 'm':
+ ctl.maxsz = strtosize_or_err(optarg,
+ _("failed to parse length"));
+ break;
case 'V':
printf(UTIL_LINUX_VERSION);
return EXIT_SUCCESS;
--
1.9.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 1/5] mcookie: use lib/randutils
2014-03-08 21:43 [PATCH 1/5] mcookie: use lib/randutils Sami Kerola
` (3 preceding siblings ...)
2014-03-08 21:43 ` [PATCH 5/5] mcookie: add --max-size option Sami Kerola
@ 2014-03-26 11:12 ` Karel Zak
4 siblings, 0 replies; 6+ messages in thread
From: Karel Zak @ 2014-03-26 11:12 UTC (permalink / raw)
To: Sami Kerola; +Cc: util-linux
On Sat, Mar 08, 2014 at 03:43:26PM -0600, Sami Kerola wrote:
> include/randutils.h | 1 +
> lib/randutils.c | 19 ++++++++++++++++
> misc-utils/Makemodule.am | 1 +
> misc-utils/mcookie.1 | 45 +++++++++++++++-----------------------
> misc-utils/mcookie.c | 57 +++++++++---------------------------------------
> 5 files changed, 48 insertions(+), 75 deletions(-)
All applied, thanks.
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-03-26 11:12 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-08 21:43 [PATCH 1/5] mcookie: use lib/randutils Sami Kerola
2014-03-08 21:43 ` [PATCH 2/5] mcookie: allow --file option be defined multiple times Sami Kerola
2014-03-08 21:43 ` [PATCH 3/5] mcookie: use same variable type consistently Sami Kerola
2014-03-08 21:43 ` [PATCH 4/5] mcookie: use control structure, and fix usage() Sami Kerola
2014-03-08 21:43 ` [PATCH 5/5] mcookie: add --max-size option Sami Kerola
2014-03-26 11:12 ` [PATCH 1/5] mcookie: use lib/randutils Karel Zak
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox