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