Home | History | Annotate | Line # | Download | only in mtree
spec.c revision 1.53
      1 /*	$NetBSD: spec.c,v 1.53 2003/11/17 00:02:33 dbj 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 /*-
     33  * Copyright (c) 2001-2002 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Luke Mewburn of Wasabi Systems.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *        This product includes software developed by the NetBSD
     50  *        Foundation, Inc. and its contributors.
     51  * 4. Neither the name of The NetBSD Foundation nor the names of its
     52  *    contributors may be used to endorse or promote products derived
     53  *    from this software without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 #if defined(__RCSID) && !defined(lint)
     70 #if 0
     71 static char sccsid[] = "@(#)spec.c	8.2 (Berkeley) 4/28/95";
     72 #else
     73 __RCSID("$NetBSD: spec.c,v 1.53 2003/11/17 00:02:33 dbj Exp $");
     74 #endif
     75 #endif /* not lint */
     76 
     77 #include <sys/param.h>
     78 #include <sys/stat.h>
     79 
     80 #include <ctype.h>
     81 #include <errno.h>
     82 #include <grp.h>
     83 #include <pwd.h>
     84 #include <stdio.h>
     85 #include <stdlib.h>
     86 #include <string.h>
     87 #include <unistd.h>
     88 #include <vis.h>
     89 #include <util.h>
     90 
     91 #include "extern.h"
     92 #include "pack_dev.h"
     93 
     94 size_t	mtree_lineno;			/* Current spec line number */
     95 int	Wflag;				/* Don't "whack" permissions */
     96 
     97 static	dev_t	parsedev(char *);
     98 static	void	replacenode(NODE *, NODE *);
     99 static	void	set(char *, NODE *);
    100 static	void	unset(char *, NODE *);
    101 
    102 NODE *
    103 spec(FILE *fp)
    104 {
    105 	NODE *centry, *last, *pathparent, *cur;
    106 	char *p, *e, *next;
    107 	NODE ginfo, *root;
    108 	char *buf, *tname, *ntname;
    109 	size_t tnamelen, plen;
    110 
    111 	root = NULL;
    112 	centry = last = NULL;
    113 	tname = NULL;
    114 	tnamelen = 0;
    115 	memset(&ginfo, 0, sizeof(ginfo));
    116 	for (mtree_lineno = 0;
    117 	    (buf = fparseln(fp, NULL, &mtree_lineno, NULL,
    118 		FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC));
    119 	    free(buf)) {
    120 		/* Skip leading whitespace. */
    121 		for (p = buf; *p && isspace((unsigned char)*p); ++p)
    122 			continue;
    123 
    124 		/* If nothing but whitespace, continue. */
    125 		if (!*p)
    126 			continue;
    127 
    128 #ifdef DEBUG
    129 		fprintf(stderr, "line %lu: {%s}\n",
    130 		    (u_long)mtree_lineno, p);
    131 #endif
    132 		/* Grab file name, "$", "set", or "unset". */
    133 		next = buf;
    134 		while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
    135 			continue;
    136 		if (p == NULL)
    137 			mtree_err("missing field");
    138 
    139 		if (p[0] == '/') {
    140 			if (strcmp(p + 1, "set") == 0)
    141 				set(next, &ginfo);
    142 			else if (strcmp(p + 1, "unset") == 0)
    143 				unset(next, &ginfo);
    144 			else
    145 				mtree_err("invalid specification `%s'", p);
    146 			continue;
    147 		}
    148 
    149 		if (strcmp(p, "..") == 0) {
    150 			/* Don't go up, if haven't gone down. */
    151 			if (root == NULL)
    152 				goto noparent;
    153 			if (last->type != F_DIR || last->flags & F_DONE) {
    154 				if (last == root)
    155 					goto noparent;
    156 				last = last->parent;
    157 			}
    158 			last->flags |= F_DONE;
    159 			continue;
    160 
    161 noparent:		mtree_err("no parent node");
    162 		}
    163 
    164 		plen = strlen(p) + 1;
    165 		if (plen > tnamelen) {
    166 			if ((ntname = realloc(tname, plen)) == NULL)
    167 				mtree_err("realloc: %s", strerror(errno));
    168 			tname = ntname;
    169 			tnamelen = plen;
    170 		}
    171 		if (strunvis(tname, p) == -1)
    172 			mtree_err("strunvis failed on `%s'", p);
    173 		p = tname;
    174 
    175 		pathparent = NULL;
    176 		if (strchr(p, '/') != NULL) {
    177 			cur = root;
    178 			for (; (e = strchr(p, '/')) != NULL; p = e+1) {
    179 				if (p == e)
    180 					continue;	/* handle // */
    181 				*e = '\0';
    182 				if (strcmp(p, ".") != 0) {
    183 					while (cur &&
    184 					    strcmp(cur->name, p) != 0) {
    185 						cur = cur->next;
    186 					}
    187 				}
    188 				if (cur == NULL || cur->type != F_DIR) {
    189 					mtree_err("%s: %s", tname,
    190 					    strerror(ENOENT));
    191 				}
    192 				*e = '/';
    193 				pathparent = cur;
    194 				cur = cur->child;
    195 			}
    196 			if (*p == '\0')
    197 				mtree_err("%s: empty leaf element", tname);
    198 		}
    199 
    200 		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
    201 			mtree_err("%s", strerror(errno));
    202 		*centry = ginfo;
    203 		centry->lineno = mtree_lineno;
    204 		strcpy(centry->name, p);
    205 #define	MAGIC	"?*["
    206 		if (strpbrk(p, MAGIC))
    207 			centry->flags |= F_MAGIC;
    208 		set(next, centry);
    209 
    210 		if (root == NULL) {
    211 				/*
    212 				 * empty tree
    213 				 */
    214 			if (strcmp(centry->name, ".") != 0 ||
    215 			    centry->type != F_DIR)
    216 				mtree_err(
    217 				    "root node must be the directory `.'");
    218 			last = root = centry;
    219 			root->parent = root;
    220 		} else if (pathparent != NULL) {
    221 				/*
    222 				 * full path entry
    223 				 */
    224 			centry->parent = pathparent;
    225 			cur = pathparent->child;
    226 			if (cur == NULL) {
    227 				pathparent->child = centry;
    228 				last = centry;
    229 			} else {
    230 				for (; cur != NULL; cur = cur->next) {
    231 					if (strcmp(cur->name, centry->name)
    232 					    == 0) {
    233 						/* existing entry; replace */
    234 						replacenode(cur, centry);
    235 						break;
    236 					}
    237 					if (cur->next == NULL) {
    238 						/* last entry; add new */
    239 						cur->next = centry;
    240 						centry->prev = cur;
    241 						break;
    242 					}
    243 				}
    244 				last = cur;
    245 				while (last->next != NULL)
    246 					last = last->next;
    247 			}
    248 		} else if (strcmp(centry->name, ".") == 0) {
    249 				/*
    250 				 * duplicate "." entry; always replace
    251 				 */
    252 			replacenode(root, centry);
    253 		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
    254 				/*
    255 				 * new relative child
    256 				 * (no duplicate check)
    257 				 */
    258 			centry->parent = last;
    259 			last = last->child = centry;
    260 		} else {
    261 				/*
    262 				 * relative entry, up one directory
    263 				 * (no duplicate check)
    264 				 */
    265 			centry->parent = last->parent;
    266 			centry->prev = last;
    267 			last = last->next = centry;
    268 		}
    269 	}
    270 	return (root);
    271 }
    272 
    273 /*
    274  * dump_nodes --
    275  *	dump the NODEs from `cur', based in the directory `dir'.
    276  *	if pathlast is none zero, print the path last, otherwise print
    277  *	it first.
    278  */
    279 void
    280 dump_nodes(const char *dir, NODE *root, int pathlast)
    281 {
    282 	NODE	*cur;
    283 	char	path[MAXPATHLEN];
    284 	const char *name;
    285 
    286 	for (cur = root; cur != NULL; cur = cur->next) {
    287 		if (cur->type != F_DIR && !matchtags(cur))
    288 			continue;
    289 
    290 		if (snprintf(path, sizeof(path), "%s%s%s",
    291 		    dir, *dir ? "/" : "", cur->name)
    292 		    >= sizeof(path))
    293 			mtree_err("Pathname too long.");
    294 
    295 		if (!pathlast)
    296 			printf("%s ", vispath(path));
    297 
    298 #define MATCHFLAG(f)	((keys & (f)) && (cur->flags & (f)))
    299 		if (MATCHFLAG(F_TYPE))
    300 			printf("type=%s ", nodetype(cur->type));
    301 		if (MATCHFLAG(F_UID | F_UNAME)) {
    302 			if (keys & F_UNAME &&
    303 			    (name = user_from_uid(cur->st_uid, 1)) != NULL)
    304 				printf("uname=%s ", name);
    305 			else
    306 				printf("uid=%u ", cur->st_uid);
    307 		}
    308 		if (MATCHFLAG(F_GID | F_GNAME)) {
    309 			if (keys & F_GNAME &&
    310 			    (name = group_from_gid(cur->st_gid, 1)) != NULL)
    311 				printf("gname=%s ", name);
    312 			else
    313 				printf("gid=%u ", cur->st_gid);
    314 		}
    315 		if (MATCHFLAG(F_MODE))
    316 			printf("mode=%#o ", cur->st_mode);
    317 		if (MATCHFLAG(F_DEV) &&
    318 		    (cur->type == F_BLOCK || cur->type == F_CHAR))
    319 			printf("device=%#x ", cur->st_rdev);
    320 		if (MATCHFLAG(F_NLINK))
    321 			printf("nlink=%d ", cur->st_nlink);
    322 		if (MATCHFLAG(F_SLINK))
    323 			printf("link=%s ", cur->slink);
    324 		if (MATCHFLAG(F_SIZE))
    325 			printf("size=%lld ", (long long)cur->st_size);
    326 		if (MATCHFLAG(F_TIME))
    327 			printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec,
    328 			    cur->st_mtimespec.tv_nsec);
    329 		if (MATCHFLAG(F_CKSUM))
    330 			printf("cksum=%lu ", cur->cksum);
    331 		if (MATCHFLAG(F_MD5))
    332 			printf("md5=%s ", cur->md5digest);
    333 		if (MATCHFLAG(F_RMD160))
    334 			printf("rmd160=%s ", cur->rmd160digest);
    335 		if (MATCHFLAG(F_SHA1))
    336 			printf("sha1=%s ", cur->sha1digest);
    337 		if (MATCHFLAG(F_FLAGS))
    338 			printf("flags=%s ",
    339 			    flags_to_string(cur->st_flags, "none"));
    340 		if (MATCHFLAG(F_IGN))
    341 			printf("ignore ");
    342 		if (MATCHFLAG(F_OPT))
    343 			printf("optional ");
    344 		if (MATCHFLAG(F_TAGS))
    345 			printf("tags=%s ", cur->tags);
    346 		puts(pathlast ? vispath(path) : "");
    347 
    348 		if (cur->child)
    349 			dump_nodes(path, cur->child, pathlast);
    350 	}
    351 }
    352 
    353 /*
    354  * vispath --
    355  *	strsvis(3) encodes path, which must not be longer than MAXPATHLEN
    356  *	characters long, and returns a pointer to a static buffer containing
    357  *	the result.
    358  */
    359 char *
    360 vispath(const char *path)
    361 {
    362 	const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
    363 	static char pathbuf[4*MAXPATHLEN + 1];
    364 
    365 	strsvis(pathbuf, path, VIS_CSTYLE, extra);
    366 	return(pathbuf);
    367 }
    368 
    369 
    370 static dev_t
    371 parsedev(char *arg)
    372 {
    373 #define MAX_PACK_ARGS	3
    374 	u_long	numbers[MAX_PACK_ARGS];
    375 	char	*p, *ep, *dev;
    376 	int	argc;
    377 	pack_t	*pack;
    378 	dev_t	result;
    379 
    380 	if ((dev = strchr(arg, ',')) != NULL) {
    381 		*dev++='\0';
    382 		if ((pack = pack_find(arg)) == NULL)
    383 			mtree_err("unknown format `%s'", arg);
    384 		argc = 0;
    385 		while ((p = strsep(&dev, ",")) != NULL) {
    386 			if (*p == '\0')
    387 				mtree_err("missing number");
    388 			numbers[argc++] = strtoul(p, &ep, 0);
    389 			if (*ep != '\0')
    390 				mtree_err("invalid number `%s'",
    391 				    p);
    392 			if (argc > MAX_PACK_ARGS)
    393 				mtree_err("too many arguments");
    394 		}
    395 		if (argc < 2)
    396 			mtree_err("not enough arguments");
    397 		result = (*pack)(argc, numbers);
    398 	} else {
    399 		result = (dev_t)strtoul(arg, &ep, 0);
    400 		if (*ep != '\0')
    401 			mtree_err("invalid device `%s'", arg);
    402 	}
    403 	return (result);
    404 }
    405 
    406 static void
    407 replacenode(NODE *cur, NODE *new)
    408 {
    409 
    410 	if (cur->type != new->type)
    411 		mtree_err("existing entry for `%s', type `%s' does not match type `%s'",
    412 		    cur->name, nodetype(cur->type), nodetype(new->type));
    413 #define REPLACE(x)	cur->x = new->x
    414 #define REPLACESTR(x)	if (cur->x) free(cur->x); cur->x = new->x
    415 
    416 	REPLACE(st_size);
    417 	REPLACE(st_mtimespec);
    418 	REPLACESTR(slink);
    419 	REPLACE(st_uid);
    420 	REPLACE(st_gid);
    421 	REPLACE(st_mode);
    422 	REPLACE(st_rdev);
    423 	REPLACE(st_flags);
    424 	REPLACE(st_nlink);
    425 	REPLACE(cksum);
    426 	REPLACESTR(md5digest);
    427 	REPLACESTR(rmd160digest);
    428 	REPLACESTR(sha1digest);
    429 	REPLACESTR(tags);
    430 	REPLACE(lineno);
    431 	REPLACE(flags);
    432 	free(new);
    433 }
    434 
    435 static void
    436 set(char *t, NODE *ip)
    437 {
    438 	int	type, value, len;
    439 	gid_t	gid;
    440 	uid_t	uid;
    441 	char	*kw, *val, *md, *ep;
    442 	void	*m;
    443 
    444 	val = NULL;
    445 	while ((kw = strsep(&t, "= \t")) != NULL) {
    446 		if (*kw == '\0')
    447 			continue;
    448 		if (strcmp(kw, "all") == 0)
    449 			mtree_err("invalid keyword `all'");
    450 		ip->flags |= type = parsekey(kw, &value);
    451 		if (value) {
    452 			while ((val = strsep(&t, " \t")) != NULL &&
    453 			    *val == '\0')
    454 				continue;
    455 			if (val == NULL)
    456 				mtree_err("missing value");
    457 		}
    458 		switch(type) {
    459 		case F_CKSUM:
    460 			ip->cksum = strtoul(val, &ep, 10);
    461 			if (*ep)
    462 				mtree_err("invalid checksum `%s'", val);
    463 			break;
    464 		case F_DEV:
    465 			ip->st_rdev = parsedev(val);
    466 			break;
    467 		case F_FLAGS:
    468 			if (strcmp("none", val) == 0)
    469 				ip->st_flags = 0;
    470 			else if (string_to_flags(&val, &ip->st_flags, NULL)
    471 			    != 0)
    472 				mtree_err("invalid flag `%s'", val);
    473 			break;
    474 		case F_GID:
    475 			ip->st_gid = (gid_t)strtoul(val, &ep, 10);
    476 			if (*ep)
    477 				mtree_err("invalid gid `%s'", val);
    478 			break;
    479 		case F_GNAME:
    480 			if (Wflag)	/* don't parse if whacking */
    481 				break;
    482 			if (gid_from_group(val, &gid) == -1)
    483 				mtree_err("unknown group `%s'", val);
    484 			ip->st_gid = gid;
    485 			break;
    486 		case F_IGN:
    487 			/* just set flag bit */
    488 			break;
    489 		case F_MD5:
    490 			if (val[0]=='0' && val[1]=='x')
    491 				md=&val[2];
    492 			else
    493 				md=val;
    494 			if ((ip->md5digest = strdup(md)) == NULL)
    495 				mtree_err("memory allocation error");
    496 			break;
    497 		case F_MODE:
    498 			if ((m = setmode(val)) == NULL)
    499 				mtree_err("invalid file mode `%s'", val);
    500 			ip->st_mode = getmode(m, 0);
    501 			free(m);
    502 			break;
    503 		case F_NLINK:
    504 			ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
    505 			if (*ep)
    506 				mtree_err("invalid link count `%s'", val);
    507 			break;
    508 		case F_OPT:
    509 			/* just set flag bit */
    510 			break;
    511 		case F_RMD160:
    512 			if (val[0]=='0' && val[1]=='x')
    513 				md=&val[2];
    514 			else
    515 				md=val;
    516 			if ((ip->rmd160digest = strdup(md)) == NULL)
    517 				mtree_err("memory allocation error");
    518 			break;
    519 		case F_SHA1:
    520 			if (val[0]=='0' && val[1]=='x')
    521 				md=&val[2];
    522 			else
    523 				md=val;
    524 			if ((ip->sha1digest = strdup(md)) == NULL)
    525 				mtree_err("memory allocation error");
    526 			break;
    527 		case F_SIZE:
    528 			ip->st_size = (off_t)strtoll(val, &ep, 10);
    529 			if (*ep)
    530 				mtree_err("invalid size `%s'", val);
    531 			break;
    532 		case F_SLINK:
    533 			if ((ip->slink = strdup(val)) == NULL)
    534 				mtree_err("memory allocation error");
    535 			break;
    536 		case F_TAGS:
    537 			len = strlen(val) + 3;	/* "," + str + ",\0" */
    538 			if ((ip->tags = malloc(len)) == NULL)
    539 				mtree_err("memory allocation error");
    540 			snprintf(ip->tags, len, ",%s,", val);
    541 			break;
    542 		case F_TIME:
    543 			ip->st_mtimespec.tv_sec =
    544 			    (time_t)strtoul(val, &ep, 10);
    545 			if (*ep != '.')
    546 				mtree_err("invalid time `%s'", val);
    547 			val = ep + 1;
    548 			ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10);
    549 			if (*ep)
    550 				mtree_err("invalid time `%s'", val);
    551 			break;
    552 		case F_TYPE:
    553 			ip->type = parsetype(val);
    554 			break;
    555 		case F_UID:
    556 			ip->st_uid = (uid_t)strtoul(val, &ep, 10);
    557 			if (*ep)
    558 				mtree_err("invalid uid `%s'", val);
    559 			break;
    560 		case F_UNAME:
    561 			if (Wflag)	/* don't parse if whacking */
    562 				break;
    563 			if (uid_from_user(val, &uid) == -1)
    564 				mtree_err("unknown user `%s'", val);
    565 			ip->st_uid = uid;
    566 			break;
    567 		default:
    568 			mtree_err(
    569 			    "set(): unsupported key type 0x%x (INTERNAL ERROR)",
    570 			    type);
    571 			/* NOTREACHED */
    572 		}
    573 	}
    574 }
    575 
    576 static void
    577 unset(char *t, NODE *ip)
    578 {
    579 	char *p;
    580 
    581 	while ((p = strsep(&t, " \t")) != NULL) {
    582 		if (*p == '\0')
    583 			continue;
    584 		ip->flags &= ~parsekey(p, NULL);
    585 	}
    586 }
    587