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