1/*	$NetBSD: dumprmt.c,v 1.33 2009/04/11 07:55:35 lukem Exp $	*/
2
3/*-
4 * Copyright (c) 1980, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)dumprmt.c	8.3 (Berkeley) 4/28/95";
36#else
37__RCSID("$NetBSD: dumprmt.c,v 1.33 2009/04/11 07:55:35 lukem Exp $");
38#endif
39#endif /* not lint */
40
41#include <sys/param.h>
42#include <sys/mtio.h>
43#include <sys/ioctl.h>
44#include <sys/socket.h>
45#include <sys/time.h>
46#include <ufs/ufs/dinode.h>
47
48#include <netinet/in.h>
49#include <netinet/in_systm.h>
50#include <netinet/ip.h>
51#include <netinet/tcp.h>
52
53#include <protocols/dumprestore.h>
54
55#include <ctype.h>
56#include <err.h>
57#include <errno.h>
58#include <netdb.h>
59#include <pwd.h>
60#include <signal.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <unistd.h>
65
66#include "pathnames.h"
67#include "dump.h"
68
69#define	TS_CLOSED	0
70#define	TS_OPEN		1
71
72static	int rmtstate = TS_CLOSED;
73static	int rmtape;
74static	char *rmtpeer;
75
76static	int	okname(char *);
77static	int	rmtcall(const char *, const char *, int);
78__dead static	void	rmtconnaborted(int);
79static	int	rmtgetb(void);
80static	void	rmtgetconn(void);
81static	void	rmtgets(char *, int);
82	int	rmtread(char *, int);
83static	int	rmtreply(const char *, int);
84	int	rmtseek(int, int);
85
86extern	int ntrec;		/* blocking factor on tape */
87
88int
89rmthost(const char *host)
90{
91
92	if ((rmtpeer = strdup(host)) == NULL)
93		err(X_STARTUP, "strdup");
94	signal(SIGPIPE, rmtconnaborted);
95	rmtgetconn();
96	if (rmtape < 0)
97		return (0);
98	return (1);
99}
100
101static void
102rmtconnaborted(int dummy __unused)
103{
104
105	errx(X_ABORT, "Lost connection to remote host.");
106}
107
108void
109rmtgetconn(void)
110{
111	char *cp;
112	static struct servent *sp = NULL;
113	static struct passwd *pwd = NULL;
114	char *tuser, *name;
115	int size, opt;
116
117	if (sp == NULL) {
118		sp = getservbyname("shell", "tcp");
119		if (sp == NULL)
120			errx(X_STARTUP, "shell/tcp: unknown service");
121		pwd = getpwuid(getuid());
122		if (pwd == NULL)
123			errx(X_STARTUP, "who are you?");
124	}
125	if ((name = strdup(pwd->pw_name)) == NULL)
126		err(X_STARTUP, "strdup");
127	if ((cp = strchr(rmtpeer, '@')) != NULL) {
128		tuser = rmtpeer;
129		*cp = '\0';
130		if (!okname(tuser))
131			exit(X_STARTUP);
132		rmtpeer = ++cp;
133	} else
134		tuser = name;
135
136	rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, tuser, _PATH_RMT,
137	    (int *)0);
138	(void)free(name);
139	if (rmtape < 0)
140		return;
141
142	size = ntrec * TP_BSIZE;
143	if (size > 60 * 1024)		/* XXX */
144		size = 60 * 1024;
145	/* Leave some space for rmt request/response protocol */
146	size += 2 * 1024;
147	while (size > TP_BSIZE &&
148	    setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
149		    size -= TP_BSIZE;
150	(void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
151
152	opt = IPTOS_THROUGHPUT;
153	(void)setsockopt(rmtape, IPPROTO_IP, IP_TOS, &opt, sizeof (opt));
154
155	opt = 1;
156	(void)setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof (opt));
157}
158
159static int
160okname(char *cp0)
161{
162	char *cp;
163	int c;
164
165	for (cp = cp0; *cp; cp++) {
166		c = *cp;
167		if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
168			warnx("invalid user name: %s", cp0);
169			return (0);
170		}
171	}
172	return (1);
173}
174
175int
176rmtopen(const char *tapedevice, int mode, int verbose)
177{
178	char buf[256];
179
180	(void)snprintf(buf, sizeof buf, "O%s\n%d\n", tapedevice, mode);
181	rmtstate = TS_OPEN;
182	return (rmtcall(tapedevice, buf, verbose));
183}
184
185void
186rmtclose(void)
187{
188
189	if (rmtstate != TS_OPEN)
190		return;
191	rmtcall("close", "C\n", 1);
192	rmtstate = TS_CLOSED;
193}
194
195int
196rmtread(char *buf, int count)
197{
198	char line[30];
199	int n, i, cc;
200
201	(void)snprintf(line, sizeof line, "R%d\n", count);
202	n = rmtcall("read", line, 1);
203	if (n < 0) {
204		/* rmtcall() properly sets errno for us on errors. */
205		return (n);
206	}
207	for (i = 0; i < n; i += cc) {
208		cc = read(rmtape, buf+i, n - i);
209		if (cc <= 0) {
210			rmtconnaborted(0);
211		}
212	}
213	return (n);
214}
215
216int
217rmtwrite(const char *buf, int count)
218{
219	char line[30];
220
221	(void)snprintf(line, sizeof line, "W%d\n", count);
222	write(rmtape, line, strlen(line));
223	write(rmtape, buf, count);
224	return (rmtreply("write", 1));
225}
226
227#if 0		/* XXX unused? */
228void
229rmtwrite0(int count)
230{
231	char line[30];
232
233	(void)snprintf(line, sizeof line, "W%d\n", count);
234	write(rmtape, line, strlen(line));
235}
236
237void
238rmtwrite1(char *buf, int count)
239{
240
241	write(rmtape, buf, count);
242}
243
244int
245rmtwrite2(void)
246{
247
248	return (rmtreply("write", 1));
249}
250#endif
251
252int
253rmtseek(int offset, int pos)
254{
255	char line[80];
256
257	(void)snprintf(line, sizeof line, "L%d\n%d\n", offset, pos);
258	return (rmtcall("seek", line, 1));
259}
260
261
262#if 0		/* XXX unused? */
263struct mtget *
264rmtstatus(void)
265{
266	struct	mtget mts;
267	int i;
268	char *cp;
269
270	if (rmtstate != TS_OPEN)
271		return (NULL);
272	rmtcall("status", "S\n", 1);
273	for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
274		*cp++ = rmtgetb();
275	return (&mts);
276}
277#endif
278
279int
280rmtioctl(int cmd, int count)
281{
282	char buf[256];
283
284	if (count < 0)
285		return (-1);
286	(void)snprintf(buf, sizeof buf, "I%d\n%d\n", cmd, count);
287	return (rmtcall("ioctl", buf, 1));
288}
289
290static int
291rmtcall(const char *cmd, const char *buf, int verbose)
292{
293
294	if ((size_t)write(rmtape, buf, strlen(buf)) != strlen(buf))
295		rmtconnaborted(0);
296	return (rmtreply(cmd, verbose));
297}
298
299static int
300rmtreply(const char *cmd, int verbose)
301{
302	char *cp;
303	char code[30], emsg[BUFSIZ];
304
305	rmtgets(code, sizeof (code));
306	if (*code == 'E' || *code == 'F') {
307		rmtgets(emsg, sizeof (emsg));
308		if (verbose)
309			msg("%s: %s", cmd, emsg);
310		errno = atoi(code + 1);
311		if (*code == 'F')
312			rmtstate = TS_CLOSED;
313		return (-1);
314	}
315	if (*code != 'A') {
316		/* Kill trailing newline */
317		cp = code + strlen(code);
318		if (cp > code && *--cp == '\n')
319			*cp = '\0';
320
321		msg("Protocol to remote tape server botched (code \"%s\").\n",
322		    code);
323		rmtconnaborted(0);
324	}
325	return (atoi(code + 1));
326}
327
328int
329rmtgetb(void)
330{
331	char c;
332
333	if (read(rmtape, &c, 1) != 1)
334		rmtconnaborted(0);
335	return (c);
336}
337
338/* Get a line (guaranteed to have a trailing newline). */
339void
340rmtgets(char *line, int len)
341{
342	char *cp = line;
343
344	while (len > 1) {
345		*cp = rmtgetb();
346		if (*cp == '\n') {
347			cp[1] = '\0';
348			return;
349		}
350		cp++;
351		len--;
352	}
353	*cp = '\0';
354	msg("Protocol to remote tape server botched.\n");
355	msg("(rmtgets got \"%s\").\n", line);
356	rmtconnaborted(0);
357}
358