cksum.c revision 1.25 1 /* $NetBSD: cksum.c,v 1.25 2005/08/21 18:51:44 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.25 2005/08/21 18:51:44 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 <rmd160.h>
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <string.h>
102 #include <unistd.h>
103
104 #include "extern.h"
105
106 #define HASH_MD2 0
107 #define HASH_MD4 1
108 #define HASH_MD5 2
109 #define HASH_SHA1 3
110 #define HASH_RMD160 4
111
112 typedef char *(*_filefunc)(const char *, char *);
113
114 struct hash {
115 const char *progname;
116 const char *hashname;
117 void (*stringfunc)(const char *);
118 void (*timetrialfunc)(void);
119 void (*testsuitefunc)(void);
120 void (*filterfunc)(int);
121 char *(*filefunc)(const char *, char *);
122 } hashes[] = {
123 { "md2", "MD2",
124 MD2String, MD2TimeTrial, MD2TestSuite,
125 MD2Filter, MD2File },
126 { "md4", "MD4",
127 MD4String, MD4TimeTrial, MD4TestSuite,
128 MD4Filter, MD4File },
129 { "md5", "MD5",
130 MD5String, MD5TimeTrial, MD5TestSuite,
131 MD5Filter, MD5File },
132 { "sha1", "SHA1",
133 SHA1String, SHA1TimeTrial, SHA1TestSuite,
134 SHA1Filter, (_filefunc) SHA1File },
135 { "rmd160", "RMD160",
136 RMD160String, RMD160TimeTrial, RMD160TestSuite,
137 RMD160Filter, (_filefunc) RMD160File },
138 { NULL }
139 };
140
141 int hash_digest_file(char *, struct hash *, int);
142 void requirehash(const char *);
143 void usage(void);
144
145 int
146 main(int argc, char **argv)
147 {
148 int ch, fd, rval, dosum, pflag, nohashstdin;
149 u_int32_t val;
150 off_t len;
151 char *fn;
152 const char *progname;
153 int (*cfncn) (int, u_int32_t *, off_t *);
154 void (*pfncn) (char *, u_int32_t, off_t);
155 struct hash *hash;
156 int normal, i;
157
158 cfncn = NULL;
159 pfncn = NULL;
160 dosum = pflag = nohashstdin = 0;
161 normal = 0;
162
163 setlocale(LC_ALL, "");
164
165 progname = getprogname();
166
167 for (hash = hashes; hash->hashname != NULL; hash++)
168 if (strcmp(progname, hash->progname) == 0)
169 break;
170
171 if (hash->hashname == NULL) {
172 hash = NULL;
173
174 if (!strcmp(progname, "sum")) {
175 dosum = 1;
176 cfncn = csum1;
177 pfncn = psum1;
178 } else {
179 cfncn = crc;
180 pfncn = pcrc;
181 }
182 }
183
184 while ((ch = getopt(argc, argv, "a:mno:ps:tx12456")) != -1)
185 switch(ch) {
186 case 'a':
187 if (hash != NULL || dosum) {
188 warnx("illegal use of -a option\n");
189 usage();
190 }
191 i = 0;
192 while (hashes[i].hashname != NULL) {
193 if (!strcasecmp(hashes[i].hashname, optarg)) {
194 hash = &hashes[i];
195 break;
196 }
197 i++;
198 }
199 if (hash == NULL) {
200 if (!strcasecmp(optarg, "old1")) {
201 cfncn = csum1;
202 pfncn = psum1;
203 } else if (!strcasecmp(optarg, "old2")) {
204 cfncn = csum2;
205 pfncn = psum2;
206 } else if (!strcasecmp(optarg, "crc")) {
207 cfncn = crc;
208 pfncn = pcrc;
209 } else {
210 warnx("illegal argument to -a option");
211 usage();
212 }
213 }
214 break;
215 case '2':
216 if (dosum) {
217 warnx("sum mutually exclusive with md2");
218 usage();
219 }
220 hash = &hashes[HASH_MD2];
221 break;
222 case '4':
223 if (dosum) {
224 warnx("sum mutually exclusive with md4");
225 usage();
226 }
227 hash = &hashes[HASH_MD4];
228 break;
229 case 'm':
230 case '5':
231 if (dosum) {
232 warnx("sum mutually exclusive with md5");
233 usage();
234 }
235 hash = &hashes[HASH_MD5];
236 break;
237 case '1':
238 if (dosum) {
239 warnx("sum mutually exclusive with sha1");
240 usage();
241 }
242 hash = &hashes[HASH_SHA1];
243 break;
244 case '6':
245 if (dosum) {
246 warnx("sum mutually exclusive with rmd160");
247 usage();
248 }
249 hash = &hashes[HASH_RMD160];
250 break;
251 case 'n':
252 normal = 1;
253 break;
254 case 'o':
255 if (hash) {
256 warnx("%s mutually exclusive with sum",
257 hash->hashname);
258 usage();
259 }
260 if (!strcmp(optarg, "1")) {
261 cfncn = csum1;
262 pfncn = psum1;
263 } else if (!strcmp(optarg, "2")) {
264 cfncn = csum2;
265 pfncn = psum2;
266 } else {
267 warnx("illegal argument to -o option");
268 usage();
269 }
270 break;
271 case 'p':
272 if (hash == NULL)
273 requirehash("-p");
274 pflag = 1;
275 break;
276 case 's':
277 if (hash == NULL)
278 requirehash("-s");
279 nohashstdin = 1;
280 hash->stringfunc(optarg);
281 break;
282 case 't':
283 if (hash == NULL)
284 requirehash("-t");
285 nohashstdin = 1;
286 hash->timetrialfunc();
287 break;
288 case 'x':
289 if (hash == NULL)
290 requirehash("-x");
291 nohashstdin = 1;
292 hash->testsuitefunc();
293 break;
294 case '?':
295 default:
296 usage();
297 }
298 argc -= optind;
299 argv += optind;
300
301 fd = STDIN_FILENO;
302 fn = NULL;
303 rval = 0;
304 do {
305 if (*argv) {
306 fn = *argv++;
307 if (hash != NULL) {
308 if (hash_digest_file(fn, hash, normal)) {
309 warn("%s", fn);
310 rval = 1;
311 }
312 continue;
313 }
314 if ((fd = open(fn, O_RDONLY, 0)) < 0) {
315 warn("%s", fn);
316 rval = 1;
317 continue;
318 }
319 } else if (hash && !nohashstdin) {
320 hash->filterfunc(pflag);
321 }
322
323 if (hash == NULL) {
324 if (cfncn(fd, &val, &len)) {
325 warn("%s", fn ? fn : "stdin");
326 rval = 1;
327 } else
328 pfncn(fn, val, len);
329 (void)close(fd);
330 }
331 } while (*argv);
332 exit(rval);
333 }
334
335 int
336 hash_digest_file(char *fn, struct hash *hash, int normal)
337 {
338 char buf[41], *cp;
339
340 cp = hash->filefunc(fn, buf);
341 if (cp == NULL)
342 return 1;
343
344 if (normal)
345 printf("%s %s\n", cp, fn);
346 else
347 printf("%s (%s) = %s\n", hash->hashname, fn, cp);
348 return 0;
349 }
350
351 void
352 requirehash(const char *flg)
353 {
354 warnx("%s flag requires `md2', `md4', `md5', `sha1', or `rmd160'",
355 flg);
356 usage();
357 }
358
359 void
360 usage(void)
361 {
362
363 (void)fprintf(stderr, "usage: cksum [-n] [-m | -1 | -2 | -4 | -5 | -6 | [-o 1 | 2]] [file ...]\n");
364 (void)fprintf(stderr, " sum [file ...]\n");
365 (void)fprintf(stderr,
366 " md2 [-n] [-p | -t | -x | -s string] [file ...]\n");
367 (void)fprintf(stderr,
368 " md4 [-n] [-p | -t | -x | -s string] [file ...]\n");
369 (void)fprintf(stderr,
370 " md5 [-n] [-p | -t | -x | -s string] [file ...]\n");
371 (void)fprintf(stderr,
372 " sha1 [-n] [-p | -t | -x | -s string] [file ...]\n");
373 (void)fprintf(stderr,
374 " rmd160 [-n] [-p | -t | -x | -s string] [file ...]\n");
375 exit(1);
376 }
377