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