xref: /dragonfly/usr.sbin/mtree/compare.c (revision 3074866b)
1 /*	@(#)compare.c	8.1 (Berkeley) 6/6/93	*/
2 /*	$NetBSD: compare.c,v 1.58 2013/11/21 18:39:50 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <unistd.h>
44 
45 #ifndef NO_MD5
46 #include <openssl/md5.h>
47 #endif
48 #ifndef NO_RMD160
49 #include <openssl/ripemd.h>
50 #endif
51 #ifndef NO_SHA
52 #include <openssl/sha.h>
53 #endif
54 
55 #include "extern.h"
56 
57 #define	INDENTNAMELEN	8
58 #define MARK								\
59 do {									\
60 	if (flavor == F_FREEBSD9) {					\
61 		len = printf("%s changed\n", RP(p));			\
62 		tab = "\t";						\
63 	} else {							\
64 		len = printf("%s: ", RP(p));				\
65 		if (len > INDENTNAMELEN) {				\
66 			tab = "\t";					\
67 			printf("\n");					\
68 		} else {						\
69 			tab = "";					\
70 			printf("%*s", INDENTNAMELEN - (int)len, "");	\
71 		}							\
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 		char *sf;						\
82 		if (!label) {						\
83 			MARK;						\
84 			sf = flags_to_string(p->fts_statp->st_flags, "none"); \
85 			printf("%sflags (\"%s\"", tab, sf);		\
86 			free(sf);					\
87 		}							\
88 		if (lchflags(p->fts_accpath, flags)) {			\
89 			label++;					\
90 			printf(", not modified: %s)\n",			\
91 			    strerror(errno));				\
92 		} else {						\
93 			sf = flags_to_string(flags, "none");		\
94 			printf(", modified to \"%s\")\n", sf);		\
95 			free(sf);					\
96 		}							\
97 	}
98 
99 /* SETFLAGS:
100  * given pflags, additionally set those flags specified in s->st_flags and
101  * selected by mask (the other flags are left unchanged).
102  */
103 #define SETFLAGS(pflags, mask)						\
104 do {									\
105 	flags = (s->st_flags & (mask)) | (pflags);			\
106 	CHANGEFLAGS;							\
107 } while (0)
108 
109 /* CLEARFLAGS:
110  * given pflags, reset the flags specified in s->st_flags and selected by mask
111  * (the other flags are left unchanged).
112  */
113 #define CLEARFLAGS(pflags, mask)					\
114 do {									\
115 	flags = (~(s->st_flags & (mask)) & CH_MASK) & (pflags);		\
116 	CHANGEFLAGS;							\
117 } while (0)
118 #endif	/* HAVE_STRUCT_STAT_ST_FLAGS */
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_SHA)
127 	char *digestbuf;
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 #ifdef S_ISSOCK
158 	case F_SOCK:
159 		if (!S_ISSOCK(p->fts_statp->st_mode))
160 			goto typeerr;
161 		break;
162 #endif
163 typeerr:		LABEL;
164 		printf(flavor == F_FREEBSD9 ?
165 		    "\ttype expected %s found %s\n" : "\ttype (%s, %s)\n",
166 		    nodetype(s->type), inotype(p->fts_statp->st_mode));
167 		return (label);
168 	}
169 	if (mtree_Wflag)
170 		goto afterpermwhack;
171 #if HAVE_STRUCT_STAT_ST_FLAGS
172 	if (iflag && !uflag) {
173 		if (s->flags & F_FLAGS)
174 		    SETFLAGS(p->fts_statp->st_flags, SP_FLGS);
175 		return (label);
176         }
177 	if (mflag && !uflag) {
178 		if (s->flags & F_FLAGS)
179 		    CLEARFLAGS(p->fts_statp->st_flags, SP_FLGS);
180 		return (label);
181         }
182 #endif
183 	if (s->flags & F_DEV &&
184 	    (s->type == F_BLOCK || s->type == F_CHAR) &&
185 	    s->st_rdev != p->fts_statp->st_rdev) {
186 		LABEL;
187 		printf(flavor == F_FREEBSD9 ?
188 		    "%sdevice expected %#jx found %#jx" :
189 		    "%sdevice (%#jx, %#jx",
190 		    tab, (uintmax_t)s->st_rdev,
191 		    (uintmax_t)p->fts_statp->st_rdev);
192 		if (uflag) {
193 			if ((unlink(p->fts_accpath) == -1) ||
194 			    (mknod(p->fts_accpath,
195 			      s->st_mode | nodetoino(s->type),
196 			      s->st_rdev) == -1) ||
197 			    (lchown(p->fts_accpath, p->fts_statp->st_uid,
198 			      p->fts_statp->st_gid) == -1) )
199 				printf(", not modified: %s%s\n",
200 				    strerror(errno),
201 				    flavor == F_FREEBSD9 ? "" : ")");
202 			 else
203 				printf(", modified%s\n",
204 				    flavor == F_FREEBSD9 ? "" : ")");
205 		} else
206 			printf(")\n");
207 		tab = "\t";
208 	}
209 	/* Set the uid/gid first, then set the mode. */
210 	if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
211 		LABEL;
212 		printf(flavor == F_FREEBSD9 ?
213 		    "%suser expected %lu found %lu" : "%suser (%lu, %lu",
214 		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
215 		if (uflag) {
216 			if (lchown(p->fts_accpath, s->st_uid, -1))
217 				printf(", not modified: %s%s\n",
218 				    strerror(errno),
219 				    flavor == F_FREEBSD9 ? "" : ")");
220 			else
221 				printf(", modified%s\n",
222 				    flavor == F_FREEBSD9 ? "" : ")");
223 		} else
224 			printf(")\n");
225 		tab = "\t";
226 	}
227 	if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
228 		LABEL;
229 		printf(flavor == F_FREEBSD9 ?
230 		    "%sgid expected %lu found %lu" : "%sgid (%lu, %lu",
231 		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
232 		if (uflag) {
233 			if (lchown(p->fts_accpath, -1, s->st_gid))
234 				printf(", not modified: %s%s\n",
235 				    strerror(errno),
236 				    flavor == F_FREEBSD9 ? "" : ")");
237 			else
238 				printf(", modified%s\n",
239 				    flavor == F_FREEBSD9 ? "" : ")");
240 		}
241 		else
242 			printf(")\n");
243 		tab = "\t";
244 	}
245 	if (s->flags & F_MODE &&
246 	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
247 		if (lflag) {
248 			mode_t tmode, mode;
249 
250 			tmode = s->st_mode;
251 			mode = p->fts_statp->st_mode & MBITS;
252 			/*
253 			 * if none of the suid/sgid/etc bits are set,
254 			 * then if the mode is a subset of the target,
255 			 * skip.
256 			 */
257 			if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
258 			    (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
259 				if ((mode | tmode) == tmode)
260 					goto skip;
261 		}
262 
263 		LABEL;
264 		printf(flavor == F_FREEBSD9 ?
265 		    "%spermissions expcted %#lo found %#lo" :
266 		    "%spermissions (%#lo, %#lo",
267 		    tab, (u_long)s->st_mode,
268 		    (u_long)p->fts_statp->st_mode & MBITS);
269 		if (uflag) {
270 			if (lchmod(p->fts_accpath, s->st_mode))
271 				printf(", not modified: %s%s\n",
272 				    strerror(errno),
273 				    flavor == F_FREEBSD9 ? "" : ")");
274 			else
275 				printf(", modified%s\n",
276 				    flavor == F_FREEBSD9 ? "" : ")");
277 		}
278 		else
279 			printf(")\n");
280 		tab = "\t";
281 	skip:	;
282 	}
283 	if (s->flags & F_NLINK && s->type != F_DIR &&
284 	    s->st_nlink != p->fts_statp->st_nlink) {
285 		LABEL;
286 		printf(flavor == F_FREEBSD9 ?
287 		    "%slink count expected %lu found %lu\n" :
288 		    "%slink count (%lu, %lu)\n",
289 		    tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
290 		tab = "\t";
291 	}
292 	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
293 		LABEL;
294 		printf(flavor == F_FREEBSD9 ?
295 		    "%ssize expected %ju found %ju\n" : "%ssize (%ju, %ju)\n",
296 		    tab, (uintmax_t)s->st_size,
297 		    (uintmax_t)p->fts_statp->st_size);
298 		tab = "\t";
299 	}
300 	/*
301 	 * XXX
302 	 * Since utimes(2) only takes a timeval, there's no point in
303 	 * comparing the low bits of the timespec nanosecond field.  This
304 	 * will only result in mismatches that we can never fix.
305 	 *
306 	 * Doesn't display microsecond differences.
307 	 */
308 	if (s->flags & F_TIME) {
309 		struct timeval tv[2];
310 		struct stat *ps = p->fts_statp;
311 		time_t smtime = s->st_mtimespec.tv_sec;
312 
313 #if defined(BSD4_4)
314 		time_t pmtime = ps->st_mtimespec.tv_sec;
315 
316 		TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
317 		TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
318 #else
319 		time_t pmtime = (time_t)ps->st_mtime;
320 
321 		tv[0].tv_sec = smtime;
322 		tv[0].tv_usec = 0;
323 		tv[1].tv_sec = pmtime;
324 		tv[1].tv_usec = 0;
325 #endif
326 
327 		if (tv[0].tv_sec != tv[1].tv_sec ||
328 		    tv[0].tv_usec != tv[1].tv_usec) {
329 			LABEL;
330 			printf(flavor == F_FREEBSD9 ?
331 			    "%smodification time expected %.24s found " :
332 			    "%smodification time (%.24s, ",
333 			    tab, ctime(&smtime));
334 			printf("%.24s", ctime(&pmtime));
335 			if (tflag) {
336 				tv[1] = tv[0];
337 				if (utimes(p->fts_accpath, tv))
338 					printf(", not modified: %s%s\n",
339 					    strerror(errno),
340 					    flavor == F_FREEBSD9 ? "" : ")");
341 				else
342 					printf(", modified%s\n",
343 					    flavor == F_FREEBSD9 ? "" : ")");
344 			} else
345 				printf("%s\n", flavor == F_FREEBSD9 ? "" : ")");
346 			tab = "\t";
347 		}
348 	}
349 #if HAVE_STRUCT_STAT_ST_FLAGS
350 	/*
351 	 * XXX
352 	 * since lchflags(2) will reset file times, the utimes() above
353 	 * may have been useless!  oh well, we'd rather have correct
354 	 * flags, rather than times?
355 	 */
356         if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
357 	    || mflag || iflag)) {
358 		if (s->st_flags != p->fts_statp->st_flags) {
359 			char *f_s;
360 			LABEL;
361 			f_s = flags_to_string(s->st_flags, "none");
362 			printf(flavor == F_FREEBSD9 ?
363 			    "%sflags expected \"%s\" found " :
364 			    "%sflags (\"%s\" is not ", tab, f_s);
365 			free(f_s);
366 			f_s = flags_to_string(p->fts_statp->st_flags, "none");
367 			printf("\"%s\"", f_s);
368 			free(f_s);
369 		}
370 		if (uflag) {
371 			if (iflag)
372 				SETFLAGS(0, CH_MASK);
373 			else if (mflag)
374 				CLEARFLAGS(0, SP_FLGS);
375 			else
376 				SETFLAGS(0, (~SP_FLGS & CH_MASK));
377 		} else
378 			printf("%s\n", flavor == F_FREEBSD9 ? "" : ")");
379 		tab = "\t";
380 	}
381 #endif	/* HAVE_STRUCT_STAT_ST_FLAGS */
382 
383 	/*
384 	 * from this point, no more permission checking or whacking
385 	 * occurs, only checking of stuff like checksums and symlinks.
386 	 */
387  afterpermwhack:
388 	if (s->flags & F_CKSUM) {
389 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
390 			LABEL;
391 			printf("%scksum: %s: %s\n",
392 			    tab, p->fts_accpath, strerror(errno));
393 			tab = "\t";
394 		} else if (crc(fd, &val, &len)) {
395 			close(fd);
396 			LABEL;
397 			printf("%scksum: %s: %s\n",
398 			    tab, p->fts_accpath, strerror(errno));
399 			tab = "\t";
400 		} else {
401 			close(fd);
402 			if (s->cksum != val) {
403 				LABEL;
404 				printf(flavor == F_FREEBSD9 ?
405 				    "%scksum expected %lu found %lu\n" :
406 				    "%scksum (%lu, %lu)\n",
407 				    tab, s->cksum, (unsigned long)val);
408 			}
409 			tab = "\t";
410 		}
411 	}
412 #ifndef NO_MD5
413 	if (s->flags & F_MD5) {
414 		if ((digestbuf = dohash(F_MD5, p->fts_accpath)) == NULL) {
415 			LABEL;
416 			printf("%s%s: %s: %s\n",
417 			    tab, MD5KEY, p->fts_accpath, strerror(errno));
418 			tab = "\t";
419 		} else {
420 			if (strcmp(s->md5digest, digestbuf)) {
421 				LABEL;
422 				printf(flavor == F_FREEBSD9 ?
423 				    "%s%s expected %s found %s\n" :
424 				    "%s%s (0x%s, 0x%s)\n",
425 				    tab, MD5KEY, s->md5digest, digestbuf);
426 			}
427 			tab = "\t";
428 			free(digestbuf);
429 		}
430 	}
431 #endif	/* ! NO_MD5 */
432 #ifndef NO_RMD160
433 	if (s->flags & F_RMD160) {
434 		if ((digestbuf = dohash(F_RMD160, p->fts_accpath)) == NULL) {
435 			LABEL;
436 			printf("%s%s: %s: %s\n",
437 			    tab, RMD160KEY, p->fts_accpath, strerror(errno));
438 			tab = "\t";
439 		} else {
440 			if (strcmp(s->rmd160digest, digestbuf)) {
441 				LABEL;
442 				printf(flavor == F_FREEBSD9 ?
443 				    "%s%s expected %s found %s\n" :
444 				    "%s%s (0x%s, 0x%s)\n",
445 				    tab, RMD160KEY, s->rmd160digest, digestbuf);
446 			}
447 			tab = "\t";
448 			free(digestbuf);
449 		}
450 	}
451 #endif	/* ! NO_RMD160 */
452 #ifndef NO_SHA
453 	if (s->flags & F_SHA1) {
454 		if ((digestbuf = dohash(F_SHA1, p->fts_accpath)) == NULL) {
455 			LABEL;
456 			printf("%s%s: %s: %s\n",
457 			    tab, SHA1KEY, p->fts_accpath, strerror(errno));
458 			tab = "\t";
459 		} else {
460 			if (strcmp(s->sha1digest, digestbuf)) {
461 				LABEL;
462 				printf(flavor == F_FREEBSD9 ?
463 				    "%s%s expected %s found %s\n" :
464 				    "%s%s (0x%s, 0x%s)\n",
465 				    tab, SHA1KEY, s->sha1digest, digestbuf);
466 			}
467 			tab = "\t";
468 			free(digestbuf);
469 		}
470 	}
471 	if (s->flags & F_SHA256) {
472 		if ((digestbuf = dohash(F_SHA256, p->fts_accpath)) == NULL) {
473 			LABEL;
474 			printf("%s%s: %s: %s\n",
475 			    tab, SHA256KEY, p->fts_accpath, strerror(errno));
476 			tab = "\t";
477 		} else {
478 			if (strcmp(s->sha256digest, digestbuf)) {
479 				LABEL;
480 				printf(flavor == F_FREEBSD9 ?
481 				    "%s%s expected %s found %s\n" :
482 				    "%s%s (0x%s, 0x%s)\n",
483 				    tab, SHA256KEY, s->sha256digest, digestbuf);
484 			}
485 			tab = "\t";
486 			free(digestbuf);
487 		}
488 	}
489 #ifdef SHA384_DIGEST_LENGTH
490 	if (s->flags & F_SHA384) {
491 		if ((digestbuf = dohash(F_SHA384, p->fts_accpath)) == NULL) {
492 			LABEL;
493 			printf("%s%s: %s: %s\n",
494 			    tab, SHA384KEY, p->fts_accpath, strerror(errno));
495 			tab = "\t";
496 		} else {
497 			if (strcmp(s->sha384digest, digestbuf)) {
498 				LABEL;
499 				printf(flavor == F_FREEBSD9 ?
500 				    "%s%s expected %s found %s\n" :
501 				    "%s%s (0x%s, 0x%s)\n",
502 				    tab, SHA384KEY, s->sha384digest, digestbuf);
503 			}
504 			tab = "\t";
505 			free(digestbuf);
506 		}
507 	}
508 #endif
509 	if (s->flags & F_SHA512) {
510 		if ((digestbuf = dohash(F_SHA512, p->fts_accpath)) == NULL) {
511 			LABEL;
512 			printf("%s%s: %s: %s\n",
513 			    tab, SHA512KEY, p->fts_accpath, strerror(errno));
514 			tab = "\t";
515 		} else {
516 			if (strcmp(s->sha512digest, digestbuf)) {
517 				LABEL;
518 				printf(flavor == F_FREEBSD9 ?
519 				    "%s%s expected %s found %s\n" :
520 				    "%s%s (0x%s, 0x%s)\n",
521 				    tab, SHA512KEY, s->sha512digest, digestbuf);
522 			}
523 			tab = "\t";
524 			free(digestbuf);
525 		}
526 	}
527 #endif	/* ! NO_SHA */
528 	if (s->flags & F_SLINK &&
529 	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
530 		LABEL;
531 		printf(flavor == F_FREEBSD9 ?
532 		    "%slink ref expected %s found %s" :
533 		    "%slink ref (%s, %s", tab, cp, s->slink);
534 		if (uflag) {
535 			if ((unlink(p->fts_accpath) == -1) ||
536 			    (symlink(s->slink, p->fts_accpath) == -1) )
537 				printf(", not modified: %s%s\n",
538 				    strerror(errno),
539 				    flavor == F_FREEBSD9 ? "" : ")");
540 			else
541 				printf(", modified%s\n",
542 				    flavor == F_FREEBSD9 ? "" : ")");
543 		} else
544 			printf("%s\n", flavor == F_FREEBSD9 ? "" : ")");
545 	}
546 	return (label);
547 }
548 
549 const char *
550 rlink(const char *name)
551 {
552 	static char lbuf[MAXPATHLEN];
553 	int len;
554 
555 	if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
556 		mtree_err("%s: %s", name, strerror(errno));
557 	lbuf[len] = '\0';
558 	return (lbuf);
559 }
560