1 /* $NetBSD: compare.c,v 1.63 2025/12/14 17:30:47 christos 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.63 2025/12/14 17:30:47 christos 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 \
74 do { \
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) \
119 do { \
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) \
129 do { \
130 flags = (~(s->st_flags & (mask)) & CH_MASK) & (pflags); \
131 CHANGEFLAGS; \
132 } while (0)
133 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
134
135 int
compare(NODE * s,FTSENT * p)136 compare(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
180 typeerr: 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, (gid_t)-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, (uid_t)-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 expected %#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", strerror(errno));
295 else
296 printf(", modified%s",
297 was_unlinked ? " by unlink" : "");
298 }
299 printf("%s\n", flavor == F_FREEBSD9 ? "" : ")");
300 tab = "\t";
301 skip: ;
302 }
303 if (s->flags & F_NLINK && s->type != F_DIR &&
304 s->st_nlink != p->fts_statp->st_nlink) {
305 LABEL;
306 printf(flavor == F_FREEBSD9 ?
307 "%slink count expected %lu found %lu\n" :
308 "%slink count (%lu, %lu)\n",
309 tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
310 tab = "\t";
311 }
312 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
313 LABEL;
314 printf(flavor == F_FREEBSD9 ?
315 "%ssize expected %ju found %ju\n" : "%ssize (%ju, %ju)\n",
316 tab, (uintmax_t)s->st_size,
317 (uintmax_t)p->fts_statp->st_size);
318 tab = "\t";
319 }
320 /*
321 * XXX
322 * Since utimes(2) only takes a timeval, there's no point in
323 * comparing the low bits of the timespec nanosecond field. This
324 * will only result in mismatches that we can never fix.
325 *
326 * Doesn't display microsecond differences.
327 */
328 if (s->flags & F_TIME) {
329 struct timeval tv[2];
330 struct stat *ps = p->fts_statp;
331 time_t smtime = s->st_mtimespec.tv_sec;
332
333 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
334 time_t pmtime = ps->st_mtimespec.tv_sec;
335
336 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
337 TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
338 #else
339 time_t pmtime = (time_t)ps->st_mtime;
340
341 tv[0].tv_sec = smtime;
342 tv[0].tv_usec = 0;
343 tv[1].tv_sec = pmtime;
344 tv[1].tv_usec = 0;
345 #endif
346
347 if (tv[0].tv_sec != tv[1].tv_sec ||
348 tv[0].tv_usec != tv[1].tv_usec) {
349 LABEL;
350 printf(flavor == F_FREEBSD9 ?
351 "%smodification time expected %.24s found " :
352 "%smodification time (%.24s, ",
353 tab, ctime(&smtime));
354 printf("%.24s", ctime(&pmtime));
355 if (tflag) {
356 tv[1] = tv[0];
357 if (utimes(p->fts_accpath, tv))
358 printf(", not modified: %s%s\n",
359 strerror(errno),
360 flavor == F_FREEBSD9 ? "" : ")");
361 else
362 printf(", modified%s\n",
363 flavor == F_FREEBSD9 ? "" : ")");
364 } else
365 printf("%s\n", flavor == F_FREEBSD9 ? "" : ")");
366 tab = "\t";
367 }
368 }
369 #if HAVE_STRUCT_STAT_ST_FLAGS
370 /*
371 * XXX
372 * since lchflags(2) will reset file times, the utimes() above
373 * may have been useless! oh well, we'd rather have correct
374 * flags, rather than times?
375 */
376 if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
377 || mflag || iflag)) {
378 if (s->st_flags != p->fts_statp->st_flags) {
379 char *f_s;
380 LABEL;
381 f_s = flags_to_string(s->st_flags, "none");
382 printf(flavor == F_FREEBSD9 ?
383 "%sflags expected \"%s\" found " :
384 "%sflags (\"%s\" is not ", tab, f_s);
385 free(f_s);
386 f_s = flags_to_string(p->fts_statp->st_flags, "none");
387 printf("\"%s\"", f_s);
388 free(f_s);
389 }
390 if (uflag) {
391 if (iflag)
392 SETFLAGS(0, CH_MASK);
393 else if (mflag)
394 CLEARFLAGS(0, SP_FLGS);
395 else
396 SETFLAGS(0, (~SP_FLGS & CH_MASK));
397 } else
398 printf("%s\n", flavor == F_FREEBSD9 ? "" : ")");
399 tab = "\t";
400 }
401 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
402
403 /*
404 * from this point, no more permission checking or whacking
405 * occurs, only checking of stuff like checksums and symlinks.
406 */
407 afterpermwhack:
408 if (s->flags & F_CKSUM) {
409 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
410 LABEL;
411 printf("%scksum: %s: %s\n",
412 tab, p->fts_accpath, strerror(errno));
413 tab = "\t";
414 } else if (crc(fd, &val, &len)) {
415 close(fd);
416 LABEL;
417 printf("%scksum: %s: %s\n",
418 tab, p->fts_accpath, strerror(errno));
419 tab = "\t";
420 } else {
421 close(fd);
422 if (s->cksum != val) {
423 LABEL;
424 printf(flavor == F_FREEBSD9 ?
425 "%scksum expected %lu found %lu\n" :
426 "%scksum (%lu, %lu)\n",
427 tab, s->cksum, (unsigned long)val);
428 }
429 tab = "\t";
430 }
431 }
432 #ifndef NO_MD5
433 if (s->flags & F_MD5) {
434 if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL) {
435 LABEL;
436 printf("%s%s: %s: %s\n",
437 tab, MD5KEY, p->fts_accpath, strerror(errno));
438 tab = "\t";
439 } else {
440 if (strcmp(s->md5digest, 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, MD5KEY, s->md5digest, digestbuf);
446 }
447 tab = "\t";
448 free(digestbuf);
449 }
450 }
451 #endif /* ! NO_MD5 */
452 #ifndef NO_RMD160
453 if (s->flags & F_RMD160) {
454 if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL) {
455 LABEL;
456 printf("%s%s: %s: %s\n",
457 tab, RMD160KEY, p->fts_accpath, strerror(errno));
458 tab = "\t";
459 } else {
460 if (strcmp(s->rmd160digest, 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, RMD160KEY, s->rmd160digest, digestbuf);
466 }
467 tab = "\t";
468 free(digestbuf);
469 }
470 }
471 #endif /* ! NO_RMD160 */
472 #ifndef NO_SHA1
473 if (s->flags & F_SHA1) {
474 if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL) {
475 LABEL;
476 printf("%s%s: %s: %s\n",
477 tab, SHA1KEY, p->fts_accpath, strerror(errno));
478 tab = "\t";
479 } else {
480 if (strcmp(s->sha1digest, digestbuf)) {
481 LABEL;
482 printf(flavor == F_FREEBSD9 ?
483 "%s%s expected %s found %s\n" :
484 "%s%s (0x%s, 0x%s)\n",
485 tab, SHA1KEY, s->sha1digest, digestbuf);
486 }
487 tab = "\t";
488 free(digestbuf);
489 }
490 }
491 #endif /* ! NO_SHA1 */
492 #ifndef NO_SHA2
493 if (s->flags & F_SHA256) {
494 if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL) {
495 LABEL;
496 printf("%s%s: %s: %s\n",
497 tab, SHA256KEY, p->fts_accpath, strerror(errno));
498 tab = "\t";
499 } else {
500 if (strcmp(s->sha256digest, digestbuf)) {
501 LABEL;
502 printf(flavor == F_FREEBSD9 ?
503 "%s%s expected %s found %s\n" :
504 "%s%s (0x%s, 0x%s)\n",
505 tab, SHA256KEY, s->sha256digest, digestbuf);
506 }
507 tab = "\t";
508 free(digestbuf);
509 }
510 }
511 #ifdef SHA384_BLOCK_LENGTH
512 if (s->flags & F_SHA384) {
513 if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL) {
514 LABEL;
515 printf("%s%s: %s: %s\n",
516 tab, SHA384KEY, p->fts_accpath, strerror(errno));
517 tab = "\t";
518 } else {
519 if (strcmp(s->sha384digest, digestbuf)) {
520 LABEL;
521 printf(flavor == F_FREEBSD9 ?
522 "%s%s expected %s found %s\n" :
523 "%s%s (0x%s, 0x%s)\n",
524 tab, SHA384KEY, s->sha384digest, digestbuf);
525 }
526 tab = "\t";
527 free(digestbuf);
528 }
529 }
530 #endif
531 if (s->flags & F_SHA512) {
532 if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL) {
533 LABEL;
534 printf("%s%s: %s: %s\n",
535 tab, SHA512KEY, p->fts_accpath, strerror(errno));
536 tab = "\t";
537 } else {
538 if (strcmp(s->sha512digest, digestbuf)) {
539 LABEL;
540 printf(flavor == F_FREEBSD9 ?
541 "%s%s expected %s found %s\n" :
542 "%s%s (0x%s, 0x%s)\n",
543 tab, SHA512KEY, s->sha512digest, digestbuf);
544 }
545 tab = "\t";
546 free(digestbuf);
547 }
548 }
549 #endif /* ! NO_SHA2 */
550 if (s->flags & F_SLINK &&
551 strcmp(cp = rlink(p->fts_accpath), s->slink)) {
552 LABEL;
553 printf(flavor == F_FREEBSD9 ?
554 "%slink ref expected %s found %s" :
555 "%slink ref (%s, %s", tab, cp, s->slink);
556 if (uflag) {
557 if ((unlink(p->fts_accpath) == -1) ||
558 (symlink(s->slink, p->fts_accpath) == -1) )
559 printf(", not modified: %s%s\n",
560 strerror(errno),
561 flavor == F_FREEBSD9 ? "" : ")");
562 else
563 printf(", modified%s\n",
564 flavor == F_FREEBSD9 ? "" : ")");
565 } else
566 printf("%s\n", flavor == F_FREEBSD9 ? "" : ")");
567 }
568 return (label);
569 }
570
571 const char *
rlink(const char * name)572 rlink(const char *name)
573 {
574 static char lbuf[MAXPATHLEN];
575 ssize_t len;
576
577 if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
578 mtree_err("%s: %s", name, strerror(errno));
579 lbuf[len] = '\0';
580 return (lbuf);
581 }
582