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