1*17538Sralph static char *sccsid = "@(#)dumprmt.c 1.8 (Berkeley) 12/19/84"; 26645Ssam 36884Ssam #include <sys/param.h> 46645Ssam #include <sys/mtio.h> 56645Ssam #include <sys/ioctl.h> 6*17538Sralph #include <sys/socket.h> 7*17538Sralph #include <sys/inode.h> 86645Ssam 99306Ssam #include <netinet/in.h> 109306Ssam 119306Ssam #include <stdio.h> 1216376Skarels #include <pwd.h> 139306Ssam #include <netdb.h> 14*17538Sralph #include <dumprestor.h> 159306Ssam 166645Ssam #define TS_CLOSED 0 176645Ssam #define TS_OPEN 1 186645Ssam 196645Ssam static int rmtstate = TS_CLOSED; 206645Ssam int rmtape; 216645Ssam int rmtconnaborted(); 226645Ssam char *rmtpeer; 236645Ssam 24*17538Sralph extern int ntrec; /* blocking factor on tape */ 25*17538Sralph 266645Ssam rmthost(host) 276645Ssam char *host; 286645Ssam { 296645Ssam 306645Ssam rmtpeer = host; 3113032Ssam signal(SIGPIPE, rmtconnaborted); 326645Ssam rmtgetconn(); 336645Ssam if (rmtape < 0) 34*17538Sralph return (0); 35*17538Sralph return (1); 366645Ssam } 376645Ssam 386645Ssam rmtconnaborted() 396645Ssam { 406645Ssam 41*17538Sralph fprintf(stderr, "rdump: Lost connection to remote host.\n"); 426884Ssam exit(1); 436645Ssam } 446645Ssam 456645Ssam rmtgetconn() 466645Ssam { 479306Ssam static struct servent *sp = 0; 4816376Skarels struct passwd *pw; 4916376Skarels char *name = "root"; 50*17538Sralph int size; 516645Ssam 529306Ssam if (sp == 0) { 539306Ssam sp = getservbyname("shell", "tcp"); 549306Ssam if (sp == 0) { 559306Ssam fprintf(stderr, "rdump: shell/tcp: unknown service\n"); 569306Ssam exit(1); 579306Ssam } 589306Ssam } 5916376Skarels pw = getpwuid(getuid()); 6016376Skarels if (pw && pw->pw_name) 6116376Skarels name = pw->pw_name; 6216376Skarels rmtape = rcmd(&rmtpeer, sp->s_port, name, name, "/etc/rmt", 0); 63*17538Sralph size = ntrec * TP_BSIZE; 64*17538Sralph if (setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0) 65*17538Sralph fprintf(stderr, "rdump: Warning: setsockopt buffer size failed.\n"); 666645Ssam } 676645Ssam 686645Ssam rmtopen(tape, mode) 696645Ssam char *tape; 706645Ssam int mode; 716645Ssam { 726645Ssam char buf[256]; 736645Ssam 746645Ssam sprintf(buf, "O%s\n%d\n", tape, mode); 756645Ssam rmtcall(tape, buf); 766645Ssam rmtstate = TS_OPEN; 776645Ssam } 786645Ssam 796645Ssam rmtclose() 806645Ssam { 816645Ssam 826645Ssam if (rmtstate != TS_OPEN) 836645Ssam return; 846645Ssam rmtcall("close", "C\n"); 856645Ssam rmtstate = TS_CLOSED; 866645Ssam } 876645Ssam 886645Ssam rmtread(buf, count) 896645Ssam char *buf; 906645Ssam int count; 916645Ssam { 926645Ssam char line[30]; 936645Ssam int n, i, cc; 946884Ssam extern errno; 956645Ssam 966645Ssam sprintf(line, "R%d\n", count); 976645Ssam n = rmtcall("read", line); 986884Ssam if (n < 0) { 996884Ssam errno = n; 1006645Ssam return (-1); 1016884Ssam } 1026645Ssam for (i = 0; i < n; i += cc) { 1036645Ssam cc = read(rmtape, buf+i, n - i); 1046884Ssam if (cc <= 0) { 1056645Ssam rmtconnaborted(); 1066884Ssam } 1076645Ssam } 1086645Ssam return (n); 1096645Ssam } 1106645Ssam 1116645Ssam rmtwrite(buf, count) 1126645Ssam char *buf; 1136645Ssam int count; 1146645Ssam { 1156645Ssam char line[30]; 1166645Ssam 1176645Ssam sprintf(line, "W%d\n", count); 1186645Ssam write(rmtape, line, strlen(line)); 1196645Ssam write(rmtape, buf, count); 1206645Ssam return (rmtreply("write")); 1216645Ssam } 1226645Ssam 1236645Ssam rmtwrite0(count) 1246645Ssam int count; 1256645Ssam { 1266645Ssam char line[30]; 1276645Ssam 1286645Ssam sprintf(line, "W%d\n", count); 1296645Ssam write(rmtape, line, strlen(line)); 1306645Ssam } 1316645Ssam 1326645Ssam rmtwrite1(buf, count) 1336645Ssam char *buf; 1346645Ssam int count; 1356645Ssam { 1366645Ssam 1376645Ssam write(rmtape, buf, count); 1386645Ssam } 1396645Ssam 1406645Ssam rmtwrite2() 1416645Ssam { 1426645Ssam int i; 1436645Ssam 1446645Ssam return (rmtreply("write")); 1456645Ssam } 1466645Ssam 1476645Ssam rmtseek(offset, pos) 1486645Ssam int offset, pos; 1496645Ssam { 1506645Ssam char line[80]; 1516645Ssam 1526645Ssam sprintf(line, "L%d\n%d\n", offset, pos); 1536645Ssam return (rmtcall("seek", line)); 1546645Ssam } 1556645Ssam 1566645Ssam struct mtget mts; 1576645Ssam 1586645Ssam struct mtget * 1596645Ssam rmtstatus() 1606645Ssam { 1616645Ssam register int i; 1626645Ssam register char *cp; 1636645Ssam 1646645Ssam if (rmtstate != TS_OPEN) 1656645Ssam return (0); 1666645Ssam rmtcall("status", "S\n"); 1676645Ssam for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++) 1686645Ssam *cp++ = rmtgetb(); 1696645Ssam return (&mts); 1706645Ssam } 1716645Ssam 1726645Ssam rmtioctl(cmd, count) 1736645Ssam int cmd, count; 1746645Ssam { 1756645Ssam char buf[256]; 1766645Ssam 1776645Ssam if (count < 0) 1786645Ssam return (-1); 1796645Ssam sprintf(buf, "I%d\n%d\n", cmd, count); 18013545Ssam return (rmtcall("ioctl", buf)); 1816645Ssam } 1826645Ssam 1836645Ssam rmtcall(cmd, buf) 1846645Ssam char *cmd, *buf; 1856645Ssam { 1866645Ssam 1876645Ssam if (write(rmtape, buf, strlen(buf)) != strlen(buf)) 1886645Ssam rmtconnaborted(); 1896645Ssam return (rmtreply(cmd)); 1906645Ssam } 1916645Ssam 1926645Ssam rmtreply(cmd) 1936645Ssam char *cmd; 1946645Ssam { 1956645Ssam register int c; 1966645Ssam char code[30], emsg[BUFSIZ]; 1976645Ssam 1986645Ssam rmtgets(code, sizeof (code)); 1996645Ssam if (*code == 'E' || *code == 'F') { 2006645Ssam rmtgets(emsg, sizeof (emsg)); 2016645Ssam msg("%s: %s\n", cmd, emsg, code + 1); 2026645Ssam if (*code == 'F') { 2036645Ssam rmtstate = TS_CLOSED; 2046645Ssam return (-1); 2056645Ssam } 2066645Ssam return (-1); 2076645Ssam } 2086645Ssam if (*code != 'A') { 2096645Ssam msg("Protocol to remote tape server botched (code %s?).\n", 2106645Ssam code); 2116645Ssam rmtconnaborted(); 2126645Ssam } 2136645Ssam return (atoi(code + 1)); 2146645Ssam } 2156645Ssam 2166645Ssam rmtgetb() 2176645Ssam { 2186645Ssam char c; 2196645Ssam 2206645Ssam if (read(rmtape, &c, 1) != 1) 2216645Ssam rmtconnaborted(); 2226645Ssam return (c); 2236645Ssam } 2246645Ssam 2256645Ssam rmtgets(cp, len) 2266645Ssam char *cp; 2276645Ssam int len; 2286645Ssam { 2296645Ssam 2306645Ssam while (len > 1) { 2316645Ssam *cp = rmtgetb(); 2326645Ssam if (*cp == '\n') { 2336645Ssam cp[1] = 0; 2346645Ssam return; 2356645Ssam } 2366645Ssam cp++; 2376645Ssam len--; 2386645Ssam } 2396645Ssam msg("Protocol to remote tape server botched (in rmtgets).\n"); 2406645Ssam rmtconnaborted(); 2416645Ssam } 242