xref: /netbsd/usr.sbin/mtree/create.c (revision c4a72b64)
1 /*	$NetBSD: create.c,v 1.41 2002/11/29 02:07:34 grant 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: create.c,v 1.41 2002/11/29 02:07:34 grant Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 
48 #if !HAVE_CONFIG_H
49 #include <dirent.h>
50 #endif
51 
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <grp.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <stdarg.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61 #include <unistd.h>
62 
63 #ifndef NO_MD5
64 #include <md5.h>
65 #endif
66 #ifndef NO_RMD160
67 #include <rmd160.h>
68 #endif
69 #ifndef NO_SHA1
70 #include <sha1.h>
71 #endif
72 
73 #include "extern.h"
74 
75 #define	INDENTNAMELEN	15
76 #define	MAXLINELEN	80
77 #define VISFLAGS        VIS_CSTYLE
78 
79 static gid_t gid;
80 static uid_t uid;
81 static mode_t mode;
82 static u_long flags;
83 static char codebuf[4*MAXPATHLEN + 1];
84 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
85 
86 static int	dsort(const FTSENT **, const FTSENT **);
87 static void	output(int *, const char *, ...)
88 	__attribute__((__format__(__printf__, 2, 3)));
89 static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
90 static void	statf(FTSENT *);
91 
92 void
93 cwalk(void)
94 {
95 	FTS *t;
96 	FTSENT *p;
97 	time_t clocktime;
98 	char host[MAXHOSTNAMELEN + 1];
99 	char *argv[2];
100 	char  dot[] = ".";
101 	argv[0] = dot;
102 	argv[1] = NULL;
103 
104 	time(&clocktime);
105 	gethostname(host, sizeof(host));
106 	host[sizeof(host) - 1] = '\0';
107 	printf(
108 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
109 	    getlogin(), host, fullpath, ctime(&clocktime));
110 
111 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
112 		mtree_err("fts_open: %s", strerror(errno));
113 	while ((p = fts_read(t)) != NULL) {
114 		if (check_excludes(p->fts_name, p->fts_path)) {
115 			fts_set(t, p, FTS_SKIP);
116 			continue;
117 		}
118 		switch(p->fts_info) {
119 		case FTS_D:
120 			printf("\n# %s\n", p->fts_path);
121 			statd(t, p, &uid, &gid, &mode, &flags);
122 			statf(p);
123 			break;
124 		case FTS_DP:
125 			if (p->fts_level > 0)
126 				printf("# %s\n..\n\n", p->fts_path);
127 			break;
128 		case FTS_DNR:
129 		case FTS_ERR:
130 		case FTS_NS:
131 			mtree_err("%s: %s",
132 			    p->fts_path, strerror(p->fts_errno));
133 			break;
134 		default:
135 			if (!dflag)
136 				statf(p);
137 			break;
138 
139 		}
140 	}
141 	fts_close(t);
142 	if (sflag && keys & F_CKSUM)
143 		mtree_err("%s checksum: %u", fullpath, crc_total);
144 }
145 
146 static void
147 statf(FTSENT *p)
148 {
149 	u_int32_t len, val;
150 	int fd, indent;
151 	const char *name;
152 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1)
153 	char digestbuf[41];	/* large enough for {MD5,RMD160,SHA1}File() */
154 #endif
155 
156 	strsvis(codebuf, p->fts_name, VISFLAGS, extra);
157 	if (S_ISDIR(p->fts_statp->st_mode))
158 		indent = printf("%s", codebuf);
159 	else
160 		indent = printf("    %s", codebuf);
161 
162 	if (indent > INDENTNAMELEN)
163 		indent = MAXLINELEN;
164 	else
165 		indent += printf("%*s", INDENTNAMELEN - indent, "");
166 
167 	if (!S_ISREG(p->fts_statp->st_mode))
168 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
169 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
170 		if (keys & F_UNAME &&
171 		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
172 			output(&indent, "uname=%s", name);
173 		else /* if (keys & F_UID) */
174 			output(&indent, "uid=%u", p->fts_statp->st_uid);
175 	}
176 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
177 		if (keys & F_GNAME &&
178 		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
179 			output(&indent, "gname=%s", name);
180 		else /* if (keys & F_GID) */
181 			output(&indent, "gid=%u", p->fts_statp->st_gid);
182 	}
183 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
184 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
185 	if (keys & F_DEV &&
186 	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
187 		output(&indent, "device=%#x", p->fts_statp->st_rdev);
188 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
189 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
190 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
191 		output(&indent, "size=%lld", (long long)p->fts_statp->st_size);
192 #ifdef BSD4_4
193 	if (keys & F_TIME)
194 		output(&indent, "time=%ld.%ld",
195 		    (long)p->fts_statp->st_mtimespec.tv_sec,
196 		    p->fts_statp->st_mtimespec.tv_nsec);
197 #else
198 		output(&indent, "time=%ld.%ld",
199 		    p->fts_statp->st_mtime, 0);
200 #endif
201 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
202 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
203 		    crc(fd, &val, &len))
204 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
205 		close(fd);
206 		output(&indent, "cksum=%lu", (long)val);
207 	}
208 #ifndef NO_MD5
209 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
210 		if (MD5File(p->fts_accpath, digestbuf) == NULL)
211 			mtree_err("%s: %s", p->fts_accpath, "MD5File");
212 		output(&indent, "md5=%s", digestbuf);
213 	}
214 #endif	/* ! NO_MD5 */
215 #ifndef NO_RMD160
216 	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
217 		if (RMD160File(p->fts_accpath, digestbuf) == NULL)
218 			mtree_err("%s: %s", p->fts_accpath, "RMD160File");
219 		output(&indent, "rmd160=%s", digestbuf);
220 	}
221 #endif	/* ! NO_RMD160 */
222 #ifndef NO_SHA1
223 	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
224 		if (SHA1File(p->fts_accpath, digestbuf) == NULL)
225 			mtree_err("%s: %s", p->fts_accpath, "SHA1File");
226 		output(&indent, "sha1=%s", digestbuf);
227 	}
228 #endif	/* ! NO_SHA1 */
229 	if (keys & F_SLINK &&
230 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
231 		output(&indent, "link=%s", rlink(p->fts_accpath));
232 #if HAVE_STRUCT_STAT_ST_FLAGS
233 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
234 		output(&indent, "flags=%s",
235 		    flags_to_string(p->fts_statp->st_flags, "none"));
236 #endif
237 	putchar('\n');
238 }
239 
240 /* XXX
241  * FLAGS2INDEX will fail once the user and system settable bits need more
242  * than one byte, respectively.
243  */
244 #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
245 
246 #define	MTREE_MAXGID	5000
247 #define	MTREE_MAXUID	5000
248 #define	MTREE_MAXMODE	(MBITS + 1)
249 #if HAVE_STRUCT_STAT_ST_FLAGS
250 #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
251 #else
252 #define MTREE_MAXFLAGS	1
253 #endif
254 #define	MTREE_MAXS 16
255 
256 static int
257 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
258       u_long *pflags)
259 {
260 	FTSENT *p;
261 	gid_t sgid;
262 	uid_t suid;
263 	mode_t smode;
264 	u_long sflags = 0;
265 	const char *name;
266 	gid_t savegid;
267 	uid_t saveuid;
268 	mode_t savemode;
269 	u_long saveflags;
270 	u_short maxgid, maxuid, maxmode, maxflags;
271 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
272 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
273 	static int first = 1;
274 
275 	savegid = *pgid;
276 	saveuid = *puid;
277 	savemode = *pmode;
278 	saveflags = *pflags;
279 	if ((p = fts_children(t, 0)) == NULL) {
280 		if (errno)
281 			mtree_err("%s: %s", RP(parent), strerror(errno));
282 		return (1);
283 	}
284 
285 	memset(g, 0, sizeof(g));
286 	memset(u, 0, sizeof(u));
287 	memset(m, 0, sizeof(m));
288 	memset(f, 0, sizeof(f));
289 
290 	maxuid = maxgid = maxmode = maxflags = 0;
291 	for (; p; p = p->fts_link) {
292 		smode = p->fts_statp->st_mode & MBITS;
293 		if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
294 			savemode = smode;
295 			maxmode = m[smode];
296 		}
297 		sgid = p->fts_statp->st_gid;
298 		if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
299 			savegid = sgid;
300 			maxgid = g[sgid];
301 		}
302 		suid = p->fts_statp->st_uid;
303 		if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
304 			saveuid = suid;
305 			maxuid = u[suid];
306 		}
307 
308 #if HAVE_STRUCT_STAT_ST_FLAGS
309 		sflags = FLAGS2INDEX(p->fts_statp->st_flags);
310 		if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
311 			saveflags = p->fts_statp->st_flags;
312 			maxflags = f[sflags];
313 		}
314 #endif
315 	}
316 	/*
317 	 * If the /set record is the same as the last one we do not need to
318 	 * output a new one.  So first we check to see if anything changed.
319 	 * Note that we always output a /set record for the first directory.
320 	 */
321 	if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
322 	    ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
323 	    ((keys & F_MODE) && (*pmode != savemode)) ||
324 	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
325 	    first) {
326 		first = 0;
327 		printf("/set type=file");
328 		if (keys & (F_UID | F_UNAME)) {
329 			if (keys & F_UNAME &&
330 			    (name = user_from_uid(saveuid, 1)) != NULL)
331 				printf(" uname=%s", name);
332 			else /* if (keys & F_UID) */
333 				printf(" uid=%lu", (u_long)saveuid);
334 		}
335 		if (keys & (F_GID | F_GNAME)) {
336 			if (keys & F_GNAME &&
337 			    (name = group_from_gid(savegid, 1)) != NULL)
338 				printf(" gname=%s", name);
339 			else /* if (keys & F_UID) */
340 				printf(" gid=%lu", (u_long)savegid);
341 		}
342 		if (keys & F_MODE)
343 			printf(" mode=%#lo", (u_long)savemode);
344 		if (keys & F_NLINK)
345 			printf(" nlink=1");
346 		if (keys & F_FLAGS)
347 			printf(" flags=%s",
348 			    flags_to_string(saveflags, "none"));
349 		printf("\n");
350 		*puid = saveuid;
351 		*pgid = savegid;
352 		*pmode = savemode;
353 		*pflags = saveflags;
354 	}
355 	return (0);
356 }
357 
358 static int
359 dsort(const FTSENT **a, const FTSENT **b)
360 {
361 
362 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
363 		if (!S_ISDIR((*b)->fts_statp->st_mode))
364 			return (1);
365 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
366 		return (-1);
367 	return (strcmp((*a)->fts_name, (*b)->fts_name));
368 }
369 
370 void
371 output(int *offset, const char *fmt, ...)
372 {
373 	va_list ap;
374 	char buf[1024];
375 
376 	va_start(ap, fmt);
377 	vsnprintf(buf, sizeof(buf), fmt, ap);
378 	va_end(ap);
379 
380 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
381 		printf(" \\\n%*s", INDENTNAMELEN, "");
382 		*offset = INDENTNAMELEN;
383 	}
384 	*offset += printf(" %s", buf) + 1;
385 }
386