Home | History | Annotate | Line # | Download | only in cksum
cksum.c revision 1.36
      1 /*	$NetBSD: cksum.c,v 1.36 2006/05/05 22:07:22 elad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * James W. Williams of NASA Goddard Space Flight Center.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*-
     36  * Copyright (c) 1997 Jason R. Thorpe.  All rights reserved.
     37  *
     38  * This code is derived from software contributed to Berkeley by
     39  * James W. Williams of NASA Goddard Space Flight Center.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. All advertising materials mentioning features or use of this software
     50  *    must display the following acknowledgement:
     51  *	This product includes software developed by the University of
     52  *	California, Berkeley and its contributors.
     53  * 4. Neither the name of the University nor the names of its contributors
     54  *    may be used to endorse or promote products derived from this software
     55  *    without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  */
     69 
     70 #if HAVE_NBTOOL_CONFIG_H
     71 #include "nbtool_config.h"
     72 #endif
     73 
     74 #include <sys/cdefs.h>
     75 #if defined(__COPYRIGHT) && !defined(lint)
     76 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     77 	The Regents of the University of California.  All rights reserved.\n");
     78 #endif /* not lint */
     79 
     80 #if defined(__RCSID) && !defined(lint)
     81 #if 0
     82 static char sccsid[] = "@(#)cksum.c	8.2 (Berkeley) 4/28/95";
     83 #endif
     84 __RCSID("$NetBSD: cksum.c,v 1.36 2006/05/05 22:07:22 elad Exp $");
     85 #endif /* not lint */
     86 
     87 #include <sys/cdefs.h>
     88 #include <sys/types.h>
     89 
     90 #include <err.h>
     91 #include <errno.h>
     92 #include <fcntl.h>
     93 #include <locale.h>
     94 #include <md5.h>
     95 #include <md4.h>
     96 #include <md2.h>
     97 #include <sha1.h>
     98 #include <crypto/sha2.h>
     99 #include <crypto/rmd160.h>
    100 #include <stdio.h>
    101 #include <stdlib.h>
    102 #include <string.h>
    103 #include <unistd.h>
    104 
    105 #include "extern.h"
    106 
    107 #define HASH_MD2	0
    108 #define HASH_MD4	1
    109 #define HASH_MD5	2
    110 #define HASH_SHA1	3
    111 #define HASH_RMD160	4
    112 
    113 typedef char *(*_filefunc)(const char *, char *);
    114 
    115 struct hash {
    116 	const char *progname;
    117 	const char *hashname;
    118 	void (*stringfunc)(const char *);
    119 	void (*timetrialfunc)(void);
    120 	void (*testsuitefunc)(void);
    121 	void (*filterfunc)(int);
    122 	char *(*filefunc)(const char *, char *);
    123 } hashes[] = {
    124 	{ "md2", "MD2",
    125 	  MD2String, MD2TimeTrial, MD2TestSuite,
    126 	  MD2Filter, MD2File },
    127 	{ "md4", "MD4",
    128 	  MD4String, MD4TimeTrial, MD4TestSuite,
    129 	  MD4Filter, MD4File },
    130 	{ "md5", "MD5",
    131 	  MD5String, MD5TimeTrial, MD5TestSuite,
    132 	  MD5Filter, MD5File },
    133 	{ "sha1", "SHA1",
    134 	  SHA1String, SHA1TimeTrial, SHA1TestSuite,
    135 	  SHA1Filter, (_filefunc) SHA1File },
    136 	{ "rmd160", "RMD160",
    137 	  RMD160String, RMD160TimeTrial, RMD160TestSuite,
    138 	  RMD160Filter, (_filefunc) RMD160File },
    139 	{ "sha256", "SHA256",
    140 	  SHA256_String, SHA256_TimeTrial, SHA256_TestSuite,
    141 	  SHA256_Filter, (_filefunc) SHA256_File },
    142 	{ "sha384", "SHA384",
    143 	  SHA384_String, SHA384_TimeTrial, SHA384_TestSuite,
    144 	  SHA384_Filter, (_filefunc) SHA384_File },
    145 	{ "sha512", "SHA512",
    146 	  SHA512_String, SHA512_TimeTrial, SHA512_TestSuite,
    147 	  SHA512_Filter, (_filefunc) SHA512_File },
    148 	{ NULL }
    149 };
    150 
    151 int	hash_digest_file(char *, struct hash *, int);
    152 void	requirehash(const char *);
    153 void	usage(void);
    154 
    155 int
    156 main(int argc, char **argv)
    157 {
    158 	int ch, fd, rval, dosum, pflag, nohashstdin;
    159 	u_int32_t val;
    160 	off_t len;
    161 	char *fn;
    162 	const char *progname;
    163 	int (*cfncn) (int, u_int32_t *, off_t *);
    164 	void (*pfncn) (char *, u_int32_t, off_t);
    165 	struct hash *hash;
    166 	int normal, i, check_warn, do_check;
    167 
    168 	cfncn = NULL;
    169 	pfncn = NULL;
    170 	dosum = pflag = nohashstdin = 0;
    171 	normal = 0;
    172 	check_warn = 0;
    173 	do_check = 0;
    174 
    175 	setlocale(LC_ALL, "");
    176 
    177 	progname = getprogname();
    178 
    179 	for (hash = hashes; hash->hashname != NULL; hash++)
    180 		if (strcmp(progname, hash->progname) == 0)
    181 			break;
    182 
    183 	if (hash->hashname == NULL) {
    184 		hash = NULL;
    185 
    186 		if (!strcmp(progname, "sum")) {
    187 			dosum = 1;
    188 			cfncn = csum1;
    189 			pfncn = psum1;
    190 		} else {
    191 			cfncn = crc;
    192 			pfncn = pcrc;
    193 		}
    194 	}
    195 
    196 	while ((ch = getopt(argc, argv, "a:cno:ps:twx")) != -1)
    197 		switch(ch) {
    198 		case 'a':
    199 			if (hash != NULL || dosum) {
    200 				warnx("illegal use of -a option\n");
    201 				usage();
    202 			}
    203 			i = 0;
    204 			while (hashes[i].hashname != NULL) {
    205 				if (!strcasecmp(hashes[i].hashname, optarg)) {
    206 					hash = &hashes[i];
    207 					break;
    208 				}
    209 				i++;
    210 			}
    211 			if (hash == NULL) {
    212 				if (!strcasecmp(optarg, "old1")) {
    213 					cfncn = csum1;
    214 					pfncn = psum1;
    215 				} else if (!strcasecmp(optarg, "old2")) {
    216 					cfncn = csum2;
    217 					pfncn = psum2;
    218 				} else if (!strcasecmp(optarg, "crc")) {
    219 					cfncn = crc;
    220 					pfncn = pcrc;
    221 				} else {
    222 					warnx("illegal argument to -a option");
    223 					usage();
    224 				}
    225 			}
    226 			break;
    227 		case 'c':
    228 			do_check = 1;
    229 			break;
    230 		case 'n':
    231 			normal = 1;
    232 			break;
    233 		case 'o':
    234 			if (hash) {
    235 				warnx("%s mutually exclusive with sum",
    236 				      hash->hashname);
    237 				usage();
    238 			}
    239 			if (!strcmp(optarg, "1")) {
    240 				cfncn = csum1;
    241 				pfncn = psum1;
    242 			} else if (!strcmp(optarg, "2")) {
    243 				cfncn = csum2;
    244 				pfncn = psum2;
    245 			} else {
    246 				warnx("illegal argument to -o option");
    247 				usage();
    248 			}
    249 			break;
    250 		case 'p':
    251 			if (hash == NULL)
    252 				requirehash("-p");
    253 			pflag = 1;
    254 			break;
    255 		case 's':
    256 			if (hash == NULL)
    257 				requirehash("-s");
    258 			nohashstdin = 1;
    259 			hash->stringfunc(optarg);
    260 			break;
    261 		case 't':
    262 			if (hash == NULL)
    263 				requirehash("-t");
    264 			nohashstdin = 1;
    265 			hash->timetrialfunc();
    266 			break;
    267 		case 'w':
    268 			check_warn = 1;
    269 			break;
    270 		case 'x':
    271 			if (hash == NULL)
    272 				requirehash("-x");
    273 			nohashstdin = 1;
    274 			hash->testsuitefunc();
    275 			break;
    276 		case '?':
    277 		default:
    278 			usage();
    279 		}
    280 	argc -= optind;
    281 	argv += optind;
    282 
    283 	if (do_check) {
    284 		/*
    285 		 * Verify checksums
    286 		 */
    287 		FILE *f;
    288 		char buf[BUFSIZ];
    289 		char *s, *p_filename, *p_cksum;
    290 		int l_filename, l_cksum;
    291 		char filename[BUFSIZ];
    292 		char cksum[BUFSIZ];
    293 		int ok,cnt,badcnt;
    294 
    295 		rval = 0;
    296 		cnt = badcnt = 0;
    297 
    298 		if (argc == 0) {
    299 			f = fdopen(STDIN_FILENO, "r");
    300 		} else {
    301 			f = fopen(argv[0], "r");
    302 		}
    303 		if (f == NULL)
    304 			err(1, "Cannot read %s",
    305 			    argc>0?argv[0]:"stdin");
    306 
    307 		while(fgets(buf, sizeof(buf), f) != NULL) {
    308 			s=strrchr(buf, '\n');
    309 			if (s)
    310 				*s = '\0';
    311 
    312 			p_cksum = p_filename = NULL;
    313 
    314 			p_filename = strchr(buf, '(');
    315 			if (p_filename) {
    316 				/*
    317 				 * Assume 'normal' output if there's a '('
    318 				 */
    319 				p_filename += 1;
    320 				normal = 0;
    321 
    322 				p_cksum = strrchr(p_filename, ')');
    323 				if (p_cksum == NULL) {
    324 					if (check_warn)
    325 						warnx("bogus format: %s. "
    326 						      "Skipping...",
    327 						      buf);
    328 					rval = 1;
    329 					continue;
    330 				}
    331  				p_cksum += 4;
    332 
    333 				l_cksum = strlen(p_cksum);
    334 				l_filename = p_cksum - p_filename - 4;
    335 
    336 				/* Sanity check, and find proper hash if
    337 				 * it's not the same as the current program
    338 				 */
    339 				if (hash == NULL ||
    340 				    strncmp(buf, hash->hashname,
    341 					    strlen(hash->hashname)) != 0) {
    342 					/*
    343 					 * Search proper hash
    344 					 */
    345 					struct hash *nhash;
    346 
    347 					for (nhash = hashes ;
    348 					     nhash->hashname != NULL;
    349 					     nhash++)
    350 						if (strncmp(buf,
    351 							    nhash->hashname,
    352 							    strlen(nhash->hashname)) == 0)
    353 							break;
    354 
    355 
    356 					if (nhash->hashname == NULL) {
    357 						if (check_warn)
    358 							warnx("unknown hash: %s",
    359 							      buf);
    360 						rval = 1;
    361 						continue;
    362 					} else {
    363 						hash = nhash;
    364 					}
    365 				}
    366 
    367 			} else {
    368 				if (hash) {
    369 					/*
    370 					 * 'normal' output, no (ck)sum
    371 					 */
    372 					normal = 1;
    373 
    374 					p_cksum = buf;
    375 					p_filename = strchr(buf, ' ');
    376 					if (p_filename == NULL) {
    377 						if (check_warn)
    378 							warnx("no filename in %s? "
    379 							      "Skipping...", buf);
    380 						rval = 1;
    381 						continue;
    382 					}
    383 					p_filename++;
    384 					l_filename = strlen(p_filename);
    385 					l_cksum = p_filename - buf - 1;
    386 				} else {
    387 					/*
    388 					 * sum/cksum output format
    389 					 */
    390 					p_cksum = buf;
    391 					s=strchr(p_cksum, ' ');
    392 					if (s == NULL) {
    393 						if (check_warn)
    394 							warnx("bogus format: %s."
    395 							      " Skipping...",
    396 							      buf);
    397 						rval = 1;
    398 						continue;
    399 					}
    400 					l_cksum = s - p_cksum;
    401 
    402 					p_filename = strrchr(buf, ' ');
    403 					if (p_filename == NULL) {
    404 						if (check_warn)
    405 							warnx("no filename in %s?"
    406 							      " Skipping...",
    407 							      buf);
    408 						rval = 1;
    409 						continue;
    410 					}
    411 					p_filename++;
    412 					l_filename = strlen(p_filename);
    413 				}
    414 			}
    415 
    416 			strlcpy(filename, p_filename, l_filename+1);
    417 			strlcpy(cksum, p_cksum, l_cksum+1);
    418 
    419 			if (hash) {
    420 				if (access(filename, R_OK) == 0
    421 				    && strcmp(cksum, hash->filefunc(filename, NULL)) == 0)
    422 					ok = 1;
    423 				else
    424 					ok = 0;
    425 			} else {
    426 				if ((fd = open(filename, O_RDONLY, 0)) < 0) {
    427 					if (check_warn)
    428 						warn("%s", filename);
    429 					rval = 1;
    430 					ok = 0;
    431 				} else {
    432 					if (cfncn(fd, &val, &len))
    433 						ok = 0;
    434 					else {
    435 						u_int32_t should_val;
    436 
    437 						should_val =
    438 						  strtoul(cksum, NULL, 10);
    439 						if (val == should_val)
    440 							ok = 1;
    441 						else
    442 							ok = 0;
    443 					}
    444 				}
    445 			}
    446 
    447 			if (! ok) {
    448 				if (hash)
    449 					printf("(%s) ", hash->hashname);
    450 				printf("%s: FAILED\n", filename);
    451 				badcnt++;
    452 			}
    453 			cnt++;
    454 
    455 		}
    456 		fclose(f);
    457 
    458 		if (badcnt > 0)
    459 			rval = 1;
    460 
    461 	} else {
    462 		/*
    463 		 * Calculate checksums
    464 		 */
    465 
    466 		fd = STDIN_FILENO;
    467 		fn = NULL;
    468 		rval = 0;
    469 		do {
    470 			if (*argv) {
    471 				fn = *argv++;
    472 				if (hash != NULL) {
    473 					if (hash_digest_file(fn, hash, normal)) {
    474 						warn("%s", fn);
    475 						rval = 1;
    476 					}
    477 					continue;
    478 				}
    479 				if ((fd = open(fn, O_RDONLY, 0)) < 0) {
    480 					warn("%s", fn);
    481 					rval = 1;
    482 					continue;
    483 				}
    484 			} else if (hash && !nohashstdin) {
    485 				hash->filterfunc(pflag);
    486 			}
    487 
    488 			if (hash == NULL) {
    489 				if (cfncn(fd, &val, &len)) {
    490 					warn("%s", fn ? fn : "stdin");
    491 					rval = 1;
    492 				} else
    493 					pfncn(fn, val, len);
    494 				(void)close(fd);
    495 			}
    496 		} while (*argv);
    497 	}
    498 	exit(rval);
    499 }
    500 
    501 int
    502 hash_digest_file(char *fn, struct hash *hash, int normal)
    503 {
    504 	char *cp;
    505 
    506 	cp = hash->filefunc(fn, NULL);
    507 	if (cp == NULL)
    508 		return 1;
    509 
    510 	if (normal)
    511 		printf("%s %s\n", cp, fn);
    512 	else
    513 		printf("%s (%s) = %s\n", hash->hashname, fn, cp);
    514 
    515 	free(cp);
    516 
    517 	return 0;
    518 }
    519 
    520 void
    521 requirehash(const char *flg)
    522 {
    523 	warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
    524 	    flg);
    525 	usage();
    526 }
    527 
    528 void
    529 usage(void)
    530 {
    531 
    532 	(void)fprintf(stderr, "usage: cksum [-nw] [-a algorithm | -c file ]\n\t\t| [-o 1 | 2]] [file ...]\n");
    533 	(void)fprintf(stderr, "       sum [-c] [file ...]\n");
    534 	(void)fprintf(stderr,
    535 	    "       md2 [-n] [-p | -t | -x | -s string] [file ...]\n");
    536 	(void)fprintf(stderr,
    537 	    "       md4 [-n] [-p | -t | -x | -s string] [file ...]\n");
    538 	(void)fprintf(stderr,
    539 	    "       md5 [-n] [-p | -t | -x | -s string] [file ...]\n");
    540 	(void)fprintf(stderr,
    541 	    "       sha1 [-n] [-p | -t | -x | -s string] [file ...]\n");
    542 	(void)fprintf(stderr,
    543 	    "       rmd160 [-n] [-p | -t | -x | -s string] [file ...]\n");
    544 	exit(1);
    545 }
    546