xref: /openbsd-src/usr.sbin/mtree/create.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$NetBSD: create.c,v 1.11 1996/09/05 09:24:19 mycroft Exp $	*/
2 /*	$OpenBSD: create.c,v 1.20 2003/06/02 23:36:54 millert Exp $	*/
3 
4 /*-
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static const char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
36 #else
37 static const char rcsid[] = "$OpenBSD: create.c,v 1.20 2003/06/02 23:36:54 millert Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <time.h>
44 #include <fcntl.h>
45 #include <fts.h>
46 #include <dirent.h>
47 #include <grp.h>
48 #include <pwd.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <stdarg.h>
53 #include <vis.h>
54 #include <md5.h>
55 #include <sha1.h>
56 #include <rmd160.h>
57 #include "mtree.h"
58 #include "extern.h"
59 
60 #define	INDENTNAMELEN	15
61 #define	MAXLINELEN	80
62 
63 extern u_int32_t crc_total;
64 extern int ftsoptions;
65 extern int dflag, iflag, nflag, sflag;
66 extern u_int keys;
67 extern char fullpath[MAXPATHLEN];
68 
69 static gid_t gid;
70 static uid_t uid;
71 static mode_t mode;
72 
73 static int	dsort(const FTSENT **, const FTSENT **);
74 static void	output(int, int *, const char *, ...);
75 static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *);
76 static void	statf(int, FTSENT *);
77 
78 void
79 cwalk()
80 {
81 	FTS *t;
82 	FTSENT *p;
83 	time_t clock;
84 	char *argv[2], host[MAXHOSTNAMELEN];
85 	int indent = 0;
86 
87 	(void)time(&clock);
88 	(void)gethostname(host, sizeof(host));
89 	(void)printf(
90 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
91 	    getlogin(), host, fullpath, ctime(&clock));
92 
93 	argv[0] = ".";
94 	argv[1] = NULL;
95 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
96 		error("fts_open: %s", strerror(errno));
97 	while ((p = fts_read(t))) {
98 		if (iflag)
99 			indent = p->fts_level * 4;
100 		switch(p->fts_info) {
101 		case FTS_D:
102 			if (!dflag)
103 				(void)printf("\n");
104 			if (!nflag)
105 				(void)printf("# %s\n", p->fts_path);
106 			statd(t, p, &uid, &gid, &mode);
107 			statf(indent, p);
108 			break;
109 		case FTS_DP:
110 			if (!nflag && (p->fts_level > 0))
111 				(void)printf("%*s# %s\n", indent, "", p->fts_path);
112 			(void)printf("%*s..\n", indent, "");
113 			if (!dflag)
114 				(void)printf("\n");
115 			break;
116 		case FTS_DNR:
117 		case FTS_ERR:
118 		case FTS_NS:
119 			(void)fprintf(stderr, "mtree: %s: %s\n",
120 			    p->fts_path, strerror(p->fts_errno));
121 			break;
122 		default:
123 			if (!dflag)
124 				statf(indent, p);
125 			break;
126 
127 		}
128 	}
129 	(void)fts_close(t);
130 	if (sflag && keys & F_CKSUM)
131 		(void)fprintf(stderr,
132 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
133 }
134 
135 static void
136 statf(indent, p)
137 	int indent;
138 	FTSENT *p;
139 {
140 	struct group *gr;
141 	struct passwd *pw;
142 	u_int32_t len, val;
143 	int fd, offset;
144 	char *name, *escaped_name;
145 	size_t esc_len;
146 
147 	esc_len = p->fts_namelen * 4 + 1;
148 	escaped_name = malloc(esc_len);
149 	if (escaped_name == NULL)
150 		error("statf: %s", strerror(errno));
151  	strnvis(escaped_name, p->fts_name, esc_len, VIS_WHITE | VIS_OCTAL);
152 
153 	if (iflag || S_ISDIR(p->fts_statp->st_mode))
154 		offset = printf("%*s%s", indent, "", escaped_name);
155 	else
156 		offset = printf("%*s    %s", indent, "", escaped_name);
157 
158 	free(escaped_name);
159 
160 	if (offset > (INDENTNAMELEN + indent))
161 		offset = MAXLINELEN;
162 	else
163 		offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
164 
165 	if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
166 		output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
167 	if (p->fts_statp->st_uid != uid) {
168 		if (keys & F_UNAME) {
169 			if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
170 				output(indent, &offset, "uname=%s", pw->pw_name);
171 			} else {
172 				error("could not get uname for uid=%u",
173 				    p->fts_statp->st_uid);
174 			}
175 		}
176 		if (keys & F_UID)
177 			output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
178 	}
179 	if (p->fts_statp->st_gid != gid) {
180 		if (keys & F_GNAME) {
181 			if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
182 				output(indent, &offset, "gname=%s", gr->gr_name);
183 			} else {
184 				error("could not get gname for gid=%u",
185 				    p->fts_statp->st_gid);
186 			}
187 		}
188 		if (keys & F_GID)
189 			output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
190 	}
191 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
192 		output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
193 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
194 		output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
195 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
196 		output(indent, &offset, "size=%qd", p->fts_statp->st_size);
197 	if (keys & F_TIME)
198 		output(indent, &offset, "time=%ld.%ld",
199 		    p->fts_statp->st_mtimespec.tv_sec,
200 		    p->fts_statp->st_mtimespec.tv_nsec);
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 			error("%s: %s", p->fts_accpath, strerror(errno));
205 		(void)close(fd);
206 		output(indent, &offset, "cksum=%lu", val);
207 	}
208 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
209 		char *md5digest, buf[33];
210 
211 		md5digest = MD5File(p->fts_accpath,buf);
212 		if (!md5digest)
213 			error("%s: %s", p->fts_accpath, strerror(errno));
214 		else
215 			output(indent, &offset, "md5digest=%s", md5digest);
216 	}
217 	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
218 		char *rmd160digest, buf[41];
219 
220 		rmd160digest = RMD160File(p->fts_accpath,buf);
221 		if (!rmd160digest)
222 			error("%s: %s", p->fts_accpath, strerror(errno));
223 		else
224 			output(indent, &offset, "rmd160digest=%s", rmd160digest);
225 	}
226 	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
227 		char *sha1digest, buf[41];
228 
229 		sha1digest = SHA1File(p->fts_accpath,buf);
230 		if (!sha1digest)
231 			error("%s: %s", p->fts_accpath, strerror(errno));
232 		else
233 			output(indent, &offset, "sha1digest=%s", sha1digest);
234 	}
235 	if (keys & F_SLINK &&
236 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
237 		name = rlink(p->fts_accpath);
238 		esc_len = strlen(name) * 4 + 1;
239 		escaped_name = malloc(esc_len);
240 		if (escaped_name == NULL)
241 			error("statf: %s", strerror(errno));
242 		strnvis(escaped_name, name, esc_len, VIS_WHITE | VIS_OCTAL);
243 		output(indent, &offset, "link=%s", escaped_name);
244 		free(escaped_name);
245 	}
246 	if (keys & F_FLAGS && !S_ISLNK(p->fts_statp->st_mode)) {
247 		char *file_flags;
248 
249 		file_flags = fflagstostr(p->fts_statp->st_flags);
250 		if (file_flags == NULL)
251 			error("%s", strerror(errno));
252 		if (*file_flags != '\0')
253 			output(indent, &offset, "flags=%s", file_flags);
254 		else
255 			output(indent, &offset, "flags=none");
256 		free(file_flags);
257 	}
258 	(void)putchar('\n');
259 }
260 
261 #define	MAXGID	5000
262 #define	MAXUID	5000
263 #define	MAXMODE	MBITS + 1
264 
265 static int
266 statd(t, parent, puid, pgid, pmode)
267 	FTS *t;
268 	FTSENT *parent;
269 	uid_t *puid;
270 	gid_t *pgid;
271 	mode_t *pmode;
272 {
273 	FTSENT *p;
274 	gid_t sgid;
275 	uid_t suid;
276 	mode_t smode;
277 	struct group *gr;
278 	struct passwd *pw;
279 	gid_t savegid = *pgid;
280 	uid_t saveuid = *puid;
281 	mode_t savemode = *pmode;
282 	int maxgid;
283 	int maxuid;
284 	u_short maxmode;
285 	gid_t g[MAXGID];
286 	uid_t u[MAXUID];
287 	mode_t m[MAXMODE];
288 	static int first = 1;
289 
290 	if ((p = fts_children(t, 0)) == NULL) {
291 		if (errno)
292 			error("%s: %s", RP(parent), strerror(errno));
293 		return (1);
294 	}
295 
296 	bzero(g, sizeof(g));
297 	bzero(u, sizeof(u));
298 	bzero(m, sizeof(m));
299 
300 	maxuid = maxgid = maxmode = 0;
301 	for (; p; p = p->fts_link) {
302 		if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
303 			smode = p->fts_statp->st_mode & MBITS;
304 			if (smode < MAXMODE && ++m[smode] > maxmode) {
305 				savemode = smode;
306 				maxmode = m[smode];
307 			}
308 			sgid = p->fts_statp->st_gid;
309 			if (sgid < MAXGID && ++g[sgid] > maxgid) {
310 				savegid = sgid;
311 				maxgid = g[sgid];
312 			}
313 			suid = p->fts_statp->st_uid;
314 			if (suid < MAXUID && ++u[suid] > maxuid) {
315 				saveuid = suid;
316 				maxuid = u[suid];
317 			}
318 		}
319 	}
320 	/*
321 	 * If the /set record is the same as the last one we do not need to output
322 	 * a new one.  So first we check to see if anything changed.  Note that we
323 	 * always output a /set record for the first directory.
324 	 */
325 	if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
326 	    (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
327 	    ((keys & F_MODE) && (*pmode != savemode)) || (first)) {
328 		first = 0;
329 		if (dflag)
330 			(void)printf("/set type=dir");
331 		else
332 			(void)printf("/set type=file");
333 		if (keys & F_UNAME) {
334 			if ((pw = getpwuid(saveuid)) != NULL)
335 				(void)printf(" uname=%s", pw->pw_name);
336 			else
337 				error("could not get uname for uid=%u", saveuid);
338 		}
339 		if (keys & F_UID)
340 			(void)printf(" uid=%u", saveuid);
341 		if (keys & F_GNAME) {
342 			if ((gr = getgrgid(savegid)) != NULL)
343 				(void)printf(" gname=%s", gr->gr_name);
344 			else
345 				error("could not get gname for gid=%u", savegid);
346 		}
347 		if (keys & F_GID)
348 			(void)printf(" gid=%u", savegid);
349 		if (keys & F_MODE)
350 			(void)printf(" mode=%#o", savemode);
351 		if (keys & F_NLINK)
352 			(void)printf(" nlink=1");
353 		(void)printf("\n");
354 		*puid = saveuid;
355 		*pgid = savegid;
356 		*pmode = savemode;
357 	}
358 	return (0);
359 }
360 
361 static int
362 dsort(a, b)
363 	const FTSENT **a, **b;
364 {
365 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
366 		if (!S_ISDIR((*b)->fts_statp->st_mode))
367 			return (1);
368 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
369 		return (-1);
370 	return (strcmp((*a)->fts_name, (*b)->fts_name));
371 }
372 
373 void
374 output(int indent, int *offset, const char *fmt, ...)
375 {
376 	va_list ap;
377 	char buf[1024];
378 
379 	va_start(ap, fmt);
380 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
381 	va_end(ap);
382 
383 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
384 		(void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
385 		*offset = INDENTNAMELEN + indent;
386 	}
387 	*offset += printf(" %s", buf) + 1;
388 }
389