1c6ec7d31SBrooks Davis /* $NetBSD: specspec.c,v 1.2 2012/10/05 01:27:29 christos Exp $ */
2c6ec7d31SBrooks Davis
3c6ec7d31SBrooks Davis /*-
4c6ec7d31SBrooks Davis * Copyright (c) 2003 Poul-Henning Kamp
5c6ec7d31SBrooks Davis * All rights reserved.
6c6ec7d31SBrooks Davis *
7c6ec7d31SBrooks Davis * Redistribution and use in source and binary forms, with or without
8c6ec7d31SBrooks Davis * modification, are permitted provided that the following conditions
9c6ec7d31SBrooks Davis * are met:
10c6ec7d31SBrooks Davis * 1. Redistributions of source code must retain the above copyright
11c6ec7d31SBrooks Davis * notice, this list of conditions and the following disclaimer.
12c6ec7d31SBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright
13c6ec7d31SBrooks Davis * notice, this list of conditions and the following disclaimer in the
14c6ec7d31SBrooks Davis * documentation and/or other materials provided with the distribution.
15c6ec7d31SBrooks Davis *
16c6ec7d31SBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c6ec7d31SBrooks Davis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c6ec7d31SBrooks Davis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c6ec7d31SBrooks Davis * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c6ec7d31SBrooks Davis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c6ec7d31SBrooks Davis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c6ec7d31SBrooks Davis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c6ec7d31SBrooks Davis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c6ec7d31SBrooks Davis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c6ec7d31SBrooks Davis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c6ec7d31SBrooks Davis * SUCH DAMAGE.
27c6ec7d31SBrooks Davis */
28c6ec7d31SBrooks Davis
29c6ec7d31SBrooks Davis #if HAVE_NBTOOL_CONFIG_H
30c6ec7d31SBrooks Davis #include "nbtool_config.h"
31c6ec7d31SBrooks Davis #endif
32c6ec7d31SBrooks Davis
33c6ec7d31SBrooks Davis #include <sys/cdefs.h>
34c6ec7d31SBrooks Davis __RCSID("$NetBSD: specspec.c,v 1.2 2012/10/05 01:27:29 christos Exp $");
35c6ec7d31SBrooks Davis
36c6ec7d31SBrooks Davis #include <err.h>
37c6ec7d31SBrooks Davis #include <grp.h>
38c6ec7d31SBrooks Davis #include <pwd.h>
39c6ec7d31SBrooks Davis #include <time.h>
40c6ec7d31SBrooks Davis #include <stdio.h>
41c6ec7d31SBrooks Davis #include <stdint.h>
42c6ec7d31SBrooks Davis #include <stdlib.h>
43c6ec7d31SBrooks Davis #include <string.h>
44c6ec7d31SBrooks Davis #include <unistd.h>
45c6ec7d31SBrooks Davis #include "mtree.h"
46c6ec7d31SBrooks Davis #include "extern.h"
47c6ec7d31SBrooks Davis
48c6ec7d31SBrooks Davis #define FF(a, b, c, d) \
49c6ec7d31SBrooks Davis (((a)->flags & (c)) && ((b)->flags & (c)) && ((a)->d) != ((b)->d))
50c6ec7d31SBrooks Davis #define FS(a, b, c, d) \
51c6ec7d31SBrooks Davis (((a)->flags & (c)) && ((b)->flags & (c)) && strcmp((a)->d,(b)->d))
52c6ec7d31SBrooks Davis #define FM(a, b, c, d) \
53c6ec7d31SBrooks Davis (((a)->flags & (c)) && ((b)->flags & (c)) && memcmp(&(a)->d,&(b)->d, sizeof (a)->d))
54c6ec7d31SBrooks Davis
55c6ec7d31SBrooks Davis static void
shownode(NODE * n,int f,char const * path)56c6ec7d31SBrooks Davis shownode(NODE *n, int f, char const *path)
57c6ec7d31SBrooks Davis {
58c6ec7d31SBrooks Davis struct group *gr;
59c6ec7d31SBrooks Davis struct passwd *pw;
60c6ec7d31SBrooks Davis
61c6ec7d31SBrooks Davis printf("%s%s %s", path, n->name, inotype(nodetoino(n->type)));
62c6ec7d31SBrooks Davis if (f & F_CKSUM)
63c6ec7d31SBrooks Davis printf(" cksum=%lu", n->cksum);
64c6ec7d31SBrooks Davis if (f & F_GID)
65c6ec7d31SBrooks Davis printf(" gid=%d", n->st_gid);
66c6ec7d31SBrooks Davis if (f & F_GNAME) {
67c6ec7d31SBrooks Davis gr = getgrgid(n->st_gid);
68c6ec7d31SBrooks Davis if (gr == NULL)
69c6ec7d31SBrooks Davis printf(" gid=%d", n->st_gid);
70c6ec7d31SBrooks Davis else
71c6ec7d31SBrooks Davis printf(" gname=%s", gr->gr_name);
72c6ec7d31SBrooks Davis }
73c6ec7d31SBrooks Davis if (f & F_MODE)
74c6ec7d31SBrooks Davis printf(" mode=%o", n->st_mode);
75c6ec7d31SBrooks Davis if (f & F_NLINK)
761c324569SKonstantin Belousov printf(" nlink=%ju", (uintmax_t)n->st_nlink);
77c6ec7d31SBrooks Davis if (f & F_SIZE)
78c6ec7d31SBrooks Davis printf(" size=%jd", (intmax_t)n->st_size);
79c6ec7d31SBrooks Davis if (f & F_UID)
80c6ec7d31SBrooks Davis printf(" uid=%d", n->st_uid);
81c6ec7d31SBrooks Davis if (f & F_UNAME) {
82c6ec7d31SBrooks Davis pw = getpwuid(n->st_uid);
83c6ec7d31SBrooks Davis if (pw == NULL)
84c6ec7d31SBrooks Davis printf(" uid=%d", n->st_uid);
85c6ec7d31SBrooks Davis else
86c6ec7d31SBrooks Davis printf(" uname=%s", pw->pw_name);
87c6ec7d31SBrooks Davis }
88c6ec7d31SBrooks Davis if (f & F_MD5)
89c6ec7d31SBrooks Davis printf(" %s=%s", MD5KEY, n->md5digest);
90c6ec7d31SBrooks Davis if (f & F_SHA1)
91c6ec7d31SBrooks Davis printf(" %s=%s", SHA1KEY, n->sha1digest);
92c6ec7d31SBrooks Davis if (f & F_RMD160)
93c6ec7d31SBrooks Davis printf(" %s=%s", RMD160KEY, n->rmd160digest);
94c6ec7d31SBrooks Davis if (f & F_SHA256)
95c6ec7d31SBrooks Davis printf(" %s=%s", SHA256KEY, n->sha256digest);
96c6ec7d31SBrooks Davis if (f & F_SHA384)
97c6ec7d31SBrooks Davis printf(" %s=%s", SHA384KEY, n->sha384digest);
98c6ec7d31SBrooks Davis if (f & F_SHA512)
99c6ec7d31SBrooks Davis printf(" %s=%s", SHA512KEY, n->sha512digest);
100c6ec7d31SBrooks Davis if (f & F_FLAGS)
101c6ec7d31SBrooks Davis printf(" flags=%s", flags_to_string(n->st_flags, "none"));
102c6ec7d31SBrooks Davis printf("\n");
103c6ec7d31SBrooks Davis }
104c6ec7d31SBrooks Davis
105c6ec7d31SBrooks Davis static int
mismatch(NODE * n1,NODE * n2,int differ,char const * path)106c6ec7d31SBrooks Davis mismatch(NODE *n1, NODE *n2, int differ, char const *path)
107c6ec7d31SBrooks Davis {
108c6ec7d31SBrooks Davis
109c6ec7d31SBrooks Davis if (n2 == NULL) {
110c6ec7d31SBrooks Davis shownode(n1, differ, path);
111c6ec7d31SBrooks Davis return (1);
112c6ec7d31SBrooks Davis }
113c6ec7d31SBrooks Davis if (n1 == NULL) {
114c6ec7d31SBrooks Davis printf("\t");
115c6ec7d31SBrooks Davis shownode(n2, differ, path);
116c6ec7d31SBrooks Davis return (1);
117c6ec7d31SBrooks Davis }
118c6ec7d31SBrooks Davis if (!(differ & keys))
119c6ec7d31SBrooks Davis return(0);
120c6ec7d31SBrooks Davis printf("\t\t");
121c6ec7d31SBrooks Davis shownode(n1, differ, path);
122c6ec7d31SBrooks Davis printf("\t\t");
123c6ec7d31SBrooks Davis shownode(n2, differ, path);
124c6ec7d31SBrooks Davis return (1);
125c6ec7d31SBrooks Davis }
126c6ec7d31SBrooks Davis
127c6ec7d31SBrooks Davis static int
compare_nodes(NODE * n1,NODE * n2,char const * path)128c6ec7d31SBrooks Davis compare_nodes(NODE *n1, NODE *n2, char const *path)
129c6ec7d31SBrooks Davis {
130c6ec7d31SBrooks Davis int differs;
131c6ec7d31SBrooks Davis
132c6ec7d31SBrooks Davis if (n1 != NULL && n1->type == F_LINK)
133c6ec7d31SBrooks Davis n1->flags &= ~F_MODE;
134c6ec7d31SBrooks Davis if (n2 != NULL && n2->type == F_LINK)
135c6ec7d31SBrooks Davis n2->flags &= ~F_MODE;
136c6ec7d31SBrooks Davis differs = 0;
137c6ec7d31SBrooks Davis if (n1 == NULL && n2 != NULL) {
138c6ec7d31SBrooks Davis differs = n2->flags;
139c6ec7d31SBrooks Davis mismatch(n1, n2, differs, path);
140c6ec7d31SBrooks Davis return (1);
141c6ec7d31SBrooks Davis }
142c6ec7d31SBrooks Davis if (n1 != NULL && n2 == NULL) {
143c6ec7d31SBrooks Davis differs = n1->flags;
144c6ec7d31SBrooks Davis mismatch(n1, n2, differs, path);
145c6ec7d31SBrooks Davis return (1);
146c6ec7d31SBrooks Davis }
147c6ec7d31SBrooks Davis if (n1->type != n2->type) {
148*4f4b548bSBryan Drewery differs = F_TYPE;
149c6ec7d31SBrooks Davis mismatch(n1, n2, differs, path);
150c6ec7d31SBrooks Davis return (1);
151c6ec7d31SBrooks Davis }
152c6ec7d31SBrooks Davis if (FF(n1, n2, F_CKSUM, cksum))
153c6ec7d31SBrooks Davis differs |= F_CKSUM;
154c6ec7d31SBrooks Davis if (FF(n1, n2, F_GID, st_gid))
155c6ec7d31SBrooks Davis differs |= F_GID;
156c6ec7d31SBrooks Davis if (FF(n1, n2, F_GNAME, st_gid))
157c6ec7d31SBrooks Davis differs |= F_GNAME;
158c6ec7d31SBrooks Davis if (FF(n1, n2, F_MODE, st_mode))
159c6ec7d31SBrooks Davis differs |= F_MODE;
160c6ec7d31SBrooks Davis if (FF(n1, n2, F_NLINK, st_nlink))
161c6ec7d31SBrooks Davis differs |= F_NLINK;
162c6ec7d31SBrooks Davis if (FF(n1, n2, F_SIZE, st_size))
163c6ec7d31SBrooks Davis differs |= F_SIZE;
164c6ec7d31SBrooks Davis if (FS(n1, n2, F_SLINK, slink))
165c6ec7d31SBrooks Davis differs |= F_SLINK;
166c6ec7d31SBrooks Davis if (FM(n1, n2, F_TIME, st_mtimespec))
167c6ec7d31SBrooks Davis differs |= F_TIME;
168c6ec7d31SBrooks Davis if (FF(n1, n2, F_UID, st_uid))
169c6ec7d31SBrooks Davis differs |= F_UID;
170c6ec7d31SBrooks Davis if (FF(n1, n2, F_UNAME, st_uid))
171c6ec7d31SBrooks Davis differs |= F_UNAME;
172c6ec7d31SBrooks Davis if (FS(n1, n2, F_MD5, md5digest))
173c6ec7d31SBrooks Davis differs |= F_MD5;
174c6ec7d31SBrooks Davis if (FS(n1, n2, F_SHA1, sha1digest))
175c6ec7d31SBrooks Davis differs |= F_SHA1;
176c6ec7d31SBrooks Davis if (FS(n1, n2, F_RMD160, rmd160digest))
177c6ec7d31SBrooks Davis differs |= F_RMD160;
178c6ec7d31SBrooks Davis if (FS(n1, n2, F_SHA256, sha256digest))
179c6ec7d31SBrooks Davis differs |= F_SHA256;
180c6ec7d31SBrooks Davis if (FS(n1, n2, F_SHA384, sha384digest))
181c6ec7d31SBrooks Davis differs |= F_SHA384;
182c6ec7d31SBrooks Davis if (FS(n1, n2, F_SHA512, sha512digest))
183c6ec7d31SBrooks Davis differs |= F_SHA512;
184c6ec7d31SBrooks Davis if (FF(n1, n2, F_FLAGS, st_flags))
185c6ec7d31SBrooks Davis differs |= F_FLAGS;
186c6ec7d31SBrooks Davis if (differs) {
187c6ec7d31SBrooks Davis mismatch(n1, n2, differs, path);
188c6ec7d31SBrooks Davis return (1);
189c6ec7d31SBrooks Davis }
190c6ec7d31SBrooks Davis return (0);
191c6ec7d31SBrooks Davis }
192c6ec7d31SBrooks Davis static int
walk_in_the_forest(NODE * t1,NODE * t2,char const * path)193c6ec7d31SBrooks Davis walk_in_the_forest(NODE *t1, NODE *t2, char const *path)
194c6ec7d31SBrooks Davis {
195c6ec7d31SBrooks Davis int r, i;
196c6ec7d31SBrooks Davis NODE *c1, *c2, *n1, *n2;
197c6ec7d31SBrooks Davis char *np;
198c6ec7d31SBrooks Davis
199c6ec7d31SBrooks Davis r = 0;
200c6ec7d31SBrooks Davis
201c6ec7d31SBrooks Davis if (t1 != NULL)
202c6ec7d31SBrooks Davis c1 = t1->child;
203c6ec7d31SBrooks Davis else
204c6ec7d31SBrooks Davis c1 = NULL;
205c6ec7d31SBrooks Davis if (t2 != NULL)
206c6ec7d31SBrooks Davis c2 = t2->child;
207c6ec7d31SBrooks Davis else
208c6ec7d31SBrooks Davis c2 = NULL;
209c6ec7d31SBrooks Davis while (c1 != NULL || c2 != NULL) {
210c6ec7d31SBrooks Davis n1 = n2 = NULL;
211c6ec7d31SBrooks Davis if (c1 != NULL)
212c6ec7d31SBrooks Davis n1 = c1->next;
213c6ec7d31SBrooks Davis if (c2 != NULL)
214c6ec7d31SBrooks Davis n2 = c2->next;
215c6ec7d31SBrooks Davis if (c1 != NULL && c2 != NULL) {
216c6ec7d31SBrooks Davis if (c1->type != F_DIR && c2->type == F_DIR) {
217c6ec7d31SBrooks Davis n2 = c2;
218c6ec7d31SBrooks Davis c2 = NULL;
219c6ec7d31SBrooks Davis } else if (c1->type == F_DIR && c2->type != F_DIR) {
220c6ec7d31SBrooks Davis n1 = c1;
221c6ec7d31SBrooks Davis c1 = NULL;
222c6ec7d31SBrooks Davis } else {
223c6ec7d31SBrooks Davis i = strcmp(c1->name, c2->name);
224c6ec7d31SBrooks Davis if (i > 0) {
225c6ec7d31SBrooks Davis n1 = c1;
226c6ec7d31SBrooks Davis c1 = NULL;
227c6ec7d31SBrooks Davis } else if (i < 0) {
228c6ec7d31SBrooks Davis n2 = c2;
229c6ec7d31SBrooks Davis c2 = NULL;
230c6ec7d31SBrooks Davis }
231c6ec7d31SBrooks Davis }
232c6ec7d31SBrooks Davis }
233c6ec7d31SBrooks Davis if (c1 == NULL && c2->type == F_DIR) {
234c6ec7d31SBrooks Davis asprintf(&np, "%s%s/", path, c2->name);
235c6ec7d31SBrooks Davis i = walk_in_the_forest(c1, c2, np);
236c6ec7d31SBrooks Davis free(np);
237c6ec7d31SBrooks Davis i += compare_nodes(c1, c2, path);
238c6ec7d31SBrooks Davis } else if (c2 == NULL && c1->type == F_DIR) {
239c6ec7d31SBrooks Davis asprintf(&np, "%s%s/", path, c1->name);
240c6ec7d31SBrooks Davis i = walk_in_the_forest(c1, c2, np);
241c6ec7d31SBrooks Davis free(np);
242c6ec7d31SBrooks Davis i += compare_nodes(c1, c2, path);
243c6ec7d31SBrooks Davis } else if (c1 == NULL || c2 == NULL) {
244c6ec7d31SBrooks Davis i = compare_nodes(c1, c2, path);
245c6ec7d31SBrooks Davis } else if (c1->type == F_DIR && c2->type == F_DIR) {
246c6ec7d31SBrooks Davis asprintf(&np, "%s%s/", path, c1->name);
247c6ec7d31SBrooks Davis i = walk_in_the_forest(c1, c2, np);
248c6ec7d31SBrooks Davis free(np);
249c6ec7d31SBrooks Davis i += compare_nodes(c1, c2, path);
250c6ec7d31SBrooks Davis } else {
251c6ec7d31SBrooks Davis i = compare_nodes(c1, c2, path);
252c6ec7d31SBrooks Davis }
253c6ec7d31SBrooks Davis r += i;
254c6ec7d31SBrooks Davis c1 = n1;
255c6ec7d31SBrooks Davis c2 = n2;
256c6ec7d31SBrooks Davis }
257c6ec7d31SBrooks Davis return (r);
258c6ec7d31SBrooks Davis }
259c6ec7d31SBrooks Davis
260c6ec7d31SBrooks Davis int
mtree_specspec(FILE * fi,FILE * fj)261c6ec7d31SBrooks Davis mtree_specspec(FILE *fi, FILE *fj)
262c6ec7d31SBrooks Davis {
263c6ec7d31SBrooks Davis int rval;
264c6ec7d31SBrooks Davis NODE *root1, *root2;
265c6ec7d31SBrooks Davis
266c6ec7d31SBrooks Davis root1 = spec(fi);
267c6ec7d31SBrooks Davis root2 = spec(fj);
268c6ec7d31SBrooks Davis rval = walk_in_the_forest(root1, root2, "");
269c6ec7d31SBrooks Davis rval += compare_nodes(root1, root2, "");
270c6ec7d31SBrooks Davis if (rval > 0)
271c6ec7d31SBrooks Davis return (MISMATCHEXIT);
272c6ec7d31SBrooks Davis return (0);
273c6ec7d31SBrooks Davis }
274