2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997 Robert Nordier
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: head/usr.sbin/ckdist/ckdist.c 326276 2017-11-27 15:37:16Z pfg $
32 #include <sys/types.h>
44 #include <openssl/md5.h>
46 extern int crc(int fd, uint32_t *cval, off_t *clen);
48 #define DISTMD5 1 /* MD5 format */
49 #define DISTINF 2 /* .inf format */
50 #define DISTTYPES 2 /* types supported */
52 #define E_UNKNOWN 1 /* Unknown format */
53 #define E_BADMD5 2 /* Invalid MD5 format */
54 #define E_BADINF 3 /* Invalid .inf format */
55 #define E_NAME 4 /* Can't derive component name */
56 #define E_LENGTH 5 /* Length mismatch */
57 #define E_CHKSUM 6 /* Checksum mismatch */
58 #define E_ERRNO 7 /* sys_errlist[errno] */
60 #define isfatal(err) ((err) && (err) <= E_NAME)
62 #define NAMESIZE 256 /* filename buffer size */
63 #define MDSUMLEN 32 /* length of MD5 message digest */
65 #define isstdin(path) ((path)[0] == '-' && !(path)[1])
67 static const char *opt_dir; /* where to look for components */
68 static const char *opt_name; /* name for accessing components */
69 static int opt_all; /* report on all components */
70 static int opt_ignore; /* ignore missing components */
71 static int opt_recurse; /* search directories recursively */
72 static int opt_silent; /* silent about inaccessible files */
73 static int opt_type; /* dist type: md5 or inf */
74 static int opt_exist; /* just verify existence */
76 static int ckdist(const char *path, int type);
77 static int chkmd5(FILE * fp, const char *path);
78 static int chkinf(FILE * fp, const char *path);
79 static int report(const char *path, const char *name, int error);
80 static const char *distname(const char *path, const char *name,
82 static const char *stripath(const char *path);
83 static int distfile(const char *path);
84 static int disttype(const char *name);
85 static int fail(const char *path, const char *msg);
86 static void usage(void);
89 main(int argc, char *argv[])
97 while ((c = getopt(argc, argv, "ad:in:rst:x")) != -1)
118 if ((opt_type = disttype(optarg)) == 0) {
119 warnx("illegal argument to -t option");
134 if (stat(opt_dir, &sb))
135 err(2, "%s", opt_dir);
136 if (!S_ISDIR(sb.st_mode))
137 errx(2, "%s: not a directory", opt_dir);
142 rval |= ckdist(*argv, opt_type);
143 else if (stat(*argv, &sb))
144 rval |= fail(*argv, NULL);
145 else if (S_ISREG(sb.st_mode))
146 rval |= ckdist(*argv, opt_type);
149 if ((ftsp = fts_open(arg, FTS_LOGICAL, NULL)) == NULL)
151 while ((f = fts_read(ftsp)) != NULL)
152 switch (f->fts_info) {
154 rval = fail(f->fts_path, "Directory causes a cycle");
159 rval = fail(f->fts_path, sys_errlist[f->fts_errno]);
162 if (!opt_recurse && f->fts_level > FTS_ROOTLEVEL &&
163 fts_set(ftsp, f, FTS_SKIP))
167 if ((type = distfile(f->fts_name)) != 0 &&
168 (!opt_type || type == opt_type))
169 rval |= ckdist(f->fts_path, type);
183 ckdist(const char *path, int type)
191 } else if ((fp = fopen(path, "r")) == NULL)
192 return fail(path, NULL);
195 type = distfile(path);
197 if ((c = fgetc(fp)) != EOF) {
198 type = c == 'M' ? DISTMD5 : c == 'P' ? DISTINF : 0;
204 rval = chkmd5(fp, path);
207 rval = chkinf(fp, path);
210 rval = report(path, NULL, E_UNKNOWN);
214 if (fp != stdin && fclose(fp))
220 md5_file(const char *filename, char * const buf)
222 unsigned char digest[MD5_DIGEST_LENGTH];
223 static const char hex[]="0123456789abcdef";
225 unsigned char buffer[4096];
233 fd = open(filename, O_RDONLY);
236 if (fstat(fd, &st) < 0) {
245 if ((size_t)size > sizeof(buffer))
246 bytes = read(fd, buffer, sizeof(buffer));
248 bytes = read(fd, buffer, size);
251 MD5_Update(&ctx, buffer, bytes);
260 MD5_Final(digest, &ctx);
261 for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
262 buf[2*i] = hex[digest[i] >> 4];
263 buf[2*i+1] = hex[digest[i] & 0x0f];
265 buf[MD5_DIGEST_LENGTH * 2] = '\0';
271 chkmd5(FILE * fp, const char *path)
273 char buf[298]; /* "MD5 (NAMESIZE = MDSUMLEN" */
274 char name[NAMESIZE + 1];
275 char sum[MDSUMLEN + 1], chk[MDSUMLEN + 1];
278 int rval, error, c, fd;
282 while (fgets(buf, sizeof(buf), fp)) {
285 if (((c = sscanf(buf, "MD5 (%256s = %32s%c", name, sum,
286 &ch)) != 3 && (!feof(fp) || c != 2)) ||
287 (c == 3 && ch != '\n') ||
288 (s = strrchr(name, ')')) == NULL ||
289 strlen(sum) != MDSUMLEN)
293 if ((dname = distname(path, name, NULL)) == NULL)
295 else if (opt_exist) {
296 if ((fd = open(dname, O_RDONLY)) == -1)
300 } else if (!md5_file(dname, chk))
302 else if (strcmp(chk, sum))
305 if (opt_ignore && error == E_ERRNO && errno == ENOENT)
307 if (error || opt_all)
308 rval |= report(path, dname, error);
316 chkinf(FILE * fp, const char *path)
318 char buf[30]; /* "cksum.2 = 10 6" */
326 int rval, error, c, pieces, cnt, fd;
330 for (cnt = -1; fgets(buf, sizeof(buf), fp); cnt++) {
335 if ((c = sscanf(buf, "Pieces = %d%c", &pieces, &ch)) != 2 ||
336 ch != '\n' || pieces < 1)
338 } else if (((c = sscanf(buf, "cksum.%2s = %lu %jd%c", ext, &sum,
339 &sumlen, &ch)) != 4 &&
340 (!feof(fp) || c != 3)) || (c == 4 && ch != '\n') ||
341 ext[0] != 'a' + cnt / 26 || ext[1] != 'a' + cnt % 26)
343 else if ((dname = distname(fp == stdin ? NULL : path, NULL,
346 else if ((fd = open(dname, O_RDONLY)) == -1)
348 else if (fstat(fd, &sb))
350 else if (sb.st_size != (off_t)sumlen)
352 else if (!opt_exist) {
353 if (crc(fd, &chk, &len))
358 if (fd != -1 && close(fd))
360 if (opt_ignore && error == E_ERRNO && errno == ENOENT)
362 if (error || (opt_all && cnt >= 0))
363 rval |= report(path, dname, error);
371 report(const char *path, const char *name, int error)
374 name = stripath(name);
377 printf("%s: Unknown format\n", path);
380 printf("%s: Invalid MD5 format\n", path);
383 printf("%s: Invalid .inf format\n", path);
386 printf("%s: Can't derive component name\n", path);
389 printf("%s: %s: Size mismatch\n", path, name);
392 printf("%s: %s: Checksum mismatch\n", path, name);
395 printf("%s: %s: %s\n", path, name, sys_errlist[errno]);
398 printf("%s: %s: OK\n", path, name);
404 distname(const char *path, const char *name, const char *ext)
406 static char buf[NAMESIZE];
415 name = stripath(path);
418 if (ext && nlen > 4 && name[nlen - 4] == '.' &&
419 disttype(name + nlen - 3) == DISTINF)
425 plen = path && (s = strrchr(path, '/')) != NULL ?
426 (size_t)(s - path) : 0;
427 if (plen + (plen > 0) + nlen + (ext ? 3 : 0) >= sizeof(buf))
431 memcpy(s, path, plen);
435 memcpy(s, name, nlen);
447 stripath(const char *path)
451 return ((s = strrchr(path, '/')) != NULL && s[1] ?
456 distfile(const char *path)
461 if ((type = disttype(path)) == DISTMD5 ||
462 ((s = strrchr(path, '.')) != NULL && s > path &&
463 (type = disttype(s + 1)) != 0))
469 disttype(const char *name)
471 static const char dname[DISTTYPES][4] = {"md5", "inf"};
474 for (i = 0; i < DISTTYPES; i++)
475 if (!strcmp(dname[i], name))
481 fail(const char *path, const char *msg)
485 warnx("%s: %s", path, msg ? msg : sys_errlist[errno]);
493 "usage: ckdist [-airsx] [-d dir] [-n name] [-t type] file ...\n");