compare.c revision 66584
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1989, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
3530027Scharnier#if 0
361553Srgrimesstatic char sccsid[] = "@(#)compare.c	8.1 (Berkeley) 6/6/93";
3730027Scharnier#endif
3830027Scharnierstatic const char rcsid[] =
3950479Speter  "$FreeBSD: head/usr.sbin/mtree/compare.c 66584 2000-10-03 13:13:47Z phk $";
401553Srgrimes#endif /* not lint */
411553Srgrimes
421553Srgrimes#include <sys/param.h>
431553Srgrimes#include <sys/stat.h>
4430027Scharnier#include <err.h>
4530027Scharnier#include <errno.h>
461553Srgrimes#include <fcntl.h>
471553Srgrimes#include <fts.h>
4844303Swollman#ifdef MD5
4930027Scharnier#include <md5.h>
5044303Swollman#endif
5144303Swollman#ifdef SHA1
5244303Swollman#include <sha.h>
5344303Swollman#endif
5444303Swollman#ifdef RMD160
5544303Swollman#include <ripemd.h>
5644303Swollman#endif
571553Srgrimes#include <stdio.h>
581553Srgrimes#include <time.h>
591553Srgrimes#include <unistd.h>
601553Srgrimes#include "mtree.h"
611553Srgrimes#include "extern.h"
621553Srgrimes
631553Srgrimesextern int uflag;
6430027Scharnierextern int lineno;
651553Srgrimes
661553Srgrimesstatic char *ftype __P((u_int));
671553Srgrimes
681553Srgrimes#define	INDENTNAMELEN	8
691553Srgrimes#define	LABEL \
701553Srgrimes	if (!label++) { \
7166584Sphk		len = printf("%s changed\n", RP(p)); \
7266584Sphk		tab = "\t"; \
731553Srgrimes	}
741553Srgrimes
751553Srgrimesint
761553Srgrimescompare(name, s, p)
771553Srgrimes	char *name;
781553Srgrimes	register NODE *s;
791553Srgrimes	register FTSENT *p;
801553Srgrimes{
811553Srgrimes	extern int uflag;
821553Srgrimes	u_long len, val;
831553Srgrimes	int fd, label;
842860Srgrimes	char *cp, *tab = "";
8561749Sjoe	char *fflags;
861553Srgrimes
871553Srgrimes	label = 0;
881553Srgrimes	switch(s->type) {
891553Srgrimes	case F_BLOCK:
901553Srgrimes		if (!S_ISBLK(p->fts_statp->st_mode))
911553Srgrimes			goto typeerr;
921553Srgrimes		break;
931553Srgrimes	case F_CHAR:
941553Srgrimes		if (!S_ISCHR(p->fts_statp->st_mode))
951553Srgrimes			goto typeerr;
961553Srgrimes		break;
971553Srgrimes	case F_DIR:
981553Srgrimes		if (!S_ISDIR(p->fts_statp->st_mode))
991553Srgrimes			goto typeerr;
1001553Srgrimes		break;
1011553Srgrimes	case F_FIFO:
1021553Srgrimes		if (!S_ISFIFO(p->fts_statp->st_mode))
1031553Srgrimes			goto typeerr;
1041553Srgrimes		break;
1051553Srgrimes	case F_FILE:
1061553Srgrimes		if (!S_ISREG(p->fts_statp->st_mode))
1071553Srgrimes			goto typeerr;
1081553Srgrimes		break;
1091553Srgrimes	case F_LINK:
1101553Srgrimes		if (!S_ISLNK(p->fts_statp->st_mode))
1111553Srgrimes			goto typeerr;
1121553Srgrimes		break;
1131553Srgrimes	case F_SOCK:
1141553Srgrimes		if (!S_ISSOCK(p->fts_statp->st_mode)) {
1151553Srgrimestypeerr:		LABEL;
11666584Sphk			(void)printf("\ttype expected %s found %s\n",
1171553Srgrimes			    ftype(s->type), inotype(p->fts_statp->st_mode));
1181553Srgrimes		}
1191553Srgrimes		break;
1201553Srgrimes	}
1211553Srgrimes	/* Set the uid/gid first, then set the mode. */
1221553Srgrimes	if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
1231553Srgrimes		LABEL;
12466584Sphk		(void)printf("%suser expected %lu found %lu",
12538020Sbde		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
1261553Srgrimes		if (uflag)
1271553Srgrimes			if (chown(p->fts_accpath, s->st_uid, -1))
12866584Sphk				(void)printf(" not modified: %s\n",
1291553Srgrimes				    strerror(errno));
1301553Srgrimes			else
13166584Sphk				(void)printf(" modified\n");
1321553Srgrimes		else
13366584Sphk			(void)printf("\n");
1341553Srgrimes		tab = "\t";
1351553Srgrimes	}
1361553Srgrimes	if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
1371553Srgrimes		LABEL;
13866584Sphk		(void)printf("%sgid expected %lu found %lu",
13938020Sbde		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
1401553Srgrimes		if (uflag)
1411553Srgrimes			if (chown(p->fts_accpath, -1, s->st_gid))
14266584Sphk				(void)printf(" not modified: %s\n",
1431553Srgrimes				    strerror(errno));
1441553Srgrimes			else
14566584Sphk				(void)printf(" modified\n");
1461553Srgrimes		else
14766584Sphk			(void)printf("\n");
1481553Srgrimes		tab = "\t";
1491553Srgrimes	}
1501553Srgrimes	if (s->flags & F_MODE &&
1511553Srgrimes	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
1521553Srgrimes		LABEL;
15366584Sphk		(void)printf("%spermissions expected %#o found %#o",
1541553Srgrimes		    tab, s->st_mode, p->fts_statp->st_mode & MBITS);
1551553Srgrimes		if (uflag)
1561553Srgrimes			if (chmod(p->fts_accpath, s->st_mode))
15766584Sphk				(void)printf(" not modified: %s\n",
1581553Srgrimes				    strerror(errno));
1591553Srgrimes			else
16066584Sphk				(void)printf(" modified\n");
1611553Srgrimes		else
16266584Sphk			(void)printf("\n");
1631553Srgrimes		tab = "\t";
1641553Srgrimes	}
1651553Srgrimes	if (s->flags & F_NLINK && s->type != F_DIR &&
1661553Srgrimes	    s->st_nlink != p->fts_statp->st_nlink) {
1671553Srgrimes		LABEL;
16866584Sphk		(void)printf("%slink_count expected %u found %u\n",
1691553Srgrimes		    tab, s->st_nlink, p->fts_statp->st_nlink);
1701553Srgrimes		tab = "\t";
1711553Srgrimes	}
17266584Sphk	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size &&
17366584Sphk		!S_ISDIR(p->fts_statp->st_mode)) {
1741553Srgrimes		LABEL;
17566584Sphk		(void)printf("%ssize expected %qd found %qd\n",
1761553Srgrimes		    tab, s->st_size, p->fts_statp->st_size);
1771553Srgrimes		tab = "\t";
1781553Srgrimes	}
1791553Srgrimes	/*
1801553Srgrimes	 * XXX
1811553Srgrimes	 * Catches nano-second differences, but doesn't display them.
1821553Srgrimes	 */
1833052Sdg	if ((s->flags & F_TIME) &&
18418404Snate	     ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) ||
18518404Snate	     (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) {
1861553Srgrimes		LABEL;
18766584Sphk		(void)printf("%smodification time expected %.24s ",
18818404Snate		    tab, ctime(&s->st_mtimespec.tv_sec));
18966584Sphk		(void)printf("found %.24s\n",
19018404Snate		    ctime(&p->fts_statp->st_mtimespec.tv_sec));
1911553Srgrimes		tab = "\t";
1921553Srgrimes	}
19351705Sbillf	if (s->flags & F_CKSUM) {
1941553Srgrimes		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
1951553Srgrimes			LABEL;
1961553Srgrimes			(void)printf("%scksum: %s: %s\n",
1971553Srgrimes			    tab, p->fts_accpath, strerror(errno));
1981553Srgrimes			tab = "\t";
1991553Srgrimes		} else if (crc(fd, &val, &len)) {
2001553Srgrimes			(void)close(fd);
2011553Srgrimes			LABEL;
2021553Srgrimes			(void)printf("%scksum: %s: %s\n",
2031553Srgrimes			    tab, p->fts_accpath, strerror(errno));
2041553Srgrimes			tab = "\t";
2051553Srgrimes		} else {
2061553Srgrimes			(void)close(fd);
2071553Srgrimes			if (s->cksum != val) {
2081553Srgrimes				LABEL;
20966584Sphk				(void)printf("%scksum expected %lu found %lu\n",
2101553Srgrimes				    tab, s->cksum, val);
2111553Srgrimes			}
2121553Srgrimes			tab = "\t";
2131553Srgrimes		}
21451705Sbillf	}
21554375Sjoe	/*
21654375Sjoe	 * XXX
21754375Sjoe	 * since chflags(2) will reset file times, the utimes() above
21854375Sjoe	 * may have been useless!  oh well, we'd rather have correct
21954375Sjoe	 * flags, rather than times?
22054375Sjoe	 */
22154375Sjoe	if ((s->flags & F_FLAGS) && s->st_flags != p->fts_statp->st_flags) {
22254375Sjoe		LABEL;
22361749Sjoe		fflags = flags_to_string(s->st_flags);
22466584Sphk		(void)printf("%sflags expected \"%s\"", tab, fflags);
22561749Sjoe		free(fflags);
22661749Sjoe
22761749Sjoe		fflags = flags_to_string(p->fts_statp->st_flags);
22866584Sphk		(void)printf(" found \"%s\"", fflags);
22961749Sjoe		free(fflags);
23061749Sjoe
23154375Sjoe		if (uflag)
23254375Sjoe			if (chflags(p->fts_accpath, s->st_flags))
23366584Sphk				(void)printf(" not modified: %s\n",
23454375Sjoe				    strerror(errno));
23554375Sjoe			else
23666584Sphk				(void)printf(" modified\n");
23754375Sjoe		else
23866584Sphk			(void)printf("\n");
23954375Sjoe		tab = "\t";
24054375Sjoe	}
24144303Swollman#ifdef MD5
2426286Swollman	if (s->flags & F_MD5) {
2439490Sphk		char *new_digest, buf[33];
2448857Srgrimes
24544303Swollman		new_digest = MD5File(p->fts_accpath, buf);
2466286Swollman		if (!new_digest) {
2476286Swollman			LABEL;
24866584Sphk			printf("%sMD5: %s: %s\n", tab, p->fts_accpath,
2496286Swollman			       strerror(errno));
2506286Swollman			tab = "\t";
2516286Swollman		} else if (strcmp(new_digest, s->md5digest)) {
2526286Swollman			LABEL;
25366584Sphk			printf("%sMD5 expected %s found %s\n", tab, s->md5digest,
2546286Swollman			       new_digest);
2556286Swollman			tab = "\t";
2566286Swollman		}
2576286Swollman	}
25844303Swollman#endif /* MD5 */
25944303Swollman#ifdef SHA1
26044303Swollman	if (s->flags & F_SHA1) {
26144303Swollman		char *new_digest, buf[41];
2626286Swollman
26344303Swollman		new_digest = SHA1_File(p->fts_accpath, buf);
26444303Swollman		if (!new_digest) {
26544303Swollman			LABEL;
26666584Sphk			printf("%sSHA-1: %s: %s\n", tab, p->fts_accpath,
26744303Swollman			       strerror(errno));
26844303Swollman			tab = "\t";
26944303Swollman		} else if (strcmp(new_digest, s->sha1digest)) {
27044303Swollman			LABEL;
27166584Sphk			printf("%sSHA-1 expected %s found %s\n",
27266584Sphk			       tab, s->sha1digest, new_digest);
27344303Swollman			tab = "\t";
27444303Swollman		}
27544303Swollman	}
27644303Swollman#endif /* SHA1 */
27744303Swollman#ifdef RMD160
27844303Swollman	if (s->flags & F_RMD160) {
27944303Swollman		char *new_digest, buf[41];
28044303Swollman
28144303Swollman		new_digest = RIPEMD160_File(p->fts_accpath, buf);
28244303Swollman		if (!new_digest) {
28344303Swollman			LABEL;
28466584Sphk			printf("%sRIPEMD160: %s: %s\n", tab,
28544303Swollman			       p->fts_accpath, strerror(errno));
28644303Swollman			tab = "\t";
28744303Swollman		} else if (strcmp(new_digest, s->rmd160digest)) {
28844303Swollman			LABEL;
28966584Sphk			printf("%sRIPEMD160 expected %s found %s\n",
29066584Sphk			       tab, s->rmd160digest, new_digest);
29144303Swollman			tab = "\t";
29244303Swollman		}
29344303Swollman	}
29444303Swollman#endif /* RMD160 */
29544303Swollman
29665812Ssheldonh	if (s->flags & F_SLINK &&
29765812Ssheldonh	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
2981553Srgrimes		LABEL;
29966584Sphk		(void)printf("%slink_ref expected %s found %s\n",
30066584Sphk		      tab, cp, s->slink);
3011553Srgrimes	}
3021553Srgrimes	return (label);
3031553Srgrimes}
3041553Srgrimes
3051553Srgrimeschar *
3061553Srgrimesinotype(type)
3071553Srgrimes	u_int type;
3081553Srgrimes{
3091553Srgrimes	switch(type & S_IFMT) {
3101553Srgrimes	case S_IFBLK:
3111553Srgrimes		return ("block");
3121553Srgrimes	case S_IFCHR:
3131553Srgrimes		return ("char");
3141553Srgrimes	case S_IFDIR:
3151553Srgrimes		return ("dir");
3161553Srgrimes	case S_IFIFO:
3171553Srgrimes		return ("fifo");
3181553Srgrimes	case S_IFREG:
3191553Srgrimes		return ("file");
3201553Srgrimes	case S_IFLNK:
3211553Srgrimes		return ("link");
3221553Srgrimes	case S_IFSOCK:
3231553Srgrimes		return ("socket");
3241553Srgrimes	default:
3251553Srgrimes		return ("unknown");
3261553Srgrimes	}
3271553Srgrimes	/* NOTREACHED */
3281553Srgrimes}
3291553Srgrimes
3301553Srgrimesstatic char *
3311553Srgrimesftype(type)
3321553Srgrimes	u_int type;
3331553Srgrimes{
3341553Srgrimes	switch(type) {
3351553Srgrimes	case F_BLOCK:
3361553Srgrimes		return ("block");
3371553Srgrimes	case F_CHAR:
3381553Srgrimes		return ("char");
3391553Srgrimes	case F_DIR:
3401553Srgrimes		return ("dir");
3411553Srgrimes	case F_FIFO:
3421553Srgrimes		return ("fifo");
3431553Srgrimes	case F_FILE:
3441553Srgrimes		return ("file");
3451553Srgrimes	case F_LINK:
3461553Srgrimes		return ("link");
3471553Srgrimes	case F_SOCK:
3481553Srgrimes		return ("socket");
3491553Srgrimes	default:
3501553Srgrimes		return ("unknown");
3511553Srgrimes	}
3521553Srgrimes	/* NOTREACHED */
3531553Srgrimes}
3541553Srgrimes
3551553Srgrimeschar *
3561553Srgrimesrlink(name)
3571553Srgrimes	char *name;
3581553Srgrimes{
3591553Srgrimes	static char lbuf[MAXPATHLEN];
3601553Srgrimes	register int len;
3611553Srgrimes
36236796Simp	if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
36330027Scharnier		err(1, "line %d: %s", lineno, name);
3641553Srgrimes	lbuf[len] = '\0';
3651553Srgrimes	return (lbuf);
3661553Srgrimes}
367