From 46360e3c1894ea03305435e83dd2e54b9948a172 Mon Sep 17 00:00:00 2001 From: deraadt Date: Tue, 10 Dec 1996 08:25:57 +0000 Subject: [PATCH] merge freebsd mtree diffs, plus -Wall cleanup --- usr.sbin/mtree/Makefile | 3 +- usr.sbin/mtree/compare.c | 32 ++++-- usr.sbin/mtree/create.c | 213 ++++++++++++++++++++++++--------------- usr.sbin/mtree/misc.c | 31 +++--- usr.sbin/mtree/mtree.8 | 72 +++++++++---- usr.sbin/mtree/mtree.c | 26 +++-- usr.sbin/mtree/mtree.h | 21 ++-- usr.sbin/mtree/spec.c | 18 ++-- usr.sbin/mtree/verify.c | 28 ++--- 9 files changed, 284 insertions(+), 160 deletions(-) diff --git a/usr.sbin/mtree/Makefile b/usr.sbin/mtree/Makefile index ebad51b485e..6410cd3cef5 100644 --- a/usr.sbin/mtree/Makefile +++ b/usr.sbin/mtree/Makefile @@ -1,8 +1,9 @@ -# $OpenBSD: Makefile,v 1.2 1996/12/08 01:13:38 niklas Exp $ +# $OpenBSD: Makefile,v 1.3 1996/12/10 08:25:57 deraadt Exp $ # $NetBSD: Makefile,v 1.8 1995/03/07 21:12:04 cgd Exp $ PROG= mtree #CFLAGS+=-DDEBUG +CFLAGS+=-Wall MAN= mtree.8 SRCS= compare.c crc.c create.c misc.c mtree.c spec.c verify.c .PATH: ${.CURDIR}/../../usr.bin/cksum diff --git a/usr.sbin/mtree/compare.c b/usr.sbin/mtree/compare.c index 28280aaacc9..71600548733 100644 --- a/usr.sbin/mtree/compare.c +++ b/usr.sbin/mtree/compare.c @@ -1,5 +1,5 @@ /* $NetBSD: compare.c,v 1.9 1995/10/22 20:12:07 pk Exp $ */ -/* $OpenBSD: compare.c,v 1.4 1996/12/08 01:13:38 niklas Exp $ */ +/* $OpenBSD: compare.c,v 1.5 1996/12/10 08:25:57 deraadt Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -50,6 +50,7 @@ static char rcsid[] = "$NetBSD: compare.c,v 1.9 1995/10/22 20:12:07 pk Exp $"; #include #include #include +#include #include "mtree.h" #include "extern.h" @@ -66,7 +67,7 @@ static char *ftype __P((u_int)); (void)printf("\n"); \ } else { \ tab = ""; \ - (void)printf("%*s", INDENTNAMELEN - len, ""); \ + (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \ } \ } @@ -79,7 +80,7 @@ compare(name, s, p) extern int uflag; u_int32_t len, val; int fd, label; - char *cp, *tab; + char *cp, *tab = ""; label = 0; switch(s->type) { @@ -176,9 +177,9 @@ typeerr: LABEL; * XXX * Catches nano-second differences, but doesn't display them. */ - if (s->flags & F_TIME && - (s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec || - s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec)) { + if ((s->flags & F_TIME) && + ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) || + (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) { LABEL; (void)printf("%smodification time (%.24s, ", tab, ctime(&s->st_mtimespec.tv_sec)); @@ -202,11 +203,28 @@ typeerr: LABEL; (void)close(fd); if (s->cksum != val) { LABEL; - (void)printf("%scksum (%u, %u)\n", + (void)printf("%scksum (%u, %u)\n", tab, s->cksum, val); } tab = "\t"; } + if (s->flags & F_MD5) { + char *new_digest, buf[33]; + + new_digest = MD5File(p->fts_accpath,buf); + if (!new_digest) { + LABEL; + printf("%sMD5File: %s: %s\n", tab, p->fts_accpath, + strerror(errno)); + tab = "\t"; + } else if (strcmp(new_digest, s->md5digest)) { + LABEL; + printf("%sMD5 (%s, %s)\n", tab, s->md5digest, + new_digest); + tab = "\t"; + } + } + if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) { LABEL; (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink); diff --git a/usr.sbin/mtree/create.c b/usr.sbin/mtree/create.c index 1ecedc63d6f..f869546e0fd 100644 --- a/usr.sbin/mtree/create.c +++ b/usr.sbin/mtree/create.c @@ -1,5 +1,5 @@ /* $NetBSD: create.c,v 1.9 1995/03/07 21:12:06 cgd Exp $ */ -/* $OpenBSD: create.c,v 1.3 1996/12/08 01:13:39 niklas Exp $ */ +/* $OpenBSD: create.c,v 1.4 1996/12/10 08:25:58 deraadt Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -53,6 +53,7 @@ static char rcsid[] = "$NetBSD: create.c,v 1.9 1995/03/07 21:12:06 cgd Exp $"; #include #include #include +#include #include "mtree.h" #include "extern.h" @@ -61,7 +62,7 @@ static char rcsid[] = "$NetBSD: create.c,v 1.9 1995/03/07 21:12:06 cgd Exp $"; extern u_int32_t crc_total; extern int ftsoptions; -extern int dflag, sflag; +extern int dflag, iflag, nflag, sflag; extern u_short keys; extern char fullpath[MAXPATHLEN]; @@ -70,9 +71,9 @@ static uid_t uid; static mode_t mode; static int dsort __P((const FTSENT **, const FTSENT **)); -static void output __P((int *, const char *, ...)); +static void output __P((int, int *, const char *, ...)); static int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *)); -static void statf __P((FTSENT *)); +static void statf __P((int, FTSENT *)); void cwalk() @@ -81,6 +82,7 @@ cwalk() register FTSENT *p; time_t clock; char *argv[2], host[MAXHOSTNAMELEN]; + int indent = 0; (void)time(&clock); (void)gethostname(host, sizeof(host)); @@ -92,29 +94,38 @@ cwalk() argv[1] = NULL; if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) err("fts_open: %s", strerror(errno)); - while (p = fts_read(t)) + while ((p = fts_read(t))) { + if (iflag) + indent = p->fts_level * 4; switch(p->fts_info) { case FTS_D: - (void)printf("\n# %s\n", p->fts_path); + if (!dflag) + (void)printf("\n"); + if (!nflag) + (void)printf("# %s\n", p->fts_path); statd(t, p, &uid, &gid, &mode); - statf(p); + statf(indent, p); break; case FTS_DP: - if (p->fts_level > 0) - (void)printf("# %s\n..\n\n", p->fts_path); + if (!nflag && (p->fts_level > 0)) + (void)printf("%*s# %s\n", indent, "", p->fts_path); + (void)printf("%*s..\n", indent, ""); + if (!dflag) + (void)printf("\n"); break; case FTS_DNR: case FTS_ERR: case FTS_NS: - (void)fprintf(stderr, - "mtree: %s: %s\n", p->fts_path, strerror(errno)); + (void)fprintf(stderr, "mtree: %s: %s\n", + p->fts_path, strerror(p->fts_errno)); break; default: if (!dflag) - statf(p); + statf(indent, p); break; } + } (void)fts_close(t); if (sflag && keys & F_CKSUM) (void)fprintf(stderr, @@ -122,44 +133,59 @@ cwalk() } static void -statf(p) +statf(indent, p) + int indent; FTSENT *p; { struct group *gr; struct passwd *pw; u_int32_t len, val; - int fd, indent; + int fd, offset; - if (S_ISDIR(p->fts_statp->st_mode)) - indent = printf("%s", p->fts_name); + if (iflag || S_ISDIR(p->fts_statp->st_mode)) + offset = printf("%*s%s", indent, "", p->fts_name); else - indent = printf(" %s", p->fts_name); + offset = printf("%*s %s", indent, "", p->fts_name); - if (indent > INDENTNAMELEN) - indent = MAXLINELEN; + if (offset > (INDENTNAMELEN + indent)) + offset = MAXLINELEN; else - indent += printf("%*s", INDENTNAMELEN - indent, ""); + offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); - if (!S_ISREG(p->fts_statp->st_mode)) - output(&indent, "type=%s", inotype(p->fts_statp->st_mode)); - if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) - if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid))) - output(&indent, "uname=%s", pw->pw_name); - else /* if (keys & F_UID) */ - output(&indent, "uid=%u", p->fts_statp->st_uid); - if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) - if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid))) - output(&indent, "gname=%s", gr->gr_name); - else /* if (keys & F_GID) */ - output(&indent, "gid=%u", p->fts_statp->st_gid); + if (!S_ISREG(p->fts_statp->st_mode) && !dflag) + output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); + if (p->fts_statp->st_uid != uid) { + if (keys & F_UNAME) { + if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { + output(indent, &offset, "uname=%s", pw->pw_name); + } else { + err("could not get uname for uid=%u", + p->fts_statp->st_uid); + } + } + if (keys & F_UID) + output(indent, &offset, "uid=%u", p->fts_statp->st_uid); + } + if (p->fts_statp->st_gid != gid) { + if (keys & F_GNAME) { + if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { + output(indent, &offset, "gname=%s", gr->gr_name); + } else { + err("could not get gname for gid=%u", + p->fts_statp->st_gid); + } + } + if (keys & F_GID) + output(indent, &offset, "gid=%u", p->fts_statp->st_gid); + } if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) - output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS); + output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); if (keys & F_NLINK && p->fts_statp->st_nlink != 1) - output(&indent, "nlink=%u", p->fts_statp->st_nlink); + output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); if (keys & F_SIZE) - output(&indent, "size=%qd", p->fts_statp->st_size); + output(indent, &offset, "size=%qd", p->fts_statp->st_size); if (keys & F_TIME) - output(&indent, "time=%ld.%ld", + output(indent, &offset, "time=%ld.%ld", p->fts_statp->st_mtimespec.tv_sec, p->fts_statp->st_mtimespec.tv_nsec); if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { @@ -167,11 +193,21 @@ statf(p) crc(fd, &val, &len)) err("%s: %s", p->fts_accpath, strerror(errno)); (void)close(fd); - output(&indent, "cksum=%u", val); + output(indent, &offset, "cksum=%lu", val); + } + if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { + char *md5digest, buf[33]; + + md5digest = MD5File(p->fts_accpath,buf); + if (!md5digest) { + err("%s: %s", p->fts_accpath, strerror(errno)); + } else { + output(indent, &offset, "md5digest=%s", md5digest); + } } if (keys & F_SLINK && (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) - output(&indent, "link=%s", rlink(p->fts_accpath)); + output(indent, &offset, "link=%s", rlink(p->fts_accpath)); (void)putchar('\n'); } @@ -193,10 +229,11 @@ statd(t, parent, puid, pgid, pmode) register mode_t smode; struct group *gr; struct passwd *pw; - gid_t savegid; - uid_t saveuid; - mode_t savemode; + gid_t savegid = *pgid; + uid_t saveuid = *puid; + mode_t savemode = *pmode; u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE]; + static int first = 1; if ((p = fts_children(t, 0)) == NULL) { if (errno) @@ -210,45 +247,60 @@ statd(t, parent, puid, pgid, pmode) maxuid = maxgid = maxmode = 0; for (; p; p = p->fts_link) { - smode = p->fts_statp->st_mode & MBITS; - if (smode < MAXMODE && ++m[smode] > maxmode) { - savemode = smode; - maxmode = m[smode]; - } - sgid = p->fts_statp->st_gid; - if (sgid < MAXGID && ++g[sgid] > maxgid) { - savegid = sgid; - maxgid = g[sgid]; - } - suid = p->fts_statp->st_uid; - if (suid < MAXUID && ++u[suid] > maxuid) { - saveuid = suid; - maxuid = u[suid]; + if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { + smode = p->fts_statp->st_mode & MBITS; + if (smode < MAXMODE && ++m[smode] > maxmode) { + savemode = smode; + maxmode = m[smode]; + } + sgid = p->fts_statp->st_gid; + if (sgid < MAXGID && ++g[sgid] > maxgid) { + savegid = sgid; + maxgid = g[sgid]; + } + suid = p->fts_statp->st_uid; + if (suid < MAXUID && ++u[suid] > maxuid) { + saveuid = suid; + maxuid = u[suid]; + } } } - (void)printf("/set type=file"); - if (keys & F_GID) - (void)printf(" gid=%u", savegid); - if (keys & F_GNAME) - if ((gr = getgrgid(savegid)) != NULL) - (void)printf(" gname=%s", gr->gr_name); - else - (void)printf(" gid=%u", savegid); - if (keys & F_UNAME) - if ((pw = getpwuid(saveuid)) != NULL) - (void)printf(" uname=%s", pw->pw_name); + /* + * If the /set record is the same as the last one we do not need to output + * a new one. So first we check to see if anything changed. Note that we + * always output a /set record for the first directory. + */ + if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || + (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || + ((keys & F_MODE) && (*pmode != savemode)) || (first)) { + first = 0; + if (dflag) + (void)printf("/set type=dir"); else + (void)printf("/set type=file"); + if (keys & F_UNAME) + if ((pw = getpwuid(saveuid)) != NULL) + (void)printf(" uname=%s", pw->pw_name); + else + err("could not get uname for uid=%u", saveuid); + if (keys & F_UID) (void)printf(" uid=%u", saveuid); - if (keys & F_UID) - (void)printf(" uid=%u", saveuid); - if (keys & F_MODE) - (void)printf(" mode=%#o", savemode); - if (keys & F_NLINK) - (void)printf(" nlink=1"); - (void)printf("\n"); - *puid = saveuid; - *pgid = savegid; - *pmode = savemode; + if (keys & F_GNAME) + if ((gr = getgrgid(savegid)) != NULL) + (void)printf(" gname=%s", gr->gr_name); + else + err("could not get gname for gid=%u", savegid); + if (keys & F_GID) + (void)printf(" gid=%u", savegid); + if (keys & F_MODE) + (void)printf(" mode=%#o", savemode); + if (keys & F_NLINK) + (void)printf(" nlink=1"); + (void)printf("\n"); + *puid = saveuid; + *pgid = savegid; + *pmode = savemode; + } return (0); } @@ -272,9 +324,10 @@ dsort(a, b) void #if __STDC__ -output(int *offset, const char *fmt, ...) +output(int indent, int *offset, const char *fmt, ...) #else -output(offset, fmt, va_alist) +output(indent, offset, fmt, va_alist) + int indent; int *offset; char *fmt; va_dcl @@ -291,8 +344,8 @@ output(offset, fmt, va_alist) va_end(ap); if (*offset + strlen(buf) > MAXLINELEN - 3) { - (void)printf(" \\\n%*s", INDENTNAMELEN, ""); - *offset = INDENTNAMELEN; + (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); + *offset = INDENTNAMELEN + indent; } *offset += printf(" %s", buf) + 1; } diff --git a/usr.sbin/mtree/misc.c b/usr.sbin/mtree/misc.c index 23ef86073bc..24712c2b328 100644 --- a/usr.sbin/mtree/misc.c +++ b/usr.sbin/mtree/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.2 1996/12/08 01:13:40 niklas Exp $ */ +/* $OpenBSD: misc.c,v 1.3 1996/12/10 08:26:08 deraadt Exp $ */ /* $NetBSD: misc.c,v 1.4 1995/03/07 21:26:23 cgd Exp $ */ /*- @@ -55,19 +55,19 @@ typedef struct _key { /* NB: the following table must be sorted lexically. */ static KEY keylist[] = { - "cksum", F_CKSUM, NEEDVALUE, - "gid", F_GID, NEEDVALUE, - "gname", F_GNAME, NEEDVALUE, - "ignore", F_IGN, 0, - "link", F_SLINK, NEEDVALUE, - "mode", F_MODE, NEEDVALUE, - "nlink", F_NLINK, NEEDVALUE, - "optional", F_OPT, 0, - "size", F_SIZE, NEEDVALUE, - "time", F_TIME, NEEDVALUE, - "type", F_TYPE, NEEDVALUE, - "uid", F_UID, NEEDVALUE, - "uname", F_UNAME, NEEDVALUE, + {"cksum", F_CKSUM, NEEDVALUE}, + {"gid", F_GID, NEEDVALUE}, + {"gname", F_GNAME, NEEDVALUE}, + {"ignore", F_IGN, 0}, + {"link", F_SLINK, NEEDVALUE}, + {"md5digest", F_MD5, NEEDVALUE}, + {"mode", F_MODE, NEEDVALUE}, + {"nlink", F_NLINK, NEEDVALUE}, + {"size", F_SIZE, NEEDVALUE}, + {"time", F_TIME, NEEDVALUE}, + {"type", F_TYPE, NEEDVALUE}, + {"uid", F_UID, NEEDVALUE}, + {"uname", F_UNAME, NEEDVALUE}, }; u_int @@ -117,7 +117,8 @@ err(fmt, va_alist) #else va_start(ap); #endif - (void)fprintf(stderr, "mtree: "); + (void)fflush(NULL); + (void)fprintf(stderr, "\nmtree: "); (void)vfprintf(stderr, fmt, ap); va_end(ap); (void)fprintf(stderr, "\n"); diff --git a/usr.sbin/mtree/mtree.8 b/usr.sbin/mtree/mtree.8 index 1ba46089bbe..cb54d93cba0 100644 --- a/usr.sbin/mtree/mtree.8 +++ b/usr.sbin/mtree/mtree.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: mtree.8,v 1.2 1996/12/08 01:13:41 niklas Exp $ +.\" $OpenBSD: mtree.8,v 1.3 1996/12/10 08:26:08 deraadt Exp $ .\" $NetBSD: mtree.8,v 1.4 1995/03/07 21:26:25 cgd Exp $ .\" .\" Copyright (c) 1989, 1990, 1993 @@ -42,7 +42,7 @@ .Nd map a directory hierarchy .Sh SYNOPSIS .Nm mtree -.Op Fl cderux +.Op Fl cdeinrUux .Op Fl f Ar spec .Op Fl K Ar keywords .Op Fl k Ar keywords @@ -70,12 +70,26 @@ specification. Read the specification from .Ar file , instead of from the standard input. +.It Fl i +Indents the output 4 spaces each time a directory level is descended when +create a specification with the +.Fl c +option. +This does not effect either the /set statements or the comment before each +directory. +It does however effect the comment before the close of each directory. .It Fl K Add the specified (whitespace or comma separated) keywords to the current set of keywords. .It Fl k Use the ``type'' keyword plus the specified (whitespace or comma separated) keywords instead of the current set of keywords. +.It Fl n +Do not emit pathname comments when creating a specification. Normally +a comment is emitted before each directory and before the close of that +directory when using the +.Fl c +option. .It Fl p Use the file hierarchy rooted in .Ar path , @@ -89,11 +103,18 @@ of the files for which the keyword .Cm cksum was specified. The checksum is seeded with the specified value. -.It Fl u +.It Fl U Modify the owner, group, and permissions of existing files to match the specification and create any missing directories. User, group, and permissions must all be specified for missing directories to be created. +Exit with a status of 0 on success, 1 if any error occurred, +a mismatch is not considered an error if it was corrected. +.It Fl u +Same as +.Fl U +except a status of 2 is returned if the file hierarchy did not match +the specification. .It Fl x Don't descend below mount points in the file hierarchy. .El @@ -116,22 +137,21 @@ Ignore any file hierarchy below this file. The file group as a numeric value. .It Cm gname The file group as a symbolic name. -.It Cm link -The file the symbolic link is expected to reference. +.It Cm md5digest +The MD5 message digest of the file. .It Cm mode The current file's permissions as a numeric (octal) or symbolic value. .It Cm nlink The number of hard links the file is expected to have. -.It Cm optional -The file is optional; don't complain about the file if it's -not in the file hierarchy. .It Cm uid The file owner as a numeric value. .It Cm uname The file owner as a symbolic name. .It Cm size The size, in bytes, of the file. +.It Cm link +The file the symbolic link is expected to reference. .It Cm time The last modification time of the file. .It Cm type @@ -157,10 +177,10 @@ socket .Pp The default set of keywords are .Cm gid , -.Cm link , .Cm mode , .Cm nlink , .Cm size , +.Cm link , .Cm time , and .Cm uid . @@ -210,32 +230,39 @@ The .Nm mtree utility exits with a status of 0 on success, 1 if any error occurred, and 2 if the file hierarchy did not match the specification. +A status of 2 is converted to a status of 0 if the +.Fl U +option is used. .Sh EXAMPLES To detect system binaries that have been ``trojan horsed'', it is recommended that .Nm mtree +.Fl K +.Cm md5digest be run on the file systems, and a copy of the results stored on a different machine, or, at least, in encrypted form. -The seed for the -.Fl s -option should not be an obvious value and the final checksum should not be -stored on-line under any circumstances! +The output file itself should be digested using the +.Xr md5 1 +utility. Then, periodically, .Nm mtree -should be run against the on-line specifications and the final checksum -compared with the previous value. +and +.Xr md5 1 +should be run against the on-line specifications. While it is possible for the bad guys to change the on-line specifications -to conform to their modified binaries, it shouldn't be possible for them -to make it produce the same final checksum value. -If the final checksum value changes, the off-line copies of the specification -can be used to detect which of the binaries have actually been modified. +to conform to their modified binaries, it is believed to be +impractical for them to create a modified specification which has +the same MD5 digest as the original. .Pp The .Fl d and .Fl u options can be used in combination to create directory hierarchies -for distributions and other such things. +for distributions and other such things; the files in +.Pa /etc/mtree +were used to create almost all directories in a normal binary +distribution. .Sh FILES .Bl -tag -width /etc/mtree -compact .It Pa /etc/mtree @@ -243,13 +270,16 @@ system specification directory .El .Sh SEE ALSO .Xr chmod 1 , -.Xr chown 1 , .Xr chgrp 1 , .Xr cksum 1 , +.Xr md5 1 , .Xr stat 2 , .Xr fts 3 , +.Xr md5 3 , +.Xr chown 8 .Sh HISTORY The .Nm mtree utility appeared in .Bx 4.3 Reno . +The MD5 digest capability was added in FreeBSD. diff --git a/usr.sbin/mtree/mtree.c b/usr.sbin/mtree/mtree.c index d36b3818aa5..f7609875de4 100644 --- a/usr.sbin/mtree/mtree.c +++ b/usr.sbin/mtree/mtree.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mtree.c,v 1.2 1996/12/08 01:13:41 niklas Exp $ */ +/* $OpenBSD: mtree.c,v 1.3 1996/12/10 08:26:09 deraadt Exp $ */ /* $NetBSD: mtree.c,v 1.5 1995/03/07 21:12:10 cgd Exp $ */ /*- @@ -44,7 +44,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)mtree.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$OpenBSD: mtree.c,v 1.2 1996/12/08 01:13:41 niklas Exp $"; +static char rcsid[] = "$OpenBSD: mtree.c,v 1.3 1996/12/10 08:26:09 deraadt Exp $"; #endif #endif /* not lint */ @@ -60,7 +60,7 @@ static char rcsid[] = "$OpenBSD: mtree.c,v 1.2 1996/12/08 01:13:41 niklas Exp $" extern u_int32_t crc_total; int ftsoptions = FTS_PHYSICAL; -int cflag, dflag, eflag, rflag, sflag, uflag; +int cflag, dflag, eflag, iflag, nflag, rflag, sflag, uflag, Uflag; u_short keys; char fullpath[MAXPATHLEN]; @@ -75,10 +75,11 @@ main(argc, argv) extern char *optarg; int ch; char *dir, *p; + int status; dir = NULL; keys = KEYDEFAULT; - while ((ch = getopt(argc, argv, "cdef:K:k:p:rs:ux")) != EOF) + while ((ch = getopt(argc, argv, "cdef:iK:k:np:rs:Uux")) != EOF) switch((char)ch) { case 'c': cflag = 1; @@ -93,6 +94,9 @@ main(argc, argv) if (!(freopen(optarg, "r", stdin))) err("%s: %s", optarg, strerror(errno)); break; + case 'i': + iflag = 1; + break; case 'K': while ((p = strsep(&optarg, " \t,")) != NULL) if (*p != '\0') @@ -104,6 +108,9 @@ main(argc, argv) if (*p != '\0') keys |= parsekey(p, NULL); break; + case 'n': + nflag = 1; + break; case 'p': dir = optarg; break; @@ -115,6 +122,10 @@ main(argc, argv) crc_total = ~strtol(optarg, &p, 0); if (*p) err("illegal seed value -- %s", optarg); + case 'U': + Uflag = 1; + uflag = 1; + break; case 'u': uflag = 1; break; @@ -141,13 +152,16 @@ main(argc, argv) cwalk(); exit(0); } - exit(verify()); + status = verify(); + if (Uflag & (status == MISMATCHEXIT)) + status = 0; + exit(status); } static void usage() { (void)fprintf(stderr, -"usage: mtree [-cderux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"); +"usage: mtree [-cdeinrUux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"); exit(1); } diff --git a/usr.sbin/mtree/mtree.h b/usr.sbin/mtree/mtree.h index 113968d1ce3..60e8108195b 100644 --- a/usr.sbin/mtree/mtree.h +++ b/usr.sbin/mtree/mtree.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mtree.h,v 1.2 1996/12/08 01:13:42 niklas Exp $ */ +/* $OpenBSD: mtree.h,v 1.3 1996/12/10 08:26:09 deraadt Exp $ */ /* $NetBSD: mtree.h,v 1.7 1995/03/07 21:26:27 cgd Exp $ */ /*- @@ -49,7 +49,8 @@ typedef struct _node { struct _node *prev, *next; /* left, right */ off_t st_size; /* size */ struct timespec st_mtimespec; /* last modification time */ - u_int32_t cksum; /* check sum */ + u_int32_t cksum; /* check sum */ + char *md5digest; /* MD5 digest */ char *slink; /* symbolic link reference */ uid_t st_uid; /* uid */ gid_t st_gid; /* gid */ @@ -65,14 +66,14 @@ typedef struct _node { #define F_MAGIC 0x0020 /* name has magic chars */ #define F_MODE 0x0040 /* mode */ #define F_NLINK 0x0080 /* number of links */ -#define F_OPT 0x0100 /* existence optional */ -#define F_SIZE 0x0200 /* size */ -#define F_SLINK 0x0400 /* link count */ -#define F_TIME 0x0800 /* modification time */ -#define F_TYPE 0x1000 /* file type */ -#define F_UID 0x2000 /* uid */ -#define F_UNAME 0x4000 /* user name */ -#define F_VISIT 0x8000 /* file visited */ +#define F_SIZE 0x0100 /* size */ +#define F_SLINK 0x0200 /* link count */ +#define F_TIME 0x0400 /* modification time */ +#define F_TYPE 0x0800 /* file type */ +#define F_UID 0x1000 /* uid */ +#define F_UNAME 0x2000 /* user name */ +#define F_VISIT 0x4000 /* file visited */ +#define F_MD5 0x8000 /* MD5 digest */ u_short flags; /* items set */ #define F_BLOCK 0x001 /* block special */ diff --git a/usr.sbin/mtree/spec.c b/usr.sbin/mtree/spec.c index 99179609d80..104675b428e 100644 --- a/usr.sbin/mtree/spec.c +++ b/usr.sbin/mtree/spec.c @@ -1,5 +1,5 @@ /* $NetBSD: spec.c,v 1.6 1995/03/07 21:12:12 cgd Exp $ */ -/* $OpenBSD: spec.c,v 1.2 1996/03/02 00:46:04 tholo Exp $ */ +/* $OpenBSD: spec.c,v 1.3 1996/12/10 08:26:10 deraadt Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -68,7 +68,7 @@ spec() int c_cur, c_next; char buf[2048]; - root = NULL; + centry = last = root = NULL; bzero(&ginfo, sizeof(ginfo)); c_cur = c_next = 0; for (lineno = 1; fgets(buf, sizeof(buf), stdin); @@ -171,14 +171,14 @@ set(t, ip) register NODE *ip; { register int type; - register char *kw, *val; + register char *kw, *val = NULL; struct group *gr; struct passwd *pw; mode_t *m; int value; char *ep; - for (; kw = strtok(t, "= \t\n"); t = NULL) { + for (; (kw = strtok(t, "= \t\n")); t = NULL) { ip->flags |= type = parsekey(kw, &value); if (value && (val = strtok(NULL, " \t\n")) == NULL) err("missing value"); @@ -188,6 +188,12 @@ set(t, ip) if (*ep) err("invalid checksum %s", val); break; + case F_MD5: + ip->md5digest = strdup(val); + if (!ip->md5digest) { + err("%s", strerror(errno)); + } + break; case F_GID: ip->st_gid = strtoul(val, &ep, 10); if (*ep) @@ -212,7 +218,7 @@ set(t, ip) err("invalid link count %s", val); break; case F_SIZE: - ip->st_size = strtouq(val, &ep, 10); + ip->st_size = strtoq(val, &ep, 10); if (*ep) err("invalid size %s", val); break; @@ -282,6 +288,6 @@ unset(t, ip) { register char *p; - while (p = strtok(t, "\n\t ")) + while ((p = strtok(t, "\n\t "))) ip->flags &= ~parsekey(p, NULL); } diff --git a/usr.sbin/mtree/verify.c b/usr.sbin/mtree/verify.c index 371cf0384a1..328ce38e663 100644 --- a/usr.sbin/mtree/verify.c +++ b/usr.sbin/mtree/verify.c @@ -1,4 +1,4 @@ -/* $OpenBSD: verify.c,v 1.2 1996/12/08 01:13:42 niklas Exp $ */ +/* $OpenBSD: verify.c,v 1.3 1996/12/10 08:26:10 deraadt Exp $ */ /* $NetBSD: verify.c,v 1.10 1995/03/07 21:26:28 cgd Exp $ */ /*- @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)verify.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$OpenBSD: verify.c,v 1.2 1996/12/08 01:13:42 niklas Exp $"; +static char rcsid[] = "$OpenBSD: verify.c,v 1.3 1996/12/10 08:26:10 deraadt Exp $"; #endif #endif /* not lint */ @@ -81,7 +81,7 @@ vwalk() register FTS *t; register FTSENT *p; register NODE *ep, *level; - int ftsdepth, specdepth, rval; + int specdepth, rval; char *argv[2]; argv[0] = "."; @@ -89,15 +89,13 @@ vwalk() if ((t = fts_open(argv, ftsoptions, NULL)) == NULL) err("fts_open: %s", strerror(errno)); level = root; - ftsdepth = specdepth = rval = 0; - while (p = fts_read(t)) { + specdepth = rval = 0; + while ((p = fts_read(t))) { switch(p->fts_info) { case FTS_D: - ++ftsdepth; break; case FTS_DP: - --ftsdepth; - if (specdepth > ftsdepth) { + if (specdepth > p->fts_level) { for (level = level->parent; level->prev; level = level->prev); --specdepth; @@ -107,16 +105,18 @@ vwalk() case FTS_ERR: case FTS_NS: (void)fprintf(stderr, "mtree: %s: %s\n", - RP(p), strerror(errno)); + RP(p), strerror(p->fts_errno)); continue; default: if (dflag) continue; } + if (specdepth != p->fts_level) + goto extra; for (ep = level; ep; ep = ep->next) - if (ep->flags & F_MAGIC && - !fnmatch(ep->name, p->fts_name, FNM_PATHNAME) || + if ((ep->flags & F_MAGIC && + !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) || !strcmp(ep->name, p->fts_name)) { ep->flags |= F_VISIT; if (compare(ep->name, ep, p)) @@ -133,10 +133,12 @@ vwalk() if (ep) continue; +extra: if (!eflag) { (void)printf("extra: %s", RP(p)); if (rflag) { - if (unlink(p->fts_accpath)) { + if ((S_ISDIR(p->fts_statp->st_mode) + ? rmdir : unlink)(p->fts_accpath)) { (void)printf(", not removed: %s", strerror(errno)); } else @@ -162,8 +164,6 @@ miss(p, tail) register char *tp; for (; p; p = p->next) { - if (p->flags & F_OPT && !(p->flags & F_VISIT)) - continue; if (p->type != F_DIR && (dflag || p->flags & F_VISIT)) continue; (void)strcpy(tail, p->name); -- 2.20.1