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