xref: /freebsd/sbin/dump/dumprmt.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
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 	"$Id: dumprmt.c,v 1.11 1998/06/15 06:58:09 charnier Exp $";
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 <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #endif
70 
71 #include "pathnames.h"
72 #include "dump.h"
73 
74 #define	TS_CLOSED	0
75 #define	TS_OPEN		1
76 
77 static	int rmtstate = TS_CLOSED;
78 static	int rmtape;
79 static	char *rmtpeer;
80 
81 static	int okname __P((char *));
82 static	int rmtcall __P((char *, char *));
83 static	void rmtconnaborted __P((/* int, int */));
84 static	int rmtgetb __P((void));
85 static	void rmtgetconn __P((void));
86 static	void rmtgets __P((char *, int));
87 static	int rmtreply __P((char *));
88 #ifdef KERBEROS
89 int	krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
90 #endif
91 
92 static	int errfd = -1;
93 extern	int dokerberos;
94 extern	int ntrec;		/* blocking factor on tape */
95 
96 int
97 rmthost(host)
98 	char *host;
99 {
100 
101 	rmtpeer = malloc(strlen(host) + 1);
102 	if (rmtpeer)
103 		strcpy(rmtpeer, host);
104 	else
105 		rmtpeer = host;
106 	signal(SIGPIPE, rmtconnaborted);
107 	rmtgetconn();
108 	if (rmtape < 0)
109 		return (0);
110 	return (1);
111 }
112 
113 static void
114 rmtconnaborted()
115 {
116 	msg("Lost connection to remote host.\n");
117 	if (errfd != -1) {
118 		fd_set r;
119 		struct timeval t;
120 
121 		FD_ZERO(&r);
122 		FD_SET(errfd, &r);
123 		t.tv_sec = 0;
124 		t.tv_usec = 0;
125 		if (select(errfd + 1, &r, NULL, NULL, &t)) {
126 			int i;
127 			char buf[2048];
128 
129 			if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
130 				buf[i] = '\0';
131 				msg("on %s: %s%s", rmtpeer, buf,
132 					buf[i - 1] == '\n' ? "" : "\n");
133 			}
134 		}
135 	}
136 
137 	exit(X_ABORT);
138 }
139 
140 void
141 rmtgetconn()
142 {
143 	register char *cp;
144 	register const char *rmt;
145 	static struct servent *sp = NULL;
146 	static struct passwd *pwd = NULL;
147 	char *tuser;
148 	int size;
149 	int throughput;
150 	int on;
151 
152 	if (sp == NULL) {
153 		sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
154 		if (sp == NULL) {
155 			msg("%s/tcp: unknown service\n",
156 			    dokerberos ? "kshell" : "shell");
157 			exit(X_STARTUP);
158 		}
159 		pwd = getpwuid(getuid());
160 		if (pwd == NULL) {
161 			msg("who are you?\n");
162 			exit(X_STARTUP);
163 		}
164 	}
165 	if ((cp = strchr(rmtpeer, '@')) != NULL) {
166 		tuser = rmtpeer;
167 		*cp = '\0';
168 		if (!okname(tuser))
169 			exit(X_STARTUP);
170 		rmtpeer = ++cp;
171 	} else
172 		tuser = pwd->pw_name;
173 	if ((rmt = getenv("RMT")) == NULL)
174 		rmt = _PATH_RMT;
175 	msg("");
176 #ifdef KERBEROS
177 	if (dokerberos)
178 		rmtape = krcmd(&rmtpeer, sp->s_port, tuser, rmt, &errfd,
179 			       (char *)0);
180 	else
181 #endif
182 		rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
183 			      tuser, rmt, &errfd);
184 	if (rmtape < 0) {
185 		msg("login to %s as %s failed.\n", rmtpeer, tuser);
186 		return;
187 	}
188 	(void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
189 	size = ntrec * TP_BSIZE;
190 	if (size > 60 * 1024)		/* XXX */
191 		size = 60 * 1024;
192 	/* Leave some space for rmt request/response protocol */
193 	size += 2 * 1024;
194 	while (size > TP_BSIZE &&
195 	    setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
196 		    size -= TP_BSIZE;
197 	(void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
198 	throughput = IPTOS_THROUGHPUT;
199 	if (setsockopt(rmtape, IPPROTO_IP, IP_TOS,
200 	    &throughput, sizeof(throughput)) < 0)
201 		perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
202 	on = 1;
203 	if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
204 		perror("TCP_NODELAY setsockopt");
205 }
206 
207 static int
208 okname(cp0)
209 	char *cp0;
210 {
211 	register char *cp;
212 	register int c;
213 
214 	for (cp = cp0; *cp; cp++) {
215 		c = *cp;
216 		if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
217 			msg("invalid user name %s\n", cp0);
218 			return (0);
219 		}
220 	}
221 	return (1);
222 }
223 
224 int
225 rmtopen(tape, mode)
226 	char *tape;
227 	int mode;
228 {
229 	char buf[256];
230 
231 	(void)snprintf(buf, sizeof (buf), "O%.226s\n%d\n", tape, mode);
232 	rmtstate = TS_OPEN;
233 	return (rmtcall(tape, buf));
234 }
235 
236 void
237 rmtclose()
238 {
239 
240 	if (rmtstate != TS_OPEN)
241 		return;
242 	rmtcall("close", "C\n");
243 	rmtstate = TS_CLOSED;
244 }
245 
246 int
247 rmtread(buf, count)
248 	char *buf;
249 	int count;
250 {
251 	char line[30];
252 	int n, i, cc;
253 	extern errno;
254 
255 	(void)snprintf(line, sizeof (line), "R%d\n", count);
256 	n = rmtcall("read", line);
257 	if (n < 0) {
258 		errno = n;
259 		return (-1);
260 	}
261 	for (i = 0; i < n; i += cc) {
262 		cc = read(rmtape, buf+i, n - i);
263 		if (cc <= 0) {
264 			rmtconnaborted();
265 		}
266 	}
267 	return (n);
268 }
269 
270 int
271 rmtwrite(buf, count)
272 	char *buf;
273 	int count;
274 {
275 	char line[30];
276 
277 	(void)snprintf(line, sizeof (line), "W%d\n", count);
278 	write(rmtape, line, strlen(line));
279 	write(rmtape, buf, count);
280 	return (rmtreply("write"));
281 }
282 
283 void
284 rmtwrite0(count)
285 	int count;
286 {
287 	char line[30];
288 
289 	(void)snprintf(line, sizeof (line), "W%d\n", count);
290 	write(rmtape, line, strlen(line));
291 }
292 
293 void
294 rmtwrite1(buf, count)
295 	char *buf;
296 	int count;
297 {
298 
299 	write(rmtape, buf, count);
300 }
301 
302 int
303 rmtwrite2()
304 {
305 
306 	return (rmtreply("write"));
307 }
308 
309 int
310 rmtseek(offset, pos)
311 	int offset, pos;
312 {
313 	char line[80];
314 
315 	(void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
316 	return (rmtcall("seek", line));
317 }
318 
319 struct	mtget mts;
320 
321 struct mtget *
322 rmtstatus()
323 {
324 	register int i;
325 	register char *cp;
326 
327 	if (rmtstate != TS_OPEN)
328 		return (NULL);
329 	rmtcall("status", "S\n");
330 	for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
331 		*cp++ = rmtgetb();
332 	return (&mts);
333 }
334 
335 int
336 rmtioctl(cmd, count)
337 	int cmd, count;
338 {
339 	char buf[256];
340 
341 	if (count < 0)
342 		return (-1);
343 	(void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
344 	return (rmtcall("ioctl", buf));
345 }
346 
347 static int
348 rmtcall(cmd, buf)
349 	char *cmd, *buf;
350 {
351 
352 	if (write(rmtape, buf, strlen(buf)) != strlen(buf))
353 		rmtconnaborted();
354 	return (rmtreply(cmd));
355 }
356 
357 static int
358 rmtreply(cmd)
359 	char *cmd;
360 {
361 	register char *cp;
362 	char code[30], emsg[BUFSIZ];
363 
364 	rmtgets(code, sizeof (code));
365 	if (*code == 'E' || *code == 'F') {
366 		rmtgets(emsg, sizeof (emsg));
367 		msg("%s: %s", cmd, emsg);
368 		if (*code == 'F') {
369 			rmtstate = TS_CLOSED;
370 			return (-1);
371 		}
372 		return (-1);
373 	}
374 	if (*code != 'A') {
375 		/* Kill trailing newline */
376 		cp = code + strlen(code);
377 		if (cp > code && *--cp == '\n')
378 			*cp = '\0';
379 
380 		msg("Protocol to remote tape server botched (code \"%s\").\n",
381 		    code);
382 		rmtconnaborted();
383 	}
384 	return (atoi(code + 1));
385 }
386 
387 int
388 rmtgetb()
389 {
390 	char c;
391 
392 	if (read(rmtape, &c, 1) != 1)
393 		rmtconnaborted();
394 	return (c);
395 }
396 
397 /* Get a line (guaranteed to have a trailing newline). */
398 void
399 rmtgets(line, len)
400 	char *line;
401 	int len;
402 {
403 	register char *cp = line;
404 
405 	while (len > 1) {
406 		*cp = rmtgetb();
407 		if (*cp == '\n') {
408 			cp[1] = '\0';
409 			return;
410 		}
411 		cp++;
412 		len--;
413 	}
414 	*cp = '\0';
415 	msg("Protocol to remote tape server botched.\n");
416 	msg("(rmtgets got \"%s\").\n", line);
417 	rmtconnaborted();
418 }
419