]> Dogcows Code - chaz/tar/blob - src/rtapelib.c
Fix improper use of 'path' word
[chaz/tar] / src / rtapelib.c
1 /* Functions for communicating with a remote tape drive.
2
3 Copyright 1988, 1992, 1994, 1996, 1997, 1999, 2000, 2001, 2004 Free
4 Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /* The man page rmt(8) for /etc/rmt documents the remote mag tape protocol
21 which rdump and rrestore use. Unfortunately, the man page is *WRONG*.
22 The author of the routines I'm including originally wrote his code just
23 based on the man page, and it didn't work, so he went to the rdump source
24 to figure out why. The only thing he had to change was to check for the
25 'F' return code in addition to the 'E', and to separate the various
26 arguments with \n instead of a space. I personally don't think that this
27 is much of a problem, but I wanted to point it out. -- Arnold Robbins
28
29 Originally written by Jeff Lee, modified some by Arnold Robbins. Redone
30 as a library that can replace open, read, write, etc., by Fred Fish, with
31 some additional work by Arnold Robbins. Modified to make all rmt* calls
32 into macros for speed by Jay Fenlason. Use -DWITH_REXEC for rexec
33 code, courtesy of Dan Kegel. */
34
35 #include "system.h"
36 #include "common.h"
37 #include <safe-read.h>
38 #include <full-write.h>
39
40 /* Try hard to get EOPNOTSUPP defined. 486/ISC has it in net/errno.h,
41 3B2/SVR3 has it in sys/inet.h. Otherwise, like on MSDOS, use EINVAL. */
42
43 #ifndef EOPNOTSUPP
44 # if HAVE_NET_ERRNO_H
45 # include <net/errno.h>
46 # endif
47 # if HAVE_SYS_INET_H
48 # include <sys/inet.h>
49 # endif
50 # ifndef EOPNOTSUPP
51 # define EOPNOTSUPP EINVAL
52 # endif
53 #endif
54
55 #include <signal.h>
56
57 #if HAVE_NETDB_H
58 # include <netdb.h>
59 #endif
60
61 #include "rmt.h"
62
63 /* Exit status if exec errors. */
64 #define EXIT_ON_EXEC_ERROR 128
65
66 /* FIXME: Size of buffers for reading and writing commands to rmt. */
67 #define COMMAND_BUFFER_SIZE 64
68
69 #ifndef RETSIGTYPE
70 # define RETSIGTYPE void
71 #endif
72
73 /* FIXME: Maximum number of simultaneous remote tape connections. */
74 #define MAXUNIT 4
75
76 #define PREAD 0 /* read file descriptor from pipe() */
77 #define PWRITE 1 /* write file descriptor from pipe() */
78
79 /* Return the parent's read side of remote tape connection Fd. */
80 #define READ_SIDE(Fd) (from_remote[Fd][PREAD])
81
82 /* Return the parent's write side of remote tape connection Fd. */
83 #define WRITE_SIDE(Fd) (to_remote[Fd][PWRITE])
84
85 /* The pipes for receiving data from remote tape drives. */
86 static int from_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
87
88 /* The pipes for sending data to remote tape drives. */
89 static int to_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
90
91 #define RMT_COMMAND (rmt_command_option ? rmt_command_option : "/etc/rmt")
92
93 /* Temporary variable used by macros in rmt.h. */
94 char *rmt_dev_name__;
95 \f
96
97 /* Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE. */
98 static void
99 _rmt_shutdown (int handle, int errno_value)
100 {
101 close (READ_SIDE (handle));
102 close (WRITE_SIDE (handle));
103 READ_SIDE (handle) = -1;
104 WRITE_SIDE (handle) = -1;
105 errno = errno_value;
106 }
107
108 /* Attempt to perform the remote tape command specified in BUFFER on
109 remote tape connection HANDLE. Return 0 if successful, -1 on
110 error. */
111 static int
112 do_command (int handle, const char *buffer)
113 {
114 /* Save the current pipe handler and try to make the request. */
115
116 size_t length = strlen (buffer);
117 RETSIGTYPE (*pipe_handler) () = signal (SIGPIPE, SIG_IGN);
118 ssize_t written = full_write (WRITE_SIDE (handle), buffer, length);
119 signal (SIGPIPE, pipe_handler);
120
121 if (written == length)
122 return 0;
123
124 /* Something went wrong. Close down and go home. */
125
126 _rmt_shutdown (handle, EIO);
127 return -1;
128 }
129
130 static char *
131 get_status_string (int handle, char *command_buffer)
132 {
133 char *cursor;
134 int counter;
135
136 /* Read the reply command line. */
137
138 for (counter = 0, cursor = command_buffer;
139 counter < COMMAND_BUFFER_SIZE;
140 counter++, cursor++)
141 {
142 if (safe_read (READ_SIDE (handle), cursor, 1) != 1)
143 {
144 _rmt_shutdown (handle, EIO);
145 return 0;
146 }
147 if (*cursor == '\n')
148 {
149 *cursor = '\0';
150 break;
151 }
152 }
153
154 if (counter == COMMAND_BUFFER_SIZE)
155 {
156 _rmt_shutdown (handle, EIO);
157 return 0;
158 }
159
160 /* Check the return status. */
161
162 for (cursor = command_buffer; *cursor; cursor++)
163 if (*cursor != ' ')
164 break;
165
166 if (*cursor == 'E' || *cursor == 'F')
167 {
168 /* Skip the error message line. */
169
170 /* FIXME: there is better to do than merely ignoring error messages
171 coming from the remote end. Translate them, too... */
172
173 {
174 char character;
175
176 while (safe_read (READ_SIDE (handle), &character, 1) == 1)
177 if (character == '\n')
178 break;
179 }
180
181 errno = atoi (cursor + 1);
182
183 if (*cursor == 'F')
184 _rmt_shutdown (handle, errno);
185
186 return 0;
187 }
188
189 /* Check for mis-synced pipes. */
190
191 if (*cursor != 'A')
192 {
193 _rmt_shutdown (handle, EIO);
194 return 0;
195 }
196
197 /* Got an `A' (success) response. */
198
199 return cursor + 1;
200 }
201
202 /* Read and return the status from remote tape connection HANDLE. If
203 an error occurred, return -1 and set errno. */
204 static long int
205 get_status (int handle)
206 {
207 char command_buffer[COMMAND_BUFFER_SIZE];
208 const char *status = get_status_string (handle, command_buffer);
209 if (status)
210 {
211 long int result = atol (status);
212 if (0 <= result)
213 return result;
214 errno = EIO;
215 }
216 return -1;
217 }
218
219 static off_t
220 get_status_off (int handle)
221 {
222 char command_buffer[COMMAND_BUFFER_SIZE];
223 const char *status = get_status_string (handle, command_buffer);
224
225 if (! status)
226 return -1;
227 else
228 {
229 /* Parse status, taking care to check for overflow.
230 We can't use standard functions,
231 since off_t might be longer than long. */
232
233 off_t count = 0;
234 int negative;
235
236 for (; *status == ' ' || *status == '\t'; status++)
237 continue;
238
239 negative = *status == '-';
240 status += negative || *status == '+';
241
242 for (;;)
243 {
244 int digit = *status++ - '0';
245 if (9 < (unsigned) digit)
246 break;
247 else
248 {
249 off_t c10 = 10 * count;
250 off_t nc = negative ? c10 - digit : c10 + digit;
251 if (c10 / 10 != count || (negative ? c10 < nc : nc < c10))
252 return -1;
253 count = nc;
254 }
255 }
256
257 return count;
258 }
259 }
260
261 #if WITH_REXEC
262
263 /* Execute /etc/rmt as user USER on remote system HOST using rexec.
264 Return a file descriptor of a bidirectional socket for stdin and
265 stdout. If USER is zero, use the current username.
266
267 By default, this code is not used, since it requires that the user
268 have a .netrc file in his/her home directory, or that the
269 application designer be willing to have rexec prompt for login and
270 password info. This may be unacceptable, and .rhosts files for use
271 with rsh are much more common on BSD systems. */
272 static int
273 _rmt_rexec (char *host, char *user)
274 {
275 int saved_stdin = dup (STDIN_FILENO);
276 int saved_stdout = dup (STDOUT_FILENO);
277 struct servent *rexecserv;
278 int result;
279
280 /* When using cpio -o < filename, stdin is no longer the tty. But the
281 rexec subroutine reads the login and the passwd on stdin, to allow
282 remote execution of the command. So, reopen stdin and stdout on
283 /dev/tty before the rexec and give them back their original value
284 after. */
285
286 if (! freopen ("/dev/tty", "r", stdin))
287 freopen ("/dev/null", "r", stdin);
288 if (! freopen ("/dev/tty", "w", stdout))
289 freopen ("/dev/null", "w", stdout);
290
291 if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv)
292 error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available"));
293
294 result = rexec (&host, rexecserv->s_port, user, 0, RMT_COMMAND, 0);
295 if (fclose (stdin) == EOF)
296 error (0, errno, _("stdin"));
297 fdopen (saved_stdin, "r");
298 if (fclose (stdout) == EOF)
299 error (0, errno, _("stdout"));
300 fdopen (saved_stdout, "w");
301
302 return result;
303 }
304
305 #endif /* WITH_REXEC */
306
307 /* Place into BUF a string representing OFLAG, which must be suitable
308 as argument 2 of `open'. BUF must be large enough to hold the
309 result. This function should generate a string that decode_oflag
310 can parse. */
311 static void
312 encode_oflag (char *buf, int oflag)
313 {
314 sprintf (buf, "%d ", oflag);
315
316 switch (oflag & O_ACCMODE)
317 {
318 case O_RDONLY: strcat (buf, "O_RDONLY"); break;
319 case O_RDWR: strcat (buf, "O_RDWR"); break;
320 case O_WRONLY: strcat (buf, "O_WRONLY"); break;
321 default: abort ();
322 }
323
324 #ifdef O_APPEND
325 if (oflag & O_APPEND) strcat (buf, "|O_APPEND");
326 #endif
327 if (oflag & O_CREAT) strcat (buf, "|O_CREAT");
328 #ifdef O_DSYNC
329 if (oflag & O_DSYNC) strcat (buf, "|O_DSYNC");
330 #endif
331 if (oflag & O_EXCL) strcat (buf, "|O_EXCL");
332 #ifdef O_LARGEFILE
333 if (oflag & O_LARGEFILE) strcat (buf, "|O_LARGEFILE");
334 #endif
335 #ifdef O_NOCTTY
336 if (oflag & O_NOCTTY) strcat (buf, "|O_NOCTTY");
337 #endif
338 #ifdef O_NONBLOCK
339 if (oflag & O_NONBLOCK) strcat (buf, "|O_NONBLOCK");
340 #endif
341 #ifdef O_RSYNC
342 if (oflag & O_RSYNC) strcat (buf, "|O_RSYNC");
343 #endif
344 #ifdef O_SYNC
345 if (oflag & O_SYNC) strcat (buf, "|O_SYNC");
346 #endif
347 if (oflag & O_TRUNC) strcat (buf, "|O_TRUNC");
348 }
349
350 /* Open a file (a magnetic tape device?) on the system specified in
351 FILE_NAME, as the given user. FILE_NAME has the form `[USER@]HOST:FILE'.
352 OPEN_MODE is O_RDONLY, O_WRONLY, etc. If successful, return the
353 remote pipe number plus BIAS. REMOTE_SHELL may be overridden. On
354 error, return -1. */
355 int
356 rmt_open__ (const char *file_name, int open_mode, int bias,
357 const char *remote_shell)
358 {
359 int remote_pipe_number; /* pseudo, biased file descriptor */
360 char *file_name_copy; /* copy of file_name string */
361 char *remote_host; /* remote host name */
362 char *remote_file; /* remote file name (often a device) */
363 char *remote_user; /* remote user name */
364
365 /* Find an unused pair of file descriptors. */
366
367 for (remote_pipe_number = 0;
368 remote_pipe_number < MAXUNIT;
369 remote_pipe_number++)
370 if (READ_SIDE (remote_pipe_number) == -1
371 && WRITE_SIDE (remote_pipe_number) == -1)
372 break;
373
374 if (remote_pipe_number == MAXUNIT)
375 {
376 errno = EMFILE;
377 return -1;
378 }
379
380 /* Pull apart the system and device, and optional user. */
381
382 {
383 char *cursor;
384
385 file_name_copy = xstrdup (file_name);
386 remote_host = file_name_copy;
387 remote_user = 0;
388 remote_file = 0;
389
390 for (cursor = file_name_copy; *cursor; cursor++)
391 switch (*cursor)
392 {
393 default:
394 break;
395
396 case '\n':
397 /* Do not allow newlines in the file_name, since the protocol
398 uses newline delimiters. */
399 free (file_name_copy);
400 errno = ENOENT;
401 return -1;
402
403 case '@':
404 if (!remote_user)
405 {
406 remote_user = remote_host;
407 *cursor = '\0';
408 remote_host = cursor + 1;
409 }
410 break;
411
412 case ':':
413 if (!remote_file)
414 {
415 *cursor = '\0';
416 remote_file = cursor + 1;
417 }
418 break;
419 }
420 }
421
422 /* FIXME: Should somewhat validate the decoding, here. */
423
424 if (remote_user && *remote_user == '\0')
425 remote_user = 0;
426
427 #if WITH_REXEC
428
429 /* Execute the remote command using rexec. */
430
431 READ_SIDE (remote_pipe_number) = _rmt_rexec (remote_host, remote_user);
432 if (READ_SIDE (remote_pipe_number) < 0)
433 {
434 int e = errno;
435 free (file_name_copy);
436 errno = e;
437 return -1;
438 }
439
440 WRITE_SIDE (remote_pipe_number) = READ_SIDE (remote_pipe_number);
441
442 #else /* not WITH_REXEC */
443 {
444 const char *remote_shell_basename;
445 pid_t status;
446
447 /* Identify the remote command to be executed. */
448
449 if (!remote_shell)
450 {
451 #ifdef REMOTE_SHELL
452 remote_shell = REMOTE_SHELL;
453 #else
454 free (file_name_copy);
455 errno = EIO;
456 return -1;
457 #endif
458 }
459 remote_shell_basename = base_name (remote_shell);
460
461 /* Set up the pipes for the `rsh' command, and fork. */
462
463 if (pipe (to_remote[remote_pipe_number]) == -1
464 || pipe (from_remote[remote_pipe_number]) == -1)
465 {
466 int e = errno;
467 free (file_name_copy);
468 errno = e;
469 return -1;
470 }
471
472 status = fork ();
473 if (status == -1)
474 {
475 int e = errno;
476 free (file_name_copy);
477 errno = e;
478 return -1;
479 }
480
481 if (status == 0)
482 {
483 /* Child. */
484
485 close (STDIN_FILENO);
486 dup (to_remote[remote_pipe_number][PREAD]);
487 close (to_remote[remote_pipe_number][PREAD]);
488 close (to_remote[remote_pipe_number][PWRITE]);
489
490 close (STDOUT_FILENO);
491 dup (from_remote[remote_pipe_number][PWRITE]);
492 close (from_remote[remote_pipe_number][PREAD]);
493 close (from_remote[remote_pipe_number][PWRITE]);
494
495 sys_reset_uid_gid ();
496
497 if (remote_user)
498 execl (remote_shell, remote_shell_basename, remote_host,
499 "-l", remote_user, RMT_COMMAND, (char *) 0);
500 else
501 execl (remote_shell, remote_shell_basename, remote_host,
502 RMT_COMMAND, (char *) 0);
503
504 /* Bad problems if we get here. */
505
506 /* In a previous version, _exit was used here instead of exit. */
507 error (EXIT_ON_EXEC_ERROR, errno, _("Cannot execute remote shell"));
508 }
509
510 /* Parent. */
511
512 close (from_remote[remote_pipe_number][PWRITE]);
513 close (to_remote[remote_pipe_number][PREAD]);
514 }
515 #endif /* not WITH_REXEC */
516
517 /* Attempt to open the tape device. */
518
519 {
520 size_t remote_file_len = strlen (remote_file);
521 char *command_buffer = xmalloc (remote_file_len + 1000);
522 sprintf (command_buffer, "O%s\n", remote_file);
523 encode_oflag (command_buffer + remote_file_len + 2, open_mode);
524 strcat (command_buffer, "\n");
525 if (do_command (remote_pipe_number, command_buffer) == -1
526 || get_status (remote_pipe_number) == -1)
527 {
528 int e = errno;
529 free (command_buffer);
530 free (file_name_copy);
531 _rmt_shutdown (remote_pipe_number, e);
532 return -1;
533 }
534 free (command_buffer);
535 }
536
537 free (file_name_copy);
538 return remote_pipe_number + bias;
539 }
540
541 /* Close remote tape connection HANDLE and shut down. Return 0 if
542 successful, -1 on error. */
543 int
544 rmt_close__ (int handle)
545 {
546 long int status;
547
548 if (do_command (handle, "C\n") == -1)
549 return -1;
550
551 status = get_status (handle);
552 _rmt_shutdown (handle, errno);
553 return status;
554 }
555
556 /* Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE.
557 Return the number of bytes read on success, SAFE_READ_ERROR on error. */
558 size_t
559 rmt_read__ (int handle, char *buffer, size_t length)
560 {
561 char command_buffer[COMMAND_BUFFER_SIZE];
562 size_t status;
563 size_t rlen;
564 size_t counter;
565
566 sprintf (command_buffer, "R%lu\n", (unsigned long) length);
567 if (do_command (handle, command_buffer) == -1
568 || (status = get_status (handle)) == SAFE_READ_ERROR)
569 return SAFE_READ_ERROR;
570
571 for (counter = 0; counter < status; counter += rlen, buffer += rlen)
572 {
573 rlen = safe_read (READ_SIDE (handle), buffer, status - counter);
574 if (rlen == SAFE_READ_ERROR || rlen == 0)
575 {
576 _rmt_shutdown (handle, EIO);
577 return SAFE_READ_ERROR;
578 }
579 }
580
581 return status;
582 }
583
584 /* Write LENGTH bytes from BUFFER to remote tape connection HANDLE.
585 Return the number of bytes written. */
586 size_t
587 rmt_write__ (int handle, char *buffer, size_t length)
588 {
589 char command_buffer[COMMAND_BUFFER_SIZE];
590 RETSIGTYPE (*pipe_handler) ();
591 size_t written;
592
593 sprintf (command_buffer, "W%lu\n", (unsigned long) length);
594 if (do_command (handle, command_buffer) == -1)
595 return 0;
596
597 pipe_handler = signal (SIGPIPE, SIG_IGN);
598 written = full_write (WRITE_SIDE (handle), buffer, length);
599 signal (SIGPIPE, pipe_handler);
600 if (written == length)
601 {
602 long int r = get_status (handle);
603 if (r < 0)
604 return 0;
605 if (r == length)
606 return length;
607 written = r;
608 }
609
610 /* Write error. */
611
612 _rmt_shutdown (handle, EIO);
613 return written;
614 }
615
616 /* Perform an imitation lseek operation on remote tape connection
617 HANDLE. Return the new file offset if successful, -1 if on error. */
618 off_t
619 rmt_lseek__ (int handle, off_t offset, int whence)
620 {
621 char command_buffer[COMMAND_BUFFER_SIZE];
622 char operand_buffer[UINTMAX_STRSIZE_BOUND];
623 uintmax_t u = offset < 0 ? - (uintmax_t) offset : (uintmax_t) offset;
624 char *p = operand_buffer + sizeof operand_buffer;
625
626 *--p = 0;
627 do
628 *--p = '0' + (int) (u % 10);
629 while ((u /= 10) != 0);
630 if (offset < 0)
631 *--p = '-';
632
633 switch (whence)
634 {
635 case SEEK_SET: whence = 0; break;
636 case SEEK_CUR: whence = 1; break;
637 case SEEK_END: whence = 2; break;
638 default: abort ();
639 }
640
641 sprintf (command_buffer, "L%s\n%d\n", p, whence);
642
643 if (do_command (handle, command_buffer) == -1)
644 return -1;
645
646 return get_status_off (handle);
647 }
648
649 /* Perform a raw tape operation on remote tape connection HANDLE.
650 Return the results of the ioctl, or -1 on error. */
651 int
652 rmt_ioctl__ (int handle, int operation, char *argument)
653 {
654 switch (operation)
655 {
656 default:
657 errno = EOPNOTSUPP;
658 return -1;
659
660 #ifdef MTIOCTOP
661 case MTIOCTOP:
662 {
663 char command_buffer[COMMAND_BUFFER_SIZE];
664 char operand_buffer[UINTMAX_STRSIZE_BOUND];
665 uintmax_t u = (((struct mtop *) argument)->mt_count < 0
666 ? - (uintmax_t) ((struct mtop *) argument)->mt_count
667 : (uintmax_t) ((struct mtop *) argument)->mt_count);
668 char *p = operand_buffer + sizeof operand_buffer;
669
670 *--p = 0;
671 do
672 *--p = '0' + (int) (u % 10);
673 while ((u /= 10) != 0);
674 if (((struct mtop *) argument)->mt_count < 0)
675 *--p = '-';
676
677 /* MTIOCTOP is the easy one. Nothing is transferred in binary. */
678
679 sprintf (command_buffer, "I%d\n%s\n",
680 ((struct mtop *) argument)->mt_op, p);
681 if (do_command (handle, command_buffer) == -1)
682 return -1;
683
684 return get_status (handle);
685 }
686 #endif /* MTIOCTOP */
687
688 #ifdef MTIOCGET
689 case MTIOCGET:
690 {
691 ssize_t status;
692 size_t counter;
693
694 /* Grab the status and read it directly into the structure. This
695 assumes that the status buffer is not padded and that 2 shorts
696 fit in a long without any word alignment problems; i.e., the
697 whole struct is contiguous. NOTE - this is probably NOT a good
698 assumption. */
699
700 if (do_command (handle, "S") == -1
701 || (status = get_status (handle), status == -1))
702 return -1;
703
704 for (; status > 0; status -= counter, argument += counter)
705 {
706 counter = safe_read (READ_SIDE (handle), argument, status);
707 if (counter == SAFE_READ_ERROR || counter == 0)
708 {
709 _rmt_shutdown (handle, EIO);
710 return -1;
711 }
712 }
713
714 /* Check for byte position. mt_type (or mt_model) is a small integer
715 field (normally) so we will check its magnitude. If it is larger
716 than 256, we will assume that the bytes are swapped and go through
717 and reverse all the bytes. */
718
719 if (((struct mtget *) argument)->MTIO_CHECK_FIELD < 256)
720 return 0;
721
722 for (counter = 0; counter < status; counter += 2)
723 {
724 char copy = argument[counter];
725
726 argument[counter] = argument[counter + 1];
727 argument[counter + 1] = copy;
728 }
729
730 return 0;
731 }
732 #endif /* MTIOCGET */
733
734 }
735 }
This page took 0.063606 seconds and 4 git commands to generate.