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