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