]> git.wh0rd.org - dump.git/blob - rmt/rmt.c
Fix some warn_unused_result compile warnings
[dump.git] / rmt / rmt.c
1 /*
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
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
24 *
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
35 * SUCH DAMAGE.
36 */
37
38 #ifndef lint
39 static const char rcsid[] =
40 "$Id: rmt.c,v 1.29 2010/06/11 11:19:18 stelian Exp $";
41 #endif /* not linux */
42
43 /*
44 * rmt
45 */
46 #include <config.h>
47 #include <compatlfs.h>
48 #include <rmtflags.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/mtio.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #ifndef __linux__
55 #include <sgtty.h>
56 #endif
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 static int tape = -1;
63
64 static char *record;
65 static int maxrecsize = -1;
66
67 #define SSIZE 64
68 static char device[SSIZE];
69 static char count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
70
71 static char resp[BUFSIZ];
72
73 static FILE *debug;
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)
77
78 /*
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.
82 *
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
88 *
89 * In order to use rmt version 1, a client must send "I-1\n0\n"
90 * before issuing the other I commands.
91 */
92 static int rmt_version = 0;
93 #define RMTI_VERSION -1
94 #define RMT_VERSION 1
95
96 /* Extended 'i' commands */
97 #define RMTI_CACHE 0
98 #define RMTI_NOCACHE 1
99 #define RMTI_RETEN 2
100 #define RMTI_ERASE 3
101 #define RMTI_EOM 4
102 #define RMTI_NBSF 5
103
104 /* Extended 's' comands */
105 #define MTS_TYPE 'T'
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'
112 #define MTS_BF 'b'
113
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));
118 #ifdef ERMT
119 char *cipher __P((char *, int, int));
120 void decrypt __P((void));
121 #endif
122
123 int
124 main(int argc, char *argv[])
125 {
126 OFF_T rval = 0;
127 char c;
128 int n, i, cc, oflags;
129 unsigned long block = 0;
130 char *cp;
131
132 int magtape = 0;
133
134 #ifdef ERMT
135 if (argc > 1 && strcmp(argv[1], "-d") == 0)
136 decrypt(); /* decrypt stdin to stdout, and exit() */
137 #endif
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;
141 argc--, argv++;
142 if (argc > 0) {
143 debug = fopen(*argv, "w");
144 if (debug == 0)
145 exit(1);
146 (void)setbuf(debug, (char *)0);
147 }
148 top:
149 errno = 0;
150 rval = 0;
151 if (read(0, &c, 1) != 1)
152 exit(0);
153 switch (c) {
154
155 case 'O':
156 if (tape >= 0)
157 (void) close(tape);
158 getstring(device);
159 getstring(filemode);
160 DEBUG2("rmtd: O %s %s\n", device, filemode);
161 /*
162 * Translate extended GNU syntax into its numeric platform equivalent
163 */
164 oflags = rmtflags_toint(filemode);
165 #ifdef O_TEXT
166 /*
167 * Default to O_BINARY the client may not know that we need it.
168 */
169 if ((oflags & O_TEXT) == 0)
170 oflags |= O_BINARY;
171 #endif
172 DEBUG2("rmtd: O %s %d\n", device, oflags);
173 /*
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
177 */
178 tape = OPEN(device, oflags, 0666);
179 if (tape < 0)
180 goto ioerror;
181 block = 0;
182 {
183 struct mtget mt_stat;
184 magtape = ioctl(tape, MTIOCGET, (char *)&mt_stat) == 0;
185 }
186 goto respond;
187
188 case 'C':
189 DEBUG1("rmtd: C (%lu blocks)\n", block);
190 getstring(device); /* discard */
191 if (close(tape) < 0)
192 goto ioerror;
193 tape = -1;
194 block = 0;
195 goto respond;
196
197 #ifdef USE_QFA
198 #define LSEEK_GET_TAPEPOS 10
199 #define LSEEK_GO2_TAPEPOS 11
200 #endif
201
202 case 'L':
203 getstring(count);
204 getstring(pos);
205 DEBUG2("rmtd: L %s %s\n", count, pos);
206 if (!magtape) { /* traditional */
207 switch (atoi(pos)) {
208 case SEEK_SET:
209 case SEEK_CUR:
210 case SEEK_END:
211 rval = LSEEK(tape, (OFF_T)atoll(count), atoi(pos));
212 break;
213 #ifdef USE_QFA
214 case LSEEK_GET_TAPEPOS:
215 rval = LSEEK(tape, (OFF_T)0, SEEK_CUR);
216 break;
217 case LSEEK_GO2_TAPEPOS:
218 rval = LSEEK(tape, (OFF_T)atoll(count), SEEK_SET);
219 break;
220 #endif /* USE_QFA */
221 default:
222 errno = EINVAL;
223 goto ioerror;
224 break;
225 }
226 }
227 else {
228 switch (atoi(pos)) {
229 case SEEK_SET:
230 case SEEK_CUR:
231 case SEEK_END:
232 rval = LSEEK(tape, (OFF_T)atoll(count), atoi(pos));
233 break;
234 #ifdef USE_QFA
235 case LSEEK_GET_TAPEPOS: /* QFA */
236 case LSEEK_GO2_TAPEPOS:
237 {
238 struct mtop buf;
239 long mtpos;
240
241 buf.mt_op = MTSETDRVBUFFER;
242 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
243 if (ioctl(tape, MTIOCTOP, &buf) < 0) {
244 goto ioerror;
245 }
246
247 if (atoi(pos) == LSEEK_GET_TAPEPOS) { /* get tapepos */
248 if (ioctl(tape, MTIOCPOS, &mtpos) < 0) {
249 goto ioerror;
250 }
251 rval = (OFF_T)mtpos;
252 } else {
253 buf.mt_op = MTSEEK;
254 buf.mt_count = atoi(count);
255 if (ioctl(tape, MTIOCTOP, &buf) < 0) {
256 goto ioerror;
257 }
258 rval = (OFF_T)buf.mt_count;
259 }
260 }
261 break;
262 #endif /* USE_QFA */
263 default:
264 errno = EINVAL;
265 goto ioerror;
266 }
267 }
268 if (rval < 0)
269 goto ioerror;
270 goto respond;
271
272 case 'W':
273 getstring(count);
274 n = atoi(count);
275 if (n < 1)
276 exit(2);
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);
281 if (cc <= 0) {
282 DEBUG("rmtd: premature eof\n");
283 exit(2);
284 }
285 }
286 #ifdef ERMT
287 if ((cp = cipher(record, n, 1)) == NULL)
288 goto ioerror;
289 #else
290 cp = record;
291 #endif
292 rval = write(tape, cp, n);
293 if (rval < 0)
294 goto ioerror;
295 block += n >> 10;
296 goto respond;
297
298 case 'R':
299 getstring(count);
300 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
301 n = atoi(count);
302 record = checkbuf(record, n);
303 rval = read(tape, record, n);
304 if (rval < 0)
305 goto ioerror;
306 #ifdef ERMT
307 if ((cp = cipher(record, rval, 0)) == NULL)
308 goto ioerror;
309 #else
310 cp = record;
311 #endif
312 (void)sprintf(resp, "A%lld\n", (long long)rval);
313 if (write(1, resp, strlen(resp)) != strlen(resp))
314 goto ioerror;
315 if (write(1, cp, rval) != rval)
316 goto ioerror;
317 block += n >> 10;
318 goto top;
319
320 case 'I':
321 getstring(op);
322 getstring(count);
323 DEBUG2("rmtd: I %s %s\n", op, count);
324 if (atoi(op) == RMTI_VERSION) {
325 rval = RMT_VERSION;
326 rmt_version = 1;
327 }
328 else {
329 struct mtop mtop;
330 mtop.mt_op = -1;
331 if (rmt_version) {
332 /* rmt version 1, assume UNIX/Solaris/Mac OS X client */
333 switch (atoi(op)) {
334 #ifdef MTWEOF
335 case 0:
336 mtop.mt_op = MTWEOF;
337 break;
338 #endif
339 #ifdef MTFSF
340 case 1:
341 mtop.mt_op = MTFSF;
342 break;
343 #endif
344 #ifdef MTBSF
345 case 2:
346 mtop.mt_op = MTBSF;
347 break;
348 #endif
349 #ifdef MTFSR
350 case 3:
351 mtop.mt_op = MTFSR;
352 break;
353 #endif
354 #ifdef MTBSR
355 case 4:
356 mtop.mt_op = MTBSR;
357 break;
358 #endif
359 #ifdef MTREW
360 case 5:
361 mtop.mt_op = MTREW;
362 break;
363 #endif
364 #ifdef MTOFFL
365 case 6:
366 mtop.mt_op = MTOFFL;
367 break;
368 #endif
369 #ifdef MTNOP
370 case 7:
371 mtop.mt_op = MTNOP;
372 break;
373 #endif
374 #ifdef MTRETEN
375 case 8:
376 mtop.mt_op = MTRETEN;
377 break;
378 #endif
379 #ifdef MTERASE
380 case 9:
381 mtop.mt_op = MTERASE;
382 break;
383 #endif
384 #ifdef MTEOM
385 case 10:
386 mtop.mt_op = MTEOM;
387 break;
388 #endif
389 }
390 if (mtop.mt_op == -1) {
391 errno = EINVAL;
392 goto ioerror;
393 }
394 }
395 else {
396 /* rmt version 0, assume linux client */
397 mtop.mt_op = atoi(op);
398 }
399 mtop.mt_count = atoi(count);
400 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0) {
401 goto ioerror;
402 }
403 rval = mtop.mt_count;
404 }
405 goto respond;
406
407 case 'i':
408 { struct mtop mtop;
409
410 getstring (op);
411 getstring (count);
412 DEBUG2 ("rmtd: i %s %s\n", op, count);
413 switch (atoi(op)) {
414 #ifdef MTCACHE
415 case RMTI_CACHE:
416 mtop.mt_op = MTCACHE;
417 break;
418 #endif
419 #ifdef MTNOCACHE
420 case RMTI_NOCACHE:
421 mtop.mt_op = MTNOCACHE;
422 break;
423 #endif
424 #ifdef MTRETEN
425 case RMTI_RETEN:
426 mtop.mt_op = MTRETEN;
427 break;
428 #endif
429 #ifdef MTERASE
430 case RMTI_ERASE:
431 mtop.mt_op = MTERASE;
432 break;
433 #endif
434 #ifdef MTEOM
435 case RMTI_EOM:
436 mtop.mt_op = MTEOM;
437 break;
438 #endif
439 #ifdef MTNBSF
440 case RMTI_NBSF:
441 mtop.mt_op = MTNBSF;
442 break;
443 #endif
444 default:
445 errno = EINVAL;
446 goto ioerror;
447 }
448 mtop.mt_count = atoi (count);
449 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0) {
450 goto ioerror;
451 }
452
453 rval = mtop.mt_count;
454
455 goto respond;
456 }
457
458 case 'S': /* status */
459 DEBUG("rmtd: S\n");
460 { struct mtget mtget;
461
462 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0) {
463 goto ioerror;
464 }
465
466 if (rmt_version) {
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
471 */
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))
481 goto ioerror;
482 if (write(1, (char *)&mtget, sizeof (mtget)) != sizeof(mtget))
483 goto ioerror;
484 } else {
485 rval = sizeof (mtget);
486 (void)sprintf(resp, "A%lld\n", (long long)rval);
487 if (write(1, resp, strlen(resp)) != strlen(resp))
488 goto ioerror;
489 if (write(1, (char *)&mtget, sizeof (mtget)) != sizeof(mtget))
490 goto ioerror;
491 }
492 goto top;
493 }
494
495 case 's':
496 { char s;
497 struct mtget mtget;
498
499 DEBUG ("rmtd: s\n");
500
501 if (read (0, &s, 1) != 1)
502 goto top;
503 DEBUG1 ("rmtd: s %d\n", s);
504
505 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0) {
506 goto ioerror;
507 }
508
509 switch (s) {
510 case MTS_TYPE:
511 rval = mtget.mt_type;
512 break;
513 case MTS_DSREG:
514 rval = mtget.mt_dsreg;
515 break;
516 case MTS_ERREG:
517 rval = mtget.mt_erreg;
518 break;
519 case MTS_RESID:
520 rval = mtget.mt_resid;
521 break;
522 case MTS_FILENO:
523 rval = mtget.mt_fileno;
524 break;
525 case MTS_BLKNO:
526 rval = mtget.mt_blkno;
527 break;
528 case MTS_FLAGS:
529 rval = mtget.mt_gstat;
530 break;
531 case MTS_BF:
532 rval = 0;
533 break;
534 default:
535 errno = EINVAL;
536 goto ioerror;
537 }
538
539 goto respond;
540 }
541
542 case 'V': /* version */
543 getstring(op);
544 DEBUG1("rmtd: V %s\n", op);
545 rval = 2;
546 goto respond;
547
548 default:
549 DEBUG1("rmtd: garbage command %c\n", c);
550 exit(3);
551 }
552 respond:
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))
556 goto ioerror;
557 goto top;
558 ioerror:
559 error(errno);
560 goto top;
561 }
562
563 static void getstring(char *bp)
564 {
565 int i;
566 char *cp = bp;
567
568 for (i = 0; i < SSIZE - 1; i++) {
569 if (read(0, cp+i, 1) != 1)
570 exit(0);
571 if (cp[i] == '\n')
572 break;
573 }
574 cp[i] = '\0';
575 }
576
577 static char *
578 checkbuf(char *record, int size)
579 {
580
581 if (size <= maxrecsize)
582 return (record);
583 if (record != 0)
584 free(record);
585 record = malloc(size);
586 if (record == 0) {
587 DEBUG("rmtd: cannot allocate buffer space\n");
588 exit(4);
589 }
590 maxrecsize = size;
591 while (size > 1024 &&
592 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
593 size -= 1024;
594 return (record);
595 }
596
597 static void
598 error(int num)
599 {
600
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");
605 }
606
607 static unsigned long
608 swaplong(unsigned long inv)
609 {
610 union lconv {
611 unsigned long ul;
612 unsigned char uc[4];
613 } *inp, outv;
614
615 inp = (union lconv *)&inv;
616
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];
621
622 return (outv.ul);
623 }