1 /* 2 * Copyright (c) 1985, 1993 3 * The Regents of the University of California. 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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 #if 0 32 static char sccsid[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95"; 33 #endif 34 #endif /* not lint */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 42 #include <ufs/ufs/dinode.h> 43 #include <ufs/ufs/dir.h> 44 #include <protocols/dumprestore.h> 45 46 #include <ctype.h> 47 #include <glob.h> 48 #include <limits.h> 49 #include <setjmp.h> 50 #include <stdint.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 #include "restore.h" 56 #include "extern.h" 57 58 /* 59 * Things to handle interruptions. 60 */ 61 static int runshell; 62 static jmp_buf reset; 63 static char *nextarg = NULL; 64 65 /* 66 * Structure and routines associated with listing directories. 67 */ 68 struct afile { 69 ino_t fnum; /* inode number of file */ 70 char *fname; /* file name */ 71 short len; /* name length */ 72 char prefix; /* prefix character */ 73 char postfix; /* postfix character */ 74 }; 75 struct arglist { 76 int freeglob; /* glob structure needs to be freed */ 77 int argcnt; /* next globbed argument to return */ 78 glob_t glob; /* globbing information */ 79 char *cmd; /* the current command */ 80 }; 81 82 static char *copynext(char *, char *); 83 static int fcmp(const void *, const void *); 84 static void formatf(struct afile *, int); 85 static void getcmd(char *, char *, char *, size_t, struct arglist *); 86 struct dirent *glob_readdir(void *); 87 static int glob_stat(const char *, struct stat *); 88 static void mkentry(char *, struct direct *, struct afile *); 89 static void printlist(char *, char *); 90 91 /* 92 * Read and execute commands from the terminal. 93 */ 94 void 95 runcmdshell(void) 96 { 97 struct entry *np; 98 ino_t ino; 99 struct arglist arglist; 100 char curdir[MAXPATHLEN]; 101 char name[MAXPATHLEN]; 102 char cmd[BUFSIZ]; 103 104 arglist.freeglob = 0; 105 arglist.argcnt = 0; 106 arglist.glob.gl_flags = GLOB_ALTDIRFUNC; 107 arglist.glob.gl_opendir = rst_opendir; 108 arglist.glob.gl_readdir = glob_readdir; 109 arglist.glob.gl_closedir = rst_closedir; 110 arglist.glob.gl_lstat = glob_stat; 111 arglist.glob.gl_stat = glob_stat; 112 canon("/", curdir, sizeof(curdir)); 113 loop: 114 if (setjmp(reset) != 0) { 115 if (arglist.freeglob != 0) { 116 arglist.freeglob = 0; 117 arglist.argcnt = 0; 118 globfree(&arglist.glob); 119 } 120 nextarg = NULL; 121 volno = 0; 122 } 123 runshell = 1; 124 getcmd(curdir, cmd, name, sizeof(name), &arglist); 125 switch (cmd[0]) { 126 /* 127 * Add elements to the extraction list. 128 */ 129 case 'a': 130 if (strncmp(cmd, "add", strlen(cmd)) != 0) 131 goto bad; 132 ino = dirlookup(name); 133 if (ino == 0) 134 break; 135 if (mflag) 136 pathcheck(name); 137 treescan(name, ino, addfile); 138 break; 139 /* 140 * Change working directory. 141 */ 142 case 'c': 143 if (strncmp(cmd, "cd", strlen(cmd)) != 0) 144 goto bad; 145 ino = dirlookup(name); 146 if (ino == 0) 147 break; 148 if (inodetype(ino) == LEAF) { 149 fprintf(stderr, "%s: not a directory\n", name); 150 break; 151 } 152 (void) strcpy(curdir, name); 153 break; 154 /* 155 * Delete elements from the extraction list. 156 */ 157 case 'd': 158 if (strncmp(cmd, "delete", strlen(cmd)) != 0) 159 goto bad; 160 np = lookupname(name); 161 if (np == NULL || (np->e_flags & NEW) == 0) { 162 fprintf(stderr, "%s: not on extraction list\n", name); 163 break; 164 } 165 treescan(name, np->e_ino, deletefile); 166 break; 167 /* 168 * Extract the requested list. 169 */ 170 case 'e': 171 if (strncmp(cmd, "extract", strlen(cmd)) != 0) 172 goto bad; 173 createfiles(); 174 createlinks(); 175 setdirmodes(0); 176 if (dflag) 177 checkrestore(); 178 volno = 0; 179 break; 180 /* 181 * List available commands. 182 */ 183 case 'h': 184 if (strncmp(cmd, "help", strlen(cmd)) != 0) 185 goto bad; 186 case '?': 187 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 188 "Available commands are:\n", 189 "\tls [arg] - list directory\n", 190 "\tcd arg - change directory\n", 191 "\tpwd - print current directory\n", 192 "\tadd [arg] - add `arg' to list of", 193 " files to be extracted\n", 194 "\tdelete [arg] - delete `arg' from", 195 " list of files to be extracted\n", 196 "\textract - extract requested files\n", 197 "\tsetmodes - set modes of requested directories\n", 198 "\tquit - immediately exit program\n", 199 "\twhat - list dump header information\n", 200 "\tverbose - toggle verbose flag", 201 " (useful with ``ls'')\n", 202 "\thelp or `?' - print this list\n", 203 "If no `arg' is supplied, the current", 204 " directory is used\n"); 205 break; 206 /* 207 * List a directory. 208 */ 209 case 'l': 210 if (strncmp(cmd, "ls", strlen(cmd)) != 0) 211 goto bad; 212 printlist(name, curdir); 213 break; 214 /* 215 * Print current directory. 216 */ 217 case 'p': 218 if (strncmp(cmd, "pwd", strlen(cmd)) != 0) 219 goto bad; 220 if (curdir[1] == '\0') 221 fprintf(stderr, "/\n"); 222 else 223 fprintf(stderr, "%s\n", &curdir[1]); 224 break; 225 /* 226 * Quit. 227 */ 228 case 'q': 229 if (strncmp(cmd, "quit", strlen(cmd)) != 0) 230 goto bad; 231 return; 232 case 'x': 233 if (strncmp(cmd, "xit", strlen(cmd)) != 0) 234 goto bad; 235 return; 236 /* 237 * Toggle verbose mode. 238 */ 239 case 'v': 240 if (strncmp(cmd, "verbose", strlen(cmd)) != 0) 241 goto bad; 242 if (vflag) { 243 fprintf(stderr, "verbose mode off\n"); 244 vflag = 0; 245 break; 246 } 247 fprintf(stderr, "verbose mode on\n"); 248 vflag++; 249 break; 250 /* 251 * Just restore requested directory modes. 252 */ 253 case 's': 254 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0) 255 goto bad; 256 setdirmodes(FORCE); 257 break; 258 /* 259 * Print out dump header information. 260 */ 261 case 'w': 262 if (strncmp(cmd, "what", strlen(cmd)) != 0) 263 goto bad; 264 printdumpinfo(); 265 break; 266 /* 267 * Turn on debugging. 268 */ 269 case 'D': 270 if (strncmp(cmd, "Debug", strlen(cmd)) != 0) 271 goto bad; 272 if (dflag) { 273 fprintf(stderr, "debugging mode off\n"); 274 dflag = 0; 275 break; 276 } 277 fprintf(stderr, "debugging mode on\n"); 278 dflag++; 279 break; 280 /* 281 * Unknown command. 282 */ 283 default: 284 bad: 285 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd); 286 break; 287 } 288 goto loop; 289 } 290 291 /* 292 * Read and parse an interactive command. 293 * The first word on the line is assigned to "cmd". If 294 * there are no arguments on the command line, then "curdir" 295 * is returned as the argument. If there are arguments 296 * on the line they are returned one at a time on each 297 * successive call to getcmd. Each argument is first assigned 298 * to "name". If it does not start with "/" the pathname in 299 * "curdir" is prepended to it. Finally "canon" is called to 300 * eliminate any embedded ".." components. 301 */ 302 static void 303 getcmd(char *curdir, char *cmd, char *name, size_t size, struct arglist *ap) 304 { 305 char *cp; 306 static char input[BUFSIZ]; 307 char output[BUFSIZ]; 308 # define rawname input /* save space by reusing input buffer */ 309 310 /* 311 * Check to see if still processing arguments. 312 */ 313 if (ap->argcnt > 0) 314 goto retnext; 315 if (nextarg != NULL) 316 goto getnext; 317 /* 318 * Read a command line and trim off trailing white space. 319 */ 320 do { 321 fprintf(stderr, "restore > "); 322 (void) fflush(stderr); 323 if (fgets(input, BUFSIZ, terminal) == NULL) { 324 strcpy(cmd, "quit"); 325 return; 326 } 327 } while (input[0] == '\n'); 328 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--) 329 /* trim off trailing white space and newline */; 330 *++cp = '\0'; 331 /* 332 * Copy the command into "cmd". 333 */ 334 cp = copynext(input, cmd); 335 ap->cmd = cmd; 336 /* 337 * If no argument, use curdir as the default. 338 */ 339 if (*cp == '\0') { 340 (void) strncpy(name, curdir, size); 341 name[size - 1] = '\0'; 342 return; 343 } 344 nextarg = cp; 345 /* 346 * Find the next argument. 347 */ 348 getnext: 349 cp = copynext(nextarg, rawname); 350 if (*cp == '\0') 351 nextarg = NULL; 352 else 353 nextarg = cp; 354 /* 355 * If it is an absolute pathname, canonicalize it and return it. 356 */ 357 if (rawname[0] == '/') { 358 canon(rawname, name, size); 359 } else { 360 /* 361 * For relative pathnames, prepend the current directory to 362 * it then canonicalize and return it. 363 */ 364 snprintf(output, sizeof(output), "%s/%s", curdir, rawname); 365 canon(output, name, size); 366 } 367 switch (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob)) { 368 case GLOB_NOSPACE: 369 fprintf(stderr, "%s: out of memory\n", ap->cmd); 370 break; 371 case GLOB_NOMATCH: 372 fprintf(stderr, "%s %s: no such file or directory\n", ap->cmd, name); 373 break; 374 } 375 if (ap->glob.gl_pathc == 0) 376 return; 377 ap->freeglob = 1; 378 ap->argcnt = ap->glob.gl_pathc; 379 380 retnext: 381 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size); 382 name[size - 1] = '\0'; 383 if (--ap->argcnt == 0) { 384 ap->freeglob = 0; 385 globfree(&ap->glob); 386 } 387 # undef rawname 388 } 389 390 /* 391 * Strip off the next token of the input. 392 */ 393 static char * 394 copynext(char *input, char *output) 395 { 396 char *cp, *bp; 397 char quote; 398 399 for (cp = input; *cp == ' ' || *cp == '\t'; cp++) 400 /* skip to argument */; 401 bp = output; 402 while (*cp != ' ' && *cp != '\t' && *cp != '\0') { 403 /* 404 * Handle back slashes. 405 */ 406 if (*cp == '\\') { 407 if (*++cp == '\0') { 408 fprintf(stderr, 409 "command lines cannot be continued\n"); 410 continue; 411 } 412 *bp++ = *cp++; 413 continue; 414 } 415 /* 416 * The usual unquoted case. 417 */ 418 if (*cp != '\'' && *cp != '"') { 419 *bp++ = *cp++; 420 continue; 421 } 422 /* 423 * Handle single and double quotes. 424 */ 425 quote = *cp++; 426 while (*cp != quote && *cp != '\0') 427 *bp++ = *cp++; 428 if (*cp++ == '\0') { 429 fprintf(stderr, "missing %c\n", quote); 430 cp--; 431 continue; 432 } 433 } 434 *bp = '\0'; 435 return (cp); 436 } 437 438 /* 439 * Canonicalize file names to always start with ``./'' and 440 * remove any embedded "." and ".." components. 441 */ 442 void 443 canon(char *rawname, char *canonname, size_t len) 444 { 445 char *cp, *np; 446 447 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0) 448 (void) strcpy(canonname, ""); 449 else if (rawname[0] == '/') 450 (void) strcpy(canonname, "."); 451 else 452 (void) strcpy(canonname, "./"); 453 if (strlen(canonname) + strlen(rawname) >= len) { 454 fprintf(stderr, "canonname: not enough buffer space\n"); 455 done(1); 456 } 457 458 (void) strcat(canonname, rawname); 459 /* 460 * Eliminate multiple and trailing '/'s 461 */ 462 for (cp = np = canonname; *np != '\0'; cp++) { 463 *cp = *np++; 464 while (*cp == '/' && *np == '/') 465 np++; 466 } 467 *cp = '\0'; 468 if (*--cp == '/') 469 *cp = '\0'; 470 /* 471 * Eliminate extraneous "." and ".." from pathnames. 472 */ 473 for (np = canonname; *np != '\0'; ) { 474 np++; 475 cp = np; 476 while (*np != '/' && *np != '\0') 477 np++; 478 if (np - cp == 1 && *cp == '.') { 479 cp--; 480 (void) strcpy(cp, np); 481 np = cp; 482 } 483 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) { 484 cp--; 485 while (cp > &canonname[1] && *--cp != '/') 486 /* find beginning of name */; 487 (void) strcpy(cp, np); 488 np = cp; 489 } 490 } 491 } 492 493 /* 494 * Do an "ls" style listing of a directory 495 */ 496 static void 497 printlist(char *name, char *basename) 498 { 499 struct afile *fp, *list, *listp; 500 struct direct *dp; 501 struct afile single; 502 RST_DIR *dirp; 503 int entries, len, namelen; 504 char locname[MAXPATHLEN]; 505 506 dp = pathsearch(name); 507 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 508 (!vflag && dp->d_ino == WINO)) 509 return; 510 if ((dirp = rst_opendir(name)) == NULL) { 511 entries = 1; 512 list = &single; 513 mkentry(name, dp, list); 514 len = strlen(basename) + 1; 515 if (strlen(name) - len > single.len) { 516 freename(single.fname); 517 single.fname = savename(&name[len]); 518 single.len = strlen(single.fname); 519 } 520 } else { 521 entries = 0; 522 while ((dp = rst_readdir(dirp))) 523 entries++; 524 rst_closedir(dirp); 525 list = (struct afile *)malloc(entries * sizeof(struct afile)); 526 if (list == NULL) { 527 fprintf(stderr, "ls: out of memory\n"); 528 return; 529 } 530 if ((dirp = rst_opendir(name)) == NULL) 531 panic("directory reopen failed\n"); 532 fprintf(stderr, "%s:\n", name); 533 entries = 0; 534 listp = list; 535 (void)strlcpy(locname, name, MAXPATHLEN); 536 (void)strlcat(locname, "/", MAXPATHLEN); 537 namelen = strlen(locname); 538 while ((dp = rst_readdir(dirp))) { 539 if (dp == NULL) 540 break; 541 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) 542 continue; 543 if (!vflag && (dp->d_ino == WINO || 544 strcmp(dp->d_name, ".") == 0 || 545 strcmp(dp->d_name, "..") == 0)) 546 continue; 547 locname[namelen] = '\0'; 548 if (namelen + dp->d_namlen >= MAXPATHLEN) { 549 fprintf(stderr, "%s%s: name exceeds %d char\n", 550 locname, dp->d_name, MAXPATHLEN); 551 } else { 552 (void)strlcat(locname, dp->d_name, MAXPATHLEN); 553 mkentry(locname, dp, listp++); 554 entries++; 555 } 556 } 557 rst_closedir(dirp); 558 if (entries == 0) { 559 fprintf(stderr, "\n"); 560 free(list); 561 return; 562 } 563 qsort((char *)list, entries, sizeof(struct afile), fcmp); 564 } 565 formatf(list, entries); 566 if (dirp != NULL) { 567 for (fp = listp - 1; fp >= list; fp--) 568 freename(fp->fname); 569 fprintf(stderr, "\n"); 570 free(list); 571 } 572 } 573 574 /* 575 * Read the contents of a directory. 576 */ 577 static void 578 mkentry(char *name, struct direct *dp, struct afile *fp) 579 { 580 char *cp; 581 struct entry *np; 582 583 fp->fnum = dp->d_ino; 584 fp->fname = savename(dp->d_name); 585 for (cp = fp->fname; *cp; cp++) 586 if (!vflag && !isprint((unsigned char)*cp)) 587 *cp = '?'; 588 fp->len = cp - fp->fname; 589 if (dflag && TSTINO(fp->fnum, dumpmap) == 0) 590 fp->prefix = '^'; 591 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW)) 592 fp->prefix = '*'; 593 else 594 fp->prefix = ' '; 595 switch(dp->d_type) { 596 597 default: 598 fprintf(stderr, "Warning: undefined file type %d\n", 599 dp->d_type); 600 /* FALLTHROUGH */ 601 case DT_REG: 602 fp->postfix = ' '; 603 break; 604 605 case DT_LNK: 606 fp->postfix = '@'; 607 break; 608 609 case DT_FIFO: 610 case DT_SOCK: 611 fp->postfix = '='; 612 break; 613 614 case DT_CHR: 615 case DT_BLK: 616 fp->postfix = '#'; 617 break; 618 619 case DT_WHT: 620 fp->postfix = '%'; 621 break; 622 623 case DT_UNKNOWN: 624 case DT_DIR: 625 if (inodetype(dp->d_ino) == NODE) 626 fp->postfix = '/'; 627 else 628 fp->postfix = ' '; 629 break; 630 } 631 return; 632 } 633 634 /* 635 * Print out a pretty listing of a directory 636 */ 637 static void 638 formatf(struct afile *list, int nentry) 639 { 640 struct afile *fp, *endlist; 641 int width, bigino, haveprefix, havepostfix; 642 int i, j, w, precision, columns, lines; 643 644 width = 0; 645 haveprefix = 0; 646 havepostfix = 0; 647 bigino = ROOTINO; 648 endlist = &list[nentry]; 649 for (fp = &list[0]; fp < endlist; fp++) { 650 if (bigino < fp->fnum) 651 bigino = fp->fnum; 652 if (width < fp->len) 653 width = fp->len; 654 if (fp->prefix != ' ') 655 haveprefix = 1; 656 if (fp->postfix != ' ') 657 havepostfix = 1; 658 } 659 if (haveprefix) 660 width++; 661 if (havepostfix) 662 width++; 663 if (vflag) { 664 for (precision = 0, i = bigino; i > 0; i /= 10) 665 precision++; 666 width += precision + 1; 667 } 668 width++; 669 columns = 81 / width; 670 if (columns == 0) 671 columns = 1; 672 lines = howmany(nentry, columns); 673 for (i = 0; i < lines; i++) { 674 for (j = 0; j < columns; j++) { 675 fp = &list[j * lines + i]; 676 if (vflag) { 677 fprintf(stderr, "%*ju ", 678 precision, (uintmax_t)fp->fnum); 679 fp->len += precision + 1; 680 } 681 if (haveprefix) { 682 putc(fp->prefix, stderr); 683 fp->len++; 684 } 685 fprintf(stderr, "%s", fp->fname); 686 if (havepostfix) { 687 putc(fp->postfix, stderr); 688 fp->len++; 689 } 690 if (fp + lines >= endlist) { 691 fprintf(stderr, "\n"); 692 break; 693 } 694 for (w = fp->len; w < width; w++) 695 putc(' ', stderr); 696 } 697 } 698 } 699 700 /* 701 * Skip over directory entries that are not on the tape 702 * 703 * First have to get definition of a dirent. 704 */ 705 #undef DIRBLKSIZ 706 #include <dirent.h> 707 #undef d_ino 708 709 struct dirent * 710 glob_readdir(void *dirp) 711 { 712 struct direct *dp; 713 static struct dirent adirent; 714 715 while ((dp = rst_readdir(dirp)) != NULL) { 716 if (!vflag && dp->d_ino == WINO) 717 continue; 718 if (dflag || TSTINO(dp->d_ino, dumpmap)) 719 break; 720 } 721 if (dp == NULL) 722 return (NULL); 723 adirent.d_fileno = dp->d_ino; 724 adirent.d_namlen = dp->d_namlen; 725 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1); 726 return (&adirent); 727 } 728 729 /* 730 * Return st_mode information in response to stat or lstat calls 731 */ 732 static int 733 glob_stat(const char *name, struct stat *stp) 734 { 735 struct direct *dp; 736 737 dp = pathsearch(name); 738 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 739 (!vflag && dp->d_ino == WINO)) 740 return (-1); 741 if (inodetype(dp->d_ino) == NODE) 742 stp->st_mode = IFDIR; 743 else 744 stp->st_mode = IFREG; 745 return (0); 746 } 747 748 /* 749 * Comparison routine for qsort. 750 */ 751 static int 752 fcmp(const void *f1, const void *f2) 753 { 754 return (strcoll(((struct afile *)f1)->fname, 755 ((struct afile *)f2)->fname)); 756 } 757 758 /* 759 * respond to interrupts 760 */ 761 void 762 onintr(int signo __unused) 763 { 764 if (command == 'i' && runshell) 765 longjmp(reset, 1); 766 if (reply("restore interrupted, continue") == FAIL) 767 done(1); 768 } 769