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