1*22039Sdist /* 2*22039Sdist * Copyright (c) 1980 Regents of the University of California. 3*22039Sdist * All rights reserved. The Berkeley software License Agreement 4*22039Sdist * specifies the terms and conditions for redistribution. 5*22039Sdist */ 66645Ssam 7*22039Sdist #ifndef lint 8*22039Sdist static char sccsid[] = "@(#)dumprmt.c 5.1 (Berkeley) 06/05/85"; 9*22039Sdist #endif not lint 10*22039Sdist 116884Ssam #include <sys/param.h> 126645Ssam #include <sys/mtio.h> 136645Ssam #include <sys/ioctl.h> 1417538Sralph #include <sys/socket.h> 1517538Sralph #include <sys/inode.h> 166645Ssam 179306Ssam #include <netinet/in.h> 189306Ssam 199306Ssam #include <stdio.h> 2016376Skarels #include <pwd.h> 219306Ssam #include <netdb.h> 2217538Sralph #include <dumprestor.h> 239306Ssam 246645Ssam #define TS_CLOSED 0 256645Ssam #define TS_OPEN 1 266645Ssam 276645Ssam static int rmtstate = TS_CLOSED; 286645Ssam int rmtape; 296645Ssam int rmtconnaborted(); 306645Ssam char *rmtpeer; 316645Ssam 3217538Sralph extern int ntrec; /* blocking factor on tape */ 3317538Sralph 346645Ssam rmthost(host) 356645Ssam char *host; 366645Ssam { 376645Ssam 386645Ssam rmtpeer = host; 3913032Ssam signal(SIGPIPE, rmtconnaborted); 406645Ssam rmtgetconn(); 416645Ssam if (rmtape < 0) 4217538Sralph return (0); 4317538Sralph return (1); 446645Ssam } 456645Ssam 466645Ssam rmtconnaborted() 476645Ssam { 486645Ssam 4917538Sralph fprintf(stderr, "rdump: Lost connection to remote host.\n"); 506884Ssam exit(1); 516645Ssam } 526645Ssam 536645Ssam rmtgetconn() 546645Ssam { 559306Ssam static struct servent *sp = 0; 5616376Skarels struct passwd *pw; 5716376Skarels char *name = "root"; 5817538Sralph int size; 596645Ssam 609306Ssam if (sp == 0) { 619306Ssam sp = getservbyname("shell", "tcp"); 629306Ssam if (sp == 0) { 639306Ssam fprintf(stderr, "rdump: shell/tcp: unknown service\n"); 649306Ssam exit(1); 659306Ssam } 669306Ssam } 6716376Skarels pw = getpwuid(getuid()); 6816376Skarels if (pw && pw->pw_name) 6916376Skarels name = pw->pw_name; 7016376Skarels rmtape = rcmd(&rmtpeer, sp->s_port, name, name, "/etc/rmt", 0); 7119983Smckusick #ifdef notdef /* broken */ 7217538Sralph size = ntrec * TP_BSIZE; 7318493Smckusick while (size > TP_BSIZE && 7418493Smckusick setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0) 7518493Smckusick size -= TP_BSIZE; 7619983Smckusick #endif notdef 776645Ssam } 786645Ssam 796645Ssam rmtopen(tape, mode) 806645Ssam char *tape; 816645Ssam int mode; 826645Ssam { 836645Ssam char buf[256]; 846645Ssam 856645Ssam sprintf(buf, "O%s\n%d\n", tape, mode); 866645Ssam rmtcall(tape, buf); 876645Ssam rmtstate = TS_OPEN; 886645Ssam } 896645Ssam 906645Ssam rmtclose() 916645Ssam { 926645Ssam 936645Ssam if (rmtstate != TS_OPEN) 946645Ssam return; 956645Ssam rmtcall("close", "C\n"); 966645Ssam rmtstate = TS_CLOSED; 976645Ssam } 986645Ssam 996645Ssam rmtread(buf, count) 1006645Ssam char *buf; 1016645Ssam int count; 1026645Ssam { 1036645Ssam char line[30]; 1046645Ssam int n, i, cc; 1056884Ssam extern errno; 1066645Ssam 1076645Ssam sprintf(line, "R%d\n", count); 1086645Ssam n = rmtcall("read", line); 1096884Ssam if (n < 0) { 1106884Ssam errno = n; 1116645Ssam return (-1); 1126884Ssam } 1136645Ssam for (i = 0; i < n; i += cc) { 1146645Ssam cc = read(rmtape, buf+i, n - i); 1156884Ssam if (cc <= 0) { 1166645Ssam rmtconnaborted(); 1176884Ssam } 1186645Ssam } 1196645Ssam return (n); 1206645Ssam } 1216645Ssam 1226645Ssam rmtwrite(buf, count) 1236645Ssam char *buf; 1246645Ssam int count; 1256645Ssam { 1266645Ssam char line[30]; 1276645Ssam 1286645Ssam sprintf(line, "W%d\n", count); 1296645Ssam write(rmtape, line, strlen(line)); 1306645Ssam write(rmtape, buf, count); 1316645Ssam return (rmtreply("write")); 1326645Ssam } 1336645Ssam 1346645Ssam rmtwrite0(count) 1356645Ssam int count; 1366645Ssam { 1376645Ssam char line[30]; 1386645Ssam 1396645Ssam sprintf(line, "W%d\n", count); 1406645Ssam write(rmtape, line, strlen(line)); 1416645Ssam } 1426645Ssam 1436645Ssam rmtwrite1(buf, count) 1446645Ssam char *buf; 1456645Ssam int count; 1466645Ssam { 1476645Ssam 1486645Ssam write(rmtape, buf, count); 1496645Ssam } 1506645Ssam 1516645Ssam rmtwrite2() 1526645Ssam { 1536645Ssam int i; 1546645Ssam 1556645Ssam return (rmtreply("write")); 1566645Ssam } 1576645Ssam 1586645Ssam rmtseek(offset, pos) 1596645Ssam int offset, pos; 1606645Ssam { 1616645Ssam char line[80]; 1626645Ssam 1636645Ssam sprintf(line, "L%d\n%d\n", offset, pos); 1646645Ssam return (rmtcall("seek", line)); 1656645Ssam } 1666645Ssam 1676645Ssam struct mtget mts; 1686645Ssam 1696645Ssam struct mtget * 1706645Ssam rmtstatus() 1716645Ssam { 1726645Ssam register int i; 1736645Ssam register char *cp; 1746645Ssam 1756645Ssam if (rmtstate != TS_OPEN) 1766645Ssam return (0); 1776645Ssam rmtcall("status", "S\n"); 1786645Ssam for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++) 1796645Ssam *cp++ = rmtgetb(); 1806645Ssam return (&mts); 1816645Ssam } 1826645Ssam 1836645Ssam rmtioctl(cmd, count) 1846645Ssam int cmd, count; 1856645Ssam { 1866645Ssam char buf[256]; 1876645Ssam 1886645Ssam if (count < 0) 1896645Ssam return (-1); 1906645Ssam sprintf(buf, "I%d\n%d\n", cmd, count); 19113545Ssam return (rmtcall("ioctl", buf)); 1926645Ssam } 1936645Ssam 1946645Ssam rmtcall(cmd, buf) 1956645Ssam char *cmd, *buf; 1966645Ssam { 1976645Ssam 1986645Ssam if (write(rmtape, buf, strlen(buf)) != strlen(buf)) 1996645Ssam rmtconnaborted(); 2006645Ssam return (rmtreply(cmd)); 2016645Ssam } 2026645Ssam 2036645Ssam rmtreply(cmd) 2046645Ssam char *cmd; 2056645Ssam { 2066645Ssam register int c; 2076645Ssam char code[30], emsg[BUFSIZ]; 2086645Ssam 2096645Ssam rmtgets(code, sizeof (code)); 2106645Ssam if (*code == 'E' || *code == 'F') { 2116645Ssam rmtgets(emsg, sizeof (emsg)); 2126645Ssam msg("%s: %s\n", cmd, emsg, code + 1); 2136645Ssam if (*code == 'F') { 2146645Ssam rmtstate = TS_CLOSED; 2156645Ssam return (-1); 2166645Ssam } 2176645Ssam return (-1); 2186645Ssam } 2196645Ssam if (*code != 'A') { 2206645Ssam msg("Protocol to remote tape server botched (code %s?).\n", 2216645Ssam code); 2226645Ssam rmtconnaborted(); 2236645Ssam } 2246645Ssam return (atoi(code + 1)); 2256645Ssam } 2266645Ssam 2276645Ssam rmtgetb() 2286645Ssam { 2296645Ssam char c; 2306645Ssam 2316645Ssam if (read(rmtape, &c, 1) != 1) 2326645Ssam rmtconnaborted(); 2336645Ssam return (c); 2346645Ssam } 2356645Ssam 2366645Ssam rmtgets(cp, len) 2376645Ssam char *cp; 2386645Ssam int len; 2396645Ssam { 2406645Ssam 2416645Ssam while (len > 1) { 2426645Ssam *cp = rmtgetb(); 2436645Ssam if (*cp == '\n') { 2446645Ssam cp[1] = 0; 2456645Ssam return; 2466645Ssam } 2476645Ssam cp++; 2486645Ssam len--; 2496645Ssam } 2506645Ssam msg("Protocol to remote tape server botched (in rmtgets).\n"); 2516645Ssam rmtconnaborted(); 2526645Ssam } 253