dumprmt.c revision 1.15 1 /* $NetBSD: dumprmt.c,v 1.15 1997/05/27 08:35:27 mrg 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.15 1997/05/27 08:35:27 mrg 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
133 if (sp == NULL) {
134 sp = getservbyname("shell", "tcp");
135 if (sp == NULL)
136 errx(1, "shell/tcp: unknown service");
137 pwd = getpwuid(getuid());
138 if (pwd == NULL)
139 errx(1, "who are you?");
140 }
141 if ((cp = strchr(rmtpeer, '@')) != NULL) {
142 tuser = rmtpeer;
143 *cp = '\0';
144 if (!okname(tuser))
145 exit(1);
146 rmtpeer = ++cp;
147 } else
148 tuser = pwd->pw_name;
149
150 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name, tuser,
151 _PATH_RMT, (int *)0);
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
166 static int
167 okname(cp0)
168 char *cp0;
169 {
170 char *cp;
171 int c;
172
173 for (cp = cp0; *cp; cp++) {
174 c = *cp;
175 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
176 warnx("invalid user name: %s", cp0);
177 return (0);
178 }
179 }
180 return (1);
181 }
182
183 int
184 rmtopen(tape, mode)
185 char *tape;
186 int mode;
187 {
188 char buf[256];
189
190 (void)snprintf(buf, sizeof buf, "O%s\n%d\n", tape, mode);
191 rmtstate = TS_OPEN;
192 return (rmtcall(tape, buf));
193 }
194
195 void
196 rmtclose()
197 {
198
199 if (rmtstate != TS_OPEN)
200 return;
201 rmtcall("close", "C\n");
202 rmtstate = TS_CLOSED;
203 }
204
205 int
206 rmtread(buf, count)
207 char *buf;
208 int count;
209 {
210 char line[30];
211 int n, i, cc;
212 extern errno;
213
214 (void)snprintf(line, sizeof line, "R%d\n", count);
215 n = rmtcall("read", line);
216 if (n < 0) {
217 errno = n;
218 return (-1);
219 }
220 for (i = 0; i < n; i += cc) {
221 cc = read(rmtape, buf+i, n - i);
222 if (cc <= 0) {
223 rmtconnaborted();
224 }
225 }
226 return (n);
227 }
228
229 int
230 rmtwrite(buf, count)
231 char *buf;
232 int count;
233 {
234 char line[30];
235
236 (void)snprintf(line, sizeof line, "W%d\n", count);
237 write(rmtape, line, strlen(line));
238 write(rmtape, buf, count);
239 return (rmtreply("write"));
240 }
241
242 void
243 rmtwrite0(count)
244 int count;
245 {
246 char line[30];
247
248 (void)snprintf(line, sizeof line, "W%d\n", count);
249 write(rmtape, line, strlen(line));
250 }
251
252 void
253 rmtwrite1(buf, count)
254 char *buf;
255 int count;
256 {
257
258 write(rmtape, buf, count);
259 }
260
261 int
262 rmtwrite2()
263 {
264
265 return (rmtreply("write"));
266 }
267
268 int
269 rmtseek(offset, pos)
270 int offset, pos;
271 {
272 char line[80];
273
274 (void)snprintf(line, sizeof line, "L%d\n%d\n", offset, pos);
275 return (rmtcall("seek", line));
276 }
277
278 struct mtget mts;
279
280 struct mtget *
281 rmtstatus()
282 {
283 int i;
284 char *cp;
285
286 if (rmtstate != TS_OPEN)
287 return (NULL);
288 rmtcall("status", "S\n");
289 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
290 *cp++ = rmtgetb();
291 return (&mts);
292 }
293
294 int
295 rmtioctl(cmd, count)
296 int cmd, count;
297 {
298 char buf[256];
299
300 if (count < 0)
301 return (-1);
302 (void)snprintf(buf, sizeof buf, "I%d\n%d\n", cmd, count);
303 return (rmtcall("ioctl", buf));
304 }
305
306 static int
307 rmtcall(cmd, buf)
308 char *cmd, *buf;
309 {
310
311 if (write(rmtape, buf, strlen(buf)) != strlen(buf))
312 rmtconnaborted();
313 return (rmtreply(cmd));
314 }
315
316 static int
317 rmtreply(cmd)
318 char *cmd;
319 {
320 char *cp;
321 char code[30], emsg[BUFSIZ];
322
323 rmtgets(code, sizeof (code));
324 if (*code == 'E' || *code == 'F') {
325 rmtgets(emsg, sizeof (emsg));
326 msg("%s: %s", cmd, emsg);
327 if (*code == 'F') {
328 rmtstate = TS_CLOSED;
329 return (-1);
330 }
331 return (-1);
332 }
333 if (*code != 'A') {
334 /* Kill trailing newline */
335 cp = code + strlen(code);
336 if (cp > code && *--cp == '\n')
337 *cp = '\0';
338
339 msg("Protocol to remote tape server botched (code \"%s\").\n",
340 code);
341 rmtconnaborted();
342 }
343 return (atoi(code + 1));
344 }
345
346 int
347 rmtgetb()
348 {
349 char c;
350
351 if (read(rmtape, &c, 1) != 1)
352 rmtconnaborted();
353 return (c);
354 }
355
356 /* Get a line (guaranteed to have a trailing newline). */
357 void
358 rmtgets(line, len)
359 char *line;
360 int len;
361 {
362 char *cp = line;
363
364 while (len > 1) {
365 *cp = rmtgetb();
366 if (*cp == '\n') {
367 cp[1] = '\0';
368 return;
369 }
370 cp++;
371 len--;
372 }
373 *cp = '\0';
374 msg("Protocol to remote tape server botched.\n");
375 msg("(rmtgets got \"%s\").\n", line);
376 rmtconnaborted();
377 }
378