]> git.wh0rd.org Git - dump.git/blob - rmt/rmt.c
Encrypting rmt
[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.26 2003/04/08 19:52:37 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 char    *checkbuf __P((char *, int));
115 void     error __P((int));
116 void     getstring __P((char *));
117 #ifdef ERMT
118 char    *cipher __P((char *, int, int));
119 void    decrypt __P((void));
120 #endif
121
122 int
123 main(int argc, char *argv[])
124 {
125         OFF_T rval = 0;
126         char c;
127         int n, i, cc, oflags;
128         unsigned long block = 0;
129         char *cp;
130
131 #ifdef ERMT
132         if (argc > 1 && strcmp(argv[1], "-d") == 0)
133                 decrypt(); /* decrypt stdin to stdout, and exit() */
134 #endif
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;
138         argc--, argv++;
139         if (argc > 0) {
140                 debug = fopen(*argv, "w");
141                 if (debug == 0)
142                         exit(1);
143                 (void)setbuf(debug, (char *)0);
144         }
145 top:
146         errno = 0;
147         rval = 0;
148         if (read(0, &c, 1) != 1)
149                 exit(0);
150         switch (c) {
151
152         case 'O':
153                 if (tape >= 0)
154                         (void) close(tape);
155                 getstring(device);
156                 getstring(filemode);
157                 DEBUG2("rmtd: O %s %s\n", device, filemode);
158                 /*
159                  * Translate extended GNU syntax into its numeric platform equivalent
160                  */
161                 oflags = rmtflags_toint(filemode);
162 #ifdef  O_TEXT
163                 /*
164                  * Default to O_BINARY the client may not know that we need it.
165                  */
166                 if ((oflags & O_TEXT) == 0)
167                         oflags |= O_BINARY;
168 #endif
169                 DEBUG2("rmtd: O %s %d\n", device, oflags);
170                 /*
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
174                  */
175                 tape = OPEN(device, oflags, 0666);
176                 if (tape < 0)
177                         goto ioerror;
178                 block = 0;
179                 goto respond;
180
181         case 'C':
182                 DEBUG1("rmtd: C  (%lu blocks)\n", block);
183                 getstring(device);              /* discard */
184                 if (close(tape) < 0)
185                         goto ioerror;
186                 tape = -1;
187                 block = 0;
188                 goto respond;
189
190         case 'L':
191                 getstring(count);
192                 getstring(pos);
193                 DEBUG2("rmtd: L %s %s\n", count, pos);
194                 rval = LSEEK(tape, (OFF_T)atoll(count), atoi(pos));
195                 if (rval < 0)
196                         goto ioerror;
197                 goto respond;
198
199         case 'W':
200                 getstring(count);
201                 n = atoi(count);
202                 if (n < 1)
203                         exit(2);
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);
208                         if (cc <= 0) {
209                                 DEBUG("rmtd: premature eof\n");
210                                 exit(2);
211                         }
212                 }
213 #ifdef ERMT
214                 if ((cp = cipher(record, n, 1)) == NULL)
215                         goto ioerror;
216 #else
217                 cp = record;
218 #endif
219                 rval = write(tape, cp, n);
220                 if (rval < 0)
221                         goto ioerror;
222                 block += n >> 10;
223                 goto respond;
224
225         case 'R':
226                 getstring(count);
227                 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
228                 n = atoi(count);
229                 record = checkbuf(record, n);
230                 rval = read(tape, record, n);
231                 if (rval < 0)
232                         goto ioerror;
233 #ifdef ERMT
234                 if ((cp = cipher(record, rval, 0)) == NULL)
235                         goto ioerror;
236 #else
237                 cp = record;
238 #endif
239                 (void)sprintf(resp, "A%lld\n", (long long)rval);
240                 (void)write(1, resp, strlen(resp));
241                 (void)write(1, cp, rval);
242                 block += n >> 10;
243                 goto top;
244
245         case 'I':
246                 getstring(op);
247                 getstring(count);
248                 DEBUG2("rmtd: I %s %s\n", op, count);
249                 if (atoi(op) == RMTI_VERSION) {
250                         rval = RMT_VERSION;
251                         rmt_version = 1;
252                 } 
253                 else { 
254                         struct mtop mtop;
255                         mtop.mt_op = -1;
256                         if (rmt_version) {
257                                 /* rmt version 1, assume UNIX client */
258                                 switch (atoi(op)) {
259 #ifdef  MTWEOF
260                                         case 0:
261                                                 mtop.mt_op = MTWEOF;
262                                                 break;
263 #endif
264 #ifdef  MTFSF
265                                         case 1:
266                                                 mtop.mt_op = MTFSF;
267                                                 break;
268 #endif
269 #ifdef  MTBSF
270                                         case 2:
271                                                 mtop.mt_op = MTBSF;
272                                                 break;
273 #endif
274 #ifdef  MTFSR
275                                         case 3:
276                                                 mtop.mt_op = MTFSR;
277                                                 break;
278 #endif
279 #ifdef  MTBSR
280                                         case 4:
281                                                 mtop.mt_op = MTBSR;
282                                                 break;
283 #endif
284 #ifdef  MTREW
285                                         case 5:
286                                                 mtop.mt_op = MTREW;
287                                                 break;
288 #endif
289 #ifdef  MTOFFL
290                                         case 6:
291                                                 mtop.mt_op = MTOFFL;
292                                                 break;
293 #endif
294 #ifdef  MTNOP
295                                         case 7:
296                                                 mtop.mt_op = MTNOP;
297                                                 break;
298 #endif
299                                 }
300                                 if (mtop.mt_op == -1) {
301                                         errno = EINVAL;
302                                         goto ioerror;
303                                 }
304                         }
305                         else {
306                                 /* rmt version 0, assume linux client */
307                                 mtop.mt_op = atoi(op);
308                         }
309                         mtop.mt_count = atoi(count);
310                         if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
311                                 goto ioerror;
312                         rval = mtop.mt_count;
313                 }
314                 goto respond;
315
316         case 'i':
317         {       struct mtop mtop;
318  
319                 getstring (op);
320                 getstring (count);
321                 DEBUG2 ("rmtd: i %s %s\n", op, count);
322                 switch (atoi(op)) {
323 #ifdef MTCACHE
324                         case RMTI_CACHE:
325                                 mtop.mt_op = MTCACHE;
326                                 break;
327 #endif
328 #ifdef MTNOCACHE
329                         case RMTI_NOCACHE:
330                                 mtop.mt_op = MTNOCACHE;
331                                 break;
332 #endif
333 #ifdef MTRETEN
334                         case RMTI_RETEN:
335                                 mtop.mt_op = MTRETEN;
336                                 break;
337 #endif
338 #ifdef MTERASE
339                         case RMTI_ERASE:
340                                 mtop.mt_op = MTERASE;
341                                 break;
342 #endif
343 #ifdef MTEOM
344                         case RMTI_EOM:
345                                 mtop.mt_op = MTEOM;
346                                 break;
347 #endif
348 #ifdef MTNBSF
349                         case RMTI_NBSF:
350                                 mtop.mt_op = MTNBSF;
351                                 break;
352 #endif
353                         default:
354                                 errno = EINVAL;
355                                 goto ioerror;
356                 }
357                 mtop.mt_count = atoi (count);
358                 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0)
359                         goto ioerror;
360
361                 rval = mtop.mt_count;
362
363                 goto respond;
364         }
365
366         case 'S':               /* status */
367                 DEBUG("rmtd: S\n");
368                 { struct mtget mtget;
369                   if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
370                         goto ioerror;
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));
375                   goto top;
376                 }
377
378         case 's':
379         {       char s;
380                 struct mtget mtget;
381  
382                 if (read (0, &s, 1) != 1)
383                         goto top;
384  
385                 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0)
386                         goto ioerror;
387
388                 switch (s) {
389                         case MTS_TYPE:
390                                 rval = mtget.mt_type;
391                                 break;
392                         case MTS_DSREG:
393                                 rval = mtget.mt_dsreg;
394                                 break;
395                         case MTS_ERREG:
396                                 rval = mtget.mt_erreg;
397                                 break;
398                         case MTS_RESID:
399                                 rval = mtget.mt_resid;
400                                 break;
401                         case MTS_FILENO:
402                                 rval = mtget.mt_fileno;
403                                 break;
404                         case MTS_BLKNO:
405                                 rval = mtget.mt_blkno;
406                                 break;
407                         case MTS_FLAGS:
408                                 rval = mtget.mt_gstat;
409                                 break;
410                         case MTS_BF:
411                                 rval = 0;
412                                 break;
413                         default:
414                                 errno = EINVAL;
415                                 goto ioerror;
416                 }
417
418                 goto respond;
419         }
420
421         case 'V':               /* version */
422                 getstring(op);
423                 DEBUG1("rmtd: V %s\n", op);
424                 rval = 2;
425                 goto respond;
426
427         default:
428                 DEBUG1("rmtd: garbage command %c\n", c);
429                 exit(3);
430         }
431 respond:
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));
435         goto top;
436 ioerror:
437         error(errno);
438         goto top;
439 }
440
441 void getstring(char *bp)
442 {
443         int i;
444         char *cp = bp;
445
446         for (i = 0; i < SSIZE - 1; i++) {
447                 if (read(0, cp+i, 1) != 1)
448                         exit(0);
449                 if (cp[i] == '\n')
450                         break;
451         }
452         cp[i] = '\0';
453 }
454
455 char *
456 checkbuf(char *record, int size)
457 {
458
459         if (size <= maxrecsize)
460                 return (record);
461         if (record != 0)
462                 free(record);
463         record = malloc(size);
464         if (record == 0) {
465                 DEBUG("rmtd: cannot allocate buffer space\n");
466                 exit(4);
467         }
468         maxrecsize = size;
469         while (size > 1024 &&
470                setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
471                 size -= 1024;
472         return (record);
473 }
474
475 void
476 error(int num)
477 {
478
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));
482 }