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