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