verify.c revision 122135
11553Srgrimes/*- 21553Srgrimes * Copyright (c) 1990, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * Redistribution and use in source and binary forms, with or without 61553Srgrimes * modification, are permitted provided that the following conditions 71553Srgrimes * are met: 81553Srgrimes * 1. Redistributions of source code must retain the above copyright 91553Srgrimes * notice, this list of conditions and the following disclaimer. 101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111553Srgrimes * notice, this list of conditions and the following disclaimer in the 121553Srgrimes * documentation and/or other materials provided with the distribution. 13121300Sphk * 3. Neither the name of the University nor the names of its contributors 141553Srgrimes * may be used to endorse or promote products derived from this software 151553Srgrimes * without specific prior written permission. 161553Srgrimes * 171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271553Srgrimes * SUCH DAMAGE. 281553Srgrimes */ 291553Srgrimes 30114601Sobrien#if 0 311553Srgrimes#ifndef lint 321553Srgrimesstatic char sccsid[] = "@(#)verify.c 8.1 (Berkeley) 6/6/93"; 33114601Sobrien#endif /* not lint */ 3430027Scharnier#endif 35114601Sobrien#include <sys/cdefs.h> 36114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/mtree/verify.c 122135 2003-11-05 20:07:40Z phk $"); 371553Srgrimes 381553Srgrimes#include <sys/param.h> 391553Srgrimes#include <sys/stat.h> 401553Srgrimes#include <dirent.h> 4130027Scharnier#include <err.h> 4230027Scharnier#include <errno.h> 431553Srgrimes#include <fts.h> 441553Srgrimes#include <fnmatch.h> 4530027Scharnier#include <stdio.h> 461553Srgrimes#include <unistd.h> 471553Srgrimes#include "mtree.h" 481553Srgrimes#include "extern.h" 491553Srgrimes 502860Srgrimesextern int ftsoptions; 5163853Simpextern int dflag, eflag, qflag, rflag, sflag, uflag; 521553Srgrimesextern char fullpath[MAXPATHLEN]; 5330027Scharnierextern int lineno; 541553Srgrimes 551553Srgrimesstatic NODE *root; 561553Srgrimesstatic char path[MAXPATHLEN]; 571553Srgrimes 5899800Salfredstatic void miss(NODE *, char *); 5999800Salfredstatic int vwalk(void); 601553Srgrimes 611553Srgrimesint 62122135Sphkmtree_verifyspec(FILE *fi) 631553Srgrimes{ 641553Srgrimes int rval; 651553Srgrimes 66122135Sphk root = mtree_readspec(fi); 671553Srgrimes rval = vwalk(); 681553Srgrimes miss(root, path); 691553Srgrimes return (rval); 701553Srgrimes} 711553Srgrimes 721553Srgrimesstatic int 73121299Sphkvwalk(void) 741553Srgrimes{ 75121299Sphk FTS *t; 76121299Sphk FTSENT *p; 77121299Sphk NODE *ep, *level; 789675Sbde int specdepth, rval; 791553Srgrimes char *argv[2]; 8099802Salfred char dot[] = "."; 811553Srgrimes 8299802Salfred argv[0] = dot; 831553Srgrimes argv[1] = NULL; 841553Srgrimes if ((t = fts_open(argv, ftsoptions, NULL)) == NULL) 8530027Scharnier err(1, "line %d: fts_open", lineno); 861553Srgrimes level = root; 879675Sbde specdepth = rval = 0; 882860Srgrimes while ((p = fts_read(t))) { 8960418Swollman if (check_excludes(p->fts_name, p->fts_path)) { 9060418Swollman fts_set(t, p, FTS_SKIP); 9160418Swollman continue; 9260418Swollman } 931553Srgrimes switch(p->fts_info) { 941553Srgrimes case FTS_D: 9563086Sjoe case FTS_SL: 961553Srgrimes break; 971553Srgrimes case FTS_DP: 989675Sbde if (specdepth > p->fts_level) { 991553Srgrimes for (level = level->parent; level->prev; 1008857Srgrimes level = level->prev); 1011553Srgrimes --specdepth; 1021553Srgrimes } 1031553Srgrimes continue; 1041553Srgrimes case FTS_DNR: 1051553Srgrimes case FTS_ERR: 1061553Srgrimes case FTS_NS: 10730027Scharnier warnx("%s: %s", RP(p), strerror(p->fts_errno)); 1081553Srgrimes continue; 1091553Srgrimes default: 1101553Srgrimes if (dflag) 1111553Srgrimes continue; 1121553Srgrimes } 1131553Srgrimes 1149675Sbde if (specdepth != p->fts_level) 1159675Sbde goto extra; 1161553Srgrimes for (ep = level; ep; ep = ep->next) 1172860Srgrimes if ((ep->flags & F_MAGIC && 1182860Srgrimes !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) || 1191553Srgrimes !strcmp(ep->name, p->fts_name)) { 1201553Srgrimes ep->flags |= F_VISIT; 12136670Speter if ((ep->flags & F_NOCHANGE) == 0 && 12236670Speter compare(ep->name, ep, p)) 1231553Srgrimes rval = MISMATCHEXIT; 12436841Speter if (ep->flags & F_IGN) 1251553Srgrimes (void)fts_set(t, p, FTS_SKIP); 1261553Srgrimes else if (ep->child && ep->type == F_DIR && 1271553Srgrimes p->fts_info == FTS_D) { 1281553Srgrimes level = ep->child; 1291553Srgrimes ++specdepth; 1301553Srgrimes } 1311553Srgrimes break; 1321553Srgrimes } 1331553Srgrimes 1341553Srgrimes if (ep) 1351553Srgrimes continue; 1369675Sbdeextra: 1371553Srgrimes if (!eflag) { 13866584Sphk (void)printf("%s extra", RP(p)); 1391553Srgrimes if (rflag) { 1409675Sbde if ((S_ISDIR(p->fts_statp->st_mode) 1419675Sbde ? rmdir : unlink)(p->fts_accpath)) { 1421553Srgrimes (void)printf(", not removed: %s", 1431553Srgrimes strerror(errno)); 1441553Srgrimes } else 1451553Srgrimes (void)printf(", removed"); 1461553Srgrimes } 1471553Srgrimes (void)putchar('\n'); 1481553Srgrimes } 1491553Srgrimes (void)fts_set(t, p, FTS_SKIP); 1501553Srgrimes } 1511553Srgrimes (void)fts_close(t); 1521553Srgrimes if (sflag) 153112214Srobert warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total); 1541553Srgrimes return (rval); 1551553Srgrimes} 1561553Srgrimes 1571553Srgrimesstatic void 158121299Sphkmiss(NODE *p, char *tail) 1591553Srgrimes{ 160121299Sphk int create; 161121299Sphk char *tp; 16263087Sjoe const char *type; 1631553Srgrimes 1641553Srgrimes for (; p; p = p->next) { 1651553Srgrimes if (p->type != F_DIR && (dflag || p->flags & F_VISIT)) 1661553Srgrimes continue; 1671553Srgrimes (void)strcpy(tail, p->name); 16863853Simp if (!(p->flags & F_VISIT)) { 16963853Simp /* Don't print missing message if file exists as a 17063853Simp symbolic link and the -q flag is set. */ 17163853Simp struct stat statbuf; 172121300Sphk 17363853Simp if (qflag && stat(path, &statbuf) == 0) 17463853Simp p->flags |= F_VISIT; 17563853Simp else 17666584Sphk (void)printf("%s missing", path); 17763853Simp } 17863087Sjoe if (p->type != F_DIR && p->type != F_LINK) { 1791553Srgrimes putchar('\n'); 1801553Srgrimes continue; 1811553Srgrimes } 1821553Srgrimes 1831553Srgrimes create = 0; 18463087Sjoe if (p->type == F_LINK) 18563087Sjoe type = "symlink"; 18663087Sjoe else 18763087Sjoe type = "directory"; 18851705Sbillf if (!(p->flags & F_VISIT) && uflag) { 1891553Srgrimes if (!(p->flags & (F_UID | F_UNAME))) 19063087Sjoe (void)printf(" (%s not created: user not specified)", type); 1911553Srgrimes else if (!(p->flags & (F_GID | F_GNAME))) 19263087Sjoe (void)printf(" (%s not created: group not specified)", type); 19363087Sjoe else if (p->type == F_LINK) { 19463087Sjoe if (symlink(p->slink, path)) 19563087Sjoe (void)printf(" (symlink not created: %s)\n", 19663087Sjoe strerror(errno)); 19763087Sjoe else 19863087Sjoe (void)printf(" (created)\n"); 19963087Sjoe if (lchown(path, p->st_uid, p->st_gid)) 20063087Sjoe (void)printf("%s: user/group not modified: %s\n", 20163087Sjoe path, strerror(errno)); 20263087Sjoe continue; 20363087Sjoe } else if (!(p->flags & F_MODE)) 20436670Speter (void)printf(" (directory not created: mode not specified)"); 2051553Srgrimes else if (mkdir(path, S_IRWXU)) 20636670Speter (void)printf(" (directory not created: %s)", 2071553Srgrimes strerror(errno)); 2081553Srgrimes else { 2091553Srgrimes create = 1; 2101553Srgrimes (void)printf(" (created)"); 2111553Srgrimes } 21251705Sbillf } 2131553Srgrimes if (!(p->flags & F_VISIT)) 2141553Srgrimes (void)putchar('\n'); 2151553Srgrimes 2161553Srgrimes for (tp = tail; *tp; ++tp); 2171553Srgrimes *tp = '/'; 2181553Srgrimes miss(p->child, tp + 1); 2191553Srgrimes *tp = '\0'; 2201553Srgrimes 2211553Srgrimes if (!create) 2221553Srgrimes continue; 2231553Srgrimes if (chown(path, p->st_uid, p->st_gid)) { 2241553Srgrimes (void)printf("%s: user/group/mode not modified: %s\n", 2251553Srgrimes path, strerror(errno)); 22654375Sjoe (void)printf("%s: warning: file mode %snot set\n", path, 22754375Sjoe (p->flags & F_FLAGS) ? "and file flags " : ""); 2281553Srgrimes continue; 2291553Srgrimes } 2301553Srgrimes if (chmod(path, p->st_mode)) 2311553Srgrimes (void)printf("%s: permissions not set: %s\n", 2321553Srgrimes path, strerror(errno)); 23354375Sjoe if ((p->flags & F_FLAGS) && p->st_flags && 23454375Sjoe chflags(path, p->st_flags)) 23554375Sjoe (void)printf("%s: file flags not set: %s\n", 23654375Sjoe path, strerror(errno)); 2371553Srgrimes } 2381553Srgrimes} 239