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 (specdepth > p->fts_level) {
95				for (level = level->parent; level->prev;
96				      level = level->prev);
97				--specdepth;
98			}
99			continue;
100		case FTS_DNR:
101		case FTS_ERR:
102		case FTS_NS:
103			warnx("%s: %s", RP(p), strerror(p->fts_errno));
104			continue;
105		default:
106			if (dflag)
107				continue;
108		}
109
110		if (specdepth != p->fts_level)
111			goto extra;
112		for (ep = level; ep; ep = ep->next)
113			if ((ep->flags & F_MAGIC &&
114			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
115			    !strcmp(ep->name, p->fts_name)) {
116				ep->flags |= F_VISIT;
117				if ((ep->flags & F_NOCHANGE) == 0 &&
118				    compare(ep->name, ep, p))
119					rval = MISMATCHEXIT;
120				if (ep->flags & F_IGN)
121					(void)fts_set(t, p, FTS_SKIP);
122				else if (ep->child && ep->type == F_DIR &&
123				    p->fts_info == FTS_D) {
124					level = ep->child;
125					++specdepth;
126				}
127				break;
128			}
129
130		if (ep)
131			continue;
132extra:
133		if (!eflag) {
134			(void)printf("%s extra", RP(p));
135			if (rflag) {
136				if ((S_ISDIR(p->fts_statp->st_mode)
137				    ? rmdir : unlink)(p->fts_accpath)) {
138					(void)printf(", not removed: %s",
139					    strerror(errno));
140				} else
141					(void)printf(", removed");
142			}
143			(void)putchar('\n');
144		}
145		(void)fts_set(t, p, FTS_SKIP);
146	}
147	(void)fts_close(t);
148	if (sflag)
149		warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);
150	return (rval);
151}
152
153static void
154miss(NODE *p, char *tail)
155{
156	int create;
157	char *tp;
158	const char *type, *what;
159	int serr;
160
161	for (; p; p = p->next) {
162		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
163			continue;
164		(void)strcpy(tail, p->name);
165		if (!(p->flags & F_VISIT)) {
166			/* Don't print missing message if file exists as a
167			   symbolic link and the -q flag is set. */
168			struct stat statbuf;
169
170			if (qflag && stat(path, &statbuf) == 0)
171				p->flags |= F_VISIT;
172			else
173				(void)printf("%s missing", path);
174		}
175		if (p->type != F_DIR && p->type != F_LINK) {
176			putchar('\n');
177			continue;
178		}
179
180		create = 0;
181		if (p->type == F_LINK)
182			type = "symlink";
183		else
184			type = "directory";
185		if (!(p->flags & F_VISIT) && uflag) {
186			if (!(p->flags & (F_UID | F_UNAME)))
187				(void)printf(" (%s not created: user not specified)", type);
188			else if (!(p->flags & (F_GID | F_GNAME)))
189				(void)printf(" (%s not created: group not specified)", type);
190			else if (p->type == F_LINK) {
191				if (symlink(p->slink, path))
192					(void)printf(" (symlink not created: %s)\n",
193					    strerror(errno));
194				else
195					(void)printf(" (created)\n");
196				if (lchown(path, p->st_uid, p->st_gid) == -1) {
197					serr = errno;
198					if (p->st_uid == (uid_t)-1)
199						what = "group";
200					else if (lchown(path, (uid_t)-1,
201					    p->st_gid) == -1)
202						what = "user & group";
203					else {
204						what = "user";
205						errno = serr;
206					}
207					(void)printf("%s: %s not modified: %s"
208					    "\n", path, what, strerror(errno));
209				}
210				continue;
211			} else if (!(p->flags & F_MODE))
212			    (void)printf(" (directory not created: mode not specified)");
213			else if (mkdir(path, S_IRWXU))
214				(void)printf(" (directory not created: %s)",
215				    strerror(errno));
216			else {
217				create = 1;
218				(void)printf(" (created)");
219			}
220		}
221		if (!(p->flags & F_VISIT))
222			(void)putchar('\n');
223
224		for (tp = tail; *tp; ++tp);
225		*tp = '/';
226		miss(p->child, tp + 1);
227		*tp = '\0';
228
229		if (!create)
230			continue;
231		if (chown(path, p->st_uid, p->st_gid) == -1) {
232			serr = errno;
233			if (p->st_uid == (uid_t)-1)
234				what = "group";
235			else if (chown(path, (uid_t)-1, p->st_gid) == -1)
236				what = "user & group";
237			else {
238				what = "user";
239				errno = serr;
240			}
241			(void)printf("%s: %s not modified: %s\n",
242			    path, what, strerror(errno));
243		}
244		if (chmod(path, p->st_mode))
245			(void)printf("%s: permissions not set: %s\n",
246			    path, strerror(errno));
247		if ((p->flags & F_FLAGS) && p->st_flags &&
248		    chflags(path, (u_int)p->st_flags))
249			(void)printf("%s: file flags not set: %s\n",
250			    path, strerror(errno));
251	}
252}
253