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