]>
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.26 2003/04/08 19:52:37 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 char *checkbuf
__P((char *, int));
115 void error
__P((int));
116 void getstring
__P((char *));
118 char *cipher
__P((char *, int, int));
119 void decrypt
__P((void));
123 main(int argc
, char *argv
[])
127 int n
, i
, cc
, oflags
;
128 unsigned long block
= 0;
132 if (argc
> 1 && strcmp(argv
[1], "-d") == 0)
133 decrypt(); /* decrypt stdin to stdout, and exit() */
135 /* Skip "-c /etc/rmt", which appears when rmt is used as a shell */
136 if (argc
> 2 && strcmp(argv
[1], "-c") == 0)
137 argc
-= 2, argv
+= 2;
140 debug
= fopen(*argv
, "w");
143 (void)setbuf(debug
, (char *)0);
148 if (read(0, &c
, 1) != 1)
157 DEBUG2("rmtd: O %s %s\n", device
, filemode
);
159 * Translate extended GNU syntax into its numeric platform equivalent
161 oflags
= rmtflags_toint(filemode
);
164 * Default to O_BINARY the client may not know that we need it.
166 if ((oflags
& O_TEXT
) == 0)
169 DEBUG2("rmtd: O %s %d\n", device
, oflags
);
171 * XXX the rmt protocol does not provide a means to
172 * specify the permission bits; allow rw for everyone,
173 * as modified by the users umask
175 tape
= OPEN(device
, oflags
, 0666);
182 DEBUG1("rmtd: C (%lu blocks)\n", block
);
183 getstring(device
); /* discard */
193 DEBUG2("rmtd: L %s %s\n", count
, pos
);
194 rval
= LSEEK(tape
, (OFF_T
)atoll(count
), atoi(pos
));
204 DEBUG2("rmtd: W %s (block = %lu)\n", count
, block
);
205 record
= checkbuf(record
, n
);
206 for (i
= 0; i
< n
; i
+= cc
) {
207 cc
= read(0, &record
[i
], n
- i
);
209 DEBUG("rmtd: premature eof\n");
214 if ((cp
= cipher(record
, n
, 1)) == NULL
)
219 rval
= write(tape
, cp
, n
);
227 DEBUG2("rmtd: R %s (block %lu)\n", count
, block
);
229 record
= checkbuf(record
, n
);
230 rval
= read(tape
, record
, n
);
234 if ((cp
= cipher(record
, rval
, 0)) == NULL
)
239 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
240 (void)write(1, resp
, strlen(resp
));
241 (void)write(1, cp
, rval
);
248 DEBUG2("rmtd: I %s %s\n", op
, count
);
249 if (atoi(op
) == RMTI_VERSION
) {
257 /* rmt version 1, assume UNIX client */
300 if (mtop
.mt_op
== -1) {
306 /* rmt version 0, assume linux client */
307 mtop
.mt_op
= atoi(op
);
309 mtop
.mt_count
= atoi(count
);
310 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
312 rval
= mtop
.mt_count
;
321 DEBUG2 ("rmtd: i %s %s\n", op
, count
);
325 mtop
.mt_op
= MTCACHE
;
330 mtop
.mt_op
= MTNOCACHE
;
335 mtop
.mt_op
= MTRETEN
;
340 mtop
.mt_op
= MTERASE
;
357 mtop
.mt_count
= atoi (count
);
358 if (ioctl (tape
, MTIOCTOP
, (char *) &mtop
) < 0)
361 rval
= mtop
.mt_count
;
366 case 'S': /* status */
368 { struct mtget mtget
;
369 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
371 rval
= sizeof (mtget
);
372 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
373 (void)write(1, resp
, strlen(resp
));
374 (void)write(1, (char *)&mtget
, sizeof (mtget
));
382 if (read (0, &s
, 1) != 1)
385 if (ioctl (tape
, MTIOCGET
, (char *) &mtget
) < 0)
390 rval
= mtget
.mt_type
;
393 rval
= mtget
.mt_dsreg
;
396 rval
= mtget
.mt_erreg
;
399 rval
= mtget
.mt_resid
;
402 rval
= mtget
.mt_fileno
;
405 rval
= mtget
.mt_blkno
;
408 rval
= mtget
.mt_gstat
;
421 case 'V': /* version */
423 DEBUG1("rmtd: V %s\n", op
);
428 DEBUG1("rmtd: garbage command %c\n", c
);
432 DEBUG1("rmtd: A %lld\n", (long long)rval
);
433 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
434 (void)write(1, resp
, strlen(resp
));
441 void getstring(char *bp
)
446 for (i
= 0; i
< SSIZE
- 1; i
++) {
447 if (read(0, cp
+i
, 1) != 1)
456 checkbuf(char *record
, int size
)
459 if (size
<= maxrecsize
)
463 record
= malloc(size
);
465 DEBUG("rmtd: cannot allocate buffer space\n");
469 while (size
> 1024 &&
470 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
479 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
480 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
481 (void)write(1, resp
, strlen(resp
));