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