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