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