xref: /netbsd-src/usr.sbin/mtree/compare.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: compare.c,v 1.36 2001/11/10 14:58:20 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)compare.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: compare.c,v 1.36 2001/11/10 14:58:20 lukem Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <fts.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <time.h>
53 #include <unistd.h>
54 
55 #ifndef NO_MD5
56 #include <md5.h>
57 #endif
58 #ifndef NO_RMD160
59 #include <rmd160.h>
60 #endif
61 #ifndef NO_SHA1
62 #include <sha1.h>
63 #endif
64 
65 #include "mtree.h"
66 #include "extern.h"
67 
68 #define	INDENTNAMELEN	8
69 #define MARK								\
70 do {									\
71 	len = printf("%s: ", RP(p));					\
72 	if (len > INDENTNAMELEN) {					\
73 		tab = "\t";						\
74 		printf("\n");						\
75 	} else {							\
76 		tab = "";						\
77 		printf("%*s", INDENTNAMELEN - (int)len, "");		\
78 	}								\
79 } while (0)
80 #define	LABEL if (!label++) MARK
81 
82 #define CHANGEFLAGS(path, oflags)					\
83 	if (flags != (oflags)) {					\
84 		if (!label) {						\
85 			MARK;						\
86 			printf("%sflags (\"%s\"", tab,			\
87 			    flags_to_string(p->fts_statp->st_flags, "none")); \
88 		}							\
89 		if (chflags(path, flags)) {				\
90 			label++;					\
91 			printf(", not modified: %s)\n",			\
92 			    strerror(errno));				\
93 		} else							\
94 			printf(", modified to \"%s\")\n",		\
95 			     flags_to_string(flags, "none"));		\
96 	}
97 
98 /* SETFLAGS:
99  * given pflags, additionally set those flags specified in sflags and
100  * selected by mask (the other flags are left unchanged). oflags is
101  * passed as reference to check if chflags is necessary.
102  */
103 #define SETFLAGS(path, sflags, pflags, oflags, mask)			\
104 do {									\
105 	flags = ((sflags) & (mask)) | (pflags);				\
106 	CHANGEFLAGS(path, oflags);					\
107 } while (0)
108 
109 /* CLEARFLAGS:
110  * given pflags, reset the flags specified in sflags and selected by mask
111  * (the other flags are left unchanged). oflags is
112  * passed as reference to check if chflags is necessary.
113  */
114 #define CLEARFLAGS(path, sflags, pflags, oflags, mask)			\
115 do {									\
116 	flags = (~((sflags) & (mask)) & CH_MASK) & (pflags);		\
117 	CHANGEFLAGS(path, oflags);					\
118 } while (0)
119 
120 int
121 compare(NODE *s, FTSENT *p)
122 {
123 	u_int32_t len, val, flags;
124 	int fd, label;
125 	const char *cp, *tab;
126 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1)
127 	char digestbuf[41];	/* large enough for {MD5,RMD160,SHA1}File() */
128 #endif
129 
130 	tab = NULL;
131 	label = 0;
132 	switch(s->type) {
133 	case F_BLOCK:
134 		if (!S_ISBLK(p->fts_statp->st_mode))
135 			goto typeerr;
136 		break;
137 	case F_CHAR:
138 		if (!S_ISCHR(p->fts_statp->st_mode))
139 			goto typeerr;
140 		break;
141 	case F_DIR:
142 		if (!S_ISDIR(p->fts_statp->st_mode))
143 			goto typeerr;
144 		break;
145 	case F_FIFO:
146 		if (!S_ISFIFO(p->fts_statp->st_mode))
147 			goto typeerr;
148 		break;
149 	case F_FILE:
150 		if (!S_ISREG(p->fts_statp->st_mode))
151 			goto typeerr;
152 		break;
153 	case F_LINK:
154 		if (!S_ISLNK(p->fts_statp->st_mode))
155 			goto typeerr;
156 		break;
157 	case F_SOCK:
158 		if (!S_ISSOCK(p->fts_statp->st_mode)) {
159  typeerr:		LABEL;
160 			printf("\ttype (%s, %s)\n",
161 			    nodetype(s->type), inotype(p->fts_statp->st_mode));
162 			return (label);
163 		}
164 		break;
165 	}
166 	if (Wflag)
167 		goto afterpermwhack;
168 	if (iflag && !uflag) {
169 		if (s->flags & F_FLAGS)
170 		    SETFLAGS(p->fts_accpath, s->st_flags,
171 			p->fts_statp->st_flags, p->fts_statp->st_flags,
172 			SP_FLGS);
173 		return (label);
174         }
175 	if (mflag && !uflag) {
176 		if (s->flags & F_FLAGS)
177 		    CLEARFLAGS(p->fts_accpath, s->st_flags,
178 			p->fts_statp->st_flags, p->fts_statp->st_flags,
179 			SP_FLGS);
180 		return (label);
181         }
182 	if (s->flags & F_DEV &&
183 	    (s->type == F_BLOCK || s->type == F_CHAR) &&
184 	    s->st_rdev != p->fts_statp->st_rdev) {
185 		LABEL;
186 		printf("%sdevice (%#x, %#x",
187 		    tab, s->st_rdev, p->fts_statp->st_rdev);
188 		if (uflag) {
189 			if ((unlink(p->fts_accpath) == -1) ||
190 			    (mknod(p->fts_accpath,
191 			      s->st_mode | nodetoino(s->type),
192 			      s->st_rdev) == -1) ||
193 			    (chown(p->fts_accpath, p->fts_statp->st_uid,
194 			      p->fts_statp->st_gid) == -1) )
195 				printf(", not modified: %s)\n",
196 				    strerror(errno));
197 			 else
198 				printf(", modified)\n");
199 		} else
200 			printf(")\n");
201 		tab = "\t";
202 	}
203 	/* Set the uid/gid first, then set the mode. */
204 	if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
205 		LABEL;
206 		printf("%suser (%lu, %lu",
207 		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
208 		if (uflag) {
209 			if (chown(p->fts_accpath, s->st_uid, -1))
210 				printf(", not modified: %s)\n",
211 				    strerror(errno));
212 			else
213 				printf(", modified)\n");
214 		} else
215 			printf(")\n");
216 		tab = "\t";
217 	}
218 	if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
219 		LABEL;
220 		printf("%sgid (%lu, %lu",
221 		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
222 		if (uflag) {
223 			if (chown(p->fts_accpath, -1, s->st_gid))
224 				printf(", not modified: %s)\n",
225 				    strerror(errno));
226 			else
227 				printf(", modified)\n");
228 		}
229 		else
230 			printf(")\n");
231 		tab = "\t";
232 	}
233 	if (s->flags & F_MODE &&
234 	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
235 		if (lflag) {
236 			mode_t tmode, mode;
237 
238 			tmode = s->st_mode;
239 			mode = p->fts_statp->st_mode & MBITS;
240 			/*
241 			 * if none of the suid/sgid/etc bits are set,
242 			 * then if the mode is a subset of the target,
243 			 * skip.
244 			 */
245 			if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
246 			    (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
247 				if ((mode | tmode) == tmode)
248 					goto skip;
249 		}
250 
251 		LABEL;
252 		printf("%spermissions (%#lo, %#lo",
253 		    tab, (u_long)s->st_mode,
254 		    (u_long)p->fts_statp->st_mode & MBITS);
255 		if (uflag) {
256 			if (chmod(p->fts_accpath, s->st_mode))
257 				printf(", not modified: %s)\n",
258 				    strerror(errno));
259 			else
260 				printf(", modified)\n");
261 		}
262 		else
263 			printf(")\n");
264 		tab = "\t";
265 	skip:	;
266 	}
267 	if (s->flags & F_NLINK && s->type != F_DIR &&
268 	    s->st_nlink != p->fts_statp->st_nlink) {
269 		LABEL;
270 		printf("%slink count (%lu, %lu)\n",
271 		    tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
272 		tab = "\t";
273 	}
274 	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
275 		LABEL;
276 		printf("%ssize (%lld, %lld)\n",
277 		    tab, (long long)s->st_size,
278 		    (long long)p->fts_statp->st_size);
279 		tab = "\t";
280 	}
281 	/*
282 	 * XXX
283 	 * Since utimes(2) only takes a timeval, there's no point in
284 	 * comparing the low bits of the timespec nanosecond field.  This
285 	 * will only result in mismatches that we can never fix.
286 	 *
287 	 * Doesn't display microsecond differences.
288 	 */
289 	if (s->flags & F_TIME) {
290 		struct timeval tv[2];
291 		struct stat *ps = p->fts_statp;
292 		time_t smtime = s->st_mtimespec.tv_sec;
293 
294 #ifdef BSD4_4
295 		time_t pmtime = ps->st_mtimespec.tv_sec;
296 
297 		TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
298 #else
299 		time_t pmtime = (time_t)ps->st_mtime;
300 
301 		tv[1].tv_sec = ps->st_mtime;
302 		tv[1].tv_usec = 0;
303 #endif
304 		TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
305 
306 		if (tv[0].tv_sec != tv[1].tv_sec ||
307 		    tv[0].tv_usec != tv[1].tv_usec) {
308 			LABEL;
309 			printf("%smodification time (%.24s, ",
310 			    tab, ctime(&smtime));
311 			printf("%.24s", ctime(&pmtime));
312 			if (tflag) {
313 				tv[1] = tv[0];
314 				if (utimes(p->fts_accpath, tv))
315 					printf(", not modified: %s)\n",
316 					    strerror(errno));
317 				else
318 					printf(", modified)\n");
319 			} else
320 				printf(")\n");
321 			tab = "\t";
322 		}
323 	}
324 	/*
325 	 * XXX
326 	 * since chflags(2) will reset file times, the utimes() above
327 	 * may have been useless!  oh well, we'd rather have correct
328 	 * flags, rather than times?
329 	 */
330         if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
331 	    || mflag || iflag)) {
332 		if (s->st_flags != p->fts_statp->st_flags) {
333 			LABEL;
334 			printf("%sflags (\"%s\" is not ", tab,
335 			    flags_to_string(s->st_flags, "none"));
336 			printf("\"%s\"",
337 			    flags_to_string(p->fts_statp->st_flags, "none"));
338 		}
339 		if (uflag) {
340 			if (iflag)
341 				SETFLAGS(p->fts_accpath, s->st_flags,
342 				    0, p->fts_statp->st_flags, CH_MASK);
343 			else if (mflag)
344 				CLEARFLAGS(p->fts_accpath, s->st_flags,
345 				    0, p->fts_statp->st_flags, SP_FLGS);
346 			else
347 				SETFLAGS(p->fts_accpath, s->st_flags,
348 			     	    0, p->fts_statp->st_flags,
349 				    (~SP_FLGS & CH_MASK));
350 		} else
351 			printf(")\n");
352 		tab = "\t";
353 	}
354 
355 	/*
356 	 * from this point, no more permission checking or whacking
357 	 * occurs, only checking of stuff like checksums and symlinks.
358 	 */
359  afterpermwhack:
360 	if (s->flags & F_CKSUM) {
361 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
362 			LABEL;
363 			printf("%scksum: %s: %s\n",
364 			    tab, p->fts_accpath, strerror(errno));
365 			tab = "\t";
366 		} else if (crc(fd, &val, &len)) {
367 			close(fd);
368 			LABEL;
369 			printf("%scksum: %s: %s\n",
370 			    tab, p->fts_accpath, strerror(errno));
371 			tab = "\t";
372 		} else {
373 			close(fd);
374 			if (s->cksum != val) {
375 				LABEL;
376 				printf("%scksum (%lu, %lu)\n",
377 				    tab, s->cksum, (unsigned long)val);
378 			}
379 			tab = "\t";
380 		}
381 	}
382 #ifndef NO_MD5
383 	if (s->flags & F_MD5) {
384 		if (MD5File(p->fts_accpath, digestbuf) == NULL) {
385 			LABEL;
386 			printf("%smd5: %s: %s\n",
387 			    tab, p->fts_accpath, strerror(errno));
388 			tab = "\t";
389 		} else {
390 			if (strcmp(s->md5digest, digestbuf)) {
391 				LABEL;
392 				printf("%smd5 (0x%s, 0x%s)\n",
393 				    tab, s->md5digest, digestbuf);
394 			}
395 			tab = "\t";
396 		}
397 	}
398 #endif	/* ! NO_MD5 */
399 #ifndef NO_RMD160
400 	if (s->flags & F_RMD160) {
401 		if (RMD160File(p->fts_accpath, digestbuf) == NULL) {
402 			LABEL;
403 			printf("%srmd160: %s: %s\n",
404 			    tab, p->fts_accpath, strerror(errno));
405 			tab = "\t";
406 		} else {
407 			if (strcmp(s->rmd160digest, digestbuf)) {
408 				LABEL;
409 				printf("%srmd160 (0x%s, 0x%s)\n",
410 				    tab, s->rmd160digest, digestbuf);
411 			}
412 			tab = "\t";
413 		}
414 	}
415 #endif	/* ! NO_RMD160 */
416 #ifndef NO_SHA1
417 	if (s->flags & F_SHA1) {
418 		if (SHA1File(p->fts_accpath, digestbuf) == NULL) {
419 			LABEL;
420 			printf("%ssha1: %s: %s\n",
421 			    tab, p->fts_accpath, strerror(errno));
422 			tab = "\t";
423 		} else {
424 			if (strcmp(s->sha1digest, digestbuf)) {
425 				LABEL;
426 				printf("%ssha1 (0x%s, 0x%s)\n",
427 				    tab, s->sha1digest, digestbuf);
428 			}
429 			tab = "\t";
430 		}
431 	}
432 #endif	/* ! NO_SHA1 */
433 	if (s->flags & F_SLINK &&
434 	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
435 		LABEL;
436 		printf("%slink ref (%s, %s", tab, cp, s->slink);
437 		if (uflag) {
438 			if ((unlink(p->fts_accpath) == -1) ||
439 			    (symlink(s->slink, p->fts_accpath) == -1) )
440 				printf(", not modified: %s)\n",
441 				    strerror(errno));
442 			else
443 				printf(", modified)\n");
444 		} else
445 			printf(")\n");
446 	}
447 	return (label);
448 }
449 
450 const char *
451 rlink(const char *name)
452 {
453 	static char lbuf[MAXPATHLEN];
454 	int len;
455 
456 	if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
457 		mtree_err("%s: %s", name, strerror(errno));
458 	lbuf[len] = '\0';
459 	return (lbuf);
460 }
461