Home | History | Annotate | Line # | Download | only in cksum
cksum.c revision 1.35
      1 /*	$NetBSD: cksum.c,v 1.35 2006/04/24 21:07:43 hubertf 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.35 2006/04/24 21:07:43 hubertf 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 	/*
    197 	 * The -1, -2, -4, -5, -6, and -m flags should be deprecated, but
    198 	 * are still supported in code to not break anything that might
    199 	 * be using them.
    200 	 */
    201 	while ((ch = getopt(argc, argv, "a:cmno:ps:twx12456")) != -1)
    202 		switch(ch) {
    203 		case 'a':
    204 			if (hash != NULL || dosum) {
    205 				warnx("illegal use of -a option\n");
    206 				usage();
    207 			}
    208 			i = 0;
    209 			while (hashes[i].hashname != NULL) {
    210 				if (!strcasecmp(hashes[i].hashname, optarg)) {
    211 					hash = &hashes[i];
    212 					break;
    213 				}
    214 				i++;
    215 			}
    216 			if (hash == NULL) {
    217 				if (!strcasecmp(optarg, "old1")) {
    218 					cfncn = csum1;
    219 					pfncn = psum1;
    220 				} else if (!strcasecmp(optarg, "old2")) {
    221 					cfncn = csum2;
    222 					pfncn = psum2;
    223 				} else if (!strcasecmp(optarg, "crc")) {
    224 					cfncn = crc;
    225 					pfncn = pcrc;
    226 				} else {
    227 					warnx("illegal argument to -a option");
    228 					usage();
    229 				}
    230 			}
    231 			break;
    232 		case '2':
    233 			if (dosum) {
    234 				warnx("sum mutually exclusive with md2");
    235 				usage();
    236 			}
    237 			hash = &hashes[HASH_MD2];
    238 			break;
    239 		case '4':
    240 			if (dosum) {
    241 				warnx("sum mutually exclusive with md4");
    242 				usage();
    243 			}
    244 			hash = &hashes[HASH_MD4];
    245 			break;
    246 		case 'm':
    247 		case '5':
    248 			if (dosum) {
    249 				warnx("sum mutually exclusive with md5");
    250 				usage();
    251 			}
    252 			hash = &hashes[HASH_MD5];
    253 			break;
    254 		case '1':
    255 			if (dosum) {
    256 				warnx("sum mutually exclusive with sha1");
    257 				usage();
    258 			}
    259 			hash = &hashes[HASH_SHA1];
    260 			break;
    261 		case '6':
    262 			if (dosum) {
    263 				warnx("sum mutually exclusive with rmd160");
    264 				usage();
    265 			}
    266 			hash = &hashes[HASH_RMD160];
    267 			break;
    268 		case 'c':
    269 			do_check = 1;
    270 			break;
    271 		case 'n':
    272 			normal = 1;
    273 			break;
    274 		case 'o':
    275 			if (hash) {
    276 				warnx("%s mutually exclusive with sum",
    277 				      hash->hashname);
    278 				usage();
    279 			}
    280 			if (!strcmp(optarg, "1")) {
    281 				cfncn = csum1;
    282 				pfncn = psum1;
    283 			} else if (!strcmp(optarg, "2")) {
    284 				cfncn = csum2;
    285 				pfncn = psum2;
    286 			} else {
    287 				warnx("illegal argument to -o option");
    288 				usage();
    289 			}
    290 			break;
    291 		case 'p':
    292 			if (hash == NULL)
    293 				requirehash("-p");
    294 			pflag = 1;
    295 			break;
    296 		case 's':
    297 			if (hash == NULL)
    298 				requirehash("-s");
    299 			nohashstdin = 1;
    300 			hash->stringfunc(optarg);
    301 			break;
    302 		case 't':
    303 			if (hash == NULL)
    304 				requirehash("-t");
    305 			nohashstdin = 1;
    306 			hash->timetrialfunc();
    307 			break;
    308 		case 'w':
    309 			check_warn = 1;
    310 			break;
    311 		case 'x':
    312 			if (hash == NULL)
    313 				requirehash("-x");
    314 			nohashstdin = 1;
    315 			hash->testsuitefunc();
    316 			break;
    317 		case '?':
    318 		default:
    319 			usage();
    320 		}
    321 	argc -= optind;
    322 	argv += optind;
    323 
    324 	if (do_check) {
    325 		/*
    326 		 * Verify checksums
    327 		 */
    328 		FILE *f;
    329 		char buf[BUFSIZ];
    330 		char *s, *p_filename, *p_cksum;
    331 		int l_filename, l_cksum;
    332 		char filename[BUFSIZ];
    333 		char cksum[BUFSIZ];
    334 		int ok,cnt,badcnt;
    335 
    336 		rval = 0;
    337 		cnt = badcnt = 0;
    338 
    339 		if (argc == 0) {
    340 			f = fdopen(STDIN_FILENO, "r");
    341 		} else {
    342 			f = fopen(argv[0], "r");
    343 		}
    344 		if (f == NULL)
    345 			err(1, "Cannot read %s",
    346 			    argc>0?argv[0]:"stdin");
    347 
    348 		while(fgets(buf, sizeof(buf), f) != NULL) {
    349 			s=strrchr(buf, '\n');
    350 			if (s)
    351 				*s = '\0';
    352 
    353 			p_cksum = p_filename = NULL;
    354 
    355 			p_filename = strchr(buf, '(');
    356 			if (p_filename) {
    357 				/*
    358 				 * Assume 'normal' output if there's a '('
    359 				 */
    360 				p_filename += 1;
    361 				normal = 0;
    362 
    363 				p_cksum = strrchr(p_filename, ')');
    364 				if (p_cksum == NULL) {
    365 					if (check_warn)
    366 						warnx("bogus format: %s. "
    367 						      "Skipping...",
    368 						      buf);
    369 					rval = 1;
    370 					continue;
    371 				}
    372  				p_cksum += 4;
    373 
    374 				l_cksum = strlen(p_cksum);
    375 				l_filename = p_cksum - p_filename - 4;
    376 
    377 				/* Sanity check, and find proper hash if
    378 				 * it's not the same as the current program
    379 				 */
    380 				if (hash == NULL ||
    381 				    strncmp(buf, hash->hashname,
    382 					    strlen(hash->hashname)) != 0) {
    383 					/*
    384 					 * Search proper hash
    385 					 */
    386 					struct hash *nhash;
    387 
    388 					for (nhash = hashes ;
    389 					     nhash->hashname != NULL;
    390 					     nhash++)
    391 						if (strncmp(buf,
    392 							    nhash->hashname,
    393 							    strlen(nhash->hashname)) == 0)
    394 							break;
    395 
    396 
    397 					if (nhash->hashname == NULL) {
    398 						if (check_warn)
    399 							warnx("unknown hash: %s",
    400 							      buf);
    401 						rval = 1;
    402 						continue;
    403 					} else {
    404 						hash = nhash;
    405 					}
    406 				}
    407 
    408 			} else {
    409 				if (hash) {
    410 					/*
    411 					 * 'normal' output, no (ck)sum
    412 					 */
    413 					normal = 1;
    414 
    415 					p_cksum = buf;
    416 					p_filename = strchr(buf, ' ');
    417 					if (p_filename == NULL) {
    418 						if (check_warn)
    419 							warnx("no filename in %s? "
    420 							      "Skipping...", buf);
    421 						rval = 1;
    422 						continue;
    423 					}
    424 					p_filename++;
    425 					l_filename = strlen(p_filename);
    426 					l_cksum = p_filename - buf - 1;
    427 				} else {
    428 					/*
    429 					 * sum/cksum output format
    430 					 */
    431 					p_cksum = buf;
    432 					s=strchr(p_cksum, ' ');
    433 					if (s == NULL) {
    434 						if (check_warn)
    435 							warnx("bogus format: %s."
    436 							      " Skipping...",
    437 							      buf);
    438 						rval = 1;
    439 						continue;
    440 					}
    441 					l_cksum = s - p_cksum;
    442 
    443 					p_filename = strrchr(buf, ' ');
    444 					if (p_filename == NULL) {
    445 						if (check_warn)
    446 							warnx("no filename in %s?"
    447 							      " Skipping...",
    448 							      buf);
    449 						rval = 1;
    450 						continue;
    451 					}
    452 					p_filename++;
    453 					l_filename = strlen(p_filename);
    454 				}
    455 			}
    456 
    457 			strlcpy(filename, p_filename, l_filename+1);
    458 			strlcpy(cksum, p_cksum, l_cksum+1);
    459 
    460 			if (hash) {
    461 				if (access(filename, R_OK) == 0
    462 				    && strcmp(cksum, hash->filefunc(filename, NULL)) == 0)
    463 					ok = 1;
    464 				else
    465 					ok = 0;
    466 			} else {
    467 				if ((fd = open(filename, O_RDONLY, 0)) < 0) {
    468 					if (check_warn)
    469 						warn("%s", filename);
    470 					rval = 1;
    471 					ok = 0;
    472 				} else {
    473 					if (cfncn(fd, &val, &len))
    474 						ok = 0;
    475 					else {
    476 						u_int32_t should_val;
    477 
    478 						should_val =
    479 						  strtoul(cksum, NULL, 10);
    480 						if (val == should_val)
    481 							ok = 1;
    482 						else
    483 							ok = 0;
    484 					}
    485 				}
    486 			}
    487 
    488 			if (! ok) {
    489 				if (hash)
    490 					printf("(%s) ", hash->hashname);
    491 				printf("%s: FAILED\n", filename);
    492 				badcnt++;
    493 			}
    494 			cnt++;
    495 
    496 		}
    497 		fclose(f);
    498 
    499 		if (badcnt > 0)
    500 			rval = 1;
    501 
    502 	} else {
    503 		/*
    504 		 * Calculate checksums
    505 		 */
    506 
    507 		fd = STDIN_FILENO;
    508 		fn = NULL;
    509 		rval = 0;
    510 		do {
    511 			if (*argv) {
    512 				fn = *argv++;
    513 				if (hash != NULL) {
    514 					if (hash_digest_file(fn, hash, normal)) {
    515 						warn("%s", fn);
    516 						rval = 1;
    517 					}
    518 					continue;
    519 				}
    520 				if ((fd = open(fn, O_RDONLY, 0)) < 0) {
    521 					warn("%s", fn);
    522 					rval = 1;
    523 					continue;
    524 				}
    525 			} else if (hash && !nohashstdin) {
    526 				hash->filterfunc(pflag);
    527 			}
    528 
    529 			if (hash == NULL) {
    530 				if (cfncn(fd, &val, &len)) {
    531 					warn("%s", fn ? fn : "stdin");
    532 					rval = 1;
    533 				} else
    534 					pfncn(fn, val, len);
    535 				(void)close(fd);
    536 			}
    537 		} while (*argv);
    538 	}
    539 	exit(rval);
    540 }
    541 
    542 int
    543 hash_digest_file(char *fn, struct hash *hash, int normal)
    544 {
    545 	char *cp;
    546 
    547 	cp = hash->filefunc(fn, NULL);
    548 	if (cp == NULL)
    549 		return 1;
    550 
    551 	if (normal)
    552 		printf("%s %s\n", cp, fn);
    553 	else
    554 		printf("%s (%s) = %s\n", hash->hashname, fn, cp);
    555 
    556 	free(cp);
    557 
    558 	return 0;
    559 }
    560 
    561 void
    562 requirehash(const char *flg)
    563 {
    564 	warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
    565 	    flg);
    566 	usage();
    567 }
    568 
    569 void
    570 usage(void)
    571 {
    572 
    573 	(void)fprintf(stderr, "usage: cksum [-nw] [-a algorithm | -c file | -m | -1 | -2 | -4 | -5 | -6 \n\t\t| [-o 1 | 2]] [file ...]\n");
    574 	(void)fprintf(stderr, "       sum [-c] [file ...]\n");
    575 	(void)fprintf(stderr,
    576 	    "       md2 [-n] [-p | -t | -x | -s string] [file ...]\n");
    577 	(void)fprintf(stderr,
    578 	    "       md4 [-n] [-p | -t | -x | -s string] [file ...]\n");
    579 	(void)fprintf(stderr,
    580 	    "       md5 [-n] [-p | -t | -x | -s string] [file ...]\n");
    581 	(void)fprintf(stderr,
    582 	    "       sha1 [-n] [-p | -t | -x | -s string] [file ...]\n");
    583 	(void)fprintf(stderr,
    584 	    "       rmd160 [-n] [-p | -t | -x | -s string] [file ...]\n");
    585 	exit(1);
    586 }
    587