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