Index: head/usr.sbin/mtree/create.c =================================================================== --- head/usr.sbin/mtree/create.c (revision 36669) +++ head/usr.sbin/mtree/create.c (revision 36670) @@ -1,354 +1,354 @@ /*- * Copyright (c) 1989, 1993 * The Regents of the University of California. 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 #if 0 static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; #endif static const char rcsid[] = - "$Id$"; + "$Id: create.c,v 1.9 1997/10/01 06:30:00 charnier Exp $"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "mtree.h" #include "extern.h" #define INDENTNAMELEN 15 #define MAXLINELEN 80 extern long int crc_total; extern int ftsoptions; extern int dflag, iflag, nflag, sflag; -extern u_short keys; +extern u_int keys; extern char fullpath[MAXPATHLEN]; extern int lineno; static gid_t gid; static uid_t uid; static mode_t mode; static int dsort __P((const FTSENT **, const FTSENT **)); static void output __P((int, int *, const char *, ...)); static int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *)); static void statf __P((int, FTSENT *)); void cwalk() { register FTS *t; register FTSENT *p; time_t clock; char *argv[2], host[MAXHOSTNAMELEN]; int indent = 0; (void)time(&clock); (void)gethostname(host, sizeof(host)); (void)printf( "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", getlogin(), host, fullpath, ctime(&clock)); argv[0] = "."; argv[1] = NULL; if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) err(1, "line %d: fts_open", lineno); while ((p = fts_read(t))) { if (iflag) indent = p->fts_level * 4; switch(p->fts_info) { case FTS_D: if (!dflag) (void)printf("\n"); if (!nflag) (void)printf("# %s\n", p->fts_path); statd(t, p, &uid, &gid, &mode); statf(indent, p); break; case FTS_DP: 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: warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); break; default: if (!dflag) statf(indent, p); break; } } (void)fts_close(t); if (sflag && keys & F_CKSUM) warnx("%s checksum: %lu", fullpath, crc_total); } static void statf(indent, p) int indent; FTSENT *p; { struct group *gr; struct passwd *pw; u_long len, val; int fd, offset; if (iflag || S_ISDIR(p->fts_statp->st_mode)) offset = printf("%*s%s", indent, "", p->fts_name); else offset = printf("%*s %s", indent, "", p->fts_name); if (offset > (INDENTNAMELEN + indent)) offset = MAXLINELEN; else offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); 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 { errx(1, "line %d: could not get uname for uid=%u", lineno, 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 { errx(1, "line %d: could not get gname for gid=%u", lineno, 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, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); if (keys & F_NLINK && p->fts_statp->st_nlink != 1) output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); if (keys & F_SIZE) output(indent, &offset, "size=%qd", p->fts_statp->st_size); if (keys & F_TIME) 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)) { if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || crc(fd, &val, &len)) err(1, "line %d: %s", lineno, p->fts_accpath); (void)close(fd); 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(1, "line %d: %s", lineno, p->fts_accpath); } else { output(indent, &offset, "md5digest=%s", md5digest); } } if (keys & F_SLINK && (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) output(indent, &offset, "link=%s", rlink(p->fts_accpath)); (void)putchar('\n'); } #define MAXGID 5000 #define MAXUID 5000 #define MAXMODE MBITS + 1 static int statd(t, parent, puid, pgid, pmode) FTS *t; FTSENT *parent; uid_t *puid; gid_t *pgid; mode_t *pmode; { register FTSENT *p; register gid_t sgid; register uid_t suid; register mode_t smode; struct group *gr; struct passwd *pw; 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) err(1, "line %d: %s", lineno, RP(parent)); return (1); } bzero(g, sizeof(g)); bzero(u, sizeof(u)); bzero(m, sizeof(m)); maxuid = maxgid = maxmode = 0; for (; p; p = p->fts_link) { 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]; } } } /* * 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 errx(1, "line %d: could not get uname for uid=%u", lineno, saveuid); if (keys & F_UID) (void)printf(" uid=%lu", saveuid); if (keys & F_GNAME) if ((gr = getgrgid(savegid)) != NULL) (void)printf(" gname=%s", gr->gr_name); else errx(1, "line %d: could not get gname for gid=%u", lineno, savegid); if (keys & F_GID) (void)printf(" gid=%lu", 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); } static int dsort(a, b) const FTSENT **a, **b; { if (S_ISDIR((*a)->fts_statp->st_mode)) { if (!S_ISDIR((*b)->fts_statp->st_mode)) return (1); } else if (S_ISDIR((*b)->fts_statp->st_mode)) return (-1); return (strcmp((*a)->fts_name, (*b)->fts_name)); } #if __STDC__ #include #else #include #endif void #if __STDC__ output(int indent, int *offset, const char *fmt, ...) #else output(indent, offset, fmt, va_alist) int indent; int *offset; char *fmt; va_dcl #endif { va_list ap; char buf[1024]; #if __STDC__ va_start(ap, fmt); #else va_start(ap); #endif (void)vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (*offset + strlen(buf) > MAXLINELEN - 3) { (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); *offset = INDENTNAMELEN + indent; } *offset += printf(" %s", buf) + 1; } Index: head/usr.sbin/mtree/misc.c =================================================================== --- head/usr.sbin/mtree/misc.c (revision 36669) +++ head/usr.sbin/mtree/misc.c (revision 36670) @@ -1,101 +1,102 @@ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 #if 0 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93"; #endif static const char rcsid[] = - "$Id$"; + "$Id: misc.c,v 1.4 1997/10/01 06:30:01 charnier Exp $"; #endif /*not lint */ #include #include #include #include #include #include "mtree.h" #include "extern.h" extern int lineno; typedef struct _key { char *name; /* key name */ u_int val; /* value */ #define NEEDVALUE 0x01 u_int flags; } 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}, {"md5digest", F_MD5, NEEDVALUE}, {"mode", F_MODE, NEEDVALUE}, {"nlink", F_NLINK, NEEDVALUE}, + {"nochange", F_NOCHANGE, 0}, {"size", F_SIZE, NEEDVALUE}, {"time", F_TIME, NEEDVALUE}, {"type", F_TYPE, NEEDVALUE}, {"uid", F_UID, NEEDVALUE}, {"uname", F_UNAME, NEEDVALUE}, }; u_int parsekey(name, needvaluep) char *name; int *needvaluep; { KEY *k, tmp; int keycompare __P((const void *, const void *)); tmp.name = name; k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY), sizeof(KEY), keycompare); if (k == NULL) errx(1, "line %d: unknown keyword %s", lineno, name); if (needvaluep) *needvaluep = k->flags & NEEDVALUE ? 1 : 0; return (k->val); } int keycompare(a, b) const void *a, *b; { return (strcmp(((KEY *)a)->name, ((KEY *)b)->name)); } Index: head/usr.sbin/mtree/mtree.8 =================================================================== --- head/usr.sbin/mtree/mtree.8 (revision 36669) +++ head/usr.sbin/mtree/mtree.8 (revision 36670) @@ -1,290 +1,293 @@ .\" Copyright (c) 1989, 1990, 1993 .\" The Regents of the University of California. 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. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. .\" .\" From: @(#)mtree.8 8.2 (Berkeley) 12/11/93 -.\" $Id: mtree.8,v 1.10 1997/02/22 16:07:52 peter Exp $ +.\" $Id: mtree.8,v 1.11 1997/10/01 06:30:01 charnier Exp $ .\" .Dd February 9, 1995 .Dt MTREE 8 .Os .Sh NAME .Nm mtree .Nd map a directory hierarchy .Sh SYNOPSIS .Nm mtree .Op Fl cdeinrUux .Op Fl f Ar spec .Op Fl K Ar keywords .Op Fl k Ar keywords .Op Fl p Ar path .Op Fl s Ar seed .Sh DESCRIPTION The utility .Nm mtree compares the file hierarchy rooted in the current directory against a specification read from the standard input. Messages are written to the standard output for any files whose characteristics do not match the specifications, or which are missing from either the file hierarchy or the specification. .Pp The options are as follows: .Bl -tag -width flag .It Fl c Print a specification for the file hierarchy to the standard output. .It Fl d Ignore everything except directory type files. .It Fl e Don't complain about files that are in the file hierarchy, but not in the specification. .It Fl f Ar file Read the specification from .Ar file , instead of from the standard input. .It Fl i Indent 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 Ar keywords Add the specified (whitespace or comma separated) .Ar keywords to the current set of keywords. .It Fl k Ar keywords Use the ``type'' keyword plus the specified (whitespace or comma separated) .Ar 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 Ar path Use the file hierarchy rooted in .Ar path , instead of the current directory. .It Fl r Remove any files in the file hierarchy that are not described in the specification. .It Fl s Ar seed Display a single checksum to the standard error output that represents all of the files for which the keyword .Cm cksum was specified. The checksum is seeded with the specified value. .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 .Pp Specifications are mostly composed of ``keywords'', i.e. strings that that specify values relating to files. No keywords have default values, and if a keyword has no value set, no checks based on it are performed. .Pp Currently supported keywords are as follows: .Bl -tag -width Cm .It Cm cksum The checksum of the file using the default algorithm specified by the .Xr cksum 1 utility. .It Cm ignore Ignore any file hierarchy below this file. .It Cm gid The file group as a numeric value. .It Cm gname The file group as a symbolic name. +.It Cm nochange +Make sure this file or directory exists but otherwise ignore all attributes. +Ignore any hierarchy below this path. .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 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 The type of the file; may be set to any one of the following: .sp .Bl -tag -width Cm -compact .It Cm block block special device .It Cm char character special device .It Cm dir directory .It Cm fifo fifo .It Cm file regular file .It Cm link symbolic link .It Cm socket socket .El .El .Pp The default set of keywords are .Cm gid , .Cm mode , .Cm nlink , .Cm size , .Cm link , .Cm time , and .Cm uid . .Pp There are four types of lines in a specification. .Pp The first type of line sets a global value for a keyword, and consists of the string ``/set'' followed by whitespace, followed by sets of keyword/value pairs, separated by whitespace. Keyword/value pairs consist of a keyword, followed by an equals sign (``=''), followed by a value, without whitespace characters. Once a keyword has been set, its value remains unchanged until either reset or unset. .Pp The second type of line unsets keywords and consists of the string ``/unset'', followed by whitespace, followed by one or more keywords, separated by whitespace. .Pp The third type of line is a file specification and consists of a file name, followed by whitespace, followed by zero or more whitespace separated keyword/value pairs. The file name may be preceded by whitespace characters. The file name may contain any of the standard file name matching characters (``['', ``]'', ``?'' or ``*''), in which case files in the hierarchy will be associated with the first pattern that they match. .Pp Each of the keyword/value pairs consist of a keyword, followed by an equals sign (``=''), followed by the keyword's value, without whitespace characters. These values override, without changing, the global value of the corresponding keyword. .Pp All paths are relative. Specifying a directory will cause subsequent files to be searched for in that directory hierarchy. Which brings us to the last type of line in a specification: a line containing only the string .Dq Nm \&.. causes the current directory path to ascend one level. .Pp Empty lines and lines whose first non-whitespace character is a hash mark (``#'') are ignored. .Pp The .Nm 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 .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 output file itself should be digested using the .Xr md5 1 utility. Then, periodically, .Nm 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 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; the files in .Pa /etc/mtree were used to create almost all directories in this .Tn FreeBSD distribution. .Sh FILES .Bl -tag -width /etc/mtree -compact .It Pa /etc/mtree system specification directory .El .Sh SEE ALSO .Xr chgrp 1 , .Xr chmod 1 , .Xr cksum 1 , .Xr md5 1 , .Xr stat 2 , .Xr fts 3 , .Xr md5 3 , .Xr chown 8 .Sh HISTORY The .Nm utility appeared in .Bx 4.3 Reno . The MD5 digest capability was added in .Fx 2.1 , in response to the widespread use of programs which can spoof .Xr cksum 1 . Index: head/usr.sbin/mtree/mtree.c =================================================================== --- head/usr.sbin/mtree/mtree.c (revision 36669) +++ head/usr.sbin/mtree/mtree.c (revision 36670) @@ -1,163 +1,163 @@ /*- * Copyright (c) 1989, 1990, 1993 * The Regents of the University of California. 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 copyright[] = "@(#) Copyright (c) 1989, 1990, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)mtree.c 8.1 (Berkeley) 6/6/93"; #endif static const char rcsid[] = - "$Id$"; + "$Id: mtree.c,v 1.6 1997/10/01 06:30:02 charnier Exp $"; #endif /* not lint */ #include #include #include #include #include #include #include #include "mtree.h" #include "extern.h" extern long int crc_total; int ftsoptions = FTS_LOGICAL; int cflag, dflag, eflag, iflag, nflag, rflag, sflag, uflag, Uflag; -u_short keys; +u_int keys; char fullpath[MAXPATHLEN]; static void usage __P((void)); int main(argc, argv) int argc; char *argv[]; { int ch; char *dir, *p; int status; dir = NULL; keys = KEYDEFAULT; while ((ch = getopt(argc, argv, "cdef:iK:k:np:rs:Uux")) != -1) switch((char)ch) { case 'c': cflag = 1; break; case 'd': dflag = 1; break; case 'e': eflag = 1; break; case 'f': if (!(freopen(optarg, "r", stdin))) err(1, "%s", optarg); break; case 'i': iflag = 1; break; case 'K': while ((p = strsep(&optarg, " \t,")) != NULL) if (*p != '\0') keys |= parsekey(p, NULL); break; case 'k': keys = F_TYPE; while ((p = strsep(&optarg, " \t,")) != NULL) if (*p != '\0') keys |= parsekey(p, NULL); break; case 'n': nflag = 1; break; case 'p': dir = optarg; break; case 'r': rflag = 1; break; case 's': sflag = 1; crc_total = ~strtol(optarg, &p, 0); if (*p) errx(1, "illegal seed value -- %s", optarg); case 'U': Uflag = 1; uflag = 1; break; case 'u': uflag = 1; break; case 'x': ftsoptions |= FTS_XDEV; break; case '?': default: usage(); } argc -= optind; argv += optind; if (argc) usage(); if (dir && chdir(dir)) err(1, "%s", dir); if ((cflag || sflag) && !getwd(fullpath)) errx(1, "%s", fullpath); if (cflag) { cwalk(); exit(0); } status = verify(); if (Uflag & (status == MISMATCHEXIT)) status = 0; exit(status); } static void usage() { (void)fprintf(stderr, "usage: mtree [-cdeinrUux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"); exit(1); } Index: head/usr.sbin/mtree/mtree.h =================================================================== --- head/usr.sbin/mtree/mtree.h (revision 36669) +++ head/usr.sbin/mtree/mtree.h (revision 36670) @@ -1,90 +1,92 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. * * @(#)mtree.h 8.1 (Berkeley) 6/6/93 */ #include #include #define KEYDEFAULT \ (F_GID | F_MODE | F_NLINK | F_SIZE | F_SLINK | F_TIME | F_UID) #define MISMATCHEXIT 2 typedef struct _node { struct _node *parent, *child; /* up, down */ struct _node *prev, *next; /* left, right */ off_t st_size; /* size */ struct timespec st_mtimespec; /* last modification time */ u_long cksum; /* check sum */ char *md5digest; /* MD5 digest */ char *slink; /* symbolic link reference */ uid_t st_uid; /* uid */ gid_t st_gid; /* gid */ #define MBITS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) mode_t st_mode; /* mode */ nlink_t st_nlink; /* link count */ #define F_CKSUM 0x0001 /* check sum */ #define F_DONE 0x0002 /* directory done */ #define F_GID 0x0004 /* gid */ #define F_GNAME 0x0008 /* group name */ #define F_IGN 0x0010 /* ignore */ #define F_MAGIC 0x0020 /* name has magic chars */ #define F_MODE 0x0040 /* mode */ #define F_NLINK 0x0080 /* number of links */ #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_NOCHANGE 0x10000 /* If owner/mode "wrong", do */ + /* not change */ + u_int flags; /* items set */ #define F_BLOCK 0x001 /* block special */ #define F_CHAR 0x002 /* char special */ #define F_DIR 0x004 /* directory */ #define F_FIFO 0x008 /* fifo */ #define F_FILE 0x010 /* regular file */ #define F_LINK 0x020 /* symbolic link */ #define F_SOCK 0x040 /* socket */ u_char type; /* file type */ char name[1]; /* file name (must be last) */ } NODE; #define RP(p) \ ((p)->fts_path[0] == '.' && (p)->fts_path[1] == '/' ? \ (p)->fts_path + 2 : (p)->fts_path) Index: head/usr.sbin/mtree/verify.c =================================================================== --- head/usr.sbin/mtree/verify.c (revision 36669) +++ head/usr.sbin/mtree/verify.c (revision 36670) @@ -1,209 +1,210 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 #if 0 static char sccsid[] = "@(#)verify.c 8.1 (Berkeley) 6/6/93"; #endif static const char rcsid[] = - "$Id$"; + "$Id: verify.c,v 1.5 1997/10/01 06:30:02 charnier Exp $"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include "mtree.h" #include "extern.h" extern long int crc_total; extern int ftsoptions; extern int dflag, eflag, rflag, sflag, uflag; extern char fullpath[MAXPATHLEN]; extern int lineno; static NODE *root; static char path[MAXPATHLEN]; static void miss __P((NODE *, char *)); static int vwalk __P((void)); int verify() { int rval; root = spec(); rval = vwalk(); miss(root, path); return (rval); } static int vwalk() { register FTS *t; register FTSENT *p; register NODE *ep, *level; int specdepth, rval; char *argv[2]; argv[0] = "."; argv[1] = NULL; if ((t = fts_open(argv, ftsoptions, NULL)) == NULL) err(1, "line %d: fts_open", lineno); level = root; specdepth = rval = 0; while ((p = fts_read(t))) { switch(p->fts_info) { case FTS_D: break; case FTS_DP: if (specdepth > p->fts_level) { for (level = level->parent; level->prev; level = level->prev); --specdepth; } continue; case FTS_DNR: case FTS_ERR: case FTS_NS: warnx("%s: %s", 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)) || !strcmp(ep->name, p->fts_name)) { ep->flags |= F_VISIT; - if (compare(ep->name, ep, p)) + if ((ep->flags & F_NOCHANGE) == 0 && + compare(ep->name, ep, p)) rval = MISMATCHEXIT; - if (ep->flags & F_IGN) + if (ep->flags & (F_IGN | F_NOCHANGE)) (void)fts_set(t, p, FTS_SKIP); else if (ep->child && ep->type == F_DIR && p->fts_info == FTS_D) { level = ep->child; ++specdepth; } break; } if (ep) continue; extra: if (!eflag) { (void)printf("extra: %s", RP(p)); if (rflag) { if ((S_ISDIR(p->fts_statp->st_mode) ? rmdir : unlink)(p->fts_accpath)) { (void)printf(", not removed: %s", strerror(errno)); } else (void)printf(", removed"); } (void)putchar('\n'); } (void)fts_set(t, p, FTS_SKIP); } (void)fts_close(t); if (sflag) warnx("%s checksum: %lu", fullpath, crc_total); return (rval); } static void miss(p, tail) register NODE *p; register char *tail; { register int create; register char *tp; for (; p; p = p->next) { if (p->type != F_DIR && (dflag || p->flags & F_VISIT)) continue; (void)strcpy(tail, p->name); if (!(p->flags & F_VISIT)) (void)printf("missing: %s", path); if (p->type != F_DIR) { putchar('\n'); continue; } create = 0; if (!(p->flags & F_VISIT) && uflag) if (!(p->flags & (F_UID | F_UNAME))) - (void)printf(" (not created: user not specified)"); + (void)printf(" (directory not created: user not specified)"); else if (!(p->flags & (F_GID | F_GNAME))) - (void)printf(" (not created: group not specified)"); + (void)printf(" (directory not created: group not specified)"); else if (!(p->flags & F_MODE)) - (void)printf(" (not created: mode not specified)"); + (void)printf(" (directory not created: mode not specified)"); else if (mkdir(path, S_IRWXU)) - (void)printf(" (not created: %s)", + (void)printf(" (directory not created: %s)", strerror(errno)); else { create = 1; (void)printf(" (created)"); } if (!(p->flags & F_VISIT)) (void)putchar('\n'); for (tp = tail; *tp; ++tp); *tp = '/'; miss(p->child, tp + 1); *tp = '\0'; if (!create) continue; if (chown(path, p->st_uid, p->st_gid)) { (void)printf("%s: user/group/mode not modified: %s\n", path, strerror(errno)); continue; } if (chmod(path, p->st_mode)) (void)printf("%s: permissions not set: %s\n", path, strerror(errno)); } }