spec.c revision 1.67
1/* $NetBSD: spec.c,v 1.67 2008/12/28 19:35:12 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/*- 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.67 2008/12/28 19:35:12 christos 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=%#llx ", (long long)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=%lld.%ld ", 353 (long long)cur->st_mtimespec.tv_sec, 354 cur->st_mtimespec.tv_nsec); 355 if (MATCHFLAG(F_CKSUM)) 356 printf("cksum=%lu ", cur->cksum); 357 if (MATCHFLAG(F_MD5)) 358 printf("md5=%s ", cur->md5digest); 359 if (MATCHFLAG(F_RMD160)) 360 printf("rmd160=%s ", cur->rmd160digest); 361 if (MATCHFLAG(F_SHA1)) 362 printf("sha1=%s ", cur->sha1digest); 363 if (MATCHFLAG(F_SHA256)) 364 printf("sha256=%s ", cur->sha256digest); 365 if (MATCHFLAG(F_SHA384)) 366 printf("sha384=%s ", cur->sha384digest); 367 if (MATCHFLAG(F_SHA512)) 368 printf("sha512=%s ", cur->sha512digest); 369 if (MATCHFLAG(F_FLAGS)) { 370 str = flags_to_string(cur->st_flags, "none"); 371 printf("flags=%s ", str); 372 free(str); 373 } 374 if (MATCHFLAG(F_IGN)) 375 printf("ignore "); 376 if (MATCHFLAG(F_OPT)) 377 printf("optional "); 378 if (MATCHFLAG(F_TAGS)) 379 printf("tags=%s ", cur->tags); 380 puts(pathlast ? vispath(path) : ""); 381 382 if (cur->child) 383 dump_nodes(path, cur->child, pathlast); 384 } 385} 386 387/* 388 * vispath -- 389 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN 390 * characters long, and returns a pointer to a static buffer containing 391 * the result. 392 */ 393char * 394vispath(const char *path) 395{ 396 const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' }; 397 static char pathbuf[4*MAXPATHLEN + 1]; 398 399 strsvis(pathbuf, path, VIS_CSTYLE, extra); 400 return(pathbuf); 401} 402 403 404static dev_t 405parsedev(char *arg) 406{ 407#define MAX_PACK_ARGS 3 408 u_long numbers[MAX_PACK_ARGS]; 409 char *p, *ep, *dev; 410 int argc; 411 pack_t *pack; 412 dev_t result; 413 const char *error = NULL; 414 415 if ((dev = strchr(arg, ',')) != NULL) { 416 *dev++='\0'; 417 if ((pack = pack_find(arg)) == NULL) 418 mtree_err("unknown format `%s'", arg); 419 argc = 0; 420 while ((p = strsep(&dev, ",")) != NULL) { 421 if (*p == '\0') 422 mtree_err("missing number"); 423 numbers[argc++] = strtoul(p, &ep, 0); 424 if (*ep != '\0') 425 mtree_err("invalid number `%s'", 426 p); 427 if (argc > MAX_PACK_ARGS) 428 mtree_err("too many arguments"); 429 } 430 if (argc < 2) 431 mtree_err("not enough arguments"); 432 result = (*pack)(argc, numbers, &error); 433 if (error != NULL) 434 mtree_err("%s", error); 435 } else { 436 result = (dev_t)strtoul(arg, &ep, 0); 437 if (*ep != '\0') 438 mtree_err("invalid device `%s'", arg); 439 } 440 return (result); 441} 442 443static void 444replacenode(NODE *cur, NODE *new) 445{ 446 447#define REPLACE(x) cur->x = new->x 448#define REPLACESTR(x) REPLACEPTR(cur->x,new->x) 449 450 if (cur->type != new->type) { 451 if (mtree_Mflag) { 452 /* 453 * merge entries with different types; we 454 * don't want children retained in this case. 455 */ 456 REPLACE(type); 457 free_nodes(cur->child); 458 cur->child = NULL; 459 } else { 460 mtree_err( 461 "existing entry for `%s', type `%s'" 462 " does not match type `%s'", 463 cur->name, nodetype(cur->type), 464 nodetype(new->type)); 465 } 466 } 467 468 REPLACE(st_size); 469 REPLACE(st_mtimespec); 470 REPLACESTR(slink); 471 if (cur->slink != NULL) { 472 if ((cur->slink = strdup(new->slink)) == NULL) 473 mtree_err("memory allocation error"); 474 if (strunvis(cur->slink, new->slink) == -1) 475 mtree_err("strunvis failed on `%s'", new->slink); 476 free(new->slink); 477 } 478 REPLACE(st_uid); 479 REPLACE(st_gid); 480 REPLACE(st_mode); 481 REPLACE(st_rdev); 482 REPLACE(st_flags); 483 REPLACE(st_nlink); 484 REPLACE(cksum); 485 REPLACESTR(md5digest); 486 REPLACESTR(rmd160digest); 487 REPLACESTR(sha1digest); 488 REPLACESTR(sha256digest); 489 REPLACESTR(sha384digest); 490 REPLACESTR(sha512digest); 491 REPLACESTR(tags); 492 REPLACE(lineno); 493 REPLACE(flags); 494 free(new); 495} 496 497static void 498set(char *t, NODE *ip) 499{ 500 int type, value, len; 501 gid_t gid; 502 uid_t uid; 503 char *kw, *val, *md, *ep; 504 void *m; 505 506 while ((kw = strsep(&t, "= \t")) != NULL) { 507 if (*kw == '\0') 508 continue; 509 if (strcmp(kw, "all") == 0) 510 mtree_err("invalid keyword `all'"); 511 ip->flags |= type = parsekey(kw, &value); 512 if (!value) 513 /* Just set flag bit (F_IGN and F_OPT) */ 514 continue; 515 while ((val = strsep(&t, " \t")) != NULL && *val == '\0') 516 continue; 517 if (val == NULL) 518 mtree_err("missing value"); 519 switch (type) { 520 case F_CKSUM: 521 ip->cksum = strtoul(val, &ep, 10); 522 if (*ep) 523 mtree_err("invalid checksum `%s'", val); 524 break; 525 case F_DEV: 526 ip->st_rdev = parsedev(val); 527 break; 528 case F_FLAGS: 529 if (strcmp("none", val) == 0) 530 ip->st_flags = 0; 531 else if (string_to_flags(&val, &ip->st_flags, NULL) 532 != 0) 533 mtree_err("invalid flag `%s'", val); 534 break; 535 case F_GID: 536 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 537 if (*ep) 538 mtree_err("invalid gid `%s'", val); 539 break; 540 case F_GNAME: 541 if (mtree_Wflag) /* don't parse if whacking */ 542 break; 543 if (gid_from_group(val, &gid) == -1) 544 mtree_err("unknown group `%s'", val); 545 ip->st_gid = gid; 546 break; 547 case F_MD5: 548 if (val[0]=='0' && val[1]=='x') 549 md=&val[2]; 550 else 551 md=val; 552 if ((ip->md5digest = strdup(md)) == NULL) 553 mtree_err("memory allocation error"); 554 break; 555 case F_MODE: 556 if ((m = setmode(val)) == NULL) 557 mtree_err("cannot set file mode `%s' (%s)", 558 val, strerror(errno)); 559 ip->st_mode = getmode(m, 0); 560 free(m); 561 break; 562 case F_NLINK: 563 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 564 if (*ep) 565 mtree_err("invalid link count `%s'", val); 566 break; 567 case F_RMD160: 568 if (val[0]=='0' && val[1]=='x') 569 md=&val[2]; 570 else 571 md=val; 572 if ((ip->rmd160digest = strdup(md)) == NULL) 573 mtree_err("memory allocation error"); 574 break; 575 case F_SHA1: 576 if (val[0]=='0' && val[1]=='x') 577 md=&val[2]; 578 else 579 md=val; 580 if ((ip->sha1digest = strdup(md)) == NULL) 581 mtree_err("memory allocation error"); 582 break; 583 case F_SIZE: 584 ip->st_size = (off_t)strtoll(val, &ep, 10); 585 if (*ep) 586 mtree_err("invalid size `%s'", val); 587 break; 588 case F_SLINK: 589 if ((ip->slink = strdup(val)) == NULL) 590 mtree_err("memory allocation error"); 591 if (strunvis(ip->slink, val) == -1) 592 mtree_err("strunvis failed on `%s'", val); 593 break; 594 case F_TAGS: 595 len = strlen(val) + 3; /* "," + str + ",\0" */ 596 if ((ip->tags = malloc(len)) == NULL) 597 mtree_err("memory allocation error"); 598 snprintf(ip->tags, len, ",%s,", val); 599 break; 600 case F_TIME: 601 ip->st_mtimespec.tv_sec = 602 (time_t)strtoll(val, &ep, 10); 603 if (*ep != '.') 604 mtree_err("invalid time `%s'", val); 605 val = ep + 1; 606 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10); 607 if (*ep) 608 mtree_err("invalid time `%s'", val); 609 break; 610 case F_TYPE: 611 ip->type = parsetype(val); 612 break; 613 case F_UID: 614 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 615 if (*ep) 616 mtree_err("invalid uid `%s'", val); 617 break; 618 case F_UNAME: 619 if (mtree_Wflag) /* don't parse if whacking */ 620 break; 621 if (uid_from_user(val, &uid) == -1) 622 mtree_err("unknown user `%s'", val); 623 ip->st_uid = uid; 624 break; 625 case F_SHA256: 626 if (val[0]=='0' && val[1]=='x') 627 md=&val[2]; 628 else 629 md=val; 630 if ((ip->sha256digest = strdup(md)) == NULL) 631 mtree_err("memory allocation error"); 632 break; 633 case F_SHA384: 634 if (val[0]=='0' && val[1]=='x') 635 md=&val[2]; 636 else 637 md=val; 638 if ((ip->sha384digest = strdup(md)) == NULL) 639 mtree_err("memory allocation error"); 640 break; 641 case F_SHA512: 642 if (val[0]=='0' && val[1]=='x') 643 md=&val[2]; 644 else 645 md=val; 646 if ((ip->sha512digest = strdup(md)) == NULL) 647 mtree_err("memory allocation error"); 648 break; 649 default: 650 mtree_err( 651 "set(): unsupported key type 0x%x (INTERNAL ERROR)", 652 type); 653 /* NOTREACHED */ 654 } 655 } 656} 657 658static void 659unset(char *t, NODE *ip) 660{ 661 char *p; 662 663 while ((p = strsep(&t, " \t")) != NULL) { 664 if (*p == '\0') 665 continue; 666 ip->flags &= ~parsekey(p, NULL); 667 } 668} 669