1 /*	$OpenBSD: tape.c,v 1.31 2005/06/14 19:46:05 millert Exp $	*/
2 /*	$NetBSD: tape.c,v 1.26 1997/04/15 07:12:25 lukem Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)tape.c	8.6 (Berkeley) 9/13/94";
41 #else
42 static const char rcsid[] = "$OpenBSD: tape.c,v 1.31 2005/06/14 19:46:05 millert Exp $";
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/mtio.h>
49 #include <sys/stat.h>
50 #include <sys/endian.h>
51 
52 #include <ufs/ufs/dinode.h>
53 #include <protocols/dumprestore.h>
54 
55 #include <err.h>
56 #include <fcntl.h>
57 #include <paths.h>
58 #include <setjmp.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "restore.h"
65 #include "extern.h"
66 
67 static long	fssize = MAXBSIZE;
68 static int	mt = -1;
69 static int	pipein = 0;
70 static char	magtape[BUFSIZ];
71 static int	blkcnt;
72 static int	numtrec;
73 static char	*tapebuf;
74 static union	u_spcl endoftapemark;
75 static long	blksread;		/* blocks read since last header */
76 static long	tpblksread = 0;		/* TP_BSIZE blocks read */
77 static long	tapesread;
78 static jmp_buf	restart;
79 static int	gettingfile = 0;	/* restart has a valid frame */
80 static char	*host = NULL;
81 
82 static int	ofile;
83 static char	*map;
84 static char	lnkbuf[MAXPATHLEN + 1];
85 static size_t	pathlen;
86 
87 int		oldinofmt;	/* old inode format conversion required */
88 int		Bcvt;		/* Swap Bytes (for CCI or sun) */
89 static int	Qcvt;		/* Swap quads (for sun) */
90 
91 #define	FLUSHTAPEBUF()	blkcnt = ntrec + 1
92 
93 static void	 accthdr(struct s_spcl *);
94 static int	 checksum(int *);
95 static void	 findinode(struct s_spcl *);
96 static void	 findtapeblksize(void);
97 static int	 gethead(struct s_spcl *);
98 static void	 readtape(char *);
99 static void	 setdumpnum(void);
100 static void	 swap_header(struct s_spcl *);
101 static void	 terminateinput(void);
102 static void	 xtrfile(char *, size_t);
103 static void	 xtrlnkfile(char *, size_t);
104 static void	 xtrlnkskip(char *, size_t);
105 static void	 xtrmap(char *, size_t);
106 static void	 xtrmapskip(char *, size_t);
107 static void	 xtrskip(char *, size_t);
108 
109 /*
110  * Set up an input source
111  */
112 void
setinput(char * source)113 setinput(char *source)
114 {
115 	FLUSHTAPEBUF();
116 	if (bflag)
117 		newtapebuf(ntrec);
118 	else
119 		newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
120 	terminal = stdin;
121 
122 #ifdef RRESTORE
123 	if (strchr(source, ':')) {
124 		host = source;
125 		source = strchr(host, ':');
126 		*source++ = '\0';
127 		if (rmthost(host) == 0)
128 			exit(1);
129 	} else
130 #endif
131 	if (strcmp(source, "-") == 0) {
132 		/*
133 		 * Since input is coming from a pipe we must establish
134 		 * our own connection to the terminal.
135 		 */
136 		terminal = fopen(_PATH_TTY, "r");
137 		if (terminal == NULL) {
138 			warn("cannot open %s", _PATH_TTY);
139 			terminal = fopen(_PATH_DEVNULL, "r");
140 			if (terminal == NULL)
141 				err(1, "cannot open %s", _PATH_DEVNULL);
142 		}
143 		pipein++;
144 	}
145 	(void)strlcpy(magtape, source, sizeof magtape);
146 }
147 
148 void
newtapebuf(long size)149 newtapebuf(long size)
150 {
151 	static long tapebufsize = -1;
152 
153 	ntrec = size;
154 	if (size <= tapebufsize)
155 		return;
156 	if (tapebuf != NULL)
157 		free(tapebuf);
158 	tapebuf = malloc(size * TP_BSIZE);
159 	if (tapebuf == NULL)
160 		errx(1, "Cannot allocate space for tape buffer");
161 	tapebufsize = size;
162 }
163 
164 /*
165  * Verify that the tape drive can be accessed and
166  * that it actually is a dump tape.
167  */
168 void
setup(void)169 setup(void)
170 {
171 	int i, j, *ip;
172 	struct stat stbuf;
173 
174 	Vprintf(stdout, "Verify tape and initialize maps\n");
175 #ifdef RRESTORE
176 	if (host)
177 		mt = rmtopen(magtape, 0);
178 	else
179 #endif
180 	if (pipein)
181 		mt = 0;
182 	else
183 		mt = open(magtape, O_RDONLY);
184 	if (mt < 0)
185 		err(1, "%s", magtape);
186 	volno = 1;
187 	setdumpnum();
188 	FLUSHTAPEBUF();
189 	if (!pipein && !bflag)
190 		findtapeblksize();
191 	if (gethead(&spcl) == FAIL) {
192 		blkcnt--; /* push back this block */
193 		blksread--;
194 		tpblksread--;
195 		cvtflag++;
196 		if (gethead(&spcl) == FAIL)
197 			errx(1, "Tape is not a dump tape");
198 		(void)fputs("Converting to new file system format.\n", stderr);
199 	}
200 	if (pipein) {
201 		endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
202 		endoftapemark.s_spcl.c_type = TS_END;
203 		ip = (int *)&endoftapemark;
204 		j = sizeof(union u_spcl) / sizeof(int);
205 		i = 0;
206 		do
207 			i += *ip++;
208 		while (--j);
209 		endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
210 	}
211 	if (vflag || command == 't')
212 		printdumpinfo();
213 	dumptime = spcl.c_ddate;
214 	dumpdate = spcl.c_date;
215 	if (stat(".", &stbuf) < 0)
216 		err(1, "cannot stat .");
217 	if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
218 		fssize = TP_BSIZE;
219 	if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
220 		fssize = stbuf.st_blksize;
221 	if (((fssize - 1) & fssize) != 0)
222 		errx(1, "bad block size %ld", fssize);
223 	if (spcl.c_volume != 1)
224 		errx(1, "Tape is not volume 1 of the dump");
225 	if (gethead(&spcl) == FAIL) {
226 		Dprintf(stdout, "header read failed at %ld blocks\n", blksread);
227 		panic("no header after volume mark!\n");
228 	}
229 	findinode(&spcl);
230 	if (spcl.c_type != TS_CLRI)
231 		errx(1, "Cannot find file removal list");
232 	maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
233 	Dprintf(stdout, "maxino = %d\n", maxino);
234 	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
235 	if (map == NULL)
236 		panic("no memory for active inode map\n");
237 	usedinomap = map;
238 	curfile.action = USING;
239 	getfile(xtrmap, xtrmapskip);
240 	if (spcl.c_type != TS_BITS)
241 		errx(1, "Cannot find file dump list");
242 	map = calloc((size_t)1, (size_t)howmany(maxino, NBBY));
243 	if (map == NULL)
244 		panic("no memory for file dump list\n");
245 	dumpmap = map;
246 	curfile.action = USING;
247 	getfile(xtrmap, xtrmapskip);
248 }
249 
250 /*
251  * Prompt user to load a new dump volume.
252  * "Nextvol" is the next suggested volume to use.
253  * This suggested volume is enforced when doing full
254  * or incremental restores, but can be overrridden by
255  * the user when only extracting a subset of the files.
256  */
257 void
getvol(long nextvol)258 getvol(long nextvol)
259 {
260 	long newvol = 0, savecnt, wantnext, i;
261 	union u_spcl tmpspcl;
262 #	define tmpbuf tmpspcl.s_spcl
263 	char buf[TP_BSIZE];
264 
265 	if (nextvol == 1) {
266 		tapesread = 0;
267 		gettingfile = 0;
268 	}
269 	if (pipein) {
270 		if (nextvol != 1)
271 			panic("Changing volumes on pipe input?\n");
272 		if (volno == 1)
273 			return;
274 		goto gethdr;
275 	}
276 	savecnt = blksread;
277 again:
278 	if (pipein)
279 		exit(1); /* pipes do not get a second chance */
280 	if (command == 'R' || command == 'r' || curfile.action != SKIP) {
281 		newvol = nextvol;
282 		wantnext = 1;
283 	} else {
284 		newvol = 0;
285 		wantnext = 0;
286 	}
287 	while (newvol <= 0) {
288 		if (tapesread == 0) {
289 			fprintf(stderr, "%s%s%s%s%s",
290 			    "You have not read any tapes yet.\n",
291 			    "Unless you know which volume your",
292 			    " file(s) are on you should start\n",
293 			    "with the last volume and work",
294 			    " towards the first.\n");
295 		} else {
296 			fprintf(stderr, "You have read volumes");
297 			strlcpy(buf, ": ", sizeof buf);
298 			for (i = 1; i < 32; i++)
299 				if (tapesread & (1 << i)) {
300 					fprintf(stderr, "%s%ld", buf, i);
301 					strlcpy(buf, ", ", sizeof buf);
302 				}
303 			fprintf(stderr, "\n");
304 		}
305 		do	{
306 			fprintf(stderr, "Specify next volume #: ");
307 			(void)fflush(stderr);
308 			(void)fgets(buf, TP_BSIZE, terminal);
309 		} while (!feof(terminal) && buf[0] == '\n');
310 		if (feof(terminal))
311 			exit(1);
312 		newvol = atoi(buf);
313 		if (newvol <= 0) {
314 			fprintf(stderr,
315 			    "Volume numbers are positive numerics\n");
316 		}
317 	}
318 	if (newvol == volno) {
319 		tapesread |= 1 << volno;
320 		return;
321 	}
322 	closemt();
323 	fprintf(stderr, "Mount tape volume %ld\n", newvol);
324 	fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
325 	fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
326 	(void)fflush(stderr);
327 	if (fgets(buf, TP_BSIZE, terminal) == NULL || feof(terminal))
328 		exit(1);
329 	i = strlen(buf);
330 	if (i > 0 && buf[--i] == '\n')
331 		buf[i] = '\0';
332 	if (strcmp(buf, "none") == 0) {
333 		terminateinput();
334 		return;
335 	}
336 	if (buf[0] != '\0')
337 		(void)strlcpy(magtape, buf, sizeof magtape);
338 
339 #ifdef RRESTORE
340 	if (host)
341 		mt = rmtopen(magtape, 0);
342 	else
343 #endif
344 		mt = open(magtape, O_RDONLY);
345 
346 	if (mt == -1) {
347 		fprintf(stderr, "Cannot open %s\n", magtape);
348 		volno = -1;
349 		goto again;
350 	}
351 gethdr:
352 	volno = newvol;
353 	setdumpnum();
354 	FLUSHTAPEBUF();
355 	if (gethead(&tmpbuf) == FAIL) {
356 		Dprintf(stdout, "header read failed at %ld blocks\n", blksread);
357 		fprintf(stderr, "tape is not dump tape\n");
358 		volno = 0;
359 		goto again;
360 	}
361 	if (tmpbuf.c_volume != volno) {
362 		fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
363 		volno = 0;
364 		goto again;
365 	}
366 	if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
367 		fprintf(stderr, "Wrong dump date\n\tgot: %s",
368 			ctime(&tmpbuf.c_date));
369 		fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
370 		volno = 0;
371 		goto again;
372 	}
373 	tapesread |= 1 << volno;
374 	blksread = savecnt;
375  	/*
376  	 * If continuing from the previous volume, skip over any
377  	 * blocks read already at the end of the previous volume.
378  	 *
379  	 * If coming to this volume at random, skip to the beginning
380  	 * of the next record.
381  	 */
382 	Dprintf(stdout, "read %ld recs, tape starts with %d\n",
383 		tpblksread, tmpbuf.c_firstrec);
384  	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
385  		if (!wantnext) {
386  			tpblksread = tmpbuf.c_firstrec;
387  			for (i = tmpbuf.c_count; i > 0; i--)
388  				readtape(buf);
389  		} else if (tmpbuf.c_firstrec > 0 &&
390 			   tmpbuf.c_firstrec < tpblksread - 1) {
391 			/*
392 			 * -1 since we've read the volume header
393 			 */
394  			i = tpblksread - tmpbuf.c_firstrec - 1;
395 			Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
396 				i, (i == 1) ? "" : "s");
397  			while (--i >= 0)
398  				readtape(buf);
399  		}
400  	}
401 	if (curfile.action == USING) {
402 		if (volno == 1)
403 			panic("active file into volume 1\n");
404 		return;
405 	}
406 	/*
407 	 * Skip up to the beginning of the next record
408 	 */
409 	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
410 		for (i = tmpbuf.c_count; i > 0; i--)
411 			readtape(buf);
412 	(void)gethead(&spcl);
413 	findinode(&spcl);
414 	if (gettingfile) {
415 		gettingfile = 0;
416 		longjmp(restart, 1);
417 	}
418 }
419 
420 /*
421  * Handle unexpected EOF.
422  */
423 static void
terminateinput(void)424 terminateinput(void)
425 {
426 
427 	if (gettingfile && curfile.action == USING) {
428 		printf("Warning: %s %s\n",
429 		    "End-of-input encountered while extracting", curfile.name);
430 	}
431 	curfile.name = "<name unknown>";
432 	curfile.action = UNKNOWN;
433 	curfile.dip = NULL;
434 	curfile.ino = maxino;
435 	if (gettingfile) {
436 		gettingfile = 0;
437 		longjmp(restart, 1);
438 	}
439 }
440 
441 /*
442  * handle multiple dumps per tape by skipping forward to the
443  * appropriate one.
444  */
445 static void
setdumpnum(void)446 setdumpnum(void)
447 {
448 	struct mtop tcom;
449 
450 	if (dumpnum == 1 || volno != 1)
451 		return;
452 	if (pipein)
453 		errx(1, "Cannot have multiple dumps on pipe input");
454 	tcom.mt_op = MTFSF;
455 	tcom.mt_count = dumpnum - 1;
456 #ifdef RRESTORE
457 	if (host)
458 		rmtioctl(MTFSF, dumpnum - 1);
459 	else
460 #endif
461 		if (ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
462 			warn("ioctl MTFSF");
463 }
464 
465 void
printdumpinfo(void)466 printdumpinfo(void)
467 {
468 	fprintf(stdout, "Dump   date: %s", ctime(&spcl.c_date));
469 	fprintf(stdout, "Dumped from: %s",
470 	    (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
471 	if (spcl.c_host[0] == '\0')
472 		return;
473 	fprintf(stderr, "Level %d dump of %s on %s:%s\n",
474 		spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
475 	fprintf(stderr, "Label: %s\n", spcl.c_label);
476 }
477 
478 int
extractfile(char * name)479 extractfile(char *name)
480 {
481 	u_int flags;
482 	mode_t mode;
483 	struct timeval timep[2];
484 	struct entry *ep;
485 
486 	curfile.name = name;
487 	curfile.action = USING;
488 	timep[0].tv_sec = curfile.dip->di_atime;
489 	timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
490 	timep[1].tv_sec = curfile.dip->di_mtime;
491 	timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
492 	mode = curfile.dip->di_mode;
493 	flags = curfile.dip->di_flags;
494 	switch (mode & IFMT) {
495 
496 	default:
497 		fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
498 		skipfile();
499 		return (FAIL);
500 
501 	case IFSOCK:
502 		Vprintf(stdout, "skipped socket %s\n", name);
503 		skipfile();
504 		return (GOOD);
505 
506 	case IFDIR:
507 		if (mflag) {
508 			ep = lookupname(name);
509 			if (ep == NULL || ep->e_flags & EXTRACT)
510 				panic("unextracted directory %s\n", name);
511 			skipfile();
512 			return (GOOD);
513 		}
514 		Vprintf(stdout, "extract file %s\n", name);
515 		return (genliteraldir(name, curfile.ino));
516 
517 	case IFLNK: {
518 			/* Gotta save these, linkit() changes curfile. */
519 			uid_t luid = curfile.dip->di_uid;
520 			gid_t lgid = curfile.dip->di_gid;
521 
522 			lnkbuf[0] = '\0';
523 			pathlen = 0;
524 			getfile(xtrlnkfile, xtrlnkskip);
525 			if (pathlen == 0) {
526 				Vprintf(stdout,
527 				    "%s: zero length symbolic link (ignored)\n",
528 				     name);
529 				return (GOOD);
530 			}
531 			if (linkit(lnkbuf, name, SYMLINK) == FAIL)
532 				return (FAIL);
533 			(void)lchown(name, luid, lgid);
534 			return (GOOD);
535 		}
536 
537 	case IFCHR:
538 	case IFBLK:
539 		Vprintf(stdout, "extract special file %s\n", name);
540 		if (Nflag) {
541 			skipfile();
542 			return (GOOD);
543 		}
544 		if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
545 			warn("%s: cannot create special file", name);
546 			skipfile();
547 			return (FAIL);
548 		}
549 		(void)chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
550 		(void)chmod(name, mode);
551 		(void)chflags(name, flags);
552 		skipfile();
553 		utimes(name, timep);
554 		return (GOOD);
555 
556 	case IFIFO:
557 		Vprintf(stdout, "extract fifo %s\n", name);
558 		if (Nflag) {
559 			skipfile();
560 			return (GOOD);
561 		}
562 		if (mkfifo(name, mode) < 0) {
563 			warn("%s: cannot create fifo", name);
564 			skipfile();
565 			return (FAIL);
566 		}
567 		(void)chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
568 		(void)chmod(name, mode);
569 		(void)chflags(name, flags);
570 		skipfile();
571 		utimes(name, timep);
572 		return (GOOD);
573 
574 	case IFREG:
575 		Vprintf(stdout, "extract file %s\n", name);
576 		if (Nflag) {
577 			skipfile();
578 			return (GOOD);
579 		}
580 		if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
581 		    0666)) < 0) {
582 			warn("%s: cannot create file", name);
583 			skipfile();
584 			return (FAIL);
585 		}
586 		(void)fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
587 		(void)fchmod(ofile, mode);
588 		(void)fchflags(ofile, flags);
589 		getfile(xtrfile, xtrskip);
590 		(void)close(ofile);
591 		utimes(name, timep);
592 		return (GOOD);
593 	}
594 	/* NOTREACHED */
595 }
596 
597 /*
598  * skip over bit maps on the tape
599  */
600 void
skipmaps(void)601 skipmaps(void)
602 {
603 
604 	while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
605 		skipfile();
606 }
607 
608 /*
609  * skip over a file on the tape
610  */
611 void
skipfile(void)612 skipfile(void)
613 {
614 
615 	curfile.action = SKIP;
616 	getfile(xtrnull, xtrnull);
617 }
618 
619 /*
620  * Extract a file from the tape.
621  * When an allocated block is found it is passed to the fill function;
622  * when an unallocated block (hole) is found, a zeroed buffer is passed
623  * to the skip function.
624  *
625  * For some block types (TS_BITS, TS_CLRI), the c_addr map is not meaningful
626  * and no blocks should be skipped.
627  */
628 void
getfile(void (* fill)(char *,size_t),void (* skip)(char *,size_t))629 getfile(void (*fill)(char *, size_t), void (*skip)(char *, size_t))
630 {
631 	int i;
632 	volatile int curblk = 0;
633 	volatile off_t size = spcl.c_dinode.di_size;
634 	static char clearedbuf[MAXBSIZE];
635 	char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
636 	char junk[TP_BSIZE];
637 	volatile int noskip = (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI);
638 
639 	if (spcl.c_type == TS_END)
640 		panic("ran off end of tape\n");
641 	if (spcl.c_magic != NFS_MAGIC)
642 		panic("not at beginning of a file\n");
643 	if (!gettingfile && setjmp(restart) != 0)
644 		return;
645 	gettingfile++;
646 loop:
647 	for (i = 0; i < spcl.c_count; i++) {
648 		if (noskip || spcl.c_addr[i]) {
649 			readtape(&buf[curblk++][0]);
650 			if (curblk == fssize / TP_BSIZE) {
651 				(*fill)((char *)buf, size > TP_BSIZE ?
652 				     fssize :
653 				     ((off_t)curblk - 1) * TP_BSIZE + size);
654 				curblk = 0;
655 			}
656 		} else {
657 			if (curblk > 0) {
658 				(*fill)((char *)buf, size > TP_BSIZE ?
659 				     (curblk * TP_BSIZE) :
660 				     ((off_t)curblk - 1) * TP_BSIZE + size);
661 				curblk = 0;
662 			}
663 			(*skip)(clearedbuf, size > TP_BSIZE ?
664 				TP_BSIZE : size);
665 		}
666 		if ((size -= TP_BSIZE) <= 0) {
667 			for (i++; i < spcl.c_count; i++)
668 				if (noskip || spcl.c_addr[i])
669 					readtape(junk);
670 			break;
671 		}
672 	}
673 	if (gethead(&spcl) == GOOD && size > 0) {
674 		if (spcl.c_type == TS_ADDR)
675 			goto loop;
676 		Dprintf(stdout,
677 			"Missing address (header) block for %s at %ld blocks\n",
678 			curfile.name, blksread);
679 	}
680 	if (curblk > 0)
681 		(*fill)((char *)buf, ((off_t)curblk * TP_BSIZE) + size);
682 	findinode(&spcl);
683 	gettingfile = 0;
684 }
685 
686 /*
687  * Write out the next block of a file.
688  */
689 static void
xtrfile(char * buf,size_t size)690 xtrfile(char *buf, size_t size)
691 {
692 
693 	if (Nflag)
694 		return;
695 	if (write(ofile, buf, size) == -1)
696 		err(1, "write error extracting inode %d, name %s\nwrite",
697 		    curfile.ino, curfile.name);
698 }
699 
700 /*
701  * Skip over a hole in a file.
702  */
703 /* ARGSUSED */
704 static void
xtrskip(char * buf,size_t size)705 xtrskip(char *buf, size_t size)
706 {
707 
708 	if (lseek(ofile, (off_t)size, SEEK_CUR) == -1)
709 		err(1, "seek error extracting inode %d, name %s\nlseek",
710 		    curfile.ino, curfile.name);
711 }
712 
713 /*
714  * Collect the next block of a symbolic link.
715  */
716 static void
xtrlnkfile(char * buf,size_t size)717 xtrlnkfile(char *buf, size_t size)
718 {
719 
720 	pathlen += size;
721 	if (pathlen > MAXPATHLEN)
722 		errx(1, "symbolic link name: %s->%s%s; too long %lu",
723 		    curfile.name, lnkbuf, buf, (u_long)pathlen);
724 	(void)strlcat(lnkbuf, buf, sizeof(lnkbuf));
725 }
726 
727 /*
728  * Skip over a hole in a symbolic link (should never happen).
729  */
730 /* ARGSUSED */
731 static void
xtrlnkskip(char * buf,size_t size)732 xtrlnkskip(char *buf, size_t size)
733 {
734 
735 	errx(1, "unallocated block in symbolic link %s", curfile.name);
736 }
737 
738 /*
739  * Collect the next block of a bit map.
740  */
741 static void
xtrmap(char * buf,size_t size)742 xtrmap(char *buf, size_t size)
743 {
744 
745 	memcpy(map, buf, size);
746 	map += size;
747 }
748 
749 /*
750  * Skip over a hole in a bit map (should never happen).
751  */
752 /* ARGSUSED */
753 static void
xtrmapskip(char * buf,size_t size)754 xtrmapskip(char *buf, size_t size)
755 {
756 
757 	panic("hole in map\n");
758 	map += size;
759 }
760 
761 /*
762  * Noop, when an extraction function is not needed.
763  */
764 /* ARGSUSED */
765 void
xtrnull(char * buf,size_t size)766 xtrnull(char *buf, size_t size)
767 {
768 
769 	return;
770 }
771 
772 /*
773  * Read TP_BSIZE blocks from the input.
774  * Handle read errors, and end of media.
775  */
776 static void
readtape(char * buf)777 readtape(char *buf)
778 {
779 	long rd, newvol, i;
780 	int cnt, seek_failed;
781 
782 	if (blkcnt < numtrec) {
783 		memcpy(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
784 		blksread++;
785 		tpblksread++;
786 		return;
787 	}
788 	for (i = 0; i < ntrec; i++)
789 		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
790 	if (numtrec == 0)
791 		numtrec = ntrec;
792 	cnt = ntrec * TP_BSIZE;
793 	rd = 0;
794 getmore:
795 #ifdef RRESTORE
796 	if (host)
797 		i = rmtread(&tapebuf[rd], cnt);
798 	else
799 #endif
800 		i = read(mt, &tapebuf[rd], cnt);
801 	/*
802 	 * Check for mid-tape short read error.
803 	 * If found, skip rest of buffer and start with the next.
804 	 */
805 	if (!pipein && numtrec < ntrec && i > 0) {
806 		Dprintf(stdout, "mid-media short read error.\n");
807 		numtrec = ntrec;
808 	}
809 	/*
810 	 * Handle partial block read.
811 	 */
812 	if (pipein && i == 0 && rd > 0)
813 		i = rd;
814 	else if (i > 0 && i != ntrec * TP_BSIZE) {
815 		if (pipein) {
816 			rd += i;
817 			cnt -= i;
818 			if (cnt > 0)
819 				goto getmore;
820 			i = rd;
821 		} else {
822 			/*
823 			 * Short read. Process the blocks read.
824 			 */
825 			if (i % TP_BSIZE != 0)
826 				Vprintf(stdout,
827 				    "partial block read: %ld should be %ld\n",
828 				    i, ntrec * TP_BSIZE);
829 			numtrec = i / TP_BSIZE;
830 		}
831 	}
832 	/*
833 	 * Handle read error.
834 	 */
835 	if (i < 0) {
836 		fprintf(stderr, "Tape read error while ");
837 		switch (curfile.action) {
838 		default:
839 			fprintf(stderr, "trying to set up tape\n");
840 			break;
841 		case UNKNOWN:
842 			fprintf(stderr, "trying to resynchronize\n");
843 			break;
844 		case USING:
845 			fprintf(stderr, "restoring %s\n", curfile.name);
846 			break;
847 		case SKIP:
848 			fprintf(stderr, "skipping over inode %d\n",
849 				curfile.ino);
850 			break;
851 		}
852 		if (!yflag && !reply("continue"))
853 			exit(1);
854 		i = ntrec * TP_BSIZE;
855 		memset(tapebuf, 0, i);
856 #ifdef RRESTORE
857 		if (host)
858 			seek_failed = (rmtseek(i, 1) < 0);
859 		else
860 #endif
861 			seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
862 
863 		if (seek_failed)
864 			err(1, "continuation failed");
865 	}
866 	/*
867 	 * Handle end of tape.
868 	 */
869 	if (i == 0) {
870 		Vprintf(stdout, "End-of-tape encountered\n");
871 		if (!pipein) {
872 			newvol = volno + 1;
873 			volno = 0;
874 			numtrec = 0;
875 			getvol(newvol);
876 			readtape(buf);
877 			return;
878 		}
879 		if (rd % TP_BSIZE != 0)
880 			panic("partial block read: %d should be %d\n",
881 				rd, ntrec * TP_BSIZE);
882 		terminateinput();
883 		memcpy(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
884 	}
885 	blkcnt = 0;
886 	memcpy(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
887 	blksread++;
888 	tpblksread++;
889 }
890 
891 static void
findtapeblksize(void)892 findtapeblksize(void)
893 {
894 	long i;
895 
896 	for (i = 0; i < ntrec; i++)
897 		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
898 	blkcnt = 0;
899 #ifdef RRESTORE
900 	if (host)
901 		i = rmtread(tapebuf, ntrec * TP_BSIZE);
902 	else
903 #endif
904 		i = read(mt, tapebuf, ntrec * TP_BSIZE);
905 
906 	if (i <= 0)
907 		err(1, "tape read error");
908 	if (i % TP_BSIZE != 0) {
909 		errx(1,
910 		    "Tape block size (%ld) is not a multiple of dump block size (%d)",
911 		    i, TP_BSIZE);
912 	}
913 	ntrec = i / TP_BSIZE;
914 	numtrec = ntrec;
915 	Vprintf(stdout, "Tape block size is %ld\n", ntrec);
916 }
917 
918 void
closemt(void)919 closemt(void)
920 {
921 
922 	if (mt < 0)
923 		return;
924 #ifdef RRESTORE
925 	if (host)
926 		rmtclose();
927 	else
928 #endif
929 		(void)close(mt);
930 }
931 
932 /*
933  * Read the next block from the tape.
934  * Check to see if it is one of several vintage headers.
935  * If it is an old style header, convert it to a new style header.
936  * If it is not any valid header, return an error.
937  */
938 static int
gethead(struct s_spcl * buf)939 gethead(struct s_spcl *buf)
940 {
941 	long i;
942 	union {
943 		quad_t	qval;
944 		int32_t	val[2];
945 	} qcvt;
946 	union u_ospcl {
947 		char dummy[TP_BSIZE];
948 		struct	s_ospcl {
949 			int32_t   c_type;
950 			int32_t   c_date;
951 			int32_t   c_ddate;
952 			int32_t   c_volume;
953 			int32_t   c_tapea;
954 			u_int16_t c_inumber;
955 			int32_t   c_magic;
956 			int32_t   c_checksum;
957 			struct odinode {
958 				unsigned short odi_mode;
959 				u_int16_t odi_nlink;
960 				u_int16_t odi_uid;
961 				u_int16_t odi_gid;
962 				int32_t   odi_size;
963 				int32_t   odi_rdev;
964 				char	  odi_addr[36];
965 				int32_t   odi_atime;
966 				int32_t   odi_mtime;
967 				int32_t   odi_ctime;
968 			} c_dinode;
969 			int32_t c_count;
970 			char	c_addr[256];
971 		} s_ospcl;
972 	} u_ospcl;
973 
974 	if (!cvtflag) {
975 		readtape((char *)buf);
976 		if (buf->c_magic != NFS_MAGIC) {
977 			if (swap32(buf->c_magic) != NFS_MAGIC)
978 				return (FAIL);
979 			if (!Bcvt) {
980 				Vprintf(stdout, "Note: Doing Byte swapping\n");
981 				Bcvt = 1;
982 			}
983 		}
984 		if (checksum((int *)buf) == FAIL)
985 			return (FAIL);
986 		if (Bcvt)
987 			swap_header(buf);
988 		goto good;
989 	}
990 	readtape((char *)(&u_ospcl.s_ospcl));
991 	memset(buf, 0, (long)TP_BSIZE);
992 	buf->c_type = u_ospcl.s_ospcl.c_type;
993 	buf->c_date = u_ospcl.s_ospcl.c_date;
994 	buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
995 	buf->c_volume = u_ospcl.s_ospcl.c_volume;
996 	buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
997 	buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
998 	buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
999 	buf->c_magic = u_ospcl.s_ospcl.c_magic;
1000 	buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1001 	buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1002 	buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1003 	buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1004 	buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1005 	buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1006 	buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1007 	buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1008 	buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1009 	buf->c_count = u_ospcl.s_ospcl.c_count;
1010 	memcpy(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1011 	if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1012 	    checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1013 		return(FAIL);
1014 	buf->c_magic = NFS_MAGIC;
1015 
1016 good:
1017 	if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1018 	    (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1019 		qcvt.qval = buf->c_dinode.di_size;
1020 		if (qcvt.val[0] || qcvt.val[1]) {
1021 			printf("Note: Doing Quad swapping\n");
1022 			Qcvt = 1;
1023 		}
1024 	}
1025 	if (Qcvt) {
1026 		qcvt.qval = buf->c_dinode.di_size;
1027 		i = qcvt.val[1];
1028 		qcvt.val[1] = qcvt.val[0];
1029 		qcvt.val[0] = i;
1030 		buf->c_dinode.di_size = qcvt.qval;
1031 	}
1032 
1033 	switch (buf->c_type) {
1034 
1035 	case TS_CLRI:
1036 	case TS_BITS:
1037 		/*
1038 		 * Have to patch up missing information in bit map headers
1039 		 */
1040 		buf->c_inumber = 0;
1041 		buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1042 		break;
1043 
1044 	case TS_TAPE:
1045 		if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1046 			oldinofmt = 1;
1047 		/* fall through */
1048 	case TS_END:
1049 		buf->c_inumber = 0;
1050 		break;
1051 
1052 	case TS_INODE:
1053 	case TS_ADDR:
1054 		break;
1055 
1056 	default:
1057 		panic("gethead: unknown inode type %d\n", buf->c_type);
1058 		break;
1059 	}
1060 	/*
1061 	 * If we are restoring a filesystem with old format inodes,
1062 	 * copy the uid/gid to the new location.
1063 	 */
1064 	if (oldinofmt) {
1065 		buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1066 		buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1067 	}
1068 	if (dflag)
1069 		accthdr(buf);
1070 	return(GOOD);
1071 }
1072 
1073 /*
1074  * Check that a header is where it belongs and predict the next header
1075  */
1076 static void
accthdr(struct s_spcl * header)1077 accthdr(struct s_spcl *header)
1078 {
1079 	static ino_t previno = 0x7fffffff;
1080 	static int prevtype;
1081 	static long predict;
1082 	long blks, i;
1083 
1084 	if (header->c_type == TS_TAPE) {
1085 		fprintf(stderr, "Volume header (%s inode format) ",
1086 		    oldinofmt ? "old" : "new");
1087  		if (header->c_firstrec)
1088  			fprintf(stderr, "begins with record %d",
1089  				header->c_firstrec);
1090  		fprintf(stderr, "\n");
1091 		previno = 0x7fffffff;
1092 		return;
1093 	}
1094 	if (previno == 0x7fffffff)
1095 		goto newcalc;
1096 	switch (prevtype) {
1097 	case TS_BITS:
1098 		fprintf(stderr, "Dumped inodes map header");
1099 		break;
1100 	case TS_CLRI:
1101 		fprintf(stderr, "Used inodes map header");
1102 		break;
1103 	case TS_INODE:
1104 		fprintf(stderr, "File header, ino %d", previno);
1105 		break;
1106 	case TS_ADDR:
1107 		fprintf(stderr, "File continuation header, ino %d", previno);
1108 		break;
1109 	case TS_END:
1110 		fprintf(stderr, "End of tape header");
1111 		break;
1112 	}
1113 	if (predict != blksread - 1)
1114 		fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1115 			predict, blksread - 1);
1116 	fprintf(stderr, "\n");
1117 newcalc:
1118 	blks = 0;
1119 	switch (header->c_type) {
1120 
1121 	case TS_BITS:
1122 	case TS_CLRI:
1123 		blks = header->c_count;
1124 		break;
1125 
1126 	case TS_END:
1127 		break;
1128 
1129 	default:
1130 		for (i = 0; i < header->c_count; i++)
1131 			if (header->c_addr[i] != 0)
1132 				blks++;
1133 	}
1134 	predict = blks;
1135 	blksread = 0;
1136 	prevtype = header->c_type;
1137 	previno = header->c_inumber;
1138 }
1139 
1140 /*
1141  * Find an inode header.
1142  * Complain if had to skip, and complain is set.
1143  */
1144 static void
findinode(struct s_spcl * header)1145 findinode(struct s_spcl *header)
1146 {
1147 	static long skipcnt = 0;
1148 	long i;
1149 	char buf[TP_BSIZE];
1150 
1151 	curfile.name = "<name unknown>";
1152 	curfile.action = UNKNOWN;
1153 	curfile.dip = NULL;
1154 	curfile.ino = 0;
1155 	do {
1156 		if (header->c_magic != NFS_MAGIC) {
1157 			skipcnt++;
1158 			while (gethead(header) == FAIL ||
1159 			    header->c_date != dumpdate)
1160 				skipcnt++;
1161 		}
1162 		switch (header->c_type) {
1163 
1164 		case TS_ADDR:
1165 			/*
1166 			 * Skip up to the beginning of the next record
1167 			 */
1168 			for (i = 0; i < header->c_count; i++)
1169 				if (header->c_addr[i])
1170 					readtape(buf);
1171 			while (gethead(header) == FAIL ||
1172 			    header->c_date != dumpdate)
1173 				skipcnt++;
1174 			break;
1175 
1176 		case TS_INODE:
1177 			curfile.dip = &header->c_dinode;
1178 			curfile.ino = header->c_inumber;
1179 			break;
1180 
1181 		case TS_END:
1182 			curfile.ino = maxino;
1183 			break;
1184 
1185 		case TS_CLRI:
1186 			curfile.name = "<file removal list>";
1187 			break;
1188 
1189 		case TS_BITS:
1190 			curfile.name = "<file dump list>";
1191 			break;
1192 
1193 		case TS_TAPE:
1194 			panic("unexpected tape header\n");
1195 			/* NOTREACHED */
1196 
1197 		default:
1198 			panic("unknown tape header type %d\n", spcl.c_type);
1199 			/* NOTREACHED */
1200 
1201 		}
1202 	} while (header->c_type == TS_ADDR);
1203 	if (skipcnt > 0)
1204 		fprintf(stderr, "resync restore, skipped %ld blocks\n", skipcnt);
1205 	skipcnt = 0;
1206 }
1207 
1208 static int
checksum(int * buf)1209 checksum(int *buf)
1210 {
1211 	int i, j;
1212 
1213 	j = sizeof(union u_spcl) / sizeof(int);
1214 	i = 0;
1215 	if (!Bcvt) {
1216 		do
1217 			i += *buf++;
1218 		while (--j);
1219 	} else {
1220 		/* What happens if we want to read restore tapes
1221 			for a 16bit int machine??? */
1222 		do
1223 			i += swap32(*buf++);
1224 		while (--j);
1225 	}
1226 
1227 	if (i != CHECKSUM) {
1228 		fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
1229 			curfile.ino, curfile.name);
1230 		return(FAIL);
1231 	}
1232 	return(GOOD);
1233 }
1234 
1235 #ifdef RRESTORE
1236 #include <stdarg.h>
1237 
1238 void
msg(const char * fmt,...)1239 msg(const char *fmt, ...)
1240 {
1241 	va_list ap;
1242 
1243 	va_start(ap, fmt);
1244 	(void)vfprintf(stderr, fmt, ap);
1245 	va_end(ap);
1246 }
1247 #endif /* RRESTORE */
1248 
1249 static void
swap_header(struct s_spcl * s)1250 swap_header(struct s_spcl *s)
1251 {
1252 	s->c_type = swap32(s->c_type);
1253 	s->c_date = swap32(s->c_date);
1254 	s->c_ddate = swap32(s->c_ddate);
1255 	s->c_volume = swap32(s->c_volume);
1256 	s->c_tapea = swap32(s->c_tapea);
1257 	s->c_inumber = swap32(s->c_inumber);
1258 	s->c_magic = swap32(s->c_magic);
1259 	s->c_checksum = swap32(s->c_checksum);
1260 
1261 	s->c_dinode.di_mode = swap16(s->c_dinode.di_mode);
1262 	s->c_dinode.di_nlink = swap16(s->c_dinode.di_nlink);
1263 	s->c_dinode.di_ouid = swap16(s->c_dinode.di_ouid);
1264 	s->c_dinode.di_ogid = swap16(s->c_dinode.di_ogid);
1265 
1266 	s->c_dinode.di_size = swap64(s->c_dinode.di_size);
1267 
1268 	s->c_dinode.di_atime = swap32(s->c_dinode.di_atime);
1269 	s->c_dinode.di_atimensec = swap32(s->c_dinode.di_atimensec);
1270 	s->c_dinode.di_mtime = swap32(s->c_dinode.di_mtime);
1271 	s->c_dinode.di_mtimensec = swap32(s->c_dinode.di_mtimensec);
1272 	s->c_dinode.di_ctime = swap32(s->c_dinode.di_ctime);
1273 	s->c_dinode.di_ctimensec = swap32(s->c_dinode.di_ctimensec);
1274 
1275 	s->c_dinode.di_flags = swap32(s->c_dinode.di_flags);
1276 	s->c_dinode.di_blocks = swap32(s->c_dinode.di_blocks);
1277 	s->c_dinode.di_gen = swap32(s->c_dinode.di_gen);
1278 	s->c_dinode.di_uid = swap32(s->c_dinode.di_uid);
1279 	s->c_dinode.di_gid = swap32(s->c_dinode.di_gid);
1280 
1281 	s->c_count = swap32(s->c_count);
1282 	s->c_level = swap32(s->c_level);
1283 	s->c_flags = swap32(s->c_flags);
1284 	s->c_firstrec = swap32(s->c_firstrec);
1285 }
1286