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