rmt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* Remote connection server.
  2. Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2003, 2004
  3. Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Copyright (C) 1983 Regents of the University of California.
  16. All rights reserved.
  17. Redistribution and use in source and binary forms are permitted provided
  18. that the above copyright notice and this paragraph are duplicated in all
  19. such forms and that any documentation, advertising materials, and other
  20. materials related to such distribution and use acknowledge that the
  21. software was developed by the University of California, Berkeley. The
  22. name of the University may not be used to endorse or promote products
  23. derived from this software without specific prior written permission.
  24. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25. WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
  27. #include "system.h"
  28. #include <localedir.h>
  29. #include <safe-read.h>
  30. #include <full-write.h>
  31. #include <getopt.h>
  32. #include <sys/socket.h>
  33. #ifndef EXIT_FAILURE
  34. # define EXIT_FAILURE 1
  35. #endif
  36. #ifndef EXIT_SUCCESS
  37. # define EXIT_SUCCESS 0
  38. #endif
  39. /* Maximum size of a string from the requesting program. */
  40. #define STRING_SIZE 64
  41. /* Name of executing program. */
  42. const char *program_name;
  43. /* File descriptor of the tape device, or negative if none open. */
  44. static int tape = -1;
  45. /* Buffer containing transferred data, and its allocated size. */
  46. static char *record_buffer;
  47. static size_t allocated_size;
  48. /* Buffer for constructing the reply. */
  49. static char reply_buffer[BUFSIZ];
  50. /* Debugging tools. */
  51. static FILE *debug_file;
  52. #define DEBUG(File) \
  53. if (debug_file) fprintf(debug_file, File)
  54. #define DEBUG1(File, Arg) \
  55. if (debug_file) fprintf(debug_file, File, Arg)
  56. #define DEBUG2(File, Arg1, Arg2) \
  57. if (debug_file) fprintf(debug_file, File, Arg1, Arg2)
  58. /* Return an error string, given an error number. */
  59. #if HAVE_STRERROR
  60. # ifndef strerror
  61. char *strerror ();
  62. # endif
  63. #else
  64. static char *
  65. private_strerror (int errnum)
  66. {
  67. extern char *sys_errlist[];
  68. extern int sys_nerr;
  69. if (errnum > 0 && errnum <= sys_nerr)
  70. return _(sys_errlist[errnum]);
  71. return _("Unknown system error");
  72. }
  73. # define strerror private_strerror
  74. #endif
  75. static void
  76. report_error_message (const char *string)
  77. {
  78. DEBUG1 ("rmtd: E 0 (%s)\n", string);
  79. sprintf (reply_buffer, "E0\n%s\n", string);
  80. full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
  81. }
  82. static void
  83. report_numbered_error (int num)
  84. {
  85. DEBUG2 ("rmtd: E %d (%s)\n", num, strerror (num));
  86. sprintf (reply_buffer, "E%d\n%s\n", num, strerror (num));
  87. full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
  88. }
  89. static void
  90. get_string (char *string)
  91. {
  92. int counter;
  93. for (counter = 0; ; counter++)
  94. {
  95. if (safe_read (STDIN_FILENO, string + counter, 1) != 1)
  96. exit (EXIT_SUCCESS);
  97. if (string[counter] == '\n' || counter == STRING_SIZE - 1)
  98. break;
  99. }
  100. string[counter] = '\0';
  101. }
  102. static void
  103. prepare_input_buffer (int fd, size_t size)
  104. {
  105. if (size <= allocated_size)
  106. return;
  107. if (record_buffer)
  108. free (record_buffer);
  109. record_buffer = malloc (size);
  110. if (! record_buffer)
  111. {
  112. DEBUG (_("rmtd: Cannot allocate buffer space\n"));
  113. report_error_message (N_("Cannot allocate buffer space"));
  114. exit (EXIT_FAILURE); /* exit status used to be 4 */
  115. }
  116. allocated_size = size;
  117. #ifdef SO_RCVBUF
  118. if (0 <= fd)
  119. {
  120. int isize = size < INT_MAX ? size : INT_MAX;
  121. while (setsockopt (fd, SOL_SOCKET, SO_RCVBUF,
  122. (char *) &isize, sizeof isize)
  123. && 1024 < isize)
  124. isize >>= 1;
  125. }
  126. #endif
  127. }
  128. /* Decode OFLAG_STRING, which represents the 2nd argument to `open'.
  129. OFLAG_STRING should contain an optional integer, followed by an optional
  130. symbolic representation of an open flag using only '|' to separate its
  131. components (e.g. "O_WRONLY|O_CREAT|O_TRUNC"). Prefer the symbolic
  132. representation if available, falling back on the numeric
  133. representation, or to zero if both formats are absent.
  134. This function should be the inverse of encode_oflag. The numeric
  135. representation is not portable from one host to another, but it is
  136. for backward compatibility with old-fashioned clients that do not
  137. emit symbolic open flags. */
  138. static int
  139. decode_oflag (char const *oflag_string)
  140. {
  141. char *oflag_num_end;
  142. int numeric_oflag = strtol (oflag_string, &oflag_num_end, 10);
  143. int symbolic_oflag = 0;
  144. oflag_string = oflag_num_end;
  145. while (ISSPACE ((unsigned char) *oflag_string))
  146. oflag_string++;
  147. do
  148. {
  149. struct name_value_pair { char const *name; int value; };
  150. static struct name_value_pair const table[] =
  151. {
  152. #ifdef O_APPEND
  153. {"APPEND", O_APPEND},
  154. #endif
  155. {"CREAT", O_CREAT},
  156. #ifdef O_DSYNC
  157. {"DSYNC", O_DSYNC},
  158. #endif
  159. {"EXCL", O_EXCL},
  160. #ifdef O_LARGEFILE
  161. {"LARGEFILE", O_LARGEFILE}, /* LFS extension for opening large files */
  162. #endif
  163. #ifdef O_NOCTTY
  164. {"NOCTTY", O_NOCTTY},
  165. #endif
  166. #ifdef O_NONBLOCK
  167. {"NONBLOCK", O_NONBLOCK},
  168. #endif
  169. {"RDONLY", O_RDONLY},
  170. {"RDWR", O_RDWR},
  171. #ifdef O_RSYNC
  172. {"RSYNC", O_RSYNC},
  173. #endif
  174. #ifdef O_SYNC
  175. {"SYNC", O_SYNC},
  176. #endif
  177. {"TRUNC", O_TRUNC},
  178. {"WRONLY", O_WRONLY}
  179. };
  180. struct name_value_pair const *t;
  181. size_t s;
  182. if (*oflag_string++ != 'O' || *oflag_string++ != '_')
  183. return numeric_oflag;
  184. for (t = table;
  185. (strncmp (oflag_string, t->name, s = strlen (t->name)) != 0
  186. || (oflag_string[s]
  187. && strchr ("ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789",
  188. oflag_string[s])));
  189. t++)
  190. if (t == table + sizeof table / sizeof *table - 1)
  191. return numeric_oflag;
  192. symbolic_oflag |= t->value;
  193. oflag_string += s;
  194. }
  195. while (*oflag_string++ == '|');
  196. return symbolic_oflag;
  197. }
  198. static struct option const long_opts[] =
  199. {
  200. {"help", no_argument, 0, 'h'},
  201. {"version", no_argument, 0, 'v'},
  202. {0, 0, 0, 0}
  203. };
  204. static void usage (int) __attribute__ ((noreturn));
  205. static void
  206. usage (int status)
  207. {
  208. if (status != EXIT_SUCCESS)
  209. fprintf (stderr, _("Try `%s --help' for more information.\n"),
  210. program_name);
  211. else
  212. {
  213. printf (_("\
  214. Usage: %s [OPTION]\n\
  215. Manipulate a tape drive, accepting commands from a remote process.\n\
  216. \n\
  217. --version Output version info.\n\
  218. --help Output this help.\n"),
  219. program_name);
  220. printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
  221. }
  222. exit (status);
  223. }
  224. int
  225. main (int argc, char *const *argv)
  226. {
  227. char command;
  228. size_t status;
  229. /* FIXME: Localization is meaningless, unless --help and --version are
  230. locally used. Localization would be best accomplished by the calling
  231. tar, on messages found within error packets. */
  232. program_name = argv[0];
  233. setlocale (LC_ALL, "");
  234. bindtextdomain (PACKAGE, LOCALEDIR);
  235. textdomain (PACKAGE);
  236. switch (getopt_long (argc, argv, "", long_opts, NULL))
  237. {
  238. default:
  239. usage (EXIT_FAILURE);
  240. case 'h':
  241. usage (EXIT_SUCCESS);
  242. case 'v':
  243. {
  244. printf ("rmt (%s) %s\n%s\n", PACKAGE_NAME, PACKAGE_VERSION,
  245. "Copyright (C) 2004 Free Software Foundation, Inc.");
  246. puts (_("\
  247. This program comes with NO WARRANTY, to the extent permitted by law.\n\
  248. You may redistribute it under the terms of the GNU General Public License;\n\
  249. see the file named COPYING for details."));
  250. }
  251. return EXIT_SUCCESS;
  252. case -1:
  253. break;
  254. }
  255. if (optind < argc)
  256. {
  257. if (optind != argc - 1)
  258. usage (EXIT_FAILURE);
  259. debug_file = fopen (argv[optind], "w");
  260. if (debug_file == 0)
  261. {
  262. report_numbered_error (errno);
  263. return EXIT_FAILURE;
  264. }
  265. setbuf (debug_file, 0);
  266. }
  267. top:
  268. errno = 0;
  269. status = 0;
  270. if (safe_read (STDIN_FILENO, &command, 1) != 1)
  271. return EXIT_SUCCESS;
  272. switch (command)
  273. {
  274. /* FIXME: Maybe 'H' and 'V' for --help and --version output? */
  275. case 'O':
  276. {
  277. char device_string[STRING_SIZE];
  278. char oflag_string[STRING_SIZE];
  279. get_string (device_string);
  280. get_string (oflag_string);
  281. DEBUG2 ("rmtd: O %s %s\n", device_string, oflag_string);
  282. if (tape >= 0)
  283. close (tape);
  284. tape = open (device_string, decode_oflag (oflag_string), MODE_RW);
  285. if (tape < 0)
  286. goto ioerror;
  287. goto respond;
  288. }
  289. case 'C':
  290. {
  291. char device_string[STRING_SIZE];
  292. get_string (device_string); /* discard */
  293. DEBUG ("rmtd: C\n");
  294. if (close (tape) < 0)
  295. goto ioerror;
  296. tape = -1;
  297. goto respond;
  298. }
  299. case 'L':
  300. {
  301. char count_string[STRING_SIZE];
  302. char position_string[STRING_SIZE];
  303. off_t count = 0;
  304. int negative;
  305. int whence;
  306. char *p;
  307. get_string (count_string);
  308. get_string (position_string);
  309. DEBUG2 ("rmtd: L %s %s\n", count_string, position_string);
  310. /* Parse count_string, taking care to check for overflow.
  311. We can't use standard functions,
  312. since off_t might be longer than long. */
  313. for (p = count_string; *p == ' ' || *p == '\t'; p++)
  314. continue;
  315. negative = *p == '-';
  316. p += negative || *p == '+';
  317. for (;;)
  318. {
  319. int digit = *p++ - '0';
  320. if (9 < (unsigned) digit)
  321. break;
  322. else
  323. {
  324. off_t c10 = 10 * count;
  325. off_t nc = negative ? c10 - digit : c10 + digit;
  326. if (c10 / 10 != count || (negative ? c10 < nc : nc < c10))
  327. {
  328. report_error_message (N_("Seek offset out of range"));
  329. return EXIT_FAILURE;
  330. }
  331. count = nc;
  332. }
  333. }
  334. switch (atoi (position_string))
  335. {
  336. case 0: whence = SEEK_SET; break;
  337. case 1: whence = SEEK_CUR; break;
  338. case 2: whence = SEEK_END; break;
  339. default:
  340. report_error_message (N_("Seek direction out of range"));
  341. return EXIT_FAILURE;
  342. }
  343. count = lseek (tape, count, whence);
  344. if (count < 0)
  345. goto ioerror;
  346. /* Convert count back to string for reply.
  347. We can't use sprintf, since off_t might be longer than long. */
  348. p = count_string + sizeof count_string;
  349. *--p = '\0';
  350. do
  351. *--p = '0' + (int) (count % 10);
  352. while ((count /= 10) != 0);
  353. DEBUG1 ("rmtd: A %s\n", p);
  354. sprintf (reply_buffer, "A%s\n", p);
  355. full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
  356. goto top;
  357. }
  358. case 'W':
  359. {
  360. char count_string[STRING_SIZE];
  361. size_t size;
  362. size_t counter;
  363. get_string (count_string);
  364. size = atol (count_string);
  365. DEBUG1 ("rmtd: W %s\n", count_string);
  366. prepare_input_buffer (STDIN_FILENO, size);
  367. for (counter = 0; counter < size; counter += status)
  368. {
  369. status = safe_read (STDIN_FILENO, &record_buffer[counter],
  370. size - counter);
  371. if (status == SAFE_READ_ERROR || status == 0)
  372. {
  373. DEBUG (_("rmtd: Premature eof\n"));
  374. report_error_message (N_("Premature end of file"));
  375. return EXIT_FAILURE; /* exit status used to be 2 */
  376. }
  377. }
  378. status = full_write (tape, record_buffer, size);
  379. if (status != size)
  380. goto ioerror;
  381. goto respond;
  382. }
  383. case 'R':
  384. {
  385. char count_string[STRING_SIZE];
  386. size_t size;
  387. get_string (count_string);
  388. DEBUG1 ("rmtd: R %s\n", count_string);
  389. size = atol (count_string);
  390. prepare_input_buffer (-1, size);
  391. status = safe_read (tape, record_buffer, size);
  392. if (status == SAFE_READ_ERROR)
  393. goto ioerror;
  394. sprintf (reply_buffer, "A%lu\n", (unsigned long int) status);
  395. full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
  396. full_write (STDOUT_FILENO, record_buffer, status);
  397. goto top;
  398. }
  399. case 'I':
  400. {
  401. char operation_string[STRING_SIZE];
  402. char count_string[STRING_SIZE];
  403. get_string (operation_string);
  404. get_string (count_string);
  405. DEBUG2 ("rmtd: I %s %s\n", operation_string, count_string);
  406. #ifdef MTIOCTOP
  407. {
  408. struct mtop mtop;
  409. const char *p;
  410. off_t count = 0;
  411. int negative;
  412. /* Parse count_string, taking care to check for overflow.
  413. We can't use standard functions,
  414. since off_t might be longer than long. */
  415. for (p = count_string; *p == ' ' || *p == '\t'; p++)
  416. continue;
  417. negative = *p == '-';
  418. p += negative || *p == '+';
  419. for (;;)
  420. {
  421. int digit = *p++ - '0';
  422. if (9 < (unsigned) digit)
  423. break;
  424. else
  425. {
  426. off_t c10 = 10 * count;
  427. off_t nc = negative ? c10 - digit : c10 + digit;
  428. if (c10 / 10 != count || (negative ? c10 < nc : nc < c10))
  429. {
  430. report_error_message (N_("Seek offset out of range"));
  431. return EXIT_FAILURE;
  432. }
  433. count = nc;
  434. }
  435. }
  436. mtop.mt_count = count;
  437. if (mtop.mt_count != count)
  438. {
  439. report_error_message (N_("Seek offset out of range"));
  440. return EXIT_FAILURE;
  441. }
  442. mtop.mt_op = atoi (operation_string);
  443. if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0)
  444. goto ioerror;
  445. }
  446. #endif
  447. goto respond;
  448. }
  449. case 'S': /* status */
  450. {
  451. DEBUG ("rmtd: S\n");
  452. #ifdef MTIOCGET
  453. {
  454. struct mtget operation;
  455. if (ioctl (tape, MTIOCGET, (char *) &operation) < 0)
  456. goto ioerror;
  457. status = sizeof operation;
  458. sprintf (reply_buffer, "A%ld\n", (long) status);
  459. full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
  460. full_write (STDOUT_FILENO, (char *) &operation, sizeof operation);
  461. }
  462. #endif
  463. goto top;
  464. }
  465. default:
  466. DEBUG1 (_("rmtd: Garbage command %c\n"), command);
  467. report_error_message (N_("Garbage command"));
  468. return EXIT_FAILURE; /* exit status used to be 3 */
  469. }
  470. respond:
  471. DEBUG1 ("rmtd: A %ld\n", (long) status);
  472. sprintf (reply_buffer, "A%ld\n", (long) status);
  473. full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
  474. goto top;
  475. ioerror:
  476. report_numbered_error (errno);
  477. goto top;
  478. }