]> git.wh0rd.org - dump.git/blame - restore/tape.c
Buffer overflow in dump, as reported from Bugtraq
[dump.git] / restore / tape.c
CommitLineData
1227625a
SP
1/*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
b45f51d6 4 * Remy Card <card@Linux.EU.Org>, 1994-1997
ebcbe7f6 5 * Stelian Pop <pop@cybercable.fr>, 1999-2000
1227625a
SP
6 */
7
8/*
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 * (c) UNIX System Laboratories, Inc.
12 * All or some portions of this file are derived from material licensed
13 * to the University of California by American Telephone and Telegraph
14 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
15 * the permission of UNIX System Laboratories, Inc.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 */
45
df9ae507
SP
46#ifndef lint
47static const char rcsid[] =
92a9bf12 48 "$Id: tape.c,v 1.12 2000/03/01 10:16:05 stelian Exp $";
df9ae507
SP
49#endif /* not lint */
50
1227625a
SP
51#include <sys/param.h>
52#include <sys/file.h>
1227625a
SP
53#include <sys/mtio.h>
54#include <sys/stat.h>
55
56#ifdef __linux__
57#include <sys/time.h>
58#include <linux/ext2_fs.h>
59#include <bsdcompat.h>
60#else /* __linux__ */
61#include <ufs/ufs/dinode.h>
62#endif /* __linux__ */
63#include <protocols/dumprestore.h>
64
65#include <errno.h>
ddd2ef55 66#include <compaterr.h>
1227625a
SP
67#include <setjmp.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <unistd.h>
72
73#ifdef __linux__
74#include <ext2fs/ext2fs.h>
75#endif
76
77#include "restore.h"
78#include "extern.h"
79#include "pathnames.h"
80
81static long fssize = MAXBSIZE;
82static int mt = -1;
83static int pipein = 0;
92a9bf12
SP
84static char magtape[MAXPATHLEN];
85static char magtapeprefix[MAXPATHLEN];
1227625a
SP
86static int blkcnt;
87static int numtrec;
88static char *tapebuf;
89static union u_spcl endoftapemark;
90static long blksread; /* blocks read since last header */
91static long tpblksread = 0; /* TP_BSIZE blocks read */
92static long tapesread;
ddd2ef55 93static sigjmp_buf restart;
1227625a
SP
94static int gettingfile = 0; /* restart has a valid frame */
95static char *host = NULL;
96
97static int ofile;
98static char *map;
99static char lnkbuf[MAXPATHLEN + 1];
100static int pathlen;
101
102int oldinofmt; /* old inode format conversion required */
103int Bcvt; /* Swap Bytes (for CCI or sun) */
104static int Qcvt; /* Swap quads (for sun) */
105
106#define FLUSHTAPEBUF() blkcnt = ntrec + 1
107
108static void accthdr __P((struct s_spcl *));
109static int checksum __P((int *));
110static void findinode __P((struct s_spcl *));
111static void findtapeblksize __P((void));
112static int gethead __P((struct s_spcl *));
113static void readtape __P((char *));
114static void setdumpnum __P((void));
ddd2ef55 115static u_int swabi __P((u_int));
1227625a 116static u_long swabl __P((u_long));
ddd2ef55
SP
117static u_char *swab64 __P((u_char *, int));
118static u_char *swab32 __P((u_char *, int));
119static u_char *swab16 __P((u_char *, int));
1227625a 120static void terminateinput __P((void));
ddd2ef55
SP
121static void xtrfile __P((char *, size_t));
122static void xtrlnkfile __P((char *, size_t));
123static void xtrlnkskip __P((char *, size_t));
124static void xtrmap __P((char *, size_t));
125static void xtrmapskip __P((char *, size_t));
126static void xtrskip __P((char *, size_t));
1227625a 127
b45f51d6
SP
128static int readmapflag;
129
1227625a
SP
130/*
131 * Set up an input source
132 */
133void
ddd2ef55 134setinput(char *source)
1227625a
SP
135{
136 FLUSHTAPEBUF();
137 if (bflag)
138 newtapebuf(ntrec);
139 else
140 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
141 terminal = stdin;
142
143#ifdef RRESTORE
144 if (strchr(source, ':')) {
145 host = source;
146 source = strchr(host, ':');
147 *source++ = '\0';
148 if (rmthost(host) == 0)
ddd2ef55 149 exit(1);
1227625a
SP
150 } else
151#endif
152 if (strcmp(source, "-") == 0) {
153 /*
154 * Since input is coming from a pipe we must establish
155 * our own connection to the terminal.
156 */
157 terminal = fopen(_PATH_TTY, "r");
158 if (terminal == NULL) {
ddd2ef55 159 warn("cannot open %s", _PATH_TTY);
1227625a 160 terminal = fopen(_PATH_DEVNULL, "r");
ddd2ef55
SP
161 if (terminal == NULL)
162 err(1, "cannot open %s", _PATH_DEVNULL);
1227625a
SP
163 }
164 pipein++;
165 }
166 setuid(getuid()); /* no longer need or want root privileges */
394240ce 167 if (Mflag) {
92a9bf12
SP
168 strncpy(magtapeprefix, source, MAXPATHLEN);
169 magtapeprefix[MAXPATHLEN-1] = '\0';
170 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
394240ce
SP
171 }
172 else
92a9bf12
SP
173 strncpy(magtape, source, MAXPATHLEN);
174 magtape[MAXPATHLEN - 1] = '\0';
1227625a
SP
175}
176
177void
ddd2ef55 178newtapebuf(long size)
1227625a 179{
ddd2ef55 180 static int tapebufsize = -1;
1227625a
SP
181
182 ntrec = size;
183 if (size <= tapebufsize)
184 return;
185 if (tapebuf != NULL)
186 free(tapebuf);
187 tapebuf = malloc(size * TP_BSIZE);
ddd2ef55
SP
188 if (tapebuf == NULL)
189 errx(1, "Cannot allocate space for tape buffer");
1227625a
SP
190 tapebufsize = size;
191}
192
193/*
194 * Verify that the tape drive can be accessed and
195 * that it actually is a dump tape.
196 */
197void
ddd2ef55 198setup(void)
1227625a
SP
199{
200 int i, j, *ip;
201 struct stat stbuf;
202
ddd2ef55 203 Vprintf(stdout, "Verify tape and initialize maps\n");
1227625a
SP
204#ifdef RRESTORE
205 if (host)
206 mt = rmtopen(magtape, 0);
207 else
208#endif
209 if (pipein)
210 mt = 0;
211 else
212 mt = open(magtape, O_RDONLY, 0);
ddd2ef55
SP
213 if (mt < 0)
214 err(1, "%s", magtape);
1227625a
SP
215 volno = 1;
216 setdumpnum();
217 FLUSHTAPEBUF();
218 if (!pipein && !bflag)
219 findtapeblksize();
220 if (gethead(&spcl) == FAIL) {
221 blkcnt--; /* push back this block */
222 blksread--;
223 tpblksread--;
224 cvtflag++;
ddd2ef55
SP
225 if (gethead(&spcl) == FAIL)
226 errx(1, "Tape is not a dump tape");
1227625a
SP
227 fprintf(stderr, "Converting to new file system format.\n");
228 }
229 if (pipein) {
230 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
231 endoftapemark.s_spcl.c_type = TS_END;
232 ip = (int *)&endoftapemark;
233 j = sizeof(union u_spcl) / sizeof(int);
234 i = 0;
235 do
236 i += *ip++;
237 while (--j);
238 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
239 }
240 if (vflag || command == 't' || command == 'C')
241 printdumpinfo();
242 if (filesys == NULL) {
243 filesys = spcl.c_filesys;
244 }
245 dumptime = spcl.c_ddate;
246 dumpdate = spcl.c_date;
ddd2ef55
SP
247 if (stat(".", &stbuf) < 0)
248 err(1, "cannot stat .");
b45f51d6
SP
249 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
250 fssize = TP_BSIZE;
251 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
1227625a 252 fssize = stbuf.st_blksize;
ddd2ef55
SP
253 if (((fssize - 1) & fssize) != 0)
254 errx(1, "bad block size %ld", fssize);
255 if (spcl.c_volume != 1)
256 errx(1, "Tape is not volume 1 of the dump");
1227625a 257 if (gethead(&spcl) == FAIL) {
ddd2ef55 258 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
1227625a
SP
259 panic("no header after volume mark!\n");
260 }
261 findinode(&spcl);
ddd2ef55
SP
262 if (spcl.c_type != TS_CLRI)
263 errx(1, "Cannot find file removal list");
1227625a 264 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
b9c89cf4 265 Dprintf(stdout, "maxino = %ld\n", (long)maxino);
1227625a
SP
266 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
267 if (map == NULL)
268 panic("no memory for active inode map\n");
269 usedinomap = map;
270 curfile.action = USING;
271 getfile(xtrmap, xtrmapskip);
ddd2ef55
SP
272 if (spcl.c_type != TS_BITS)
273 errx(1, "Cannot find file dump list");
1227625a
SP
274 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
275 if (map == (char *)NULL)
276 panic("no memory for file dump list\n");
277 dumpmap = map;
278 curfile.action = USING;
279 getfile(xtrmap, xtrmapskip);
280 /*
281 * If there may be whiteout entries on the tape, pretend that the
282 * whiteout inode exists, so that the whiteout entries can be
283 * extracted.
284 */
285 if (oldinofmt == 0)
286 SETINO(WINO, dumpmap);
287}
288
289/*
290 * Prompt user to load a new dump volume.
291 * "Nextvol" is the next suggested volume to use.
292 * This suggested volume is enforced when doing full
b45f51d6 293 * or incremental restores, but can be overridden by
1227625a
SP
294 * the user when only extracting a subset of the files.
295 */
296void
ddd2ef55 297getvol(long nextvol)
1227625a 298{
ddd2ef55 299 long newvol = 0, savecnt = 0, wantnext = 0, i;
1227625a
SP
300 union u_spcl tmpspcl;
301# define tmpbuf tmpspcl.s_spcl
302 char buf[TP_BSIZE];
394240ce 303 int haderror = 0;
1227625a
SP
304
305 if (nextvol == 1) {
306 tapesread = 0;
307 gettingfile = 0;
308 }
309 if (pipein) {
310 if (nextvol != 1)
311 panic("Changing volumes on pipe input?\n");
312 if (volno == 1)
313 return;
314 goto gethdr;
315 }
316 savecnt = blksread;
317again:
318 if (pipein)
ddd2ef55 319 exit(1); /* pipes do not get a second chance */
1227625a
SP
320 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
321 newvol = nextvol;
322 wantnext = 1;
b45f51d6 323 } else {
1227625a
SP
324 newvol = 0;
325 wantnext = 0;
326 }
327 while (newvol <= 0) {
328 if (tapesread == 0) {
329 fprintf(stderr, "%s%s%s%s%s",
330 "You have not read any tapes yet.\n",
331 "Unless you know which volume your",
332 " file(s) are on you should start\n",
333 "with the last volume and work",
334 " towards the first.\n");
335 } else {
336 fprintf(stderr, "You have read volumes");
337 strcpy(buf, ": ");
338 for (i = 1; i < 32; i++)
339 if (tapesread & (1 << i)) {
ddd2ef55 340 fprintf(stderr, "%s%ld", buf, (long)i);
1227625a
SP
341 strcpy(buf, ", ");
342 }
343 fprintf(stderr, "\n");
344 }
345 do {
346 fprintf(stderr, "Specify next volume #: ");
347 (void) fflush(stderr);
348 (void) fgets(buf, BUFSIZ, terminal);
349 } while (!feof(terminal) && buf[0] == '\n');
350 if (feof(terminal))
ddd2ef55 351 exit(1);
1227625a
SP
352 newvol = atoi(buf);
353 if (newvol <= 0) {
354 fprintf(stderr,
355 "Volume numbers are positive numerics\n");
356 }
357 }
358 if (newvol == volno) {
359 tapesread |= 1 << volno;
360 return;
361 }
362 closemt();
394240ce 363 if (Mflag) {
92a9bf12
SP
364 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
365 magtape[MAXPATHLEN - 1] = '\0';
1227625a 366 }
394240ce
SP
367 if (!Mflag || haderror) {
368 haderror = 0;
369 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
370 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
371 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
372 (void) fflush(stderr);
373 (void) fgets(buf, BUFSIZ, terminal);
374 if (feof(terminal))
375 exit(1);
376 if (!strcmp(buf, "none\n")) {
377 terminateinput();
378 return;
379 }
380 if (buf[0] != '\n') {
381 (void) strcpy(magtape, buf);
382 magtape[strlen(magtape) - 1] = '\0';
383 }
1227625a
SP
384 }
385#ifdef RRESTORE
386 if (host)
387 mt = rmtopen(magtape, 0);
388 else
389#endif
390 mt = open(magtape, O_RDONLY, 0);
391
392 if (mt == -1) {
393 fprintf(stderr, "Cannot open %s\n", magtape);
394 volno = -1;
394240ce 395 haderror = 1;
1227625a
SP
396 goto again;
397 }
398gethdr:
399 volno = newvol;
400 setdumpnum();
401 FLUSHTAPEBUF();
402 if (gethead(&tmpbuf) == FAIL) {
ddd2ef55 403 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
1227625a
SP
404 fprintf(stderr, "tape is not dump tape\n");
405 volno = 0;
394240ce 406 haderror = 1;
1227625a
SP
407 goto again;
408 }
409 if (tmpbuf.c_volume != volno) {
410 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
411 volno = 0;
394240ce 412 haderror = 1;
1227625a
SP
413 goto again;
414 }
415 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
416#ifdef __linux__
417 fprintf(stderr, "Wrong dump date\n\tgot: %s",
418 ctime4(&tmpbuf.c_date));
419 fprintf(stderr, "\twanted: %s", ctime4(&dumpdate));
420#else
421 fprintf(stderr, "Wrong dump date\n\tgot: %s",
422 ctime(&tmpbuf.c_date));
423 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
424#endif
425 volno = 0;
394240ce 426 haderror = 1;
1227625a
SP
427 goto again;
428 }
429 tapesread |= 1 << volno;
430 blksread = savecnt;
431 /*
432 * If continuing from the previous volume, skip over any
433 * blocks read already at the end of the previous volume.
434 *
435 * If coming to this volume at random, skip to the beginning
436 * of the next record.
437 */
ddd2ef55
SP
438 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
439 tpblksread, (long)tmpbuf.c_firstrec);
1227625a
SP
440 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
441 if (!wantnext) {
442 tpblksread = tmpbuf.c_firstrec;
443 for (i = tmpbuf.c_count; i > 0; i--)
444 readtape(buf);
445 } else if (tmpbuf.c_firstrec > 0 &&
446 tmpbuf.c_firstrec < tpblksread - 1) {
447 /*
448 * -1 since we've read the volume header
449 */
450 i = tpblksread - tmpbuf.c_firstrec - 1;
ddd2ef55
SP
451 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
452 (long)i, i > 1 ? "s" : "");
1227625a
SP
453 while (--i >= 0)
454 readtape(buf);
455 }
456 }
457 if (curfile.action == USING) {
458 if (volno == 1)
459 panic("active file into volume 1\n");
460 return;
461 }
462 /*
463 * Skip up to the beginning of the next record
464 */
465 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
466 for (i = tmpbuf.c_count; i > 0; i--)
467 readtape(buf);
468 (void) gethead(&spcl);
469 findinode(&spcl);
470 if (gettingfile) {
471 gettingfile = 0;
ddd2ef55 472 siglongjmp(restart, 1);
1227625a
SP
473 }
474}
475
476/*
477 * Handle unexpected EOF.
478 */
479static void
ddd2ef55 480terminateinput(void)
1227625a
SP
481{
482
483 if (gettingfile && curfile.action == USING) {
484 printf("Warning: %s %s\n",
485 "End-of-input encountered while extracting", curfile.name);
486 }
487 curfile.name = "<name unknown>";
488 curfile.action = UNKNOWN;
489 curfile.dip = NULL;
490 curfile.ino = maxino;
491 if (gettingfile) {
492 gettingfile = 0;
ddd2ef55 493 siglongjmp(restart, 1);
1227625a
SP
494 }
495}
496
497/*
498 * handle multiple dumps per tape by skipping forward to the
499 * appropriate one.
500 */
501static void
ddd2ef55 502setdumpnum(void)
1227625a
SP
503{
504 struct mtop tcom;
505
506 if (dumpnum == 1 || volno != 1)
507 return;
ddd2ef55
SP
508 if (pipein)
509 errx(1, "Cannot have multiple dumps on pipe input");
1227625a
SP
510 tcom.mt_op = MTFSF;
511 tcom.mt_count = dumpnum - 1;
512#ifdef RRESTORE
513 if (host)
514 rmtioctl(MTFSF, dumpnum - 1);
b45f51d6 515 else
1227625a
SP
516#endif
517 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
ddd2ef55 518 warn("ioctl MTFSF");
1227625a
SP
519}
520
521void
ddd2ef55 522printdumpinfo(void)
1227625a
SP
523{
524#ifdef __linux__
525 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
526 fprintf(stdout, "Dumped from: %s",
527 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
528#else
529 fprintf(stdout, "Dump date: %s", ctime(&spcl.c_date));
530 fprintf(stdout, "Dumped from: %s",
531 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
532#endif
533 if (spcl.c_host[0] == '\0')
534 return;
535 fprintf(stderr, "Level %d dump of %s on %s:%s\n",
536 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
537 fprintf(stderr, "Label: %s\n", spcl.c_label);
538}
539
540int
ddd2ef55 541extractfile(char *name)
1227625a 542{
ddd2ef55 543 unsigned int flags;
1227625a
SP
544 mode_t mode;
545 struct timeval timep[2];
546 struct entry *ep;
1227625a
SP
547
548 curfile.name = name;
549 curfile.action = USING;
550#ifdef __linux__
551 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
552 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
553 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
554 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
555#else /* __linux__ */
556 timep[0].tv_sec = curfile.dip->di_atime;
557 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
558 timep[1].tv_sec = curfile.dip->di_mtime;
559 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
560#endif /* __linux__ */
561 mode = curfile.dip->di_mode;
562 flags = curfile.dip->di_flags;
563 switch (mode & IFMT) {
564
565 default:
566 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
567 skipfile();
568 return (FAIL);
569
570 case IFSOCK:
ddd2ef55 571 Vprintf(stdout, "skipped socket %s\n", name);
1227625a
SP
572 skipfile();
573 return (GOOD);
574
575 case IFDIR:
576 if (mflag) {
577 ep = lookupname(name);
578 if (ep == NULL || ep->e_flags & EXTRACT)
579 panic("unextracted directory %s\n", name);
580 skipfile();
581 return (GOOD);
582 }
ddd2ef55 583 Vprintf(stdout, "extract file %s\n", name);
1227625a
SP
584 return (genliteraldir(name, curfile.ino));
585
586 case IFLNK:
a95c4c36
SP
587 {
588#ifdef HAVE_LCHOWN
589 uid_t luid = curfile.dip->di_uid;
ddd2ef55 590 gid_t lgid = curfile.dip->di_gid;
a95c4c36 591#endif
1227625a
SP
592 lnkbuf[0] = '\0';
593 pathlen = 0;
1227625a
SP
594 getfile(xtrlnkfile, xtrlnkskip);
595 if (pathlen == 0) {
ddd2ef55 596 Vprintf(stdout,
1227625a
SP
597 "%s: zero length symbolic link (ignored)\n", name);
598 return (GOOD);
599 }
ddd2ef55
SP
600 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
601 return (FAIL);
08ebf8d7
SP
602#ifdef HAVE_LCHOWN
603 (void) lchown(name, luid, lgid);
604#endif
ddd2ef55
SP
605 return (GOOD);
606 }
1227625a 607
b45f51d6 608 case IFIFO:
ddd2ef55 609 Vprintf(stdout, "extract fifo %s\n", name);
1227625a
SP
610 if (Nflag) {
611 skipfile();
612 return (GOOD);
613 }
b45f51d6
SP
614 if (uflag && !Nflag)
615 (void)unlink(name);
616 if (mkfifo(name, mode) < 0) {
ddd2ef55 617 warn("%s: cannot create fifo", name);
1227625a
SP
618 skipfile();
619 return (FAIL);
620 }
621 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
622 (void) chmod(name, mode);
8d4197bb 623 if (flags)
b45f51d6 624#ifdef __linux__
8d4197bb 625 (void) fsetflags(name, flags);
1227625a 626#else
8d4197bb 627 (void) chflags(name, flags);
1227625a
SP
628#endif
629 skipfile();
630 utimes(name, timep);
631 return (GOOD);
632
b45f51d6
SP
633 case IFCHR:
634 case IFBLK:
ddd2ef55 635 Vprintf(stdout, "extract special file %s\n", name);
1227625a
SP
636 if (Nflag) {
637 skipfile();
638 return (GOOD);
639 }
b45f51d6
SP
640 if (uflag)
641 (void)unlink(name);
642 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
ddd2ef55 643 warn("%s: cannot create special file", name);
1227625a
SP
644 skipfile();
645 return (FAIL);
646 }
647 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
648 (void) chmod(name, mode);
8d4197bb 649 if (flags)
1227625a 650#ifdef __linux__
8d4197bb
SP
651 {
652 warn("%s: fsetflags called on a special file", name);
653 (void) fsetflags(name, flags);
654 }
1227625a 655#else
8d4197bb 656 (void) chflags(name, flags);
1227625a
SP
657#endif
658 skipfile();
659 utimes(name, timep);
660 return (GOOD);
661
662 case IFREG:
ddd2ef55 663 Vprintf(stdout, "extract file %s\n", name);
1227625a
SP
664 if (Nflag) {
665 skipfile();
666 return (GOOD);
667 }
b45f51d6
SP
668 if (uflag)
669 (void)unlink(name);
1227625a
SP
670 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
671 0666)) < 0) {
ddd2ef55 672 warn("%s: cannot create file", name);
1227625a
SP
673 skipfile();
674 return (FAIL);
675 }
676 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
677 (void) fchmod(ofile, mode);
8d4197bb 678 if (flags)
1227625a 679#ifdef __linux__
8d4197bb 680 (void) setflags(ofile, flags);
1227625a 681#else
8d4197bb 682 (void) fchflags(ofile, flags);
1227625a
SP
683#endif
684 getfile(xtrfile, xtrskip);
685 (void) close(ofile);
686 utimes(name, timep);
687 return (GOOD);
688 }
689 /* NOTREACHED */
690}
691
692/*
693 * skip over bit maps on the tape
694 */
695void
ddd2ef55 696skipmaps(void)
1227625a
SP
697{
698
699 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
700 skipfile();
701}
702
703/*
704 * skip over a file on the tape
705 */
706void
ddd2ef55 707skipfile(void)
1227625a
SP
708{
709
710 curfile.action = SKIP;
711 getfile(xtrnull, xtrnull);
712}
713
714/*
715 * Extract a file from the tape.
716 * When an allocated block is found it is passed to the fill function;
717 * when an unallocated block (hole) is found, a zeroed buffer is passed
718 * to the skip function.
719 */
720void
ddd2ef55 721getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
1227625a
SP
722{
723 register int i;
ddd2ef55
SP
724 volatile int curblk = 0;
725 volatile quad_t size = spcl.c_dinode.di_size;
726 volatile int last_write_was_hole = 0;
727 quad_t origsize = size;
1227625a
SP
728 static char clearedbuf[MAXBSIZE];
729 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
730 char junk[TP_BSIZE];
731
732 if (spcl.c_type == TS_END)
733 panic("ran off end of tape\n");
734 if (spcl.c_magic != NFS_MAGIC)
735 panic("not at beginning of a file\n");
736 if (!gettingfile && setjmp(restart) != 0)
737 return;
738 gettingfile++;
739loop:
740 for (i = 0; i < spcl.c_count; i++) {
b45f51d6 741 if (readmapflag || spcl.c_addr[i]) {
1227625a
SP
742 readtape(&buf[curblk++][0]);
743 if (curblk == fssize / TP_BSIZE) {
ddd2ef55 744 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1227625a
SP
745 fssize : (curblk - 1) * TP_BSIZE + size));
746 curblk = 0;
747 last_write_was_hole = 0;
748 }
749 } else {
750 if (curblk > 0) {
ddd2ef55 751 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1227625a
SP
752 curblk * TP_BSIZE :
753 (curblk - 1) * TP_BSIZE + size));
754 curblk = 0;
755 }
756 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
757 TP_BSIZE : size));
758 last_write_was_hole = 1;
759 }
760 if ((size -= TP_BSIZE) <= 0) {
761 for (i++; i < spcl.c_count; i++)
b45f51d6 762 if (readmapflag || spcl.c_addr[i])
1227625a
SP
763 readtape(junk);
764 break;
765 }
766 }
767 if (gethead(&spcl) == GOOD && size > 0) {
768 if (spcl.c_type == TS_ADDR)
769 goto loop;
ddd2ef55 770 Dprintf(stdout,
b45f51d6 771 "Missing address (header) block for %s at %ld blocks\n",
ddd2ef55 772 curfile.name, (long)blksread);
1227625a
SP
773 }
774 if (curblk > 0) {
ddd2ef55 775 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
1227625a
SP
776 last_write_was_hole = 0;
777 }
778 if (last_write_was_hole) {
779 ftruncate(ofile, origsize);
780 }
781 findinode(&spcl);
782 gettingfile = 0;
783}
784
785/*
786 * Write out the next block of a file.
787 */
788static void
ddd2ef55 789xtrfile(char *buf, size_t size)
1227625a
SP
790{
791
792 if (Nflag)
793 return;
ddd2ef55
SP
794 if (write(ofile, buf, (int) size) == -1)
795 err(1, "write error extracting inode %lu, name %s\nwrite",
796 (unsigned long)curfile.ino, curfile.name);
1227625a
SP
797}
798
799/*
800 * Skip over a hole in a file.
801 */
802/* ARGSUSED */
803static void
ddd2ef55 804xtrskip(char *buf, size_t size)
1227625a
SP
805{
806
ddd2ef55
SP
807 if (lseek(ofile, (off_t)size, SEEK_CUR) == -1)
808 err(1, "seek error extracting inode %lu, name %s\nlseek",
809 (unsigned long)curfile.ino, curfile.name);
1227625a
SP
810}
811
812/*
813 * Collect the next block of a symbolic link.
814 */
815static void
ddd2ef55 816xtrlnkfile(char *buf, size_t size)
1227625a
SP
817{
818
819 pathlen += size;
ddd2ef55
SP
820 if (pathlen > MAXPATHLEN)
821 errx(1, "symbolic link name: %s->%s%s; too long %d",
1227625a 822 curfile.name, lnkbuf, buf, pathlen);
1227625a
SP
823 (void) strcat(lnkbuf, buf);
824}
825
826/*
827 * Skip over a hole in a symbolic link (should never happen).
828 */
829/* ARGSUSED */
830static void
ddd2ef55 831xtrlnkskip(char *buf, size_t size)
1227625a
SP
832{
833
ddd2ef55 834 errx(1, "unallocated block in symbolic link %s", curfile.name);
1227625a
SP
835}
836
837/*
838 * Collect the next block of a bit map.
839 */
840static void
ddd2ef55 841xtrmap(char *buf, size_t size)
1227625a
SP
842{
843
844 memmove(map, buf, size);
845 map += size;
846}
847
848/*
849 * Skip over a hole in a bit map (should never happen).
850 */
851/* ARGSUSED */
852static void
ddd2ef55 853xtrmapskip(char *buf, size_t size)
1227625a
SP
854{
855
856 panic("hole in map\n");
857 map += size;
858}
859
860/*
861 * Noop, when an extraction function is not needed.
862 */
863/* ARGSUSED */
864void
ddd2ef55 865xtrnull(char *buf, size_t size)
1227625a
SP
866{
867
868 return;
869}
870
ddd2ef55 871static int
1227625a
SP
872do_cmpfiles(int fd_tape, int fd_disk, long size)
873{
1227625a
SP
874 static char buf_tape[BUFSIZ];
875 static char buf_disk[BUFSIZ];
ddd2ef55
SP
876 ssize_t n_tape;
877 ssize_t n_disk;
1227625a
SP
878
879 while (size > 0) {
ddd2ef55 880 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1227625a
SP
881 close(fd_tape), close(fd_disk);
882 panic("do_cmpfiles: unexpected EOF[1]");
883 }
ddd2ef55 884 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1227625a
SP
885 close(fd_tape), close(fd_disk);
886 panic("do_cmpfiles: unexpected EOF[2]");
887 }
888 if (n_tape != n_disk) {
889 close(fd_tape), close(fd_disk);
890 panic("do_cmpfiles: sizes different!");
891 }
ddd2ef55 892 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1227625a
SP
893 size -= n_tape;
894 }
895 return (0);
896}
897
898/* for debugging compare problems */
899#undef COMPARE_FAIL_KEEP_FILE
900
ddd2ef55 901static
1227625a
SP
902#ifdef COMPARE_FAIL_KEEP_FILE
903/* return true if tapefile should be unlinked after compare */
904int
905#else
906void
907#endif
908cmpfiles(char *tapefile, char *diskfile, struct stat *sbuf_disk)
909{
910 struct stat sbuf_tape;
911 int fd_tape, fd_disk;
912
913 if (stat(tapefile, &sbuf_tape) != 0) {
914 panic("Can't lstat tmp file %s: %s\n", tapefile,
915 strerror(errno));
916 }
917
918 if (sbuf_disk->st_size != sbuf_tape.st_size) {
919 fprintf(stderr,
b45f51d6 920 "%s: size changed from %ld to %ld.\n",
ddd2ef55 921 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1227625a
SP
922#ifdef COMPARE_FAIL_KEEP_FILE
923 return (0);
924#else
925 return;
926#endif
927 }
928
929 if ((fd_tape = open(tapefile, O_RDONLY)) < 0) {
930 panic("Can't open %s: %s\n", tapefile, strerror(errno));
931 }
932 if ((fd_disk = open(diskfile, O_RDONLY)) < 0) {
933 close(fd_tape);
934 panic("Can't open %s: %s\n", diskfile, strerror(errno));
935 }
936
937 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
938 fprintf(stderr, "%s: tape and disk copies are different\n",
939 diskfile);
940 close(fd_tape);
941 close(fd_disk);
942#ifdef COMPARE_FAIL_KEEP_FILE
943 /* rename the file to live in /tmp */
944 /* rename `tapefile' to /tmp/<basename of diskfile> */
945 {
946 char *p = strrchr(diskfile, '/');
947 char newname[MAXPATHLEN];
948 if (!p) {
949 panic("can't find / in %s\n", diskfile);
950 }
ddd2ef55 951 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1227625a
SP
952 if (rename(tapefile, newname)) {
953 panic("rename from %s to %s failed: %s\n",
954 tapefile, newname,
955 strerror(errno));
956 } else {
957 fprintf(stderr, "*** %s saved to %s\n",
958 tapefile, newname);
959 }
960 }
961
962 /* don't unlink the file (it's not there anymore */
963 /* anyway) */
964 return (0);
965#else
966 return;
967#endif
968 }
969 close(fd_tape);
970 close(fd_disk);
971#ifdef COMPARE_FAIL_KEEP_FILE
972 return (1);
973#endif
974}
975
ddd2ef55 976static char tmpfilename[MAXPATHLEN];
1227625a
SP
977
978void
ddd2ef55 979comparefile(char *name)
1227625a
SP
980{
981 static char *tmpfile = NULL;
982 int mode;
983 struct stat sb, stemp;
984 int r;
985
986 if ((r = lstat(name, &sb)) != 0) {
ddd2ef55 987 warn("%s: does not exist (%d)", name, r);
1227625a
SP
988 skipfile();
989 return;
990 }
991
992 curfile.name = name;
b45f51d6 993 curfile.action = USING;
1227625a
SP
994 mode = curfile.dip->di_mode;
995
ddd2ef55
SP
996 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
997 (long)sb.st_size, mode);
1227625a
SP
998
999 if (sb.st_mode != mode) {
1000 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1001 name, mode & 07777, sb.st_mode & 07777);
1002 }
1003 switch (mode & IFMT) {
1004 default:
1005 skipfile();
1006 return;
1007
1008 case IFSOCK:
1009 skipfile();
1010 return;
1011
1012 case IFDIR:
1013 skipfile();
1014 return;
1015
1016 case IFLNK: {
1017 char lbuf[MAXPATHLEN + 1];
1018 int lsize;
1019
1020 if (!(sb.st_mode & S_IFLNK)) {
1021 fprintf(stderr, "%s: is no longer a symbolic link\n",
1022 name);
1023 return;
1024 }
1025 lnkbuf[0] = '\0';
1026 pathlen = 0;
1027 getfile(xtrlnkfile, xtrlnkskip);
1028 if (pathlen == 0) {
1029 fprintf(stderr,
1030 "%s: zero length symbolic link (ignored)\n",
1031 name);
1032 return;
1033 }
1034 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1035 panic("readlink of %s failed: %s", name,
1036 strerror(errno));
1037 }
1038 lbuf[lsize] = 0;
1039 if (strcmp(lbuf, lnkbuf) != 0) {
1040 fprintf(stderr,
1041 "%s: symbolic link changed from %s to %s.\n",
1042 name, lnkbuf, lbuf);
1043 return;
1044 }
1045 return;
1046 }
1047
1048 case IFCHR:
1049 case IFBLK:
1050 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1051 fprintf(stderr, "%s: no longer a special file\n",
1052 name);
1053 skipfile();
1054 return;
1055 }
1056
1057 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1058 fprintf(stderr,
1059 "%s: device changed from %d,%d to %d,%d.\n",
1060 name,
ddd2ef55
SP
1061 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1062 (int)curfile.dip->di_rdev & 0xff,
1063 ((int)sb.st_rdev >> 8) & 0xff,
1064 (int)sb.st_rdev & 0xff);
1227625a
SP
1065 }
1066 skipfile();
1067 return;
1068
1069 case IFREG:
1070 if (tmpfile == NULL) {
1071 /* argument to mktemp() must not be in RO space: */
ddd2ef55 1072 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1227625a
SP
1073 tmpfile = mktemp(&tmpfilename[0]);
1074 }
1075 if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1076 panic("cannot delete tmp file %s: %s\n",
1077 tmpfile, strerror(errno));
1078 }
1079 if ((ofile = creat(tmpfile, 0600)) < 0) {
1080 panic("cannot create file temp file %s: %s\n",
1081 name, strerror(errno));
1082 }
1083 getfile(xtrfile, xtrskip);
1084 (void) close(ofile);
1085#ifdef COMPARE_FAIL_KEEP_FILE
1086 if (cmpfiles(tmpfile, name, &sb))
1087 unlink(tmpfile);
1088#else
1089 cmpfiles(tmpfile, name, &sb);
1090 unlink(tmpfile);
1091#endif
1092 return;
1093 }
1094 /* NOTREACHED */
1095}
1096
1097/*
1098 * Read TP_BSIZE blocks from the input.
1099 * Handle read errors, and end of media.
1100 */
1101static void
ddd2ef55 1102readtape(char *buf)
1227625a 1103{
ddd2ef55 1104 ssize_t rd, newvol, i;
1227625a
SP
1105 int cnt, seek_failed;
1106
1107 if (blkcnt < numtrec) {
ddd2ef55 1108 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1227625a
SP
1109 blksread++;
1110 tpblksread++;
1111 return;
1112 }
1113 for (i = 0; i < ntrec; i++)
1114 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1115 if (numtrec == 0)
1116 numtrec = ntrec;
1117 cnt = ntrec * TP_BSIZE;
1118 rd = 0;
1119getmore:
1120#ifdef RRESTORE
1121 if (host)
1122 i = rmtread(&tapebuf[rd], cnt);
1123 else
1124#endif
1125 i = read(mt, &tapebuf[rd], cnt);
1126 /*
1127 * Check for mid-tape short read error.
1128 * If found, skip rest of buffer and start with the next.
1129 */
1130 if (!pipein && numtrec < ntrec && i > 0) {
ddd2ef55 1131 Dprintf(stdout, "mid-media short read error.\n");
1227625a
SP
1132 numtrec = ntrec;
1133 }
1134 /*
1135 * Handle partial block read.
1136 */
1137 if (pipein && i == 0 && rd > 0)
1138 i = rd;
1139 else if (i > 0 && i != ntrec * TP_BSIZE) {
1140 if (pipein) {
1141 rd += i;
1142 cnt -= i;
1143 if (cnt > 0)
1144 goto getmore;
1145 i = rd;
1146 } else {
1147 /*
1148 * Short read. Process the blocks read.
1149 */
1150 if (i % TP_BSIZE != 0)
ddd2ef55 1151 Vprintf(stdout,
b45f51d6 1152 "partial block read: %ld should be %ld\n",
ddd2ef55 1153 (long)i, ntrec * TP_BSIZE);
1227625a
SP
1154 numtrec = i / TP_BSIZE;
1155 }
1156 }
1157 /*
1158 * Handle read error.
1159 */
1160 if (i < 0) {
1161 fprintf(stderr, "Tape read error while ");
1162 switch (curfile.action) {
1163 default:
1164 fprintf(stderr, "trying to set up tape\n");
1165 break;
1166 case UNKNOWN:
1167 fprintf(stderr, "trying to resynchronize\n");
1168 break;
1169 case USING:
1170 fprintf(stderr, "restoring %s\n", curfile.name);
1171 break;
1172 case SKIP:
ddd2ef55
SP
1173 fprintf(stderr, "skipping over inode %lu\n",
1174 (unsigned long)curfile.ino);
1227625a
SP
1175 break;
1176 }
1177 if (!yflag && !reply("continue"))
ddd2ef55 1178 exit(1);
1227625a 1179 i = ntrec * TP_BSIZE;
ddd2ef55 1180 memset(tapebuf, 0, (size_t)i);
1227625a
SP
1181#ifdef RRESTORE
1182 if (host)
1183 seek_failed = (rmtseek(i, 1) < 0);
1184 else
1185#endif
1186 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1187
ddd2ef55
SP
1188 if (seek_failed)
1189 err(1, "continuation failed");
1227625a
SP
1190 }
1191 /*
1192 * Handle end of tape.
1193 */
1194 if (i == 0) {
ddd2ef55 1195 Vprintf(stdout, "End-of-tape encountered\n");
1227625a
SP
1196 if (!pipein) {
1197 newvol = volno + 1;
1198 volno = 0;
1199 numtrec = 0;
1200 getvol(newvol);
1201 readtape(buf);
1202 return;
1203 }
1204 if (rd % TP_BSIZE != 0)
1205 panic("partial block read: %d should be %d\n",
1206 rd, ntrec * TP_BSIZE);
1207 terminateinput();
ddd2ef55 1208 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1227625a
SP
1209 }
1210 blkcnt = 0;
ddd2ef55 1211 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1227625a
SP
1212 blksread++;
1213 tpblksread++;
1214}
1215
1216static void
ddd2ef55 1217findtapeblksize(void)
1227625a
SP
1218{
1219 register long i;
1220
1221 for (i = 0; i < ntrec; i++)
1222 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1223 blkcnt = 0;
1224#ifdef RRESTORE
1225 if (host)
ddd2ef55 1226 i = rmtread(tapebuf, (size_t)(ntrec * TP_BSIZE));
1227625a
SP
1227 else
1228#endif
ddd2ef55 1229 i = read(mt, tapebuf, (size_t)(ntrec * TP_BSIZE));
1227625a 1230
ddd2ef55
SP
1231 if (i <= 0)
1232 err(1, "tape read error");
1233 if (i % TP_BSIZE != 0)
1234 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1235 (long)i, TP_BSIZE);
1227625a
SP
1236 ntrec = i / TP_BSIZE;
1237 numtrec = ntrec;
ddd2ef55 1238 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1227625a
SP
1239}
1240
1241void
ddd2ef55 1242closemt(void)
1227625a
SP
1243{
1244
1245 if (mt < 0)
1246 return;
1247#ifdef RRESTORE
1248 if (host)
1249 rmtclose();
1250 else
1251#endif
1252 (void) close(mt);
1253}
1254
1255/*
1256 * Read the next block from the tape.
1257 * Check to see if it is one of several vintage headers.
1258 * If it is an old style header, convert it to a new style header.
1259 * If it is not any valid header, return an error.
1260 */
1261static int
ddd2ef55 1262gethead(struct s_spcl *buf)
1227625a 1263{
ddd2ef55 1264 int32_t i;
1227625a
SP
1265 union {
1266 quad_t qval;
b45f51d6 1267 int32_t val[2];
1227625a
SP
1268 } qcvt;
1269 union u_ospcl {
1270 char dummy[TP_BSIZE];
1271 struct s_ospcl {
b45f51d6
SP
1272 int32_t c_type;
1273 int32_t c_date;
1274 int32_t c_ddate;
1275 int32_t c_volume;
1276 int32_t c_tapea;
ddd2ef55 1277 u_int16_t c_inumber;
b45f51d6
SP
1278 int32_t c_magic;
1279 int32_t c_checksum;
1227625a 1280 struct odinode {
ddd2ef55
SP
1281 u_int16_t odi_mode;
1282 u_int16_t odi_nlink;
1283 u_int16_t odi_uid;
1284 u_int16_t odi_gid;
b45f51d6
SP
1285 int32_t odi_size;
1286 int32_t odi_rdev;
1227625a 1287 char odi_addr[36];
b45f51d6
SP
1288 int32_t odi_atime;
1289 int32_t odi_mtime;
1290 int32_t odi_ctime;
1227625a 1291 } c_dinode;
b45f51d6 1292 int32_t c_count;
1227625a
SP
1293 char c_addr[256];
1294 } s_ospcl;
1295 } u_ospcl;
1296
1297 if (!cvtflag) {
1298 readtape((char *)buf);
1299 if (buf->c_magic != NFS_MAGIC) {
ddd2ef55 1300 if (swabi(buf->c_magic) != NFS_MAGIC)
1227625a
SP
1301 return (FAIL);
1302 if (!Bcvt) {
ddd2ef55 1303 Vprintf(stdout, "Note: Doing Byte swapping\n");
1227625a
SP
1304 Bcvt = 1;
1305 }
1306 }
1307 if (checksum((int *)buf) == FAIL)
1308 return (FAIL);
ddd2ef55
SP
1309 if (Bcvt)
1310 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1227625a
SP
1311 goto good;
1312 }
1313 readtape((char *)(&u_ospcl.s_ospcl));
ddd2ef55 1314 memset((char *)buf, 0, (long)TP_BSIZE);
1227625a
SP
1315 buf->c_type = u_ospcl.s_ospcl.c_type;
1316 buf->c_date = u_ospcl.s_ospcl.c_date;
1317 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1318 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1319 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1320 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1321 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1322 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1323 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1324 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1325 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1326 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1327 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1328 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1329#ifdef __linux__
1330 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1331 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1332 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1333#else /* __linux__ */
1334 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1335 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1336 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1337#endif /* __linux__ */
1338 buf->c_count = u_ospcl.s_ospcl.c_count;
1339 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1340 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1341 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1342 return(FAIL);
1343 buf->c_magic = NFS_MAGIC;
1344
1345good:
1346 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1347 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1348 qcvt.qval = buf->c_dinode.di_size;
1349 if (qcvt.val[0] || qcvt.val[1]) {
1350 printf("Note: Doing Quad swapping\n");
1351 Qcvt = 1;
1352 }
1353 }
1354 if (Qcvt) {
1355 qcvt.qval = buf->c_dinode.di_size;
1356 i = qcvt.val[1];
1357 qcvt.val[1] = qcvt.val[0];
1358 qcvt.val[0] = i;
1359 buf->c_dinode.di_size = qcvt.qval;
1360 }
b45f51d6 1361 readmapflag = 0;
1227625a
SP
1362
1363 switch (buf->c_type) {
1364
1365 case TS_CLRI:
1366 case TS_BITS:
1367 /*
1368 * Have to patch up missing information in bit map headers
1369 */
1370 buf->c_inumber = 0;
1371 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
b45f51d6
SP
1372 if (buf->c_count > TP_NINDIR)
1373 readmapflag = 1;
1374 else
1375 for (i = 0; i < buf->c_count; i++)
1376 buf->c_addr[i]++;
1227625a
SP
1377 break;
1378
1379 case TS_TAPE:
1380 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1381 oldinofmt = 1;
1382 /* fall through */
1383 case TS_END:
1384 buf->c_inumber = 0;
1385 break;
1386
1387 case TS_INODE:
1388 case TS_ADDR:
1389 break;
1390
1391 default:
1392 panic("gethead: unknown inode type %d\n", buf->c_type);
1393 break;
1394 }
1395 /*
b45f51d6 1396 * If we are restoring a filesystem with old format inodes,
1227625a
SP
1397 * copy the uid/gid to the new location.
1398 */
1399 if (oldinofmt) {
1400 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1401 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1402 }
1403 if (dflag)
1404 accthdr(buf);
1405 return(GOOD);
1406}
1407
1408/*
1409 * Check that a header is where it belongs and predict the next header
1410 */
1411static void
ddd2ef55 1412accthdr(struct s_spcl *header)
1227625a
SP
1413{
1414 static ino_t previno = 0x7fffffff;
1415 static int prevtype;
1416 static long predict;
1417 long blks, i;
1418
1419 if (header->c_type == TS_TAPE) {
1420 fprintf(stderr, "Volume header (%s inode format) ",
1421 oldinofmt ? "old" : "new");
1422 if (header->c_firstrec)
1423 fprintf(stderr, "begins with record %d",
1424 header->c_firstrec);
1425 fprintf(stderr, "\n");
1426 previno = 0x7fffffff;
1427 return;
1428 }
1429 if (previno == 0x7fffffff)
1430 goto newcalc;
1431 switch (prevtype) {
1432 case TS_BITS:
1433 fprintf(stderr, "Dumped inodes map header");
1434 break;
1435 case TS_CLRI:
1436 fprintf(stderr, "Used inodes map header");
1437 break;
1438 case TS_INODE:
ddd2ef55 1439 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
1227625a
SP
1440 break;
1441 case TS_ADDR:
b9c89cf4 1442 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
1227625a
SP
1443 break;
1444 case TS_END:
1445 fprintf(stderr, "End of tape header");
1446 break;
1447 }
1448 if (predict != blksread - 1)
b45f51d6 1449 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1227625a
SP
1450 predict, blksread - 1);
1451 fprintf(stderr, "\n");
1452newcalc:
1453 blks = 0;
1454 if (header->c_type != TS_END)
1455 for (i = 0; i < header->c_count; i++)
b45f51d6 1456 if (readmapflag || header->c_addr[i] != 0)
1227625a
SP
1457 blks++;
1458 predict = blks;
1459 blksread = 0;
1460 prevtype = header->c_type;
1461 previno = header->c_inumber;
1462}
1463
1464/*
1465 * Find an inode header.
1466 * Complain if had to skip, and complain is set.
1467 */
1468static void
ddd2ef55 1469findinode(struct s_spcl *header)
1227625a
SP
1470{
1471 static long skipcnt = 0;
1472 long i;
1473 char buf[TP_BSIZE];
1474
1475 curfile.name = "<name unknown>";
1476 curfile.action = UNKNOWN;
1477 curfile.dip = NULL;
1478 curfile.ino = 0;
1479 do {
1480 if (header->c_magic != NFS_MAGIC) {
1481 skipcnt++;
1482 while (gethead(header) == FAIL ||
1483 header->c_date != dumpdate)
1484 skipcnt++;
1485 }
1486 switch (header->c_type) {
1487
1488 case TS_ADDR:
1489 /*
1490 * Skip up to the beginning of the next record
1491 */
1492 for (i = 0; i < header->c_count; i++)
1493 if (header->c_addr[i])
1494 readtape(buf);
1495 while (gethead(header) == FAIL ||
1496 header->c_date != dumpdate)
1497 skipcnt++;
1498 break;
1499
1500 case TS_INODE:
1501 curfile.dip = &header->c_dinode;
1502 curfile.ino = header->c_inumber;
1503 break;
1504
1505 case TS_END:
1506 curfile.ino = maxino;
1507 break;
1508
1509 case TS_CLRI:
1510 curfile.name = "<file removal list>";
1511 break;
1512
1513 case TS_BITS:
1514 curfile.name = "<file dump list>";
1515 break;
1516
1517 case TS_TAPE:
1518 panic("unexpected tape header\n");
1519 /* NOTREACHED */
1520
1521 default:
1522 panic("unknown tape header type %d\n", spcl.c_type);
1523 /* NOTREACHED */
1524
1525 }
1526 } while (header->c_type == TS_ADDR);
1527 if (skipcnt > 0)
b45f51d6
SP
1528 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1529 skipcnt);
1227625a
SP
1530 skipcnt = 0;
1531}
1532
1533static int
ddd2ef55 1534checksum(int *buf)
1227625a
SP
1535{
1536 register int i, j;
1537
1538 j = sizeof(union u_spcl) / sizeof(int);
1539 i = 0;
1540 if(!Bcvt) {
1541 do
1542 i += *buf++;
1543 while (--j);
1544 } else {
1545 /* What happens if we want to read restore tapes
1546 for a 16bit int machine??? */
b45f51d6 1547 do
ddd2ef55 1548 i += swabi(*buf++);
1227625a
SP
1549 while (--j);
1550 }
b45f51d6 1551
1227625a 1552 if (i != CHECKSUM) {
ddd2ef55
SP
1553 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
1554 (unsigned long)curfile.ino, curfile.name);
1227625a
SP
1555 return(FAIL);
1556 }
1557 return(GOOD);
1558}
1559
1560#ifdef RRESTORE
ddd2ef55 1561#ifdef __STDC__
1227625a
SP
1562#include <stdarg.h>
1563#else
1564#include <varargs.h>
1565#endif
1566
1567void
ddd2ef55 1568#ifdef __STDC__
1227625a
SP
1569msg(const char *fmt, ...)
1570#else
1571msg(fmt, va_alist)
1572 char *fmt;
1573 va_dcl
1574#endif
1575{
1576 va_list ap;
ddd2ef55 1577#ifdef __STDC__
1227625a
SP
1578 va_start(ap, fmt);
1579#else
1580 va_start(ap);
1581#endif
1582 (void)vfprintf(stderr, fmt, ap);
1583 va_end(ap);
1584}
1585#endif /* RRESTORE */
1586
1587static u_char *
ddd2ef55 1588swab16(u_char *sp, int n)
1227625a
SP
1589{
1590 char c;
1591
1592 while (--n >= 0) {
1593 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1594 sp += 2;
1595 }
1596 return (sp);
1597}
1598
1599static u_char *
ddd2ef55 1600swab32(u_char *sp, int n)
1227625a
SP
1601{
1602 char c;
1603
1604 while (--n >= 0) {
1605 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
ddd2ef55 1606 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
1227625a
SP
1607 sp += 4;
1608 }
1609 return (sp);
1610}
1611
ddd2ef55
SP
1612static u_char *
1613swab64(u_char *sp, int n)
1614{
1615 char c;
1616
1617 while (--n >= 0) {
1618 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1619 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1620 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1621 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1622 sp += 8;
1623 }
1624 return (sp);
1625}
1626
1227625a 1627void
ddd2ef55 1628swabst(u_char *cp, u_char *sp)
1227625a
SP
1629{
1630 int n = 0;
1631
1632 while (*cp) {
1633 switch (*cp) {
1634 case '0': case '1': case '2': case '3': case '4':
1635 case '5': case '6': case '7': case '8': case '9':
1636 n = (n * 10) + (*cp++ - '0');
1637 continue;
b45f51d6 1638
1227625a
SP
1639 case 's': case 'w': case 'h':
1640 if (n == 0)
1641 n = 1;
ddd2ef55
SP
1642 sp = swab16(sp, n);
1643 break;
1644
1645 case 'i':
1646 if (n == 0)
1647 n = 1;
1648 sp = swab32(sp, n);
1227625a
SP
1649 break;
1650
1651 case 'l':
1652 if (n == 0)
1653 n = 1;
ddd2ef55 1654 sp = swab64(sp, n);
1227625a
SP
1655 break;
1656
1657 default: /* Any other character, like 'b' counts as byte. */
1658 if (n == 0)
1659 n = 1;
1660 sp += n;
1661 break;
1662 }
1663 cp++;
1664 n = 0;
1665 }
1666}
1667
ddd2ef55
SP
1668static u_int
1669swabi(u_int x)
1670{
1671 swabst((u_char *)"i", (u_char *)&x);
1672 return (x);
1673}
1674
1227625a 1675static u_long
ddd2ef55 1676swabl(u_long x)
1227625a
SP
1677{
1678 swabst((u_char *)"l", (u_char *)&x);
1679 return (x);
1680}