rmt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* Remote connection server.
  2. Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001 Free Software
  3. 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 <print-copyr.h>
  29. #include <localedir.h>
  30. #include <safe-read.h>
  31. #include <full-write.h>
  32. #include <getopt.h>
  33. #include <sys/socket.h>
  34. #ifndef EXIT_FAILURE
  35. # define EXIT_FAILURE 1
  36. #endif
  37. #ifndef EXIT_SUCCESS
  38. # define EXIT_SUCCESS 0
  39. #endif
  40. /* Maximum size of a string from the requesting program. */
  41. #define STRING_SIZE 64
  42. /* Name of executing program. */
  43. const char *program_name;
  44. /* File descriptor of the tape device, or negative if none open. */
  45. static int tape = -1;
  46. /* Buffer containing transferred data, and its allocated size. */
  47. static char *record_buffer;
  48. static size_t allocated_size;
  49. /* Buffer for constructing the reply. */
  50. static char reply_buffer[BUFSIZ];
  51. /* Debugging tools. */
  52. static FILE *debug_file;
  53. #define DEBUG(File) \
  54. if (debug_file) fprintf(debug_file, File)
  55. #define DEBUG1(File, Arg) \
  56. if (debug_file) fprintf(debug_file, File, Arg)
  57. #define DEBUG2(File, Arg1, Arg2) \
  58. if (debug_file) fprintf(debug_file, File, Arg1, Arg2)
  59. /* Return an error string, given an error number. */
  60. #if HAVE_STRERROR
  61. # ifndef strerror
  62. char *strerror ();
  63. # endif
  64. #else
  65. static char *
  66. private_strerror (int errnum)
  67. {
  68. extern char *sys_errlist[];
  69. extern int sys_nerr;
  70. if (errnum > 0 && errnum <= sys_nerr)
  71. return _(sys_errlist[errnum]);
  72. return _("Unknown system error");
  73. }
  74. # define strerror private_strerror
  75. #endif
  76. static void
  77. report_error_message (const char *string)
  78. {
  79. DEBUG1 ("rmtd: E 0 (%s)\n", string);
  80. sprintf (reply_buffer, "E0\n%s\n", string);
  81. full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
  82. }
  83. static void
  84. report_numbered_error (int num)
  85. {
  86. DEBUG2 ("rmtd: E %d (%s)\n", num, strerror (num));
  87. sprintf (reply_buffer, "E%d\n%s\n", num, strerror (num));
  88. full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
  89. }
  90. static void
  91. get_string (char *string)
  92. {
  93. int counter;
  94. for (counter = 0; counter < STRING_SIZE; counter++)
  95. {
  96. if (safe_read (STDIN_FILENO, string + counter, 1) != 1)
  97. exit (EXIT_SUCCESS);
  98. if (string[counter] == '\n')
  99. break;
  100. }
  101. string[counter] = '\0';
  102. }
  103. static void
  104. prepare_record_buffer (size_t size)
  105. {
  106. if (size <= allocated_size)
  107. return;
  108. if (record_buffer)
  109. free (record_buffer);
  110. record_buffer = malloc (size);
  111. if (! record_buffer)
  112. {
  113. DEBUG (_("rmtd: Cannot allocate buffer space\n"));
  114. report_error_message (N_("Cannot allocate buffer space"));
  115. exit (EXIT_FAILURE); /* exit status used to be 4 */
  116. }
  117. allocated_size = size;
  118. #ifdef SO_RCVBUF
  119. while (size > 1024 &&
  120. (setsockopt (STDIN_FILENO, SOL_SOCKET, SO_RCVBUF,
  121. (char *) &size, sizeof size)
  122. < 0))
  123. size -= 1024;
  124. #else
  125. /* FIXME: I do not see any purpose to the following line... Sigh! */
  126. size = 1 + ((size - 1) % 1024);
  127. #endif
  128. }
  129. /* Decode OFLAG_STRING, which represents the 2nd argument to `open'.
  130. OFLAG_STRING should contain an optional integer, followed by an optional
  131. symbolic representation of an open flag using only '|' to separate its
  132. components (e.g. "O_WRONLY|O_CREAT|O_TRUNC"). Prefer the symbolic
  133. representation if available, falling back on the numeric
  134. representation, or to zero if both formats are absent.
  135. This function should be the inverse of encode_oflag. The numeric
  136. representation is not portable from one host to another, but it is
  137. for backward compatibility with old-fashioned clients that do not
  138. emit symbolic open flags. */
  139. static int
  140. decode_oflag (char const *oflag_string)
  141. {
  142. char *oflag_num_end;
  143. int numeric_oflag = strtol (oflag_string, &oflag_num_end, 10);
  144. int symbolic_oflag = 0;
  145. oflag_string = oflag_num_end;
  146. while (ISSPACE ((unsigned char) *oflag_string))
  147. oflag_string++;
  148. do
  149. {
  150. struct name_value_pair { char const *name; int value; };
  151. static struct name_value_pair const table[] =
  152. {
  153. #ifdef O_APPEND
  154. {"APPEND", O_APPEND},
  155. #endif
  156. {"CREAT", O_CREAT},
  157. #ifdef O_DSYNC
  158. {"DSYNC", O_DSYNC},
  159. #endif
  160. {"EXCL", O_EXCL},
  161. #ifdef O_LARGEFILE
  162. {"LARGEFILE", O_LARGEFILE}, /* LFS extension for opening large files */
  163. #endif
  164. #ifdef O_NOCTTY
  165. {"NOCTTY", O_NOCTTY},
  166. #endif
  167. #ifdef O_NONBLOCK
  168. {"NONBLOCK", O_NONBLOCK},
  169. #endif
  170. {"RDONLY", O_RDONLY},
  171. {"RDWR", O_RDWR},
  172. #ifdef O_RSYNC
  173. {"RSYNC", O_RSYNC},
  174. #endif
  175. #ifdef O_SYNC
  176. {"SYNC", O_SYNC},
  177. #endif
  178. {"TRUNC", O_TRUNC},
  179. {"WRONLY", O_WRONLY}
  180. };
  181. struct name_value_pair const *t;
  182. size_t s;
  183. if (*oflag_string++ != 'O' || *oflag_string++ != '_')
  184. return numeric_oflag;
  185. for (t = table;
  186. (strncmp (oflag_string, t->name, s = strlen (t->name)) != 0
  187. || (oflag_string[s]
  188. && strchr ("ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789",
  189. oflag_string[s])));
  190. t++)
  191. if (t == table + sizeof table / sizeof *table - 1)
  192. return numeric_oflag;
  193. symbolic_oflag |= t->value;
  194. oflag_string += s;
  195. }
  196. while (*oflag_string++ == '|');
  197. return symbolic_oflag;
  198. }
  199. static struct option const long_opts[] =
  200. {
  201. {"help", no_argument, 0, 'h'},
  202. {"version", no_argument, 0, 'v'},
  203. {0, 0, 0, 0}
  204. };
  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. fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout);
  221. }
  222. exit (status);
  223. }
  224. int
  225. main (int argc, char *const *argv)
  226. {
  227. char command;
  228. ssize_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 (GNU %s) %s\n", PACKAGE, VERSION);
  245. print_copyright ("2001 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. exit (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. exit (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. exit (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_record_buffer (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 <= 0)
  372. {
  373. DEBUG (_("rmtd: Premature eof\n"));
  374. report_error_message (N_("Premature end of file"));
  375. exit (EXIT_FAILURE); /* exit status used to be 2 */
  376. }
  377. }
  378. status = full_write (tape, record_buffer, size);
  379. if (status < 0)
  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_record_buffer (size);
  391. status = safe_read (tape, record_buffer, size);
  392. if (status < 0)
  393. goto ioerror;
  394. sprintf (reply_buffer, "A%ld\n", (long) 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. exit (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. exit (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. exit (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. }