xref: /netbsd-src/usr.sbin/mtree/create.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: create.c,v 1.55 2007/12/05 16:55:29 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[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: create.c,v 1.55 2007/12/05 16:55:29 christos Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 
48 #if ! HAVE_NBTOOL_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 #ifndef NO_SHA2
73 #include <sha2.h>
74 #endif
75 
76 #include "extern.h"
77 
78 #define	INDENTNAMELEN	15
79 #define	MAXLINELEN	80
80 
81 static gid_t gid;
82 static uid_t uid;
83 static mode_t mode;
84 static u_long flags;
85 
86 static int	dcmp(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 	const char *user;
100 	char *argv[2];
101 	char  dot[] = ".";
102 
103 	argv[0] = dot;
104 	argv[1] = NULL;
105 
106 	time(&clocktime);
107 	gethostname(host, sizeof(host));
108 	host[sizeof(host) - 1] = '\0';
109 	if ((user = getlogin()) == NULL) {
110 		struct passwd *pw;
111 		user = (pw = getpwuid(getuid())) == NULL ? pw->pw_name :
112 		    "<unknown>";
113 	}
114 
115 	printf(
116 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
117 	    user, host, fullpath, ctime(&clocktime));
118 
119 	if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
120 		mtree_err("fts_open: %s", strerror(errno));
121 	while ((p = fts_read(t)) != NULL) {
122 		if (check_excludes(p->fts_name, p->fts_path)) {
123 			fts_set(t, p, FTS_SKIP);
124 			continue;
125 		}
126 		switch(p->fts_info) {
127 		case FTS_D:
128 			printf("\n# %s\n", p->fts_path);
129 			statd(t, p, &uid, &gid, &mode, &flags);
130 			statf(p);
131 			break;
132 		case FTS_DP:
133 			if (p->fts_level > 0)
134 				printf("# %s\n..\n\n", p->fts_path);
135 			break;
136 		case FTS_DNR:
137 		case FTS_ERR:
138 		case FTS_NS:
139 			mtree_err("%s: %s",
140 			    p->fts_path, strerror(p->fts_errno));
141 			break;
142 		default:
143 			if (!dflag)
144 				statf(p);
145 			break;
146 
147 		}
148 	}
149 	fts_close(t);
150 	if (sflag && keys & F_CKSUM)
151 		mtree_err("%s checksum: %u", fullpath, crc_total);
152 }
153 
154 static void
155 statf(FTSENT *p)
156 {
157 	u_int32_t len, val;
158 	int fd, indent;
159 	const char *name;
160 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
161 	char *digestbuf;
162 #endif
163 
164 	indent = printf("%s%s",
165 	    S_ISDIR(p->fts_statp->st_mode) ? "" : "    ", vispath(p->fts_name));
166 
167 	if (indent > INDENTNAMELEN)
168 		indent = MAXLINELEN;
169 	else
170 		indent += printf("%*s", INDENTNAMELEN - indent, "");
171 
172 	if (!S_ISREG(p->fts_statp->st_mode))
173 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
174 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
175 		if (keys & F_UNAME &&
176 		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
177 			output(&indent, "uname=%s", name);
178 		else /* if (keys & F_UID) */
179 			output(&indent, "uid=%u", p->fts_statp->st_uid);
180 	}
181 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
182 		if (keys & F_GNAME &&
183 		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
184 			output(&indent, "gname=%s", name);
185 		else /* if (keys & F_GID) */
186 			output(&indent, "gid=%u", p->fts_statp->st_gid);
187 	}
188 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
189 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
190 	if (keys & F_DEV &&
191 	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
192 		output(&indent, "device=%#x", p->fts_statp->st_rdev);
193 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
194 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
195 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
196 		output(&indent, "size=%lld", (long long)p->fts_statp->st_size);
197 	if (keys & F_TIME)
198 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
199 		output(&indent, "time=%ld.%ld",
200 		    (long)p->fts_statp->st_mtimespec.tv_sec,
201 		    p->fts_statp->st_mtimespec.tv_nsec);
202 #else
203 		output(&indent, "time=%ld.%ld",
204 		    (long)p->fts_statp->st_mtime, (long)0);
205 #endif
206 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
207 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
208 		    crc(fd, &val, &len))
209 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
210 		close(fd);
211 		output(&indent, "cksum=%lu", (long)val);
212 	}
213 #ifndef NO_MD5
214 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
215 		if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL)
216 			mtree_err("%s: %s", p->fts_accpath, "MD5File");
217 		output(&indent, "md5=%s", digestbuf);
218 		free(digestbuf);
219 	}
220 #endif	/* ! NO_MD5 */
221 #ifndef NO_RMD160
222 	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
223 		if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL)
224 			mtree_err("%s: %s", p->fts_accpath, "RMD160File");
225 		output(&indent, "rmd160=%s", digestbuf);
226 		free(digestbuf);
227 	}
228 #endif	/* ! NO_RMD160 */
229 #ifndef NO_SHA1
230 	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
231 		if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL)
232 			mtree_err("%s: %s", p->fts_accpath, "SHA1File");
233 		output(&indent, "sha1=%s", digestbuf);
234 		free(digestbuf);
235 	}
236 #endif	/* ! NO_SHA1 */
237 #ifndef NO_SHA2
238 	if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
239 		if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL)
240 			mtree_err("%s: %s", p->fts_accpath, "SHA256_File");
241 		output(&indent, "sha256=%s", digestbuf);
242 		free(digestbuf);
243 	}
244 	if (keys & F_SHA384 && S_ISREG(p->fts_statp->st_mode)) {
245 		if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL)
246 			mtree_err("%s: %s", p->fts_accpath, "SHA384_File");
247 		output(&indent, "sha384=%s", digestbuf);
248 		free(digestbuf);
249 	}
250 	if (keys & F_SHA512 && S_ISREG(p->fts_statp->st_mode)) {
251 		if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL)
252 			mtree_err("%s: %s", p->fts_accpath, "SHA512_File");
253 		output(&indent, "sha512=%s", digestbuf);
254 		free(digestbuf);
255 	}
256 #endif	/* ! NO_SHA2 */
257 	if (keys & F_SLINK &&
258 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
259 		output(&indent, "link=%s", vispath(rlink(p->fts_accpath)));
260 #if HAVE_STRUCT_STAT_ST_FLAGS
261 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
262 		output(&indent, "flags=%s",
263 		    flags_to_string(p->fts_statp->st_flags, "none"));
264 #endif
265 	putchar('\n');
266 }
267 
268 /* XXX
269  * FLAGS2INDEX will fail once the user and system settable bits need more
270  * than one byte, respectively.
271  */
272 #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
273 
274 #define	MTREE_MAXGID	5000
275 #define	MTREE_MAXUID	5000
276 #define	MTREE_MAXMODE	(MBITS + 1)
277 #if HAVE_STRUCT_STAT_ST_FLAGS
278 #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
279 #else
280 #define MTREE_MAXFLAGS	1
281 #endif
282 #define	MTREE_MAXS 16
283 
284 static int
285 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
286       u_long *pflags)
287 {
288 	FTSENT *p;
289 	gid_t sgid;
290 	uid_t suid;
291 	mode_t smode;
292 	u_long sflags = 0;
293 	const char *name;
294 	gid_t savegid;
295 	uid_t saveuid;
296 	mode_t savemode;
297 	u_long saveflags;
298 	u_short maxgid, maxuid, maxmode, maxflags;
299 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
300 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
301 	static int first = 1;
302 
303 	savegid = *pgid;
304 	saveuid = *puid;
305 	savemode = *pmode;
306 	saveflags = *pflags;
307 	if ((p = fts_children(t, 0)) == NULL) {
308 		if (errno)
309 			mtree_err("%s: %s", RP(parent), strerror(errno));
310 		return (1);
311 	}
312 
313 	memset(g, 0, sizeof(g));
314 	memset(u, 0, sizeof(u));
315 	memset(m, 0, sizeof(m));
316 	memset(f, 0, sizeof(f));
317 
318 	maxuid = maxgid = maxmode = maxflags = 0;
319 	for (; p; p = p->fts_link) {
320 		smode = p->fts_statp->st_mode & MBITS;
321 		if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
322 			savemode = smode;
323 			maxmode = m[smode];
324 		}
325 		sgid = p->fts_statp->st_gid;
326 		if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
327 			savegid = sgid;
328 			maxgid = g[sgid];
329 		}
330 		suid = p->fts_statp->st_uid;
331 		if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
332 			saveuid = suid;
333 			maxuid = u[suid];
334 		}
335 
336 #if HAVE_STRUCT_STAT_ST_FLAGS
337 		sflags = FLAGS2INDEX(p->fts_statp->st_flags);
338 		if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
339 			saveflags = p->fts_statp->st_flags;
340 			maxflags = f[sflags];
341 		}
342 #endif
343 	}
344 	/*
345 	 * If the /set record is the same as the last one we do not need to
346 	 * output a new one.  So first we check to see if anything changed.
347 	 * Note that we always output a /set record for the first directory.
348 	 */
349 	if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
350 	    ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
351 	    ((keys & F_MODE) && (*pmode != savemode)) ||
352 	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
353 	    first) {
354 		first = 0;
355 		printf("/set type=file");
356 		if (keys & (F_UID | F_UNAME)) {
357 			if (keys & F_UNAME &&
358 			    (name = user_from_uid(saveuid, 1)) != NULL)
359 				printf(" uname=%s", name);
360 			else /* if (keys & F_UID) */
361 				printf(" uid=%lu", (u_long)saveuid);
362 		}
363 		if (keys & (F_GID | F_GNAME)) {
364 			if (keys & F_GNAME &&
365 			    (name = group_from_gid(savegid, 1)) != NULL)
366 				printf(" gname=%s", name);
367 			else /* if (keys & F_UID) */
368 				printf(" gid=%lu", (u_long)savegid);
369 		}
370 		if (keys & F_MODE)
371 			printf(" mode=%#lo", (u_long)savemode);
372 		if (keys & F_NLINK)
373 			printf(" nlink=1");
374 		if (keys & F_FLAGS)
375 			printf(" flags=%s",
376 			    flags_to_string(saveflags, "none"));
377 		printf("\n");
378 		*puid = saveuid;
379 		*pgid = savegid;
380 		*pmode = savemode;
381 		*pflags = saveflags;
382 	}
383 	return (0);
384 }
385 
386 static int
387 dcmp(const FTSENT **a, const FTSENT **b)
388 {
389 
390 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
391 		if (!S_ISDIR((*b)->fts_statp->st_mode))
392 			return (1);
393 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
394 		return (-1);
395 	return (strcmp((*a)->fts_name, (*b)->fts_name));
396 }
397 
398 void
399 output(int *offset, const char *fmt, ...)
400 {
401 	va_list ap;
402 	char buf[1024];
403 
404 	va_start(ap, fmt);
405 	vsnprintf(buf, sizeof(buf), fmt, ap);
406 	va_end(ap);
407 
408 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
409 		printf(" \\\n%*s", INDENTNAMELEN, "");
410 		*offset = INDENTNAMELEN;
411 	}
412 	*offset += printf(" %s", buf) + 1;
413 }
414