xref: /netbsd-src/usr.sbin/mtree/compare.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: compare.c,v 1.51 2007/02/04 08:03:18 elad 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 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(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.51 2007/02/04 08:03:18 elad Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdlib.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 #ifndef NO_SHA2
65 #include <sha2.h>
66 #endif
67 
68 #include "extern.h"
69 
70 #define	INDENTNAMELEN	8
71 #define MARK								\
72 do {									\
73 	len = printf("%s: ", RP(p));					\
74 	if (len > INDENTNAMELEN) {					\
75 		tab = "\t";						\
76 		printf("\n");						\
77 	} else {							\
78 		tab = "";						\
79 		printf("%*s", INDENTNAMELEN - (int)len, "");		\
80 	}								\
81 } while (0)
82 #define	LABEL if (!label++) MARK
83 
84 #if HAVE_STRUCT_STAT_ST_FLAGS
85 
86 
87 #define CHANGEFLAGS							\
88 	if (flags != p->fts_statp->st_flags) {				\
89 		char *sf;						\
90 		if (!label) {						\
91 			MARK;						\
92 			sf = flags_to_string(p->fts_statp->st_flags, "none"); \
93 			printf("%sflags (\"%s\"", tab, sf);		\
94 			free(sf);					\
95 		}							\
96 		if (lchflags(p->fts_accpath, flags)) {			\
97 			label++;					\
98 			printf(", not modified: %s)\n",			\
99 			    strerror(errno));				\
100 		} else {						\
101 			sf = flags_to_string(flags, "none");		\
102 			printf(", modified to \"%s\")\n", sf);		\
103 			free(sf);					\
104 		}							\
105 	}
106 
107 /* SETFLAGS:
108  * given pflags, additionally set those flags specified in s->st_flags and
109  * selected by mask (the other flags are left unchanged).
110  */
111 #define SETFLAGS(pflags, mask)						\
112 do {									\
113 	flags = (s->st_flags & (mask)) | (pflags);			\
114 	CHANGEFLAGS;							\
115 } while (0)
116 
117 /* CLEARFLAGS:
118  * given pflags, reset the flags specified in s->st_flags and selected by mask
119  * (the other flags are left unchanged).
120  */
121 #define CLEARFLAGS(pflags, mask)					\
122 do {									\
123 	flags = (~(s->st_flags & (mask)) & CH_MASK) & (pflags);		\
124 	CHANGEFLAGS;							\
125 } while (0)
126 #endif	/* HAVE_STRUCT_STAT_ST_FLAGS */
127 
128 int
129 compare(NODE *s, FTSENT *p)
130 {
131 	u_int32_t len, val, flags;
132 	int fd, label;
133 	const char *cp, *tab;
134 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
135 	char *digestbuf;
136 #endif
137 
138 	tab = NULL;
139 	label = 0;
140 	switch(s->type) {
141 	case F_BLOCK:
142 		if (!S_ISBLK(p->fts_statp->st_mode))
143 			goto typeerr;
144 		break;
145 	case F_CHAR:
146 		if (!S_ISCHR(p->fts_statp->st_mode))
147 			goto typeerr;
148 		break;
149 	case F_DIR:
150 		if (!S_ISDIR(p->fts_statp->st_mode))
151 			goto typeerr;
152 		break;
153 	case F_FIFO:
154 		if (!S_ISFIFO(p->fts_statp->st_mode))
155 			goto typeerr;
156 		break;
157 	case F_FILE:
158 		if (!S_ISREG(p->fts_statp->st_mode))
159 			goto typeerr;
160 		break;
161 	case F_LINK:
162 		if (!S_ISLNK(p->fts_statp->st_mode))
163 			goto typeerr;
164 		break;
165 #ifdef S_ISSOCK
166 	case F_SOCK:
167 		if (!S_ISSOCK(p->fts_statp->st_mode))
168 			goto typeerr;
169 		break;
170 #endif
171 typeerr:		LABEL;
172 		printf("\ttype (%s, %s)\n",
173 		    nodetype(s->type), inotype(p->fts_statp->st_mode));
174 		return (label);
175 	}
176 	if (mtree_Wflag)
177 		goto afterpermwhack;
178 #if HAVE_STRUCT_STAT_ST_FLAGS
179 	if (iflag && !uflag) {
180 		if (s->flags & F_FLAGS)
181 		    SETFLAGS(p->fts_statp->st_flags, SP_FLGS);
182 		return (label);
183         }
184 	if (mflag && !uflag) {
185 		if (s->flags & F_FLAGS)
186 		    CLEARFLAGS(p->fts_statp->st_flags, SP_FLGS);
187 		return (label);
188         }
189 #endif
190 	if (s->flags & F_DEV &&
191 	    (s->type == F_BLOCK || s->type == F_CHAR) &&
192 	    s->st_rdev != p->fts_statp->st_rdev) {
193 		LABEL;
194 		printf("%sdevice (%#x, %#x",
195 		    tab, s->st_rdev, p->fts_statp->st_rdev);
196 		if (uflag) {
197 			if ((unlink(p->fts_accpath) == -1) ||
198 			    (mknod(p->fts_accpath,
199 			      s->st_mode | nodetoino(s->type),
200 			      s->st_rdev) == -1) ||
201 			    (lchown(p->fts_accpath, p->fts_statp->st_uid,
202 			      p->fts_statp->st_gid) == -1) )
203 				printf(", not modified: %s)\n",
204 				    strerror(errno));
205 			 else
206 				printf(", modified)\n");
207 		} else
208 			printf(")\n");
209 		tab = "\t";
210 	}
211 	/* Set the uid/gid first, then set the mode. */
212 	if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
213 		LABEL;
214 		printf("%suser (%lu, %lu",
215 		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
216 		if (uflag) {
217 			if (lchown(p->fts_accpath, s->st_uid, -1))
218 				printf(", not modified: %s)\n",
219 				    strerror(errno));
220 			else
221 				printf(", modified)\n");
222 		} else
223 			printf(")\n");
224 		tab = "\t";
225 	}
226 	if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
227 		LABEL;
228 		printf("%sgid (%lu, %lu",
229 		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
230 		if (uflag) {
231 			if (lchown(p->fts_accpath, -1, s->st_gid))
232 				printf(", not modified: %s)\n",
233 				    strerror(errno));
234 			else
235 				printf(", modified)\n");
236 		}
237 		else
238 			printf(")\n");
239 		tab = "\t";
240 	}
241 	if (s->flags & F_MODE &&
242 	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
243 		if (lflag) {
244 			mode_t tmode, mode;
245 
246 			tmode = s->st_mode;
247 			mode = p->fts_statp->st_mode & MBITS;
248 			/*
249 			 * if none of the suid/sgid/etc bits are set,
250 			 * then if the mode is a subset of the target,
251 			 * skip.
252 			 */
253 			if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
254 			    (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
255 				if ((mode | tmode) == tmode)
256 					goto skip;
257 		}
258 
259 		LABEL;
260 		printf("%spermissions (%#lo, %#lo",
261 		    tab, (u_long)s->st_mode,
262 		    (u_long)p->fts_statp->st_mode & MBITS);
263 		if (uflag) {
264 			if (lchmod(p->fts_accpath, s->st_mode))
265 				printf(", not modified: %s)\n",
266 				    strerror(errno));
267 			else
268 				printf(", modified)\n");
269 		}
270 		else
271 			printf(")\n");
272 		tab = "\t";
273 	skip:	;
274 	}
275 	if (s->flags & F_NLINK && s->type != F_DIR &&
276 	    s->st_nlink != p->fts_statp->st_nlink) {
277 		LABEL;
278 		printf("%slink count (%lu, %lu)\n",
279 		    tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
280 		tab = "\t";
281 	}
282 	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
283 		LABEL;
284 		printf("%ssize (%lld, %lld)\n",
285 		    tab, (long long)s->st_size,
286 		    (long long)p->fts_statp->st_size);
287 		tab = "\t";
288 	}
289 	/*
290 	 * XXX
291 	 * Since utimes(2) only takes a timeval, there's no point in
292 	 * comparing the low bits of the timespec nanosecond field.  This
293 	 * will only result in mismatches that we can never fix.
294 	 *
295 	 * Doesn't display microsecond differences.
296 	 */
297 	if (s->flags & F_TIME) {
298 		struct timeval tv[2];
299 		struct stat *ps = p->fts_statp;
300 		time_t smtime = s->st_mtimespec.tv_sec;
301 
302 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
303 		time_t pmtime = ps->st_mtimespec.tv_sec;
304 
305 		TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
306 		TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
307 #else
308 		time_t pmtime = (time_t)ps->st_mtime;
309 
310 		tv[0].tv_sec = smtime;
311 		tv[0].tv_usec = 0;
312 		tv[1].tv_sec = pmtime;
313 		tv[1].tv_usec = 0;
314 #endif
315 
316 		if (tv[0].tv_sec != tv[1].tv_sec ||
317 		    tv[0].tv_usec != tv[1].tv_usec) {
318 			LABEL;
319 			printf("%smodification time (%.24s, ",
320 			    tab, ctime(&smtime));
321 			printf("%.24s", ctime(&pmtime));
322 			if (tflag) {
323 				tv[1] = tv[0];
324 				if (utimes(p->fts_accpath, tv))
325 					printf(", not modified: %s)\n",
326 					    strerror(errno));
327 				else
328 					printf(", modified)\n");
329 			} else
330 				printf(")\n");
331 			tab = "\t";
332 		}
333 	}
334 #if HAVE_STRUCT_STAT_ST_FLAGS
335 	/*
336 	 * XXX
337 	 * since lchflags(2) will reset file times, the utimes() above
338 	 * may have been useless!  oh well, we'd rather have correct
339 	 * flags, rather than times?
340 	 */
341         if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
342 	    || mflag || iflag)) {
343 		if (s->st_flags != p->fts_statp->st_flags) {
344 			char *f_s;
345 			LABEL;
346 			f_s = flags_to_string(s->st_flags, "none");
347 			printf("%sflags (\"%s\" is not ", tab, f_s);
348 			free(f_s);
349 			f_s = flags_to_string(p->fts_statp->st_flags, "none");
350 			printf("\"%s\"", f_s);
351 			free(f_s);
352 		}
353 		if (uflag) {
354 			if (iflag)
355 				SETFLAGS(0, CH_MASK);
356 			else if (mflag)
357 				CLEARFLAGS(0, SP_FLGS);
358 			else
359 				SETFLAGS(0, (~SP_FLGS & CH_MASK));
360 		} else
361 			printf(")\n");
362 		tab = "\t";
363 	}
364 #endif	/* HAVE_STRUCT_STAT_ST_FLAGS */
365 
366 	/*
367 	 * from this point, no more permission checking or whacking
368 	 * occurs, only checking of stuff like checksums and symlinks.
369 	 */
370  afterpermwhack:
371 	if (s->flags & F_CKSUM) {
372 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
373 			LABEL;
374 			printf("%scksum: %s: %s\n",
375 			    tab, p->fts_accpath, strerror(errno));
376 			tab = "\t";
377 		} else if (crc(fd, &val, &len)) {
378 			close(fd);
379 			LABEL;
380 			printf("%scksum: %s: %s\n",
381 			    tab, p->fts_accpath, strerror(errno));
382 			tab = "\t";
383 		} else {
384 			close(fd);
385 			if (s->cksum != val) {
386 				LABEL;
387 				printf("%scksum (%lu, %lu)\n",
388 				    tab, s->cksum, (unsigned long)val);
389 			}
390 			tab = "\t";
391 		}
392 	}
393 #ifndef NO_MD5
394 	if (s->flags & F_MD5) {
395 		if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL) {
396 			LABEL;
397 			printf("%smd5: %s: %s\n",
398 			    tab, p->fts_accpath, strerror(errno));
399 			tab = "\t";
400 		} else {
401 			if (strcmp(s->md5digest, digestbuf)) {
402 				LABEL;
403 				printf("%smd5 (0x%s, 0x%s)\n",
404 				    tab, s->md5digest, digestbuf);
405 			}
406 			tab = "\t";
407 			free(digestbuf);
408 		}
409 	}
410 #endif	/* ! NO_MD5 */
411 #ifndef NO_RMD160
412 	if (s->flags & F_RMD160) {
413 		if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL) {
414 			LABEL;
415 			printf("%srmd160: %s: %s\n",
416 			    tab, p->fts_accpath, strerror(errno));
417 			tab = "\t";
418 		} else {
419 			if (strcmp(s->rmd160digest, digestbuf)) {
420 				LABEL;
421 				printf("%srmd160 (0x%s, 0x%s)\n",
422 				    tab, s->rmd160digest, digestbuf);
423 			}
424 			tab = "\t";
425 			free(digestbuf);
426 		}
427 	}
428 #endif	/* ! NO_RMD160 */
429 #ifndef NO_SHA1
430 	if (s->flags & F_SHA1) {
431 		if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL) {
432 			LABEL;
433 			printf("%ssha1: %s: %s\n",
434 			    tab, p->fts_accpath, strerror(errno));
435 			tab = "\t";
436 		} else {
437 			if (strcmp(s->sha1digest, digestbuf)) {
438 				LABEL;
439 				printf("%ssha1 (0x%s, 0x%s)\n",
440 				    tab, s->sha1digest, digestbuf);
441 			}
442 			tab = "\t";
443 			free(digestbuf);
444 		}
445 	}
446 #endif	/* ! NO_SHA1 */
447 #ifndef NO_SHA2
448 	if (s->flags & F_SHA256) {
449 		if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL) {
450 			LABEL;
451 			printf("%ssha256: %s: %s\n",
452 			    tab, p->fts_accpath, strerror(errno));
453 			tab = "\t";
454 		} else {
455 			if (strcmp(s->sha256digest, digestbuf)) {
456 				LABEL;
457 				printf("%ssha256 (0x%s, 0x%s)\n",
458 				    tab, s->sha256digest, digestbuf);
459 			}
460 			tab = "\t";
461 			free(digestbuf);
462 		}
463 	}
464 	if (s->flags & F_SHA384) {
465 		if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL) {
466 			LABEL;
467 			printf("%ssha384: %s: %s\n",
468 			    tab, p->fts_accpath, strerror(errno));
469 			tab = "\t";
470 		} else {
471 			if (strcmp(s->sha384digest, digestbuf)) {
472 				LABEL;
473 				printf("%ssha384 (0x%s, 0x%s)\n",
474 				    tab, s->sha384digest, digestbuf);
475 			}
476 			tab = "\t";
477 			free(digestbuf);
478 		}
479 	}
480 	if (s->flags & F_SHA512) {
481 		if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL) {
482 			LABEL;
483 			printf("%ssha512: %s: %s\n",
484 			    tab, p->fts_accpath, strerror(errno));
485 			tab = "\t";
486 		} else {
487 			if (strcmp(s->sha512digest, digestbuf)) {
488 				LABEL;
489 				printf("%ssha512 (0x%s, 0x%s)\n",
490 				    tab, s->sha512digest, digestbuf);
491 			}
492 			tab = "\t";
493 			free(digestbuf);
494 		}
495 	}
496 #endif	/* ! NO_SHA2 */
497 	if (s->flags & F_SLINK &&
498 	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
499 		LABEL;
500 		printf("%slink ref (%s, %s", tab, cp, s->slink);
501 		if (uflag) {
502 			if ((unlink(p->fts_accpath) == -1) ||
503 			    (symlink(s->slink, p->fts_accpath) == -1) )
504 				printf(", not modified: %s)\n",
505 				    strerror(errno));
506 			else
507 				printf(", modified)\n");
508 		} else
509 			printf(")\n");
510 	}
511 	return (label);
512 }
513 
514 const char *
515 rlink(const char *name)
516 {
517 	static char lbuf[MAXPATHLEN];
518 	int len;
519 
520 	if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
521 		mtree_err("%s: %s", name, strerror(errno));
522 	lbuf[len] = '\0';
523 	return (lbuf);
524 }
525