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