]>
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.28 2003/11/22 16:52:16 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 (void)write(1, resp
, strlen(resp
));
314 (void)write(1, cp
, rval
);
321 DEBUG2("rmtd: I %s %s\n", op
, count
);
322 if (atoi(op
) == RMTI_VERSION
) {
330 /* rmt version 1, assume UNIX/Solaris/Mac OS X client */
374 mtop
.mt_op
= MTRETEN
;
379 mtop
.mt_op
= MTERASE
;
388 if (mtop
.mt_op
== -1) {
394 /* rmt version 0, assume linux client */
395 mtop
.mt_op
= atoi(op
);
397 mtop
.mt_count
= atoi(count
);
398 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0) {
401 rval
= mtop
.mt_count
;
410 DEBUG2 ("rmtd: i %s %s\n", op
, count
);
414 mtop
.mt_op
= MTCACHE
;
419 mtop
.mt_op
= MTNOCACHE
;
424 mtop
.mt_op
= MTRETEN
;
429 mtop
.mt_op
= MTERASE
;
446 mtop
.mt_count
= atoi (count
);
447 if (ioctl (tape
, MTIOCTOP
, (char *) &mtop
) < 0) {
451 rval
= mtop
.mt_count
;
456 case 'S': /* status */
458 { struct mtget mtget
;
460 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0) {
465 rval
= sizeof(mtget
);
466 /* assume byte order:
467 Linux on Intel (little), Solaris on SPARC (big), Mac OS X on PPC (big)
468 thus need byte swapping from little to big
470 mtget
.mt_type
= swaplong(mtget
.mt_type
);
471 mtget
.mt_resid
= swaplong(mtget
.mt_resid
);
472 mtget
.mt_dsreg
= swaplong(mtget
.mt_dsreg
);
473 mtget
.mt_gstat
= swaplong(mtget
.mt_gstat
);
474 mtget
.mt_erreg
= swaplong(mtget
.mt_erreg
);
475 mtget
.mt_fileno
= swaplong(mtget
.mt_fileno
);
476 mtget
.mt_blkno
= swaplong(mtget
.mt_blkno
);
477 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
478 (void)write(1, resp
, strlen(resp
));
479 (void)write(1, (char *)&mtget
, sizeof (mtget
));
481 rval
= sizeof (mtget
);
482 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
483 (void)write(1, resp
, strlen(resp
));
484 (void)write(1, (char *)&mtget
, sizeof (mtget
));
495 if (read (0, &s
, 1) != 1)
497 DEBUG1 ("rmtd: s %d\n", s
);
499 if (ioctl (tape
, MTIOCGET
, (char *) &mtget
) < 0) {
505 rval
= mtget
.mt_type
;
508 rval
= mtget
.mt_dsreg
;
511 rval
= mtget
.mt_erreg
;
514 rval
= mtget
.mt_resid
;
517 rval
= mtget
.mt_fileno
;
520 rval
= mtget
.mt_blkno
;
523 rval
= mtget
.mt_gstat
;
536 case 'V': /* version */
538 DEBUG1("rmtd: V %s\n", op
);
543 DEBUG1("rmtd: garbage command %c\n", c
);
547 DEBUG1("rmtd: A %lld\n", (long long)rval
);
548 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
549 (void)write(1, resp
, strlen(resp
));
556 static void getstring(char *bp
)
561 for (i
= 0; i
< SSIZE
- 1; i
++) {
562 if (read(0, cp
+i
, 1) != 1)
571 checkbuf(char *record
, int size
)
574 if (size
<= maxrecsize
)
578 record
= malloc(size
);
580 DEBUG("rmtd: cannot allocate buffer space\n");
584 while (size
> 1024 &&
585 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
594 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
595 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
596 (void)write(1, resp
, strlen(resp
));
600 swaplong(unsigned long inv
)
607 inp
= (union lconv
*)&inv
;
609 outv
.uc
[0] = inp
->uc
[3];
610 outv
.uc
[1] = inp
->uc
[2];
611 outv
.uc
[2] = inp
->uc
[1];
612 outv
.uc
[3] = inp
->uc
[0];