dumprmt.c revision 1.1.1.3 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.3 (Berkeley) 4/28/95";
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 <err.h>
58 #include <netdb.h>
59 #include <pwd.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #ifdef __STDC__
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #endif
67
68 #include "pathnames.h"
69 #include "dump.h"
70
71 #define TS_CLOSED 0
72 #define TS_OPEN 1
73
74 static int rmtstate = TS_CLOSED;
75 static int rmtape;
76 static char *rmtpeer;
77
78 static int okname __P((char *));
79 static int rmtcall __P((char *, char *));
80 static void rmtconnaborted __P((/* int, int */));
81 static int rmtgetb __P((void));
82 static void rmtgetconn __P((void));
83 static void rmtgets __P((char *, int));
84 static int rmtreply __P((char *));
85
86 extern int ntrec; /* blocking factor on tape */
87
88 int
89 rmthost(host)
90 char *host;
91 {
92
93 rmtpeer = malloc(strlen(host) + 1);
94 if (rmtpeer)
95 strcpy(rmtpeer, host);
96 else
97 rmtpeer = host;
98 signal(SIGPIPE, rmtconnaborted);
99 rmtgetconn();
100 if (rmtape < 0)
101 return (0);
102 return (1);
103 }
104
105 static void
106 rmtconnaborted()
107 {
108
109 errx(1, "Lost connection to remote host.");
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 errx(1, "shell/tcp: unknown service");
129 pwd = getpwuid(getuid());
130 if (pwd == NULL)
131 errx(1, "who are you?");
132 }
133 if ((cp = strchr(rmtpeer, '@')) != NULL) {
134 tuser = rmtpeer;
135 *cp = '\0';
136 if (!okname(tuser))
137 exit(1);
138 rmtpeer = ++cp;
139 } else
140 tuser = pwd->pw_name;
141 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name, tuser,
142 _PATH_RMT, (int *)0);
143 size = ntrec * TP_BSIZE;
144 if (size > 60 * 1024) /* XXX */
145 size = 60 * 1024;
146 /* Leave some space for rmt request/response protocol */
147 size += 2 * 1024;
148 while (size > TP_BSIZE &&
149 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
150 size -= TP_BSIZE;
151 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
152 maxseg = 1024;
153 if (setsockopt(rmtape, IPPROTO_TCP, TCP_MAXSEG,
154 &maxseg, sizeof (maxseg)) < 0)
155 perror("TCP_MAXSEG setsockopt");
156
157 #ifdef notdef
158 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
159 perror("TCP_NODELAY setsockopt");
160 #endif
161 }
162
163 static int
164 okname(cp0)
165 char *cp0;
166 {
167 register char *cp;
168 register int c;
169
170 for (cp = cp0; *cp; cp++) {
171 c = *cp;
172 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
173 warnx("invalid user name: %s", cp0);
174 return (0);
175 }
176 }
177 return (1);
178 }
179
180 int
181 rmtopen(tape, mode)
182 char *tape;
183 int mode;
184 {
185 char buf[256];
186
187 (void)sprintf(buf, "O%s\n%d\n", tape, mode);
188 rmtstate = TS_OPEN;
189 return (rmtcall(tape, buf));
190 }
191
192 void
193 rmtclose()
194 {
195
196 if (rmtstate != TS_OPEN)
197 return;
198 rmtcall("close", "C\n");
199 rmtstate = TS_CLOSED;
200 }
201
202 int
203 rmtread(buf, count)
204 char *buf;
205 int count;
206 {
207 char line[30];
208 int n, i, cc;
209 extern errno;
210
211 (void)sprintf(line, "R%d\n", count);
212 n = rmtcall("read", line);
213 if (n < 0) {
214 errno = n;
215 return (-1);
216 }
217 for (i = 0; i < n; i += cc) {
218 cc = read(rmtape, buf+i, n - i);
219 if (cc <= 0) {
220 rmtconnaborted();
221 }
222 }
223 return (n);
224 }
225
226 int
227 rmtwrite(buf, count)
228 char *buf;
229 int count;
230 {
231 char line[30];
232
233 (void)sprintf(line, "W%d\n", count);
234 write(rmtape, line, strlen(line));
235 write(rmtape, buf, count);
236 return (rmtreply("write"));
237 }
238
239 void
240 rmtwrite0(count)
241 int count;
242 {
243 char line[30];
244
245 (void)sprintf(line, "W%d\n", count);
246 write(rmtape, line, strlen(line));
247 }
248
249 void
250 rmtwrite1(buf, count)
251 char *buf;
252 int count;
253 {
254
255 write(rmtape, buf, count);
256 }
257
258 int
259 rmtwrite2()
260 {
261
262 return (rmtreply("write"));
263 }
264
265 int
266 rmtseek(offset, pos)
267 int offset, pos;
268 {
269 char line[80];
270
271 (void)sprintf(line, "L%d\n%d\n", offset, pos);
272 return (rmtcall("seek", line));
273 }
274
275 struct mtget mts;
276
277 struct mtget *
278 rmtstatus()
279 {
280 register int i;
281 register char *cp;
282
283 if (rmtstate != TS_OPEN)
284 return (NULL);
285 rmtcall("status", "S\n");
286 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
287 *cp++ = rmtgetb();
288 return (&mts);
289 }
290
291 int
292 rmtioctl(cmd, count)
293 int cmd, count;
294 {
295 char buf[256];
296
297 if (count < 0)
298 return (-1);
299 (void)sprintf(buf, "I%d\n%d\n", cmd, count);
300 return (rmtcall("ioctl", buf));
301 }
302
303 static int
304 rmtcall(cmd, buf)
305 char *cmd, *buf;
306 {
307
308 if (write(rmtape, buf, strlen(buf)) != strlen(buf))
309 rmtconnaborted();
310 return (rmtreply(cmd));
311 }
312
313 static int
314 rmtreply(cmd)
315 char *cmd;
316 {
317 register char *cp;
318 char code[30], emsg[BUFSIZ];
319
320 rmtgets(code, sizeof (code));
321 if (*code == 'E' || *code == 'F') {
322 rmtgets(emsg, sizeof (emsg));
323 msg("%s: %s", cmd, emsg);
324 if (*code == 'F') {
325 rmtstate = TS_CLOSED;
326 return (-1);
327 }
328 return (-1);
329 }
330 if (*code != 'A') {
331 /* Kill trailing newline */
332 cp = code + strlen(code);
333 if (cp > code && *--cp == '\n')
334 *cp = '\0';
335
336 msg("Protocol to remote tape server botched (code \"%s\").\n",
337 code);
338 rmtconnaborted();
339 }
340 return (atoi(code + 1));
341 }
342
343 int
344 rmtgetb()
345 {
346 char c;
347
348 if (read(rmtape, &c, 1) != 1)
349 rmtconnaborted();
350 return (c);
351 }
352
353 /* Get a line (guaranteed to have a trailing newline). */
354 void
355 rmtgets(line, len)
356 char *line;
357 int len;
358 {
359 register char *cp = line;
360
361 while (len > 1) {
362 *cp = rmtgetb();
363 if (*cp == '\n') {
364 cp[1] = '\0';
365 return;
366 }
367 cp++;
368 len--;
369 }
370 *cp = '\0';
371 msg("Protocol to remote tape server botched.\n");
372 msg("(rmtgets got \"%s\").\n", line);
373 rmtconnaborted();
374 }
375