xref: /dragonfly/sbin/dump/dumprmt.c (revision c9f721c2)
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  * @(#)dumprmt.c	8.3 (Berkeley) 4/28/95
34  * $FreeBSD: src/sbin/dump/dumprmt.c,v 1.14.2.1 2000/07/01 06:31:52 ps Exp $
35  * $DragonFly: src/sbin/dump/dumprmt.c,v 1.5 2003/11/01 17:15:58 drhodus Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/mtio.h>
40 #include <sys/socket.h>
41 #include <sys/time.h>
42 #ifdef sunos
43 #include <sys/vnode.h>
44 
45 #include <ufs/inode.h>
46 #else
47 #include <vfs/ufs/dinode.h>
48 #endif
49 
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/tcp.h>
54 
55 #include <protocols/dumprestore.h>
56 
57 #include <ctype.h>
58 #include <netdb.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #ifdef __STDC__
62 #include <errno.h>
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 
74 static	int rmtstate = TS_CLOSED;
75 static	int rmtape;
76 static	char *rmtpeer;
77 
78 static	int okname(char *);
79 static	int rmtcall(char *, char *);
80 static	void rmtconnaborted(/* int, int */);
81 static	int rmtgetb(void);
82 static	void rmtgetconn(void);
83 static	void rmtgets(char *, int);
84 static	int rmtreply(char *);
85 #ifdef KERBEROS
86 int	krcmd(char **, int /*u_short*/, char *, char *, int *, char *);
87 #endif
88 
89 static	int errfd = -1;
90 extern	int dokerberos;
91 extern	int ntrec;		/* blocking factor on tape */
92 
93 int
94 rmthost(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 
109 static void
110 rmtconnaborted(void)
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 
136 void
137 rmtgetconn(void)
138 {
139 	register char *cp;
140 	register 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 
203 static int
204 okname(char *cp0)
205 {
206 	register char *cp;
207 	register int c;
208 
209 	for (cp = cp0; *cp; cp++) {
210 		c = *cp;
211 		if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
212 			msg("invalid user name %s\n", cp0);
213 			return (0);
214 		}
215 	}
216 	return (1);
217 }
218 
219 int
220 rmtopen(char *tape, int mode)
221 {
222 	char buf[256];
223 
224 	(void)snprintf(buf, sizeof (buf), "O%.226s\n%d\n", tape, mode);
225 	rmtstate = TS_OPEN;
226 	return (rmtcall(tape, buf));
227 }
228 
229 void
230 rmtclose(void)
231 {
232 
233 	if (rmtstate != TS_OPEN)
234 		return;
235 	rmtcall("close", "C\n");
236 	rmtstate = TS_CLOSED;
237 }
238 
239 int
240 rmtread(char *buf, int count)
241 {
242 	char line[30];
243 	int n, i, cc;
244 
245 	(void)snprintf(line, sizeof (line), "R%d\n", count);
246 	n = rmtcall("read", line);
247 	if (n < 0)
248 		/* rmtcall() properly sets errno for us on errors. */
249 		return (n);
250 	for (i = 0; i < n; i += cc) {
251 		cc = read(rmtape, buf+i, n - i);
252 		if (cc <= 0)
253 			rmtconnaborted();
254 	}
255 	return (n);
256 }
257 
258 int
259 rmtwrite(char *buf, int count)
260 {
261 	char line[30];
262 
263 	(void)snprintf(line, sizeof (line), "W%d\n", count);
264 	write(rmtape, line, strlen(line));
265 	write(rmtape, buf, count);
266 	return (rmtreply("write"));
267 }
268 
269 void
270 rmtwrite0(int count)
271 {
272 	char line[30];
273 
274 	(void)snprintf(line, sizeof (line), "W%d\n", count);
275 	write(rmtape, line, strlen(line));
276 }
277 
278 void
279 rmtwrite1(char *buf, int count)
280 {
281 
282 	write(rmtape, buf, count);
283 }
284 
285 int
286 rmtwrite2(void)
287 {
288 
289 	return (rmtreply("write"));
290 }
291 
292 int
293 rmtseek(int offset, int pos)
294 {
295 	char line[80];
296 
297 	(void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
298 	return (rmtcall("seek", line));
299 }
300 
301 struct	mtget mts;
302 
303 struct mtget *
304 rmtstatus(void)
305 {
306 	register int i;
307 	register char *cp;
308 
309 	if (rmtstate != TS_OPEN)
310 		return (NULL);
311 	rmtcall("status", "S\n");
312 	for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
313 		*cp++ = rmtgetb();
314 	return (&mts);
315 }
316 
317 int
318 rmtioctl(int cmd, int count)
319 {
320 	char buf[256];
321 
322 	if (count < 0)
323 		return (-1);
324 	(void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
325 	return (rmtcall("ioctl", buf));
326 }
327 
328 static int
329 rmtcall(char *cmd, char *buf)
330 {
331 
332 	if (write(rmtape, buf, strlen(buf)) != strlen(buf))
333 		rmtconnaborted();
334 	return (rmtreply(cmd));
335 }
336 
337 static int
338 rmtreply(char *cmd)
339 {
340 	register char *cp;
341 	char code[30], emsg[BUFSIZ];
342 
343 	rmtgets(code, sizeof (code));
344 	if (*code == 'E' || *code == 'F') {
345 		rmtgets(emsg, sizeof (emsg));
346 		msg("%s: %s", cmd, emsg);
347 		errno = atoi(code + 1);
348 		if (*code == 'F')
349 			rmtstate = TS_CLOSED;
350 		return (-1);
351 	}
352 	if (*code != 'A') {
353 		/* Kill trailing newline */
354 		cp = code + strlen(code);
355 		if (cp > code && *--cp == '\n')
356 			*cp = '\0';
357 
358 		msg("Protocol to remote tape server botched (code \"%s\").\n",
359 		    code);
360 		rmtconnaborted();
361 	}
362 	return (atoi(code + 1));
363 }
364 
365 int
366 rmtgetb(void)
367 {
368 	char c;
369 
370 	if (read(rmtape, &c, 1) != 1)
371 		rmtconnaborted();
372 	return (c);
373 }
374 
375 /* Get a line (guaranteed to have a trailing newline). */
376 void
377 rmtgets(char *line, int len)
378 {
379 	register char *cp = line;
380 
381 	while (len > 1) {
382 		*cp = rmtgetb();
383 		if (*cp == '\n') {
384 			cp[1] = '\0';
385 			return;
386 		}
387 		cp++;
388 		len--;
389 	}
390 	*cp = '\0';
391 	msg("Protocol to remote tape server botched.\n");
392 	msg("(rmtgets got \"%s\").\n", line);
393 	rmtconnaborted();
394 }
395