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