4
0

rtapelib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* Functions for communicating with a remote tape drive.
  2. Copyright (C) 1988, 1992 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
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. /* The man page rmt(8) for /etc/rmt documents the remote mag tape
  15. protocol which rdump and rrestore use. Unfortunately, the man
  16. page is *WRONG*. The author of the routines I'm including originally
  17. wrote his code just based on the man page, and it didn't work, so he
  18. went to the rdump source to figure out why. The only thing he had to
  19. change was to check for the 'F' return code in addition to the 'E',
  20. and to separate the various arguments with \n instead of a space. I
  21. personally don't think that this is much of a problem, but I wanted to
  22. point it out. -- Arnold Robbins
  23. Originally written by Jeff Lee, modified some by Arnold Robbins.
  24. Redone as a library that can replace open, read, write, etc., by
  25. Fred Fish, with some additional work by Arnold Robbins.
  26. Modified to make all rmtXXX calls into macros for speed by Jay Fenlason.
  27. Use -DUSE_REXEC for rexec code, courtesy of Dan Kegel, srs!dan. */
  28. #include <stdio.h>
  29. #include <sys/types.h>
  30. #include <signal.h>
  31. #ifndef NO_MTIO
  32. #include <sys/ioctl.h>
  33. #include <sys/mtio.h>
  34. #endif
  35. #ifdef USE_REXEC
  36. #include <netdb.h>
  37. #endif
  38. #include <errno.h>
  39. #include <setjmp.h>
  40. #include <sys/stat.h>
  41. #ifndef errno
  42. extern int errno;
  43. #endif
  44. /* Maximum size of a fully qualified host name. */
  45. #define MAXHOSTLEN 257
  46. /* Size of buffers for reading and writing commands to rmt.
  47. (An arbitrary limit.) */
  48. #define CMDBUFSIZE 64
  49. #ifndef RETSIGTYPE
  50. #define RETSIGTYPE void
  51. #endif
  52. /* Maximum number of simultaneous remote tape connections.
  53. (Another arbitrary limit.) */
  54. #define MAXUNIT 4
  55. /* Return the parent's read side of remote tape connection FILDES. */
  56. #define READ(fildes) (from_rmt[fildes][0])
  57. /* Return the parent's write side of remote tape connection FILDES. */
  58. #define WRITE(fildes) (to_rmt[fildes][1])
  59. /* The pipes for receiving data from remote tape drives. */
  60. static int from_rmt[MAXUNIT][2] = {-1, -1, -1, -1, -1, -1, -1, -1};
  61. /* The pipes for sending data to remote tape drives. */
  62. static int to_rmt[MAXUNIT][2] = {-1, -1, -1, -1, -1, -1, -1, -1};
  63. /* Temporary variable used by macros in rmt.h. */
  64. char *__rmt_path;
  65. /* Close remote tape connection FILDES. */
  66. static void
  67. _rmt_shutdown (fildes)
  68. int fildes;
  69. {
  70. close (READ (fildes));
  71. close (WRITE (fildes));
  72. READ (fildes) = -1;
  73. WRITE (fildes) = -1;
  74. }
  75. /* Attempt to perform the remote tape command specified in BUF
  76. on remote tape connection FILDES.
  77. Return 0 if successful, -1 on error. */
  78. static int
  79. command (fildes, buf)
  80. int fildes;
  81. char *buf;
  82. {
  83. register int buflen;
  84. RETSIGTYPE (*pipe_handler) ();
  85. /* Save the current pipe handler and try to make the request. */
  86. pipe_handler = signal (SIGPIPE, SIG_IGN);
  87. buflen = strlen (buf);
  88. if (write (WRITE (fildes), buf, buflen) == buflen)
  89. {
  90. signal (SIGPIPE, pipe_handler);
  91. return 0;
  92. }
  93. /* Something went wrong. Close down and go home. */
  94. signal (SIGPIPE, pipe_handler);
  95. _rmt_shutdown (fildes);
  96. errno = EIO;
  97. return -1;
  98. }
  99. /* Read and return the status from remote tape connection FILDES.
  100. If an error occurred, return -1 and set errno. */
  101. static int
  102. status (fildes)
  103. int fildes;
  104. {
  105. int i;
  106. char c, *cp;
  107. char buffer[CMDBUFSIZE];
  108. /* Read the reply command line. */
  109. for (i = 0, cp = buffer; i < CMDBUFSIZE; i++, cp++)
  110. {
  111. if (read (READ (fildes), cp, 1) != 1)
  112. {
  113. _rmt_shutdown (fildes);
  114. errno = EIO;
  115. return -1;
  116. }
  117. if (*cp == '\n')
  118. {
  119. *cp = '\0';
  120. break;
  121. }
  122. }
  123. if (i == CMDBUFSIZE)
  124. {
  125. _rmt_shutdown (fildes);
  126. errno = EIO;
  127. return -1;
  128. }
  129. /* Check the return status. */
  130. for (cp = buffer; *cp; cp++)
  131. if (*cp != ' ')
  132. break;
  133. if (*cp == 'E' || *cp == 'F')
  134. {
  135. errno = atoi (cp + 1);
  136. /* Skip the error message line. */
  137. while (read (READ (fildes), &c, 1) == 1)
  138. if (c == '\n')
  139. break;
  140. if (*cp == 'F')
  141. _rmt_shutdown (fildes);
  142. return -1;
  143. }
  144. /* Check for mis-synced pipes. */
  145. if (*cp != 'A')
  146. {
  147. _rmt_shutdown (fildes);
  148. errno = EIO;
  149. return -1;
  150. }
  151. /* Got an `A' (success) response. */
  152. return atoi (cp + 1);
  153. }
  154. #ifdef USE_REXEC
  155. /* Execute /etc/rmt as user USER on remote system HOST using rexec.
  156. Return a file descriptor of a bidirectional socket for stdin and stdout.
  157. If USER is NULL, or an empty string, use the current username.
  158. By default, this code is not used, since it requires that
  159. the user have a .netrc file in his/her home directory, or that the
  160. application designer be willing to have rexec prompt for login and
  161. password info. This may be unacceptable, and .rhosts files for use
  162. with rsh are much more common on BSD systems. */
  163. static int
  164. _rmt_rexec (host, user)
  165. char *host;
  166. char *user;
  167. {
  168. struct servent *rexecserv;
  169. int save_stdin = dup (fileno (stdin));
  170. int save_stdout = dup (fileno (stdout));
  171. int tape_fd; /* Return value. */
  172. /* When using cpio -o < filename, stdin is no longer the tty.
  173. But the rexec subroutine reads the login and the passwd on stdin,
  174. to allow remote execution of the command.
  175. So, reopen stdin and stdout on /dev/tty before the rexec and
  176. give them back their original value after. */
  177. if (freopen ("/dev/tty", "r", stdin) == NULL)
  178. freopen ("/dev/null", "r", stdin);
  179. if (freopen ("/dev/tty", "w", stdout) == NULL)
  180. freopen ("/dev/null", "w", stdout);
  181. rexecserv = getservbyname ("exec", "tcp");
  182. if (NULL == rexecserv)
  183. {
  184. fprintf (stderr, "exec/tcp: service not available");
  185. exit (1);
  186. }
  187. if (user != NULL && *user == '\0')
  188. user = NULL;
  189. tape_fd = rexec (&host, rexecserv->s_port, user, NULL,
  190. "/etc/rmt", (int *) NULL);
  191. fclose (stdin);
  192. fdopen (save_stdin, "r");
  193. fclose (stdout);
  194. fdopen (save_stdout, "w");
  195. return tape_fd;
  196. }
  197. #endif /* USE_REXEC */
  198. /* Open a magtape device on the system specified in PATH, as the given user.
  199. PATH has the form `[user@]system:/dev/????'.
  200. If COMPAT is defined, it can also have the form `system[.user]:/dev/????'.
  201. OFLAG is O_RDONLY, O_WRONLY, etc.
  202. MODE is ignored; 0666 is always used.
  203. If successful, return the remote tape pipe number plus BIAS.
  204. On error, return -1. */
  205. int
  206. __rmt_open (path, oflag, mode, bias)
  207. char *path;
  208. int oflag;
  209. int mode;
  210. int bias;
  211. {
  212. int i, rc;
  213. char buffer[CMDBUFSIZE]; /* Command buffer. */
  214. char system[MAXHOSTLEN]; /* The remote host name. */
  215. char device[CMDBUFSIZE]; /* The remote device name. */
  216. char login[CMDBUFSIZE]; /* The remote user name. */
  217. char *sys, *dev, *user; /* For copying into the above buffers. */
  218. sys = system;
  219. dev = device;
  220. user = login;
  221. /* Find an unused pair of file descriptors. */
  222. for (i = 0; i < MAXUNIT; i++)
  223. if (READ (i) == -1 && WRITE (i) == -1)
  224. break;
  225. if (i == MAXUNIT)
  226. {
  227. errno = EMFILE;
  228. return -1;
  229. }
  230. /* Pull apart the system and device, and optional user.
  231. Don't munge the original string. */
  232. while (*path != '@'
  233. #ifdef COMPAT
  234. && *path != '.'
  235. #endif
  236. && *path != ':')
  237. {
  238. *sys++ = *path++;
  239. }
  240. *sys = '\0';
  241. path++;
  242. if (*(path - 1) == '@')
  243. {
  244. /* Saw user part of user@host. Start over. */
  245. strcpy (user, system);
  246. sys = system;
  247. while (*path != ':')
  248. {
  249. *sys++ = *path++;
  250. }
  251. *sys = '\0';
  252. path++;
  253. }
  254. #ifdef COMPAT
  255. else if (*(path - 1) == '.')
  256. {
  257. while (*path != ':')
  258. {
  259. *user++ = *path++;
  260. }
  261. *user = '\0';
  262. path++;
  263. }
  264. #endif
  265. else
  266. *user = '\0';
  267. while (*path)
  268. {
  269. *dev++ = *path++;
  270. }
  271. *dev = '\0';
  272. #ifdef USE_REXEC
  273. /* Execute the remote command using rexec. */
  274. READ (i) = WRITE (i) = _rmt_rexec (system, login);
  275. if (READ (i) < 0)
  276. return -1;
  277. #else /* !USE_REXEC */
  278. /* Set up the pipes for the `rsh' command, and fork. */
  279. if (pipe (to_rmt[i]) == -1 || pipe (from_rmt[i]) == -1)
  280. return -1;
  281. rc = fork ();
  282. if (rc == -1)
  283. return -1;
  284. if (rc == 0)
  285. {
  286. /* Child. */
  287. close (0);
  288. dup (to_rmt[i][0]);
  289. close (to_rmt[i][0]);
  290. close (to_rmt[i][1]);
  291. close (1);
  292. dup (from_rmt[i][1]);
  293. close (from_rmt[i][0]);
  294. close (from_rmt[i][1]);
  295. setuid (getuid ());
  296. setgid (getgid ());
  297. if (*login)
  298. {
  299. execl ("/usr/ucb/rsh", "rsh", system, "-l", login,
  300. "/etc/rmt", (char *) 0);
  301. execl ("/usr/bin/remsh", "remsh", system, "-l", login,
  302. "/etc/rmt", (char *) 0);
  303. execl ("/usr/bin/rsh", "rsh", system, "-l", login,
  304. "/etc/rmt", (char *) 0);
  305. execl ("/usr/bsd/rsh", "rsh", system, "-l", login,
  306. "/etc/rmt", (char *) 0);
  307. execl ("/usr/bin/nsh", "nsh", system, "-l", login,
  308. "/etc/rmt", (char *) 0);
  309. }
  310. else
  311. {
  312. execl ("/usr/ucb/rsh", "rsh", system,
  313. "/etc/rmt", (char *) 0);
  314. execl ("/usr/bin/remsh", "remsh", system,
  315. "/etc/rmt", (char *) 0);
  316. execl ("/usr/bin/rsh", "rsh", system,
  317. "/etc/rmt", (char *) 0);
  318. execl ("/usr/bsd/rsh", "rsh", system,
  319. "/etc/rmt", (char *) 0);
  320. execl ("/usr/bin/nsh", "nsh", system,
  321. "/etc/rmt", (char *) 0);
  322. }
  323. /* Bad problems if we get here. */
  324. perror ("cannot execute remote shell");
  325. _exit (1);
  326. }
  327. /* Parent. */
  328. close (to_rmt[i][0]);
  329. close (from_rmt[i][1]);
  330. #endif /* !USE_REXEC */
  331. /* Attempt to open the tape device. */
  332. sprintf (buffer, "O%s\n%d\n", device, oflag);
  333. if (command (i, buffer) == -1 || status (i) == -1)
  334. return -1;
  335. return i + bias;
  336. }
  337. /* Close remote tape connection FILDES and shut down.
  338. Return 0 if successful, -1 on error. */
  339. int
  340. __rmt_close (fildes)
  341. int fildes;
  342. {
  343. int rc;
  344. if (command (fildes, "C\n") == -1)
  345. return -1;
  346. rc = status (fildes);
  347. _rmt_shutdown (fildes);
  348. return rc;
  349. }
  350. /* Read up to NBYTE bytes into BUF from remote tape connection FILDES.
  351. Return the number of bytes read on success, -1 on error. */
  352. int
  353. __rmt_read (fildes, buf, nbyte)
  354. int fildes;
  355. char *buf;
  356. unsigned int nbyte;
  357. {
  358. int rc, i;
  359. char buffer[CMDBUFSIZE];
  360. sprintf (buffer, "R%d\n", nbyte);
  361. if (command (fildes, buffer) == -1 || (rc = status (fildes)) == -1)
  362. return -1;
  363. for (i = 0; i < rc; i += nbyte, buf += nbyte)
  364. {
  365. nbyte = read (READ (fildes), buf, rc);
  366. if (nbyte <= 0)
  367. {
  368. _rmt_shutdown (fildes);
  369. errno = EIO;
  370. return -1;
  371. }
  372. }
  373. return rc;
  374. }
  375. /* Write NBYTE bytes from BUF to remote tape connection FILDES.
  376. Return the number of bytes written on success, -1 on error. */
  377. int
  378. __rmt_write (fildes, buf, nbyte)
  379. int fildes;
  380. char *buf;
  381. unsigned int nbyte;
  382. {
  383. char buffer[CMDBUFSIZE];
  384. RETSIGTYPE (*pipe_handler) ();
  385. sprintf (buffer, "W%d\n", nbyte);
  386. if (command (fildes, buffer) == -1)
  387. return -1;
  388. pipe_handler = signal (SIGPIPE, SIG_IGN);
  389. if (write (WRITE (fildes), buf, nbyte) == nbyte)
  390. {
  391. signal (SIGPIPE, pipe_handler);
  392. return status (fildes);
  393. }
  394. /* Write error. */
  395. signal (SIGPIPE, pipe_handler);
  396. _rmt_shutdown (fildes);
  397. errno = EIO;
  398. return -1;
  399. }
  400. /* Perform an imitation lseek operation on remote tape connection FILDES.
  401. Return the new file offset if successful, -1 if on error. */
  402. long
  403. __rmt_lseek (fildes, offset, whence)
  404. int fildes;
  405. long offset;
  406. int whence;
  407. {
  408. char buffer[CMDBUFSIZE];
  409. sprintf (buffer, "L%ld\n%d\n", offset, whence);
  410. if (command (fildes, buffer) == -1)
  411. return -1;
  412. return status (fildes);
  413. }
  414. /* Perform a raw tape operation on remote tape connection FILDES.
  415. Return the results of the ioctl, or -1 on error. */
  416. #ifndef NO_MTIO
  417. __rmt_ioctl (fildes, op, arg)
  418. int fildes, op;
  419. char *arg;
  420. {
  421. char c;
  422. int rc, cnt;
  423. char buffer[CMDBUFSIZE];
  424. switch (op)
  425. {
  426. default:
  427. errno = EINVAL;
  428. return -1;
  429. case MTIOCTOP:
  430. /* MTIOCTOP is the easy one. Nothing is transfered in binary. */
  431. sprintf (buffer, "I%d\n%d\n", ((struct mtop *) arg)->mt_op,
  432. ((struct mtop *) arg)->mt_count);
  433. if (command (fildes, buffer) == -1)
  434. return -1;
  435. return status (fildes); /* Return the count. */
  436. case MTIOCGET:
  437. /* Grab the status and read it directly into the structure.
  438. This assumes that the status buffer is not padded
  439. and that 2 shorts fit in a long without any word
  440. alignment problems; i.e., the whole struct is contiguous.
  441. NOTE - this is probably NOT a good assumption. */
  442. if (command (fildes, "S") == -1 || (rc = status (fildes)) == -1)
  443. return -1;
  444. for (; rc > 0; rc -= cnt, arg += cnt)
  445. {
  446. cnt = read (READ (fildes), arg, rc);
  447. if (cnt <= 0)
  448. {
  449. _rmt_shutdown (fildes);
  450. errno = EIO;
  451. return -1;
  452. }
  453. }
  454. /* Check for byte position. mt_type is a small integer field
  455. (normally) so we will check its magnitude. If it is larger than
  456. 256, we will assume that the bytes are swapped and go through
  457. and reverse all the bytes. */
  458. if (((struct mtget *) arg)->mt_type < 256)
  459. return 0;
  460. for (cnt = 0; cnt < rc; cnt += 2)
  461. {
  462. c = arg[cnt];
  463. arg[cnt] = arg[cnt + 1];
  464. arg[cnt + 1] = c;
  465. }
  466. return 0;
  467. }
  468. }
  469. #endif /* NO_MTIO */