1 /* $OpenBSD: verify.c,v 1.16 2004/11/21 19:36:04 otto 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.16 2004/11/21 19:36:04 otto 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 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
verify(void)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
vwalk(void)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 } else
142 (void)printf(", removed");
143 }
144 (void)putchar('\n');
145 }
146 (void)fts_set(t, p, FTS_SKIP);
147 }
148 (void)fts_close(t);
149 if (sflag)
150 (void)fprintf(stderr,
151 "mtree: %s checksum: %u\n", fullpath, crc_total);
152 return (rval);
153 }
154
155 static void
miss(NODE * p,char * tail,size_t len)156 miss(NODE *p, char *tail, size_t len)
157 {
158 int create;
159 char *tp;
160
161 for (; p; p = p->next) {
162 if ((p->flags & F_OPT) && !(p->flags & F_VISIT))
163 continue;
164 if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
165 continue;
166 (void)strlcpy(tail, p->name, len);
167 if (!(p->flags & F_VISIT)) {
168 /* Don't print missing message if file exists as a
169 symbolic link and the -q flag is set. */
170 struct stat statbuf;
171
172 if (qflag && stat(path, &statbuf) == 0)
173 p->flags |= F_VISIT;
174 else
175 (void)printf("missing: %s", path);
176 }
177 if (p->type != F_DIR) {
178 putchar('\n');
179 continue;
180 }
181
182 create = 0;
183 if (!(p->flags & F_VISIT) && uflag) {
184 if (!(p->flags & (F_UID | F_UNAME)))
185 (void)printf(" (not created: user not specified)");
186 else if (!(p->flags & (F_GID | F_GNAME)))
187 (void)printf(" (not created: group not specified)");
188 else if (!(p->flags & F_MODE))
189 (void)printf(" (not created: mode not specified)");
190 else if (mkdir(path, S_IRWXU))
191 (void)printf(" (not created: %s)",
192 strerror(errno));
193 else {
194 create = 1;
195 (void)printf(" (created)");
196 }
197 }
198
199 if (!(p->flags & F_VISIT))
200 (void)putchar('\n');
201
202 for (tp = tail; *tp; ++tp);
203 *tp = '/';
204 miss(p->child, tp + 1, len - (tp + 1 - tail));
205 *tp = '\0';
206
207 if (!create)
208 continue;
209 if (chown(path, p->st_uid, p->st_gid)) {
210 (void)printf("%s: user/group/mode not modified: %s\n",
211 path, strerror(errno));
212 continue;
213 }
214 if (chmod(path, p->st_mode))
215 (void)printf("%s: permissions not set: %s\n",
216 path, strerror(errno));
217 }
218 }
219