spec.c revision 1.54
1/* $NetBSD: spec.c,v 1.54 2004/01/30 19:09:03 ross 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 71static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95"; 72#else 73__RCSID("$NetBSD: spec.c,v 1.54 2004/01/30 19:09:03 ross 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 94size_t mtree_lineno; /* Current spec line number */ 95int Wflag; /* Don't "whack" permissions */ 96 97static dev_t parsedev(char *); 98static void replacenode(NODE *, NODE *); 99static void set(char *, NODE *); 100static void unset(char *, NODE *); 101 102NODE * 103spec(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 161noparent: 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 */ 279void 280dump_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 */ 359char * 360vispath(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 370static dev_t 371parsedev(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 char *error = NULL; 380 381 if ((dev = strchr(arg, ',')) != NULL) { 382 *dev++='\0'; 383 if ((pack = pack_find(arg)) == NULL) 384 mtree_err("unknown format `%s'", arg); 385 argc = 0; 386 while ((p = strsep(&dev, ",")) != NULL) { 387 if (*p == '\0') 388 mtree_err("missing number"); 389 numbers[argc++] = strtoul(p, &ep, 0); 390 if (*ep != '\0') 391 mtree_err("invalid number `%s'", 392 p); 393 if (argc > MAX_PACK_ARGS) 394 mtree_err("too many arguments"); 395 } 396 if (argc < 2) 397 mtree_err("not enough arguments"); 398 result = (*pack)(argc, numbers, &error); 399 if (error != NULL) 400 mtree_err(error); 401 } else { 402 result = (dev_t)strtoul(arg, &ep, 0); 403 if (*ep != '\0') 404 mtree_err("invalid device `%s'", arg); 405 } 406 return (result); 407} 408 409static void 410replacenode(NODE *cur, NODE *new) 411{ 412 413 if (cur->type != new->type) 414 mtree_err("existing entry for `%s', type `%s' does not match type `%s'", 415 cur->name, nodetype(cur->type), nodetype(new->type)); 416#define REPLACE(x) cur->x = new->x 417#define REPLACESTR(x) if (cur->x) free(cur->x); cur->x = new->x 418 419 REPLACE(st_size); 420 REPLACE(st_mtimespec); 421 REPLACESTR(slink); 422 REPLACE(st_uid); 423 REPLACE(st_gid); 424 REPLACE(st_mode); 425 REPLACE(st_rdev); 426 REPLACE(st_flags); 427 REPLACE(st_nlink); 428 REPLACE(cksum); 429 REPLACESTR(md5digest); 430 REPLACESTR(rmd160digest); 431 REPLACESTR(sha1digest); 432 REPLACESTR(tags); 433 REPLACE(lineno); 434 REPLACE(flags); 435 free(new); 436} 437 438static void 439set(char *t, NODE *ip) 440{ 441 int type, value, len; 442 gid_t gid; 443 uid_t uid; 444 char *kw, *val, *md, *ep; 445 void *m; 446 447 val = NULL; 448 while ((kw = strsep(&t, "= \t")) != NULL) { 449 if (*kw == '\0') 450 continue; 451 if (strcmp(kw, "all") == 0) 452 mtree_err("invalid keyword `all'"); 453 ip->flags |= type = parsekey(kw, &value); 454 if (value) { 455 while ((val = strsep(&t, " \t")) != NULL && 456 *val == '\0') 457 continue; 458 if (val == NULL) 459 mtree_err("missing value"); 460 } 461 switch(type) { 462 case F_CKSUM: 463 ip->cksum = strtoul(val, &ep, 10); 464 if (*ep) 465 mtree_err("invalid checksum `%s'", val); 466 break; 467 case F_DEV: 468 ip->st_rdev = parsedev(val); 469 break; 470 case F_FLAGS: 471 if (strcmp("none", val) == 0) 472 ip->st_flags = 0; 473 else if (string_to_flags(&val, &ip->st_flags, NULL) 474 != 0) 475 mtree_err("invalid flag `%s'", val); 476 break; 477 case F_GID: 478 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 479 if (*ep) 480 mtree_err("invalid gid `%s'", val); 481 break; 482 case F_GNAME: 483 if (Wflag) /* don't parse if whacking */ 484 break; 485 if (gid_from_group(val, &gid) == -1) 486 mtree_err("unknown group `%s'", val); 487 ip->st_gid = gid; 488 break; 489 case F_IGN: 490 /* just set flag bit */ 491 break; 492 case F_MD5: 493 if (val[0]=='0' && val[1]=='x') 494 md=&val[2]; 495 else 496 md=val; 497 if ((ip->md5digest = strdup(md)) == NULL) 498 mtree_err("memory allocation error"); 499 break; 500 case F_MODE: 501 if ((m = setmode(val)) == NULL) 502 mtree_err("invalid file mode `%s'", val); 503 ip->st_mode = getmode(m, 0); 504 free(m); 505 break; 506 case F_NLINK: 507 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 508 if (*ep) 509 mtree_err("invalid link count `%s'", val); 510 break; 511 case F_OPT: 512 /* just set flag bit */ 513 break; 514 case F_RMD160: 515 if (val[0]=='0' && val[1]=='x') 516 md=&val[2]; 517 else 518 md=val; 519 if ((ip->rmd160digest = strdup(md)) == NULL) 520 mtree_err("memory allocation error"); 521 break; 522 case F_SHA1: 523 if (val[0]=='0' && val[1]=='x') 524 md=&val[2]; 525 else 526 md=val; 527 if ((ip->sha1digest = strdup(md)) == NULL) 528 mtree_err("memory allocation error"); 529 break; 530 case F_SIZE: 531 ip->st_size = (off_t)strtoll(val, &ep, 10); 532 if (*ep) 533 mtree_err("invalid size `%s'", val); 534 break; 535 case F_SLINK: 536 if ((ip->slink = strdup(val)) == NULL) 537 mtree_err("memory allocation error"); 538 break; 539 case F_TAGS: 540 len = strlen(val) + 3; /* "," + str + ",\0" */ 541 if ((ip->tags = malloc(len)) == NULL) 542 mtree_err("memory allocation error"); 543 snprintf(ip->tags, len, ",%s,", val); 544 break; 545 case F_TIME: 546 ip->st_mtimespec.tv_sec = 547 (time_t)strtoul(val, &ep, 10); 548 if (*ep != '.') 549 mtree_err("invalid time `%s'", val); 550 val = ep + 1; 551 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); 552 if (*ep) 553 mtree_err("invalid time `%s'", val); 554 break; 555 case F_TYPE: 556 ip->type = parsetype(val); 557 break; 558 case F_UID: 559 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 560 if (*ep) 561 mtree_err("invalid uid `%s'", val); 562 break; 563 case F_UNAME: 564 if (Wflag) /* don't parse if whacking */ 565 break; 566 if (uid_from_user(val, &uid) == -1) 567 mtree_err("unknown user `%s'", val); 568 ip->st_uid = uid; 569 break; 570 default: 571 mtree_err( 572 "set(): unsupported key type 0x%x (INTERNAL ERROR)", 573 type); 574 /* NOTREACHED */ 575 } 576 } 577} 578 579static void 580unset(char *t, NODE *ip) 581{ 582 char *p; 583 584 while ((p = strsep(&t, " \t")) != NULL) { 585 if (*p == '\0') 586 continue; 587 ip->flags &= ~parsekey(p, NULL); 588 } 589} 590