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