dumprmt.c revision 1.24 1 /* $NetBSD: dumprmt.c,v 1.24 2001/05/27 14:17:56 lukem 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.24 2001/05/27 14:17:56 lukem 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(char *);
89 static int rmtcall(char *, char *);
90 static void rmtconnaborted(int);
91 static int rmtgetb(void);
92 static void rmtgetconn(void);
93 static void rmtgets(char *, int);
94 int rmtioctl(int, int);
95 int rmtread(char *, int);
96 static int rmtreply(char *);
97 int rmtseek(int, int);
98
99 extern int ntrec; /* blocking factor on tape */
100
101 int
102 rmthost(char *host)
103 {
104
105 if ((rmtpeer = strdup(host)) == NULL)
106 err(1, "strdup");
107 signal(SIGPIPE, rmtconnaborted);
108 rmtgetconn();
109 if (rmtape < 0)
110 return (0);
111 return (1);
112 }
113
114 static void
115 rmtconnaborted(int dummy)
116 {
117
118 errx(1, "Lost connection to remote host.");
119 }
120
121 void
122 rmtgetconn(void)
123 {
124 char *cp;
125 static struct servent *sp = NULL;
126 static struct passwd *pwd = NULL;
127 char *tuser, *name;
128 int size, opt;
129
130 if (sp == NULL) {
131 sp = getservbyname("shell", "tcp");
132 if (sp == NULL)
133 errx(1, "shell/tcp: unknown service");
134 pwd = getpwuid(getuid());
135 if (pwd == NULL)
136 errx(1, "who are you?");
137 }
138 if ((name = strdup(pwd->pw_name)) == NULL)
139 err(1, "strdup");
140 if ((cp = strchr(rmtpeer, '@')) != NULL) {
141 tuser = rmtpeer;
142 *cp = '\0';
143 if (!okname(tuser))
144 exit(1);
145 rmtpeer = ++cp;
146 } else
147 tuser = name;
148
149 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, tuser, _PATH_RMT,
150 (int *)0);
151 (void)free(name);
152 if (rmtape < 0)
153 return;
154
155 size = ntrec * TP_BSIZE;
156 if (size > 60 * 1024) /* XXX */
157 size = 60 * 1024;
158 /* Leave some space for rmt request/response protocol */
159 size += 2 * 1024;
160 while (size > TP_BSIZE &&
161 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
162 size -= TP_BSIZE;
163 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
164
165 opt = IPTOS_THROUGHPUT;
166 (void)setsockopt(rmtape, IPPROTO_IP, IP_TOS, &opt, sizeof (opt));
167
168 opt = 1;
169 (void)setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof (opt));
170 }
171
172 static int
173 okname(char *cp0)
174 {
175 char *cp;
176 int c;
177
178 for (cp = cp0; *cp; cp++) {
179 c = *cp;
180 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
181 warnx("invalid user name: %s", cp0);
182 return (0);
183 }
184 }
185 return (1);
186 }
187
188 int
189 rmtopen(char *tape, int mode)
190 {
191 char buf[256];
192
193 (void)snprintf(buf, sizeof buf, "O%s\n%d\n", tape, mode);
194 rmtstate = TS_OPEN;
195 return (rmtcall(tape, buf));
196 }
197
198 void
199 rmtclose(void)
200 {
201
202 if (rmtstate != TS_OPEN)
203 return;
204 rmtcall("close", "C\n");
205 rmtstate = TS_CLOSED;
206 }
207
208 int
209 rmtread(char *buf, int count)
210 {
211 char line[30];
212 int n, i, cc;
213
214 (void)snprintf(line, sizeof line, "R%d\n", count);
215 n = rmtcall("read", line);
216 if (n < 0) {
217 errno = n;
218 return (-1);
219 }
220 for (i = 0; i < n; i += cc) {
221 cc = read(rmtape, buf+i, n - i);
222 if (cc <= 0) {
223 rmtconnaborted(0);
224 }
225 }
226 return (n);
227 }
228
229 int
230 rmtwrite(char *buf, int count)
231 {
232 char line[30];
233
234 (void)snprintf(line, sizeof line, "W%d\n", count);
235 write(rmtape, line, strlen(line));
236 write(rmtape, buf, count);
237 return (rmtreply("write"));
238 }
239
240 #if 0 /* XXX unused? */
241 void
242 rmtwrite0(int count)
243 {
244 char line[30];
245
246 (void)snprintf(line, sizeof line, "W%d\n", count);
247 write(rmtape, line, strlen(line));
248 }
249
250 void
251 rmtwrite1(char *buf, int count)
252 {
253
254 write(rmtape, buf, count);
255 }
256
257 int
258 rmtwrite2(void)
259 {
260
261 return (rmtreply("write"));
262 }
263 #endif
264
265 int
266 rmtseek(int offset, int pos)
267 {
268 char line[80];
269
270 (void)snprintf(line, sizeof line, "L%d\n%d\n", offset, pos);
271 return (rmtcall("seek", line));
272 }
273
274
275 #if 0 /* XXX unused? */
276 struct mtget *
277 rmtstatus(void)
278 {
279 struct mtget mts;
280 int i;
281 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 #endif
291
292 int
293 rmtioctl(int cmd, int count)
294 {
295 char buf[256];
296
297 if (count < 0)
298 return (-1);
299 (void)snprintf(buf, sizeof buf, "I%d\n%d\n", cmd, count);
300 return (rmtcall("ioctl", buf));
301 }
302
303 static int
304 rmtcall(char *cmd, char *buf)
305 {
306
307 if (write(rmtape, buf, strlen(buf)) != strlen(buf))
308 rmtconnaborted(0);
309 return (rmtreply(cmd));
310 }
311
312 static int
313 rmtreply(char *cmd)
314 {
315 char *cp;
316 char code[30], emsg[BUFSIZ];
317
318 rmtgets(code, sizeof (code));
319 if (*code == 'E' || *code == 'F') {
320 rmtgets(emsg, sizeof (emsg));
321 msg("%s: %s", cmd, emsg);
322 if (*code == 'F') {
323 rmtstate = TS_CLOSED;
324 return (-1);
325 }
326 return (-1);
327 }
328 if (*code != 'A') {
329 /* Kill trailing newline */
330 cp = code + strlen(code);
331 if (cp > code && *--cp == '\n')
332 *cp = '\0';
333
334 msg("Protocol to remote tape server botched (code \"%s\").\n",
335 code);
336 rmtconnaborted(0);
337 }
338 return (atoi(code + 1));
339 }
340
341 int
342 rmtgetb(void)
343 {
344 char c;
345
346 if (read(rmtape, &c, 1) != 1)
347 rmtconnaborted(0);
348 return (c);
349 }
350
351 /* Get a line (guaranteed to have a trailing newline). */
352 void
353 rmtgets(char *line, int len)
354 {
355 char *cp = line;
356
357 while (len > 1) {
358 *cp = rmtgetb();
359 if (*cp == '\n') {
360 cp[1] = '\0';
361 return;
362 }
363 cp++;
364 len--;
365 }
366 *cp = '\0';
367 msg("Protocol to remote tape server botched.\n");
368 msg("(rmtgets got \"%s\").\n", line);
369 rmtconnaborted(0);
370 }
371