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