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