From owner-freebsd-bugs@FreeBSD.ORG Thu May 13 08:00:18 2010 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0146E1065675 for ; Thu, 13 May 2010 08:00:18 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (unknown [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id BB6B18FC24 for ; Thu, 13 May 2010 08:00:17 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id o4D80HKA049346 for ; Thu, 13 May 2010 08:00:17 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id o4D80HQw049345; Thu, 13 May 2010 08:00:17 GMT (envelope-from gnats) Resent-Date: Thu, 13 May 2010 08:00:17 GMT Resent-Message-Id: <201005130800.o4D80HQw049345@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Eitan Adler Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 756161065674 for ; Thu, 13 May 2010 07:52:46 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21]) by mx1.freebsd.org (Postfix) with ESMTP id 5CB898FC25 for ; Thu, 13 May 2010 07:52:46 +0000 (UTC) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o4D7qjcT051393 for ; Thu, 13 May 2010 07:52:46 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.14.3/8.14.3/Submit) id o4D7qjPY051392; Thu, 13 May 2010 07:52:45 GMT (envelope-from nobody) Message-Id: <201005130752.o4D7qjPY051392@www.freebsd.org> Date: Thu, 13 May 2010 07:52:45 GMT From: Eitan Adler To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: bin/146541: [patch] add check option to md5(1) X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 May 2010 08:00:18 -0000 >Number: 146541 >Category: bin >Synopsis: [patch] add check option to md5(1) >Confidential: no >Severity: non-critical >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Thu May 13 08:00:17 UTC 2010 >Closed-Date: >Last-Modified: >Originator: Eitan Adler >Release: >Organization: >Environment: >Description: This patch adds a -c option to md5(1) and related utilities that accepts as a command line option a hash to check against. If the -q option is not specified it prints "[ failed ]" if the hash of the given filename or string does not match. Regardless of -q it returns 2 on any mismatch. I sent a the patch to -hackers without any feedback. >How-To-Repeat: >Fix: Patch attached with submission follows: Index: md5.1 =================================================================== --- md5.1 (revision 207433) +++ md5.1 (working copy) @@ -73,6 +73,8 @@ The hexadecimal checksum of each file listed on the command line is printed after the options are processed. .Bl -tag -width indent +.It Fl c Ar string +Compare files to this md5 string .It Fl s Ar string Print a checksum of the given .Ar string . @@ -101,7 +103,8 @@ and .Nm rmd160 utilities exit 0 on success, -and 1 if at least one of the input files could not be read. +1 if at least one of the input files could not be read, +and 2 if at least one file does not have the same hash as the -c option. .Sh SEE ALSO .Xr cksum 1 , .Xr md5 3 , Index: md5.c =================================================================== --- md5.c (revision 207433) +++ md5.c (working copy) @@ -44,6 +44,8 @@ int qflag; int rflag; int sflag; +unsigned char* checkAgainst; +int checksFailed; typedef void (DIGEST_Init)(void *); typedef void (DIGEST_Update)(void *, const unsigned char *, size_t); @@ -138,8 +140,13 @@ digest = 0; failed = 0; - while ((ch = getopt(argc, argv, "pqrs:tx")) != -1) + checkAgainst = NULL; + checksFailed = 0; + while ((ch = getopt(argc, argv, "c:pqrs:tx")) != -1) switch (ch) { + case 'c': + checkAgainst = optarg; + break; case 'p': MDFilter(&Algorithm[digest], 1); break; @@ -173,12 +180,19 @@ failed++; } else { if (qflag) - printf("%s\n", p); + printf("%s", p); else if (rflag) - printf("%s %s\n", p, *argv); + printf("%s %s", p, *argv); else - printf("%s (%s) = %s\n", + printf("%s (%s) = %s", Algorithm[digest].name, *argv, p); + if (checkAgainst && strcmp(checkAgainst,p)) + { + checksFailed++; + if (!qflag) + printf("%s","[ Failed ]"); + } + printf("\n"); } } while (*++argv); } else if (!sflag && (optind == 1 || qflag || rflag)) @@ -186,6 +200,8 @@ if (failed != 0) return (1); + if (checksFailed != 0) + return (2); return (0); } @@ -198,12 +214,20 @@ size_t len = strlen(string); char buf[HEX_DIGEST_LENGTH]; + alg->Data(string,len,buf); if (qflag) - printf("%s\n", alg->Data(string, len, buf)); + printf("%s", buf); else if (rflag) - printf("%s \"%s\"\n", alg->Data(string, len, buf), string); + printf("%s \"%s\"", buf, string); else - printf("%s (\"%s\") = %s\n", alg->name, string, alg->Data(string, len, buf)); + printf("%s (\"%s\") = %s", alg->name, string, buf); + if (checkAgainst && strcmp(buf,checkAgainst)) + { + checksFailed++; + if (!qflag) + printf("%s"," [ failed ]"); + } + printf("\n"); } /* * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. >Release-Note: >Audit-Trail: >Unformatted: