Commit | Line | Data |
---|---|---|
e375fe33 | 1 | /* -*-c-*- |
e375fe33 | 2 | * |
3 | * Hash files using some secure hash function | |
4 | * | |
5 | * (c) 2000 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
e375fe33 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
12 | * Catacomb is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
45c0fd36 | 16 | * |
e375fe33 | 17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
45c0fd36 | 21 | * |
e375fe33 | 22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
e375fe33 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
43d1332f MW |
30 | #define _FILE_OFFSET_BITS 64 |
31 | ||
e375fe33 | 32 | #include "config.h" |
33 | ||
5685a696 | 34 | #include <assert.h> |
e375fe33 | 35 | #include <errno.h> |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | ||
40 | #include <mLib/alloc.h> | |
41 | #include <mLib/dstr.h> | |
42 | #include <mLib/mdwopt.h> | |
43 | #include <mLib/quis.h> | |
44 | #include <mLib/report.h> | |
45 | #include <mLib/sub.h> | |
e375fe33 | 46 | |
47 | #include "ghash.h" | |
c65df279 | 48 | #include "cc.h" |
e375fe33 | 49 | |
e375fe33 | 50 | /*----- Static variables --------------------------------------------------*/ |
51 | ||
18b3351a MW |
52 | #define f_bogus 1u |
53 | #define f_verbose 2u | |
54 | #define f_check 4u | |
55 | #define f_files 8u | |
56 | #define f_oddhash 16u | |
57 | #define f_escape 32u | |
58 | #define f_oddenc 64u | |
e375fe33 | 59 | |
e375fe33 | 60 | /*----- Guts --------------------------------------------------------------*/ |
61 | ||
f5e91c02 MW |
62 | static int checkjunk(const char *path, const struct stat *st, void *p) |
63 | { | |
64 | const char *what; | |
65 | fhashstate *fh = p; | |
66 | ||
67 | if (!st) { | |
68 | if (fh->f & f_verbose) | |
69 | fprintf(stderr, "JUNK (error %s) %s\n", strerror(errno), path); | |
70 | else | |
71 | moan("couldn't stat junk file `%s': %s", path, strerror(errno)); | |
72 | } else { | |
73 | what = describefile(st); | |
74 | if (fh->f & f_verbose) | |
75 | fprintf(stderr, "JUNK %s %s\n", what, path); | |
76 | else | |
77 | moan("found junk %s `%s'", what, path); | |
78 | } | |
79 | return (0); | |
80 | } | |
81 | ||
82 | static int warnjunk(const char *path, const struct stat *st, void *p) | |
83 | { | |
84 | if (st) | |
85 | moan("unexpected %s `%s'", describefile(st), path); | |
86 | else | |
87 | moan("couldn't stat unexpected file `%s': %s", path, strerror(errno)); | |
88 | return (0); | |
89 | } | |
90 | ||
07290a45 | 91 | static int checkhash(fhashstate *fh, const char *file, const encodeops *e) |
e375fe33 | 92 | { |
93 | int rc; | |
f377eee1 MW |
94 | hfpctx hfp; |
95 | dstr dl = DSTR_INIT; | |
96 | dstr df = DSTR_INIT; | |
e375fe33 | 97 | unsigned long n = 0, nfail = 0; |
f377eee1 | 98 | int hf; |
e375fe33 | 99 | |
d7e6bc66 | 100 | if (!file || strcmp(file, "-") == 0) |
f377eee1 | 101 | hfp.fp = stdin; |
07290a45 | 102 | else if ((hfp.fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) { |
e375fe33 | 103 | moan("couldn't open `%s': %s", file, strerror(errno)); |
104 | return (EXIT_FAILURE); | |
105 | } | |
106 | ||
f377eee1 MW |
107 | hfp.dline = &dl; |
108 | hfp.dfile = &df; | |
07290a45 MW |
109 | hfp.hbuf = xmalloc(2 * fh->gch->hashsz); |
110 | hfp.gch = fh->gch; | |
f377eee1 | 111 | hfp.ee = e; |
07290a45 | 112 | hfp.f = fh->f; |
f377eee1 MW |
113 | |
114 | while ((hf = hfparse(&hfp)) != HF_EOF) { | |
115 | switch (hf) { | |
116 | case HF_HASH: | |
117 | xfree(hfp.hbuf); | |
118 | hfp.hbuf = xmalloc(2 * hfp.gch->hashsz); | |
119 | break; | |
120 | case HF_FILE: | |
07290a45 | 121 | if (fhash(fh, df.buf, hfp.hbuf + hfp.gch->hashsz)) { |
f377eee1 MW |
122 | moan("couldn't read `%s': %s", df.buf, strerror(errno)); |
123 | rc = EXIT_FAILURE; | |
5685a696 | 124 | continue; |
f377eee1 MW |
125 | } |
126 | if (memcmp(hfp.hbuf, hfp.hbuf + hfp.gch->hashsz, | |
127 | hfp.gch->hashsz) != 0) { | |
128 | if (hfp.f & f_verbose) | |
129 | fprintf(stderr, "FAIL %s\n", df.buf); | |
130 | else | |
131 | moan("%s check failed for `%s'", hfp.gch->name, df.buf); | |
132 | nfail++; | |
133 | rc = EXIT_FAILURE; | |
134 | } else { | |
135 | if (hfp.f & f_verbose) | |
136 | fprintf(stderr, "OK %s\n", df.buf); | |
137 | } | |
138 | n++; | |
e375fe33 | 139 | } |
e375fe33 | 140 | } |
141 | ||
95d92463 MW |
142 | if (ferror(hfp.fp)) { |
143 | moan("error reading input `%s': %s", | |
144 | file ? file : "<stdin>", strerror(errno)); | |
145 | rc = EXIT_FAILURE; | |
146 | } | |
f377eee1 MW |
147 | dstr_destroy(&dl); |
148 | dstr_destroy(&df); | |
149 | xfree(hfp.hbuf); | |
07290a45 | 150 | if ((fh->f & f_verbose) && nfail) |
f377eee1 | 151 | moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name); |
e375fe33 | 152 | else if (!n) |
153 | moan("no files checked"); | |
df8e52c7 | 154 | return (rc); |
e375fe33 | 155 | } |
156 | ||
07290a45 | 157 | static int dohash(fhashstate *fh, const char *file, const encodeops *e) |
e375fe33 | 158 | { |
159 | int rc = 0; | |
07290a45 | 160 | octet *p = xmalloc(fh->gch->hashsz); |
e375fe33 | 161 | |
07290a45 | 162 | if (fhash(fh, file, p)) { |
e375fe33 | 163 | moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno)); |
164 | rc = EXIT_FAILURE; | |
165 | } else { | |
07290a45 | 166 | e->put(p, fh->gch->hashsz, stdout); |
e375fe33 | 167 | if (file) { |
168 | fputc(' ', stdout); | |
07290a45 MW |
169 | fputc(fh->f & FHF_BINARY ? '*' : ' ', stdout); |
170 | if (fh->f & f_escape) | |
e375fe33 | 171 | putstring(stdout, file, 0); |
172 | else | |
173 | fputs(file, stdout); | |
174 | } | |
175 | fputc('\n', stdout); | |
176 | } | |
177 | ||
178 | xfree(p); | |
179 | return (rc); | |
180 | } | |
181 | ||
07290a45 MW |
182 | static int dofile(fhashstate *fh, const char *file, const encodeops *e) |
183 | { return (fh->f & f_check ? checkhash : dohash)(fh, file, e); } | |
12902a5c | 184 | |
07290a45 | 185 | static int hashfiles(fhashstate *fh, const char *file, const encodeops *e) |
e375fe33 | 186 | { |
187 | FILE *fp; | |
188 | dstr d = DSTR_INIT; | |
189 | int rc = 0; | |
190 | int rrc; | |
191 | ||
d7e6bc66 | 192 | if (!file || strcmp(file, "-") == 0) |
e375fe33 | 193 | fp = stdin; |
07290a45 | 194 | else if ((fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) { |
e375fe33 | 195 | moan("couldn't open `%s': %s", file, strerror(errno)); |
196 | return (EXIT_FAILURE); | |
197 | } | |
198 | ||
199 | for (;;) { | |
200 | DRESET(&d); | |
07290a45 | 201 | if (getstring(fp, &d, GSF_FILE | fh->f)) |
e375fe33 | 202 | break; |
07290a45 | 203 | if ((rrc = dofile(fh, d.buf, e)) != 0) |
e375fe33 | 204 | rc = rrc; |
205 | } | |
206 | ||
207 | return (rc); | |
208 | } | |
209 | ||
07290a45 MW |
210 | static int hashsum(fhashstate *fh, const char *file, const encodeops *e) |
211 | { return (fh->f & f_files ? hashfiles : dofile)(fh, file, e); } | |
e375fe33 | 212 | |
213 | /*----- Main driver -------------------------------------------------------*/ | |
214 | ||
c65df279 | 215 | void version(FILE *fp) |
e375fe33 | 216 | { |
217 | pquis(fp, "$, Catacomb version " VERSION "\n"); | |
218 | } | |
219 | ||
220 | static void usage(FILE *fp) | |
221 | { | |
c65df279 | 222 | pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n"); |
e375fe33 | 223 | } |
224 | ||
225 | static void help(FILE *fp, const gchash *gch) | |
226 | { | |
227 | version(fp); | |
228 | fputc('\n', fp); | |
229 | usage(fp); | |
230 | pquis(fp, "\n\ | |
231 | Generates or checks message digests on files. Options available:\n\ | |
232 | \n\ | |
233 | -h, --help Display this help message.\n\ | |
234 | -V, --version Display program's version number.\n\ | |
235 | -u, --usage Display a terse usage message.\n\ | |
c65df279 | 236 | -l, --list [ITEM...] Show known hash functions and/or encodings.\n\ |
e375fe33 | 237 | \n\ |
238 | -a, --algorithm=ALG Use the message digest algorithm ALG.\n\ | |
92c494ce | 239 | -E, --encoding=ENC Represent hashes using encoding ENC.\n\ |
e375fe33 | 240 | \n\ |
241 | -f, --files Read a list of file names from standard input.\n\ | |
242 | -0, --null File names are null terminated, not plain text.\n\ | |
243 | \n\ | |
244 | -e, --escape Escape funny characters in filenames.\n\ | |
245 | -c, --check Check message digests rather than emitting them.\n\ | |
246 | -b, --binary When reading files, treat them as binary.\n\ | |
b2776fdf | 247 | -p, --progress Show a progress indicator for large files.\n\ |
e375fe33 | 248 | -v, --verbose Be verbose when checking digests.\n\ |
249 | \n\ | |
92c494ce | 250 | For a list of hashing algorithms and encodings, type `$ --list'.\n\ |
e375fe33 | 251 | "); |
252 | if (gch) | |
253 | fprintf(fp, "The default message digest algorithm is %s.\n", gch->name); | |
254 | } | |
255 | ||
c65df279 | 256 | #define LISTS(LI) \ |
257 | LI("Lists", list, listtab[i].name, listtab[i].name) \ | |
258 | LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \ | |
259 | LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name) | |
260 | ||
261 | MAKELISTTAB(listtab, LISTS) | |
262 | ||
e375fe33 | 263 | int main(int argc, char *argv[]) |
264 | { | |
07290a45 | 265 | fhashstate fh; |
95d92463 | 266 | const encodeops *e = &encodingtab[ENC_HEX]; |
e375fe33 | 267 | int rc; |
268 | ||
269 | /* --- Initialization --- */ | |
270 | ||
271 | ego(argv[0]); | |
272 | sub_init(); | |
07290a45 | 273 | fhash_init(&fh, 0, 0); |
e375fe33 | 274 | |
275 | /* --- Choose a hash function from the name --- */ | |
276 | ||
277 | { | |
278 | char *q = xstrdup(QUIS); | |
279 | size_t len = strlen(q); | |
280 | if (len > 3 && strcmp(q + len - 3, "sum") == 0) { | |
281 | q[len - 3] = 0; | |
07290a45 | 282 | fh.gch = gethash(q); |
e375fe33 | 283 | } |
07290a45 MW |
284 | if (!fh.gch) |
285 | fh.gch = gethash("md5"); | |
e375fe33 | 286 | xfree(q); |
287 | } | |
288 | ||
289 | /* --- Read options --- */ | |
290 | ||
291 | for (;;) { | |
292 | static struct option opts[] = { | |
293 | { "help", 0, 0, 'h' }, | |
294 | { "verbose", 0, 0, 'V' }, | |
295 | { "usage", 0, 0, 'u' }, | |
296 | ||
297 | { "algorithm", OPTF_ARGREQ, 0, 'a' }, | |
298 | { "hash", OPTF_ARGREQ, 0, 'a' }, | |
5685a696 | 299 | { "encoding", OPTF_ARGREQ, 0, 'E' }, |
e375fe33 | 300 | { "list", 0, 0, 'l' }, |
301 | ||
302 | { "files", 0, 0, 'f' }, | |
303 | { "find", 0, 0, 'f' }, | |
304 | { "null", 0, 0, '0' }, | |
305 | ||
306 | { "escape", 0, 0, 'e' }, | |
307 | { "check", 0, 0, 'c' }, | |
f5e91c02 | 308 | { "junk", 0, 0, 'j' }, |
e375fe33 | 309 | { "binary", 0, 0, 'b' }, |
310 | { "verbose", 0, 0, 'v' }, | |
43d1332f | 311 | { "progress", 0, 0, 'p' }, |
e375fe33 | 312 | |
313 | { 0, 0, 0, 0 } | |
314 | }; | |
f5e91c02 | 315 | int i = mdwopt(argc, argv, "hVu a:E:l f0 ecjbvp", opts, 0, 0, 0); |
e375fe33 | 316 | if (i < 0) |
317 | break; | |
318 | ||
319 | switch (i) { | |
320 | case 'h': | |
07290a45 | 321 | help(stdout, fh.gch); |
e375fe33 | 322 | exit(0); |
323 | case 'V': | |
324 | version(stdout); | |
325 | exit(0); | |
326 | case 'u': | |
327 | usage(stdout); | |
328 | exit(0); | |
c65df279 | 329 | case 'l': |
330 | exit(displaylists(listtab, argv + optind)); | |
e375fe33 | 331 | case 'a': |
07290a45 | 332 | if ((fh.gch = gethash(optarg)) == 0) |
e375fe33 | 333 | die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg); |
07290a45 | 334 | fh.f |= f_oddhash; |
e375fe33 | 335 | break; |
5685a696 | 336 | case 'E': |
c65df279 | 337 | if ((e = getencoding(optarg)) == 0) |
5685a696 | 338 | die(EXIT_FAILURE, "unknown encoding `%s'", optarg); |
07290a45 | 339 | fh.f |= f_oddenc; |
5685a696 | 340 | break; |
e375fe33 | 341 | case 'f': |
07290a45 | 342 | fh.f |= f_files; |
e375fe33 | 343 | break; |
344 | case '0': | |
07290a45 | 345 | fh.f |= GSF_RAW; |
e375fe33 | 346 | break; |
347 | case 'e': | |
07290a45 | 348 | fh.f |= f_escape; |
e375fe33 | 349 | break; |
350 | case 'c': | |
07290a45 | 351 | fh.f |= f_check; |
e375fe33 | 352 | break; |
f5e91c02 MW |
353 | case 'j': |
354 | fh.f |= FHF_JUNK; | |
355 | break; | |
e375fe33 | 356 | case 'b': |
07290a45 | 357 | fh.f |= FHF_BINARY; |
e375fe33 | 358 | break; |
359 | case 'v': | |
07290a45 | 360 | fh.f |= f_verbose; |
e375fe33 | 361 | break; |
43d1332f | 362 | case 'p': |
07290a45 | 363 | fh.f |= FHF_PROGRESS; |
43d1332f | 364 | break; |
e375fe33 | 365 | default: |
07290a45 | 366 | fh.f |= f_bogus; |
e375fe33 | 367 | break; |
368 | } | |
369 | } | |
370 | ||
07290a45 | 371 | if (fh.f & f_bogus) { |
e375fe33 | 372 | usage(stderr); |
373 | exit(EXIT_FAILURE); | |
374 | } | |
375 | argv += optind; | |
376 | argc -= optind; | |
377 | ||
378 | /* --- Generate output --- */ | |
379 | ||
07290a45 MW |
380 | if (!(fh.f & f_check) && (argc || (fh.f & f_files))) { |
381 | if (fh.f & f_oddhash) printf("#hash %s\n", fh.gch->name); | |
382 | if (fh.f & f_oddenc) printf("#encoding %s\n", e->name); | |
383 | if (fh.f & f_escape) fputs("#escape\n", stdout); | |
db2d393e | 384 | } |
92c494ce | 385 | if (!argc) |
07290a45 | 386 | rc = hashsum(&fh, 0, e); |
92c494ce | 387 | else { |
e375fe33 | 388 | int i; |
389 | int rrc; | |
92c494ce | 390 | |
e375fe33 | 391 | rc = 0; |
392 | for (i = 0; i < argc; i++) { | |
07290a45 | 393 | if ((rrc = hashsum(&fh, argv[i], e)) != 0) |
e375fe33 | 394 | rc = rrc; |
395 | } | |
92c494ce | 396 | } |
e375fe33 | 397 | |
f5e91c02 MW |
398 | if (fh.f & FHF_JUNK) { |
399 | if (fh.f & f_check) { | |
400 | if (fhash_junk(&fh, checkjunk, &fh)) rc = EXIT_FAILURE; | |
401 | } else { | |
402 | if (fhash_junk(&fh, warnjunk, 0) < 0) rc = EXIT_FAILURE; | |
403 | } | |
404 | } | |
405 | fhash_free(&fh); | |
406 | ||
e375fe33 | 407 | return (rc); |
408 | } | |
409 | ||
410 | /*----- That's all, folks -------------------------------------------------*/ |