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