Deleted Added
sdiff udiff text old ( 124387 ) new ( 144295 )
full compact
1/*-
2 * Copyright (c) 1989, 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[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93";
33#endif /* not lint */
34#endif
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/usr.sbin/mtree/compare.c 124387 2004-01-11 19:25:56Z phk $");
37
38#include <sys/param.h>
39#include <sys/stat.h>
40#include <sys/time.h>
41
42#include <err.h>
43#include <errno.h>
44#include <fcntl.h>
45#include <fts.h>
46#ifdef MD5
47#include <md5.h>
48#endif
49#ifdef RMD160
50#include <ripemd.h>
51#endif
52#ifdef SHA1
53#include <sha.h>
54#endif
55#include <stdint.h>
56#include <stdio.h>
57#include <time.h>
58#include <unistd.h>
59#include <vis.h>
60
61#include "mtree.h"
62#include "extern.h"
63
64#define INDENTNAMELEN 8
65#define LABEL \
66 if (!label++) { \
67 len = printf("%s changed\n", RP(p)); \
68 tab = "\t"; \
69 }
70
71int
72compare(char *name __unused, NODE *s, FTSENT *p)
73{
74 struct timeval tv[2];
75 uint32_t val;
76 int fd, label;
77 off_t len;
78 char *cp;
79 const char *tab = "";
80 char *fflags;
81
82 label = 0;
83 switch(s->type) {
84 case F_BLOCK:
85 if (!S_ISBLK(p->fts_statp->st_mode))
86 goto typeerr;
87 break;
88 case F_CHAR:
89 if (!S_ISCHR(p->fts_statp->st_mode))
90 goto typeerr;
91 break;
92 case F_DIR:
93 if (!S_ISDIR(p->fts_statp->st_mode))
94 goto typeerr;
95 break;
96 case F_FIFO:
97 if (!S_ISFIFO(p->fts_statp->st_mode))
98 goto typeerr;
99 break;
100 case F_FILE:
101 if (!S_ISREG(p->fts_statp->st_mode))
102 goto typeerr;
103 break;
104 case F_LINK:
105 if (!S_ISLNK(p->fts_statp->st_mode))
106 goto typeerr;
107 break;
108 case F_SOCK:
109 if (!S_ISSOCK(p->fts_statp->st_mode)) {
110typeerr: LABEL;
111 (void)printf("\ttype expected %s found %s\n",
112 ftype(s->type), inotype(p->fts_statp->st_mode));
113 return (label);
114 }
115 break;
116 }
117 /* Set the uid/gid first, then set the mode. */
118 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
119 LABEL;
120 (void)printf("%suser expected %lu found %lu",
121 tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
122 if (uflag)
123 if (chown(p->fts_accpath, s->st_uid, -1))
124 (void)printf(" not modified: %s\n",
125 strerror(errno));
126 else
127 (void)printf(" modified\n");
128 else
129 (void)printf("\n");
130 tab = "\t";
131 }
132 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
133 LABEL;
134 (void)printf("%sgid expected %lu found %lu",
135 tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
136 if (uflag)
137 if (chown(p->fts_accpath, -1, s->st_gid))
138 (void)printf(" not modified: %s\n",
139 strerror(errno));
140 else
141 (void)printf(" modified\n");
142 else
143 (void)printf("\n");
144 tab = "\t";
145 }
146 if (s->flags & F_MODE &&
147 !S_ISLNK(p->fts_statp->st_mode) &&
148 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
149 LABEL;
150 (void)printf("%spermissions expected %#o found %#o",
151 tab, s->st_mode, p->fts_statp->st_mode & MBITS);
152 if (uflag)
153 if (chmod(p->fts_accpath, s->st_mode))
154 (void)printf(" not modified: %s\n",
155 strerror(errno));
156 else
157 (void)printf(" modified\n");
158 else
159 (void)printf("\n");
160 tab = "\t";
161 }
162 if (s->flags & F_NLINK && s->type != F_DIR &&
163 s->st_nlink != p->fts_statp->st_nlink) {
164 LABEL;
165 (void)printf("%slink_count expected %u found %u\n",
166 tab, s->st_nlink, p->fts_statp->st_nlink);
167 tab = "\t";
168 }
169 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size &&
170 !S_ISDIR(p->fts_statp->st_mode)) {
171 LABEL;
172 (void)printf("%ssize expected %jd found %jd\n", tab,
173 (intmax_t)s->st_size, (intmax_t)p->fts_statp->st_size);
174 tab = "\t";
175 }
176 /*
177 * XXX
178 * Catches nano-second differences, but doesn't display them.
179 */
180 if ((s->flags & F_TIME) &&
181 ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) ||
182 (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) {
183 LABEL;
184 (void)printf("%smodification time expected %.24s ",
185 tab, ctime(&s->st_mtimespec.tv_sec));
186 (void)printf("found %.24s",
187 ctime(&p->fts_statp->st_mtimespec.tv_sec));
188 if (uflag) {
189 tv[0].tv_sec = s->st_mtimespec.tv_sec;
190 tv[0].tv_usec = s->st_mtimespec.tv_nsec / 1000;
191 tv[1] = tv[0];
192 if (utimes(p->fts_accpath, tv))
193 (void)printf(" not modified: %s\n",
194 strerror(errno));
195 else
196 (void)printf(" modified\n");
197 } else
198 (void)printf("\n");
199 tab = "\t";
200 }
201 if (s->flags & F_CKSUM) {
202 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
203 LABEL;
204 (void)printf("%scksum: %s: %s\n",
205 tab, p->fts_accpath, strerror(errno));
206 tab = "\t";
207 } else if (crc(fd, &val, &len)) {
208 (void)close(fd);
209 LABEL;
210 (void)printf("%scksum: %s: %s\n",
211 tab, p->fts_accpath, strerror(errno));
212 tab = "\t";
213 } else {
214 (void)close(fd);
215 if (s->cksum != val) {
216 LABEL;
217 (void)printf("%scksum expected %lu found %lu\n",
218 tab, s->cksum, (unsigned long)val);
219 tab = "\t";
220 }
221 }
222 }
223 if ((s->flags & F_FLAGS) && s->st_flags != p->fts_statp->st_flags) {
224 LABEL;
225 fflags = flags_to_string(s->st_flags);
226 (void)printf("%sflags expected \"%s\"", tab, fflags);
227 free(fflags);
228
229 fflags = flags_to_string(p->fts_statp->st_flags);
230 (void)printf(" found \"%s\"", fflags);
231 free(fflags);
232
233 if (uflag)
234 if (chflags(p->fts_accpath, s->st_flags))
235 (void)printf(" not modified: %s\n",
236 strerror(errno));
237 else
238 (void)printf(" modified\n");
239 else
240 (void)printf("\n");
241 tab = "\t";
242 }
243#ifdef MD5
244 if (s->flags & F_MD5) {
245 char *new_digest, buf[33];
246
247 new_digest = MD5File(p->fts_accpath, buf);
248 if (!new_digest) {
249 LABEL;
250 printf("%sMD5: %s: %s\n", tab, p->fts_accpath,
251 strerror(errno));
252 tab = "\t";
253 } else if (strcmp(new_digest, s->md5digest)) {
254 LABEL;
255 printf("%sMD5 expected %s found %s\n", tab, s->md5digest,
256 new_digest);
257 tab = "\t";
258 }
259 }
260#endif /* MD5 */
261#ifdef SHA1
262 if (s->flags & F_SHA1) {
263 char *new_digest, buf[41];
264
265 new_digest = SHA1_File(p->fts_accpath, buf);
266 if (!new_digest) {
267 LABEL;
268 printf("%sSHA-1: %s: %s\n", tab, p->fts_accpath,
269 strerror(errno));
270 tab = "\t";
271 } else if (strcmp(new_digest, s->sha1digest)) {
272 LABEL;
273 printf("%sSHA-1 expected %s found %s\n",
274 tab, s->sha1digest, new_digest);
275 tab = "\t";
276 }
277 }
278#endif /* SHA1 */
279#ifdef RMD160
280 if (s->flags & F_RMD160) {
281 char *new_digest, buf[41];
282
283 new_digest = RIPEMD160_File(p->fts_accpath, buf);
284 if (!new_digest) {
285 LABEL;
286 printf("%sRIPEMD160: %s: %s\n", tab,
287 p->fts_accpath, strerror(errno));
288 tab = "\t";
289 } else if (strcmp(new_digest, s->rmd160digest)) {
290 LABEL;
291 printf("%sRIPEMD160 expected %s found %s\n",
292 tab, s->rmd160digest, new_digest);
293 tab = "\t";
294 }
295 }
296#endif /* RMD160 */
297
298 if (s->flags & F_SLINK &&
299 strcmp(cp = rlink(p->fts_accpath), s->slink)) {
300 LABEL;
301 (void)printf("%slink_ref expected %s found %s\n",
302 tab, s->slink, cp);
303 }
304 return (label);
305}
306
307const char *
308inotype(u_int type)
309{
310 switch(type & S_IFMT) {
311 case S_IFBLK:
312 return ("block");
313 case S_IFCHR:
314 return ("char");
315 case S_IFDIR:
316 return ("dir");
317 case S_IFIFO:
318 return ("fifo");
319 case S_IFREG:
320 return ("file");
321 case S_IFLNK:
322 return ("link");
323 case S_IFSOCK:
324 return ("socket");
325 default:
326 return ("unknown");
327 }
328 /* NOTREACHED */
329}
330
331const char *
332ftype(u_int type)
333{
334 switch(type) {
335 case F_BLOCK:
336 return ("block");
337 case F_CHAR:
338 return ("char");
339 case F_DIR:
340 return ("dir");
341 case F_FIFO:
342 return ("fifo");
343 case F_FILE:
344 return ("file");
345 case F_LINK:
346 return ("link");
347 case F_SOCK:
348 return ("socket");
349 default:
350 return ("unknown");
351 }
352 /* NOTREACHED */
353}
354
355char *
356rlink(char *name)
357{
358 static char lbuf[MAXPATHLEN * 4];
359 int len;
360 char tbuf[MAXPATHLEN];
361
362 if ((len = readlink(name, tbuf, sizeof(tbuf) - 1)) == -1)
363 err(1, "line %d: %s", lineno, name);
364 tbuf[len] = '\0';
365 strvis(lbuf, tbuf, VIS_WHITE | VIS_OCTAL);
366 return (lbuf);
367}