Home | History | Annotate | Line # | Download | only in cksum
cksum.c revision 1.16
      1  1.16    bjh21 /*	$NetBSD: cksum.c,v 1.16 2002/03/31 14:43:22 bjh21 Exp $	*/
      2   1.6    glass 
      3   1.1      cgd /*-
      4   1.8  thorpej  * Copyright (c) 1997 Jason R. Thorpe.  All rights reserved.
      5   1.3      cgd  * Copyright (c) 1991, 1993
      6   1.3      cgd  *	The Regents of the University of California.  All rights reserved.
      7   1.1      cgd  *
      8   1.1      cgd  * This code is derived from software contributed to Berkeley by
      9   1.3      cgd  * James W. Williams of NASA Goddard Space Flight Center.
     10   1.1      cgd  *
     11   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1      cgd  * modification, are permitted provided that the following conditions
     13   1.1      cgd  * are met:
     14   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1      cgd  *    must display the following acknowledgement:
     21   1.1      cgd  *	This product includes software developed by the University of
     22   1.1      cgd  *	California, Berkeley and its contributors.
     23   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1      cgd  *    may be used to endorse or promote products derived from this software
     25   1.1      cgd  *    without specific prior written permission.
     26   1.1      cgd  *
     27   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1      cgd  * SUCH DAMAGE.
     38   1.1      cgd  */
     39   1.1      cgd 
     40  1.10    lukem #include <sys/cdefs.h>
     41  1.16    bjh21 #if defined(__COPYRIGHT) && !defined(lint)
     42  1.10    lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     43  1.10    lukem 	The Regents of the University of California.  All rights reserved.\n");
     44   1.1      cgd #endif /* not lint */
     45   1.1      cgd 
     46  1.16    bjh21 #if defined(__RCSID) && !defined(lint)
     47   1.6    glass #if 0
     48   1.7      jtc static char sccsid[] = "@(#)cksum.c	8.2 (Berkeley) 4/28/95";
     49   1.6    glass #endif
     50  1.16    bjh21 __RCSID("$NetBSD: cksum.c,v 1.16 2002/03/31 14:43:22 bjh21 Exp $");
     51   1.1      cgd #endif /* not lint */
     52   1.1      cgd 
     53   1.1      cgd #include <sys/cdefs.h>
     54   1.1      cgd #include <sys/types.h>
     55   1.5  mycroft 
     56   1.5  mycroft #include <err.h>
     57   1.5  mycroft #include <errno.h>
     58   1.1      cgd #include <fcntl.h>
     59   1.9   kleink #include <locale.h>
     60   1.8  thorpej #include <md5.h>
     61  1.13   atatat #include <md4.h>
     62  1.13   atatat #include <md2.h>
     63  1.13   atatat #include <sha1.h>
     64  1.13   atatat #include <rmd160.h>
     65   1.1      cgd #include <stdio.h>
     66   1.1      cgd #include <stdlib.h>
     67   1.1      cgd #include <string.h>
     68   1.5  mycroft #include <unistd.h>
     69   1.5  mycroft 
     70   1.1      cgd #include "extern.h"
     71   1.1      cgd 
     72  1.13   atatat #define HASH_MD2	0
     73  1.13   atatat #define HASH_MD4	1
     74  1.13   atatat #define HASH_MD5	2
     75  1.13   atatat #define HASH_SHA1	3
     76  1.13   atatat #define HASH_RMD160	4
     77  1.13   atatat 
     78  1.13   atatat typedef char *(*_filefunc)(const char *, char *);
     79  1.13   atatat 
     80  1.13   atatat struct hash {
     81  1.13   atatat 	const char *progname;
     82  1.13   atatat 	const char *hashname;
     83  1.13   atatat 	void (*stringfunc)(const char *);
     84  1.13   atatat 	void (*timetrialfunc)(void);
     85  1.13   atatat 	void (*testsuitefunc)(void);
     86  1.13   atatat 	void (*filterfunc)(int);
     87  1.13   atatat 	char *(*filefunc)(const char *, char *);
     88  1.13   atatat } hashes[] = {
     89  1.13   atatat 	{ "md2", "MD2",
     90  1.13   atatat 	  MD2String, MD2TimeTrial, MD2TestSuite,
     91  1.13   atatat 	  MD2Filter, MD2File },
     92  1.13   atatat 	{ "md4", "MD4",
     93  1.13   atatat 	  MD4String, MD4TimeTrial, MD4TestSuite,
     94  1.13   atatat 	  MD4Filter, MD4File },
     95  1.13   atatat 	{ "md5", "MD5",
     96  1.13   atatat 	  MD5String, MD5TimeTrial, MD5TestSuite,
     97  1.13   atatat 	  MD5Filter, MD5File },
     98  1.13   atatat 	{ "sha1", "SHA1",
     99  1.13   atatat 	  SHA1String, SHA1TimeTrial, SHA1TestSuite,
    100  1.13   atatat 	  SHA1Filter, (_filefunc) SHA1File },
    101  1.13   atatat 	{ "rmd160", "RMD160",
    102  1.13   atatat 	  RMD160String, RMD160TimeTrial, RMD160TestSuite,
    103  1.13   atatat 	  RMD160Filter, (_filefunc) RMD160File },
    104  1.13   atatat 	{ NULL }
    105  1.13   atatat };
    106  1.13   atatat 
    107  1.10    lukem int	main __P((int, char **));
    108  1.13   atatat int	hash_digest_file __P((char *, struct hash *));
    109  1.13   atatat void	requirehash __P((const char *));
    110  1.10    lukem void	usage __P((void));
    111   1.3      cgd 
    112   1.3      cgd int
    113   1.1      cgd main(argc, argv)
    114   1.1      cgd 	int argc;
    115   1.1      cgd 	char **argv;
    116   1.1      cgd {
    117  1.13   atatat 	register int ch, fd, rval, dosum, pflag, nohashstdin;
    118   1.4      cgd 	u_int32_t len, val;
    119   1.1      cgd 	char *fn;
    120  1.13   atatat 	const char *progname;
    121   1.4      cgd 	int (*cfncn) __P((int, u_int32_t *, u_int32_t *));
    122   1.4      cgd 	void (*pfncn) __P((char *, u_int32_t, u_int32_t));
    123  1.13   atatat 	struct hash *hash;
    124   1.1      cgd 
    125  1.10    lukem 	cfncn = NULL;
    126  1.10    lukem 	pfncn = NULL;
    127  1.13   atatat 	dosum = pflag = nohashstdin = 0;
    128   1.9   kleink 
    129   1.9   kleink 	setlocale(LC_ALL, "");
    130   1.8  thorpej 
    131  1.13   atatat 	progname = getprogname();
    132  1.13   atatat 
    133  1.13   atatat 	for (hash = hashes; hash->hashname != NULL; hash++)
    134  1.13   atatat 		if (strcmp(progname, hash->progname) == 0)
    135  1.13   atatat 			break;
    136  1.13   atatat 
    137  1.13   atatat 	if (hash->hashname == NULL) {
    138  1.13   atatat 		hash = NULL;
    139  1.13   atatat 
    140  1.13   atatat 		if (!strcmp(progname, "sum")) {
    141  1.13   atatat 			dosum = 1;
    142  1.13   atatat 			cfncn = csum1;
    143  1.13   atatat 			pfncn = psum1;
    144  1.13   atatat 		} else {
    145  1.15   atatat 			cfncn = crc;
    146  1.13   atatat 			pfncn = pcrc;
    147  1.13   atatat 		}
    148   1.5  mycroft 	}
    149   1.5  mycroft 
    150  1.13   atatat 	while ((ch = getopt(argc, argv, "mo:ps:tx12456")) != -1)
    151   1.1      cgd 		switch(ch) {
    152  1.13   atatat 		case '2':
    153  1.13   atatat 			if (dosum) {
    154  1.13   atatat 				warnx("sum mutually exclusive with md2");
    155  1.13   atatat 				usage();
    156  1.13   atatat 			}
    157  1.13   atatat 			hash = &hashes[HASH_MD2];
    158  1.13   atatat 			break;
    159  1.13   atatat 		case '4':
    160  1.13   atatat 			if (dosum) {
    161  1.13   atatat 				warnx("sum mutually exclusive with md4");
    162  1.13   atatat 				usage();
    163  1.13   atatat 			}
    164  1.13   atatat 			hash = &hashes[HASH_MD4];
    165  1.13   atatat 			break;
    166   1.8  thorpej 		case 'm':
    167  1.13   atatat 		case '5':
    168   1.8  thorpej 			if (dosum) {
    169   1.8  thorpej 				warnx("sum mutually exclusive with md5");
    170   1.8  thorpej 				usage();
    171   1.8  thorpej 			}
    172  1.13   atatat 			hash = &hashes[HASH_MD5];
    173  1.13   atatat 			break;
    174  1.13   atatat 		case '1':
    175  1.13   atatat 			if (dosum) {
    176  1.13   atatat 				warnx("sum mutually exclusive with sha1");
    177  1.13   atatat 				usage();
    178  1.13   atatat 			}
    179  1.13   atatat 			hash = &hashes[HASH_SHA1];
    180  1.13   atatat 			break;
    181  1.13   atatat 		case '6':
    182  1.13   atatat 			if (dosum) {
    183  1.13   atatat 				warnx("sum mutually exclusive with rmd160");
    184  1.13   atatat 				usage();
    185  1.13   atatat 			}
    186  1.13   atatat 			hash = &hashes[HASH_RMD160];
    187   1.8  thorpej 			break;
    188   1.1      cgd 		case 'o':
    189  1.13   atatat 			if (hash) {
    190  1.13   atatat 				warnx("%s mutually exclusive with sum",
    191  1.13   atatat 				      hash->hashname);
    192   1.8  thorpej 				usage();
    193   1.8  thorpej 			}
    194   1.5  mycroft 			if (!strcmp(optarg, "1")) {
    195   1.1      cgd 				cfncn = csum1;
    196   1.1      cgd 				pfncn = psum1;
    197   1.5  mycroft 			} else if (!strcmp(optarg, "2")) {
    198   1.1      cgd 				cfncn = csum2;
    199   1.1      cgd 				pfncn = psum2;
    200   1.1      cgd 			} else {
    201   1.5  mycroft 				warnx("illegal argument to -o option");
    202   1.1      cgd 				usage();
    203   1.1      cgd 			}
    204   1.1      cgd 			break;
    205   1.8  thorpej 		case 'p':
    206  1.13   atatat 			if (hash == NULL)
    207  1.13   atatat 				requirehash("-p");
    208   1.8  thorpej 			pflag = 1;
    209   1.8  thorpej 			break;
    210   1.8  thorpej 		case 's':
    211  1.13   atatat 			if (hash == NULL)
    212  1.13   atatat 				requirehash("-s");
    213  1.13   atatat 			nohashstdin = 1;
    214  1.13   atatat 			hash->stringfunc(optarg);
    215   1.8  thorpej 			break;
    216   1.8  thorpej 		case 't':
    217  1.13   atatat 			if (hash == NULL)
    218  1.13   atatat 				requirehash("-t");
    219  1.13   atatat 			nohashstdin = 1;
    220  1.13   atatat 			hash->timetrialfunc();
    221   1.8  thorpej 			break;
    222   1.8  thorpej 		case 'x':
    223  1.13   atatat 			if (hash == NULL)
    224  1.13   atatat 				requirehash("-x");
    225  1.13   atatat 			nohashstdin = 1;
    226  1.13   atatat 			hash->testsuitefunc();
    227   1.8  thorpej 			break;
    228   1.1      cgd 		case '?':
    229   1.1      cgd 		default:
    230   1.1      cgd 			usage();
    231   1.1      cgd 		}
    232   1.1      cgd 	argc -= optind;
    233   1.1      cgd 	argv += optind;
    234   1.1      cgd 
    235   1.1      cgd 	fd = STDIN_FILENO;
    236   1.3      cgd 	fn = NULL;
    237   1.1      cgd 	rval = 0;
    238   1.1      cgd 	do {
    239   1.1      cgd 		if (*argv) {
    240   1.1      cgd 			fn = *argv++;
    241  1.13   atatat 			if (hash != NULL) {
    242  1.13   atatat 				if (hash_digest_file(fn, hash)) {
    243   1.8  thorpej 					warn("%s", fn);
    244   1.8  thorpej 					rval = 1;
    245   1.8  thorpej 				}
    246   1.8  thorpej 				continue;
    247   1.8  thorpej 			}
    248   1.1      cgd 			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
    249   1.5  mycroft 				warn("%s", fn);
    250   1.1      cgd 				rval = 1;
    251   1.1      cgd 				continue;
    252   1.1      cgd 			}
    253  1.13   atatat 		} else if (hash && !nohashstdin) {
    254  1.13   atatat 			hash->filterfunc(pflag);
    255  1.13   atatat 		}
    256   1.8  thorpej 
    257  1.13   atatat 		if (hash == NULL) {
    258   1.8  thorpej 			if (cfncn(fd, &val, &len)) {
    259   1.8  thorpej 				warn("%s", fn ? fn : "stdin");
    260   1.8  thorpej 				rval = 1;
    261   1.8  thorpej 			} else
    262   1.8  thorpej 				pfncn(fn, val, len);
    263   1.8  thorpej 			(void)close(fd);
    264   1.1      cgd 		}
    265   1.1      cgd 	} while (*argv);
    266   1.1      cgd 	exit(rval);
    267   1.1      cgd }
    268   1.1      cgd 
    269   1.8  thorpej int
    270  1.13   atatat hash_digest_file(fn, hash)
    271   1.8  thorpej 	char *fn;
    272  1.13   atatat 	struct hash *hash;
    273   1.8  thorpej {
    274  1.13   atatat 	char buf[41], *cp;
    275   1.8  thorpej 
    276  1.13   atatat 	cp = hash->filefunc(fn, buf);
    277   1.8  thorpej 	if (cp == NULL)
    278   1.8  thorpej 		return (1);
    279   1.8  thorpej 
    280  1.13   atatat 	printf("%s (%s) = %s\n", hash->hashname, fn, cp);
    281   1.8  thorpej 	return (0);
    282   1.8  thorpej }
    283   1.8  thorpej 
    284   1.8  thorpej void
    285  1.13   atatat requirehash(flg)
    286   1.8  thorpej 	const char *flg;
    287   1.8  thorpej {
    288  1.13   atatat 	warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
    289  1.13   atatat 	    flg);
    290   1.8  thorpej 	usage();
    291   1.8  thorpej }
    292   1.8  thorpej 
    293   1.3      cgd void
    294   1.1      cgd usage()
    295   1.1      cgd {
    296   1.5  mycroft 
    297   1.8  thorpej 	(void)fprintf(stderr, "usage: cksum [-m | [-o 1 | 2]] [file ...]\n");
    298   1.8  thorpej 	(void)fprintf(stderr, "       sum [file ...]\n");
    299   1.8  thorpej 	(void)fprintf(stderr,
    300  1.13   atatat 	    "       md2 [-p | -t | -x | -s string] [file ...]\n");
    301  1.13   atatat 	(void)fprintf(stderr,
    302  1.13   atatat 	    "       md4 [-p | -t | -x | -s string] [file ...]\n");
    303  1.13   atatat 	(void)fprintf(stderr,
    304   1.8  thorpej 	    "       md5 [-p | -t | -x | -s string] [file ...]\n");
    305  1.13   atatat 	(void)fprintf(stderr,
    306  1.13   atatat 	    "       sha1 [-p | -t | -x | -s string] [file ...]\n");
    307  1.13   atatat 	(void)fprintf(stderr,
    308  1.13   atatat 	    "       rmd160 [-p | -t | -x | -s string] [file ...]\n");
    309   1.1      cgd 	exit(1);
    310   1.1      cgd }
    311