Imported Upstream version 0.4b37
[debian/dump] / 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.28 2003/11/22 16:52:16 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                 (void)write(1, resp, strlen(resp));
314                 (void)write(1, cp, rval);
315                 block += n >> 10;
316                 goto top;
317
318         case 'I':
319                 getstring(op);
320                 getstring(count);
321                 DEBUG2("rmtd: I %s %s\n", op, count);
322                 if (atoi(op) == RMTI_VERSION) {
323                         rval = RMT_VERSION;
324                         rmt_version = 1;
325                 } 
326                 else { 
327                         struct mtop mtop;
328                         mtop.mt_op = -1;
329                         if (rmt_version) {
330                                 /* rmt version 1, assume UNIX/Solaris/Mac OS X client */
331                                 switch (atoi(op)) {
332 #ifdef  MTWEOF
333                                         case 0:
334                                                 mtop.mt_op = MTWEOF;
335                                                 break;
336 #endif
337 #ifdef  MTFSF
338                                         case 1:
339                                                 mtop.mt_op = MTFSF;
340                                                 break;
341 #endif
342 #ifdef  MTBSF
343                                         case 2:
344                                                 mtop.mt_op = MTBSF;
345                                                 break;
346 #endif
347 #ifdef  MTFSR
348                                         case 3:
349                                                 mtop.mt_op = MTFSR;
350                                                 break;
351 #endif
352 #ifdef  MTBSR
353                                         case 4:
354                                                 mtop.mt_op = MTBSR;
355                                                 break;
356 #endif
357 #ifdef  MTREW
358                                         case 5:
359                                                 mtop.mt_op = MTREW;
360                                                 break;
361 #endif
362 #ifdef  MTOFFL
363                                         case 6:
364                                                 mtop.mt_op = MTOFFL;
365                                                 break;
366 #endif
367 #ifdef  MTNOP
368                                         case 7:
369                                                 mtop.mt_op = MTNOP;
370                                                 break;
371 #endif
372 #ifdef  MTRETEN
373                     case 8:
374                         mtop.mt_op = MTRETEN;
375                         break;
376 #endif
377 #ifdef  MTERASE
378                     case 9:
379                         mtop.mt_op = MTERASE;
380                         break;
381 #endif
382 #ifdef  MTEOM
383                     case 10:
384                         mtop.mt_op = MTEOM;
385                         break;
386 #endif
387                                 }
388                                 if (mtop.mt_op == -1) {
389                                         errno = EINVAL;
390                                         goto ioerror;
391                                 }
392                         }
393                         else {
394                                 /* rmt version 0, assume linux client */
395                                 mtop.mt_op = atoi(op);
396                         }
397                         mtop.mt_count = atoi(count);
398                         if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0) {
399                                 goto ioerror;
400                         }
401                         rval = mtop.mt_count;
402                 }
403                 goto respond;
404
405         case 'i':
406         {       struct mtop mtop;
407  
408                 getstring (op);
409                 getstring (count);
410                 DEBUG2 ("rmtd: i %s %s\n", op, count);
411                 switch (atoi(op)) {
412 #ifdef MTCACHE
413                         case RMTI_CACHE:
414                                 mtop.mt_op = MTCACHE;
415                                 break;
416 #endif
417 #ifdef MTNOCACHE
418                         case RMTI_NOCACHE:
419                                 mtop.mt_op = MTNOCACHE;
420                                 break;
421 #endif
422 #ifdef MTRETEN
423                         case RMTI_RETEN:
424                                 mtop.mt_op = MTRETEN;
425                                 break;
426 #endif
427 #ifdef MTERASE
428                         case RMTI_ERASE:
429                                 mtop.mt_op = MTERASE;
430                                 break;
431 #endif
432 #ifdef MTEOM
433                         case RMTI_EOM:
434                                 mtop.mt_op = MTEOM;
435                                 break;
436 #endif
437 #ifdef MTNBSF
438                         case RMTI_NBSF:
439                                 mtop.mt_op = MTNBSF;
440                                 break;
441 #endif
442                         default:
443                                 errno = EINVAL;
444                                 goto ioerror;
445                 }
446                 mtop.mt_count = atoi (count);
447                 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0) {
448                         goto ioerror;
449                 }
450
451                 rval = mtop.mt_count;
452
453                 goto respond;
454         }
455
456         case 'S':               /* status */
457                 DEBUG("rmtd: S\n");
458                 { struct mtget mtget;
459
460                   if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0) {
461                         goto ioerror;
462                   }
463
464                   if (rmt_version) {
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
469                         */
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));
480                   } else {
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));
485                   }
486                   goto top;
487                 }
488
489         case 's':
490         {       char s;
491                 struct mtget mtget;
492  
493                 DEBUG ("rmtd: s\n");
494
495                 if (read (0, &s, 1) != 1)
496                         goto top;
497                 DEBUG1 ("rmtd: s %d\n", s);
498  
499                 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0) {
500                         goto ioerror;
501                 }
502
503                 switch (s) {
504                         case MTS_TYPE:
505                                 rval = mtget.mt_type;
506                                 break;
507                         case MTS_DSREG:
508                                 rval = mtget.mt_dsreg;
509                                 break;
510                         case MTS_ERREG:
511                                 rval = mtget.mt_erreg;
512                                 break;
513                         case MTS_RESID:
514                                 rval = mtget.mt_resid;
515                                 break;
516                         case MTS_FILENO:
517                                 rval = mtget.mt_fileno;
518                                 break;
519                         case MTS_BLKNO:
520                                 rval = mtget.mt_blkno;
521                                 break;
522                         case MTS_FLAGS:
523                                 rval = mtget.mt_gstat;
524                                 break;
525                         case MTS_BF:
526                                 rval = 0;
527                                 break;
528                         default:
529                                 errno = EINVAL;
530                                 goto ioerror;
531                 }
532
533                 goto respond;
534         }
535
536         case 'V':       /* version */
537                 getstring(op);
538                 DEBUG1("rmtd: V %s\n", op);
539                 rval = 2;
540                 goto respond;
541
542         default:
543                 DEBUG1("rmtd: garbage command %c\n", c);
544                 exit(3);
545         }
546 respond:
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));
550         goto top;
551 ioerror:
552         error(errno);
553         goto top;
554 }
555
556 static void getstring(char *bp)
557 {
558         int i;
559         char *cp = bp;
560
561         for (i = 0; i < SSIZE - 1; i++) {
562                 if (read(0, cp+i, 1) != 1)
563                         exit(0);
564                 if (cp[i] == '\n')
565                         break;
566         }
567         cp[i] = '\0';
568 }
569
570 static char *
571 checkbuf(char *record, int size)
572 {
573
574         if (size <= maxrecsize)
575                 return (record);
576         if (record != 0)
577                 free(record);
578         record = malloc(size);
579         if (record == 0) {
580                 DEBUG("rmtd: cannot allocate buffer space\n");
581                 exit(4);
582         }
583         maxrecsize = size;
584         while (size > 1024 &&
585                setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
586                 size -= 1024;
587         return (record);
588 }
589
590 static void
591 error(int num)
592 {
593
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));
597 }
598
599 static unsigned long
600 swaplong(unsigned long inv)
601 {
602          union lconv {
603                 unsigned long   ul;
604                 unsigned char   uc[4];
605         } *inp, outv;
606
607         inp = (union lconv *)&inv;
608
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];
613
614         return (outv.ul);
615 }