Home | History | Annotate | Line # | Download | only in mtree
      1 /*	$NetBSD: create.c,v 1.79 2024/12/05 17:17:43 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.79 2024/12/05 17:17:43 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 <stdint.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <time.h>
     62 #include <unistd.h>
     63 
     64 #ifndef NO_MD5
     65 #include <md5.h>
     66 #endif
     67 #ifndef NO_RMD160
     68 #include <rmd160.h>
     69 #endif
     70 #ifndef NO_SHA1
     71 #include <sha1.h>
     72 #endif
     73 #ifndef NO_SHA2
     74 #include <sha2.h>
     75 #endif
     76 
     77 #include "extern.h"
     78 
     79 #define	INDENTNAMELEN	15
     80 #define	MAXLINELEN	80
     81 
     82 static gid_t gid;
     83 static uid_t uid;
     84 static mode_t mode;
     85 static u_long flags;
     86 
     87 static void	output(FILE *, int, int *, const char *, ...)
     88     __printflike(4, 5);
     89 static int	statd(FILE *, FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
     90     u_long *);
     91 static void	statf(FILE *, int, FTSENT *);
     92 
     93 void
     94 cwalk(FILE *fp)
     95 {
     96 	FTS *t;
     97 	FTSENT *p;
     98 	time_t clocktime;
     99 	char host[MAXHOSTNAMELEN + 1];
    100 	const char *user;
    101 	char *argv[2];
    102 	char  dot[] = ".";
    103 	int indent = 0;
    104 
    105 	argv[0] = dot;
    106 	argv[1] = NULL;
    107 
    108 	time(&clocktime);
    109 	gethostname(host, sizeof(host));
    110 	host[sizeof(host) - 1] = '\0';
    111 	if ((user = getlogin()) == NULL) {
    112 		struct passwd *pw;
    113 		user = (pw = getpwuid(getuid())) != NULL ? pw->pw_name :
    114 		    "<unknown>";
    115 	}
    116 
    117 	if (!nflag)
    118 		fprintf(fp,
    119 	    	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n"
    120 		    "#\t   date: %s",
    121 		    user, host, fullpath, ctime(&clocktime));
    122 
    123 	if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
    124 		mtree_err("fts_open: %s", strerror(errno));
    125 	while ((p = fts_read(t)) != NULL) {
    126 		if (jflag)
    127 			indent = p->fts_level * 4;
    128 		if (check_excludes(p->fts_name, p->fts_path)) {
    129 			fts_set(t, p, FTS_SKIP);
    130 			continue;
    131 		}
    132 		if (!find_only(p->fts_path)) {
    133 			fts_set(t, p, FTS_SKIP);
    134 			continue;
    135 		}
    136 		switch(p->fts_info) {
    137 		case FTS_D:
    138 			if (!bflag)
    139 				fprintf(fp, "\n");
    140 			if (!nflag)
    141 				fprintf(fp, "# %s\n", p->fts_path);
    142 			statd(fp, t, p, &uid, &gid, &mode, &flags);
    143 			statf(fp, indent, p);
    144 			break;
    145 		case FTS_DP:
    146 			if (p->fts_level > 0)
    147 				if (!nflag)
    148 					fprintf(fp, "%*s# %s\n", indent, "",
    149 					    p->fts_path);
    150 			if (p->fts_level > 0 || flavor == F_FREEBSD9) {
    151 				fprintf(fp, "%*s..\n", indent, "");
    152 				if (!bflag)
    153 					fprintf(fp, "\n");
    154 			}
    155 			break;
    156 		case FTS_DNR:
    157 		case FTS_ERR:
    158 		case FTS_NS:
    159 			mtree_err("%s: %s",
    160 			    p->fts_path, strerror(p->fts_errno));
    161 			break;
    162 		default:
    163 			if (!dflag)
    164 				statf(fp, indent, p);
    165 			break;
    166 
    167 		}
    168 	}
    169 	if (errno != 0)
    170 		mtree_err("fts_read: %s", strerror(errno));
    171 	fts_close(t);
    172 	if (sflag && keys & F_CKSUM)
    173 		mtree_err("%s checksum: %u", fullpath, crc_total);
    174 }
    175 
    176 static void
    177 dosum(FILE *fp, int indent, FTSENT *p, int *offset, int flag,
    178     char * (*func)(const char *, char *), const char *key)
    179 {
    180 	char *digestbuf;
    181 
    182 	if ((keys & flag) == 0)
    183 		return;
    184 
    185 	digestbuf = (*func)(p->fts_accpath, NULL);
    186 	if (digestbuf != NULL) {
    187 		output(fp, indent, offset, "%s=%s", key, digestbuf);
    188 		free(digestbuf);
    189 		return;
    190 	}
    191 
    192 	if (qflag) {
    193 		warn("%s: %s failed", p->fts_path, key);
    194 		return;
    195 	}
    196 
    197 	mtree_err("%s: %s failed: %s", p->fts_path, key, strerror(errno));
    198 }
    199 
    200 static char *
    201 crcFile(const char *fname, char *dummy __unused)
    202 {
    203 	char *ptr;
    204 	uint32_t val, len;
    205 	int fd, e;
    206 
    207 	if ((fd = open(fname, O_RDONLY)) == -1)
    208 		goto out;
    209 
    210 	e = crc(fd, &val, &len);
    211 	close(fd);
    212 	if (e)
    213 		goto out;
    214 
    215 	if (asprintf(&ptr, "%u", val) < 0)
    216 		goto out;
    217 
    218 	return ptr;
    219 out:
    220 	mtree_err("%s: %s", fname, strerror(errno));
    221 	return NULL;
    222 }
    223 
    224 static void
    225 statf(FILE *fp, int indent, FTSENT *p)
    226 {
    227 	int offset;
    228 	const char *name = NULL;
    229 
    230 	offset = fprintf(fp, "%*s%s%s", indent, "",
    231 	    S_ISDIR(p->fts_statp->st_mode) ? "" : "    ", vispath(p->fts_name));
    232 
    233 	if (offset > (INDENTNAMELEN + indent))
    234 		offset = MAXLINELEN;
    235 	else
    236 		offset += fprintf(fp, "%*s",
    237 		    (INDENTNAMELEN + indent) - offset, "");
    238 
    239 	if (!S_ISREG(p->fts_statp->st_mode) && (flavor == F_NETBSD6 || !dflag))
    240 		output(fp, indent, &offset, "type=%s",
    241 		    inotype(p->fts_statp->st_mode));
    242 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
    243 		if (keys & F_UNAME &&
    244 		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
    245 			output(fp, indent, &offset, "uname=%s", name);
    246 		if (keys & F_UID || (keys & F_UNAME && name == NULL))
    247 			output(fp, indent, &offset, "uid=%u",
    248 			    p->fts_statp->st_uid);
    249 	}
    250 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
    251 		if (keys & F_GNAME &&
    252 		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
    253 			output(fp, indent, &offset, "gname=%s", name);
    254 		if (keys & F_GID || (keys & F_GNAME && name == NULL))
    255 			output(fp, indent, &offset, "gid=%u",
    256 			    p->fts_statp->st_gid);
    257 	}
    258 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
    259 		output(fp, indent, &offset, "mode=%#o",
    260 		    p->fts_statp->st_mode & MBITS);
    261 	if (keys & F_DEV &&
    262 	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
    263 		output(fp, indent, &offset, "device=%#jx",
    264 		    (uintmax_t)p->fts_statp->st_rdev);
    265 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
    266 		output(fp, indent, &offset, "nlink=%ju",
    267 		    (uintmax_t)p->fts_statp->st_nlink);
    268 	if (keys & F_SIZE &&
    269 	    (flavor == F_FREEBSD9 || S_ISREG(p->fts_statp->st_mode)))
    270 		output(fp, indent, &offset, "size=%ju",
    271 		    (uintmax_t)p->fts_statp->st_size);
    272 	if (keys & F_TIME)
    273 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
    274 		output(fp, indent, &offset, "time=%jd.%09ld",
    275 		    (intmax_t)p->fts_statp->st_mtimespec.tv_sec,
    276 		    p->fts_statp->st_mtimespec.tv_nsec);
    277 #else
    278 		output(fp, indent, &offset, "time=%jd.%09ld",
    279 		    (intmax_t)p->fts_statp->st_mtime, (long)0);
    280 #endif
    281 	if (S_ISREG(p->fts_statp->st_mode))  {
    282 		dosum(fp, indent, p, &offset, F_CKSUM, crcFile, "cksum");
    283 #ifndef NO_MD5
    284 		dosum(fp, indent, p, &offset, F_MD5, MD5File, MD5KEY);
    285 #endif	/* ! NO_MD5 */
    286 #ifndef NO_RMD160
    287 		dosum(fp, indent, p, &offset, F_RMD160, RMD160File, RMD160KEY);
    288 #endif	/* ! NO_RMD160 */
    289 #ifndef NO_SHA1
    290 		dosum(fp, indent, p, &offset, F_SHA1, SHA1File, SHA1KEY);
    291 #endif	/* ! NO_SHA1 */
    292 #ifndef NO_SHA2
    293 		dosum(fp, indent, p, &offset, F_SHA256, SHA256_File, SHA256KEY);
    294 #ifdef SHA384_BLOCK_LENGTH
    295 		dosum(fp, indent, p, &offset, F_SHA384, SHA384_File, SHA384KEY);
    296 #endif
    297 		dosum(fp, indent, p, &offset, F_SHA512, SHA512_File, SHA512KEY);
    298 #endif	/* ! NO_SHA2 */
    299 	}
    300 	if (keys & F_SLINK &&
    301 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
    302 		output(fp, indent, &offset, "link=%s",
    303 		    vispath(rlink(p->fts_accpath)));
    304 #if HAVE_STRUCT_STAT_ST_FLAGS
    305 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
    306 		char *str = flags_to_string(p->fts_statp->st_flags, "none");
    307 		output(fp, indent, &offset, "flags=%s", str);
    308 		free(str);
    309 	}
    310 #endif
    311 	putchar('\n');
    312 }
    313 
    314 /* XXX
    315  * FLAGS2INDEX will fail once the user and system settable bits need more
    316  * than one byte, respectively.
    317  */
    318 #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
    319 
    320 #define	MTREE_MAXGID	5000
    321 #define	MTREE_MAXUID	5000
    322 #define	MTREE_MAXMODE	(MBITS + 1)
    323 #if HAVE_STRUCT_STAT_ST_FLAGS
    324 #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
    325 #else
    326 #define MTREE_MAXFLAGS	1
    327 #endif
    328 #define	MTREE_MAXS 16
    329 
    330 static int
    331 statd(FILE *fp, FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
    332     u_long *pflags)
    333 {
    334 	FTSENT *p;
    335 	gid_t sgid;
    336 	uid_t suid;
    337 	mode_t smode;
    338 	u_long sflags = 0;
    339 	const char *name = NULL;
    340 	gid_t savegid;
    341 	uid_t saveuid;
    342 	mode_t savemode;
    343 	u_long saveflags;
    344 	u_short maxgid, maxuid, maxmode, maxflags;
    345 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
    346 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
    347 	static int first = 1;
    348 
    349 	savegid = *pgid;
    350 	saveuid = *puid;
    351 	savemode = *pmode;
    352 	saveflags = *pflags;
    353 	if ((p = fts_children(t, 0)) == NULL) {
    354 		if (errno)
    355 			mtree_err("%s: %s", RP(parent), strerror(errno));
    356 		return (1);
    357 	}
    358 
    359 	memset(g, 0, sizeof(g));
    360 	memset(u, 0, sizeof(u));
    361 	memset(m, 0, sizeof(m));
    362 	memset(f, 0, sizeof(f));
    363 
    364 	maxuid = maxgid = maxmode = maxflags = 0;
    365 	for (; p; p = p->fts_link) {
    366 		if (flavor == F_NETBSD6 || !dflag ||
    367 		    (dflag && S_ISDIR(p->fts_statp->st_mode))) {
    368 			smode = p->fts_statp->st_mode & MBITS;
    369 			if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
    370 				savemode = smode;
    371 				maxmode = m[smode];
    372 			}
    373 			sgid = p->fts_statp->st_gid;
    374 			if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
    375 				savegid = sgid;
    376 				maxgid = g[sgid];
    377 			}
    378 			suid = p->fts_statp->st_uid;
    379 			if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
    380 				saveuid = suid;
    381 				maxuid = u[suid];
    382 			}
    383 
    384 #if HAVE_STRUCT_STAT_ST_FLAGS
    385 			sflags = FLAGS2INDEX(p->fts_statp->st_flags);
    386 			if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
    387 				saveflags = p->fts_statp->st_flags;
    388 				maxflags = f[sflags];
    389 			}
    390 #endif
    391 		}
    392 	}
    393 	/*
    394 	 * If the /set record is the same as the last one we do not need to
    395 	 * output a new one.  So first we check to see if anything changed.
    396 	 * Note that we always output a /set record for the first directory.
    397 	 */
    398 	if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
    399 	    ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
    400 	    ((keys & F_MODE) && (*pmode != savemode)) ||
    401 	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
    402 	    first) {
    403 		first = 0;
    404 		if (flavor != F_NETBSD6 && dflag)
    405 			fprintf(fp, "/set type=dir");
    406 		else
    407 			fprintf(fp, "/set type=file");
    408 		if (keys & (F_UID | F_UNAME)) {
    409 			if (keys & F_UNAME &&
    410 			    (name = user_from_uid(saveuid, 1)) != NULL)
    411 				fprintf(fp, " uname=%s", name);
    412 			if (keys & F_UID || (keys & F_UNAME && name == NULL))
    413 				fprintf(fp, " uid=%lu", (u_long)saveuid);
    414 		}
    415 		if (keys & (F_GID | F_GNAME)) {
    416 			if (keys & F_GNAME &&
    417 			    (name = group_from_gid(savegid, 1)) != NULL)
    418 				fprintf(fp, " gname=%s", name);
    419 			if (keys & F_GID || (keys & F_GNAME && name == NULL))
    420 				fprintf(fp, " gid=%lu", (u_long)savegid);
    421 		}
    422 		if (keys & F_MODE)
    423 			fprintf(fp, " mode=%#lo", (u_long)savemode);
    424 		if (keys & F_NLINK)
    425 			fprintf(fp, " nlink=1");
    426 		if (keys & F_FLAGS) {
    427 			char *str = flags_to_string(saveflags, "none");
    428 			fprintf(fp, " flags=%s", str);
    429 			free(str);
    430 		}
    431 		fprintf(fp, "\n");
    432 		*puid = saveuid;
    433 		*pgid = savegid;
    434 		*pmode = savemode;
    435 		*pflags = saveflags;
    436 	}
    437 	return (0);
    438 }
    439 
    440 void
    441 output(FILE *fp, int indent, int *offset, const char *fmt, ...)
    442 {
    443 	va_list ap;
    444 	char buf[1024];
    445 
    446 	va_start(ap, fmt);
    447 	vsnprintf(buf, sizeof(buf), fmt, ap);
    448 	va_end(ap);
    449 
    450 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
    451 		fprintf(fp, " \\\n%*s", INDENTNAMELEN + indent, "");
    452 		*offset = INDENTNAMELEN + indent;
    453 	}
    454 	*offset += fprintf(fp, " %s", buf) + 1;
    455 }
    456