xref: /openbsd-src/usr.sbin/mtree/verify.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: verify.c,v 1.18 2009/01/09 19:55:27 stsp Exp $	*/
2 /*	$NetBSD: verify.c,v 1.10 1995/03/07 21:26:28 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static const char sccsid[] = "@(#)verify.c	8.1 (Berkeley) 6/6/93";
36 #else
37 static const char rcsid[] = "$OpenBSD: verify.c,v 1.18 2009/01/09 19:55:27 stsp Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <dirent.h>
44 #include <fts.h>
45 #include <fnmatch.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include "mtree.h"
50 #include "extern.h"
51 
52 extern u_int32_t crc_total;
53 extern int ftsoptions;
54 extern int dflag, eflag, qflag, rflag, sflag, uflag;
55 extern char fullpath[MAXPATHLEN];
56 
57 static NODE *root;
58 static char path[MAXPATHLEN];
59 
60 static void	miss(NODE *, char *, size_t);
61 static int	vwalk(void);
62 
63 int
64 verify(void)
65 {
66 	int rval;
67 
68 	root = spec();
69 	rval = vwalk();
70 	miss(root, path, sizeof(path));
71 	return (rval);
72 }
73 
74 static int
75 vwalk(void)
76 {
77 	FTS *t;
78 	FTSENT *p;
79 	NODE *ep, *level;
80 	int specdepth, rval;
81 	char *argv[2];
82 
83 	argv[0] = ".";
84 	argv[1] = NULL;
85 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
86 		error("fts_open: %s", strerror(errno));
87 	level = root;
88 	specdepth = rval = 0;
89 	while ((p = fts_read(t))) {
90 		switch(p->fts_info) {
91 		case FTS_D:
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 			(void)fprintf(stderr, "mtree: %s: %s\n",
104 			    RP(p), strerror(p->fts_errno));
105 			continue;
106 		default:
107 			if (dflag)
108 				continue;
109 		}
110 
111 		if (specdepth != p->fts_level)
112 			goto extra;
113 		for (ep = level; ep; ep = ep->next)
114 			if ((ep->flags & F_MAGIC &&
115 			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
116 			    !strcmp(ep->name, p->fts_name)) {
117 				ep->flags |= F_VISIT;
118 				if ((ep->flags & F_NOCHANGE) == 0 &&
119 				    compare(ep->name, ep, p))
120 					rval = MISMATCHEXIT;
121 				if (ep->flags & F_IGN)
122 					(void)fts_set(t, p, FTS_SKIP);
123 				else if (ep->child && ep->type == F_DIR &&
124 				    p->fts_info == FTS_D) {
125 					level = ep->child;
126 					++specdepth;
127 				}
128 				break;
129 			}
130 
131 		if (ep)
132 			continue;
133 extra:
134 		if (!eflag) {
135 			(void)printf("extra: %s", RP(p));
136 			if (rflag) {
137 				if ((S_ISDIR(p->fts_statp->st_mode)
138 				    ? rmdir : unlink)(p->fts_accpath)) {
139 					(void)printf(", not removed: %s",
140 					    strerror(errno));
141 					rval = ERROREXIT;
142 				} else
143 					(void)printf(", removed");
144 			} else
145 				rval = MISMATCHEXIT;
146 			(void)putchar('\n');
147 		}
148 		(void)fts_set(t, p, FTS_SKIP);
149 	}
150 	(void)fts_close(t);
151 	if (sflag)
152 		(void)fprintf(stderr,
153 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
154 	return (rval);
155 }
156 
157 static void
158 miss(NODE *p, char *tail, size_t len)
159 {
160 	int create;
161 	char *tp;
162 
163 	for (; p; p = p->next) {
164 		if ((p->flags & F_OPT) && !(p->flags & F_VISIT))
165 			continue;
166 		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
167 			continue;
168 		(void)strlcpy(tail, p->name, len);
169 		if (!(p->flags & F_VISIT)) {
170 			/* Don't print missing message if file exists as a
171 			   symbolic link and the -q flag is set. */
172 			struct stat statbuf;
173 
174 			if (qflag && stat(path, &statbuf) == 0)
175 				p->flags |= F_VISIT;
176 			else
177 				(void)printf("missing: %s", path);
178 		}
179 		if (p->type != F_DIR) {
180 			putchar('\n');
181 			continue;
182 		}
183 
184 		create = 0;
185 		if (!(p->flags & F_VISIT) && uflag) {
186 			if (!(p->flags & (F_UID | F_UNAME)))
187 			    (void)printf(" (not created: user not specified)");
188 			else if (!(p->flags & (F_GID | F_GNAME)))
189 			    (void)printf(" (not created: group not specified)");
190 			else if (!(p->flags & F_MODE))
191 			    (void)printf(" (not created: mode not specified)");
192 			else if (mkdir(path, S_IRWXU))
193 				(void)printf(" (not created: %s)",
194 				    strerror(errno));
195 			else {
196 				create = 1;
197 				(void)printf(" (created)");
198 			}
199 		}
200 
201 		if (!(p->flags & F_VISIT))
202 			(void)putchar('\n');
203 
204 		for (tp = tail; *tp; ++tp);
205 		*tp = '/';
206 		miss(p->child, tp + 1, len - (tp + 1 - tail));
207 		*tp = '\0';
208 
209 		if (!create)
210 			continue;
211 		if (chown(path, p->st_uid, p->st_gid)) {
212 			(void)printf("%s: user/group/mode not modified: %s\n",
213 			    path, strerror(errno));
214 			continue;
215 		}
216 		if (chmod(path, p->st_mode))
217 			(void)printf("%s: permissions not set: %s\n",
218 			    path, strerror(errno));
219 	}
220 }
221