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