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 static const char rcsid[] = 37 "$FreeBSD$"; 38 #endif /* not lint */ 39 40 #include <sys/param.h> 41 #include <sys/mtio.h> 42 #include <sys/socket.h> 43 #include <sys/time.h> 44 45 #include <ufs/ufs/dinode.h> 46 47 #include <netinet/in.h> 48 #include <netinet/in_systm.h> 49 #include <netinet/ip.h> 50 #include <netinet/tcp.h> 51 52 #include <protocols/dumprestore.h> 53 54 #include <ctype.h> 55 #include <netdb.h> 56 #include <pwd.h> 57 #include <stdio.h> 58 #include <limits.h> 59 #include <errno.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 #include "pathnames.h" 65 #include "dump.h" 66 67 #define TS_CLOSED 0 68 #define TS_OPEN 1 69 70 static int rmtstate = TS_CLOSED; 71 static int rmtape; 72 static char *rmtpeer; 73 74 static int okname(const char *); 75 static int rmtcall(const char *, const char *); 76 static void rmtconnaborted(int); 77 static int rmtgetb(void); 78 static void rmtgetconn(void); 79 static void rmtgets(char *, int); 80 static int rmtreply(const char *); 81 82 static int errfd = -1; 83 84 int 85 rmthost(const char *host) 86 { 87 88 rmtpeer = strdup(host); 89 if (rmtpeer == NULL) 90 return (0); 91 signal(SIGPIPE, rmtconnaborted); 92 rmtgetconn(); 93 if (rmtape < 0) 94 return (0); 95 return (1); 96 } 97 98 static void 99 rmtconnaborted(int sig __unused) 100 { 101 msg("Lost connection to remote host.\n"); 102 if (errfd != -1) { 103 fd_set r; 104 struct timeval t; 105 106 FD_ZERO(&r); 107 FD_SET(errfd, &r); 108 t.tv_sec = 0; 109 t.tv_usec = 0; 110 if (select(errfd + 1, &r, NULL, NULL, &t)) { 111 int i; 112 char buf[2048]; 113 114 if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) { 115 buf[i] = '\0'; 116 msg("on %s: %s%s", rmtpeer, buf, 117 buf[i - 1] == '\n' ? "" : "\n"); 118 } 119 } 120 } 121 122 exit(X_ABORT); 123 } 124 125 void 126 rmtgetconn(void) 127 { 128 char *cp; 129 const char *rmt; 130 static struct servent *sp = NULL; 131 static struct passwd *pwd = NULL; 132 char *tuser; 133 int size; 134 int throughput; 135 int on; 136 137 if (sp == NULL) { 138 sp = getservbyname("shell", "tcp"); 139 if (sp == NULL) { 140 msg("shell/tcp: unknown service\n"); 141 exit(X_STARTUP); 142 } 143 pwd = getpwuid(getuid()); 144 if (pwd == NULL) { 145 msg("who are you?\n"); 146 exit(X_STARTUP); 147 } 148 } 149 if ((cp = strchr(rmtpeer, '@')) != NULL) { 150 tuser = rmtpeer; 151 *cp = '\0'; 152 if (!okname(tuser)) 153 exit(X_STARTUP); 154 rmtpeer = ++cp; 155 } else 156 tuser = pwd->pw_name; 157 if ((rmt = getenv("RMT")) == NULL) 158 rmt = _PATH_RMT; 159 msg("%s", ""); 160 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name, 161 tuser, rmt, &errfd); 162 if (rmtape < 0) { 163 msg("login to %s as %s failed.\n", rmtpeer, tuser); 164 return; 165 } 166 (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer); 167 size = ntrec * TP_BSIZE; 168 if (size > 60 * 1024) /* XXX */ 169 size = 60 * 1024; 170 /* Leave some space for rmt request/response protocol */ 171 size += 2 * 1024; 172 while (size > TP_BSIZE && 173 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0) 174 size -= TP_BSIZE; 175 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)); 176 throughput = IPTOS_THROUGHPUT; 177 if (setsockopt(rmtape, IPPROTO_IP, IP_TOS, 178 &throughput, sizeof(throughput)) < 0) 179 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt"); 180 on = 1; 181 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0) 182 perror("TCP_NODELAY setsockopt"); 183 } 184 185 static int 186 okname(const char *cp0) 187 { 188 const char *cp; 189 int c; 190 191 for (cp = cp0; *cp; cp++) { 192 c = *cp; 193 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) { 194 msg("invalid user name %s\n", cp0); 195 return (0); 196 } 197 } 198 return (1); 199 } 200 201 int 202 rmtopen(const char *tape, int mode) 203 { 204 char buf[256]; 205 206 (void)snprintf(buf, sizeof (buf), "O%.226s\n%d\n", tape, mode); 207 rmtstate = TS_OPEN; 208 return (rmtcall(tape, buf)); 209 } 210 211 void 212 rmtclose(void) 213 { 214 215 if (rmtstate != TS_OPEN) 216 return; 217 rmtcall("close", "C\n"); 218 rmtstate = TS_CLOSED; 219 } 220 221 int 222 rmtread(char *buf, int count) 223 { 224 char line[30]; 225 int n, i, cc; 226 227 (void)snprintf(line, sizeof (line), "R%d\n", count); 228 n = rmtcall("read", line); 229 if (n < 0) 230 /* rmtcall() properly sets errno for us on errors. */ 231 return (n); 232 for (i = 0; i < n; i += cc) { 233 cc = read(rmtape, buf+i, n - i); 234 if (cc <= 0) 235 rmtconnaborted(0); 236 } 237 return (n); 238 } 239 240 int 241 rmtwrite(const char *buf, int count) 242 { 243 char line[30]; 244 245 (void)snprintf(line, sizeof (line), "W%d\n", count); 246 write(rmtape, line, strlen(line)); 247 write(rmtape, buf, count); 248 return (rmtreply("write")); 249 } 250 251 void 252 rmtwrite0(int count) 253 { 254 char line[30]; 255 256 (void)snprintf(line, sizeof (line), "W%d\n", count); 257 write(rmtape, line, strlen(line)); 258 } 259 260 void 261 rmtwrite1(const char *buf, int count) 262 { 263 264 write(rmtape, buf, count); 265 } 266 267 int 268 rmtwrite2(void) 269 { 270 271 return (rmtreply("write")); 272 } 273 274 int 275 rmtseek(int offset, int pos) /* XXX off_t ? */ 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(void) 287 { 288 int i; 289 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(int cmd, int count) 301 { 302 char buf[256]; 303 304 if (count < 0) 305 return (-1); 306 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count); 307 return (rmtcall("ioctl", buf)); 308 } 309 310 static int 311 rmtcall(const char *cmd, const char *buf) 312 { 313 314 if (write(rmtape, buf, strlen(buf)) != strlen(buf)) 315 rmtconnaborted(0); 316 return (rmtreply(cmd)); 317 } 318 319 static int 320 rmtreply(const char *cmd) 321 { 322 char *cp; 323 char code[30], emsg[BUFSIZ]; 324 325 rmtgets(code, sizeof (code)); 326 if (*code == 'E' || *code == 'F') { 327 rmtgets(emsg, sizeof (emsg)); 328 msg("%s: %s", cmd, emsg); 329 errno = atoi(code + 1); 330 if (*code == 'F') 331 rmtstate = TS_CLOSED; 332 return (-1); 333 } 334 if (*code != 'A') { 335 /* Kill trailing newline */ 336 cp = code + strlen(code); 337 if (cp > code && *--cp == '\n') 338 *cp = '\0'; 339 340 msg("Protocol to remote tape server botched (code \"%s\").\n", 341 code); 342 rmtconnaborted(0); 343 } 344 return (atoi(code + 1)); 345 } 346 347 int 348 rmtgetb(void) 349 { 350 char c; 351 352 if (read(rmtape, &c, 1) != 1) 353 rmtconnaborted(0); 354 return (c); 355 } 356 357 /* Get a line (guaranteed to have a trailing newline). */ 358 void 359 rmtgets(char *line, int len) 360 { 361 char *cp = line; 362 363 while (len > 1) { 364 *cp = rmtgetb(); 365 if (*cp == '\n') { 366 cp[1] = '\0'; 367 return; 368 } 369 cp++; 370 len--; 371 } 372 *cp = '\0'; 373 msg("Protocol to remote tape server botched.\n"); 374 msg("(rmtgets got \"%s\").\n", line); 375 rmtconnaborted(0); 376 } 377