]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mcookie: add --max-size option
authorSami Kerola <kerolasa@iki.fi>
Sat, 8 Mar 2014 21:43:30 +0000 (15:43 -0600)
committerKarel Zak <kzak@redhat.com>
Wed, 26 Mar 2014 11:08:57 +0000 (12:08 +0100)
Just in case someone wants to add entropy from device with invocation
demonstrated below.

$ mcookie --file /dev/urandom --max-size 64k

[kzak@redhat.com: - use all-io.h]

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/mcookie.1
misc-utils/mcookie.c

index b974ff0f929464c4648235890d9d5742eed78351..989b7f9b9aff76c8b4a88bf28c91f363fc998da5 100644 (file)
@@ -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.
index 494c1cf89209e0276870c2aca276f1c9ac06ebad..7b60dd825eb61fc167f89c931f277e0e3b98581a 100644 (file)
@@ -23,7 +23,9 @@
 #include "nls.h"
 #include "closestream.h"
 #include "randutils.h"
+#include "strutils.h"
 #include "xalloc.h"
+#include "all-io.h"
 
 #include <fcntl.h>
 #include <getopt.h>
@@ -42,18 +44,29 @@ struct mcookie_control {
        struct  MD5Context ctx;
        char    **files;
        size_t  nfiles;
+       uint64_t maxsz;
 
        unsigned int verbose:1;
 };
 
 /* The basic function to hash a file */
-static size_t hash_file(struct mcookie_control *ctl, int fd)
+static uint64_t hash_file(struct mcookie_control *ctl, int fd)
 {
-       size_t count = 0;
-       ssize_t r;
        unsigned char buf[BUFFERSIZE];
+       uint64_t wanted, count;
 
-       while ((r = read(fd, buf, sizeof(buf))) > 0) {
+       wanted = ctl->maxsz ? ctl->maxsz : sizeof(buf);
+
+       for (count = 0; count < wanted; ) {
+               size_t rdsz = sizeof(buf);
+               ssize_t r;
+
+               if (wanted - count < rdsz)
+                       rdsz = wanted - count;
+
+               r = read_all(fd, (char *) buf, rdsz);
+               if (r < 0)
+                       break;
                MD5Update(&ctl->ctx, buf, r);
                count += r;
        }
@@ -70,6 +83,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
 
        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);
@@ -122,6 +136,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'},
@@ -136,7 +151,7 @@ int main(int argc, char **argv)
        if (2 < argc)
                ctl.files = xmalloc(sizeof(char *) * argc);
 
-       while ((c = getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1) {
+       while ((c = 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)
                case 'f':
                        ctl.files[ctl.nfiles++] = optarg;
                        break;
+               case 'm':
+                       ctl.maxsz = strtosize_or_err(optarg,
+                                                    _("failed to parse length"));
+                       break;
                case 'V':
                        printf(UTIL_LINUX_VERSION);
                        return EXIT_SUCCESS;
@@ -154,6 +173,9 @@ int main(int argc, char **argv)
                }
        }
 
+       if (ctl.maxsz && ctl.nfiles == 0)
+               warnx(_("--max-size ignored when used without --file."));
+
        randomness_from_files(&ctl);
        free(ctl.files);