]> git.wh0rd.org Git - dump.git/blob - rmt/rmt.c
From Uwe Gohlke:
[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.27 2003/10/26 16:05:49 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                         rval = LSEEK(tape, (OFF_T)atoll(count), atoi(pos));
208                 }
209                 else {
210                         switch (atoi(pos)) {
211                         case SEEK_SET:
212                         case SEEK_CUR:
213                         case SEEK_END:
214                                 rval = LSEEK(tape, (OFF_T)atoll(count), atoi(pos));
215                                 break;
216 #ifdef USE_QFA
217                         case LSEEK_GET_TAPEPOS: /* QFA */
218                         case LSEEK_GO2_TAPEPOS:
219                                 {
220                                 struct mtop buf;
221                                 long mtpos;
222
223                                 buf.mt_op = MTSETDRVBUFFER;
224                                 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
225                                 if (ioctl(tape, MTIOCTOP, &buf) < 0) {
226                                         goto ioerror;
227                                 }
228
229                                 if (atoi(pos) == LSEEK_GET_TAPEPOS) { /* get tapepos */
230                                         if (ioctl(tape, MTIOCPOS, &mtpos) < 0) {
231                                                 goto ioerror;
232                                         }
233                                         rval = (OFF_T)mtpos;
234                                 } else {
235                                         buf.mt_op = MTSEEK;
236                                         buf.mt_count = atoi(count);
237                                         if (ioctl(tape, MTIOCTOP, &buf) < 0) {
238                                                 goto ioerror;
239                                         }
240                                         rval = (OFF_T)buf.mt_count;
241                                 }
242                                 }
243                                 break;
244 #endif /* USE_QFA */
245                         default:
246                                 errno = EINVAL;
247                                 goto ioerror;
248                         }
249                 }
250                 if (rval < 0)
251                         goto ioerror;
252                 goto respond;
253
254         case 'W':
255                 getstring(count);
256                 n = atoi(count);
257                 if (n < 1)
258                         exit(2);
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);
263                         if (cc <= 0) {
264                                 DEBUG("rmtd: premature eof\n");
265                                 exit(2);
266                         }
267                 }
268 #ifdef ERMT
269                 if ((cp = cipher(record, n, 1)) == NULL)
270                         goto ioerror;
271 #else
272                 cp = record;
273 #endif
274                 rval = write(tape, cp, n);
275                 if (rval < 0)
276                         goto ioerror;
277                 block += n >> 10;
278                 goto respond;
279
280         case 'R':
281                 getstring(count);
282                 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
283                 n = atoi(count);
284                 record = checkbuf(record, n);
285                 rval = read(tape, record, n);
286                 if (rval < 0)
287                         goto ioerror;
288 #ifdef ERMT
289                 if ((cp = cipher(record, rval, 0)) == NULL)
290                         goto ioerror;
291 #else
292                 cp = record;
293 #endif
294                 (void)sprintf(resp, "A%lld\n", (long long)rval);
295                 (void)write(1, resp, strlen(resp));
296                 (void)write(1, cp, rval);
297                 block += n >> 10;
298                 goto top;
299
300         case 'I':
301                 getstring(op);
302                 getstring(count);
303                 DEBUG2("rmtd: I %s %s\n", op, count);
304                 if (atoi(op) == RMTI_VERSION) {
305                         rval = RMT_VERSION;
306                         rmt_version = 1;
307                 } 
308                 else { 
309                         struct mtop mtop;
310                         mtop.mt_op = -1;
311                         if (rmt_version) {
312                                 /* rmt version 1, assume UNIX/Solaris/Mac OS X client */
313                                 switch (atoi(op)) {
314 #ifdef  MTWEOF
315                                         case 0:
316                                                 mtop.mt_op = MTWEOF;
317                                                 break;
318 #endif
319 #ifdef  MTFSF
320                                         case 1:
321                                                 mtop.mt_op = MTFSF;
322                                                 break;
323 #endif
324 #ifdef  MTBSF
325                                         case 2:
326                                                 mtop.mt_op = MTBSF;
327                                                 break;
328 #endif
329 #ifdef  MTFSR
330                                         case 3:
331                                                 mtop.mt_op = MTFSR;
332                                                 break;
333 #endif
334 #ifdef  MTBSR
335                                         case 4:
336                                                 mtop.mt_op = MTBSR;
337                                                 break;
338 #endif
339 #ifdef  MTREW
340                                         case 5:
341                                                 mtop.mt_op = MTREW;
342                                                 break;
343 #endif
344 #ifdef  MTOFFL
345                                         case 6:
346                                                 mtop.mt_op = MTOFFL;
347                                                 break;
348 #endif
349 #ifdef  MTNOP
350                                         case 7:
351                                                 mtop.mt_op = MTNOP;
352                                                 break;
353 #endif
354 #ifdef  MTRETEN
355                     case 8:
356                         mtop.mt_op = MTRETEN;
357                         break;
358 #endif
359 #ifdef  MTERASE
360                     case 9:
361                         mtop.mt_op = MTERASE;
362                         break;
363 #endif
364 #ifdef  MTEOM
365                     case 10:
366                         mtop.mt_op = MTEOM;
367                         break;
368 #endif
369                                 }
370                                 if (mtop.mt_op == -1) {
371                                         errno = EINVAL;
372                                         goto ioerror;
373                                 }
374                         }
375                         else {
376                                 /* rmt version 0, assume linux client */
377                                 mtop.mt_op = atoi(op);
378                         }
379                         mtop.mt_count = atoi(count);
380                         if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0) {
381                                 goto ioerror;
382                         }
383                         rval = mtop.mt_count;
384                 }
385                 goto respond;
386
387         case 'i':
388         {       struct mtop mtop;
389  
390                 getstring (op);
391                 getstring (count);
392                 DEBUG2 ("rmtd: i %s %s\n", op, count);
393                 switch (atoi(op)) {
394 #ifdef MTCACHE
395                         case RMTI_CACHE:
396                                 mtop.mt_op = MTCACHE;
397                                 break;
398 #endif
399 #ifdef MTNOCACHE
400                         case RMTI_NOCACHE:
401                                 mtop.mt_op = MTNOCACHE;
402                                 break;
403 #endif
404 #ifdef MTRETEN
405                         case RMTI_RETEN:
406                                 mtop.mt_op = MTRETEN;
407                                 break;
408 #endif
409 #ifdef MTERASE
410                         case RMTI_ERASE:
411                                 mtop.mt_op = MTERASE;
412                                 break;
413 #endif
414 #ifdef MTEOM
415                         case RMTI_EOM:
416                                 mtop.mt_op = MTEOM;
417                                 break;
418 #endif
419 #ifdef MTNBSF
420                         case RMTI_NBSF:
421                                 mtop.mt_op = MTNBSF;
422                                 break;
423 #endif
424                         default:
425                                 errno = EINVAL;
426                                 goto ioerror;
427                 }
428                 mtop.mt_count = atoi (count);
429                 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0) {
430                         goto ioerror;
431                 }
432
433                 rval = mtop.mt_count;
434
435                 goto respond;
436         }
437
438         case 'S':               /* status */
439                 DEBUG("rmtd: S\n");
440                 { struct mtget mtget;
441
442                   if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0) {
443                         goto ioerror;
444                   }
445
446                   if (rmt_version) {
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
451                         */
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));
462                   } else {
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));
467                   }
468                   goto top;
469                 }
470
471         case 's':
472         {       char s;
473                 struct mtget mtget;
474  
475                 DEBUG ("rmtd: s\n");
476
477                 if (read (0, &s, 1) != 1)
478                         goto top;
479                 DEBUG1 ("rmtd: s %d\n", s);
480  
481                 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0) {
482                         goto ioerror;
483                 }
484
485                 switch (s) {
486                         case MTS_TYPE:
487                                 rval = mtget.mt_type;
488                                 break;
489                         case MTS_DSREG:
490                                 rval = mtget.mt_dsreg;
491                                 break;
492                         case MTS_ERREG:
493                                 rval = mtget.mt_erreg;
494                                 break;
495                         case MTS_RESID:
496                                 rval = mtget.mt_resid;
497                                 break;
498                         case MTS_FILENO:
499                                 rval = mtget.mt_fileno;
500                                 break;
501                         case MTS_BLKNO:
502                                 rval = mtget.mt_blkno;
503                                 break;
504                         case MTS_FLAGS:
505                                 rval = mtget.mt_gstat;
506                                 break;
507                         case MTS_BF:
508                                 rval = 0;
509                                 break;
510                         default:
511                                 errno = EINVAL;
512                                 goto ioerror;
513                 }
514
515                 goto respond;
516         }
517
518         case 'V':       /* version */
519                 getstring(op);
520                 DEBUG1("rmtd: V %s\n", op);
521                 rval = 2;
522                 goto respond;
523
524         default:
525                 DEBUG1("rmtd: garbage command %c\n", c);
526                 exit(3);
527         }
528 respond:
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));
532         goto top;
533 ioerror:
534         error(errno);
535         goto top;
536 }
537
538 static void getstring(char *bp)
539 {
540         int i;
541         char *cp = bp;
542
543         for (i = 0; i < SSIZE - 1; i++) {
544                 if (read(0, cp+i, 1) != 1)
545                         exit(0);
546                 if (cp[i] == '\n')
547                         break;
548         }
549         cp[i] = '\0';
550 }
551
552 static char *
553 checkbuf(char *record, int size)
554 {
555
556         if (size <= maxrecsize)
557                 return (record);
558         if (record != 0)
559                 free(record);
560         record = malloc(size);
561         if (record == 0) {
562                 DEBUG("rmtd: cannot allocate buffer space\n");
563                 exit(4);
564         }
565         maxrecsize = size;
566         while (size > 1024 &&
567                setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
568                 size -= 1024;
569         return (record);
570 }
571
572 static void
573 error(int num)
574 {
575
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));
579 }
580
581 static unsigned long
582 swaplong(unsigned long inv)
583 {
584          union lconv {
585                 unsigned long   ul;
586                 unsigned char   uc[4];
587         } *inp, outv;
588
589         inp = (union lconv *)&inv;
590
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];
595
596         return (outv.ul);
597 }