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