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