2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 static char sccsid
[] = "@(#)verify.c 8.1 (Berkeley) 6/6/93";
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 $");
38 #include <sys/param.h>
48 #include <removefile.h>
54 static char path
[MAXPATHLEN
];
56 static int miss(NODE
*, char *, size_t path_length
);
57 static int vwalk(void);
60 mtree_verifyspec(FILE *fi
)
63 size_t path_length
= 0;
65 root
= mtree_readspec(fi
);
67 mval
= miss(root
, path
, path_length
);
70 RECORD_FAILURE(60, WARN_MISMATCH
);
74 RECORD_FAILURE(61, WARN_MISMATCH
);
93 if ((t
= fts_open(argv
, ftsoptions
, NULL
)) == NULL
) {
95 RECORD_FAILURE(62, error
);
96 errc(1, error
, "line %d: fts_open", lineno
);
100 while ((p
= fts_read(t
))) {
101 if (check_excludes(p
->fts_name
, p
->fts_path
)) {
102 fts_set(t
, p
, FTS_SKIP
);
105 switch(p
->fts_info
) {
111 RECORD_FAILURE(63, EINVAL
);
112 errx(1 , "invalid root in vwalk");
114 if (specdepth
> p
->fts_level
) {
115 for (level
= level
->parent
; level
->prev
;
116 level
= level
->prev
);
123 warnx("%s: %s", RP(p
), strerror(p
->fts_errno
));
130 if (specdepth
!= p
->fts_level
)
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
;
137 if ((ep
->flags
& F_NOCHANGE
) == 0 &&
138 compare(ep
->name
, ep
, p
)) {
139 RECORD_FAILURE(64, WARN_MISMATCH
);
142 if (ep
->flags
& F_IGN
)
143 (void)fts_set(t
, p
, FTS_SKIP
);
144 else if (ep
->child
&& ep
->type
== F_DIR
&&
145 p
->fts_info
== FTS_D
) {
156 (void)printf("%s extra", RP(p
));
159 /* rflag implies: delete stuff if "extra" is observed" */
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
))) {
166 RECORD_FAILURE(65, error
);
167 errx (1, "\n error deleting item (or descendant) at path %s (%s)", RP(p
), strerror(error
));
170 /* removefile success */
171 (void) printf(", removed");
173 removefile_state_free(rmstate
);
177 /* legacy: use rmdir/unlink if "-m" not specified */
180 if (S_ISDIR(p
->fts_statp
->st_mode
)){
181 syserr
= rmdir(p
->fts_accpath
);
184 syserr
= unlink(p
->fts_accpath
);
190 RECORD_FAILURE(66, error
);
191 (void) printf(", not removed :%s", strerror(error
));
195 RECORD_FAILURE(68956, WARN_MISMATCH
);
196 errx(1, "cannot generate the XML dictionary");
200 (void)fts_set(t
, p
, FTS_SKIP
);
204 RECORD_FAILURE(67, WARN_CHECKSUM
);
205 warnx("%s checksum: %lu", fullpath
, (unsigned long)crc_total
);
211 miss(NODE
*p
, char *tail
, size_t path_length
)
215 const char *type
, *what
;
219 size_t file_name_length
= 0;
221 for (; p
; p
= p
->next
) {
222 if (p
->type
!= F_DIR
&& (dflag
|| p
->flags
& F_VISIT
))
224 file_name_length
= strnlen(p
->name
, MAXPATHLEN
);
225 path_length
+= file_name_length
;
226 if (path_length
>= MAXPATHLEN
) {
227 RECORD_FAILURE(61971, ENAMETOOLONG
);
230 (void)strcpy(tail
, p
->name
);
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. */
236 if (qflag
&& stat(path
, &statbuf
) == 0) {
239 (void)printf("%s missing", path
);
240 RECORD_FAILURE(68, WARN_MISMATCH
);
244 if (p
->type
!= F_DIR
&& p
->type
!= F_LINK
) {
250 if (p
->type
== F_LINK
)
254 if (!(p
->flags
& F_VISIT
) && uflag
) {
255 if (!(p
->flags
& (F_UID
| F_UNAME
))) {
256 (void)printf(" (%s not created: user not specified)", type
);
257 } else if (!(p
->flags
& (F_GID
| F_GNAME
))) {
258 (void)printf(" (%s not created: group not specified)", type
);
259 } else if (p
->type
== F_LINK
) {
260 if (symlink(p
->slink
, path
)) {
262 RECORD_FAILURE(69, serr
);
263 (void)printf(" (symlink not created: %s)\n",
266 (void)printf(" (created)\n");
268 if (lchown(path
, p
->st_uid
, p
->st_gid
) == -1) {
270 if (p
->st_uid
== (uid_t
)-1)
272 else if (lchown(path
, (uid_t
)-1,
274 what
= "user & group";
280 RECORD_FAILURE(70, serr
);
281 (void)printf("%s: %s not modified: %s"
282 "\n", path
, what
, strerror(serr
));
285 } else if (!(p
->flags
& F_MODE
)) {
286 (void)printf(" (directory not created: mode not specified)");
287 } else if (mkdir(path
, S_IRWXU
)) {
289 RECORD_FAILURE(71, serr
);
290 (void)printf(" (directory not created: %s)",
294 (void)printf(" (created)");
297 if (!(p
->flags
& F_VISIT
))
300 for (tp
= tail
; *tp
; ++tp
);
303 rrval
= miss(p
->child
, tp
+ 1, path_length
);
305 RECORD_FAILURE(72, WARN_MISMATCH
);
308 path_length
-= (file_name_length
+ 1);
313 if (chown(path
, p
->st_uid
, p
->st_gid
) == -1) {
315 if (p
->st_uid
== (uid_t
)-1)
317 else if (chown(path
, (uid_t
)-1, p
->st_gid
) == -1)
318 what
= "user & group";
324 RECORD_FAILURE(73, serr
);
325 (void)printf("%s: %s not modified: %s\n",
326 path
, what
, strerror(serr
));
328 if (chmod(path
, p
->st_mode
)) {
330 RECORD_FAILURE(74, serr
);
331 (void)printf("%s: permissions not set: %s\n",
332 path
, strerror(serr
));
334 if ((p
->flags
& F_FLAGS
) && p
->st_flags
&&
335 chflags(path
, (u_int
)p
->st_flags
)) {
337 RECORD_FAILURE(75, serr
);
338 (void)printf("%s: file flags not set: %s\n",
339 path
, strerror(serr
));