]>
git.wh0rd.org - dump.git/blob - rmt/rmt.c
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - Alcôve <www.alcove.com>, 2000-2002
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
39 static const char rcsid
[] =
40 "$Id: rmt.c,v 1.27 2003/10/26 16:05:49 stelian Exp $";
41 #endif /* not linux */
47 #include <compatlfs.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
65 static int maxrecsize
= -1;
68 static char device
[SSIZE
];
69 static char count
[SSIZE
], filemode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
71 static char resp
[BUFSIZ
];
74 #define DEBUG(f) if (debug) fprintf(debug, f)
75 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
76 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
79 * Support for Sun's extended RMT protocol
80 * code originally written by Jörg Schilling <schilling@fokus.gmd.de>
81 * and relicensed by his permission from GPL to BSD for use in dump.
83 * rmt_version is 0 for regular clients (Linux included)
84 * rmt_version is 1 for extended clients (Sun especially). In this case
85 * we support some extended commands (see below) and we remap
86 * the ioctl commands to the UNIX "standard", as per:
87 * ftp://ftp.fokus.gmd.de/pub/unix/star/README.mtio
89 * In order to use rmt version 1, a client must send "I-1\n0\n"
90 * before issuing the other I commands.
92 static int rmt_version
= 0;
93 #define RMTI_VERSION -1
96 /* Extended 'i' commands */
98 #define RMTI_NOCACHE 1
104 /* Extended 's' comands */
106 #define MTS_DSREG 'D'
107 #define MTS_ERREG 'E'
108 #define MTS_RESID 'R'
109 #define MTS_FILENO 'F'
110 #define MTS_BLKNO 'B'
111 #define MTS_FLAGS 'f'
114 static char *checkbuf
__P((char *, int));
115 static void error
__P((int));
116 static void getstring
__P((char *));
117 static unsigned long swaplong
__P((unsigned long inv
));
119 char *cipher
__P((char *, int, int));
120 void decrypt
__P((void));
124 main(int argc
, char *argv
[])
128 int n
, i
, cc
, oflags
;
129 unsigned long block
= 0;
135 if (argc
> 1 && strcmp(argv
[1], "-d") == 0)
136 decrypt(); /* decrypt stdin to stdout, and exit() */
138 /* Skip "-c /etc/rmt", which appears when rmt is used as a shell */
139 if (argc
> 2 && strcmp(argv
[1], "-c") == 0)
140 argc
-= 2, argv
+= 2;
143 debug
= fopen(*argv
, "w");
146 (void)setbuf(debug
, (char *)0);
151 if (read(0, &c
, 1) != 1)
160 DEBUG2("rmtd: O %s %s\n", device
, filemode
);
162 * Translate extended GNU syntax into its numeric platform equivalent
164 oflags
= rmtflags_toint(filemode
);
167 * Default to O_BINARY the client may not know that we need it.
169 if ((oflags
& O_TEXT
) == 0)
172 DEBUG2("rmtd: O %s %d\n", device
, oflags
);
174 * XXX the rmt protocol does not provide a means to
175 * specify the permission bits; allow rw for everyone,
176 * as modified by the users umask
178 tape
= OPEN(device
, oflags
, 0666);
183 struct mtget mt_stat
;
184 magtape
= ioctl(tape
, MTIOCGET
, (char *)&mt_stat
) == 0;
189 DEBUG1("rmtd: C (%lu blocks)\n", block
);
190 getstring(device
); /* discard */
198 #define LSEEK_GET_TAPEPOS 10
199 #define LSEEK_GO2_TAPEPOS 11
205 DEBUG2("rmtd: L %s %s\n", count
, pos
);
206 if (!magtape
) { /* traditional */
207 rval
= LSEEK(tape
, (OFF_T
)atoll(count
), atoi(pos
));
214 rval
= LSEEK(tape
, (OFF_T
)atoll(count
), atoi(pos
));
217 case LSEEK_GET_TAPEPOS
: /* QFA */
218 case LSEEK_GO2_TAPEPOS
:
223 buf
.mt_op
= MTSETDRVBUFFER
;
224 buf
.mt_count
= MT_ST_BOOLEANS
| MT_ST_SCSI2LOGICAL
;
225 if (ioctl(tape
, MTIOCTOP
, &buf
) < 0) {
229 if (atoi(pos
) == LSEEK_GET_TAPEPOS
) { /* get tapepos */
230 if (ioctl(tape
, MTIOCPOS
, &mtpos
) < 0) {
236 buf
.mt_count
= atoi(count
);
237 if (ioctl(tape
, MTIOCTOP
, &buf
) < 0) {
240 rval
= (OFF_T
)buf
.mt_count
;
259 DEBUG2("rmtd: W %s (block = %lu)\n", count
, block
);
260 record
= checkbuf(record
, n
);
261 for (i
= 0; i
< n
; i
+= cc
) {
262 cc
= read(0, &record
[i
], n
- i
);
264 DEBUG("rmtd: premature eof\n");
269 if ((cp
= cipher(record
, n
, 1)) == NULL
)
274 rval
= write(tape
, cp
, n
);
282 DEBUG2("rmtd: R %s (block %lu)\n", count
, block
);
284 record
= checkbuf(record
, n
);
285 rval
= read(tape
, record
, n
);
289 if ((cp
= cipher(record
, rval
, 0)) == NULL
)
294 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
295 (void)write(1, resp
, strlen(resp
));
296 (void)write(1, cp
, rval
);
303 DEBUG2("rmtd: I %s %s\n", op
, count
);
304 if (atoi(op
) == RMTI_VERSION
) {
312 /* rmt version 1, assume UNIX/Solaris/Mac OS X client */
356 mtop
.mt_op
= MTRETEN
;
361 mtop
.mt_op
= MTERASE
;
370 if (mtop
.mt_op
== -1) {
376 /* rmt version 0, assume linux client */
377 mtop
.mt_op
= atoi(op
);
379 mtop
.mt_count
= atoi(count
);
380 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0) {
383 rval
= mtop
.mt_count
;
392 DEBUG2 ("rmtd: i %s %s\n", op
, count
);
396 mtop
.mt_op
= MTCACHE
;
401 mtop
.mt_op
= MTNOCACHE
;
406 mtop
.mt_op
= MTRETEN
;
411 mtop
.mt_op
= MTERASE
;
428 mtop
.mt_count
= atoi (count
);
429 if (ioctl (tape
, MTIOCTOP
, (char *) &mtop
) < 0) {
433 rval
= mtop
.mt_count
;
438 case 'S': /* status */
440 { struct mtget mtget
;
442 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0) {
447 rval
= sizeof(mtget
);
448 /* assume byte order:
449 Linux on Intel (little), Solaris on SPARC (big), Mac OS X on PPC (big)
450 thus need byte swapping from little to big
452 mtget
.mt_type
= swaplong(mtget
.mt_type
);
453 mtget
.mt_resid
= swaplong(mtget
.mt_resid
);
454 mtget
.mt_dsreg
= swaplong(mtget
.mt_dsreg
);
455 mtget
.mt_gstat
= swaplong(mtget
.mt_gstat
);
456 mtget
.mt_erreg
= swaplong(mtget
.mt_erreg
);
457 mtget
.mt_fileno
= swaplong(mtget
.mt_fileno
);
458 mtget
.mt_blkno
= swaplong(mtget
.mt_blkno
);
459 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
460 (void)write(1, resp
, strlen(resp
));
461 (void)write(1, (char *)&mtget
, sizeof (mtget
));
463 rval
= sizeof (mtget
);
464 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
465 (void)write(1, resp
, strlen(resp
));
466 (void)write(1, (char *)&mtget
, sizeof (mtget
));
477 if (read (0, &s
, 1) != 1)
479 DEBUG1 ("rmtd: s %d\n", s
);
481 if (ioctl (tape
, MTIOCGET
, (char *) &mtget
) < 0) {
487 rval
= mtget
.mt_type
;
490 rval
= mtget
.mt_dsreg
;
493 rval
= mtget
.mt_erreg
;
496 rval
= mtget
.mt_resid
;
499 rval
= mtget
.mt_fileno
;
502 rval
= mtget
.mt_blkno
;
505 rval
= mtget
.mt_gstat
;
518 case 'V': /* version */
520 DEBUG1("rmtd: V %s\n", op
);
525 DEBUG1("rmtd: garbage command %c\n", c
);
529 DEBUG1("rmtd: A %lld\n", (long long)rval
);
530 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
531 (void)write(1, resp
, strlen(resp
));
538 static void getstring(char *bp
)
543 for (i
= 0; i
< SSIZE
- 1; i
++) {
544 if (read(0, cp
+i
, 1) != 1)
553 checkbuf(char *record
, int size
)
556 if (size
<= maxrecsize
)
560 record
= malloc(size
);
562 DEBUG("rmtd: cannot allocate buffer space\n");
566 while (size
> 1024 &&
567 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
576 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
577 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
578 (void)write(1, resp
, strlen(resp
));
582 swaplong(unsigned long inv
)
589 inp
= (union lconv
*)&inv
;
591 outv
.uc
[0] = inp
->uc
[3];
592 outv
.uc
[1] = inp
->uc
[2];
593 outv
.uc
[2] = inp
->uc
[1];
594 outv
.uc
[3] = inp
->uc
[0];