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