rtapelib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* Functions for communicating with a remote tape drive.
  2. Copyright (C) 1988, 1992, 1994, 1996 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* The man page rmt(8) for /etc/rmt documents the remote mag tape protocol
  15. which rdump and rrestore use. Unfortunately, the man page is *WRONG*.
  16. The author of the routines I'm including originally wrote his code just
  17. based on the man page, and it didn't work, so he went to the rdump source
  18. to figure out why. The only thing he had to change was to check for the
  19. 'F' return code in addition to the 'E', and to separate the various
  20. arguments with \n instead of a space. I personally don't think that this
  21. is much of a problem, but I wanted to point it out. -- Arnold Robbins
  22. Originally written by Jeff Lee, modified some by Arnold Robbins. Redone
  23. as a library that can replace open, read, write, etc., by Fred Fish, with
  24. some additional work by Arnold Robbins. Modified to make all rmt* calls
  25. into macros for speed by Jay Fenlason. Use -DHAVE_NETDB_H for rexec
  26. code, courtesy of Dan Kegel. */
  27. #include "system.h"
  28. /* Try hard to get EOPNOTSUPP defined. 486/ISC has it in net/errno.h,
  29. 3B2/SVR3 has it in sys/inet.h. Otherwise, like on MSDOS, use EINVAL. */
  30. #ifndef EOPNOTSUPP
  31. # if HAVE_NET_ERRNO_H
  32. # include <net/errno.h>
  33. # endif
  34. # if HAVE_SYS_INET_H
  35. # include <sys/inet.h>
  36. # endif
  37. # ifndef EOPNOTSUPP
  38. # define EOPNOTSUPP EINVAL
  39. # endif
  40. #endif
  41. #include <signal.h>
  42. #if HAVE_NETDB_H
  43. # include <netdb.h>
  44. #endif
  45. #include "rmt.h"
  46. /* FIXME: Just to shut up -Wall. */
  47. int rexec ();
  48. /* Exit status if exec errors. */
  49. #define EXIT_ON_EXEC_ERROR 128
  50. /* FIXME: Size of buffers for reading and writing commands to rmt. */
  51. #define COMMAND_BUFFER_SIZE 64
  52. #ifndef RETSIGTYPE
  53. # define RETSIGTYPE void
  54. #endif
  55. /* FIXME: Maximum number of simultaneous remote tape connections. */
  56. #define MAXUNIT 4
  57. #define PREAD 0 /* read file descriptor from pipe() */
  58. #define PWRITE 1 /* write file descriptor from pipe() */
  59. /* Return the parent's read side of remote tape connection Fd. */
  60. #define READ_SIDE(Fd) (from_remote[Fd][PREAD])
  61. /* Return the parent's write side of remote tape connection Fd. */
  62. #define WRITE_SIDE(Fd) (to_remote[Fd][PWRITE])
  63. /* The pipes for receiving data from remote tape drives. */
  64. static int from_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
  65. /* The pipes for sending data to remote tape drives. */
  66. static int to_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
  67. /* Temporary variable used by macros in rmt.h. */
  68. char *rmt_path__;
  69. /*----------------------------------------------------------------------.
  70. | Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE. |
  71. `----------------------------------------------------------------------*/
  72. static void
  73. _rmt_shutdown (int handle, int errno_value)
  74. {
  75. close (READ_SIDE (handle));
  76. close (WRITE_SIDE (handle));
  77. READ_SIDE (handle) = -1;
  78. WRITE_SIDE (handle) = -1;
  79. errno = errno_value; /* FIXME: errno should be read-only */
  80. }
  81. /*-------------------------------------------------------------------------.
  82. | Attempt to perform the remote tape command specified in BUFFER on remote |
  83. | tape connection HANDLE. Return 0 if successful, -1 on error. |
  84. `-------------------------------------------------------------------------*/
  85. static int
  86. do_command (int handle, const char *buffer)
  87. {
  88. int length;
  89. RETSIGTYPE (*pipe_handler) ();
  90. /* Save the current pipe handler and try to make the request. */
  91. pipe_handler = signal (SIGPIPE, SIG_IGN);
  92. length = strlen (buffer);
  93. if (write (WRITE_SIDE (handle), buffer, (size_t) length) == length)
  94. {
  95. signal (SIGPIPE, pipe_handler);
  96. return 0;
  97. }
  98. /* Something went wrong. Close down and go home. */
  99. signal (SIGPIPE, pipe_handler);
  100. _rmt_shutdown (handle, EIO);
  101. return -1;
  102. }
  103. /*----------------------------------------------------------------------.
  104. | Read and return the status from remote tape connection HANDLE. If an |
  105. | error occurred, return -1 and set errno. |
  106. `----------------------------------------------------------------------*/
  107. static int
  108. get_status (int handle)
  109. {
  110. char command_buffer[COMMAND_BUFFER_SIZE];
  111. char *cursor;
  112. int counter;
  113. /* Read the reply command line. */
  114. for (counter = 0, cursor = command_buffer;
  115. counter < COMMAND_BUFFER_SIZE;
  116. counter++, cursor++)
  117. {
  118. if (read (READ_SIDE (handle), cursor, 1) != 1)
  119. {
  120. _rmt_shutdown (handle, EIO);
  121. return -1;
  122. }
  123. if (*cursor == '\n')
  124. {
  125. *cursor = '\0';
  126. break;
  127. }
  128. }
  129. if (counter == COMMAND_BUFFER_SIZE)
  130. {
  131. _rmt_shutdown (handle, EIO);
  132. return -1;
  133. }
  134. /* Check the return status. */
  135. for (cursor = command_buffer; *cursor; cursor++)
  136. if (*cursor != ' ')
  137. break;
  138. if (*cursor == 'E' || *cursor == 'F')
  139. {
  140. errno = atoi (cursor + 1); /* FIXME: errno should be read-only */
  141. /* Skip the error message line. */
  142. /* FIXME: there is better to do than merely ignoring error messages
  143. coming from the remote end. Translate them, too... */
  144. {
  145. char character;
  146. while (read (READ_SIDE (handle), &character, 1) == 1)
  147. if (character == '\n')
  148. break;
  149. }
  150. if (*cursor == 'F')
  151. _rmt_shutdown (handle, errno);
  152. return -1;
  153. }
  154. /* Check for mis-synced pipes. */
  155. if (*cursor != 'A')
  156. {
  157. _rmt_shutdown (handle, EIO);
  158. return -1;
  159. }
  160. /* Got an `A' (success) response. */
  161. return atoi (cursor + 1);
  162. }
  163. #if HAVE_NETDB_H
  164. /*-------------------------------------------------------------------------.
  165. | Execute /etc/rmt as user USER on remote system HOST using rexec. Return |
  166. | a file descriptor of a bidirectional socket for stdin and stdout. If |
  167. | USER is NULL, use the current username. |
  168. | |
  169. | By default, this code is not used, since it requires that the user have |
  170. | a .netrc file in his/her home directory, or that the application |
  171. | designer be willing to have rexec prompt for login and password info. |
  172. | This may be unacceptable, and .rhosts files for use with rsh are much |
  173. | more common on BSD systems. |
  174. `-------------------------------------------------------------------------*/
  175. static int
  176. _rmt_rexec (char *host, char *user)
  177. {
  178. int saved_stdin = dup (fileno (stdin));
  179. int saved_stdout = dup (fileno (stdout));
  180. struct servent *rexecserv;
  181. int result;
  182. /* When using cpio -o < filename, stdin is no longer the tty. But the
  183. rexec subroutine reads the login and the passwd on stdin, to allow
  184. remote execution of the command. So, reopen stdin and stdout on
  185. /dev/tty before the rexec and give them back their original value
  186. after. */
  187. if (freopen ("/dev/tty", "r", stdin) == NULL)
  188. freopen ("/dev/null", "r", stdin);
  189. if (freopen ("/dev/tty", "w", stdout) == NULL)
  190. freopen ("/dev/null", "w", stdout);
  191. if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv)
  192. error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available"));
  193. result = rexec (&host, rexecserv->s_port, user, NULL,
  194. "/etc/rmt", (int *) NULL);
  195. if (fclose (stdin) == EOF)
  196. error (0, errno, _("stdin"));
  197. fdopen (saved_stdin, "r");
  198. if (fclose (stdout) == EOF)
  199. error (0, errno, _("stdout"));
  200. fdopen (saved_stdout, "w");
  201. return result;
  202. }
  203. #endif /* HAVE_NETDB_H */
  204. /*------------------------------------------------------------------------.
  205. | Open a file (a magnetic tape device?) on the system specified in PATH, |
  206. | as the given user. PATH has the form `[USER@]HOST:FILE'. OPEN_MODE is |
  207. | O_RDONLY, O_WRONLY, etc. If successful, return the remote pipe number |
  208. | plus BIAS. REMOTE_SHELL may be overriden. On error, return -1. |
  209. `------------------------------------------------------------------------*/
  210. int
  211. rmt_open__ (const char *path, int open_mode, int bias, const char *remote_shell)
  212. {
  213. int remote_pipe_number; /* pseudo, biased file descriptor */
  214. char *path_copy ; /* copy of path string */
  215. char *remote_host; /* remote host name */
  216. char *remote_file; /* remote file name (often a device) */
  217. char *remote_user; /* remote user name */
  218. /* Find an unused pair of file descriptors. */
  219. for (remote_pipe_number = 0;
  220. remote_pipe_number < MAXUNIT;
  221. remote_pipe_number++)
  222. if (READ_SIDE (remote_pipe_number) == -1
  223. && WRITE_SIDE (remote_pipe_number) == -1)
  224. break;
  225. if (remote_pipe_number == MAXUNIT)
  226. {
  227. errno = EMFILE; /* FIXME: errno should be read-only */
  228. return -1;
  229. }
  230. /* Pull apart the system and device, and optional user. */
  231. {
  232. char *cursor;
  233. path_copy = xstrdup (path);
  234. remote_host = path_copy;
  235. remote_user = NULL;
  236. remote_file = NULL;
  237. for (cursor = path_copy; *cursor; cursor++)
  238. switch (*cursor)
  239. {
  240. default:
  241. break;
  242. case '@':
  243. if (!remote_user)
  244. {
  245. remote_user = remote_host;
  246. *cursor = '\0';
  247. remote_host = cursor + 1;
  248. }
  249. break;
  250. case ':':
  251. if (!remote_file)
  252. {
  253. *cursor = '\0';
  254. remote_file = cursor + 1;
  255. }
  256. break;
  257. }
  258. }
  259. /* FIXME: Should somewhat validate the decoding, here. */
  260. if (remote_user && *remote_user == '\0')
  261. remote_user = NULL;
  262. #if HAVE_NETDB_H
  263. /* Execute the remote command using rexec. */
  264. READ_SIDE (remote_pipe_number) = _rmt_rexec (remote_host, remote_user);
  265. if (READ_SIDE (remote_pipe_number) < 0)
  266. {
  267. free (path_copy);
  268. return -1;
  269. }
  270. WRITE_SIDE (remote_pipe_number) = READ_SIDE (remote_pipe_number);
  271. #else /* not HAVE_NETDB_H */
  272. {
  273. const char *remote_shell_basename;
  274. int status;
  275. /* Identify the remote command to be executed. */
  276. if (!remote_shell)
  277. {
  278. #ifdef REMOTE_SHELL
  279. remote_shell = REMOTE_SHELL;
  280. #else
  281. errno = EIO; /* FIXME: errno should be read-only */
  282. free (path_copy);
  283. return -1;
  284. #endif
  285. }
  286. remote_shell_basename = strrchr (remote_shell, '/');
  287. if (remote_shell_basename)
  288. remote_shell_basename++;
  289. else
  290. remote_shell_basename = remote_shell;
  291. /* Set up the pipes for the `rsh' command, and fork. */
  292. if (pipe (to_remote[remote_pipe_number]) == -1
  293. || pipe (from_remote[remote_pipe_number]) == -1)
  294. {
  295. free (path_copy);
  296. return -1;
  297. }
  298. status = fork ();
  299. if (status == -1)
  300. {
  301. free (path_copy);
  302. return -1;
  303. }
  304. if (status == 0)
  305. {
  306. /* Child. */
  307. close (0);
  308. dup (to_remote[remote_pipe_number][PREAD]);
  309. close (to_remote[remote_pipe_number][PREAD]);
  310. close (to_remote[remote_pipe_number][PWRITE]);
  311. close (1);
  312. dup (from_remote[remote_pipe_number][PWRITE]);
  313. close (from_remote[remote_pipe_number][PREAD]);
  314. close (from_remote[remote_pipe_number][PWRITE]);
  315. #if !MSDOS
  316. setuid (getuid ());
  317. setgid (getgid ());
  318. #endif
  319. if (remote_user)
  320. execl (remote_shell, remote_shell_basename, remote_host,
  321. "-l", remote_user, "/etc/rmt", (char *) 0);
  322. else
  323. execl (remote_shell, remote_shell_basename, remote_host,
  324. "/etc/rmt", (char *) 0);
  325. /* Bad problems if we get here. */
  326. /* In a previous version, _exit was used here instead of exit. */
  327. error (EXIT_ON_EXEC_ERROR, errno, _("Cannot execute remote shell"));
  328. }
  329. /* Parent. */
  330. close (from_remote[remote_pipe_number][PWRITE]);
  331. close (to_remote[remote_pipe_number][PREAD]);
  332. }
  333. #endif /* not HAVE_NETDB_H */
  334. /* Attempt to open the tape device. */
  335. {
  336. char command_buffer[COMMAND_BUFFER_SIZE];
  337. sprintf (command_buffer, "O%s\n%d\n", remote_file, open_mode);
  338. if (do_command (remote_pipe_number, command_buffer) == -1
  339. || get_status (remote_pipe_number) == -1)
  340. {
  341. _rmt_shutdown (remote_pipe_number, errno);
  342. free (path_copy);
  343. return -1;
  344. }
  345. }
  346. free (path_copy);
  347. return remote_pipe_number + bias;
  348. }
  349. /*----------------------------------------------------------------.
  350. | Close remote tape connection HANDLE and shut down. Return 0 if |
  351. | successful, -1 on error. |
  352. `----------------------------------------------------------------*/
  353. int
  354. rmt_close__ (int handle)
  355. {
  356. int status;
  357. if (do_command (handle, "C\n") == -1)
  358. return -1;
  359. status = get_status (handle);
  360. _rmt_shutdown (handle, errno);
  361. return status;
  362. }
  363. /*-------------------------------------------------------------------------.
  364. | Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE. |
  365. | Return the number of bytes read on success, -1 on error. |
  366. `-------------------------------------------------------------------------*/
  367. int
  368. rmt_read__ (int handle, char *buffer, unsigned int length)
  369. {
  370. char command_buffer[COMMAND_BUFFER_SIZE];
  371. int status;
  372. int counter;
  373. sprintf (command_buffer, "R%d\n", length);
  374. if (do_command (handle, command_buffer) == -1
  375. || (status = get_status (handle)) == -1)
  376. return -1;
  377. for (counter = 0; counter < status; counter += length, buffer += length)
  378. {
  379. length = read (READ_SIDE (handle), buffer, (size_t) (status - counter));
  380. if (length <= 0)
  381. {
  382. _rmt_shutdown (handle, EIO);
  383. return -1;
  384. }
  385. }
  386. return status;
  387. }
  388. /*-------------------------------------------------------------------------.
  389. | Write LENGTH bytes from BUFFER to remote tape connection HANDLE. Return |
  390. | the number of bytes written on success, -1 on error. |
  391. `-------------------------------------------------------------------------*/
  392. int
  393. rmt_write__ (int handle, char *buffer, unsigned int length)
  394. {
  395. char command_buffer[COMMAND_BUFFER_SIZE];
  396. RETSIGTYPE (*pipe_handler) ();
  397. sprintf (command_buffer, "W%d\n", length);
  398. if (do_command (handle, command_buffer) == -1)
  399. return -1;
  400. pipe_handler = signal (SIGPIPE, SIG_IGN);
  401. if (write (WRITE_SIDE (handle), buffer, length) == length)
  402. {
  403. signal (SIGPIPE, pipe_handler);
  404. return get_status (handle);
  405. }
  406. /* Write error. */
  407. signal (SIGPIPE, pipe_handler);
  408. _rmt_shutdown (handle, EIO);
  409. return -1;
  410. }
  411. /*------------------------------------------------------------------------.
  412. | Perform an imitation lseek operation on remote tape connection HANDLE. |
  413. | Return the new file offset if successful, -1 if on error. |
  414. `------------------------------------------------------------------------*/
  415. long
  416. rmt_lseek__ (int handle, off_t offset, int whence)
  417. {
  418. char command_buffer[COMMAND_BUFFER_SIZE];
  419. sprintf (command_buffer, "L%ld\n%d\n", offset, whence);
  420. if (do_command (handle, command_buffer) == -1)
  421. return -1;
  422. return get_status (handle);
  423. }
  424. /*-----------------------------------------------------------------------.
  425. | Perform a raw tape operation on remote tape connection HANDLE. Return |
  426. | the results of the ioctl, or -1 on error. |
  427. `-----------------------------------------------------------------------*/
  428. int
  429. rmt_ioctl__ (int handle, int operation, char *argument)
  430. {
  431. switch (operation)
  432. {
  433. default:
  434. errno = EOPNOTSUPP; /* FIXME: errno should be read-only */
  435. return -1;
  436. #ifdef MTIOCTOP
  437. case MTIOCTOP:
  438. {
  439. char command_buffer[COMMAND_BUFFER_SIZE];
  440. /* MTIOCTOP is the easy one. Nothing is transfered in binary. */
  441. sprintf (command_buffer, "I%d\n%d\n", ((struct mtop *) argument)->mt_op,
  442. ((struct mtop *) argument)->mt_count);
  443. if (do_command (handle, command_buffer) == -1)
  444. return -1;
  445. /* Return the count. */
  446. return get_status (handle);
  447. }
  448. #endif /* MTIOCTOP */
  449. #ifdef MTIOCGET
  450. case MTIOCGET:
  451. {
  452. int status;
  453. int counter;
  454. /* Grab the status and read it directly into the structure. This
  455. assumes that the status buffer is not padded and that 2 shorts
  456. fit in a long without any word alignment problems; i.e., the
  457. whole struct is contiguous. NOTE - this is probably NOT a good
  458. assumption. */
  459. if (do_command (handle, "S") == -1
  460. || (status = get_status (handle), status == -1))
  461. return -1;
  462. for (; status > 0; status -= counter, argument += counter)
  463. {
  464. counter = read (READ_SIDE (handle), argument, (size_t) status);
  465. if (counter <= 0)
  466. {
  467. _rmt_shutdown (handle, EIO);
  468. return -1;
  469. }
  470. }
  471. /* Check for byte position. mt_type (or mt_model) is a small integer
  472. field (normally) so we will check its magnitude. If it is larger
  473. than 256, we will assume that the bytes are swapped and go through
  474. and reverse all the bytes. */
  475. if (((struct mtget *) argument)->MTIO_CHECK_FIELD < 256)
  476. return 0;
  477. for (counter = 0; counter < status; counter += 2)
  478. {
  479. char copy = argument[counter];
  480. argument[counter] = argument[counter + 1];
  481. argument[counter + 1] = copy;
  482. }
  483. return 0;
  484. }
  485. #endif /* MTIOCGET */
  486. }
  487. }