xref: /freebsd/sbin/dump/dumprmt.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
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 static char sccsid[] = "@(#)dumprmt.c	8.3 (Berkeley) 4/28/95";
36 #endif /* not lint */
37 
38 #include <sys/param.h>
39 #include <sys/mtio.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #ifdef sunos
44 #include <sys/vnode.h>
45 
46 #include <ufs/inode.h>
47 #else
48 #include <ufs/ufs/dinode.h>
49 #endif
50 
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 
55 #include <protocols/dumprestore.h>
56 
57 #include <ctype.h>
58 #include <err.h>
59 #include <netdb.h>
60 #include <pwd.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #ifdef __STDC__
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 #endif
68 
69 #include "pathnames.h"
70 #include "dump.h"
71 
72 #define	TS_CLOSED	0
73 #define	TS_OPEN		1
74 
75 static	int rmtstate = TS_CLOSED;
76 static	int rmtape;
77 static	char *rmtpeer;
78 
79 static	int okname __P((char *));
80 static	int rmtcall __P((char *, char *));
81 static	void rmtconnaborted __P((/* int, int */));
82 static	int rmtgetb __P((void));
83 static	void rmtgetconn __P((void));
84 static	void rmtgets __P((char *, int));
85 static	int rmtreply __P((char *));
86 #ifdef KERBEROS
87 int	krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
88 #endif
89 
90 static	int errfd = -1;
91 extern	int dokerberos;
92 extern	int ntrec;		/* blocking factor on tape */
93 
94 int
95 rmthost(host)
96 	char *host;
97 {
98 
99 	rmtpeer = malloc(strlen(host) + 1);
100 	if (rmtpeer)
101 		strcpy(rmtpeer, host);
102 	else
103 		rmtpeer = host;
104 	signal(SIGPIPE, rmtconnaborted);
105 	rmtgetconn();
106 	if (rmtape < 0)
107 		return (0);
108 	return (1);
109 }
110 
111 static void
112 rmtconnaborted()
113 {
114 	msg("Lost connection to remote host.\n");
115 	if (errfd != -1) {
116 		fd_set r;
117 		struct timeval t;
118 
119 		FD_ZERO(&r);
120 		FD_SET(errfd, &r);
121 		t.tv_sec = 0;
122 		t.tv_usec = 0;
123 		if (select(errfd + 1, &r, NULL, NULL, &t)) {
124 			int i;
125 			char buf[2048];
126 
127 			if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
128 				buf[i] = '\0';
129 				msg("on %s: %s%s", rmtpeer, buf,
130 					buf[i - 1] == '\n' ? "" : "\n");
131 			}
132 		}
133 	}
134 
135 	exit(X_ABORT);
136 }
137 
138 void
139 rmtgetconn()
140 {
141 	register char *cp;
142 	register const char *rmt;
143 	static struct servent *sp = NULL;
144 	static struct passwd *pwd = NULL;
145 #ifdef notdef
146 	static int on = 1;
147 #endif
148 	char *tuser;
149 	int size;
150 	int throughput;
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_ABORT);
158 		}
159 		pwd = getpwuid(getuid());
160 		if (pwd == NULL) {
161 			msg("who are you?\n");
162 			exit(X_ABORT);
163 		}
164 	}
165 	if ((cp = strchr(rmtpeer, '@')) != NULL) {
166 		tuser = rmtpeer;
167 		*cp = '\0';
168 		if (!okname(tuser))
169 			exit(X_ABORT);
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 
203 #ifdef notdef
204 	if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
205 		perror("TCP_NODELAY setsockopt");
206 #endif
207 }
208 
209 static int
210 okname(cp0)
211 	char *cp0;
212 {
213 	register char *cp;
214 	register int c;
215 
216 	for (cp = cp0; *cp; cp++) {
217 		c = *cp;
218 		if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
219 			msg("invalid user name %s\n", cp0);
220 			return (0);
221 		}
222 	}
223 	return (1);
224 }
225 
226 int
227 rmtopen(tape, mode)
228 	char *tape;
229 	int mode;
230 {
231 	char buf[256];
232 
233 	(void)snprintf(buf, sizeof (buf), "O%.226s\n%d\n", tape, mode);
234 	rmtstate = TS_OPEN;
235 	return (rmtcall(tape, buf));
236 }
237 
238 void
239 rmtclose()
240 {
241 
242 	if (rmtstate != TS_OPEN)
243 		return;
244 	rmtcall("close", "C\n");
245 	rmtstate = TS_CLOSED;
246 }
247 
248 int
249 rmtread(buf, count)
250 	char *buf;
251 	int count;
252 {
253 	char line[30];
254 	int n, i, cc;
255 	extern errno;
256 
257 	(void)snprintf(line, sizeof (line), "R%d\n", count);
258 	n = rmtcall("read", line);
259 	if (n < 0) {
260 		errno = n;
261 		return (-1);
262 	}
263 	for (i = 0; i < n; i += cc) {
264 		cc = read(rmtape, buf+i, n - i);
265 		if (cc <= 0) {
266 			rmtconnaborted();
267 		}
268 	}
269 	return (n);
270 }
271 
272 int
273 rmtwrite(buf, count)
274 	char *buf;
275 	int count;
276 {
277 	char line[30];
278 
279 	(void)snprintf(line, sizeof (line), "W%d\n", count);
280 	write(rmtape, line, strlen(line));
281 	write(rmtape, buf, count);
282 	return (rmtreply("write"));
283 }
284 
285 void
286 rmtwrite0(count)
287 	int count;
288 {
289 	char line[30];
290 
291 	(void)snprintf(line, sizeof (line), "W%d\n", count);
292 	write(rmtape, line, strlen(line));
293 }
294 
295 void
296 rmtwrite1(buf, count)
297 	char *buf;
298 	int count;
299 {
300 
301 	write(rmtape, buf, count);
302 }
303 
304 int
305 rmtwrite2()
306 {
307 
308 	return (rmtreply("write"));
309 }
310 
311 int
312 rmtseek(offset, pos)
313 	int offset, pos;
314 {
315 	char line[80];
316 
317 	(void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
318 	return (rmtcall("seek", line));
319 }
320 
321 struct	mtget mts;
322 
323 struct mtget *
324 rmtstatus()
325 {
326 	register int i;
327 	register char *cp;
328 
329 	if (rmtstate != TS_OPEN)
330 		return (NULL);
331 	rmtcall("status", "S\n");
332 	for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
333 		*cp++ = rmtgetb();
334 	return (&mts);
335 }
336 
337 int
338 rmtioctl(cmd, count)
339 	int cmd, count;
340 {
341 	char buf[256];
342 
343 	if (count < 0)
344 		return (-1);
345 	(void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
346 	return (rmtcall("ioctl", buf));
347 }
348 
349 static int
350 rmtcall(cmd, buf)
351 	char *cmd, *buf;
352 {
353 
354 	if (write(rmtape, buf, strlen(buf)) != strlen(buf))
355 		rmtconnaborted();
356 	return (rmtreply(cmd));
357 }
358 
359 static int
360 rmtreply(cmd)
361 	char *cmd;
362 {
363 	register char *cp;
364 	char code[30], emsg[BUFSIZ];
365 
366 	rmtgets(code, sizeof (code));
367 	if (*code == 'E' || *code == 'F') {
368 		rmtgets(emsg, sizeof (emsg));
369 		msg("%s: %s", cmd, emsg);
370 		if (*code == 'F') {
371 			rmtstate = TS_CLOSED;
372 			return (-1);
373 		}
374 		return (-1);
375 	}
376 	if (*code != 'A') {
377 		/* Kill trailing newline */
378 		cp = code + strlen(code);
379 		if (cp > code && *--cp == '\n')
380 			*cp = '\0';
381 
382 		msg("Protocol to remote tape server botched (code \"%s\").\n",
383 		    code);
384 		rmtconnaborted();
385 	}
386 	return (atoi(code + 1));
387 }
388 
389 int
390 rmtgetb()
391 {
392 	char c;
393 
394 	if (read(rmtape, &c, 1) != 1)
395 		rmtconnaborted();
396 	return (c);
397 }
398 
399 /* Get a line (guaranteed to have a trailing newline). */
400 void
401 rmtgets(line, len)
402 	char *line;
403 	int len;
404 {
405 	register char *cp = line;
406 
407 	while (len > 1) {
408 		*cp = rmtgetb();
409 		if (*cp == '\n') {
410 			cp[1] = '\0';
411 			return;
412 		}
413 		cp++;
414 		len--;
415 	}
416 	*cp = '\0';
417 	msg("Protocol to remote tape server botched.\n");
418 	msg("(rmtgets got \"%s\").\n", line);
419 	rmtconnaborted();
420 }
421