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 | ||
18b3351a MW |
64 | static int checkhash(const gchash *gch, unsigned f, |
65 | const char *file, const encodeops *e) | |
e375fe33 | 66 | { |
67 | int rc; | |
f377eee1 MW |
68 | hfpctx hfp; |
69 | dstr dl = DSTR_INIT; | |
70 | dstr df = DSTR_INIT; | |
e375fe33 | 71 | unsigned long n = 0, nfail = 0; |
f377eee1 | 72 | int hf; |
e375fe33 | 73 | |
d7e6bc66 | 74 | if (!file || strcmp(file, "-") == 0) |
f377eee1 MW |
75 | hfp.fp = stdin; |
76 | else if ((hfp.fp = fopen(file, f & GSF_RAW ? "r" : "rb")) == 0) { | |
e375fe33 | 77 | moan("couldn't open `%s': %s", file, strerror(errno)); |
78 | return (EXIT_FAILURE); | |
79 | } | |
80 | ||
f377eee1 MW |
81 | hfp.dline = &dl; |
82 | hfp.dfile = &df; | |
83 | hfp.hbuf = xmalloc(2 * gch->hashsz); | |
84 | hfp.gch = gch; | |
85 | hfp.ee = e; | |
86 | hfp.f = f; | |
87 | ||
88 | while ((hf = hfparse(&hfp)) != HF_EOF) { | |
89 | switch (hf) { | |
90 | case HF_HASH: | |
91 | xfree(hfp.hbuf); | |
92 | hfp.hbuf = xmalloc(2 * hfp.gch->hashsz); | |
93 | break; | |
94 | case HF_FILE: | |
95 | if (fhash(hfp.gch, hfp.f, df.buf, hfp.hbuf + hfp.gch->hashsz)) { | |
96 | moan("couldn't read `%s': %s", df.buf, strerror(errno)); | |
97 | rc = EXIT_FAILURE; | |
5685a696 | 98 | continue; |
f377eee1 MW |
99 | } |
100 | if (memcmp(hfp.hbuf, hfp.hbuf + hfp.gch->hashsz, | |
101 | hfp.gch->hashsz) != 0) { | |
102 | if (hfp.f & f_verbose) | |
103 | fprintf(stderr, "FAIL %s\n", df.buf); | |
104 | else | |
105 | moan("%s check failed for `%s'", hfp.gch->name, df.buf); | |
106 | nfail++; | |
107 | rc = EXIT_FAILURE; | |
108 | } else { | |
109 | if (hfp.f & f_verbose) | |
110 | fprintf(stderr, "OK %s\n", df.buf); | |
111 | } | |
112 | n++; | |
e375fe33 | 113 | } |
e375fe33 | 114 | } |
115 | ||
f377eee1 MW |
116 | dstr_destroy(&dl); |
117 | dstr_destroy(&df); | |
118 | xfree(hfp.hbuf); | |
e375fe33 | 119 | if ((f & f_verbose) && nfail) |
f377eee1 | 120 | moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name); |
e375fe33 | 121 | else if (!n) |
122 | moan("no files checked"); | |
123 | return (0); | |
124 | } | |
125 | ||
18b3351a MW |
126 | static int dohash(const gchash *gch, unsigned f, |
127 | const char *file, const encodeops *e) | |
e375fe33 | 128 | { |
129 | int rc = 0; | |
130 | octet *p = xmalloc(gch->hashsz); | |
131 | ||
18b3351a | 132 | if (fhash(gch, f, file, p)) { |
e375fe33 | 133 | moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno)); |
134 | rc = EXIT_FAILURE; | |
135 | } else { | |
5685a696 | 136 | e->put(p, gch->hashsz, stdout); |
e375fe33 | 137 | if (file) { |
138 | fputc(' ', stdout); | |
18b3351a | 139 | fputc(f & FHF_BINARY ? '*' : ' ', stdout); |
e375fe33 | 140 | if (f & f_escape) |
141 | putstring(stdout, file, 0); | |
142 | else | |
143 | fputs(file, stdout); | |
144 | } | |
145 | fputc('\n', stdout); | |
146 | } | |
147 | ||
148 | xfree(p); | |
149 | return (rc); | |
150 | } | |
151 | ||
18b3351a MW |
152 | static int dofile(const gchash *gch, unsigned f, |
153 | const char *file, const encodeops *e) | |
154 | { return (f & f_check ? checkhash : dohash)(gch, f, file, e); } | |
12902a5c | 155 | |
18b3351a MW |
156 | static int hashfiles(const gchash *gch, unsigned f, |
157 | const char *file, const encodeops *e) | |
e375fe33 | 158 | { |
159 | FILE *fp; | |
160 | dstr d = DSTR_INIT; | |
161 | int rc = 0; | |
162 | int rrc; | |
163 | ||
d7e6bc66 | 164 | if (!file || strcmp(file, "-") == 0) |
e375fe33 | 165 | fp = stdin; |
18b3351a | 166 | else if ((fp = fopen(file, f & GSF_RAW ? "r" : "rb")) == 0) { |
e375fe33 | 167 | moan("couldn't open `%s': %s", file, strerror(errno)); |
168 | return (EXIT_FAILURE); | |
169 | } | |
170 | ||
171 | for (;;) { | |
172 | DRESET(&d); | |
18b3351a | 173 | if (getstring(fp, &d, GSF_FILE | f)) |
e375fe33 | 174 | break; |
18b3351a | 175 | if ((rrc = dofile(gch, f, d.buf, e)) != 0) |
e375fe33 | 176 | rc = rrc; |
177 | } | |
178 | ||
179 | return (rc); | |
180 | } | |
181 | ||
18b3351a MW |
182 | static int hashsum(const gchash *gch, unsigned f, |
183 | const char *file, const encodeops *e) | |
184 | { return (f & f_files ? hashfiles : dofile)(gch, f, 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 | { | |
237 | unsigned f = 0; | |
238 | const gchash *gch = 0; | |
c65df279 | 239 | const encodeops *e = &encodingtab[0]; |
e375fe33 | 240 | int rc; |
241 | ||
242 | /* --- Initialization --- */ | |
243 | ||
244 | ego(argv[0]); | |
245 | sub_init(); | |
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; | |
254 | gch = gethash(q); | |
255 | } | |
256 | if (!gch) | |
e9026a0a | 257 | 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': | |
292 | help(stdout, gch); | |
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': |
303 | if ((gch = gethash(optarg)) == 0) | |
304 | die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg); | |
305 | f |= f_oddhash; | |
306 | break; | |
5685a696 | 307 | case 'E': |
c65df279 | 308 | if ((e = getencoding(optarg)) == 0) |
5685a696 | 309 | die(EXIT_FAILURE, "unknown encoding `%s'", optarg); |
310 | f |= f_oddenc; | |
311 | break; | |
e375fe33 | 312 | case 'f': |
313 | f |= f_files; | |
314 | break; | |
315 | case '0': | |
18b3351a | 316 | f |= GSF_RAW; |
e375fe33 | 317 | break; |
318 | case 'e': | |
319 | f |= f_escape; | |
320 | break; | |
321 | case 'c': | |
322 | f |= f_check; | |
323 | break; | |
324 | case 'b': | |
18b3351a | 325 | f |= FHF_BINARY; |
e375fe33 | 326 | break; |
327 | case 'v': | |
328 | f |= f_verbose; | |
329 | break; | |
43d1332f | 330 | case 'p': |
18b3351a | 331 | f |= FHF_PROGRESS; |
43d1332f | 332 | break; |
e375fe33 | 333 | default: |
334 | f |= f_bogus; | |
335 | break; | |
336 | } | |
337 | } | |
338 | ||
339 | if (f & f_bogus) { | |
340 | usage(stderr); | |
341 | exit(EXIT_FAILURE); | |
342 | } | |
343 | argv += optind; | |
344 | argc -= optind; | |
345 | ||
346 | /* --- Generate output --- */ | |
347 | ||
db2d393e MW |
348 | if (!(f & f_check) && (argc || (f & f_files))) { |
349 | if (f & f_oddhash) printf("#hash %s\n", gch->name); | |
350 | if (f & f_oddenc) printf("#encoding %s\n", e->name); | |
351 | if (f & f_escape) fputs("#escape\n", stdout); | |
352 | } | |
92c494ce | 353 | if (!argc) |
18b3351a | 354 | rc = hashsum(gch, f, 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++) { | |
18b3351a | 361 | if ((rrc = hashsum(gch, f, argv[i], e)) != 0) |
e375fe33 | 362 | rc = rrc; |
363 | } | |
92c494ce | 364 | } |
e375fe33 | 365 | |
366 | return (rc); | |
367 | } | |
368 | ||
369 | /*----- That's all, folks -------------------------------------------------*/ |