]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/mtree/compare.c
MFC r256687:
[FreeBSD/stable/10.git] / contrib / mtree / compare.c
1 /*      $NetBSD: compare.c,v 1.56 2013/09/09 23:27:43 christos Exp $    */
2
3 /*-
4  * Copyright (c) 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)compare.c   8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: compare.c,v 1.56 2013/09/09 23:27:43 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #include <unistd.h>
56
57 #ifndef NO_MD5
58 #include <md5.h>
59 #endif
60 #ifndef NO_RMD160
61 #include <rmd160.h>
62 #endif
63 #ifndef NO_SHA1
64 #include <sha1.h>
65 #endif
66 #ifndef NO_SHA2
67 #include <sha2.h>
68 #endif
69
70 #include "extern.h"
71
72 #define INDENTNAMELEN   8
73 #define MARK                                                            \
74 do {                                                                    \
75         len = printf("%s: ", RP(p));                                    \
76         if (len > INDENTNAMELEN) {                                      \
77                 tab = "\t";                                             \
78                 printf("\n");                                           \
79         } else {                                                        \
80                 tab = "";                                               \
81                 printf("%*s", INDENTNAMELEN - (int)len, "");            \
82         }                                                               \
83 } while (0)
84 #define LABEL if (!label++) MARK
85
86 #if HAVE_STRUCT_STAT_ST_FLAGS
87
88
89 #define CHANGEFLAGS                                                     \
90         if (flags != p->fts_statp->st_flags) {                          \
91                 char *sf;                                               \
92                 if (!label) {                                           \
93                         MARK;                                           \
94                         sf = flags_to_string(p->fts_statp->st_flags, "none"); \
95                         printf("%sflags (\"%s\"", tab, sf);             \
96                         free(sf);                                       \
97                 }                                                       \
98                 if (lchflags(p->fts_accpath, flags)) {                  \
99                         label++;                                        \
100                         printf(", not modified: %s)\n",                 \
101                             strerror(errno));                           \
102                 } else {                                                \
103                         sf = flags_to_string(flags, "none");            \
104                         printf(", modified to \"%s\")\n", sf);          \
105                         free(sf);                                       \
106                 }                                                       \
107         }
108
109 /* SETFLAGS:
110  * given pflags, additionally set those flags specified in s->st_flags and
111  * selected by mask (the other flags are left unchanged).
112  */
113 #define SETFLAGS(pflags, mask)                                          \
114 do {                                                                    \
115         flags = (s->st_flags & (mask)) | (pflags);                      \
116         CHANGEFLAGS;                                                    \
117 } while (0)
118
119 /* CLEARFLAGS:
120  * given pflags, reset the flags specified in s->st_flags and selected by mask
121  * (the other flags are left unchanged).
122  */
123 #define CLEARFLAGS(pflags, mask)                                        \
124 do {                                                                    \
125         flags = (~(s->st_flags & (mask)) & CH_MASK) & (pflags);         \
126         CHANGEFLAGS;                                                    \
127 } while (0)
128 #endif  /* HAVE_STRUCT_STAT_ST_FLAGS */
129
130 int
131 compare(NODE *s, FTSENT *p)
132 {
133         u_int32_t len, val, flags;
134         int fd, label;
135         const char *cp, *tab;
136 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
137         char *digestbuf;
138 #endif
139
140         tab = NULL;
141         label = 0;
142         switch(s->type) {
143         case F_BLOCK:
144                 if (!S_ISBLK(p->fts_statp->st_mode))
145                         goto typeerr;
146                 break;
147         case F_CHAR:
148                 if (!S_ISCHR(p->fts_statp->st_mode))
149                         goto typeerr;
150                 break;
151         case F_DIR:
152                 if (!S_ISDIR(p->fts_statp->st_mode))
153                         goto typeerr;
154                 break;
155         case F_FIFO:
156                 if (!S_ISFIFO(p->fts_statp->st_mode))
157                         goto typeerr;
158                 break;
159         case F_FILE:
160                 if (!S_ISREG(p->fts_statp->st_mode))
161                         goto typeerr;
162                 break;
163         case F_LINK:
164                 if (!S_ISLNK(p->fts_statp->st_mode))
165                         goto typeerr;
166                 break;
167 #ifdef S_ISSOCK
168         case F_SOCK:
169                 if (!S_ISSOCK(p->fts_statp->st_mode))
170                         goto typeerr;
171                 break;
172 #endif
173 typeerr:                LABEL;
174                 printf("\ttype (%s, %s)\n",
175                     nodetype(s->type), inotype(p->fts_statp->st_mode));
176                 return (label);
177         }
178         if (mtree_Wflag)
179                 goto afterpermwhack;
180 #if HAVE_STRUCT_STAT_ST_FLAGS
181         if (iflag && !uflag) {
182                 if (s->flags & F_FLAGS)
183                     SETFLAGS(p->fts_statp->st_flags, SP_FLGS);
184                 return (label);
185         }
186         if (mflag && !uflag) {
187                 if (s->flags & F_FLAGS)
188                     CLEARFLAGS(p->fts_statp->st_flags, SP_FLGS);
189                 return (label);
190         }
191 #endif
192         if (s->flags & F_DEV &&
193             (s->type == F_BLOCK || s->type == F_CHAR) &&
194             s->st_rdev != p->fts_statp->st_rdev) {
195                 LABEL;
196                 printf("%sdevice (%#jx, %#jx",
197                     tab, (uintmax_t)s->st_rdev,
198                     (uintmax_t)p->fts_statp->st_rdev);
199                 if (uflag) {
200                         if ((unlink(p->fts_accpath) == -1) ||
201                             (mknod(p->fts_accpath,
202                               s->st_mode | nodetoino(s->type),
203                               s->st_rdev) == -1) ||
204                             (lchown(p->fts_accpath, p->fts_statp->st_uid,
205                               p->fts_statp->st_gid) == -1) )
206                                 printf(", not modified: %s)\n",
207                                     strerror(errno));
208                          else
209                                 printf(", modified)\n");
210                 } else
211                         printf(")\n");
212                 tab = "\t";
213         }
214         /* Set the uid/gid first, then set the mode. */
215         if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
216                 LABEL;
217                 printf("%suser (%lu, %lu",
218                     tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
219                 if (uflag) {
220                         if (lchown(p->fts_accpath, s->st_uid, -1))
221                                 printf(", not modified: %s)\n",
222                                     strerror(errno));
223                         else
224                                 printf(", modified)\n");
225                 } else
226                         printf(")\n");
227                 tab = "\t";
228         }
229         if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
230                 LABEL;
231                 printf("%sgid (%lu, %lu",
232                     tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
233                 if (uflag) {
234                         if (lchown(p->fts_accpath, -1, s->st_gid))
235                                 printf(", not modified: %s)\n",
236                                     strerror(errno));
237                         else
238                                 printf(", modified)\n");
239                 }
240                 else
241                         printf(")\n");
242                 tab = "\t";
243         }
244         if (s->flags & F_MODE &&
245             s->st_mode != (p->fts_statp->st_mode & MBITS)) {
246                 if (lflag) {
247                         mode_t tmode, mode;
248
249                         tmode = s->st_mode;
250                         mode = p->fts_statp->st_mode & MBITS;
251                         /*
252                          * if none of the suid/sgid/etc bits are set,
253                          * then if the mode is a subset of the target,
254                          * skip.
255                          */
256                         if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
257                             (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
258                                 if ((mode | tmode) == tmode)
259                                         goto skip;
260                 }
261
262                 LABEL;
263                 printf("%spermissions (%#lo, %#lo",
264                     tab, (u_long)s->st_mode,
265                     (u_long)p->fts_statp->st_mode & MBITS);
266                 if (uflag) {
267                         if (lchmod(p->fts_accpath, s->st_mode))
268                                 printf(", not modified: %s)\n",
269                                     strerror(errno));
270                         else
271                                 printf(", modified)\n");
272                 }
273                 else
274                         printf(")\n");
275                 tab = "\t";
276         skip:   ;
277         }
278         if (s->flags & F_NLINK && s->type != F_DIR &&
279             s->st_nlink != p->fts_statp->st_nlink) {
280                 LABEL;
281                 printf("%slink count (%lu, %lu)\n",
282                     tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
283                 tab = "\t";
284         }
285         if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
286                 LABEL;
287                 printf("%ssize (%ju, %ju)\n",
288                     tab, (uintmax_t)s->st_size,
289                     (uintmax_t)p->fts_statp->st_size);
290                 tab = "\t";
291         }
292         /*
293          * XXX
294          * Since utimes(2) only takes a timeval, there's no point in
295          * comparing the low bits of the timespec nanosecond field.  This
296          * will only result in mismatches that we can never fix.
297          *
298          * Doesn't display microsecond differences.
299          */
300         if (s->flags & F_TIME) {
301                 struct timeval tv[2];
302                 struct stat *ps = p->fts_statp;
303                 time_t smtime = s->st_mtimespec.tv_sec;
304
305 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
306                 time_t pmtime = ps->st_mtimespec.tv_sec;
307
308                 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
309                 TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
310 #else
311                 time_t pmtime = (time_t)ps->st_mtime;
312
313                 tv[0].tv_sec = smtime;
314                 tv[0].tv_usec = 0;
315                 tv[1].tv_sec = pmtime;
316                 tv[1].tv_usec = 0;
317 #endif
318
319                 if (tv[0].tv_sec != tv[1].tv_sec ||
320                     tv[0].tv_usec != tv[1].tv_usec) {
321                         LABEL;
322                         printf("%smodification time (%.24s, ",
323                             tab, ctime(&smtime));
324                         printf("%.24s", ctime(&pmtime));
325                         if (tflag) {
326                                 tv[1] = tv[0];
327                                 if (utimes(p->fts_accpath, tv))
328                                         printf(", not modified: %s)\n",
329                                             strerror(errno));
330                                 else
331                                         printf(", modified)\n");
332                         } else
333                                 printf(")\n");
334                         tab = "\t";
335                 }
336         }
337 #if HAVE_STRUCT_STAT_ST_FLAGS
338         /*
339          * XXX
340          * since lchflags(2) will reset file times, the utimes() above
341          * may have been useless!  oh well, we'd rather have correct
342          * flags, rather than times?
343          */
344         if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
345             || mflag || iflag)) {
346                 if (s->st_flags != p->fts_statp->st_flags) {
347                         char *f_s;
348                         LABEL;
349                         f_s = flags_to_string(s->st_flags, "none");
350                         printf("%sflags (\"%s\" is not ", tab, f_s);
351                         free(f_s);
352                         f_s = flags_to_string(p->fts_statp->st_flags, "none");
353                         printf("\"%s\"", f_s);
354                         free(f_s);
355                 }
356                 if (uflag) {
357                         if (iflag)
358                                 SETFLAGS(0, CH_MASK);
359                         else if (mflag)
360                                 CLEARFLAGS(0, SP_FLGS);
361                         else
362                                 SETFLAGS(0, (~SP_FLGS & CH_MASK));
363                 } else
364                         printf(")\n");
365                 tab = "\t";
366         }
367 #endif  /* HAVE_STRUCT_STAT_ST_FLAGS */
368
369         /*
370          * from this point, no more permission checking or whacking
371          * occurs, only checking of stuff like checksums and symlinks.
372          */
373  afterpermwhack:
374         if (s->flags & F_CKSUM) {
375                 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
376                         LABEL;
377                         printf("%scksum: %s: %s\n",
378                             tab, p->fts_accpath, strerror(errno));
379                         tab = "\t";
380                 } else if (crc(fd, &val, &len)) {
381                         close(fd);
382                         LABEL;
383                         printf("%scksum: %s: %s\n",
384                             tab, p->fts_accpath, strerror(errno));
385                         tab = "\t";
386                 } else {
387                         close(fd);
388                         if (s->cksum != val) {
389                                 LABEL;
390                                 printf("%scksum (%lu, %lu)\n",
391                                     tab, s->cksum, (unsigned long)val);
392                         }
393                         tab = "\t";
394                 }
395         }
396 #ifndef NO_MD5
397         if (s->flags & F_MD5) {
398                 if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL) {
399                         LABEL;
400                         printf("%s%s: %s: %s\n",
401                             tab, MD5KEY, p->fts_accpath, strerror(errno));
402                         tab = "\t";
403                 } else {
404                         if (strcmp(s->md5digest, digestbuf)) {
405                                 LABEL;
406                                 printf("%s%s (0x%s, 0x%s)\n",
407                                     tab, MD5KEY, s->md5digest, digestbuf);
408                         }
409                         tab = "\t";
410                         free(digestbuf);
411                 }
412         }
413 #endif  /* ! NO_MD5 */
414 #ifndef NO_RMD160
415         if (s->flags & F_RMD160) {
416                 if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL) {
417                         LABEL;
418                         printf("%s%s: %s: %s\n",
419                             tab, RMD160KEY, p->fts_accpath, strerror(errno));
420                         tab = "\t";
421                 } else {
422                         if (strcmp(s->rmd160digest, digestbuf)) {
423                                 LABEL;
424                                 printf("%s%s (0x%s, 0x%s)\n",
425                                     tab, RMD160KEY, s->rmd160digest, digestbuf);
426                         }
427                         tab = "\t";
428                         free(digestbuf);
429                 }
430         }
431 #endif  /* ! NO_RMD160 */
432 #ifndef NO_SHA1
433         if (s->flags & F_SHA1) {
434                 if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL) {
435                         LABEL;
436                         printf("%s%s: %s: %s\n",
437                             tab, SHA1KEY, p->fts_accpath, strerror(errno));
438                         tab = "\t";
439                 } else {
440                         if (strcmp(s->sha1digest, digestbuf)) {
441                                 LABEL;
442                                 printf("%s%s (0x%s, 0x%s)\n",
443                                     tab, SHA1KEY, s->sha1digest, digestbuf);
444                         }
445                         tab = "\t";
446                         free(digestbuf);
447                 }
448         }
449 #endif  /* ! NO_SHA1 */
450 #ifndef NO_SHA2
451         if (s->flags & F_SHA256) {
452                 if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL) {
453                         LABEL;
454                         printf("%s%s: %s: %s\n",
455                             tab, SHA256KEY, p->fts_accpath, strerror(errno));
456                         tab = "\t";
457                 } else {
458                         if (strcmp(s->sha256digest, digestbuf)) {
459                                 LABEL;
460                                 printf("%s%s (0x%s, 0x%s)\n",
461                                     tab, SHA256KEY, s->sha256digest, digestbuf);
462                         }
463                         tab = "\t";
464                         free(digestbuf);
465                 }
466         }
467 #ifdef SHA384_BLOCK_LENGTH
468         if (s->flags & F_SHA384) {
469                 if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL) {
470                         LABEL;
471                         printf("%s%s: %s: %s\n",
472                             tab, SHA384KEY, p->fts_accpath, strerror(errno));
473                         tab = "\t";
474                 } else {
475                         if (strcmp(s->sha384digest, digestbuf)) {
476                                 LABEL;
477                                 printf("%s%s (0x%s, 0x%s)\n",
478                                     tab, SHA384KEY, s->sha384digest, digestbuf);
479                         }
480                         tab = "\t";
481                         free(digestbuf);
482                 }
483         }
484 #endif
485         if (s->flags & F_SHA512) {
486                 if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL) {
487                         LABEL;
488                         printf("%s%s: %s: %s\n",
489                             tab, SHA512KEY, p->fts_accpath, strerror(errno));
490                         tab = "\t";
491                 } else {
492                         if (strcmp(s->sha512digest, digestbuf)) {
493                                 LABEL;
494                                 printf("%s%s (0x%s, 0x%s)\n",
495                                     tab, SHA512KEY, s->sha512digest, digestbuf);
496                         }
497                         tab = "\t";
498                         free(digestbuf);
499                 }
500         }
501 #endif  /* ! NO_SHA2 */
502         if (s->flags & F_SLINK &&
503             strcmp(cp = rlink(p->fts_accpath), s->slink)) {
504                 LABEL;
505                 printf("%slink ref (%s, %s", tab, cp, s->slink);
506                 if (uflag) {
507                         if ((unlink(p->fts_accpath) == -1) ||
508                             (symlink(s->slink, p->fts_accpath) == -1) )
509                                 printf(", not modified: %s)\n",
510                                     strerror(errno));
511                         else
512                                 printf(", modified)\n");
513                 } else
514                         printf(")\n");
515         }
516         return (label);
517 }
518
519 const char *
520 rlink(const char *name)
521 {
522         static char lbuf[MAXPATHLEN];
523         int len;
524
525         if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
526                 mtree_err("%s: %s", name, strerror(errno));
527         lbuf[len] = '\0';
528         return (lbuf);
529 }