1 /* $NetBSD: spec.c,v 1.64 2006/12/14 20:09:36 he 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 * 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 #if HAVE_NBTOOL_CONFIG_H 69 #include "nbtool_config.h" 70 #endif 71 72 #include <sys/cdefs.h> 73 #if defined(__RCSID) && !defined(lint) 74 #if 0 75 static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95"; 76 #else 77 __RCSID("$NetBSD: spec.c,v 1.64 2006/12/14 20:09:36 he Exp $"); 78 #endif 79 #endif /* not lint */ 80 81 #include <sys/param.h> 82 #include <sys/stat.h> 83 84 #include <ctype.h> 85 #include <errno.h> 86 #include <grp.h> 87 #include <pwd.h> 88 #include <stdio.h> 89 #include <stdlib.h> 90 #include <string.h> 91 #include <unistd.h> 92 #include <vis.h> 93 #include <util.h> 94 95 #include "extern.h" 96 #include "pack_dev.h" 97 98 size_t mtree_lineno; /* Current spec line number */ 99 int mtree_Mflag; /* Merge duplicate entries */ 100 int mtree_Wflag; /* Don't "whack" permissions */ 101 102 static dev_t parsedev(char *); 103 static void replacenode(NODE *, NODE *); 104 static void set(char *, NODE *); 105 static void unset(char *, NODE *); 106 107 #define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0) 108 109 NODE * 110 spec(FILE *fp) 111 { 112 NODE *centry, *last, *pathparent, *cur; 113 char *p, *e, *next; 114 NODE ginfo, *root; 115 char *buf, *tname, *ntname; 116 size_t tnamelen, plen; 117 118 root = NULL; 119 centry = last = NULL; 120 tname = NULL; 121 tnamelen = 0; 122 memset(&ginfo, 0, sizeof(ginfo)); 123 for (mtree_lineno = 0; 124 (buf = fparseln(fp, NULL, &mtree_lineno, NULL, 125 FPARSELN_UNESCCOMM)); 126 free(buf)) { 127 /* Skip leading whitespace. */ 128 for (p = buf; *p && isspace((unsigned char)*p); ++p) 129 continue; 130 131 /* If nothing but whitespace, continue. */ 132 if (!*p) 133 continue; 134 135 #ifdef DEBUG 136 fprintf(stderr, "line %lu: {%s}\n", 137 (u_long)mtree_lineno, p); 138 #endif 139 /* Grab file name, "$", "set", or "unset". */ 140 next = buf; 141 while ((p = strsep(&next, " \t")) != NULL && *p == '\0') 142 continue; 143 if (p == NULL) 144 mtree_err("missing field"); 145 146 if (p[0] == '/') { 147 if (strcmp(p + 1, "set") == 0) 148 set(next, &ginfo); 149 else if (strcmp(p + 1, "unset") == 0) 150 unset(next, &ginfo); 151 else 152 mtree_err("invalid specification `%s'", p); 153 continue; 154 } 155 156 if (strcmp(p, "..") == 0) { 157 /* Don't go up, if haven't gone down. */ 158 if (root == NULL) 159 goto noparent; 160 if (last->type != F_DIR || last->flags & F_DONE) { 161 if (last == root) 162 goto noparent; 163 last = last->parent; 164 } 165 last->flags |= F_DONE; 166 continue; 167 168 noparent: mtree_err("no parent node"); 169 } 170 171 plen = strlen(p) + 1; 172 if (plen > tnamelen) { 173 if ((ntname = realloc(tname, plen)) == NULL) 174 mtree_err("realloc: %s", strerror(errno)); 175 tname = ntname; 176 tnamelen = plen; 177 } 178 if (strunvis(tname, p) == -1) 179 mtree_err("strunvis failed on `%s'", p); 180 p = tname; 181 182 pathparent = NULL; 183 if (strchr(p, '/') != NULL) { 184 cur = root; 185 for (; (e = strchr(p, '/')) != NULL; p = e+1) { 186 if (p == e) 187 continue; /* handle // */ 188 *e = '\0'; 189 if (strcmp(p, ".") != 0) { 190 while (cur && 191 strcmp(cur->name, p) != 0) { 192 cur = cur->next; 193 } 194 } 195 if (cur == NULL || cur->type != F_DIR) { 196 mtree_err("%s: %s", tname, 197 strerror(ENOENT)); 198 } 199 *e = '/'; 200 pathparent = cur; 201 cur = cur->child; 202 } 203 if (*p == '\0') 204 mtree_err("%s: empty leaf element", tname); 205 } 206 207 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL) 208 mtree_err("%s", strerror(errno)); 209 *centry = ginfo; 210 centry->lineno = mtree_lineno; 211 strcpy(centry->name, p); 212 #define MAGIC "?*[" 213 if (strpbrk(p, MAGIC)) 214 centry->flags |= F_MAGIC; 215 set(next, centry); 216 217 if (root == NULL) { 218 /* 219 * empty tree 220 */ 221 if (strcmp(centry->name, ".") != 0 || 222 centry->type != F_DIR) 223 mtree_err( 224 "root node must be the directory `.'"); 225 last = root = centry; 226 root->parent = root; 227 } else if (pathparent != NULL) { 228 /* 229 * full path entry 230 */ 231 centry->parent = pathparent; 232 cur = pathparent->child; 233 if (cur == NULL) { 234 pathparent->child = centry; 235 last = centry; 236 } else { 237 for (; cur != NULL; cur = cur->next) { 238 if (strcmp(cur->name, centry->name) 239 == 0) { 240 /* existing entry; replace */ 241 replacenode(cur, centry); 242 break; 243 } 244 if (cur->next == NULL) { 245 /* last entry; add new */ 246 cur->next = centry; 247 centry->prev = cur; 248 break; 249 } 250 } 251 last = cur; 252 while (last->next != NULL) 253 last = last->next; 254 } 255 } else if (strcmp(centry->name, ".") == 0) { 256 /* 257 * duplicate "." entry; always replace 258 */ 259 replacenode(root, centry); 260 } else if (last->type == F_DIR && !(last->flags & F_DONE)) { 261 /* 262 * new relative child 263 * (no duplicate check) 264 */ 265 centry->parent = last; 266 last = last->child = centry; 267 } else { 268 /* 269 * relative entry, up one directory 270 * (no duplicate check) 271 */ 272 centry->parent = last->parent; 273 centry->prev = last; 274 last = last->next = centry; 275 } 276 } 277 return (root); 278 } 279 280 void 281 free_nodes(NODE *root) 282 { 283 NODE *cur, *next; 284 285 if (root == NULL) 286 return; 287 288 next = NULL; 289 for (cur = root; cur != NULL; cur = next) { 290 next = cur->next; 291 free_nodes(cur->child); 292 REPLACEPTR(cur->slink, NULL); 293 REPLACEPTR(cur->md5digest, NULL); 294 REPLACEPTR(cur->rmd160digest, NULL); 295 REPLACEPTR(cur->sha1digest, NULL); 296 REPLACEPTR(cur->sha256digest, NULL); 297 REPLACEPTR(cur->sha384digest, NULL); 298 REPLACEPTR(cur->sha512digest, NULL); 299 REPLACEPTR(cur->tags, NULL); 300 REPLACEPTR(cur, NULL); 301 } 302 } 303 304 /* 305 * dump_nodes -- 306 * dump the NODEs from `cur', based in the directory `dir'. 307 * if pathlast is none zero, print the path last, otherwise print 308 * it first. 309 */ 310 void 311 dump_nodes(const char *dir, NODE *root, int pathlast) 312 { 313 NODE *cur; 314 char path[MAXPATHLEN]; 315 const char *name; 316 char *str; 317 318 for (cur = root; cur != NULL; cur = cur->next) { 319 if (cur->type != F_DIR && !matchtags(cur)) 320 continue; 321 322 if (snprintf(path, sizeof(path), "%s%s%s", 323 dir, *dir ? "/" : "", cur->name) 324 >= sizeof(path)) 325 mtree_err("Pathname too long."); 326 327 if (!pathlast) 328 printf("%s ", vispath(path)); 329 330 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f))) 331 if (MATCHFLAG(F_TYPE)) 332 printf("type=%s ", nodetype(cur->type)); 333 if (MATCHFLAG(F_UID | F_UNAME)) { 334 if (keys & F_UNAME && 335 (name = user_from_uid(cur->st_uid, 1)) != NULL) 336 printf("uname=%s ", name); 337 else 338 printf("uid=%u ", cur->st_uid); 339 } 340 if (MATCHFLAG(F_GID | F_GNAME)) { 341 if (keys & F_GNAME && 342 (name = group_from_gid(cur->st_gid, 1)) != NULL) 343 printf("gname=%s ", name); 344 else 345 printf("gid=%u ", cur->st_gid); 346 } 347 if (MATCHFLAG(F_MODE)) 348 printf("mode=%#o ", cur->st_mode); 349 if (MATCHFLAG(F_DEV) && 350 (cur->type == F_BLOCK || cur->type == F_CHAR)) 351 printf("device=%#x ", cur->st_rdev); 352 if (MATCHFLAG(F_NLINK)) 353 printf("nlink=%d ", cur->st_nlink); 354 if (MATCHFLAG(F_SLINK)) 355 printf("link=%s ", vispath(cur->slink)); 356 if (MATCHFLAG(F_SIZE)) 357 printf("size=%lld ", (long long)cur->st_size); 358 if (MATCHFLAG(F_TIME)) 359 printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec, 360 cur->st_mtimespec.tv_nsec); 361 if (MATCHFLAG(F_CKSUM)) 362 printf("cksum=%lu ", cur->cksum); 363 if (MATCHFLAG(F_MD5)) 364 printf("md5=%s ", cur->md5digest); 365 if (MATCHFLAG(F_RMD160)) 366 printf("rmd160=%s ", cur->rmd160digest); 367 if (MATCHFLAG(F_SHA1)) 368 printf("sha1=%s ", cur->sha1digest); 369 if (MATCHFLAG(F_SHA256)) 370 printf("sha256=%s ", cur->sha256digest); 371 if (MATCHFLAG(F_SHA384)) 372 printf("sha384=%s ", cur->sha384digest); 373 if (MATCHFLAG(F_SHA512)) 374 printf("sha512=%s ", cur->sha512digest); 375 if (MATCHFLAG(F_FLAGS)) { 376 str = flags_to_string(cur->st_flags, "none"); 377 printf("flags=%s ", str); 378 free(str); 379 } 380 if (MATCHFLAG(F_IGN)) 381 printf("ignore "); 382 if (MATCHFLAG(F_OPT)) 383 printf("optional "); 384 if (MATCHFLAG(F_TAGS)) 385 printf("tags=%s ", cur->tags); 386 puts(pathlast ? vispath(path) : ""); 387 388 if (cur->child) 389 dump_nodes(path, cur->child, pathlast); 390 } 391 } 392 393 /* 394 * vispath -- 395 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN 396 * characters long, and returns a pointer to a static buffer containing 397 * the result. 398 */ 399 char * 400 vispath(const char *path) 401 { 402 const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' }; 403 static char pathbuf[4*MAXPATHLEN + 1]; 404 405 strsvis(pathbuf, path, VIS_CSTYLE, extra); 406 return(pathbuf); 407 } 408 409 410 static dev_t 411 parsedev(char *arg) 412 { 413 #define MAX_PACK_ARGS 3 414 u_long numbers[MAX_PACK_ARGS]; 415 char *p, *ep, *dev; 416 int argc; 417 pack_t *pack; 418 dev_t result; 419 const char *error = NULL; 420 421 if ((dev = strchr(arg, ',')) != NULL) { 422 *dev++='\0'; 423 if ((pack = pack_find(arg)) == NULL) 424 mtree_err("unknown format `%s'", arg); 425 argc = 0; 426 while ((p = strsep(&dev, ",")) != NULL) { 427 if (*p == '\0') 428 mtree_err("missing number"); 429 numbers[argc++] = strtoul(p, &ep, 0); 430 if (*ep != '\0') 431 mtree_err("invalid number `%s'", 432 p); 433 if (argc > MAX_PACK_ARGS) 434 mtree_err("too many arguments"); 435 } 436 if (argc < 2) 437 mtree_err("not enough arguments"); 438 result = (*pack)(argc, numbers, &error); 439 if (error != NULL) 440 mtree_err(error); 441 } else { 442 result = (dev_t)strtoul(arg, &ep, 0); 443 if (*ep != '\0') 444 mtree_err("invalid device `%s'", arg); 445 } 446 return (result); 447 } 448 449 static void 450 replacenode(NODE *cur, NODE *new) 451 { 452 453 #define REPLACE(x) cur->x = new->x 454 #define REPLACESTR(x) REPLACEPTR(cur->x,new->x) 455 456 if (cur->type != new->type) { 457 if (mtree_Mflag) { 458 /* 459 * merge entries with different types; we 460 * don't want children retained in this case. 461 */ 462 REPLACE(type); 463 free_nodes(cur->child); 464 cur->child = NULL; 465 } else { 466 mtree_err( 467 "existing entry for `%s', type `%s'" 468 " does not match type `%s'", 469 cur->name, nodetype(cur->type), 470 nodetype(new->type)); 471 } 472 } 473 474 REPLACE(st_size); 475 REPLACE(st_mtimespec); 476 REPLACESTR(slink); 477 if (cur->slink != NULL) { 478 if ((cur->slink = strdup(new->slink)) == NULL) 479 mtree_err("memory allocation error"); 480 if (strunvis(cur->slink, new->slink) == -1) 481 mtree_err("strunvis failed on `%s'", new->slink); 482 free(new->slink); 483 } 484 REPLACE(st_uid); 485 REPLACE(st_gid); 486 REPLACE(st_mode); 487 REPLACE(st_rdev); 488 REPLACE(st_flags); 489 REPLACE(st_nlink); 490 REPLACE(cksum); 491 REPLACESTR(md5digest); 492 REPLACESTR(rmd160digest); 493 REPLACESTR(sha1digest); 494 REPLACESTR(sha256digest); 495 REPLACESTR(sha384digest); 496 REPLACESTR(sha512digest); 497 REPLACESTR(tags); 498 REPLACE(lineno); 499 REPLACE(flags); 500 free(new); 501 } 502 503 static void 504 set(char *t, NODE *ip) 505 { 506 int type, value, len; 507 gid_t gid; 508 uid_t uid; 509 char *kw, *val, *md, *ep; 510 void *m; 511 512 while ((kw = strsep(&t, "= \t")) != NULL) { 513 if (*kw == '\0') 514 continue; 515 if (strcmp(kw, "all") == 0) 516 mtree_err("invalid keyword `all'"); 517 ip->flags |= type = parsekey(kw, &value); 518 if (!value) 519 /* Just set flag bit (F_IGN and F_OPT) */ 520 continue; 521 while ((val = strsep(&t, " \t")) != NULL && *val == '\0') 522 continue; 523 if (val == NULL) 524 mtree_err("missing value"); 525 switch (type) { 526 case F_CKSUM: 527 ip->cksum = strtoul(val, &ep, 10); 528 if (*ep) 529 mtree_err("invalid checksum `%s'", val); 530 break; 531 case F_DEV: 532 ip->st_rdev = parsedev(val); 533 break; 534 case F_FLAGS: 535 if (strcmp("none", val) == 0) 536 ip->st_flags = 0; 537 else if (string_to_flags(&val, &ip->st_flags, NULL) 538 != 0) 539 mtree_err("invalid flag `%s'", val); 540 break; 541 case F_GID: 542 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 543 if (*ep) 544 mtree_err("invalid gid `%s'", val); 545 break; 546 case F_GNAME: 547 if (mtree_Wflag) /* don't parse if whacking */ 548 break; 549 if (gid_from_group(val, &gid) == -1) 550 mtree_err("unknown group `%s'", val); 551 ip->st_gid = gid; 552 break; 553 case F_MD5: 554 if (val[0]=='0' && val[1]=='x') 555 md=&val[2]; 556 else 557 md=val; 558 if ((ip->md5digest = strdup(md)) == NULL) 559 mtree_err("memory allocation error"); 560 break; 561 case F_MODE: 562 if ((m = setmode(val)) == NULL) 563 mtree_err("cannot set file mode `%s' (%s)", 564 val, strerror(errno)); 565 ip->st_mode = getmode(m, 0); 566 free(m); 567 break; 568 case F_NLINK: 569 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 570 if (*ep) 571 mtree_err("invalid link count `%s'", val); 572 break; 573 case F_RMD160: 574 if (val[0]=='0' && val[1]=='x') 575 md=&val[2]; 576 else 577 md=val; 578 if ((ip->rmd160digest = strdup(md)) == NULL) 579 mtree_err("memory allocation error"); 580 break; 581 case F_SHA1: 582 if (val[0]=='0' && val[1]=='x') 583 md=&val[2]; 584 else 585 md=val; 586 if ((ip->sha1digest = strdup(md)) == NULL) 587 mtree_err("memory allocation error"); 588 break; 589 case F_SIZE: 590 ip->st_size = (off_t)strtoll(val, &ep, 10); 591 if (*ep) 592 mtree_err("invalid size `%s'", val); 593 break; 594 case F_SLINK: 595 if ((ip->slink = strdup(val)) == NULL) 596 mtree_err("memory allocation error"); 597 if (strunvis(ip->slink, val) == -1) 598 mtree_err("strunvis failed on `%s'", val); 599 break; 600 case F_TAGS: 601 len = strlen(val) + 3; /* "," + str + ",\0" */ 602 if ((ip->tags = malloc(len)) == NULL) 603 mtree_err("memory allocation error"); 604 snprintf(ip->tags, len, ",%s,", val); 605 break; 606 case F_TIME: 607 ip->st_mtimespec.tv_sec = 608 (time_t)strtoul(val, &ep, 10); 609 if (*ep != '.') 610 mtree_err("invalid time `%s'", val); 611 val = ep + 1; 612 ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); 613 if (*ep) 614 mtree_err("invalid time `%s'", val); 615 break; 616 case F_TYPE: 617 ip->type = parsetype(val); 618 break; 619 case F_UID: 620 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 621 if (*ep) 622 mtree_err("invalid uid `%s'", val); 623 break; 624 case F_UNAME: 625 if (mtree_Wflag) /* don't parse if whacking */ 626 break; 627 if (uid_from_user(val, &uid) == -1) 628 mtree_err("unknown user `%s'", val); 629 ip->st_uid = uid; 630 break; 631 case F_SHA256: 632 if (val[0]=='0' && val[1]=='x') 633 md=&val[2]; 634 else 635 md=val; 636 if ((ip->sha256digest = strdup(md)) == NULL) 637 mtree_err("memory allocation error"); 638 break; 639 case F_SHA384: 640 if (val[0]=='0' && val[1]=='x') 641 md=&val[2]; 642 else 643 md=val; 644 if ((ip->sha384digest = strdup(md)) == NULL) 645 mtree_err("memory allocation error"); 646 break; 647 case F_SHA512: 648 if (val[0]=='0' && val[1]=='x') 649 md=&val[2]; 650 else 651 md=val; 652 if ((ip->sha512digest = strdup(md)) == NULL) 653 mtree_err("memory allocation error"); 654 break; 655 default: 656 mtree_err( 657 "set(): unsupported key type 0x%x (INTERNAL ERROR)", 658 type); 659 /* NOTREACHED */ 660 } 661 } 662 } 663 664 static void 665 unset(char *t, NODE *ip) 666 { 667 char *p; 668 669 while ((p = strsep(&t, " \t")) != NULL) { 670 if (*p == '\0') 671 continue; 672 ip->flags &= ~parsekey(p, NULL); 673 } 674 } 675