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.29 2010/06/11 11:19:18 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 */
211 rval = LSEEK(tape, (OFF_T)atoll(count), atoi(pos));
214 case LSEEK_GET_TAPEPOS:
215 rval = LSEEK(tape, (OFF_T)0, SEEK_CUR);
217 case LSEEK_GO2_TAPEPOS:
218 rval = LSEEK(tape, (OFF_T)atoll(count), SEEK_SET);
232 rval = LSEEK(tape, (OFF_T)atoll(count), atoi(pos));
235 case LSEEK_GET_TAPEPOS: /* QFA */
236 case LSEEK_GO2_TAPEPOS:
241 buf.mt_op = MTSETDRVBUFFER;
242 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
243 if (ioctl(tape, MTIOCTOP, &buf) < 0) {
247 if (atoi(pos) == LSEEK_GET_TAPEPOS) { /* get tapepos */
248 if (ioctl(tape, MTIOCPOS, &mtpos) < 0) {
254 buf.mt_count = atoi(count);
255 if (ioctl(tape, MTIOCTOP, &buf) < 0) {
258 rval = (OFF_T)buf.mt_count;
277 DEBUG2("rmtd: W %s (block = %lu)\n", count, block);
278 record = checkbuf(record, n);
279 for (i = 0; i < n; i += cc) {
280 cc = read(0, &record[i], n - i);
282 DEBUG("rmtd: premature eof\n");
287 if ((cp = cipher(record, n, 1)) == NULL)
292 rval = write(tape, cp, n);
300 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
302 record = checkbuf(record, n);
303 rval = read(tape, record, n);
307 if ((cp = cipher(record, rval, 0)) == NULL)
312 (void)sprintf(resp, "A%lld\n", (long long)rval);
313 if (write(1, resp, strlen(resp)) != strlen(resp))
315 if (write(1, cp, rval) != rval)
323 DEBUG2("rmtd: I %s %s\n", op, count);
324 if (atoi(op) == RMTI_VERSION) {
332 /* rmt version 1, assume UNIX/Solaris/Mac OS X client */
376 mtop.mt_op = MTRETEN;
381 mtop.mt_op = MTERASE;
390 if (mtop.mt_op == -1) {
396 /* rmt version 0, assume linux client */
397 mtop.mt_op = atoi(op);
399 mtop.mt_count = atoi(count);
400 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0) {
403 rval = mtop.mt_count;
412 DEBUG2 ("rmtd: i %s %s\n", op, count);
416 mtop.mt_op = MTCACHE;
421 mtop.mt_op = MTNOCACHE;
426 mtop.mt_op = MTRETEN;
431 mtop.mt_op = MTERASE;
448 mtop.mt_count = atoi (count);
449 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0) {
453 rval = mtop.mt_count;
458 case 'S': /* status */
460 { struct mtget mtget;
462 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0) {
467 rval = sizeof(mtget);
468 /* assume byte order:
469 Linux on Intel (little), Solaris on SPARC (big), Mac OS X on PPC (big)
470 thus need byte swapping from little to big
472 mtget.mt_type = swaplong(mtget.mt_type);
473 mtget.mt_resid = swaplong(mtget.mt_resid);
474 mtget.mt_dsreg = swaplong(mtget.mt_dsreg);
475 mtget.mt_gstat = swaplong(mtget.mt_gstat);
476 mtget.mt_erreg = swaplong(mtget.mt_erreg);
477 mtget.mt_fileno = swaplong(mtget.mt_fileno);
478 mtget.mt_blkno = swaplong(mtget.mt_blkno);
479 (void)sprintf(resp, "A%lld\n", (long long)rval);
480 if (write(1, resp, strlen(resp)) != strlen(resp))
482 if (write(1, (char *)&mtget, sizeof (mtget)) != sizeof(mtget))
485 rval = sizeof (mtget);
486 (void)sprintf(resp, "A%lld\n", (long long)rval);
487 if (write(1, resp, strlen(resp)) != strlen(resp))
489 if (write(1, (char *)&mtget, sizeof (mtget)) != sizeof(mtget))
501 if (read (0, &s, 1) != 1)
503 DEBUG1 ("rmtd: s %d\n", s);
505 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0) {
511 rval = mtget.mt_type;
514 rval = mtget.mt_dsreg;
517 rval = mtget.mt_erreg;
520 rval = mtget.mt_resid;
523 rval = mtget.mt_fileno;
526 rval = mtget.mt_blkno;
529 rval = mtget.mt_gstat;
542 case 'V': /* version */
544 DEBUG1("rmtd: V %s\n", op);
549 DEBUG1("rmtd: garbage command %c\n", c);
553 DEBUG1("rmtd: A %lld\n", (long long)rval);
554 (void)sprintf(resp, "A%lld\n", (long long)rval);
555 if (write(1, resp, strlen(resp)) != strlen(resp))
563 static void getstring(char *bp)
568 for (i = 0; i < SSIZE - 1; i++) {
569 if (read(0, cp+i, 1) != 1)
578 checkbuf(char *record, int size)
581 if (size <= maxrecsize)
585 record = malloc(size);
587 DEBUG("rmtd: cannot allocate buffer space\n");
591 while (size > 1024 &&
592 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
601 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
602 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
603 if (write(1, resp, strlen(resp)) != strlen(resp))
604 DEBUG("rmtd: write error\n");
608 swaplong(unsigned long inv)
615 inp = (union lconv *)&inv;
617 outv.uc[0] = inp->uc[3];
618 outv.uc[1] = inp->uc[2];
619 outv.uc[2] = inp->uc[1];
620 outv.uc[3] = inp->uc[0];