spec.c revision 1.65
1/* $NetBSD: spec.c,v 1.65 2008/04/28 20:24:17 martin 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-2004 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 * 48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 58 * POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61#if HAVE_NBTOOL_CONFIG_H 62#include "nbtool_config.h" 63#endif 64 65#include <sys/cdefs.h> 66#if defined(__RCSID) && !defined(lint) 67#if 0 68static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95"; 69#else 70__RCSID("$NetBSD: spec.c,v 1.65 2008/04/28 20:24:17 martin Exp $"); 71#endif 72#endif /* not lint */ 73 74#include <sys/param.h> 75#include <sys/stat.h> 76 77#include <ctype.h> 78#include <errno.h> 79#include <grp.h> 80#include <pwd.h> 81#include <stdio.h> 82#include <stdlib.h> 83#include <string.h> 84#include <unistd.h> 85#include <vis.h> 86#include <util.h> 87 88#include "extern.h" 89#include "pack_dev.h" 90 91size_t mtree_lineno; /* Current spec line number */ 92int mtree_Mflag; /* Merge duplicate entries */ 93int mtree_Wflag; /* Don't "whack" permissions */ 94 95static dev_t parsedev(char *); 96static void replacenode(NODE *, NODE *); 97static void set(char *, NODE *); 98static void unset(char *, NODE *); 99 100#define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0) 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)); 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 273void 274free_nodes(NODE *root) 275{ 276 NODE *cur, *next; 277 278 if (root == NULL) 279 return; 280 281 next = NULL; 282 for (cur = root; cur != NULL; cur = next) { 283 next = cur->next; 284 free_nodes(cur->child); 285 REPLACEPTR(cur->slink, NULL); 286 REPLACEPTR(cur->md5digest, NULL); 287 REPLACEPTR(cur->rmd160digest, NULL); 288 REPLACEPTR(cur->sha1digest, NULL); 289 REPLACEPTR(cur->sha256digest, NULL); 290 REPLACEPTR(cur->sha384digest, NULL); 291 REPLACEPTR(cur->sha512digest, NULL); 292 REPLACEPTR(cur->tags, NULL); 293 REPLACEPTR(cur, NULL); 294 } 295} 296 297/* 298 * dump_nodes -- 299 * dump the NODEs from `cur', based in the directory `dir'. 300 * if pathlast is none zero, print the path last, otherwise print 301 * it first. 302 */ 303void 304dump_nodes(const char *dir, NODE *root, int pathlast) 305{ 306 NODE *cur; 307 char path[MAXPATHLEN]; 308 const char *name; 309 char *str; 310 311 for (cur = root; cur != NULL; cur = cur->next) { 312 if (cur->type != F_DIR && !matchtags(cur)) 313 continue; 314 315 if (snprintf(path, sizeof(path), "%s%s%s", 316 dir, *dir ? "/" : "", cur->name) 317 >= sizeof(path)) 318 mtree_err("Pathname too long."); 319 320 if (!pathlast) 321 printf("%s ", vispath(path)); 322 323#define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f))) 324 if (MATCHFLAG(F_TYPE)) 325 printf("type=%s ", nodetype(cur->type)); 326 if (MATCHFLAG(F_UID | F_UNAME)) { 327 if (keys & F_UNAME && 328 (name = user_from_uid(cur->st_uid, 1)) != NULL) 329 printf("uname=%s ", name); 330 else 331 printf("uid=%u ", cur->st_uid); 332 } 333 if (MATCHFLAG(F_GID | F_GNAME)) { 334 if (keys & F_GNAME && 335 (name = group_from_gid(cur->st_gid, 1)) != NULL) 336 printf("gname=%s ", name); 337 else 338 printf("gid=%u ", cur->st_gid); 339 } 340 if (MATCHFLAG(F_MODE)) 341 printf("mode=%#o ", cur->st_mode); 342 if (MATCHFLAG(F_DEV) && 343 (cur->type == F_BLOCK || cur->type == F_CHAR)) 344 printf("device=%#x ", cur->st_rdev); 345 if (MATCHFLAG(F_NLINK)) 346 printf("nlink=%d ", cur->st_nlink); 347 if (MATCHFLAG(F_SLINK)) 348 printf("link=%s ", vispath(cur->slink)); 349 if (MATCHFLAG(F_SIZE)) 350 printf("size=%lld ", (long long)cur->st_size); 351 if (MATCHFLAG(F_TIME)) 352 printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec, 353 cur->st_mtimespec.tv_nsec); 354 if (MATCHFLAG(F_CKSUM)) 355 printf("cksum=%lu ", cur->cksum); 356 if (MATCHFLAG(F_MD5)) 357 printf("md5=%s ", cur->md5digest); 358 if (MATCHFLAG(F_RMD160)) 359 printf("rmd160=%s ", cur->rmd160digest); 360 if (MATCHFLAG(F_SHA1)) 361 printf("sha1=%s ", cur->sha1digest); 362 if (MATCHFLAG(F_SHA256)) 363 printf("sha256=%s ", cur->sha256digest); 364 if (MATCHFLAG(F_SHA384)) 365 printf("sha384=%s ", cur->sha384digest); 366 if (MATCHFLAG(F_SHA512)) 367 printf("sha512=%s ", cur->sha512digest); 368 if (MATCHFLAG(F_FLAGS)) { 369 str = flags_to_string(cur->st_flags, "none"); 370 printf("flags=%s ", str); 371 free(str); 372 } 373 if (MATCHFLAG(F_IGN)) 374 printf("ignore "); 375 if (MATCHFLAG(F_OPT)) 376 printf("optional "); 377 if (MATCHFLAG(F_TAGS)) 378 printf("tags=%s ", cur->tags); 379 puts(pathlast ? vispath(path) : ""); 380 381 if (cur->child) 382 dump_nodes(path, cur->child, pathlast); 383 } 384} 385 386/* 387 * vispath -- 388 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN 389 * characters long, and returns a pointer to a static buffer containing 390 * the result. 391 */ 392char * 393vispath(const char *path) 394{ 395 const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' }; 396 static char pathbuf[4*MAXPATHLEN + 1]; 397 398 strsvis(pathbuf, path, VIS_CSTYLE, extra); 399 return(pathbuf); 400} 401 402 403static dev_t 404parsedev(char *arg) 405{ 406#define MAX_PACK_ARGS 3 407 u_long numbers[MAX_PACK_ARGS]; 408 char *p, *ep, *dev; 409 int argc; 410 pack_t *pack; 411 dev_t result; 412 const char *error = NULL; 413 414 if ((dev = strchr(arg, ',')) != NULL) { 415 *dev++='\0'; 416 if ((pack = pack_find(arg)) == NULL) 417 mtree_err("unknown format `%s'", arg); 418 argc = 0; 419 while ((p = strsep(&dev, ",")) != NULL) { 420 if (*p == '\0') 421 mtree_err("missing number"); 422 numbers[argc++] = strtoul(p, &ep, 0); 423 if (*ep != '\0') 424 mtree_err("invalid number `%s'", 425 p); 426 if (argc > MAX_PACK_ARGS) 427 mtree_err("too many arguments"); 428 } 429 if (argc < 2) 430 mtree_err("not enough arguments"); 431 result = (*pack)(argc, numbers, &error); 432 if (error != NULL) 433 mtree_err(error); 434 } else { 435 result = (dev_t)strtoul(arg, &ep, 0); 436 if (*ep != '\0') 437 mtree_err("invalid device `%s'", arg); 438 } 439 return (result); 440} 441 442static void 443replacenode(NODE *cur, NODE *new) 444{ 445 446#define REPLACE(x) cur->x = new->x 447#define REPLACESTR(x) REPLACEPTR(cur->x,new->x) 448 449 if (cur->type != new->type) { 450 if (mtree_Mflag) { 451 /* 452 * merge entries with different types; we 453 * don't want children retained in this case. 454 */ 455 REPLACE(type); 456 free_nodes(cur->child); 457 cur->child = NULL; 458 } else { 459 mtree_err( 460 "existing entry for `%s', type `%s'" 461 " does not match type `%s'", 462 cur->name, nodetype(cur->type), 463 nodetype(new->type)); 464 } 465 } 466 467 REPLACE(st_size); 468 REPLACE(st_mtimespec); 469 REPLACESTR(slink); 470 if (cur->slink != NULL) { 471 if ((cur->slink = strdup(new->slink)) == NULL) 472 mtree_err("memory allocation error"); 473 if (strunvis(cur->slink, new->slink) == -1) 474 mtree_err("strunvis failed on `%s'", new->slink); 475 free(new->slink); 476 } 477 REPLACE(st_uid); 478 REPLACE(st_gid); 479 REPLACE(st_mode); 480 REPLACE(st_rdev); 481 REPLACE(st_flags); 482 REPLACE(st_nlink); 483 REPLACE(cksum); 484 REPLACESTR(md5digest); 485 REPLACESTR(rmd160digest); 486 REPLACESTR(sha1digest); 487 REPLACESTR(sha256digest); 488 REPLACESTR(sha384digest); 489 REPLACESTR(sha512digest); 490 REPLACESTR(tags); 491 REPLACE(lineno); 492 REPLACE(flags); 493 free(new); 494} 495 496static void 497set(char *t, NODE *ip) 498{ 499 int type, value, len; 500 gid_t gid; 501 uid_t uid; 502 char *kw, *val, *md, *ep; 503 void *m; 504 505 while ((kw = strsep(&t, "= \t")) != NULL) { 506 if (*kw == '\0') 507 continue; 508 if (strcmp(kw, "all") == 0) 509 mtree_err("invalid keyword `all'"); 510 ip->flags |= type = parsekey(kw, &value); 511 if (!value) 512 /* Just set flag bit (F_IGN and F_OPT) */ 513 continue; 514 while ((val = strsep(&t, " \t")) != NULL && *val == '\0') 515 continue; 516 if (val == NULL) 517 mtree_err("missing value"); 518 switch (type) { 519 case F_CKSUM: 520 ip->cksum = strtoul(val, &ep, 10); 521 if (*ep) 522 mtree_err("invalid checksum `%s'", val); 523 break; 524 case F_DEV: 525 ip->st_rdev = parsedev(val); 526 break; 527 case F_FLAGS: 528 if (strcmp("none", val) == 0) 529 ip->st_flags = 0; 530 else if (string_to_flags(&val, &ip->st_flags, NULL) 531 != 0) 532 mtree_err("invalid flag `%s'", val); 533 break; 534 case F_GID: 535 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 536 if (*ep) 537 mtree_err("invalid gid `%s'", val); 538 break; 539 case F_GNAME: 540 if (mtree_Wflag) /* don't parse if whacking */ 541 break; 542 if (gid_from_group(val, &gid) == -1) 543 mtree_err("unknown group `%s'", val); 544 ip->st_gid = gid; 545 break; 546 case F_MD5: 547 if (val[0]=='0' && val[1]=='x') 548 md=&val[2]; 549 else 550 md=val; 551 if ((ip->md5digest = strdup(md)) == NULL) 552 mtree_err("memory allocation error"); 553 break; 554 case F_MODE: 555 if ((m = setmode(val)) == NULL) 556 mtree_err("cannot set file mode `%s' (%s)", 557 val, strerror(errno)); 558 ip->st_mode = getmode(m, 0); 559 free(m); 560 break; 561 case F_NLINK: 562 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 563 if (*ep) 564 mtree_err("invalid link count `%s'", val); 565 break; 566 case F_RMD160: 567 if (val[0]=='0' && val[1]=='x') 568 md=&val[2]; 569 else 570 md=val; 571 if ((ip->rmd160digest = strdup(md)) == NULL) 572 mtree_err("memory allocation error"); 573 break; 574 case F_SHA1: 575 if (val[0]=='0' && val[1]=='x') 576 md=&val[2]; 577 else 578 md=val; 579 if ((ip->sha1digest = strdup(md)) == NULL) 580 mtree_err("memory allocation error"); 581 break; 582 case F_SIZE: 583 ip->st_size = (off_t)strtoll(val, &ep, 10); 584 if (*ep) 585 mtree_err("invalid size `%s'", val); 586 break; 587 case F_SLINK: 588 if ((ip->slink = strdup(val)) == NULL) 589 mtree_err("memory allocation error"); 590 if (strunvis(ip->slink, val) == -1) 591 mtree_err("strunvis failed on `%s'", val); 592 break; 593 case F_TAGS: 594 len = strlen(val) + 3; /* "," + str + ",\0" */ 595 if ((ip->tags = malloc(len)) == NULL) 596 mtree_err("memory allocation error"); 597 snprintf(ip->tags, len, ",%s,", val); 598 break; 599 case F_TIME: 600 ip->st_mtimespec.tv_sec = 601 (time_t)strtoul(val, &ep, 10); 602 if (*ep != '.') 603 mtree_err("invalid time `%s'", val); 604 val = ep + 1; 605 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); 606 if (*ep) 607 mtree_err("invalid time `%s'", val); 608 break; 609 case F_TYPE: 610 ip->type = parsetype(val); 611 break; 612 case F_UID: 613 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 614 if (*ep) 615 mtree_err("invalid uid `%s'", val); 616 break; 617 case F_UNAME: 618 if (mtree_Wflag) /* don't parse if whacking */ 619 break; 620 if (uid_from_user(val, &uid) == -1) 621 mtree_err("unknown user `%s'", val); 622 ip->st_uid = uid; 623 break; 624 case F_SHA256: 625 if (val[0]=='0' && val[1]=='x') 626 md=&val[2]; 627 else 628 md=val; 629 if ((ip->sha256digest = strdup(md)) == NULL) 630 mtree_err("memory allocation error"); 631 break; 632 case F_SHA384: 633 if (val[0]=='0' && val[1]=='x') 634 md=&val[2]; 635 else 636 md=val; 637 if ((ip->sha384digest = strdup(md)) == NULL) 638 mtree_err("memory allocation error"); 639 break; 640 case F_SHA512: 641 if (val[0]=='0' && val[1]=='x') 642 md=&val[2]; 643 else 644 md=val; 645 if ((ip->sha512digest = strdup(md)) == NULL) 646 mtree_err("memory allocation error"); 647 break; 648 default: 649 mtree_err( 650 "set(): unsupported key type 0x%x (INTERNAL ERROR)", 651 type); 652 /* NOTREACHED */ 653 } 654 } 655} 656 657static void 658unset(char *t, NODE *ip) 659{ 660 char *p; 661 662 while ((p = strsep(&t, " \t")) != NULL) { 663 if (*p == '\0') 664 continue; 665 ip->flags &= ~parsekey(p, NULL); 666 } 667} 668