dumprmt.c revision 1.1.1.2 1 /*-
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)dumprmt.c 8.1 (Berkeley) 6/5/93";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <sys/mtio.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #ifdef sunos
44 #include <sys/vnode.h>
45
46 #include <ufs/inode.h>
47 #else
48 #include <ufs/ufs/dinode.h>
49 #endif
50
51 #include <netinet/in.h>
52 #include <netinet/tcp.h>
53
54 #include <protocols/dumprestore.h>
55
56 #include <ctype.h>
57 #include <netdb.h>
58 #include <pwd.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #ifdef __STDC__
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #endif
66
67 #include "pathnames.h"
68 #include "dump.h"
69
70 #define TS_CLOSED 0
71 #define TS_OPEN 1
72
73 static int rmtstate = TS_CLOSED;
74 static int rmtape;
75 static char *rmtpeer;
76
77 static int okname __P((char *));
78 static int rmtcall __P((char *, char *));
79 static void rmtconnaborted __P((/* int, int */));
80 static int rmtgetb __P((void));
81 static void rmtgetconn __P((void));
82 static void rmtgets __P((char *, int));
83 static int rmtreply __P((char *));
84
85 extern int ntrec; /* blocking factor on tape */
86
87 int
88 rmthost(host)
89 char *host;
90 {
91
92 rmtpeer = malloc(strlen(host) + 1);
93 if (rmtpeer)
94 strcpy(rmtpeer, host);
95 else
96 rmtpeer = host;
97 signal(SIGPIPE, rmtconnaborted);
98 rmtgetconn();
99 if (rmtape < 0)
100 return (0);
101 return (1);
102 }
103
104 static void
105 rmtconnaborted()
106 {
107
108 (void) fprintf(stderr, "rdump: Lost connection to remote host.\n");
109 exit(1);
110 }
111
112 void
113 rmtgetconn()
114 {
115 register char *cp;
116 static struct servent *sp = NULL;
117 static struct passwd *pwd = NULL;
118 #ifdef notdef
119 static int on = 1;
120 #endif
121 char *tuser;
122 int size;
123 int maxseg;
124
125 if (sp == NULL) {
126 sp = getservbyname("shell", "tcp");
127 if (sp == NULL) {
128 (void) fprintf(stderr,
129 "rdump: shell/tcp: unknown service\n");
130 exit(1);
131 }
132 pwd = getpwuid(getuid());
133 if (pwd == NULL) {
134 (void) fprintf(stderr, "rdump: who are you?\n");
135 exit(1);
136 }
137 }
138 if ((cp = index(rmtpeer, '@')) != NULL) {
139 tuser = rmtpeer;
140 *cp = '\0';
141 if (!okname(tuser))
142 exit(1);
143 rmtpeer = ++cp;
144 } else
145 tuser = pwd->pw_name;
146 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name, tuser,
147 _PATH_RMT, (int *)0);
148 size = ntrec * TP_BSIZE;
149 if (size > 60 * 1024) /* XXX */
150 size = 60 * 1024;
151 /* Leave some space for rmt request/response protocol */
152 size += 2 * 1024;
153 while (size > TP_BSIZE &&
154 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
155 size -= TP_BSIZE;
156 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
157 maxseg = 1024;
158 if (setsockopt(rmtape, IPPROTO_TCP, TCP_MAXSEG,
159 &maxseg, sizeof (maxseg)) < 0)
160 perror("TCP_MAXSEG setsockopt");
161
162 #ifdef notdef
163 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
164 perror("TCP_NODELAY setsockopt");
165 #endif
166 }
167
168 static int
169 okname(cp0)
170 char *cp0;
171 {
172 register char *cp;
173 register int c;
174
175 for (cp = cp0; *cp; cp++) {
176 c = *cp;
177 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
178 (void) fprintf(stderr, "rdump: invalid user name %s\n",
179 cp0);
180 return (0);
181 }
182 }
183 return (1);
184 }
185
186 int
187 rmtopen(tape, mode)
188 char *tape;
189 int mode;
190 {
191 char buf[256];
192
193 (void)sprintf(buf, "O%s\n%d\n", tape, mode);
194 rmtstate = TS_OPEN;
195 return (rmtcall(tape, buf));
196 }
197
198 void
199 rmtclose()
200 {
201
202 if (rmtstate != TS_OPEN)
203 return;
204 rmtcall("close", "C\n");
205 rmtstate = TS_CLOSED;
206 }
207
208 int
209 rmtread(buf, count)
210 char *buf;
211 int count;
212 {
213 char line[30];
214 int n, i, cc;
215 extern errno;
216
217 (void)sprintf(line, "R%d\n", count);
218 n = rmtcall("read", line);
219 if (n < 0) {
220 errno = n;
221 return (-1);
222 }
223 for (i = 0; i < n; i += cc) {
224 cc = read(rmtape, buf+i, n - i);
225 if (cc <= 0) {
226 rmtconnaborted();
227 }
228 }
229 return (n);
230 }
231
232 int
233 rmtwrite(buf, count)
234 char *buf;
235 int count;
236 {
237 char line[30];
238
239 (void)sprintf(line, "W%d\n", count);
240 write(rmtape, line, strlen(line));
241 write(rmtape, buf, count);
242 return (rmtreply("write"));
243 }
244
245 void
246 rmtwrite0(count)
247 int count;
248 {
249 char line[30];
250
251 (void)sprintf(line, "W%d\n", count);
252 write(rmtape, line, strlen(line));
253 }
254
255 void
256 rmtwrite1(buf, count)
257 char *buf;
258 int count;
259 {
260
261 write(rmtape, buf, count);
262 }
263
264 int
265 rmtwrite2()
266 {
267
268 return (rmtreply("write"));
269 }
270
271 int
272 rmtseek(offset, pos)
273 int offset, pos;
274 {
275 char line[80];
276
277 (void)sprintf(line, "L%d\n%d\n", offset, pos);
278 return (rmtcall("seek", line));
279 }
280
281 struct mtget mts;
282
283 struct mtget *
284 rmtstatus()
285 {
286 register int i;
287 register char *cp;
288
289 if (rmtstate != TS_OPEN)
290 return (NULL);
291 rmtcall("status", "S\n");
292 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
293 *cp++ = rmtgetb();
294 return (&mts);
295 }
296
297 int
298 rmtioctl(cmd, count)
299 int cmd, count;
300 {
301 char buf[256];
302
303 if (count < 0)
304 return (-1);
305 (void)sprintf(buf, "I%d\n%d\n", cmd, count);
306 return (rmtcall("ioctl", buf));
307 }
308
309 static int
310 rmtcall(cmd, buf)
311 char *cmd, *buf;
312 {
313
314 if (write(rmtape, buf, strlen(buf)) != strlen(buf))
315 rmtconnaborted();
316 return (rmtreply(cmd));
317 }
318
319 static int
320 rmtreply(cmd)
321 char *cmd;
322 {
323 register char *cp;
324 char code[30], emsg[BUFSIZ];
325
326 rmtgets(code, sizeof (code));
327 if (*code == 'E' || *code == 'F') {
328 rmtgets(emsg, sizeof (emsg));
329 msg("%s: %s", cmd, emsg);
330 if (*code == 'F') {
331 rmtstate = TS_CLOSED;
332 return (-1);
333 }
334 return (-1);
335 }
336 if (*code != 'A') {
337 /* Kill trailing newline */
338 cp = code + strlen(code);
339 if (cp > code && *--cp == '\n')
340 *cp = '\0';
341
342 msg("Protocol to remote tape server botched (code \"%s\").\n",
343 code);
344 rmtconnaborted();
345 }
346 return (atoi(code + 1));
347 }
348
349 int
350 rmtgetb()
351 {
352 char c;
353
354 if (read(rmtape, &c, 1) != 1)
355 rmtconnaborted();
356 return (c);
357 }
358
359 /* Get a line (guaranteed to have a trailing newline). */
360 void
361 rmtgets(line, len)
362 char *line;
363 int len;
364 {
365 register char *cp = line;
366
367 while (len > 1) {
368 *cp = rmtgetb();
369 if (*cp == '\n') {
370 cp[1] = '\0';
371 return;
372 }
373 cp++;
374 len--;
375 }
376 *cp = '\0';
377 msg("Protocol to remote tape server botched.\n");
378 msg("(rmtgets got \"%s\").\n", line);
379 rmtconnaborted();
380 }
381