home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / src / rtapelib.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  17KB  |  601 lines

  1. /* Functions for communicating with a remote tape drive.
  2.    Copyright (C) 1988, 1992, 1994 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
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* The man page rmt(8) for /etc/rmt documents the remote mag tape
  19.    protocol which rdump and rrestore use.  Unfortunately, the man page is
  20.    *WRONG*.  The author of the routines I'm including originally wrote
  21.    his code just based on the man page, and it didn't work, so he went to
  22.    the rdump source to figure out why.  The only thing he had to change
  23.    was to check for the 'F' return code in addition to the 'E', and to
  24.    separate the various arguments with \n instead of a space.  I
  25.    personally don't think that this is much of a problem, but I wanted to
  26.    point it out. -- Arnold Robbins
  27.    
  28.    Originally written by Jeff Lee, modified some by Arnold Robbins.
  29.    Redone as a library that can replace open, read, write, etc., by Fred
  30.    Fish, with some additional work by Arnold Robbins.  Modified to make
  31.    all rmt* calls into macros for speed by Jay Fenlason.  Use
  32.    -DHAVE_NETDB_H for rexec code, courtesy of Dan Kegel.  */
  33.  
  34. #ifdef HAVE_CONFIG_H
  35. #include <config.h>
  36. #endif
  37.  
  38. #ifdef HAVE_VFORK
  39. #define fork vfork
  40. #endif
  41.  
  42. #include "system.h"
  43.  
  44. #include <signal.h>
  45.  
  46. #if defined (HAVE_NETDB_H) && defined (HAVE_REXEC)
  47. # include <netdb.h>
  48. #endif
  49.  
  50. #include <setjmp.h>
  51.  
  52. #include "rmt.h"
  53.  
  54. /* Just to shut up -Wall.  FIXME.  */
  55. int rexec ();
  56.  
  57. /* Exit status if exec errors.  */
  58. #define EXIT_ON_EXEC_ERROR 128
  59.  
  60. /* Size of buffers for reading and writing commands to rmt.  FIXME.  */
  61. #define CMDBUFSIZE 64
  62.  
  63. #ifndef RETSIGTYPE
  64. #define RETSIGTYPE void
  65. #endif
  66.  
  67. /* Maximum number of simultaneous remote tape connections.  FIXME.  */
  68. #define MAXUNIT    4
  69.  
  70. /* Return the parent's read side of remote tape connection Fd.  */
  71. #define READ(Fd) (from_remote[Fd][0])
  72.  
  73. /* Return the parent's write side of remote tape connection Fd.  */
  74. #define WRITE(Fd) (to_remote[Fd][1])
  75.  
  76. /* The pipes for receiving data from remote tape drives.  */
  77. static int from_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
  78.  
  79. /* The pipes for sending data to remote tape drives.  */
  80. static int to_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
  81.  
  82. /* Temporary variable used by macros in rmt.h.  */
  83. char *__rmt_path;
  84.  
  85.  
  86. /*-----------------------------------------------.
  87. | Close remote tape connection FILE_DESCRIPTOR.     |
  88. `-----------------------------------------------*/
  89.  
  90. static void
  91. _rmt_shutdown (int file_descriptor)
  92. {
  93.   close (READ (file_descriptor));
  94.   close (WRITE (file_descriptor));
  95.   READ (file_descriptor) = -1;
  96.   WRITE (file_descriptor) = -1;
  97. }
  98.  
  99. /*------------------------------------------------------------------------.
  100. | Attempt to perform the remote tape command specified in BUF on remote      |
  101. | tape connection FILE_DESCRIPTOR.  Return 0 if successful, -1 on error.  |
  102. `------------------------------------------------------------------------*/
  103.  
  104. static int
  105. do_command (int file_descriptor, const char *buf)
  106. {
  107.   register int buflen;
  108.   RETSIGTYPE (*pipe_handler) ();
  109.  
  110.   /* Save the current pipe handler and try to make the request.  */
  111.  
  112.   pipe_handler = signal (SIGPIPE, SIG_IGN);
  113.   buflen = strlen (buf);
  114.   if (write (WRITE (file_descriptor), buf, (size_t) buflen) == buflen)
  115.     {
  116.       signal (SIGPIPE, pipe_handler);
  117.       return 0;
  118.     }
  119.  
  120.   /* Something went wrong.  Close down and go home.  */
  121.  
  122.   signal (SIGPIPE, pipe_handler);
  123.   _rmt_shutdown (file_descriptor);
  124.   errno = EIO;
  125.   return -1;
  126. }
  127.  
  128. /*-------------------------------------------------------------------------.
  129. | Read and return the status from remote tape connection FILE_DESCRIPTOR.  |
  130. | If an error occurred, return -1 and set errno.               |
  131. `-------------------------------------------------------------------------*/
  132.  
  133. static int
  134. get_status (int file_descriptor)
  135. {
  136.   int i;
  137.   char c, *cp;
  138.   char command_buffer[CMDBUFSIZE];
  139.  
  140.   /* Read the reply command line.  */
  141.  
  142.   for (i = 0, cp = command_buffer; i < CMDBUFSIZE; i++, cp++)
  143.     {
  144.       if (read (READ (file_descriptor), cp, 1) != 1)
  145.     {
  146.       _rmt_shutdown (file_descriptor);
  147.       errno = EIO;
  148.       return -1;
  149.     }
  150.       if (*cp == '\n')
  151.     {
  152.       *cp = '\0';
  153.       break;
  154.     }
  155.     }
  156.  
  157.   if (i == CMDBUFSIZE)
  158.     {
  159.       _rmt_shutdown (file_descriptor);
  160.       errno = EIO;
  161.       return -1;
  162.     }
  163.  
  164.   /* Check the return status.  */
  165.  
  166.   for (cp = command_buffer; *cp; cp++)
  167.     if (*cp != ' ')
  168.       break;
  169.  
  170.   if (*cp == 'E' || *cp == 'F')
  171.     {
  172.       errno = atoi (cp + 1);
  173.  
  174.       /* Skip the error message line.  */
  175.  
  176.       while (read (READ (file_descriptor), &c, 1) == 1)
  177.     if (c == '\n')
  178.       break;
  179.  
  180.       if (*cp == 'F')
  181.     _rmt_shutdown (file_descriptor);
  182.  
  183.       return -1;
  184.     }
  185.  
  186.   /* Check for mis-synced pipes.  */
  187.  
  188.   if (*cp != 'A')
  189.     {
  190.       _rmt_shutdown (file_descriptor);
  191.       errno = EIO;
  192.       return -1;
  193.     }
  194.  
  195.   /* Got an `A' (success) response.  */
  196.  
  197.   return atoi (cp + 1);
  198. }
  199.  
  200. #if defined (HAVE_NETDB_H) && defined (HAVE_REXEC)
  201.  
  202. /*-------------------------------------------------------------------------.
  203. | Execute /etc/rmt as user USER on remote system HOST using rexec.  Return |
  204. | a file descriptor of a bidirectional socket for stdin and stdout.  If       |
  205. | USER is NULL, use the current username.                   |
  206. |                                        |
  207. | By default, this code is not used, since it requires that the user have  |
  208. | a .netrc file in his/her home directory, or that the application       |
  209. | designer be willing to have rexec prompt for login and password info.       |
  210. | This may be unacceptable, and .rhosts files for use with rsh are much       |
  211. | more common on BSD systems.                           |
  212. `-------------------------------------------------------------------------*/
  213.  
  214. static int
  215. _rmt_rexec (char *host, char *user)
  216. {
  217.   struct servent *rexecserv;
  218.   int save_stdin = dup (fileno (stdin));
  219.   int save_stdout = dup (fileno (stdout));
  220.   int tape_fd;            /* return value */
  221.  
  222.   /* When using cpio -o < filename, stdin is no longer the tty.  But the
  223.      rexec subroutine reads the login and the passwd on stdin, to allow
  224.      remote execution of the command.  So, reopen stdin and stdout on
  225.      /dev/tty before the rexec and give them back their original value
  226.      after.  */
  227.  
  228.   if (freopen ("/dev/tty", "r", stdin) == NULL)
  229.     freopen ("/dev/null", "r", stdin);
  230.   if (freopen ("/dev/tty", "w", stdout) == NULL)
  231.     freopen ("/dev/null", "w", stdout);
  232.  
  233.   if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv)
  234.     error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available"));
  235.  
  236.   tape_fd = rexec (&host, rexecserv->s_port, user, NULL,
  237.            "/etc/rmt", (int *) NULL);
  238.   fclose (stdin);
  239.   fdopen (save_stdin, "r");
  240.   fclose (stdout);
  241.   fdopen (save_stdout, "w");
  242.  
  243.   return tape_fd;
  244. }
  245.  
  246. #endif /* HAVE_NETDB_H && HAVE_REXEC */
  247.  
  248. /*-------------------------------------------------------------------------.
  249. | Open a file (some magnetic tape device?) on the system specified in       |
  250. | PATH, as the given user.  PATH has the form `[USER@]HOST:FILE'.  OFLAG   |
  251. | is O_RDONLY, O_WRONLY, etc.  If successful, return the remote pipe       |
  252. | number plus BIAS.  REMOTE_SHELL may be overriden.  On error, return -1.  |
  253. `-------------------------------------------------------------------------*/
  254.  
  255. int
  256. __rmt_open (const char *path, int oflag, int bias, const char *remote_shell)
  257. {
  258.   int remote_pipe_number;    /* pseudo, biased file descriptor */
  259.   char *path_copy;        /* copy of path string */
  260.   char *cursor;            /* cursor in path_copy */
  261.   char *remote_host;        /* remote host name */
  262.   char *remote_file;        /* remote file name (often a device) */
  263.   char *remote_user;        /* remote user name */
  264.   char command_buffer[CMDBUFSIZE];
  265.  
  266. #if !defined (HAVE_NETDB_H) || !defined (HAVE_REXEC)
  267.   const char *remote_shell_basename;
  268.   int status;
  269. #endif
  270.  
  271.   /* Find an unused pair of file descriptors.  */
  272.  
  273.   for (remote_pipe_number = 0;
  274.        remote_pipe_number < MAXUNIT;
  275.        remote_pipe_number++)
  276.     if (READ (remote_pipe_number) == -1
  277.     && WRITE (remote_pipe_number) == -1)
  278.       break;
  279.  
  280.   if (remote_pipe_number == MAXUNIT)
  281.     {
  282.       errno = EMFILE;
  283.       return -1;
  284.     }
  285.  
  286.   /* Pull apart the system and device, and optional user.  */
  287.  
  288.   path_copy = xstrdup (path);
  289.   remote_host = path_copy;
  290.   remote_user = NULL;
  291.   remote_file = NULL;
  292.  
  293.   for (cursor = path_copy; *cursor; cursor++)
  294.     switch (*cursor)
  295.       {
  296.       default:
  297.     break;
  298.  
  299.       case '@':
  300.     if (!remote_user)
  301.       {
  302.         remote_user = remote_host;
  303.         *cursor = '\0';
  304.         remote_host = cursor + 1;
  305.       }
  306.     break;
  307.  
  308.       case ':':
  309.     if (!remote_file)
  310.       {
  311.         *cursor = '\0';
  312.         remote_file = cursor + 1;
  313.       }
  314.     break;
  315.       }
  316.  
  317.   /* FIXME: Should somewhat validate the decoding, here.  */
  318.  
  319.   if (*remote_user == '\0')
  320.     remote_user = NULL;
  321.  
  322. #if defined (HAVE_NETDB_H) && defined (HAVE_REXEC)
  323.  
  324.   /* Execute the remote command using rexec.  */
  325.  
  326.   READ (remote_pipe_number) = _rmt_rexec (remote_host, remote_user);
  327.   if (READ (remote_pipe_number) < 0)
  328.     {
  329.       free (path_copy);
  330.       return -1;
  331.     }
  332.  
  333.   WRITE (remote_pipe_number) = READ (remote_pipe_number);
  334.  
  335. #else /* not HAVE_NETDB_H || not HAVE_REXEC */
  336.  
  337.   /* Identify the remote command to be executed.  */
  338.  
  339.   if (!remote_shell)
  340.     {
  341. #ifdef REMOTE_SHELL
  342.       remote_shell = REMOTE_SHELL;
  343. #else
  344.       errno = EIO;
  345.       free (path_copy);
  346.       return -1;
  347. #endif
  348.     }
  349.   remote_shell_basename = strrchr (remote_shell, '/');
  350.   if (remote_shell_basename)
  351.     remote_shell_basename++;
  352.   else
  353.     remote_shell_basename = remote_shell;
  354.  
  355.   /* Set up the pipes for the `rsh' command, and fork.  */
  356.  
  357.   if (pipe (to_remote[remote_pipe_number]) == -1
  358.       || pipe (from_remote[remote_pipe_number]) == -1)
  359.     {
  360.       free (path_copy);
  361.       return -1;
  362.     }
  363.  
  364.   status = fork ();
  365.   if (status == -1)
  366.     {
  367.       free (path_copy);
  368.       return -1;
  369.     }
  370.  
  371.   if (status == 0)
  372.     {
  373.  
  374.       /* Child.  */
  375.  
  376.       close (0);
  377.       dup (to_remote[remote_pipe_number][0]);
  378.       close (to_remote[remote_pipe_number][0]);
  379.       close (to_remote[remote_pipe_number][1]);
  380.  
  381.       close (1);
  382.       dup (from_remote[remote_pipe_number][1]);
  383.       close (from_remote[remote_pipe_number][0]);
  384.       close (from_remote[remote_pipe_number][1]);
  385.  
  386.       setuid (getuid ());
  387.       setgid (getgid ());
  388.  
  389.       if (remote_user)
  390.     execl (remote_shell, remote_shell_basename, remote_host,
  391.            "-l", remote_user, "/etc/rmt", (char *) 0);
  392.       else
  393.     execl (remote_shell, remote_shell_basename, remote_host,
  394.            "/etc/rmt", (char *) 0);
  395.  
  396.       /* Bad problems if we get here.  */
  397.       
  398.       /* In a previous version, _exit was used here instead of exit.  */
  399.       error (EXIT_ON_EXEC_ERROR, errno, _("Cannot execute remote shell"));
  400.     }
  401.  
  402.   /* Parent.  */
  403.  
  404.   close (from_remote[remote_pipe_number][1]);
  405.   close (to_remote[remote_pipe_number][0]);
  406.  
  407. #endif /* not HAVE_NETDB_H or not HAVE_REXEC */
  408.  
  409.   /* Attempt to open the tape device.  */
  410.  
  411.   sprintf (command_buffer, "O%s\n%d\n", remote_file, oflag);
  412.   if (do_command (remote_pipe_number, command_buffer) == -1
  413.       || get_status (remote_pipe_number) == -1)
  414.     {
  415.       free (path_copy);
  416.       return -1;
  417.     }
  418.  
  419.   free (path_copy);
  420.   return remote_pipe_number + bias;
  421. }
  422.  
  423. /*-------------------------------------------------------------------------.
  424. | Close remote tape connection FILE_DESCRIPTOR and shut down.  Return 0 if |
  425. | successful, -1 on error.                           |
  426. `-------------------------------------------------------------------------*/
  427.  
  428. int
  429. __rmt_close (int file_descriptor)
  430. {
  431.   int status;
  432.  
  433.   if (do_command (file_descriptor, "C\n") == -1)
  434.     return -1;
  435.  
  436.   status = get_status (file_descriptor);
  437.   _rmt_shutdown (file_descriptor);
  438.   return status;
  439. }
  440.  
  441. /*--------------------------------------------------------------------.
  442. | Read up to NBYTE bytes into BUF from remote tape connection          |
  443. | FILE_DESCRIPTOR.  Return the number of bytes read on success, -1 on |
  444. | error.                                  |
  445. `--------------------------------------------------------------------*/
  446.  
  447. int
  448. __rmt_read (int file_descriptor, char *buf, unsigned int nbyte)
  449. {
  450.   int status, i;
  451.   char command_buffer[CMDBUFSIZE];
  452.  
  453.   sprintf (command_buffer, "R%d\n", nbyte);
  454.   if (do_command (file_descriptor, command_buffer) == -1
  455.       || (status = get_status (file_descriptor)) == -1)
  456.     return -1;
  457.  
  458.   for (i = 0; i < status; i += nbyte, buf += nbyte)
  459.     {
  460.       nbyte = read (READ (file_descriptor), buf, (size_t) (status - i));
  461.       if (nbyte <= 0)
  462.     {
  463.       _rmt_shutdown (file_descriptor);
  464.       errno = EIO;
  465.       return -1;
  466.     }
  467.     }
  468.  
  469.   return status;
  470. }
  471.  
  472. /*-----------------------------------------------------------------------.
  473. | Write NBYTE bytes from BUF to remote tape connection FILE_DESCRIPTOR.     |
  474. | Return the number of bytes written on success, -1 on error.         |
  475. `-----------------------------------------------------------------------*/
  476.  
  477. int
  478. __rmt_write (int file_descriptor, char *buf, unsigned int nbyte)
  479. {
  480.   char command_buffer[CMDBUFSIZE];
  481.   RETSIGTYPE (*pipe_handler) ();
  482.  
  483.   sprintf (command_buffer, "W%d\n", nbyte);
  484.   if (do_command (file_descriptor, command_buffer) == -1)
  485.     return -1;
  486.  
  487.   pipe_handler = signal (SIGPIPE, SIG_IGN);
  488.   if (write (WRITE (file_descriptor), buf, nbyte) == nbyte)
  489.     {
  490.       signal (SIGPIPE, pipe_handler);
  491.       return get_status (file_descriptor);
  492.     }
  493.  
  494.   /* Write error.  */
  495.  
  496.   signal (SIGPIPE, pipe_handler);
  497.   _rmt_shutdown (file_descriptor);
  498.   errno = EIO;
  499.   return -1;
  500. }
  501.  
  502. /*---------------------------------------------------------------------.
  503. | Perform an imitation lseek operation on remote tape connection       |
  504. | FILE_DESCRIPTOR.  Return the new file offset if successful, -1 if on |
  505. | error.                                   |
  506. `---------------------------------------------------------------------*/
  507.  
  508. long
  509. __rmt_lseek (int file_descriptor, off_t offset, int whence)
  510. {
  511.   char command_buffer[CMDBUFSIZE];
  512.  
  513.   sprintf (command_buffer, "L%ld\n%d\n", offset, whence);
  514.   if (do_command (file_descriptor, command_buffer) == -1)
  515.     return -1;
  516.  
  517.   return get_status (file_descriptor);
  518. }
  519.  
  520. /*-------------------------------------------------------------------------.
  521. | Perform a raw tape operation on remote tape connection FILE_DESCRIPTOR.  |
  522. | Return the results of the ioctl, or -1 on error.               |
  523. `-------------------------------------------------------------------------*/
  524.  
  525. int
  526. __rmt_ioctl (int file_descriptor, int op, char *arg)
  527. {
  528.   char c;
  529.   int status, cnt;
  530.   char command_buffer[CMDBUFSIZE];
  531.  
  532.   switch (op)
  533.     {
  534.     default:
  535.       errno = EOPNOTSUPP;
  536.       return -1;
  537.  
  538. #ifdef MTIOCTOP
  539.  
  540.     case MTIOCTOP:
  541.  
  542.       /* MTIOCTOP is the easy one.  Nothing is transfered in binary.  */
  543.  
  544.       sprintf (command_buffer, "I%d\n%d\n", ((struct mtop *) arg)->mt_op,
  545.            ((struct mtop *) arg)->mt_count);
  546.       if (do_command (file_descriptor, command_buffer) == -1)
  547.     return -1;
  548.  
  549.       /* Return the count.  */
  550.  
  551.       return get_status (file_descriptor);
  552.  
  553. #endif /* MTIOCTOP */
  554.  
  555. #ifdef MTIOCGET
  556.  
  557.     case MTIOCGET:
  558.  
  559.       /* Grab the status and read it directly into the structure.  This
  560.      assumes that the status buffer is not padded and that 2 shorts
  561.      fit in a long without any word alignment problems; i.e., the
  562.      whole struct is contiguous.  NOTE - this is probably NOT a good
  563.      assumption.  */
  564.  
  565.       if (do_command (file_descriptor, "S") == -1
  566.       || (status = get_status (file_descriptor), status == -1))
  567.     return -1;
  568.  
  569.       for (; status > 0; status -= cnt, arg += cnt)
  570.     {
  571.       cnt = read (READ (file_descriptor), arg, (size_t) status);
  572.       if (cnt <= 0)
  573.         {
  574.           _rmt_shutdown (file_descriptor);
  575.           errno = EIO;
  576.           return -1;
  577.         }
  578.     }
  579.  
  580.       /* Check for byte position.  mt_type (or mt_model) is a small integer
  581.      field (normally) so we will check its magnitude.  If it is larger
  582.      than 256, we will assume that the bytes are swapped and go through
  583.      and reverse all the bytes.  */
  584.  
  585.       if (((struct mtget *) arg)->MTIO_CHECK_FIELD < 256)
  586.     return 0;
  587.  
  588.       for (cnt = 0; cnt < status; cnt += 2)
  589.     {
  590.       c = arg[cnt];
  591.       arg[cnt] = arg[cnt + 1];
  592.       arg[cnt + 1] = c;
  593.     }
  594.  
  595.       return 0;
  596.  
  597. #endif /* MTIOCGET */
  598.  
  599.     }
  600. }
  601.