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