xref: /openbsd-src/usr.sbin/mtree/create.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$NetBSD: create.c,v 1.11 1996/09/05 09:24:19 mycroft Exp $	*/
2 /*	$OpenBSD: create.c,v 1.25 2005/08/10 00:42:09 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.25 2005/08/10 00:42:09 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 void	output(int, int *, const char *, ...)
74 		    __attribute__((__format__ (printf, 3, 4)));
75 static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *);
76 static void	statf(int, FTSENT *);
77 
78 void
79 cwalk(void)
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(int indent, FTSENT *p)
137 {
138 	struct group *gr;
139 	struct passwd *pw;
140 	u_int32_t len, val;
141 	int fd, offset;
142 	char *name, *escaped_name;
143 	size_t esc_len;
144 
145 	esc_len = p->fts_namelen * 4 + 1;
146 	escaped_name = malloc(esc_len);
147 	if (escaped_name == NULL)
148 		error("statf: %s", strerror(errno));
149  	strnvis(escaped_name, p->fts_name, esc_len,
150 	    VIS_WHITE | VIS_OCTAL | VIS_GLOB);
151 
152 	if (iflag || S_ISDIR(p->fts_statp->st_mode))
153 		offset = printf("%*s%s", indent, "", escaped_name);
154 	else
155 		offset = printf("%*s    %s", indent, "", escaped_name);
156 
157 	free(escaped_name);
158 
159 	if (offset > (INDENTNAMELEN + indent))
160 		offset = MAXLINELEN;
161 	else
162 		offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
163 
164 	if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
165 		output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
166 	if (p->fts_statp->st_uid != uid) {
167 		if (keys & F_UNAME) {
168 			if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
169 				output(indent, &offset, "uname=%s", pw->pw_name);
170 			} else {
171 				error("could not get uname for uid=%u",
172 				    p->fts_statp->st_uid);
173 			}
174 		}
175 		if (keys & F_UID)
176 			output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
177 	}
178 	if (p->fts_statp->st_gid != gid) {
179 		if (keys & F_GNAME) {
180 			if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
181 				output(indent, &offset, "gname=%s", gr->gr_name);
182 			} else {
183 				error("could not get gname for gid=%u",
184 				    p->fts_statp->st_gid);
185 			}
186 		}
187 		if (keys & F_GID)
188 			output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
189 	}
190 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
191 		output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
192 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
193 		output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
194 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
195 		output(indent, &offset, "size=%qd", p->fts_statp->st_size);
196 	if (keys & F_TIME)
197 		output(indent, &offset, "time=%ld.%ld",
198 		    p->fts_statp->st_mtimespec.tv_sec,
199 		    p->fts_statp->st_mtimespec.tv_nsec);
200 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
201 		if ((fd = open(p->fts_accpath, MTREE_O_FLAGS, 0)) < 0 ||
202 		    crc(fd, &val, &len))
203 			error("%s: %s", p->fts_accpath, strerror(errno));
204 		(void)close(fd);
205 		output(indent, &offset, "cksum=%lu", val);
206 	}
207 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
208 		char *md5digest, buf[MD5_DIGEST_STRING_LENGTH];
209 
210 		md5digest = MD5File(p->fts_accpath,buf);
211 		if (!md5digest)
212 			error("%s: %s", p->fts_accpath, strerror(errno));
213 		else
214 			output(indent, &offset, "md5digest=%s", md5digest);
215 	}
216 	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
217 		char *rmd160digest, buf[RMD160_DIGEST_STRING_LENGTH];
218 
219 		rmd160digest = RMD160File(p->fts_accpath,buf);
220 		if (!rmd160digest)
221 			error("%s: %s", p->fts_accpath, strerror(errno));
222 		else
223 			output(indent, &offset, "rmd160digest=%s", rmd160digest);
224 	}
225 	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
226 		char *sha1digest, buf[SHA1_DIGEST_STRING_LENGTH];
227 
228 		sha1digest = SHA1File(p->fts_accpath,buf);
229 		if (!sha1digest)
230 			error("%s: %s", p->fts_accpath, strerror(errno));
231 		else
232 			output(indent, &offset, "sha1digest=%s", sha1digest);
233 	}
234 	if (keys & F_SLINK &&
235 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
236 		name = rlink(p->fts_accpath);
237 		esc_len = strlen(name) * 4 + 1;
238 		escaped_name = malloc(esc_len);
239 		if (escaped_name == NULL)
240 			error("statf: %s", strerror(errno));
241 		strnvis(escaped_name, name, esc_len, VIS_WHITE | VIS_OCTAL);
242 		output(indent, &offset, "link=%s", escaped_name);
243 		free(escaped_name);
244 	}
245 	if (keys & F_FLAGS && !S_ISLNK(p->fts_statp->st_mode)) {
246 		char *file_flags;
247 
248 		file_flags = fflagstostr(p->fts_statp->st_flags);
249 		if (file_flags == NULL)
250 			error("%s", strerror(errno));
251 		if (*file_flags != '\0')
252 			output(indent, &offset, "flags=%s", file_flags);
253 		else
254 			output(indent, &offset, "flags=none");
255 		free(file_flags);
256 	}
257 	(void)putchar('\n');
258 }
259 
260 #define	MAXGID	5000
261 #define	MAXUID	5000
262 #define	MAXMODE	MBITS + 1
263 
264 static int
265 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode)
266 {
267 	FTSENT *p;
268 	gid_t sgid;
269 	uid_t suid;
270 	mode_t smode;
271 	struct group *gr;
272 	struct passwd *pw;
273 	gid_t savegid = *pgid;
274 	uid_t saveuid = *puid;
275 	mode_t savemode = *pmode;
276 	int maxgid;
277 	int maxuid;
278 	u_short maxmode;
279 	gid_t g[MAXGID];
280 	uid_t u[MAXUID];
281 	mode_t m[MAXMODE];
282 	static int first = 1;
283 
284 	if ((p = fts_children(t, 0)) == NULL) {
285 		if (errno)
286 			error("%s: %s", RP(parent), strerror(errno));
287 		return (1);
288 	}
289 
290 	bzero(g, sizeof(g));
291 	bzero(u, sizeof(u));
292 	bzero(m, sizeof(m));
293 
294 	maxuid = maxgid = maxmode = 0;
295 	for (; p; p = p->fts_link) {
296 		if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
297 			smode = p->fts_statp->st_mode & MBITS;
298 			if (smode < MAXMODE && ++m[smode] > maxmode) {
299 				savemode = smode;
300 				maxmode = m[smode];
301 			}
302 			sgid = p->fts_statp->st_gid;
303 			if (sgid < MAXGID && ++g[sgid] > maxgid) {
304 				savegid = sgid;
305 				maxgid = g[sgid];
306 			}
307 			suid = p->fts_statp->st_uid;
308 			if (suid < MAXUID && ++u[suid] > maxuid) {
309 				saveuid = suid;
310 				maxuid = u[suid];
311 			}
312 		}
313 	}
314 	/*
315 	 * If the /set record is the same as the last one we do not need to output
316 	 * a new one.  So first we check to see if anything changed.  Note that we
317 	 * always output a /set record for the first directory.
318 	 */
319 	if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
320 	    (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
321 	    ((keys & F_MODE) && (*pmode != savemode)) || (first)) {
322 		first = 0;
323 		if (dflag)
324 			(void)printf("/set type=dir");
325 		else
326 			(void)printf("/set type=file");
327 		if (keys & F_UNAME) {
328 			if ((pw = getpwuid(saveuid)) != NULL)
329 				(void)printf(" uname=%s", pw->pw_name);
330 			else
331 				error("could not get uname for uid=%u", saveuid);
332 		}
333 		if (keys & F_UID)
334 			(void)printf(" uid=%u", saveuid);
335 		if (keys & F_GNAME) {
336 			if ((gr = getgrgid(savegid)) != NULL)
337 				(void)printf(" gname=%s", gr->gr_name);
338 			else
339 				error("could not get gname for gid=%u", savegid);
340 		}
341 		if (keys & F_GID)
342 			(void)printf(" gid=%u", savegid);
343 		if (keys & F_MODE)
344 			(void)printf(" mode=%#o", savemode);
345 		if (keys & F_NLINK)
346 			(void)printf(" nlink=1");
347 		(void)printf("\n");
348 		*puid = saveuid;
349 		*pgid = savegid;
350 		*pmode = savemode;
351 	}
352 	return (0);
353 }
354 
355 int
356 dsort(const FTSENT **a, const FTSENT **b)
357 {
358 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
359 		if (!S_ISDIR((*b)->fts_statp->st_mode))
360 			return (1);
361 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
362 		return (-1);
363 	return (strcmp((*a)->fts_name, (*b)->fts_name));
364 }
365 
366 void
367 output(int indent, int *offset, const char *fmt, ...)
368 {
369 	va_list ap;
370 	char buf[1024];
371 
372 	va_start(ap, fmt);
373 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
374 	va_end(ap);
375 
376 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
377 		(void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
378 		*offset = INDENTNAMELEN + indent;
379 	}
380 	*offset += printf(" %s", buf) + 1;
381 }
382