1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/*
* Copyright (c) 1997 Robert Nordier
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static const char rcsid[] =
"$FreeBSD$";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <md5.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
extern int crc(int fd, uint32_t *cval, off_t *clen);
#define DISTMD5 1 /* MD5 format */
#define DISTINF 2 /* .inf format */
#define DISTTYPES 2 /* types supported */
#define E_UNKNOWN 1 /* Unknown format */
#define E_BADMD5 2 /* Invalid MD5 format */
#define E_BADINF 3 /* Invalid .inf format */
#define E_NAME 4 /* Can't derive component name */
#define E_LENGTH 5 /* Length mismatch */
#define E_CHKSUM 6 /* Checksum mismatch */
#define E_ERRNO 7 /* sys_errlist[errno] */
#define isfatal(err) ((err) && (err) <= E_NAME)
#define NAMESIZE 256 /* filename buffer size */
#define MDSUMLEN 32 /* length of MD5 message digest */
#define isstdin(path) ((path)[0] == '-' && !(path)[1])
static const char *opt_dir; /* where to look for components */
static const char *opt_name; /* name for accessing components */
static int opt_all; /* report on all components */
static int opt_ignore; /* ignore missing components */
static int opt_recurse; /* search directories recursively */
static int opt_silent; /* silent about inaccessible files */
static int opt_type; /* dist type: md5 or inf */
static int opt_exist; /* just verify existence */
static int ckdist(const char *path, int type);
static int chkmd5(FILE * fp, const char *path);
static int chkinf(FILE * fp, const char *path);
static int report(const char *path, const char *name, int error);
static const char *distname(const char *path, const char *name,
const char *ext);
static const char *stripath(const char *path);
static int distfile(const char *path);
static int disttype(const char *name);
static int fail(const char *path, const char *msg);
static void usage(void);
int
main(int argc, char *argv[])
{
static char *arg[2];
struct stat sb;
FTS *ftsp;
FTSENT *f;
int rval, c, type;
while ((c = getopt(argc, argv, "ad:in:rst:x")) != -1)
switch (c) {
case 'a':
opt_all = 1;
break;
case 'd':
opt_dir = optarg;
break;
case 'i':
opt_ignore = 1;
break;
case 'n':
opt_name = optarg;
break;
case 'r':
opt_recurse = 1;
break;
case 's':
opt_silent = 1;
break;
case 't':
if ((opt_type = disttype(optarg)) == 0) {
warnx("illegal argument to -t option");
usage();
}
break;
case 'x':
opt_exist = 1;
break;
default:
usage();
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
if (opt_dir) {
if (stat(opt_dir, &sb))
err(2, "%s", opt_dir);
if (!S_ISDIR(sb.st_mode))
errx(2, "%s: not a directory", opt_dir);
}
rval = 0;
do {
if (isstdin(*argv))
rval |= ckdist(*argv, opt_type);
else if (stat(*argv, &sb))
rval |= fail(*argv, NULL);
else if (S_ISREG(sb.st_mode))
rval |= ckdist(*argv, opt_type);
else {
arg[0] = *argv;
if ((ftsp = fts_open(arg, FTS_LOGICAL, NULL)) == NULL)
err(2, "fts_open");
while ((f = fts_read(ftsp)) != NULL)
switch (f->fts_info) {
case FTS_DC:
rval = fail(f->fts_path, "Directory causes a cycle");
break;
case FTS_DNR:
case FTS_ERR:
case FTS_NS:
rval = fail(f->fts_path, sys_errlist[f->fts_errno]);
break;
case FTS_D:
if (!opt_recurse && f->fts_level > FTS_ROOTLEVEL &&
fts_set(ftsp, f, FTS_SKIP))
err(2, "fts_set");
break;
case FTS_F:
if ((type = distfile(f->fts_name)) != 0 &&
(!opt_type || type == opt_type))
rval |= ckdist(f->fts_path, type);
break;
default: ;
}
if (errno)
err(2, "fts_read");
if (fts_close(ftsp))
err(2, "fts_close");
}
} while (*++argv);
return rval;
}
static int
ckdist(const char *path, int type)
{
FILE *fp;
int rval, c;
if (isstdin(path)) {
path = "(stdin)";
fp = stdin;
} else if ((fp = fopen(path, "r")) == NULL)
return fail(path, NULL);
if (!type) {
if (fp != stdin)
type = distfile(path);
if (!type)
if ((c = fgetc(fp)) != EOF) {
type = c == 'M' ? DISTMD5 : c == 'P' ? DISTINF : 0;
(void)ungetc(c, fp);
}
}
switch (type) {
case DISTMD5:
rval = chkmd5(fp, path);
break;
case DISTINF:
rval = chkinf(fp, path);
break;
default:
rval = report(path, NULL, E_UNKNOWN);
}
if (ferror(fp))
warn("%s", path);
if (fp != stdin && fclose(fp))
err(2, "%s", path);
return rval;
}
static int
chkmd5(FILE * fp, const char *path)
{
char buf[298]; /* "MD5 (NAMESIZE = MDSUMLEN" */
char name[NAMESIZE + 1];
char sum[MDSUMLEN + 1], chk[MDSUMLEN + 1];
const char *dname;
char *s;
int rval, error, c, fd;
char ch;
rval = 0;
while (fgets(buf, sizeof(buf), fp)) {
dname = NULL;
error = 0;
if (((c = sscanf(buf, "MD5 (%256s = %32s%c", name, sum,
&ch)) != 3 && (!feof(fp) || c != 2)) ||
(c == 3 && ch != '\n') ||
(s = strrchr(name, ')')) == NULL ||
strlen(sum) != MDSUMLEN)
error = E_BADMD5;
else {
*s = 0;
if ((dname = distname(path, name, NULL)) == NULL)
error = E_NAME;
else if (opt_exist) {
if ((fd = open(dname, O_RDONLY)) == -1)
error = E_ERRNO;
else if (close(fd))
err(2, "%s", dname);
} else if (!MD5File(dname, chk))
error = E_ERRNO;
else if (strcmp(chk, sum))
error = E_CHKSUM;
}
if (opt_ignore && error == E_ERRNO && errno == ENOENT)
continue;
if (error || opt_all)
rval |= report(path, dname, error);
if (isfatal(error))
break;
}
return rval;
}
static int
chkinf(FILE * fp, const char *path)
{
char buf[30]; /* "cksum.2 = 10 6" */
char ext[3];
struct stat sb;
const char *dname;
off_t len;
u_long sum;
intmax_t sumlen;
uint32_t chk;
int rval, error, c, pieces, cnt, fd;
char ch;
rval = 0;
for (cnt = -1; fgets(buf, sizeof(buf), fp); cnt++) {
fd = -1;
dname = NULL;
error = 0;
if (cnt == -1) {
if ((c = sscanf(buf, "Pieces = %d%c", &pieces, &ch)) != 2 ||
ch != '\n' || pieces < 1)
error = E_BADINF;
} else if (((c = sscanf(buf, "cksum.%2s = %lu %jd%c", ext, &sum,
&sumlen, &ch)) != 4 &&
(!feof(fp) || c != 3)) || (c == 4 && ch != '\n') ||
ext[0] != 'a' + cnt / 26 || ext[1] != 'a' + cnt % 26)
error = E_BADINF;
else if ((dname = distname(fp == stdin ? NULL : path, NULL,
ext)) == NULL)
error = E_NAME;
else if ((fd = open(dname, O_RDONLY)) == -1)
error = E_ERRNO;
else if (fstat(fd, &sb))
error = E_ERRNO;
else if (sb.st_size != (off_t)sumlen)
error = E_LENGTH;
else if (!opt_exist) {
if (crc(fd, &chk, &len))
error = E_ERRNO;
else if (chk != sum)
error = E_CHKSUM;
}
if (fd != -1 && close(fd))
err(2, "%s", dname);
if (opt_ignore && error == E_ERRNO && errno == ENOENT)
continue;
if (error || (opt_all && cnt >= 0))
rval |= report(path, dname, error);
if (isfatal(error))
break;
}
return rval;
}
static int
report(const char *path, const char *name, int error)
{
if (name)
name = stripath(name);
switch (error) {
case E_UNKNOWN:
printf("%s: Unknown format\n", path);
break;
case E_BADMD5:
printf("%s: Invalid MD5 format\n", path);
break;
case E_BADINF:
printf("%s: Invalid .inf format\n", path);
break;
case E_NAME:
printf("%s: Can't derive component name\n", path);
break;
case E_LENGTH:
printf("%s: %s: Size mismatch\n", path, name);
break;
case E_CHKSUM:
printf("%s: %s: Checksum mismatch\n", path, name);
break;
case E_ERRNO:
printf("%s: %s: %s\n", path, name, sys_errlist[errno]);
break;
default:
printf("%s: %s: OK\n", path, name);
}
return error != 0;
}
static const char *
distname(const char *path, const char *name, const char *ext)
{
static char buf[NAMESIZE];
size_t plen, nlen;
char *s;
if (opt_name)
name = opt_name;
else if (!name) {
if (!path)
return NULL;
name = stripath(path);
}
nlen = strlen(name);
if (ext && nlen > 4 && name[nlen - 4] == '.' &&
disttype(name + nlen - 3) == DISTINF)
nlen -= 4;
if (opt_dir) {
path = opt_dir;
plen = strlen(path);
} else
plen = path && (s = strrchr(path, '/')) != NULL ?
(size_t)(s - path) : 0;
if (plen + (plen > 0) + nlen + (ext ? 3 : 0) >= sizeof(buf))
return NULL;
s = buf;
if (plen) {
memcpy(s, path, plen);
s += plen;
*s++ = '/';
}
memcpy(s, name, nlen);
s += nlen;
if (ext) {
*s++ = '.';
memcpy(s, ext, 2);
s += 2;
}
*s = 0;
return buf;
}
static const char *
stripath(const char *path)
{
const char *s;
return ((s = strrchr(path, '/')) != NULL && s[1] ?
s + 1 : path);
}
static int
distfile(const char *path)
{
const char *s;
int type;
if ((type = disttype(path)) == DISTMD5 ||
((s = strrchr(path, '.')) != NULL && s > path &&
(type = disttype(s + 1)) != 0))
return type;
return 0;
}
static int
disttype(const char *name)
{
static const char dname[DISTTYPES][4] = {"md5", "inf"};
int i;
for (i = 0; i < DISTTYPES; i++)
if (!strcmp(dname[i], name))
return 1 + i;
return 0;
}
static int
fail(const char *path, const char *msg)
{
if (opt_silent)
return 0;
warnx("%s: %s", path, msg ? msg : sys_errlist[errno]);
return 2;
}
static void
usage(void)
{
fprintf(stderr,
"usage: ckdist [-airsx] [-d dir] [-n name] [-t type] file ...\n");
exit(2);
}
|