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