1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#if 0 31#ifndef lint 32static char sccsid[] = "@(#)verify.c 8.1 (Berkeley) 6/6/93"; 33#endif /* not lint */ 34#endif 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD$"); 37 38#include <sys/param.h> 39#include <sys/stat.h> 40#include <dirent.h> 41#include <err.h> 42#include <errno.h> 43#include <fts.h> 44#include <fnmatch.h> 45#include <stdio.h> 46#include <unistd.h> 47#include "mtree.h" 48#include "extern.h" 49 50static NODE *root; 51static char path[MAXPATHLEN]; 52 53static void miss(NODE *, char *); 54static int vwalk(void); 55 56int 57mtree_verifyspec(FILE *fi) 58{ 59 int rval; 60 61 root = mtree_readspec(fi); 62 rval = vwalk(); 63 miss(root, path); 64 return (rval); 65} 66 67static int 68nsort(const FTSENT * const *a, const FTSENT * const *b) 69{ 70 return (strcmp((*a)->fts_name, (*b)->fts_name)); 71} 72 73static int 74vwalk(void) 75{ 76 FTS *t; 77 FTSENT *p; 78 NODE *ep, *level; 79 int specdepth, rval; 80 char *argv[2]; 81 char dot[] = "."; 82 83 argv[0] = dot; 84 argv[1] = NULL; 85 if ((t = fts_open(argv, ftsoptions, nsort)) == NULL) 86 err(1, "line %d: fts_open", lineno); 87 level = root; 88 specdepth = rval = 0; 89 while ((p = fts_read(t))) { 90 if (check_excludes(p->fts_name, p->fts_path)) { 91 fts_set(t, p, FTS_SKIP); 92 continue; 93 } 94 switch(p->fts_info) { 95 case FTS_D: 96 case FTS_SL: 97 break; 98 case FTS_DP: 99 if (specdepth > p->fts_level) { 100 for (level = level->parent; level->prev; 101 level = level->prev); 102 --specdepth; 103 } 104 continue; 105 case FTS_DNR: 106 case FTS_ERR: 107 case FTS_NS: 108 warnx("%s: %s", RP(p), strerror(p->fts_errno)); 109 continue; 110 default: 111 if (dflag) 112 continue; 113 } 114 115 if (specdepth != p->fts_level) 116 goto extra; 117 for (ep = level; ep; ep = ep->next) 118 if ((ep->flags & F_MAGIC && 119 !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) || 120 !strcmp(ep->name, p->fts_name)) { 121 ep->flags |= F_VISIT; 122 if ((ep->flags & F_NOCHANGE) == 0 && 123 compare(ep->name, ep, p)) 124 rval = MISMATCHEXIT; 125 if (ep->flags & F_IGN) 126 (void)fts_set(t, p, FTS_SKIP); 127 else if (ep->child && ep->type == F_DIR && 128 p->fts_info == FTS_D) { 129 level = ep->child; 130 ++specdepth; 131 } 132 break; 133 } 134 135 if (ep) 136 continue; 137extra: 138 if (!eflag) { 139 (void)printf("%s extra", RP(p)); 140 if (rflag) { 141 if ((S_ISDIR(p->fts_statp->st_mode) 142 ? rmdir : unlink)(p->fts_accpath)) { 143 (void)printf(", not removed: %s", 144 strerror(errno)); 145 } else 146 (void)printf(", removed"); 147 } 148 (void)putchar('\n'); 149 } 150 (void)fts_set(t, p, FTS_SKIP); 151 } 152 (void)fts_close(t); 153 if (sflag) 154 warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total); 155 return (rval); 156} 157 158static void 159miss(NODE *p, char *tail) 160{ 161 int create; 162 char *tp; 163 const char *type, *what; 164 int serr; 165 166 for (; p; p = p->next) { 167 if (p->flags & F_OPT && !(p->flags & F_VISIT)) 168 continue; 169 if (p->type != F_DIR && (dflag || p->flags & F_VISIT)) 170 continue; 171 (void)strcpy(tail, p->name); 172 if (!(p->flags & F_VISIT)) { 173 /* Don't print missing message if file exists as a 174 symbolic link and the -q flag is set. */ 175 struct stat statbuf; 176 177 if (qflag && stat(path, &statbuf) == 0) 178 p->flags |= F_VISIT; 179 else 180 (void)printf("%s missing", path); 181 } 182 if (p->type != F_DIR && p->type != F_LINK) { 183 putchar('\n'); 184 continue; 185 } 186 187 create = 0; 188 if (p->type == F_LINK) 189 type = "symlink"; 190 else 191 type = "directory"; 192 if (!(p->flags & F_VISIT) && uflag) { 193 if (!(p->flags & (F_UID | F_UNAME))) 194 (void)printf(" (%s not created: user not specified)", type); 195 else if (!(p->flags & (F_GID | F_GNAME))) 196 (void)printf(" (%s not created: group not specified)", type); 197 else if (p->type == F_LINK) { 198 if (symlink(p->slink, path)) 199 (void)printf(" (symlink not created: %s)\n", 200 strerror(errno)); 201 else 202 (void)printf(" (created)\n"); 203 if (lchown(path, p->st_uid, p->st_gid) == -1) { 204 serr = errno; 205 if (p->st_uid == (uid_t)-1) 206 what = "group"; 207 else if (lchown(path, (uid_t)-1, 208 p->st_gid) == -1) 209 what = "user & group"; 210 else { 211 what = "user"; 212 errno = serr; 213 } 214 (void)printf("%s: %s not modified: %s" 215 "\n", path, what, strerror(errno)); 216 } 217 continue; 218 } else if (!(p->flags & F_MODE)) 219 (void)printf(" (directory not created: mode not specified)"); 220 else if (mkdir(path, S_IRWXU)) 221 (void)printf(" (directory not created: %s)", 222 strerror(errno)); 223 else { 224 create = 1; 225 (void)printf(" (created)"); 226 } 227 } 228 if (!(p->flags & F_VISIT)) 229 (void)putchar('\n'); 230 231 for (tp = tail; *tp; ++tp); 232 *tp = '/'; 233 miss(p->child, tp + 1); 234 *tp = '\0'; 235 236 if (!create) 237 continue; 238 if (chown(path, p->st_uid, p->st_gid) == -1) { 239 serr = errno; 240 if (p->st_uid == (uid_t)-1) 241 what = "group"; 242 else if (chown(path, (uid_t)-1, p->st_gid) == -1) 243 what = "user & group"; 244 else { 245 what = "user"; 246 errno = serr; 247 } 248 (void)printf("%s: %s not modified: %s\n", 249 path, what, strerror(errno)); 250 } 251 if (chmod(path, p->st_mode)) 252 (void)printf("%s: permissions not set: %s\n", 253 path, strerror(errno)); 254 if ((p->flags & F_FLAGS) && p->st_flags && 255 chflags(path, p->st_flags)) 256 (void)printf("%s: file flags not set: %s\n", 257 path, strerror(errno)); 258 } 259} 260