]> Dogcows Code - chaz/tar/blob - src/rtapelib.c
(start_header): Do not assume S_IFMT is defined.
[chaz/tar] / src / rtapelib.c
1 /* Functions for communicating with a remote tape drive.
2 Copyright (C) 1988, 1992, 1994, 1996 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18 /* The man page rmt(8) for /etc/rmt documents the remote mag tape protocol
19 which rdump and rrestore use. Unfortunately, the man page is *WRONG*.
20 The author of the routines I'm including originally wrote his code just
21 based on the man page, and it didn't work, so he went to the rdump source
22 to figure out why. The only thing he had to change was to check for the
23 'F' return code in addition to the 'E', and to separate the various
24 arguments with \n instead of a space. I personally don't think that this
25 is much of a problem, but I wanted to point it out. -- Arnold Robbins
26
27 Originally written by Jeff Lee, modified some by Arnold Robbins. Redone
28 as a library that can replace open, read, write, etc., by Fred Fish, with
29 some additional work by Arnold Robbins. Modified to make all rmt* calls
30 into macros for speed by Jay Fenlason. Use -DWITH_REXEC for rexec
31 code, courtesy of Dan Kegel. */
32
33 #include "system.h"
34
35 #include "basename.h"
36 #include "safe-read.h"
37
38 /* Try hard to get EOPNOTSUPP defined. 486/ISC has it in net/errno.h,
39 3B2/SVR3 has it in sys/inet.h. Otherwise, like on MSDOS, use EINVAL. */
40
41 #ifndef EOPNOTSUPP
42 # if HAVE_NET_ERRNO_H
43 # include <net/errno.h>
44 # endif
45 # if HAVE_SYS_INET_H
46 # include <sys/inet.h>
47 # endif
48 # ifndef EOPNOTSUPP
49 # define EOPNOTSUPP EINVAL
50 # endif
51 #endif
52
53 #include <signal.h>
54
55 #if HAVE_NETDB_H
56 # include <netdb.h>
57 #endif
58
59 #include "rmt.h"
60
61 /* FIXME: Just to shut up -Wall. */
62 int rexec ();
63
64 /* Exit status if exec errors. */
65 #define EXIT_ON_EXEC_ERROR 128
66
67 /* FIXME: Size of buffers for reading and writing commands to rmt. */
68 #define COMMAND_BUFFER_SIZE 64
69
70 #ifndef RETSIGTYPE
71 # define RETSIGTYPE void
72 #endif
73
74 /* FIXME: Maximum number of simultaneous remote tape connections. */
75 #define MAXUNIT 4
76
77 #define PREAD 0 /* read file descriptor from pipe() */
78 #define PWRITE 1 /* write file descriptor from pipe() */
79
80 /* Return the parent's read side of remote tape connection Fd. */
81 #define READ_SIDE(Fd) (from_remote[Fd][PREAD])
82
83 /* Return the parent's write side of remote tape connection Fd. */
84 #define WRITE_SIDE(Fd) (to_remote[Fd][PWRITE])
85
86 /* The pipes for receiving data from remote tape drives. */
87 static int from_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
88
89 /* The pipes for sending data to remote tape drives. */
90 static int to_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
91
92 /* Temporary variable used by macros in rmt.h. */
93 char *rmt_path__;
94 \f
95
96 /*----------------------------------------------------------------------.
97 | Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE. |
98 `----------------------------------------------------------------------*/
99
100 static void
101 _rmt_shutdown (int handle, int errno_value)
102 {
103 close (READ_SIDE (handle));
104 close (WRITE_SIDE (handle));
105 READ_SIDE (handle) = -1;
106 WRITE_SIDE (handle) = -1;
107 errno = errno_value; /* FIXME: errno should be read-only */
108 }
109
110 /*-------------------------------------------------------------------------.
111 | Attempt to perform the remote tape command specified in BUFFER on remote |
112 | tape connection HANDLE. Return 0 if successful, -1 on error. |
113 `-------------------------------------------------------------------------*/
114
115 static int
116 do_command (int handle, const char *buffer)
117 {
118 size_t length;
119 RETSIGTYPE (*pipe_handler) ();
120
121 /* Save the current pipe handler and try to make the request. */
122
123 pipe_handler = signal (SIGPIPE, SIG_IGN);
124 length = strlen (buffer);
125 if (full_write (WRITE_SIDE (handle), buffer, length) == length)
126 {
127 signal (SIGPIPE, pipe_handler);
128 return 0;
129 }
130
131 /* Something went wrong. Close down and go home. */
132
133 signal (SIGPIPE, pipe_handler);
134 _rmt_shutdown (handle, EIO);
135 return -1;
136 }
137
138 static char *
139 get_status_string (int handle, char *command_buffer)
140 {
141 char *cursor;
142 int counter;
143
144 /* Read the reply command line. */
145
146 for (counter = 0, cursor = command_buffer;
147 counter < COMMAND_BUFFER_SIZE;
148 counter++, cursor++)
149 {
150 if (safe_read (READ_SIDE (handle), cursor, 1) != 1)
151 {
152 _rmt_shutdown (handle, EIO);
153 return 0;
154 }
155 if (*cursor == '\n')
156 {
157 *cursor = '\0';
158 break;
159 }
160 }
161
162 if (counter == COMMAND_BUFFER_SIZE)
163 {
164 _rmt_shutdown (handle, EIO);
165 return 0;
166 }
167
168 /* Check the return status. */
169
170 for (cursor = command_buffer; *cursor; cursor++)
171 if (*cursor != ' ')
172 break;
173
174 if (*cursor == 'E' || *cursor == 'F')
175 {
176 errno = atoi (cursor + 1); /* FIXME: errno should be read-only */
177
178 /* Skip the error message line. */
179
180 /* FIXME: there is better to do than merely ignoring error messages
181 coming from the remote end. Translate them, too... */
182
183 {
184 char character;
185
186 while (safe_read (READ_SIDE (handle), &character, 1) == 1)
187 if (character == '\n')
188 break;
189 }
190
191 if (*cursor == 'F')
192 _rmt_shutdown (handle, errno);
193
194 return 0;
195 }
196
197 /* Check for mis-synced pipes. */
198
199 if (*cursor != 'A')
200 {
201 _rmt_shutdown (handle, EIO);
202 return 0;
203 }
204
205 /* Got an `A' (success) response. */
206
207 return cursor + 1;
208 }
209
210 /*----------------------------------------------------------------------.
211 | Read and return the status from remote tape connection HANDLE. If an |
212 | error occurred, return -1 and set errno. |
213 `----------------------------------------------------------------------*/
214
215 static long
216 get_status (int handle)
217 {
218 char command_buffer[COMMAND_BUFFER_SIZE];
219 const char *status = get_status_string (handle, command_buffer);
220 return status ? atol (status) : -1L;
221 }
222
223 static off_t
224 get_status_off (int handle)
225 {
226 char command_buffer[COMMAND_BUFFER_SIZE];
227 const char *status = get_status_string (handle, command_buffer);
228
229 if (! status)
230 return -1;
231 else
232 {
233 /* Parse status, taking care to check for overflow.
234 We can't use standard functions,
235 since off_t might be longer than long. */
236
237 off_t count = 0;
238 int negative;
239
240 for (; *status == ' ' || *status == '\t'; status++)
241 continue;
242
243 negative = *status == '-';
244 status += negative || *status == '+';
245
246 for (;;)
247 {
248 int digit = *status++ - '0';
249 if (9 < (unsigned) digit)
250 break;
251 else
252 {
253 off_t c10 = 10 * count;
254 off_t nc = negative ? c10 - digit : c10 + digit;
255 if (c10 / 10 != count || (negative ? c10 < nc : nc < c10))
256 return -1;
257 count = nc;
258 }
259 }
260
261 return count;
262 }
263 }
264
265 #if WITH_REXEC
266
267 /*-------------------------------------------------------------------------.
268 | Execute /etc/rmt as user USER on remote system HOST using rexec. Return |
269 | a file descriptor of a bidirectional socket for stdin and stdout. If |
270 | USER is NULL, use the current username. |
271 | |
272 | By default, this code is not used, since it requires that the user have |
273 | a .netrc file in his/her home directory, or that the application |
274 | designer be willing to have rexec prompt for login and password info. |
275 | This may be unacceptable, and .rhosts files for use with rsh are much |
276 | more common on BSD systems. |
277 `-------------------------------------------------------------------------*/
278
279 static int
280 _rmt_rexec (char *host, char *user)
281 {
282 int saved_stdin = dup (STDIN_FILENO);
283 int saved_stdout = dup (STDOUT_FILENO);
284 struct servent *rexecserv;
285 int result;
286
287 /* When using cpio -o < filename, stdin is no longer the tty. But the
288 rexec subroutine reads the login and the passwd on stdin, to allow
289 remote execution of the command. So, reopen stdin and stdout on
290 /dev/tty before the rexec and give them back their original value
291 after. */
292
293 if (freopen ("/dev/tty", "r", stdin) == NULL)
294 freopen ("/dev/null", "r", stdin);
295 if (freopen ("/dev/tty", "w", stdout) == NULL)
296 freopen ("/dev/null", "w", stdout);
297
298 if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv)
299 error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available"));
300
301 result = rexec (&host, rexecserv->s_port, user, NULL,
302 "/etc/rmt", (int *) NULL);
303 if (fclose (stdin) == EOF)
304 error (0, errno, _("stdin"));
305 fdopen (saved_stdin, "r");
306 if (fclose (stdout) == EOF)
307 error (0, errno, _("stdout"));
308 fdopen (saved_stdout, "w");
309
310 return result;
311 }
312
313 #endif /* WITH_REXEC */
314
315 /*------------------------------------------------------------------------.
316 | Open a file (a magnetic tape device?) on the system specified in PATH, |
317 | as the given user. PATH has the form `[USER@]HOST:FILE'. OPEN_MODE is |
318 | O_RDONLY, O_WRONLY, etc. If successful, return the remote pipe number |
319 | plus BIAS. REMOTE_SHELL may be overriden. On error, return -1. |
320 `------------------------------------------------------------------------*/
321
322 int
323 rmt_open__ (const char *path, int open_mode, int bias, const char *remote_shell)
324 {
325 int remote_pipe_number; /* pseudo, biased file descriptor */
326 char *path_copy ; /* copy of path string */
327 char *remote_host; /* remote host name */
328 char *remote_file; /* remote file name (often a device) */
329 char *remote_user; /* remote user name */
330
331 /* Find an unused pair of file descriptors. */
332
333 for (remote_pipe_number = 0;
334 remote_pipe_number < MAXUNIT;
335 remote_pipe_number++)
336 if (READ_SIDE (remote_pipe_number) == -1
337 && WRITE_SIDE (remote_pipe_number) == -1)
338 break;
339
340 if (remote_pipe_number == MAXUNIT)
341 {
342 errno = EMFILE; /* FIXME: errno should be read-only */
343 return -1;
344 }
345
346 /* Pull apart the system and device, and optional user. */
347
348 {
349 char *cursor;
350
351 path_copy = xstrdup (path);
352 remote_host = path_copy;
353 remote_user = NULL;
354 remote_file = NULL;
355
356 for (cursor = path_copy; *cursor; cursor++)
357 switch (*cursor)
358 {
359 default:
360 break;
361
362 case '@':
363 if (!remote_user)
364 {
365 remote_user = remote_host;
366 *cursor = '\0';
367 remote_host = cursor + 1;
368 }
369 break;
370
371 case ':':
372 if (!remote_file)
373 {
374 *cursor = '\0';
375 remote_file = cursor + 1;
376 }
377 break;
378 }
379 }
380
381 /* FIXME: Should somewhat validate the decoding, here. */
382
383 if (remote_user && *remote_user == '\0')
384 remote_user = NULL;
385
386 #if WITH_REXEC
387
388 /* Execute the remote command using rexec. */
389
390 READ_SIDE (remote_pipe_number) = _rmt_rexec (remote_host, remote_user);
391 if (READ_SIDE (remote_pipe_number) < 0)
392 {
393 free (path_copy);
394 return -1;
395 }
396
397 WRITE_SIDE (remote_pipe_number) = READ_SIDE (remote_pipe_number);
398
399 #else /* not WITH_REXEC */
400 {
401 const char *remote_shell_basename;
402 pid_t status;
403
404 /* Identify the remote command to be executed. */
405
406 if (!remote_shell)
407 {
408 #ifdef REMOTE_SHELL
409 remote_shell = REMOTE_SHELL;
410 #else
411 errno = EIO; /* FIXME: errno should be read-only */
412 free (path_copy);
413 return -1;
414 #endif
415 }
416 remote_shell_basename = base_name (remote_shell);
417 if (remote_shell_basename)
418 remote_shell_basename++;
419 else
420 remote_shell_basename = remote_shell;
421
422 /* Set up the pipes for the `rsh' command, and fork. */
423
424 if (pipe (to_remote[remote_pipe_number]) == -1
425 || pipe (from_remote[remote_pipe_number]) == -1)
426 {
427 free (path_copy);
428 return -1;
429 }
430
431 status = fork ();
432 if (status == -1)
433 {
434 free (path_copy);
435 return -1;
436 }
437
438 if (status == 0)
439 {
440 /* Child. */
441
442 close (STDIN_FILENO);
443 dup (to_remote[remote_pipe_number][PREAD]);
444 close (to_remote[remote_pipe_number][PREAD]);
445 close (to_remote[remote_pipe_number][PWRITE]);
446
447 close (STDOUT_FILENO);
448 dup (from_remote[remote_pipe_number][PWRITE]);
449 close (from_remote[remote_pipe_number][PREAD]);
450 close (from_remote[remote_pipe_number][PWRITE]);
451
452 #if !MSDOS
453 setuid (getuid ());
454 setgid (getgid ());
455 #endif
456
457 if (remote_user)
458 execl (remote_shell, remote_shell_basename, remote_host,
459 "-l", remote_user, "/etc/rmt", (char *) 0);
460 else
461 execl (remote_shell, remote_shell_basename, remote_host,
462 "/etc/rmt", (char *) 0);
463
464 /* Bad problems if we get here. */
465
466 /* In a previous version, _exit was used here instead of exit. */
467 error (EXIT_ON_EXEC_ERROR, errno, _("Cannot execute remote shell"));
468 }
469
470 /* Parent. */
471
472 close (from_remote[remote_pipe_number][PWRITE]);
473 close (to_remote[remote_pipe_number][PREAD]);
474 }
475 #endif /* not WITH_REXEC */
476
477 /* Attempt to open the tape device. */
478
479 {
480 char command_buffer[COMMAND_BUFFER_SIZE];
481
482 sprintf (command_buffer, "O%s\n%d\n", remote_file, open_mode);
483 if (do_command (remote_pipe_number, command_buffer) == -1
484 || get_status (remote_pipe_number) == -1)
485 {
486 _rmt_shutdown (remote_pipe_number, errno);
487 free (path_copy);
488 return -1;
489 }
490 }
491
492 free (path_copy);
493 return remote_pipe_number + bias;
494 }
495
496 /*----------------------------------------------------------------.
497 | Close remote tape connection HANDLE and shut down. Return 0 if |
498 | successful, -1 on error. |
499 `----------------------------------------------------------------*/
500
501 int
502 rmt_close__ (int handle)
503 {
504 int status;
505
506 if (do_command (handle, "C\n") == -1)
507 return -1;
508
509 status = get_status (handle);
510 _rmt_shutdown (handle, errno);
511 return status;
512 }
513
514 /*-------------------------------------------------------------------------.
515 | Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE. |
516 | Return the number of bytes read on success, -1 on error. |
517 `-------------------------------------------------------------------------*/
518
519 ssize_t
520 rmt_read__ (int handle, char *buffer, size_t length)
521 {
522 char command_buffer[COMMAND_BUFFER_SIZE];
523 ssize_t status, rlen;
524 size_t counter;
525
526 sprintf (command_buffer, "R%lu\n", (unsigned long) length);
527 if (do_command (handle, command_buffer) == -1
528 || (status = get_status (handle)) == -1)
529 return -1;
530
531 for (counter = 0; counter < status; counter += rlen, buffer += rlen)
532 {
533 rlen = safe_read (READ_SIDE (handle), buffer, status - counter);
534 if (rlen <= 0)
535 {
536 _rmt_shutdown (handle, EIO);
537 return -1;
538 }
539 }
540
541 return status;
542 }
543
544 /*-------------------------------------------------------------------------.
545 | Write LENGTH bytes from BUFFER to remote tape connection HANDLE. Return |
546 | the number of bytes written on success, -1 on error. |
547 `-------------------------------------------------------------------------*/
548
549 ssize_t
550 rmt_write__ (int handle, char *buffer, size_t length)
551 {
552 char command_buffer[COMMAND_BUFFER_SIZE];
553 RETSIGTYPE (*pipe_handler) ();
554
555 sprintf (command_buffer, "W%lu\n", (unsigned long) length);
556 if (do_command (handle, command_buffer) == -1)
557 return -1;
558
559 pipe_handler = signal (SIGPIPE, SIG_IGN);
560 if (full_write (WRITE_SIDE (handle), buffer, length) == length)
561 {
562 signal (SIGPIPE, pipe_handler);
563 return get_status (handle);
564 }
565
566 /* Write error. */
567
568 signal (SIGPIPE, pipe_handler);
569 _rmt_shutdown (handle, EIO);
570 return -1;
571 }
572
573 /*------------------------------------------------------------------------.
574 | Perform an imitation lseek operation on remote tape connection HANDLE. |
575 | Return the new file offset if successful, -1 if on error. |
576 `------------------------------------------------------------------------*/
577
578 off_t
579 rmt_lseek__ (int handle, off_t offset, int whence)
580 {
581 char command_buffer[COMMAND_BUFFER_SIZE];
582 char operand_buffer[UINTMAX_STRSIZE_BOUND];
583 uintmax_t u = offset < 0 ? - (uintmax_t) offset : (uintmax_t) offset;
584 char *p = operand_buffer + sizeof operand_buffer;
585
586 do
587 *--p = '0' + (int) (u % 10);
588 while ((u /= 10) != 0);
589 if (offset < 0)
590 *--p = '-';
591
592 switch (whence)
593 {
594 case SEEK_SET: whence = 0; break;
595 case SEEK_CUR: whence = 1; break;
596 case SEEK_END: whence = 2; break;
597 default: abort ();
598 }
599
600 sprintf (command_buffer, "L%s\n%d\n", p, whence);
601
602 if (do_command (handle, command_buffer) == -1)
603 return -1;
604
605 return get_status_off (handle);
606 }
607
608 /*-----------------------------------------------------------------------.
609 | Perform a raw tape operation on remote tape connection HANDLE. Return |
610 | the results of the ioctl, or -1 on error. |
611 `-----------------------------------------------------------------------*/
612
613 int
614 rmt_ioctl__ (int handle, int operation, char *argument)
615 {
616 switch (operation)
617 {
618 default:
619 errno = EOPNOTSUPP; /* FIXME: errno should be read-only */
620 return -1;
621
622 #ifdef MTIOCTOP
623 case MTIOCTOP:
624 {
625 char command_buffer[COMMAND_BUFFER_SIZE];
626 char operand_buffer[UINTMAX_STRSIZE_BOUND];
627 uintmax_t u = (((struct mtop *) argument)->mt_count < 0
628 ? - (uintmax_t) ((struct mtop *) argument)->mt_count
629 : (uintmax_t) ((struct mtop *) argument)->mt_count);
630 char *p = operand_buffer + sizeof operand_buffer;
631
632 do
633 *--p = '0' + (int) (u % 10);
634 while ((u /= 10) != 0);
635 if (((struct mtop *) argument)->mt_count < 0)
636 *--p = '-';
637
638 /* MTIOCTOP is the easy one. Nothing is transfered in binary. */
639
640 sprintf (command_buffer, "I%d\n%s\n",
641 ((struct mtop *) argument)->mt_op, p);
642 if (do_command (handle, command_buffer) == -1)
643 return -1;
644
645 return get_status (handle);
646 }
647 #endif /* MTIOCTOP */
648
649 #ifdef MTIOCGET
650 case MTIOCGET:
651 {
652 ssize_t status;
653 ssize_t counter;
654
655 /* Grab the status and read it directly into the structure. This
656 assumes that the status buffer is not padded and that 2 shorts
657 fit in a long without any word alignment problems; i.e., the
658 whole struct is contiguous. NOTE - this is probably NOT a good
659 assumption. */
660
661 if (do_command (handle, "S") == -1
662 || (status = get_status (handle), status == -1))
663 return -1;
664
665 for (; status > 0; status -= counter, argument += counter)
666 {
667 counter = safe_read (READ_SIDE (handle),
668 argument, (size_t) status);
669 if (counter <= 0)
670 {
671 _rmt_shutdown (handle, EIO);
672 return -1;
673 }
674 }
675
676 /* Check for byte position. mt_type (or mt_model) is a small integer
677 field (normally) so we will check its magnitude. If it is larger
678 than 256, we will assume that the bytes are swapped and go through
679 and reverse all the bytes. */
680
681 if (((struct mtget *) argument)->MTIO_CHECK_FIELD < 256)
682 return 0;
683
684 for (counter = 0; counter < status; counter += 2)
685 {
686 char copy = argument[counter];
687
688 argument[counter] = argument[counter + 1];
689 argument[counter + 1] = copy;
690 }
691
692 return 0;
693 }
694 #endif /* MTIOCGET */
695
696 }
697 }
This page took 0.061545 seconds and 4 git commands to generate.