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