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