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