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