| 1 | static char *sccsid = "@(#)dumprmt.c 1.4 (Berkeley) %G%"; |
| 2 | |
| 3 | #include <sys/param.h> |
| 4 | #include <sys/mtio.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | |
| 7 | #include <netinet/in.h> |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <netdb.h> |
| 11 | |
| 12 | #define TS_CLOSED 0 |
| 13 | #define TS_OPEN 1 |
| 14 | |
| 15 | static int rmtstate = TS_CLOSED; |
| 16 | int rmtape; |
| 17 | int rmtconnaborted(); |
| 18 | char *rmtpeer; |
| 19 | |
| 20 | rmthost(host) |
| 21 | char *host; |
| 22 | { |
| 23 | |
| 24 | rmtpeer = host; |
| 25 | sigset(SIGPIPE, rmtconnaborted); |
| 26 | rmtgetconn(); |
| 27 | if (rmtape < 0) |
| 28 | exit(1); |
| 29 | } |
| 30 | |
| 31 | rmtconnaborted() |
| 32 | { |
| 33 | |
| 34 | fprintf(stderr, "Lost connection to remote host.\n"); |
| 35 | exit(1); |
| 36 | } |
| 37 | |
| 38 | rmtgetconn() |
| 39 | { |
| 40 | static struct servent *sp = 0; |
| 41 | |
| 42 | if (sp == 0) { |
| 43 | sp = getservbyname("shell", "tcp"); |
| 44 | if (sp == 0) { |
| 45 | fprintf(stderr, "rdump: shell/tcp: unknown service\n"); |
| 46 | exit(1); |
| 47 | } |
| 48 | } |
| 49 | rmtape = rcmd(&rmtpeer, sp->s_port, "root", "root", "/etc/rmt", 0); |
| 50 | } |
| 51 | |
| 52 | rmtopen(tape, mode) |
| 53 | char *tape; |
| 54 | int mode; |
| 55 | { |
| 56 | char buf[256]; |
| 57 | |
| 58 | sprintf(buf, "O%s\n%d\n", tape, mode); |
| 59 | rmtcall(tape, buf); |
| 60 | rmtstate = TS_OPEN; |
| 61 | } |
| 62 | |
| 63 | rmtclose() |
| 64 | { |
| 65 | |
| 66 | if (rmtstate != TS_OPEN) |
| 67 | return; |
| 68 | rmtcall("close", "C\n"); |
| 69 | rmtstate = TS_CLOSED; |
| 70 | } |
| 71 | |
| 72 | rmtread(buf, count) |
| 73 | char *buf; |
| 74 | int count; |
| 75 | { |
| 76 | char line[30]; |
| 77 | int n, i, cc; |
| 78 | extern errno; |
| 79 | |
| 80 | sprintf(line, "R%d\n", count); |
| 81 | n = rmtcall("read", line); |
| 82 | if (n < 0) { |
| 83 | errno = n; |
| 84 | return (-1); |
| 85 | } |
| 86 | for (i = 0; i < n; i += cc) { |
| 87 | cc = read(rmtape, buf+i, n - i); |
| 88 | if (cc <= 0) { |
| 89 | rmtconnaborted(); |
| 90 | } |
| 91 | } |
| 92 | return (n); |
| 93 | } |
| 94 | |
| 95 | rmtwrite(buf, count) |
| 96 | char *buf; |
| 97 | int count; |
| 98 | { |
| 99 | char line[30]; |
| 100 | |
| 101 | sprintf(line, "W%d\n", count); |
| 102 | write(rmtape, line, strlen(line)); |
| 103 | write(rmtape, buf, count); |
| 104 | return (rmtreply("write")); |
| 105 | } |
| 106 | |
| 107 | rmtwrite0(count) |
| 108 | int count; |
| 109 | { |
| 110 | char line[30]; |
| 111 | |
| 112 | sprintf(line, "W%d\n", count); |
| 113 | write(rmtape, line, strlen(line)); |
| 114 | } |
| 115 | |
| 116 | rmtwrite1(buf, count) |
| 117 | char *buf; |
| 118 | int count; |
| 119 | { |
| 120 | |
| 121 | write(rmtape, buf, count); |
| 122 | } |
| 123 | |
| 124 | rmtwrite2() |
| 125 | { |
| 126 | int i; |
| 127 | |
| 128 | return (rmtreply("write")); |
| 129 | } |
| 130 | |
| 131 | rmtseek(offset, pos) |
| 132 | int offset, pos; |
| 133 | { |
| 134 | char line[80]; |
| 135 | |
| 136 | sprintf(line, "L%d\n%d\n", offset, pos); |
| 137 | return (rmtcall("seek", line)); |
| 138 | } |
| 139 | |
| 140 | struct mtget mts; |
| 141 | |
| 142 | struct mtget * |
| 143 | rmtstatus() |
| 144 | { |
| 145 | register int i; |
| 146 | register char *cp; |
| 147 | |
| 148 | if (rmtstate != TS_OPEN) |
| 149 | return (0); |
| 150 | rmtcall("status", "S\n"); |
| 151 | for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++) |
| 152 | *cp++ = rmtgetb(); |
| 153 | return (&mts); |
| 154 | } |
| 155 | |
| 156 | rmtioctl(cmd, count) |
| 157 | int cmd, count; |
| 158 | { |
| 159 | char buf[256]; |
| 160 | |
| 161 | if (count < 0) |
| 162 | return (-1); |
| 163 | sprintf(buf, "I%d\n%d\n", cmd, count); |
| 164 | rmtcall("ioctl", buf); |
| 165 | } |
| 166 | |
| 167 | rmtcall(cmd, buf) |
| 168 | char *cmd, *buf; |
| 169 | { |
| 170 | |
| 171 | if (write(rmtape, buf, strlen(buf)) != strlen(buf)) |
| 172 | rmtconnaborted(); |
| 173 | return (rmtreply(cmd)); |
| 174 | } |
| 175 | |
| 176 | rmtreply(cmd) |
| 177 | char *cmd; |
| 178 | { |
| 179 | register int c; |
| 180 | char code[30], emsg[BUFSIZ]; |
| 181 | |
| 182 | rmtgets(code, sizeof (code)); |
| 183 | if (*code == 'E' || *code == 'F') { |
| 184 | rmtgets(emsg, sizeof (emsg)); |
| 185 | msg("%s: %s\n", cmd, emsg, code + 1); |
| 186 | if (*code == 'F') { |
| 187 | rmtstate = TS_CLOSED; |
| 188 | return (-1); |
| 189 | } |
| 190 | return (-1); |
| 191 | } |
| 192 | if (*code != 'A') { |
| 193 | msg("Protocol to remote tape server botched (code %s?).\n", |
| 194 | code); |
| 195 | rmtconnaborted(); |
| 196 | } |
| 197 | return (atoi(code + 1)); |
| 198 | } |
| 199 | |
| 200 | rmtgetb() |
| 201 | { |
| 202 | char c; |
| 203 | |
| 204 | if (read(rmtape, &c, 1) != 1) |
| 205 | rmtconnaborted(); |
| 206 | return (c); |
| 207 | } |
| 208 | |
| 209 | rmtgets(cp, len) |
| 210 | char *cp; |
| 211 | int len; |
| 212 | { |
| 213 | |
| 214 | while (len > 1) { |
| 215 | *cp = rmtgetb(); |
| 216 | if (*cp == '\n') { |
| 217 | cp[1] = 0; |
| 218 | return; |
| 219 | } |
| 220 | cp++; |
| 221 | len--; |
| 222 | } |
| 223 | msg("Protocol to remote tape server botched (in rmtgets).\n"); |
| 224 | rmtconnaborted(); |
| 225 | } |