rmt.c 13 KB

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