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