file_cmds-321.40.3.tar.gz
[apple/file_cmds.git] / mtree / verify.c
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
32 static char sccsid[] = "@(#)verify.c 8.1 (Berkeley) 6/6/93";
33 #endif /* not lint */
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.sbin/mtree/verify.c,v 1.24 2005/08/11 15:43:55 brian Exp $");
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 <stdint.h>
47 #include <unistd.h>
48 #include <removefile.h>
49 #include "metrics.h"
50 #include "mtree.h"
51 #include "extern.h"
52
53 static NODE *root;
54 static char path[MAXPATHLEN];
55
56 static int miss(NODE *, char *, size_t path_length);
57 static int vwalk(void);
58
59 int
60 mtree_verifyspec(FILE *fi)
61 {
62 int rval, mval;
63 size_t path_length = 0;
64
65 root = mtree_readspec(fi);
66 rval = vwalk();
67 mval = miss(root, path, path_length);
68
69 if (rval != 0) {
70 RECORD_FAILURE(60, WARN_MISMATCH);
71 return rval;
72 } else {
73 RECORD_FAILURE(61, WARN_MISMATCH);
74 return mval;
75 }
76 }
77
78 static int
79 vwalk(void)
80 {
81 int error = 0;
82 FTS *t;
83 FTSENT *p;
84 NODE *ep, *level;
85 int specdepth, rval;
86 char *argv[2];
87 char dot[] = ".";
88
89 argv[0] = dot;
90 argv[1] = NULL;
91 if ((t = fts_open(argv, ftsoptions, NULL)) == NULL) {
92 error = errno;
93 RECORD_FAILURE(62, error);
94 errc(1, error, "line %d: fts_open", lineno);
95 }
96 level = root;
97 specdepth = rval = 0;
98 while ((p = fts_read(t))) {
99 if (check_excludes(p->fts_name, p->fts_path)) {
100 fts_set(t, p, FTS_SKIP);
101 continue;
102 }
103 switch(p->fts_info) {
104 case FTS_D:
105 case FTS_SL:
106 break;
107 case FTS_DP:
108 if (level == NULL) {
109 RECORD_FAILURE(63, EINVAL);
110 errx(1 , "invalid root in vwalk");
111 }
112 if (specdepth > p->fts_level) {
113 for (level = level->parent; level->prev;
114 level = level->prev);
115 --specdepth;
116 }
117 continue;
118 case FTS_DNR:
119 case FTS_ERR:
120 case FTS_NS:
121 warnx("%s: %s", RP(p), strerror(p->fts_errno));
122 continue;
123 default:
124 if (dflag)
125 continue;
126 }
127
128 if (specdepth != p->fts_level)
129 goto extra;
130 for (ep = level; ep; ep = ep->next)
131 if ((ep->flags & F_MAGIC &&
132 !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
133 !strcmp(ep->name, p->fts_name)) {
134 ep->flags |= F_VISIT;
135 if ((ep->flags & F_NOCHANGE) == 0 &&
136 compare(ep->name, ep, p)) {
137 RECORD_FAILURE(64, WARN_MISMATCH);
138 rval = MISMATCHEXIT;
139 }
140 if (ep->flags & F_IGN)
141 (void)fts_set(t, p, FTS_SKIP);
142 else if (ep->child && ep->type == F_DIR &&
143 p->fts_info == FTS_D) {
144 level = ep->child;
145 ++specdepth;
146 }
147 break;
148 }
149
150 if (ep)
151 continue;
152 extra:
153 if (!eflag) {
154 (void)printf("%s extra", RP(p));
155
156 if (rflag) {
157 /* rflag implies: delete stuff if "extra" is observed" */
158 if (mflag) {
159 /* -mflag is used for sealing & verification -- use removefile for recursive behavior */
160 removefile_state_t rmstate;
161 rmstate = removefile_state_alloc();
162 if (removefile(p->fts_accpath, rmstate, (REMOVEFILE_RECURSIVE))) {
163 error = errno;
164 RECORD_FAILURE(65, error);
165 errx (1, "\n error deleting item (or descendant) at path %s (%s)", RP(p), strerror(error));
166 }
167 else {
168 /* removefile success */
169 (void) printf(", removed");
170 }
171 removefile_state_free(rmstate);
172
173 }
174 else {
175 /* legacy: use rmdir/unlink if "-m" not specified */
176 int syserr = 0;
177
178 if (S_ISDIR(p->fts_statp->st_mode)){
179 syserr = rmdir(p->fts_accpath);
180 }
181 else {
182 syserr = unlink(p->fts_accpath);
183 }
184
185 /* log failures */
186 if (syserr) {
187 error = errno;
188 RECORD_FAILURE(66, error);
189 (void) printf(", not removed :%s", strerror(error));
190 }
191 }
192 } else if (mflag) {
193 RECORD_FAILURE(68956, WARN_MISMATCH);
194 errx(1, "cannot generate the XML dictionary");
195 }
196 (void)putchar('\n');
197 }
198 (void)fts_set(t, p, FTS_SKIP);
199 }
200 (void)fts_close(t);
201 if (sflag) {
202 RECORD_FAILURE(67, WARN_CHECKSUM);
203 warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);
204 }
205 return (rval);
206 }
207
208 static int
209 miss(NODE *p, char *tail, size_t path_length)
210 {
211 int create;
212 char *tp;
213 const char *type, *what;
214 int serr;
215 int rval = 0;
216 int rrval = 0;
217 size_t file_name_length = 0;
218
219 for (; p; p = p->next) {
220 if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
221 continue;
222 file_name_length = strnlen(p->name, MAXPATHLEN);
223 path_length += file_name_length;
224 if (path_length >= MAXPATHLEN) {
225 RECORD_FAILURE(61971, ENAMETOOLONG);
226 continue;
227 }
228 (void)strcpy(tail, p->name);
229 if (!(p->flags & F_VISIT)) {
230 /* Don't print missing message if file exists as a
231 symbolic link and the -q flag is set. */
232 struct stat statbuf;
233
234 if (qflag && stat(path, &statbuf) == 0) {
235 p->flags |= F_VISIT;
236 } else {
237 (void)printf("%s missing", path);
238 RECORD_FAILURE(68, WARN_MISMATCH);
239 rval = MISMATCHEXIT;
240 }
241 }
242 if (p->type != F_DIR && p->type != F_LINK) {
243 putchar('\n');
244 continue;
245 }
246
247 create = 0;
248 if (p->type == F_LINK)
249 type = "symlink";
250 else
251 type = "directory";
252 if (!(p->flags & F_VISIT) && uflag) {
253 if (!(p->flags & (F_UID | F_UNAME))) {
254 (void)printf(" (%s not created: user not specified)", type);
255 } else if (!(p->flags & (F_GID | F_GNAME))) {
256 (void)printf(" (%s not created: group not specified)", type);
257 } else if (p->type == F_LINK) {
258 if (symlink(p->slink, path)) {
259 serr = errno;
260 RECORD_FAILURE(69, serr);
261 (void)printf(" (symlink not created: %s)\n",
262 strerror(serr));
263 } else {
264 (void)printf(" (created)\n");
265 }
266 if (lchown(path, p->st_uid, p->st_gid) == -1) {
267 serr = errno;
268 if (p->st_uid == (uid_t)-1)
269 what = "group";
270 else if (lchown(path, (uid_t)-1,
271 p->st_gid) == -1)
272 what = "user & group";
273 else {
274 what = "user";
275 errno = serr;
276 }
277 serr = errno;
278 RECORD_FAILURE(70, serr);
279 (void)printf("%s: %s not modified: %s"
280 "\n", path, what, strerror(serr));
281 }
282 continue;
283 } else if (!(p->flags & F_MODE)) {
284 (void)printf(" (directory not created: mode not specified)");
285 } else if (mkdir(path, S_IRWXU)) {
286 serr = errno;
287 RECORD_FAILURE(71, serr);
288 (void)printf(" (directory not created: %s)",
289 strerror(serr));
290 } else {
291 create = 1;
292 (void)printf(" (created)");
293 }
294 }
295 if (!(p->flags & F_VISIT))
296 (void)putchar('\n');
297
298 for (tp = tail; *tp; ++tp);
299 *tp = '/';
300 ++path_length;
301 rrval = miss(p->child, tp + 1, path_length);
302 if (rrval != 0) {
303 RECORD_FAILURE(72, WARN_MISMATCH);
304 rval = rrval;
305 }
306 path_length -= (file_name_length + 1);
307 *tp = '\0';
308
309 if (!create)
310 continue;
311 if (chown(path, p->st_uid, p->st_gid) == -1) {
312 serr = errno;
313 if (p->st_uid == (uid_t)-1)
314 what = "group";
315 else if (chown(path, (uid_t)-1, p->st_gid) == -1)
316 what = "user & group";
317 else {
318 what = "user";
319 errno = serr;
320 }
321 serr = errno;
322 RECORD_FAILURE(73, serr);
323 (void)printf("%s: %s not modified: %s\n",
324 path, what, strerror(serr));
325 }
326 if (chmod(path, p->st_mode)) {
327 serr = errno;
328 RECORD_FAILURE(74, serr);
329 (void)printf("%s: permissions not set: %s\n",
330 path, strerror(serr));
331 }
332 if ((p->flags & F_FLAGS) && p->st_flags &&
333 chflags(path, (u_int)p->st_flags)) {
334 serr = errno;
335 RECORD_FAILURE(75, serr);
336 (void)printf("%s: file flags not set: %s\n",
337 path, strerror(serr));
338 }
339 }
340 return rval;
341 }