Home | History | Annotate | Line # | Download | only in mtree
compare.c revision 1.35
      1 /*	$NetBSD: compare.c,v 1.35 2001/11/09 06:55:56 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.35 2001/11/09 06:55:56 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 <md5.h>
     51 #include <rmd160.h>
     52 #include <sha1.h>
     53 #include <stdio.h>
     54 #include <string.h>
     55 #include <time.h>
     56 #include <unistd.h>
     57 
     58 #include "mtree.h"
     59 #include "extern.h"
     60 
     61 #define	INDENTNAMELEN	8
     62 #define MARK								\
     63 do {									\
     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 } while (0)
     73 #define	LABEL if (!label++) MARK
     74 
     75 #define CHANGEFLAGS(path, oflags)					\
     76 	if (flags != (oflags)) {					\
     77 		if (!label) {						\
     78 			MARK;						\
     79 			printf("%sflags (\"%s\"", tab,			\
     80 			    flags_to_string(p->fts_statp->st_flags, "none")); \
     81 		}							\
     82 		if (chflags(path, flags)) {				\
     83 			label++;					\
     84 			printf(", not modified: %s)\n",			\
     85 			    strerror(errno));				\
     86 		} else							\
     87 			printf(", modified to \"%s\")\n",		\
     88 			     flags_to_string(flags, "none"));		\
     89 	}
     90 
     91 /* SETFLAGS:
     92  * given pflags, additionally set those flags specified in sflags and
     93  * selected by mask (the other flags are left unchanged). oflags is
     94  * passed as reference to check if chflags is necessary.
     95  */
     96 #define SETFLAGS(path, sflags, pflags, oflags, mask)			\
     97 do {									\
     98 	flags = ((sflags) & (mask)) | (pflags);				\
     99 	CHANGEFLAGS(path, oflags);					\
    100 } while (0)
    101 
    102 /* CLEARFLAGS:
    103  * given pflags, reset the flags specified in sflags and selected by mask
    104  * (the other flags are left unchanged). oflags is
    105  * passed as reference to check if chflags is necessary.
    106  */
    107 #define CLEARFLAGS(path, sflags, pflags, oflags, mask)			\
    108 do {									\
    109 	flags = (~((sflags) & (mask)) & CH_MASK) & (pflags);		\
    110 	CHANGEFLAGS(path, oflags);					\
    111 } while (0)
    112 
    113 int
    114 compare(NODE *s, FTSENT *p)
    115 {
    116 	u_int32_t len, val, flags;
    117 	int fd, label;
    118 	const char *cp, *tab;
    119 	char digestbuf[41];	/* large enough for {MD5,RMD160,SHA1}File() */
    120 
    121 	tab = NULL;
    122 	label = 0;
    123 	switch(s->type) {
    124 	case F_BLOCK:
    125 		if (!S_ISBLK(p->fts_statp->st_mode))
    126 			goto typeerr;
    127 		break;
    128 	case F_CHAR:
    129 		if (!S_ISCHR(p->fts_statp->st_mode))
    130 			goto typeerr;
    131 		break;
    132 	case F_DIR:
    133 		if (!S_ISDIR(p->fts_statp->st_mode))
    134 			goto typeerr;
    135 		break;
    136 	case F_FIFO:
    137 		if (!S_ISFIFO(p->fts_statp->st_mode))
    138 			goto typeerr;
    139 		break;
    140 	case F_FILE:
    141 		if (!S_ISREG(p->fts_statp->st_mode))
    142 			goto typeerr;
    143 		break;
    144 	case F_LINK:
    145 		if (!S_ISLNK(p->fts_statp->st_mode))
    146 			goto typeerr;
    147 		break;
    148 	case F_SOCK:
    149 		if (!S_ISSOCK(p->fts_statp->st_mode)) {
    150  typeerr:		LABEL;
    151 			printf("\ttype (%s, %s)\n",
    152 			    nodetype(s->type), inotype(p->fts_statp->st_mode));
    153 			return (label);
    154 		}
    155 		break;
    156 	}
    157 	if (Wflag)
    158 		goto afterpermwhack;
    159 	if (iflag && !uflag) {
    160 		if (s->flags & F_FLAGS)
    161 		    SETFLAGS(p->fts_accpath, s->st_flags,
    162 			p->fts_statp->st_flags, p->fts_statp->st_flags,
    163 			SP_FLGS);
    164 		return (label);
    165         }
    166 	if (mflag && !uflag) {
    167 		if (s->flags & F_FLAGS)
    168 		    CLEARFLAGS(p->fts_accpath, s->st_flags,
    169 			p->fts_statp->st_flags, p->fts_statp->st_flags,
    170 			SP_FLGS);
    171 		return (label);
    172         }
    173 	if (s->flags & F_DEV &&
    174 	    (s->type == F_BLOCK || s->type == F_CHAR) &&
    175 	    s->st_rdev != p->fts_statp->st_rdev) {
    176 		LABEL;
    177 		printf("%sdevice (%#x, %#x",
    178 		    tab, s->st_rdev, p->fts_statp->st_rdev);
    179 		if (uflag) {
    180 			if ((unlink(p->fts_accpath) == -1) ||
    181 			    (mknod(p->fts_accpath,
    182 			      s->st_mode | nodetoino(s->type),
    183 			      s->st_rdev) == -1) ||
    184 			    (chown(p->fts_accpath, p->fts_statp->st_uid,
    185 			      p->fts_statp->st_gid) == -1) )
    186 				printf(", not modified: %s)\n",
    187 				    strerror(errno));
    188 			 else
    189 				printf(", modified)\n");
    190 		} else
    191 			printf(")\n");
    192 		tab = "\t";
    193 	}
    194 	/* Set the uid/gid first, then set the mode. */
    195 	if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
    196 		LABEL;
    197 		printf("%suser (%lu, %lu",
    198 		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
    199 		if (uflag) {
    200 			if (chown(p->fts_accpath, s->st_uid, -1))
    201 				printf(", not modified: %s)\n",
    202 				    strerror(errno));
    203 			else
    204 				printf(", modified)\n");
    205 		} else
    206 			printf(")\n");
    207 		tab = "\t";
    208 	}
    209 	if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
    210 		LABEL;
    211 		printf("%sgid (%lu, %lu",
    212 		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
    213 		if (uflag) {
    214 			if (chown(p->fts_accpath, -1, s->st_gid))
    215 				printf(", not modified: %s)\n",
    216 				    strerror(errno));
    217 			else
    218 				printf(", modified)\n");
    219 		}
    220 		else
    221 			printf(")\n");
    222 		tab = "\t";
    223 	}
    224 	if (s->flags & F_MODE &&
    225 	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
    226 		if (lflag) {
    227 			mode_t tmode, mode;
    228 
    229 			tmode = s->st_mode;
    230 			mode = p->fts_statp->st_mode & MBITS;
    231 			/*
    232 			 * if none of the suid/sgid/etc bits are set,
    233 			 * then if the mode is a subset of the target,
    234 			 * skip.
    235 			 */
    236 			if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
    237 			    (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
    238 				if ((mode | tmode) == tmode)
    239 					goto skip;
    240 		}
    241 
    242 		LABEL;
    243 		printf("%spermissions (%#lo, %#lo",
    244 		    tab, (u_long)s->st_mode,
    245 		    (u_long)p->fts_statp->st_mode & MBITS);
    246 		if (uflag) {
    247 			if (chmod(p->fts_accpath, s->st_mode))
    248 				printf(", not modified: %s)\n",
    249 				    strerror(errno));
    250 			else
    251 				printf(", modified)\n");
    252 		}
    253 		else
    254 			printf(")\n");
    255 		tab = "\t";
    256 	skip:	;
    257 	}
    258 	if (s->flags & F_NLINK && s->type != F_DIR &&
    259 	    s->st_nlink != p->fts_statp->st_nlink) {
    260 		LABEL;
    261 		printf("%slink count (%lu, %lu)\n",
    262 		    tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
    263 		tab = "\t";
    264 	}
    265 	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
    266 		LABEL;
    267 		printf("%ssize (%lld, %lld)\n",
    268 		    tab, (long long)s->st_size,
    269 		    (long long)p->fts_statp->st_size);
    270 		tab = "\t";
    271 	}
    272 	/*
    273 	 * XXX
    274 	 * Since utimes(2) only takes a timeval, there's no point in
    275 	 * comparing the low bits of the timespec nanosecond field.  This
    276 	 * will only result in mismatches that we can never fix.
    277 	 *
    278 	 * Doesn't display microsecond differences.
    279 	 */
    280 	if (s->flags & F_TIME) {
    281 		struct timeval tv[2];
    282 		struct stat *ps = p->fts_statp;
    283 		time_t smtime = s->st_mtimespec.tv_sec;
    284 
    285 #ifdef BSD4_4
    286 		time_t pmtime = ps->st_mtimespec.tv_sec;
    287 
    288 		TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
    289 #else
    290 		time_t pmtime = (time_t)ps->st_mtime;
    291 
    292 		tv[1].tv_sec = ps->st_mtime;
    293 		tv[1].tv_usec = 0;
    294 #endif
    295 		TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
    296 
    297 		if (tv[0].tv_sec != tv[1].tv_sec ||
    298 		    tv[0].tv_usec != tv[1].tv_usec) {
    299 			LABEL;
    300 			printf("%smodification time (%.24s, ",
    301 			    tab, ctime(&smtime));
    302 			printf("%.24s", ctime(&pmtime));
    303 			if (tflag) {
    304 				tv[1] = tv[0];
    305 				if (utimes(p->fts_accpath, tv))
    306 					printf(", not modified: %s)\n",
    307 					    strerror(errno));
    308 				else
    309 					printf(", modified)\n");
    310 			} else
    311 				printf(")\n");
    312 			tab = "\t";
    313 		}
    314 	}
    315 	/*
    316 	 * XXX
    317 	 * since chflags(2) will reset file times, the utimes() above
    318 	 * may have been useless!  oh well, we'd rather have correct
    319 	 * flags, rather than times?
    320 	 */
    321         if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
    322 	    || mflag || iflag)) {
    323 		if (s->st_flags != p->fts_statp->st_flags) {
    324 			LABEL;
    325 			printf("%sflags (\"%s\" is not ", tab,
    326 			    flags_to_string(s->st_flags, "none"));
    327 			printf("\"%s\"",
    328 			    flags_to_string(p->fts_statp->st_flags, "none"));
    329 		}
    330 		if (uflag) {
    331 			if (iflag)
    332 				SETFLAGS(p->fts_accpath, s->st_flags,
    333 				    0, p->fts_statp->st_flags, CH_MASK);
    334 			else if (mflag)
    335 				CLEARFLAGS(p->fts_accpath, s->st_flags,
    336 				    0, p->fts_statp->st_flags, SP_FLGS);
    337 			else
    338 				SETFLAGS(p->fts_accpath, s->st_flags,
    339 			     	    0, p->fts_statp->st_flags,
    340 				    (~SP_FLGS & CH_MASK));
    341 		} else
    342 			printf(")\n");
    343 		tab = "\t";
    344 	}
    345 
    346 	/*
    347 	 * from this point, no more permission checking or whacking
    348 	 * occurs, only checking of stuff like checksums and symlinks.
    349 	 */
    350  afterpermwhack:
    351 	if (s->flags & F_CKSUM) {
    352 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
    353 			LABEL;
    354 			printf("%scksum: %s: %s\n",
    355 			    tab, p->fts_accpath, strerror(errno));
    356 			tab = "\t";
    357 		} else if (crc(fd, &val, &len)) {
    358 			close(fd);
    359 			LABEL;
    360 			printf("%scksum: %s: %s\n",
    361 			    tab, p->fts_accpath, strerror(errno));
    362 			tab = "\t";
    363 		} else {
    364 			close(fd);
    365 			if (s->cksum != val) {
    366 				LABEL;
    367 				printf("%scksum (%lu, %lu)\n",
    368 				    tab, s->cksum, (unsigned long)val);
    369 			}
    370 			tab = "\t";
    371 		}
    372 	}
    373 	if (s->flags & F_MD5) {
    374 		if (MD5File(p->fts_accpath, digestbuf) == NULL) {
    375 			LABEL;
    376 			printf("%smd5: %s: %s\n",
    377 			    tab, p->fts_accpath, strerror(errno));
    378 			tab = "\t";
    379 		} else {
    380 			if (strcmp(s->md5digest, digestbuf)) {
    381 				LABEL;
    382 				printf("%smd5 (0x%s, 0x%s)\n",
    383 				    tab, s->md5digest, digestbuf);
    384 			}
    385 			tab = "\t";
    386 		}
    387 	}
    388 	if (s->flags & F_RMD160) {
    389 		if (RMD160File(p->fts_accpath, digestbuf) == NULL) {
    390 			LABEL;
    391 			printf("%srmd160: %s: %s\n",
    392 			    tab, p->fts_accpath, strerror(errno));
    393 			tab = "\t";
    394 		} else {
    395 			if (strcmp(s->rmd160digest, digestbuf)) {
    396 				LABEL;
    397 				printf("%srmd160 (0x%s, 0x%s)\n",
    398 				    tab, s->rmd160digest, digestbuf);
    399 			}
    400 			tab = "\t";
    401 		}
    402 	}
    403 	if (s->flags & F_SHA1) {
    404 		if (SHA1File(p->fts_accpath, digestbuf) == NULL) {
    405 			LABEL;
    406 			printf("%ssha1: %s: %s\n",
    407 			    tab, p->fts_accpath, strerror(errno));
    408 			tab = "\t";
    409 		} else {
    410 			if (strcmp(s->sha1digest, digestbuf)) {
    411 				LABEL;
    412 				printf("%ssha1 (0x%s, 0x%s)\n",
    413 				    tab, s->sha1digest, digestbuf);
    414 			}
    415 			tab = "\t";
    416 		}
    417 	}
    418 	if (s->flags & F_SLINK &&
    419 	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
    420 		LABEL;
    421 		printf("%slink ref (%s, %s", tab, cp, s->slink);
    422 		if (uflag) {
    423 			if ((unlink(p->fts_accpath) == -1) ||
    424 			    (symlink(s->slink, p->fts_accpath) == -1) )
    425 				printf(", not modified: %s)\n",
    426 				    strerror(errno));
    427 			else
    428 				printf(", modified)\n");
    429 		} else
    430 			printf(")\n");
    431 	}
    432 	return (label);
    433 }
    434 
    435 const char *
    436 rlink(const char *name)
    437 {
    438 	static char lbuf[MAXPATHLEN];
    439 	int len;
    440 
    441 	if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
    442 		mtree_err("%s: %s", name, strerror(errno));
    443 	lbuf[len] = '\0';
    444 	return (lbuf);
    445 }
    446