compare.c revision 121299
1/*-
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static char sccsid[] = "@(#)compare.c	8.1 (Berkeley) 6/6/93";
37#endif /* not lint */
38#endif
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/usr.sbin/mtree/compare.c 121299 2003-10-21 07:58:52Z phk $");
41
42#include <sys/param.h>
43#include <sys/stat.h>
44#include <err.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <fts.h>
48#ifdef MD5
49#include <md5.h>
50#endif
51#ifdef SHA1
52#include <sha.h>
53#endif
54#ifdef RMD160
55#include <ripemd.h>
56#endif
57#include <stdint.h>
58#include <stdio.h>
59#include <time.h>
60#include <unistd.h>
61#include "mtree.h"
62#include "extern.h"
63
64extern int uflag;
65extern int lineno;
66
67static const char *ftype(u_int);
68
69#define	INDENTNAMELEN	8
70#define	LABEL \
71	if (!label++) { \
72		len = printf("%s changed\n", RP(p)); \
73		tab = "\t"; \
74	}
75
76int
77compare(char *name __unused, NODE *s, FTSENT *p)
78{
79	uint32_t val;
80	int fd, label;
81	off_t len;
82	char *cp;
83	const char *tab = "";
84	char *fflags;
85
86	label = 0;
87	switch(s->type) {
88	case F_BLOCK:
89		if (!S_ISBLK(p->fts_statp->st_mode))
90			goto typeerr;
91		break;
92	case F_CHAR:
93		if (!S_ISCHR(p->fts_statp->st_mode))
94			goto typeerr;
95		break;
96	case F_DIR:
97		if (!S_ISDIR(p->fts_statp->st_mode))
98			goto typeerr;
99		break;
100	case F_FIFO:
101		if (!S_ISFIFO(p->fts_statp->st_mode))
102			goto typeerr;
103		break;
104	case F_FILE:
105		if (!S_ISREG(p->fts_statp->st_mode))
106			goto typeerr;
107		break;
108	case F_LINK:
109		if (!S_ISLNK(p->fts_statp->st_mode))
110			goto typeerr;
111		break;
112	case F_SOCK:
113		if (!S_ISSOCK(p->fts_statp->st_mode)) {
114typeerr:		LABEL;
115			(void)printf("\ttype expected %s found %s\n",
116			    ftype(s->type), inotype(p->fts_statp->st_mode));
117			return (label);
118		}
119		break;
120	}
121	/* Set the uid/gid first, then set the mode. */
122	if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
123		LABEL;
124		(void)printf("%suser expected %lu found %lu",
125		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
126		if (uflag)
127			if (chown(p->fts_accpath, s->st_uid, -1))
128				(void)printf(" not modified: %s\n",
129				    strerror(errno));
130			else
131				(void)printf(" modified\n");
132		else
133			(void)printf("\n");
134		tab = "\t";
135	}
136	if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
137		LABEL;
138		(void)printf("%sgid expected %lu found %lu",
139		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
140		if (uflag)
141			if (chown(p->fts_accpath, -1, s->st_gid))
142				(void)printf(" not modified: %s\n",
143				    strerror(errno));
144			else
145				(void)printf(" modified\n");
146		else
147			(void)printf("\n");
148		tab = "\t";
149	}
150	if (s->flags & F_MODE &&
151	    !S_ISLNK(p->fts_statp->st_mode) &&
152	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
153		LABEL;
154		(void)printf("%spermissions expected %#o found %#o",
155		    tab, s->st_mode, p->fts_statp->st_mode & MBITS);
156		if (uflag)
157			if (chmod(p->fts_accpath, s->st_mode))
158				(void)printf(" not modified: %s\n",
159				    strerror(errno));
160			else
161				(void)printf(" modified\n");
162		else
163			(void)printf("\n");
164		tab = "\t";
165	}
166	if (s->flags & F_NLINK && s->type != F_DIR &&
167	    s->st_nlink != p->fts_statp->st_nlink) {
168		LABEL;
169		(void)printf("%slink_count expected %u found %u\n",
170		    tab, s->st_nlink, p->fts_statp->st_nlink);
171		tab = "\t";
172	}
173	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size &&
174		!S_ISDIR(p->fts_statp->st_mode)) {
175		LABEL;
176		(void)printf("%ssize expected %jd found %jd\n", tab,
177		    (intmax_t)s->st_size, (intmax_t)p->fts_statp->st_size);
178		tab = "\t";
179	}
180	/*
181	 * XXX
182	 * Catches nano-second differences, but doesn't display them.
183	 */
184	if ((s->flags & F_TIME) &&
185	     ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) ||
186	     (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) {
187		LABEL;
188		(void)printf("%smodification time expected %.24s ",
189		    tab, ctime(&s->st_mtimespec.tv_sec));
190		(void)printf("found %.24s\n",
191		    ctime(&p->fts_statp->st_mtimespec.tv_sec));
192		tab = "\t";
193	}
194	if (s->flags & F_CKSUM) {
195		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
196			LABEL;
197			(void)printf("%scksum: %s: %s\n",
198			    tab, p->fts_accpath, strerror(errno));
199			tab = "\t";
200		} else if (crc(fd, &val, &len)) {
201			(void)close(fd);
202			LABEL;
203			(void)printf("%scksum: %s: %s\n",
204			    tab, p->fts_accpath, strerror(errno));
205			tab = "\t";
206		} else {
207			(void)close(fd);
208			if (s->cksum != val) {
209				LABEL;
210				(void)printf("%scksum expected %lu found %lu\n",
211				    tab, s->cksum, (unsigned long)val);
212				tab = "\t";
213			}
214		}
215	}
216	/*
217	 * XXX
218	 * since chflags(2) will reset file times, the utimes() above
219	 * may have been useless!  oh well, we'd rather have correct
220	 * flags, rather than times?
221	 */
222	if ((s->flags & F_FLAGS) && s->st_flags != p->fts_statp->st_flags) {
223		LABEL;
224		fflags = flags_to_string(s->st_flags);
225		(void)printf("%sflags expected \"%s\"", tab, fflags);
226		free(fflags);
227
228		fflags = flags_to_string(p->fts_statp->st_flags);
229		(void)printf(" found \"%s\"", fflags);
230		free(fflags);
231
232		if (uflag)
233			if (chflags(p->fts_accpath, s->st_flags))
234				(void)printf(" not modified: %s\n",
235				    strerror(errno));
236			else
237				(void)printf(" modified\n");
238		else
239			(void)printf("\n");
240		tab = "\t";
241	}
242#ifdef MD5
243	if (s->flags & F_MD5) {
244		char *new_digest, buf[33];
245
246		new_digest = MD5File(p->fts_accpath, buf);
247		if (!new_digest) {
248			LABEL;
249			printf("%sMD5: %s: %s\n", tab, p->fts_accpath,
250			       strerror(errno));
251			tab = "\t";
252		} else if (strcmp(new_digest, s->md5digest)) {
253			LABEL;
254			printf("%sMD5 expected %s found %s\n", tab, s->md5digest,
255			       new_digest);
256			tab = "\t";
257		}
258	}
259#endif /* MD5 */
260#ifdef SHA1
261	if (s->flags & F_SHA1) {
262		char *new_digest, buf[41];
263
264		new_digest = SHA1_File(p->fts_accpath, buf);
265		if (!new_digest) {
266			LABEL;
267			printf("%sSHA-1: %s: %s\n", tab, p->fts_accpath,
268			       strerror(errno));
269			tab = "\t";
270		} else if (strcmp(new_digest, s->sha1digest)) {
271			LABEL;
272			printf("%sSHA-1 expected %s found %s\n",
273			       tab, s->sha1digest, new_digest);
274			tab = "\t";
275		}
276	}
277#endif /* SHA1 */
278#ifdef RMD160
279	if (s->flags & F_RMD160) {
280		char *new_digest, buf[41];
281
282		new_digest = RIPEMD160_File(p->fts_accpath, buf);
283		if (!new_digest) {
284			LABEL;
285			printf("%sRIPEMD160: %s: %s\n", tab,
286			       p->fts_accpath, strerror(errno));
287			tab = "\t";
288		} else if (strcmp(new_digest, s->rmd160digest)) {
289			LABEL;
290			printf("%sRIPEMD160 expected %s found %s\n",
291			       tab, s->rmd160digest, new_digest);
292			tab = "\t";
293		}
294	}
295#endif /* RMD160 */
296
297	if (s->flags & F_SLINK &&
298	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
299		LABEL;
300		(void)printf("%slink_ref expected %s found %s\n",
301		      tab, s->slink, cp);
302	}
303	return (label);
304}
305
306const char *
307inotype(u_int type)
308{
309	switch(type & S_IFMT) {
310	case S_IFBLK:
311		return ("block");
312	case S_IFCHR:
313		return ("char");
314	case S_IFDIR:
315		return ("dir");
316	case S_IFIFO:
317		return ("fifo");
318	case S_IFREG:
319		return ("file");
320	case S_IFLNK:
321		return ("link");
322	case S_IFSOCK:
323		return ("socket");
324	default:
325		return ("unknown");
326	}
327	/* NOTREACHED */
328}
329
330static const char *
331ftype(u_int type)
332{
333	switch(type) {
334	case F_BLOCK:
335		return ("block");
336	case F_CHAR:
337		return ("char");
338	case F_DIR:
339		return ("dir");
340	case F_FIFO:
341		return ("fifo");
342	case F_FILE:
343		return ("file");
344	case F_LINK:
345		return ("link");
346	case F_SOCK:
347		return ("socket");
348	default:
349		return ("unknown");
350	}
351	/* NOTREACHED */
352}
353
354char *
355rlink(char *name)
356{
357	static char lbuf[MAXPATHLEN];
358	int len;
359
360	if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
361		err(1, "line %d: %s", lineno, name);
362	lbuf[len] = '\0';
363	return (lbuf);
364}
365