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