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 if (value == NULL) { 635 error = ENOATTR; 636 break; 637 } 638 p = strchr(value, '.'); 639 if (p != NULL) 640 *p++ = '\0'; 641 error = read_number(value, 10, &num, 0, 642 INTMAX_MAX); 643 if (error) 644 break; 645 st->st_atime = num; 646 st->st_ctime = num; 647 st->st_mtime = num; 648 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 649 if (p == NULL) 650 break; 651 error = read_number(p, 10, &num, 0, 652 INTMAX_MAX); 653 if (error) 654 break; 655 st->st_atimensec = num; 656 st->st_ctimensec = num; 657 st->st_mtimensec = num; 658 #endif 659 } else if (strcmp(keyword, "type") == 0) { 660 if (value == NULL) { 661 error = ENOATTR; 662 break; 663 } 664 if (strcmp(value, "dir") == 0) 665 node->type = S_IFDIR; 666 else if (strcmp(value, "file") == 0) 667 node->type = S_IFREG; 668 else if (strcmp(value, "link") == 0) 669 node->type = S_IFLNK; 670 else 671 error = EINVAL; 672 } else 673 error = ENOSYS; 674 break; 675 case 'u': 676 if (strcmp(keyword, "uid") == 0) { 677 if (value == NULL) { 678 error = ENOATTR; 679 break; 680 } 681 error = read_number(value, 10, &num, 682 0, UINT_MAX); 683 if (!error) 684 st->st_uid = num; 685 } else if (strcmp(keyword, "uname") == 0) { 686 if (value == NULL) { 687 error = ENOATTR; 688 break; 689 } 690 if (uid_from_user(value, &uid) == 0) 691 st->st_uid = uid; 692 else 693 error = EINVAL; 694 } else 695 error = ENOSYS; 696 break; 697 default: 698 error = ENOSYS; 699 break; 700 } 701 702 switch (error) { 703 case EINVAL: 704 mtree_error("%s: invalid value '%s'", keyword, value); 705 break; 706 case ENOATTR: 707 mtree_error("%s: keyword needs a value", keyword); 708 break; 709 case ENOSYS: 710 mtree_warning("%s: unsupported keyword", keyword); 711 break; 712 case ENXIO: 713 mtree_error("%s: keyword does not take a value", 714 keyword); 715 break; 716 } 717 } while (1); 718 719 if (error) 720 return (error); 721 722 st->st_mode = (st->st_mode & ~S_IFMT) | node->type; 723 724 /* Nothing more to do for the global defaults. */ 725 if (node->name == NULL) 726 return (0); 727 728 /* 729 * Be intelligent about the file type. 730 */ 731 if (node->contents != NULL) { 732 if (node->symlink != NULL) { 733 mtree_error("%s: both link and contents keywords " 734 "defined", node->name); 735 return (0); 736 } 737 type = S_IFREG; 738 } else if (node->type != 0) { 739 type = node->type; 740 if (type == S_IFLNK && node->symlink == NULL) { 741 mtree_error("%s: link type requires link keyword", node->name); 742 return (0); 743 } else if (type == S_IFREG) { 744 /* the named path is the default contents */ 745 node->contents = mtree_file_path(node); 746 } 747 } else 748 type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR; 749 750 if (node->type == 0) 751 node->type = type; 752 753 if (node->type != type) { 754 mtree_error("%s: file type and defined keywords to not match", 755 node->name); 756 return (0); 757 } 758 759 st->st_mode = (st->st_mode & ~S_IFMT) | node->type; 760 761 if (node->contents == NULL) 762 return (0); 763 764 name = mtree_resolve(node->contents, &istemp); 765 if (name == NULL) 766 return (errno); 767 768 if (stat(name, &sb) != 0) { 769 mtree_error("%s: contents file '%s' not found", node->name, 770 name); 771 free(name); 772 return (0); 773 } 774 775 /* 776 * Check for hardlinks. If the contents key is used, then the check 777 * will only trigger if the contents file is a link even if it is used 778 * by more than one file 779 */ 780 if (sb.st_nlink > 1) { 781 fsinode *curino; 782 783 st->st_ino = sb.st_ino; 784 st->st_dev = sb.st_dev; 785 curino = link_check(node->inode); 786 if (curino != NULL) { 787 free(node->inode); 788 node->inode = curino; 789 node->inode->nlink++; 790 /* Reset st since node->inode has been updated. */ 791 st = &node->inode->st; 792 } 793 } 794 795 free(node->contents); 796 node->contents = name; 797 st->st_size = sb.st_size; 798 return (0); 799 } 800 801 static int 802 read_mtree_command(FILE *fp) 803 { 804 char cmd[10]; 805 int error; 806 807 error = read_word(fp, cmd, sizeof(cmd)); 808 if (error) 809 goto out; 810 811 error = read_mtree_keywords(fp, &mtree_global); 812 813 out: 814 skip_to(fp, "\n"); 815 (void)getc(fp); 816 return (error); 817 } 818 819 static int 820 read_mtree_spec1(FILE *fp, bool def, const char *name) 821 { 822 fsnode *last, *node, *parent; 823 u_int type; 824 int error; 825 826 assert(name[0] != '\0'); 827 828 /* 829 * Treat '..' specially, because it only changes our current 830 * directory. We don't create a node for it. We simply ignore 831 * any keywords that may appear on the line as well. 832 * Going up a directory is a little non-obvious. A directory 833 * node has a corresponding '.' child. The parent of '.' is 834 * not the '.' node of the parent directory, but the directory 835 * node within the parent to which the child relates. However, 836 * going up a directory means we need to find the '.' node to 837 * which the directory node is linked. This we can do via the 838 * first * pointer, because '.' is always the first entry in a 839 * directory. 840 */ 841 if (IS_DOTDOT(name)) { 842 /* This deals with NULL pointers as well. */ 843 if (mtree_current == mtree_root) { 844 mtree_warning("ignoring .. in root directory"); 845 return (0); 846 } 847 848 node = mtree_current; 849 850 assert(node != NULL); 851 assert(IS_DOT(node->name)); 852 assert(node->first == node); 853 854 /* Get the corresponding directory node in the parent. */ 855 node = mtree_current->parent; 856 857 assert(node != NULL); 858 assert(!IS_DOT(node->name)); 859 860 node = node->first; 861 862 assert(node != NULL); 863 assert(IS_DOT(node->name)); 864 assert(node->first == node); 865 866 mtree_current = node; 867 return (0); 868 } 869 870 /* 871 * If we don't have a current directory and the first specification 872 * (either implicit or defined) is not '.', then we need to create 873 * a '.' node first (using a recursive call). 874 */ 875 if (!IS_DOT(name) && mtree_current == NULL) { 876 error = read_mtree_spec1(fp, false, "."); 877 if (error) 878 return (error); 879 } 880 881 /* 882 * Lookup the name in the current directory (if we have a current 883 * directory) to make sure we do not create multiple nodes for the 884 * same component. For non-definitions, if we find a node with the 885 * same name, simply change the current directory. For definitions 886 * more happens. 887 */ 888 last = NULL; 889 node = mtree_current; 890 while (node != NULL) { 891 assert(node->first == mtree_current); 892 893 if (strcmp(name, node->name) == 0) { 894 if (def == true) { 895 if (dupsok == 0) 896 mtree_error( 897 "duplicate definition of %s", 898 name); 899 else if (dupsok == 1) 900 mtree_warning( 901 "duplicate definition of %s", 902 name); 903 return (0); 904 } 905 906 if (node->type != S_IFDIR) { 907 mtree_error("%s is not a directory", name); 908 return (0); 909 } 910 911 assert(!IS_DOT(name)); 912 913 node = node->child; 914 915 assert(node != NULL); 916 assert(IS_DOT(node->name)); 917 918 mtree_current = node; 919 return (0); 920 } 921 922 last = node; 923 node = last->next; 924 } 925 926 parent = (mtree_current != NULL) ? mtree_current->parent : NULL; 927 type = (def == false || IS_DOT(name)) ? S_IFDIR : 0; 928 node = create_node(name, type, parent, &mtree_global); 929 if (node == NULL) 930 return (ENOMEM); 931 932 if (def == true) { 933 error = read_mtree_keywords(fp, node); 934 if (error) { 935 destroy_node(node); 936 return (error); 937 } 938 } 939 940 node->first = (mtree_current != NULL) ? mtree_current : node; 941 942 if (last != NULL) 943 last->next = node; 944 945 if (node->type != S_IFDIR) 946 return (0); 947 948 if (!IS_DOT(node->name)) { 949 parent = node; 950 node = create_node(".", S_IFDIR, parent, parent); 951 if (node == NULL) { 952 last->next = NULL; 953 destroy_node(parent); 954 return (ENOMEM); 955 } 956 parent->child = node; 957 node->first = node; 958 } 959 960 assert(node != NULL); 961 assert(IS_DOT(node->name)); 962 assert(node->first == node); 963 964 mtree_current = node; 965 if (mtree_root == NULL) 966 mtree_root = node; 967 968 return (0); 969 } 970 971 static int 972 read_mtree_spec(FILE *fp) 973 { 974 char pathspec[PATH_MAX], pathtmp[4*PATH_MAX + 1]; 975 char *cp; 976 int error; 977 978 error = read_word(fp, pathtmp, sizeof(pathtmp)); 979 if (error) 980 goto out; 981 if (strnunvis(pathspec, PATH_MAX, pathtmp) == -1) { 982 error = errno; 983 goto out; 984 } 985 error = 0; 986 987 cp = strchr(pathspec, '/'); 988 if (cp != NULL) { 989 /* Absolute pathname */ 990 mtree_current = mtree_root; 991 992 do { 993 *cp++ = '\0'; 994 995 /* Disallow '..' as a component. */ 996 if (IS_DOTDOT(pathspec)) { 997 mtree_error("absolute path cannot contain " 998 ".. component"); 999 goto out; 1000 } 1001 1002 /* Ignore multiple adjacent slashes and '.'. */ 1003 if (pathspec[0] != '\0' && !IS_DOT(pathspec)) 1004 error = read_mtree_spec1(fp, false, pathspec); 1005 memmove(pathspec, cp, strlen(cp) + 1); 1006 cp = strchr(pathspec, '/'); 1007 } while (!error && cp != NULL); 1008 1009 /* Disallow '.' and '..' as the last component. */ 1010 if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) { 1011 mtree_error("absolute path cannot contain . or .. " 1012 "components"); 1013 goto out; 1014 } 1015 } 1016 1017 /* Ignore absolute specifications that end with a slash. */ 1018 if (!error && pathspec[0] != '\0') 1019 error = read_mtree_spec1(fp, true, pathspec); 1020 1021 out: 1022 skip_to(fp, "\n"); 1023 (void)getc(fp); 1024 return (error); 1025 } 1026 1027 fsnode * 1028 read_mtree(const char *fname, fsnode *node) 1029 { 1030 struct mtree_fileinfo *fi; 1031 FILE *fp; 1032 int c, error; 1033 1034 /* We do not yet support nesting... */ 1035 assert(node == NULL); 1036 1037 if (strcmp(fname, "-") == 0) 1038 fp = stdin; 1039 else { 1040 fp = fopen(fname, "r"); 1041 if (fp == NULL) 1042 err(1, "Can't open `%s'", fname); 1043 } 1044 1045 error = mtree_file_push(fname, fp); 1046 if (error) 1047 goto out; 1048 1049 memset(&mtree_global, 0, sizeof(mtree_global)); 1050 memset(&mtree_global_inode, 0, sizeof(mtree_global_inode)); 1051 mtree_global.inode = &mtree_global_inode; 1052 mtree_global_inode.nlink = 1; 1053 mtree_global_inode.st.st_nlink = 1; 1054 mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime = 1055 mtree_global_inode.st.st_mtime = time(NULL); 1056 errors = warnings = 0; 1057 1058 setgroupent(1); 1059 setpassent(1); 1060 1061 mtree_root = node; 1062 mtree_current = node; 1063 do { 1064 /* Start of a new line... */ 1065 fi = SLIST_FIRST(&mtree_fileinfo); 1066 fi->line++; 1067 1068 error = skip_over(fp, " \t"); 1069 if (error) 1070 break; 1071 1072 c = getc(fp); 1073 if (c == EOF) { 1074 error = ferror(fp) ? errno : -1; 1075 break; 1076 } 1077 1078 switch (c) { 1079 case '\n': /* empty line */ 1080 error = 0; 1081 break; 1082 case '#': /* comment -- skip to end of line. */ 1083 error = skip_to(fp, "\n"); 1084 if (!error) 1085 (void)getc(fp); 1086 break; 1087 case '/': /* special commands */ 1088 error = read_mtree_command(fp); 1089 break; 1090 default: /* specification */ 1091 ungetc(c, fp); 1092 error = read_mtree_spec(fp); 1093 break; 1094 } 1095 } while (!error); 1096 1097 endpwent(); 1098 endgrent(); 1099 1100 if (error <= 0 && (errors || warnings)) { 1101 warnx("%u error(s) and %u warning(s) in mtree manifest", 1102 errors, warnings); 1103 if (errors) 1104 exit(1); 1105 } 1106 1107 out: 1108 if (error > 0) 1109 errc(1, error, "Error reading mtree file"); 1110 1111 if (fp != stdin) 1112 fclose(fp); 1113 1114 if (mtree_root != NULL) 1115 return (mtree_root); 1116 1117 /* Handle empty specifications. */ 1118 node = create_node(".", S_IFDIR, NULL, &mtree_global); 1119 node->first = node; 1120 return (node); 1121 } 1122