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