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