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: 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 "mtree.h"
49#include "extern.h"
50
51static NODE *root;
52static char path[MAXPATHLEN];
53
54static void	miss(NODE *, char *);
55static int	vwalk(void);
56
57int
58mtree_verifyspec(FILE *fi)
59{
60	int rval;
61
62	root = mtree_readspec(fi);
63	rval = vwalk();
64	miss(root, path);
65	return (rval);
66}
67
68static int
69vwalk(void)
70{
71	FTS *t;
72	FTSENT *p;
73	NODE *ep, *level;
74	int specdepth, rval;
75	char *argv[2];
76	char dot[] = ".";
77
78	argv[0] = dot;
79	argv[1] = NULL;
80	if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
81		err(1, "line %d: fts_open", lineno);
82	level = root;
83	specdepth = rval = 0;
84	while ((p = fts_read(t))) {
85		if (check_excludes(p->fts_name, p->fts_path)) {
86			fts_set(t, p, FTS_SKIP);
87			continue;
88		}
89		switch(p->fts_info) {
90		case FTS_D:
91		case FTS_SL:
92			break;
93		case FTS_DP:
94			if (level == NULL) {
95				errx(1 , "invalid root in vwalk");
96			}
97			if (specdepth > p->fts_level) {
98				for (level = level->parent; level->prev;
99				      level = level->prev);
100				--specdepth;
101			}
102			continue;
103		case FTS_DNR:
104		case FTS_ERR:
105		case FTS_NS:
106			warnx("%s: %s", RP(p), strerror(p->fts_errno));
107			continue;
108		default:
109			if (dflag)
110				continue;
111		}
112
113		if (specdepth != p->fts_level)
114			goto extra;
115		for (ep = level; ep; ep = ep->next)
116			if ((ep->flags & F_MAGIC &&
117			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
118			    !strcmp(ep->name, p->fts_name)) {
119				ep->flags |= F_VISIT;
120				if ((ep->flags & F_NOCHANGE) == 0 &&
121				    compare(ep->name, ep, p))
122					rval = MISMATCHEXIT;
123				if (ep->flags & F_IGN)
124					(void)fts_set(t, p, FTS_SKIP);
125				else if (ep->child && ep->type == F_DIR &&
126				    p->fts_info == FTS_D) {
127					level = ep->child;
128					++specdepth;
129				}
130				break;
131			}
132
133		if (ep)
134			continue;
135extra:
136		if (!eflag) {
137			(void)printf("%s extra", RP(p));
138			if (rflag) {
139				if ((S_ISDIR(p->fts_statp->st_mode)
140				    ? rmdir : unlink)(p->fts_accpath)) {
141					(void)printf(", not removed: %s",
142					    strerror(errno));
143				} else
144					(void)printf(", removed");
145			}
146			(void)putchar('\n');
147		}
148		(void)fts_set(t, p, FTS_SKIP);
149	}
150	(void)fts_close(t);
151	if (sflag)
152		warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);
153	return (rval);
154}
155
156static void
157miss(NODE *p, char *tail)
158{
159	int create;
160	char *tp;
161	const char *type, *what;
162	int serr;
163
164	for (; p; p = p->next) {
165		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
166			continue;
167		(void)strcpy(tail, p->name);
168		if (!(p->flags & F_VISIT)) {
169			/* Don't print missing message if file exists as a
170			   symbolic link and the -q flag is set. */
171			struct stat statbuf;
172
173			if (qflag && stat(path, &statbuf) == 0)
174				p->flags |= F_VISIT;
175			else
176				(void)printf("%s missing", path);
177		}
178		if (p->type != F_DIR && p->type != F_LINK) {
179			putchar('\n');
180			continue;
181		}
182
183		create = 0;
184		if (p->type == F_LINK)
185			type = "symlink";
186		else
187			type = "directory";
188		if (!(p->flags & F_VISIT) && uflag) {
189			if (!(p->flags & (F_UID | F_UNAME)))
190				(void)printf(" (%s not created: user not specified)", type);
191			else if (!(p->flags & (F_GID | F_GNAME)))
192				(void)printf(" (%s not created: group not specified)", type);
193			else if (p->type == F_LINK) {
194				if (symlink(p->slink, path))
195					(void)printf(" (symlink not created: %s)\n",
196					    strerror(errno));
197				else
198					(void)printf(" (created)\n");
199				if (lchown(path, p->st_uid, p->st_gid) == -1) {
200					serr = errno;
201					if (p->st_uid == (uid_t)-1)
202						what = "group";
203					else if (lchown(path, (uid_t)-1,
204					    p->st_gid) == -1)
205						what = "user & group";
206					else {
207						what = "user";
208						errno = serr;
209					}
210					(void)printf("%s: %s not modified: %s"
211					    "\n", path, what, strerror(errno));
212				}
213				continue;
214			} else if (!(p->flags & F_MODE))
215			    (void)printf(" (directory not created: mode not specified)");
216			else if (mkdir(path, S_IRWXU))
217				(void)printf(" (directory not created: %s)",
218				    strerror(errno));
219			else {
220				create = 1;
221				(void)printf(" (created)");
222			}
223		}
224		if (!(p->flags & F_VISIT))
225			(void)putchar('\n');
226
227		for (tp = tail; *tp; ++tp);
228		*tp = '/';
229		miss(p->child, tp + 1);
230		*tp = '\0';
231
232		if (!create)
233			continue;
234		if (chown(path, p->st_uid, p->st_gid) == -1) {
235			serr = errno;
236			if (p->st_uid == (uid_t)-1)
237				what = "group";
238			else if (chown(path, (uid_t)-1, p->st_gid) == -1)
239				what = "user & group";
240			else {
241				what = "user";
242				errno = serr;
243			}
244			(void)printf("%s: %s not modified: %s\n",
245			    path, what, strerror(errno));
246		}
247		if (chmod(path, p->st_mode))
248			(void)printf("%s: permissions not set: %s\n",
249			    path, strerror(errno));
250		if ((p->flags & F_FLAGS) && p->st_flags &&
251		    chflags(path, (u_int)p->st_flags))
252			(void)printf("%s: file flags not set: %s\n",
253			    path, strerror(errno));
254	}
255}
256