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