compare.c revision 121299
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
34114601Sobrien#if 0
351553Srgrimes#ifndef lint
361553Srgrimesstatic char sccsid[] = "@(#)compare.c	8.1 (Berkeley) 6/6/93";
37114601Sobrien#endif /* not lint */
3830027Scharnier#endif
39114601Sobrien#include <sys/cdefs.h>
40114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/mtree/compare.c 121299 2003-10-21 07:58:52Z phk $");
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
57100070Sdes#include <stdint.h>
581553Srgrimes#include <stdio.h>
591553Srgrimes#include <time.h>
601553Srgrimes#include <unistd.h>
611553Srgrimes#include "mtree.h"
621553Srgrimes#include "extern.h"
631553Srgrimes
641553Srgrimesextern int uflag;
6530027Scharnierextern int lineno;
661553Srgrimes
6799802Salfredstatic const char *ftype(u_int);
681553Srgrimes
691553Srgrimes#define	INDENTNAMELEN	8
701553Srgrimes#define	LABEL \
711553Srgrimes	if (!label++) { \
7266584Sphk		len = printf("%s changed\n", RP(p)); \
7366584Sphk		tab = "\t"; \
741553Srgrimes	}
751553Srgrimes
761553Srgrimesint
77121299Sphkcompare(char *name __unused, NODE *s, FTSENT *p)
781553Srgrimes{
79112214Srobert	uint32_t val;
801553Srgrimes	int fd, label;
81112214Srobert	off_t len;
8299802Salfred	char *cp;
8399802Salfred	const char *tab = "";
8461749Sjoe	char *fflags;
851553Srgrimes
861553Srgrimes	label = 0;
871553Srgrimes	switch(s->type) {
881553Srgrimes	case F_BLOCK:
891553Srgrimes		if (!S_ISBLK(p->fts_statp->st_mode))
901553Srgrimes			goto typeerr;
911553Srgrimes		break;
921553Srgrimes	case F_CHAR:
931553Srgrimes		if (!S_ISCHR(p->fts_statp->st_mode))
941553Srgrimes			goto typeerr;
951553Srgrimes		break;
961553Srgrimes	case F_DIR:
971553Srgrimes		if (!S_ISDIR(p->fts_statp->st_mode))
981553Srgrimes			goto typeerr;
991553Srgrimes		break;
1001553Srgrimes	case F_FIFO:
1011553Srgrimes		if (!S_ISFIFO(p->fts_statp->st_mode))
1021553Srgrimes			goto typeerr;
1031553Srgrimes		break;
1041553Srgrimes	case F_FILE:
1051553Srgrimes		if (!S_ISREG(p->fts_statp->st_mode))
1061553Srgrimes			goto typeerr;
1071553Srgrimes		break;
1081553Srgrimes	case F_LINK:
1091553Srgrimes		if (!S_ISLNK(p->fts_statp->st_mode))
1101553Srgrimes			goto typeerr;
1111553Srgrimes		break;
1121553Srgrimes	case F_SOCK:
1131553Srgrimes		if (!S_ISSOCK(p->fts_statp->st_mode)) {
1141553Srgrimestypeerr:		LABEL;
11566584Sphk			(void)printf("\ttype expected %s found %s\n",
1161553Srgrimes			    ftype(s->type), inotype(p->fts_statp->st_mode));
11766747Sphk			return (label);
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 &&
15166746Sphk	    !S_ISLNK(p->fts_statp->st_mode) &&
1521553Srgrimes	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
1531553Srgrimes		LABEL;
15466584Sphk		(void)printf("%spermissions expected %#o found %#o",
1551553Srgrimes		    tab, s->st_mode, p->fts_statp->st_mode & MBITS);
1561553Srgrimes		if (uflag)
1571553Srgrimes			if (chmod(p->fts_accpath, s->st_mode))
15866584Sphk				(void)printf(" not modified: %s\n",
1591553Srgrimes				    strerror(errno));
1601553Srgrimes			else
16166584Sphk				(void)printf(" modified\n");
1621553Srgrimes		else
16366584Sphk			(void)printf("\n");
1641553Srgrimes		tab = "\t";
1651553Srgrimes	}
1661553Srgrimes	if (s->flags & F_NLINK && s->type != F_DIR &&
1671553Srgrimes	    s->st_nlink != p->fts_statp->st_nlink) {
1681553Srgrimes		LABEL;
16966584Sphk		(void)printf("%slink_count expected %u found %u\n",
1701553Srgrimes		    tab, s->st_nlink, p->fts_statp->st_nlink);
1711553Srgrimes		tab = "\t";
1721553Srgrimes	}
17366584Sphk	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size &&
17466584Sphk		!S_ISDIR(p->fts_statp->st_mode)) {
1751553Srgrimes		LABEL;
176100070Sdes		(void)printf("%ssize expected %jd found %jd\n", tab,
177100070Sdes		    (intmax_t)s->st_size, (intmax_t)p->fts_statp->st_size);
1781553Srgrimes		tab = "\t";
1791553Srgrimes	}
1801553Srgrimes	/*
1811553Srgrimes	 * XXX
1821553Srgrimes	 * Catches nano-second differences, but doesn't display them.
1831553Srgrimes	 */
1843052Sdg	if ((s->flags & F_TIME) &&
18518404Snate	     ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) ||
18618404Snate	     (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) {
1871553Srgrimes		LABEL;
18866584Sphk		(void)printf("%smodification time expected %.24s ",
18918404Snate		    tab, ctime(&s->st_mtimespec.tv_sec));
19066584Sphk		(void)printf("found %.24s\n",
19118404Snate		    ctime(&p->fts_statp->st_mtimespec.tv_sec));
1921553Srgrimes		tab = "\t";
1931553Srgrimes	}
19451705Sbillf	if (s->flags & F_CKSUM) {
1951553Srgrimes		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
1961553Srgrimes			LABEL;
1971553Srgrimes			(void)printf("%scksum: %s: %s\n",
1981553Srgrimes			    tab, p->fts_accpath, strerror(errno));
1991553Srgrimes			tab = "\t";
2001553Srgrimes		} else if (crc(fd, &val, &len)) {
2011553Srgrimes			(void)close(fd);
2021553Srgrimes			LABEL;
2031553Srgrimes			(void)printf("%scksum: %s: %s\n",
2041553Srgrimes			    tab, p->fts_accpath, strerror(errno));
2051553Srgrimes			tab = "\t";
2061553Srgrimes		} else {
2071553Srgrimes			(void)close(fd);
2081553Srgrimes			if (s->cksum != val) {
2091553Srgrimes				LABEL;
21066584Sphk				(void)printf("%scksum expected %lu found %lu\n",
211112214Srobert				    tab, s->cksum, (unsigned long)val);
212112194Stobez				tab = "\t";
2131553Srgrimes			}
2141553Srgrimes		}
21551705Sbillf	}
21654375Sjoe	/*
21754375Sjoe	 * XXX
21854375Sjoe	 * since chflags(2) will reset file times, the utimes() above
21954375Sjoe	 * may have been useless!  oh well, we'd rather have correct
22054375Sjoe	 * flags, rather than times?
22154375Sjoe	 */
22254375Sjoe	if ((s->flags & F_FLAGS) && s->st_flags != p->fts_statp->st_flags) {
22354375Sjoe		LABEL;
22461749Sjoe		fflags = flags_to_string(s->st_flags);
22566584Sphk		(void)printf("%sflags expected \"%s\"", tab, fflags);
22661749Sjoe		free(fflags);
22761749Sjoe
22861749Sjoe		fflags = flags_to_string(p->fts_statp->st_flags);
22966584Sphk		(void)printf(" found \"%s\"", fflags);
23061749Sjoe		free(fflags);
23161749Sjoe
23254375Sjoe		if (uflag)
23354375Sjoe			if (chflags(p->fts_accpath, s->st_flags))
23466584Sphk				(void)printf(" not modified: %s\n",
23554375Sjoe				    strerror(errno));
23654375Sjoe			else
23766584Sphk				(void)printf(" modified\n");
23854375Sjoe		else
23966584Sphk			(void)printf("\n");
24054375Sjoe		tab = "\t";
24154375Sjoe	}
24244303Swollman#ifdef MD5
2436286Swollman	if (s->flags & F_MD5) {
2449490Sphk		char *new_digest, buf[33];
2458857Srgrimes
24644303Swollman		new_digest = MD5File(p->fts_accpath, buf);
2476286Swollman		if (!new_digest) {
2486286Swollman			LABEL;
24966584Sphk			printf("%sMD5: %s: %s\n", tab, p->fts_accpath,
2506286Swollman			       strerror(errno));
2516286Swollman			tab = "\t";
2526286Swollman		} else if (strcmp(new_digest, s->md5digest)) {
2536286Swollman			LABEL;
25466584Sphk			printf("%sMD5 expected %s found %s\n", tab, s->md5digest,
2556286Swollman			       new_digest);
2566286Swollman			tab = "\t";
2576286Swollman		}
2586286Swollman	}
25944303Swollman#endif /* MD5 */
26044303Swollman#ifdef SHA1
26144303Swollman	if (s->flags & F_SHA1) {
26244303Swollman		char *new_digest, buf[41];
2636286Swollman
26444303Swollman		new_digest = SHA1_File(p->fts_accpath, buf);
26544303Swollman		if (!new_digest) {
26644303Swollman			LABEL;
26766584Sphk			printf("%sSHA-1: %s: %s\n", tab, p->fts_accpath,
26844303Swollman			       strerror(errno));
26944303Swollman			tab = "\t";
27044303Swollman		} else if (strcmp(new_digest, s->sha1digest)) {
27144303Swollman			LABEL;
27266584Sphk			printf("%sSHA-1 expected %s found %s\n",
27366584Sphk			       tab, s->sha1digest, new_digest);
27444303Swollman			tab = "\t";
27544303Swollman		}
27644303Swollman	}
27744303Swollman#endif /* SHA1 */
27844303Swollman#ifdef RMD160
27944303Swollman	if (s->flags & F_RMD160) {
28044303Swollman		char *new_digest, buf[41];
28144303Swollman
28244303Swollman		new_digest = RIPEMD160_File(p->fts_accpath, buf);
28344303Swollman		if (!new_digest) {
28444303Swollman			LABEL;
28566584Sphk			printf("%sRIPEMD160: %s: %s\n", tab,
28644303Swollman			       p->fts_accpath, strerror(errno));
28744303Swollman			tab = "\t";
28844303Swollman		} else if (strcmp(new_digest, s->rmd160digest)) {
28944303Swollman			LABEL;
29066584Sphk			printf("%sRIPEMD160 expected %s found %s\n",
29166584Sphk			       tab, s->rmd160digest, new_digest);
29244303Swollman			tab = "\t";
29344303Swollman		}
29444303Swollman	}
29544303Swollman#endif /* RMD160 */
29644303Swollman
29765812Ssheldonh	if (s->flags & F_SLINK &&
29865812Ssheldonh	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
2991553Srgrimes		LABEL;
30066584Sphk		(void)printf("%slink_ref expected %s found %s\n",
301112194Stobez		      tab, s->slink, cp);
3021553Srgrimes	}
3031553Srgrimes	return (label);
3041553Srgrimes}
3051553Srgrimes
30699802Salfredconst char *
307121299Sphkinotype(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
33099802Salfredstatic const char *
331121299Sphkftype(u_int type)
3321553Srgrimes{
3331553Srgrimes	switch(type) {
3341553Srgrimes	case F_BLOCK:
3351553Srgrimes		return ("block");
3361553Srgrimes	case F_CHAR:
3371553Srgrimes		return ("char");
3381553Srgrimes	case F_DIR:
3391553Srgrimes		return ("dir");
3401553Srgrimes	case F_FIFO:
3411553Srgrimes		return ("fifo");
3421553Srgrimes	case F_FILE:
3431553Srgrimes		return ("file");
3441553Srgrimes	case F_LINK:
3451553Srgrimes		return ("link");
3461553Srgrimes	case F_SOCK:
3471553Srgrimes		return ("socket");
3481553Srgrimes	default:
3491553Srgrimes		return ("unknown");
3501553Srgrimes	}
3511553Srgrimes	/* NOTREACHED */
3521553Srgrimes}
3531553Srgrimes
3541553Srgrimeschar *
355121299Sphkrlink(char *name)
3561553Srgrimes{
3571553Srgrimes	static char lbuf[MAXPATHLEN];
358121299Sphk	int len;
3591553Srgrimes
36036796Simp	if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
36130027Scharnier		err(1, "line %d: %s", lineno, name);
3621553Srgrimes	lbuf[len] = '\0';
3631553Srgrimes	return (lbuf);
3641553Srgrimes}
365