Re: cksum not working with both -a and -C flags together

2017-04-25 Thread Craig Skinner
On Tue, 28 Mar 2017 11:56:19 -0600 "Todd C. Miller" wrote:
> On Tue, 28 Mar 2017 09:33:53 -0600, "Todd C. Miller" wrote:
> 
> > It seems to me that in -C mode it should really process all the
> > checksums that match the specified file(s), but the documentation
> > does not actually specify what the behavior is in this case.
> 
> Here's a diff that accomplishes that.
> 
>  - todd


Who's OK with Todd's proved patch?


> 
> Index: bin/md5/md5.c
> ===
> RCS file: /cvs/src/bin/md5/md5.c,v
> retrieving revision 1.89
> diff -u -p -u -r1.89 md5.c
> --- bin/md5/md5.c 16 Dec 2016 17:55:26 -  1.89
> +++ bin/md5/md5.c 28 Mar 2017 17:54:05 -
> @@ -554,6 +554,7 @@ digest_filelist(const char *file, struct
>   char *lbuf = NULL;
>   FILE *listfp, *fp;
>   size_t len, nread;
> + int *sel_found = NULL;
>   u_char data[32 * 1024];
>   union ANY_CTX context;
>   struct hash_function *hf;
> @@ -565,6 +566,12 @@ digest_filelist(const char *file, struct
>   return(1);
>   }
>  
> + if (sel != NULL) {
> + sel_found = calloc((size_t)selcount,
> sizeof(*sel_found));
> + if (sel_found == NULL)
> + err(1, NULL);
> + }
> +
>   algorithm_max = algorithm_min = strlen(functions[0].name);
>   for (hf = [1]; hf->name != NULL; hf++) {
>   len = strlen(hf->name);
> @@ -673,13 +680,11 @@ digest_filelist(const char *file, struct
>   /*
>* If only a selection of files is wanted, proceed
> only
>* if the filename matches one of those in the
> selection.
> -  * Mark found files by setting them to NULL so that
> we can
> -  * detect files that are missing from the checklist
> later. */
> - if (sel) {
> + if (sel != NULL) {
>   for (i = 0; i < selcount; i++) {
> - if (sel[i] && strcmp(sel[i],
> filename) == 0) {
> - sel[i] = NULL;
> + if (strcmp(sel[i], filename) == 0) {
> + sel_found[i] = 1;
>   break;
>   }
>   }
> @@ -725,6 +730,17 @@ digest_filelist(const char *file, struct
>   if (!found)
>   warnx("%s: no properly formatted checksum lines
> found", file); free(lbuf);
> + if (sel_found != NULL) {
> + /*
> +  * Mark found files by setting them to NULL so that
> we can
> +  * detect files that are missing from the checklist
> later.
> +  */
> + for (i = 0; i < selcount; i++) {
> + if (sel_found[i])
> + sel[i] = NULL;
> + }
> + free(sel_found);
> + }
>   return(error || !found);
>  }
>  
> 



Re: cksum not working with both -a and -C flags together

2017-04-18 Thread Craig Skinner
On 2017-03-28 Tue 11:56 AM |, Todd C. Miller wrote:
> On Tue, 28 Mar 2017 09:33:53 -0600, "Todd C. Miller" wrote:
> 
> > It seems to me that in -C mode it should really process all the
> > checksums that match the specified file(s), but the documentation
> > does not actually specify what the behavior is in this case.
> 
> Here's a diff that accomplishes that.
> 

Yep:


$ cksum -b -a cksum,MD5,RMD160,SHA1,SHA256 -h /tmp/bsd.digest /bsd
$ cksum -C /tmp/bsd.digest /bsd
(CKSUM) /bsd: OK
(MD5) /bsd: OK
(RMD160) /bsd: OK
(SHA1) /bsd: OK
(SHA256) /bsd: OK

# Scramble line order:
$ vi /tmp/bsd.digest
$ cksum -C /tmp/bsd.digest /bsd
(MD5) /bsd: OK
(SHA256) /bsd: OK
(CKSUM) /bsd: OK
(SHA1) /bsd: OK
(RMD160) /bsd: OK

# Only SHA* digests, a bad MD5 digest & some junk:
$ mv /tmp/bsd.digest /tmp/bsd.digest~
$ fgrep SHA /tmp/bsd.digest~ > /tmp/bsd.digest
$ print 'MD5 (/bsd) = I-no-thinky-so-fail-me==' >> /tmp/bsd.digest
$ print 'JUNK (/bsd) = road-killed-dead-beef==' >> /tmp/bsd.digest
$ cksum -C /tmp/bsd.digest /bsd
(SHA256) /bsd: OK
(SHA1) /bsd: OK
(MD5) /bsd: FAILED
print $?
1

# Reorder check list, making valid digests last:
$ sort -o /tmp/bsd.digest /tmp/bsd.digest
$ cksum -C /tmp/bsd.digest /bsd
(MD5) /bsd: FAILED
(SHA1) /bsd: OK
(SHA256) /bsd: OK
$ print $?
1



Re: cksum not working with both -a and -C flags together

2017-03-28 Thread Todd C. Miller
On Tue, 28 Mar 2017 09:33:53 -0600, "Todd C. Miller" wrote:

> It seems to me that in -C mode it should really process all the
> checksums that match the specified file(s), but the documentation
> does not actually specify what the behavior is in this case.

Here's a diff that accomplishes that.

 - todd

Index: bin/md5/md5.c
===
RCS file: /cvs/src/bin/md5/md5.c,v
retrieving revision 1.89
diff -u -p -u -r1.89 md5.c
--- bin/md5/md5.c   16 Dec 2016 17:55:26 -  1.89
+++ bin/md5/md5.c   28 Mar 2017 17:54:05 -
@@ -554,6 +554,7 @@ digest_filelist(const char *file, struct
char *lbuf = NULL;
FILE *listfp, *fp;
size_t len, nread;
+   int *sel_found = NULL;
u_char data[32 * 1024];
union ANY_CTX context;
struct hash_function *hf;
@@ -565,6 +566,12 @@ digest_filelist(const char *file, struct
return(1);
}
 
+   if (sel != NULL) {
+   sel_found = calloc((size_t)selcount, sizeof(*sel_found));
+   if (sel_found == NULL)
+   err(1, NULL);
+   }
+
algorithm_max = algorithm_min = strlen(functions[0].name);
for (hf = [1]; hf->name != NULL; hf++) {
len = strlen(hf->name);
@@ -673,13 +680,11 @@ digest_filelist(const char *file, struct
/*
 * If only a selection of files is wanted, proceed only
 * if the filename matches one of those in the selection.
-* Mark found files by setting them to NULL so that we can
-* detect files that are missing from the checklist later.
 */
-   if (sel) {
+   if (sel != NULL) {
for (i = 0; i < selcount; i++) {
-   if (sel[i] && strcmp(sel[i], filename) == 0) {
-   sel[i] = NULL;
+   if (strcmp(sel[i], filename) == 0) {
+   sel_found[i] = 1;
break;
}
}
@@ -725,6 +730,17 @@ digest_filelist(const char *file, struct
if (!found)
warnx("%s: no properly formatted checksum lines found", file);
free(lbuf);
+   if (sel_found != NULL) {
+   /*
+* Mark found files by setting them to NULL so that we can
+* detect files that are missing from the checklist later.
+*/
+   for (i = 0; i < selcount; i++) {
+   if (sel_found[i])
+   sel[i] = NULL;
+   }
+   free(sel_found);
+   }
return(error || !found);
 }
 



Re: cksum not working with both -a and -C flags together

2017-03-28 Thread Todd C. Miller
On Tue, 28 Mar 2017 15:49:24 +0100, Craig Skinner wrote:

> When cksum(1) is used with a -C checklist listing multiple algorithms,
> and a single -a algorithm is specified, cksum doesn't select the correct
> -a [algorithm], nor the correct line in the -C [checklist]:

The -a option is only used to set a default hash type in -c/-C mode
when the default cannot be determined from the file name or when
you want to override the default.

In this case, it is really only useful for GNU-style checklist files
that don't list the algorithm.

It seems to me that in -C mode it should really process all the
checksums that match the specified file(s), but the documentation
does not actually specify what the behavior is in this case.

 - todd