1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011 Marcel Moolenaar 5 * 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #if HAVE_NBTOOL_CONFIG_H 29 #include "nbtool_config.h" 30 #endif 31 32 #include <sys/param.h> 33 #include <sys/queue.h> 34 #include <sys/sbuf.h> 35 #include <sys/stat.h> 36 #include <sys/types.h> 37 #include <assert.h> 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <grp.h> 41 #include <inttypes.h> 42 #include <pwd.h> 43 #include <stdarg.h> 44 #include <stdbool.h> 45 #include <stddef.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <strings.h> 50 #include <time.h> 51 #include <unistd.h> 52 #include <util.h> 53 #include <vis.h> 54 55 #include "makefs.h" 56 57 #ifndef ENOATTR 58 #define ENOATTR ENODATA 59 #endif 60 61 #define IS_DOT(nm) ((nm)[0] == '.' && (nm)[1] == '\0') 62 #define IS_DOTDOT(nm) ((nm)[0] == '.' && (nm)[1] == '.' && (nm)[2] == '\0') 63 64 struct mtree_fileinfo { 65 SLIST_ENTRY(mtree_fileinfo) next; 66 FILE *fp; 67 const char *name; 68 u_int line; 69 }; 70 71 /* Global state used while parsing. */ 72 static SLIST_HEAD(, mtree_fileinfo) mtree_fileinfo = 73 SLIST_HEAD_INITIALIZER(mtree_fileinfo); 74 static fsnode *mtree_root; 75 static fsnode *mtree_current; 76 static fsnode mtree_global; 77 static fsinode mtree_global_inode; 78 static u_int errors, warnings; 79 80 static void mtree_error(const char *, ...) __printflike(1, 2); 81 static void mtree_warning(const char *, ...) __printflike(1, 2); 82 83 static int 84 mtree_file_push(const char *name, FILE *fp) 85 { 86 struct mtree_fileinfo *fi; 87 88 fi = emalloc(sizeof(*fi)); 89 if (strcmp(name, "-") == 0) 90 fi->name = estrdup("(stdin)"); 91 else 92 fi->name = estrdup(name); 93 if (fi->name == NULL) { 94 free(fi); 95 return (ENOMEM); 96 } 97 98 fi->fp = fp; 99 fi->line = 0; 100 101 SLIST_INSERT_HEAD(&mtree_fileinfo, fi, next); 102 return (0); 103 } 104 105 static void 106 mtree_print(const char *msgtype, const char *fmt, va_list ap) 107 { 108 struct mtree_fileinfo *fi; 109 110 if (msgtype != NULL) { 111 fi = SLIST_FIRST(&mtree_fileinfo); 112 if (fi != NULL) 113 fprintf(stderr, "%s:%u: ", fi->name, fi->line); 114 fprintf(stderr, "%s: ", msgtype); 115 } 116 vfprintf(stderr, fmt, ap); 117 } 118 119 static void 120 mtree_error(const char *fmt, ...) 121 { 122 va_list ap; 123 124 va_start(ap, fmt); 125 mtree_print("error", fmt, ap); 126 va_end(ap); 127 128 errors++; 129 fputc('\n', stderr); 130 } 131 132 static void 133 mtree_warning(const char *fmt, ...) 134 { 135 va_list ap; 136 137 va_start(ap, fmt); 138 mtree_print("warning", fmt, ap); 139 va_end(ap); 140 141 warnings++; 142 fputc('\n', stderr); 143 } 144 145 #ifndef MAKEFS_MAX_TREE_DEPTH 146 # define MAKEFS_MAX_TREE_DEPTH (MAXPATHLEN/2) 147 #endif 148 149 /* construct path to node->name */ 150 static char * 151 mtree_file_path(fsnode *node) 152 { 153 fsnode *pnode; 154 struct sbuf *sb; 155 char *res, *rp[MAKEFS_MAX_TREE_DEPTH]; 156 int depth; 157 158 depth = 0; 159 rp[depth] = node->name; 160 for (pnode = node->parent; pnode && depth < MAKEFS_MAX_TREE_DEPTH - 1; 161 pnode = pnode->parent) { 162 if (strcmp(pnode->name, ".") == 0) 163 break; 164 rp[++depth] = pnode->name; 165 } 166 167 sb = sbuf_new_auto(); 168 if (sb == NULL) { 169 errno = ENOMEM; 170 return (NULL); 171 } 172 while (depth > 0) { 173 sbuf_cat(sb, rp[depth--]); 174 sbuf_putc(sb, '/'); 175 } 176 sbuf_cat(sb, rp[depth]); 177 sbuf_finish(sb); 178 res = estrdup(sbuf_data(sb)); 179 sbuf_delete(sb); 180 if (res == NULL) 181 errno = ENOMEM; 182 return res; 183 184 } 185 186 /* mtree_resolve() sets errno to indicate why NULL was returned. */ 187 static char * 188 mtree_resolve(const char *spec, int *istemp) 189 { 190 struct sbuf *sb; 191 char *res, *var = NULL; 192 const char *base, *p, *v; 193 size_t len; 194 int c, error, quoted, subst; 195 196 len = strlen(spec); 197 if (len == 0) { 198 errno = EINVAL; 199 return (NULL); 200 } 201 202 c = (len > 1) ? (spec[0] == spec[len - 1]) ? spec[0] : 0 : 0; 203 *istemp = (c == '`') ? 1 : 0; 204 subst = (c == '`' || c == '"') ? 1 : 0; 205 quoted = (subst || c == '\'') ? 1 : 0; 206 207 if (!subst) { 208 res = estrdup(spec + quoted); 209 if (quoted) 210 res[len - 2] = '\0'; 211 return (res); 212 } 213 214 sb = sbuf_new_auto(); 215 if (sb == NULL) { 216 errno = ENOMEM; 217 return (NULL); 218 } 219 220 base = spec + 1; 221 len -= 2; 222 error = 0; 223 while (len > 0) { 224 p = strchr(base, '$'); 225 if (p == NULL) { 226 sbuf_bcat(sb, base, len); 227 base += len; 228 len = 0; 229 continue; 230 } 231 /* The following is safe. spec always starts with a quote. */ 232 if (p[-1] == '\\') 233 p--; 234 if (base != p) { 235 sbuf_bcat(sb, base, p - base); 236 len -= p - base; 237 base = p; 238 } 239 if (*p == '\\') { 240 sbuf_putc(sb, '$'); 241 base += 2; 242 len -= 2; 243 continue; 244 } 245 /* Skip the '$'. */ 246 base++; 247 len--; 248 /* Handle ${X} vs $X. */ 249 v = base; 250 if (*base == '{') { 251 p = strchr(v, '}'); 252 if (p == NULL) 253 p = v; 254 } else 255 p = v; 256 len -= (p + 1) - base; 257 base = p + 1; 258 259 if (v == p) { 260 sbuf_putc(sb, *v); 261 continue; 262 } 263 264 error = ENOMEM; 265 var = ecalloc(p - v, 1); 266 memcpy(var, v + 1, p - v - 1); 267 if (strcmp(var, ".CURDIR") == 0) { 268 res = getcwd(NULL, 0); 269 if (res == NULL) 270 break; 271 } else if (strcmp(var, ".PROG") == 0) { 272 res = estrdup(getprogname()); 273 } else { 274 v = getenv(var); 275 if (v != NULL) { 276 res = estrdup(v); 277 } else 278 res = NULL; 279 } 280 error = 0; 281 282 if (res != NULL) { 283 sbuf_cat(sb, res); 284 free(res); 285 } 286 free(var); 287 var = NULL; 288 } 289 290 free(var); 291 sbuf_finish(sb); 292 res = (error == 0) ? strdup(sbuf_data(sb)) : NULL; 293 sbuf_delete(sb); 294 if (res == NULL) 295 errno = ENOMEM; 296 return (res); 297 } 298 299 static int 300 skip_over(FILE *fp, const char *cs) 301 { 302 int c; 303 304 c = getc(fp); 305 while (c != EOF && strchr(cs, c) != NULL) 306 c = getc(fp); 307 if (c != EOF) { 308 ungetc(c, fp); 309 return (0); 310 } 311 return (ferror(fp) ? errno : -1); 312 } 313 314 static int 315 skip_to(FILE *fp, const char *cs) 316 { 317 int c; 318 319 c = getc(fp); 320 while (c != EOF && strchr(cs, c) == NULL) 321 c = getc(fp); 322 if (c != EOF) { 323 ungetc(c, fp); 324 return (0); 325 } 326 return (ferror(fp) ? errno : -1); 327 } 328 329 static int 330 read_word(FILE *fp, char *buf, size_t bufsz) 331 { 332 struct mtree_fileinfo *fi; 333 size_t idx, qidx; 334 int c, done, error, esc, qlvl; 335 336 if (bufsz == 0) 337 return (EINVAL); 338 339 done = 0; 340 esc = 0; 341 idx = 0; 342 qidx = -1; 343 qlvl = 0; 344 do { 345 c = getc(fp); 346 switch (c) { 347 case EOF: 348 buf[idx] = '\0'; 349 error = ferror(fp) ? errno : -1; 350 if (error == -1) 351 mtree_error("unexpected end of file"); 352 return (error); 353 case '#': /* comment -- skip to end of line. */ 354 if (!esc) { 355 error = skip_to(fp, "\n"); 356 if (!error) 357 continue; 358 } 359 break; 360 case '\\': 361 esc++; 362 break; 363 case '`': 364 case '\'': 365 case '"': 366 if (esc) 367 break; 368 if (qlvl == 0) { 369 qlvl++; 370 qidx = idx; 371 } else if (c == buf[qidx]) { 372 qlvl--; 373 if (qlvl > 0) { 374 do { 375 qidx--; 376 } while (buf[qidx] != '`' && 377 buf[qidx] != '\'' && 378 buf[qidx] != '"'); 379 } else 380 qidx = -1; 381 } else { 382 qlvl++; 383 qidx = idx; 384 } 385 break; 386 case ' ': 387 case '\t': 388 case '\n': 389 if (!esc && qlvl == 0) { 390 ungetc(c, fp); 391 c = '\0'; 392 done = 1; 393 break; 394 } 395 if (c == '\n') { 396 /* 397 * We going to eat the newline ourselves. 398 */ 399 if (qlvl > 0) 400 mtree_warning("quoted word straddles " 401 "onto next line."); 402 fi = SLIST_FIRST(&mtree_fileinfo); 403 fi->line++; 404 } 405 break; 406 default: 407 if (esc) 408 buf[idx++] = '\\'; 409 break; 410 } 411 buf[idx++] = c; 412 esc = 0; 413 } while (idx < bufsz && !done); 414 415 if (idx >= bufsz) { 416 mtree_error("word too long to fit buffer (max %zu characters)", 417 bufsz); 418 skip_to(fp, " \t\n"); 419 } 420 return (0); 421 } 422 423 static fsnode * 424 create_node(const char *name, u_int type, fsnode *parent, fsnode *global) 425 { 426 fsnode *n; 427 428 n = ecalloc(1, sizeof(*n)); 429 n->name = estrdup(name); 430 n->type = (type == 0) ? global->type : type; 431 n->parent = parent; 432 433 n->inode = ecalloc(1, sizeof(*n->inode)); 434 435 /* Assign global options/defaults. */ 436 memcpy(n->inode, global->inode, sizeof(*n->inode)); 437 n->inode->st.st_mode = (n->inode->st.st_mode & ~S_IFMT) | n->type; 438 439 if (n->type == S_IFLNK) 440 n->symlink = global->symlink; 441 else if (n->type == S_IFREG) 442 n->contents = global->contents; 443 444 return (n); 445 } 446 447 static void 448 destroy_node(fsnode *n) 449 { 450 451 assert(n != NULL); 452 assert(n->name != NULL); 453 assert(n->inode != NULL); 454 455 free(n->inode); 456 free(n->name); 457 free(n); 458 } 459 460 static int 461 read_number(const char *tok, u_int base, intmax_t *res, intmax_t min, 462 intmax_t max) 463 { 464 char *end; 465 intmax_t val; 466 467 val = strtoimax(tok, &end, base); 468 if (end == tok || end[0] != '\0') 469 return (EINVAL); 470 if (val < min || val > max) 471 return (EDOM); 472 *res = val; 473 return (0); 474 } 475 476 static int 477 read_mtree_keywords(FILE *fp, fsnode *node) 478 { 479 char keyword[PATH_MAX]; 480 char *name, *p, *value; 481 gid_t gid; 482 uid_t uid; 483 struct stat *st, sb; 484 intmax_t num; 485 u_long flset, flclr; 486 int error, istemp; 487 uint32_t type; 488 489 st = &node->inode->st; 490 do { 491 error = skip_over(fp, " \t"); 492 if (error) 493 break; 494 495 error = read_word(fp, keyword, sizeof(keyword)); 496 if (error) 497 break; 498 499 if (keyword[0] == '\0') 500 break; 501 502 value = strchr(keyword, '='); 503 if (value != NULL) 504 *value++ = '\0'; 505 506 /* 507 * We use EINVAL, ENOATTR, ENOSYS and ENXIO to signal 508 * certain conditions: 509 * EINVAL - Value provided for a keyword that does 510 * not take a value. The value is ignored. 511 * ENOATTR - Value missing for a keyword that needs 512 * a value. The keyword is ignored. 513 * ENOSYS - Unsupported keyword encountered. The 514 * keyword is ignored. 515 * ENXIO - Value provided for a keyword that does 516 * not take a value. The value is ignored. 517 */ 518 switch (keyword[0]) { 519 case 'c': 520 if (strcmp(keyword, "contents") == 0) { 521 if (value == NULL) { 522 error = ENOATTR; 523 break; 524 } 525 node->contents = estrdup(value); 526 } else 527 error = ENOSYS; 528 break; 529 case 'f': 530 if (strcmp(keyword, "flags") == 0) { 531 if (value == NULL) { 532 error = ENOATTR; 533 break; 534 } 535 flset = flclr = 0; 536 if (!strtofflags(&value, &flset, &flclr)) { 537 FSINODE_ST_FLAGS(*node->inode) &= ~flclr; 538 FSINODE_ST_FLAGS(*node->inode) |= flset; 539 } else 540 error = errno; 541 } else 542 error = ENOSYS; 543 break; 544 case 'g': 545 if (strcmp(keyword, "gid") == 0) { 546 if (value == NULL) { 547 error = ENOATTR; 548 break; 549 } 550 error = read_number(value, 10, &num, 551 0, UINT_MAX); 552 if (!error) 553 st->st_gid = num; 554 } else if (strcmp(keyword, "gname") == 0) { 555 if (value == NULL) { 556 error = ENOATTR; 557 break; 558 } 559 if (gid_from_group(value, &gid) == 0) 560 st->st_gid = gid; 561 else 562 error = EINVAL; 563 } else 564 error = ENOSYS; 565 break; 566 case 'l': 567 if (strcmp(keyword, "link") == 0) { 568 if (value == NULL) { 569 error = ENOATTR; 570 break; 571 } 572 node->symlink = emalloc(strlen(value) + 1); 573 if (node->symlink == NULL) { 574 error = errno; 575 break; 576 } 577 if (strunvis(node->symlink, value) < 0) { 578 error = errno; 579 break; 580 } 581 } else 582 error = ENOSYS; 583 break; 584 case 'm': 585 if (strcmp(keyword, "mode") == 0) { 586 if (value == NULL) { 587 error = ENOATTR; 588 break; 589 } 590 if (value[0] >= '0' && value[0] <= '9') { 591 error = read_number(value, 8, &num, 592 0, 07777); 593 if (!error) { 594 st->st_mode &= S_IFMT; 595 st->st_mode |= num; 596 } 597 } else { 598 /* Symbolic mode not supported. */ 599 error = EINVAL; 600 break; 601 } 602 } else 603 error = ENOSYS; 604 break; 605 case 'o': 606 if (strcmp(keyword, "optional") == 0) { 607 if (value != NULL) 608 error = ENXIO; 609 node->flags |= FSNODE_F_OPTIONAL; 610 } else 611 error = ENOSYS; 612 break; 613 case 's': 614 if (strcmp(keyword, "size") == 0) { 615 if (value == NULL) { 616 error = ENOATTR; 617 break; 618 } 619 error = read_number(value, 10, &num, 620 0, INTMAX_MAX); 621 if (!error) 622 st->st_size = num; 623 } else 624 error = ENOSYS; 625 break; 626 case 't': 627 if (strcmp(keyword, "tags") == 0) { 628 if (value == NULL) { 629 error = ENOATTR; 630 break; 631 } 632 /* Ignore. */ 633 } else if (strcmp(keyword, "time") == 0) { 634 /* Ignore if a default timestamp is present. */ 635 if (stampst.st_ino != 0) 636 break; 637 if (value == NULL) { 638 error = ENOATTR; 639 break; 640 } 641 p = strchr(value, '.'); 642 if (p != NULL) 643 *p++ = '\0'; 644 error = read_number(value, 10, &num, 0, 645 INTMAX_MAX); 646 if (error) 647 break; 648 st->st_atime = num; 649 st->st_ctime = num; 650 st->st_mtime = num; 651 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 652 if (p == NULL) 653 break; 654 error = read_number(p, 10, &num, 0, 655 INTMAX_MAX); 656 if (error) 657 break; 658 st->st_atimensec = num; 659 st->st_ctimensec = num; 660 st->st_mtimensec = num; 661 #endif 662 } else if (strcmp(keyword, "type") == 0) { 663 if (value == NULL) { 664 error = ENOATTR; 665 break; 666 } 667 if (strcmp(value, "dir") == 0) 668 node->type = S_IFDIR; 669 else if (strcmp(value, "file") == 0) 670 node->type = S_IFREG; 671 else if (strcmp(value, "link") == 0) 672 node->type = S_IFLNK; 673 else 674 error = EINVAL; 675 } else 676 error = ENOSYS; 677 break; 678 case 'u': 679 if (strcmp(keyword, "uid") == 0) { 680 if (value == NULL) { 681 error = ENOATTR; 682 break; 683 } 684 error = read_number(value, 10, &num, 685 0, UINT_MAX); 686 if (!error) 687 st->st_uid = num; 688 } else if (strcmp(keyword, "uname") == 0) { 689 if (value == NULL) { 690 error = ENOATTR; 691 break; 692 } 693 if (uid_from_user(value, &uid) == 0) 694 st->st_uid = uid; 695 else 696 error = EINVAL; 697 } else 698 error = ENOSYS; 699 break; 700 default: 701 error = ENOSYS; 702 break; 703 } 704 705 switch (error) { 706 case EINVAL: 707 mtree_error("%s: invalid value '%s'", keyword, value); 708 break; 709 case ENOATTR: 710 mtree_error("%s: keyword needs a value", keyword); 711 break; 712 case ENOSYS: 713 mtree_warning("%s: unsupported keyword", keyword); 714 break; 715 case ENXIO: 716 mtree_error("%s: keyword does not take a value", 717 keyword); 718 break; 719 } 720 } while (1); 721 722 if (error) 723 return (error); 724 725 st->st_mode = (st->st_mode & ~S_IFMT) | node->type; 726 /* Store default timestamp, if present. */ 727 if (stampst.st_ino != 0) 728 set_tstamp(node); 729 /* Nothing more to do for the global defaults. */ 730 if (node->name == NULL) 731 return (0); 732 733 /* 734 * Be intelligent about the file type. 735 */ 736 if (node->contents != NULL) { 737 if (node->symlink != NULL) { 738 mtree_error("%s: both link and contents keywords " 739 "defined", node->name); 740 return (0); 741 } 742 type = S_IFREG; 743 } else if (node->type != 0) { 744 type = node->type; 745 if (type == S_IFLNK && node->symlink == NULL) { 746 mtree_error("%s: link type requires link keyword", node->name); 747 return (0); 748 } else if (type == S_IFREG) { 749 /* the named path is the default contents */ 750 node->contents = mtree_file_path(node); 751 } 752 } else 753 type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR; 754 755 if (node->type == 0) 756 node->type = type; 757 758 if (node->type != type) { 759 mtree_error("%s: file type and defined keywords to not match", 760 node->name); 761 return (0); 762 } 763 764 st->st_mode = (st->st_mode & ~S_IFMT) | node->type; 765 766 if (node->contents == NULL) 767 return (0); 768 769 name = mtree_resolve(node->contents, &istemp); 770 if (name == NULL) 771 return (errno); 772 773 if (stat(name, &sb) != 0) { 774 mtree_error("%s: contents file '%s' not found", node->name, 775 name); 776 free(name); 777 return (0); 778 } 779 780 /* 781 * Check for hardlinks. If the contents key is used, then the check 782 * will only trigger if the contents file is a link even if it is used 783 * by more than one file 784 */ 785 if (sb.st_nlink > 1) { 786 fsinode *curino; 787 788 st->st_ino = sb.st_ino; 789 st->st_dev = sb.st_dev; 790 curino = link_check(node->inode); 791 if (curino != NULL) { 792 free(node->inode); 793 node->inode = curino; 794 node->inode->nlink++; 795 /* Reset st since node->inode has been updated. */ 796 st = &node->inode->st; 797 } 798 } 799 800 free(node->contents); 801 node->contents = name; 802 st->st_size = sb.st_size; 803 return (0); 804 } 805 806 static int 807 read_mtree_command(FILE *fp) 808 { 809 char cmd[10]; 810 int error; 811 812 error = read_word(fp, cmd, sizeof(cmd)); 813 if (error) 814 goto out; 815 816 error = read_mtree_keywords(fp, &mtree_global); 817 818 out: 819 skip_to(fp, "\n"); 820 (void)getc(fp); 821 return (error); 822 } 823 824 static int 825 read_mtree_spec1(FILE *fp, bool def, const char *name) 826 { 827 fsnode *last, *node, *parent; 828 u_int type; 829 int error; 830 831 assert(name[0] != '\0'); 832 833 /* 834 * Treat '..' specially, because it only changes our current 835 * directory. We don't create a node for it. We simply ignore 836 * any keywords that may appear on the line as well. 837 * Going up a directory is a little non-obvious. A directory 838 * node has a corresponding '.' child. The parent of '.' is 839 * not the '.' node of the parent directory, but the directory 840 * node within the parent to which the child relates. However, 841 * going up a directory means we need to find the '.' node to 842 * which the directory node is linked. This we can do via the 843 * first * pointer, because '.' is always the first entry in a 844 * directory. 845 */ 846 if (IS_DOTDOT(name)) { 847 /* This deals with NULL pointers as well. */ 848 if (mtree_current == mtree_root) { 849 mtree_warning("ignoring .. in root directory"); 850 return (0); 851 } 852 853 node = mtree_current; 854 855 assert(node != NULL); 856 assert(IS_DOT(node->name)); 857 assert(node->first == node); 858 859 /* Get the corresponding directory node in the parent. */ 860 node = mtree_current->parent; 861 862 assert(node != NULL); 863 assert(!IS_DOT(node->name)); 864 865 node = node->first; 866 867 assert(node != NULL); 868 assert(IS_DOT(node->name)); 869 assert(node->first == node); 870 871 mtree_current = node; 872 return (0); 873 } 874 875 /* 876 * If we don't have a current directory and the first specification 877 * (either implicit or defined) is not '.', then we need to create 878 * a '.' node first (using a recursive call). 879 */ 880 if (!IS_DOT(name) && mtree_current == NULL) { 881 error = read_mtree_spec1(fp, false, "."); 882 if (error) 883 return (error); 884 } 885 886 /* 887 * Lookup the name in the current directory (if we have a current 888 * directory) to make sure we do not create multiple nodes for the 889 * same component. For non-definitions, if we find a node with the 890 * same name, simply change the current directory. For definitions 891 * more happens. 892 */ 893 last = NULL; 894 node = mtree_current; 895 while (node != NULL) { 896 assert(node->first == mtree_current); 897 898 if (strcmp(name, node->name) == 0) { 899 if (def == true) { 900 if (dupsok == 0) 901 mtree_error( 902 "duplicate definition of %s", 903 name); 904 else if (dupsok == 1) 905 mtree_warning( 906 "duplicate definition of %s", 907 name); 908 return (0); 909 } 910 911 if (node->type != S_IFDIR) { 912 mtree_error("%s is not a directory", name); 913 return (0); 914 } 915 916 assert(!IS_DOT(name)); 917 918 node = node->child; 919 920 assert(node != NULL); 921 assert(IS_DOT(node->name)); 922 923 mtree_current = node; 924 return (0); 925 } 926 927 last = node; 928 node = last->next; 929 } 930 931 parent = (mtree_current != NULL) ? mtree_current->parent : NULL; 932 type = (def == false || IS_DOT(name)) ? S_IFDIR : 0; 933 node = create_node(name, type, parent, &mtree_global); 934 if (node == NULL) 935 return (ENOMEM); 936 937 if (def == true) { 938 error = read_mtree_keywords(fp, node); 939 if (error) { 940 destroy_node(node); 941 return (error); 942 } 943 } 944 945 node->first = (mtree_current != NULL) ? mtree_current : node; 946 947 if (last != NULL) 948 last->next = node; 949 950 if (node->type != S_IFDIR) 951 return (0); 952 953 if (!IS_DOT(node->name)) { 954 parent = node; 955 node = create_node(".", S_IFDIR, parent, parent); 956 if (node == NULL) { 957 last->next = NULL; 958 destroy_node(parent); 959 return (ENOMEM); 960 } 961 parent->child = node; 962 node->first = node; 963 } 964 965 assert(node != NULL); 966 assert(IS_DOT(node->name)); 967 assert(node->first == node); 968 969 mtree_current = node; 970 if (mtree_root == NULL) 971 mtree_root = node; 972 973 return (0); 974 } 975 976 static int 977 read_mtree_spec(FILE *fp) 978 { 979 char pathspec[PATH_MAX], pathtmp[4*PATH_MAX + 1]; 980 char *cp; 981 int error; 982 983 error = read_word(fp, pathtmp, sizeof(pathtmp)); 984 if (error) 985 goto out; 986 if (strnunvis(pathspec, PATH_MAX, pathtmp) == -1) { 987 error = errno; 988 goto out; 989 } 990 error = 0; 991 992 cp = strchr(pathspec, '/'); 993 if (cp != NULL) { 994 /* Absolute pathname */ 995 mtree_current = mtree_root; 996 997 do { 998 *cp++ = '\0'; 999 1000 /* Disallow '..' as a component. */ 1001 if (IS_DOTDOT(pathspec)) { 1002 mtree_error("absolute path cannot contain " 1003 ".. component"); 1004 goto out; 1005 } 1006 1007 /* Ignore multiple adjacent slashes and '.'. */ 1008 if (pathspec[0] != '\0' && !IS_DOT(pathspec)) 1009 error = read_mtree_spec1(fp, false, pathspec); 1010 memmove(pathspec, cp, strlen(cp) + 1); 1011 cp = strchr(pathspec, '/'); 1012 } while (!error && cp != NULL); 1013 1014 /* Disallow '.' and '..' as the last component. */ 1015 if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) { 1016 mtree_error("absolute path cannot contain . or .. " 1017 "components"); 1018 goto out; 1019 } 1020 } 1021 1022 /* Ignore absolute specifications that end with a slash. */ 1023 if (!error && pathspec[0] != '\0') 1024 error = read_mtree_spec1(fp, true, pathspec); 1025 1026 out: 1027 skip_to(fp, "\n"); 1028 (void)getc(fp); 1029 return (error); 1030 } 1031 1032 fsnode * 1033 read_mtree(const char *fname, fsnode *node) 1034 { 1035 struct mtree_fileinfo *fi; 1036 FILE *fp; 1037 int c, error; 1038 1039 /* We do not yet support nesting... */ 1040 assert(node == NULL); 1041 1042 if (strcmp(fname, "-") == 0) 1043 fp = stdin; 1044 else { 1045 fp = fopen(fname, "r"); 1046 if (fp == NULL) 1047 err(1, "Can't open `%s'", fname); 1048 } 1049 1050 error = mtree_file_push(fname, fp); 1051 if (error) 1052 goto out; 1053 1054 memset(&mtree_global, 0, sizeof(mtree_global)); 1055 memset(&mtree_global_inode, 0, sizeof(mtree_global_inode)); 1056 mtree_global.inode = &mtree_global_inode; 1057 mtree_global_inode.nlink = 1; 1058 mtree_global_inode.st.st_nlink = 1; 1059 if (stampst.st_ino != 0) { 1060 set_tstamp(&mtree_global); 1061 } else { 1062 #if HAVE_STRUCT_STAT_BIRTHTIME 1063 mtree_global_inode.st.st_birthtime = 1064 #endif 1065 mtree_global_inode.st.st_atime = 1066 mtree_global_inode.st.st_ctime = 1067 mtree_global_inode.st.st_mtime = time(NULL); 1068 } 1069 errors = warnings = 0; 1070 1071 setgroupent(1); 1072 setpassent(1); 1073 1074 mtree_root = node; 1075 mtree_current = node; 1076 do { 1077 /* Start of a new line... */ 1078 fi = SLIST_FIRST(&mtree_fileinfo); 1079 fi->line++; 1080 1081 error = skip_over(fp, " \t"); 1082 if (error) 1083 break; 1084 1085 c = getc(fp); 1086 if (c == EOF) { 1087 error = ferror(fp) ? errno : -1; 1088 break; 1089 } 1090 1091 switch (c) { 1092 case '\n': /* empty line */ 1093 error = 0; 1094 break; 1095 case '#': /* comment -- skip to end of line. */ 1096 error = skip_to(fp, "\n"); 1097 if (!error) 1098 (void)getc(fp); 1099 break; 1100 case '/': /* special commands */ 1101 error = read_mtree_command(fp); 1102 break; 1103 default: /* specification */ 1104 ungetc(c, fp); 1105 error = read_mtree_spec(fp); 1106 break; 1107 } 1108 } while (!error); 1109 1110 endpwent(); 1111 endgrent(); 1112 1113 if (error <= 0 && (errors || warnings)) { 1114 warnx("%u error(s) and %u warning(s) in mtree manifest", 1115 errors, warnings); 1116 if (errors) 1117 exit(1); 1118 } 1119 1120 out: 1121 if (error > 0) 1122 errc(1, error, "Error reading mtree file"); 1123 1124 if (fp != stdin) 1125 fclose(fp); 1126 1127 if (mtree_root != NULL) 1128 return (mtree_root); 1129 1130 /* Handle empty specifications. */ 1131 node = create_node(".", S_IFDIR, NULL, &mtree_global); 1132 node->first = node; 1133 return (node); 1134 } 1135