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