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