dumprmt.c revision 19239
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 35static char sccsid[] = "@(#)dumprmt.c 8.1 (Berkeley) 6/5/93"; 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 <netdb.h> 59#include <pwd.h> 60#include <signal.h> 61#include <stdio.h> 62#ifdef __STDC__ 63#include <stdlib.h> 64#include <string.h> 65#include <unistd.h> 66#endif 67 68#include "pathnames.h" 69#include "dump.h" 70 71#define TS_CLOSED 0 72#define TS_OPEN 1 73 74static int rmtstate = TS_CLOSED; 75static int rmtape; 76static char *rmtpeer; 77 78static int okname __P((char *)); 79static int rmtcall __P((char *, char *)); 80static void rmtconnaborted __P((/* int, int */)); 81static int rmtgetb __P((void)); 82static void rmtgetconn __P((void)); 83static void rmtgets __P((char *, int)); 84static int rmtreply __P((char *)); 85 86extern int ntrec; /* blocking factor on tape */ 87 88int 89rmthost(host) 90 char *host; 91{ 92 93 rmtpeer = malloc(strlen(host) + 1); 94 if (rmtpeer) 95 strcpy(rmtpeer, host); 96 else 97 rmtpeer = host; 98 signal(SIGPIPE, rmtconnaborted); 99 rmtgetconn(); 100 if (rmtape < 0) 101 return (0); 102 return (1); 103} 104 105static void 106rmtconnaborted() 107{ 108 109 (void) fprintf(stderr, "rdump: Lost connection to remote host.\n"); 110 exit(1); 111} 112 113void 114rmtgetconn() 115{ 116 register char *cp; 117 register const char *rmt; 118 static struct servent *sp = NULL; 119 static struct passwd *pwd = NULL; 120#ifdef notdef 121 static int on = 1; 122#endif 123 char *tuser; 124 int size; 125 int throughput; 126 127 if (sp == NULL) { 128 sp = getservbyname("shell", "tcp"); 129 if (sp == NULL) { 130 (void) fprintf(stderr, 131 "rdump: shell/tcp: unknown service\n"); 132 exit(1); 133 } 134 pwd = getpwuid(getuid()); 135 if (pwd == NULL) { 136 (void) fprintf(stderr, "rdump: who are you?\n"); 137 exit(1); 138 } 139 } 140 if ((cp = index(rmtpeer, '@')) != NULL) { 141 tuser = rmtpeer; 142 *cp = '\0'; 143 if (!okname(tuser)) 144 exit(1); 145 rmtpeer = ++cp; 146 } else 147 tuser = pwd->pw_name; 148 if ((rmt = getenv("RMT")) == NULL) 149 rmt = _PATH_RMT; 150 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name, tuser, 151 rmt, (int *)0); 152 if (rmtape < 0) 153 return; 154 size = ntrec * TP_BSIZE; 155 if (size > 60 * 1024) /* XXX */ 156 size = 60 * 1024; 157 /* Leave some space for rmt request/response protocol */ 158 size += 2 * 1024; 159 while (size > TP_BSIZE && 160 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0) 161 size -= TP_BSIZE; 162 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)); 163 throughput = IPTOS_THROUGHPUT; 164 if (setsockopt(rmtape, IPPROTO_IP, IP_TOS, 165 &throughput, sizeof(throughput)) < 0) 166 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt"); 167 168#ifdef notdef 169 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0) 170 perror("TCP_NODELAY setsockopt"); 171#endif 172} 173 174static int 175okname(cp0) 176 char *cp0; 177{ 178 register char *cp; 179 register int c; 180 181 for (cp = cp0; *cp; cp++) { 182 c = *cp; 183 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) { 184 (void) fprintf(stderr, "rdump: invalid user name %s\n", 185 cp0); 186 return (0); 187 } 188 } 189 return (1); 190} 191 192int 193rmtopen(tape, mode) 194 char *tape; 195 int mode; 196{ 197 char buf[256]; 198 199 (void)sprintf(buf, "O%s\n%d\n", tape, mode); 200 rmtstate = TS_OPEN; 201 return (rmtcall(tape, buf)); 202} 203 204void 205rmtclose() 206{ 207 208 if (rmtstate != TS_OPEN) 209 return; 210 rmtcall("close", "C\n"); 211 rmtstate = TS_CLOSED; 212} 213 214int 215rmtread(buf, count) 216 char *buf; 217 int count; 218{ 219 char line[30]; 220 int n, i, cc; 221 extern errno; 222 223 (void)sprintf(line, "R%d\n", count); 224 n = rmtcall("read", line); 225 if (n < 0) { 226 errno = n; 227 return (-1); 228 } 229 for (i = 0; i < n; i += cc) { 230 cc = read(rmtape, buf+i, n - i); 231 if (cc <= 0) { 232 rmtconnaborted(); 233 } 234 } 235 return (n); 236} 237 238int 239rmtwrite(buf, count) 240 char *buf; 241 int count; 242{ 243 char line[30]; 244 245 (void)sprintf(line, "W%d\n", count); 246 write(rmtape, line, strlen(line)); 247 write(rmtape, buf, count); 248 return (rmtreply("write")); 249} 250 251void 252rmtwrite0(count) 253 int count; 254{ 255 char line[30]; 256 257 (void)sprintf(line, "W%d\n", count); 258 write(rmtape, line, strlen(line)); 259} 260 261void 262rmtwrite1(buf, count) 263 char *buf; 264 int count; 265{ 266 267 write(rmtape, buf, count); 268} 269 270int 271rmtwrite2() 272{ 273 274 return (rmtreply("write")); 275} 276 277int 278rmtseek(offset, pos) 279 int offset, pos; 280{ 281 char line[80]; 282 283 (void)sprintf(line, "L%d\n%d\n", offset, pos); 284 return (rmtcall("seek", line)); 285} 286 287struct mtget mts; 288 289struct mtget * 290rmtstatus() 291{ 292 register int i; 293 register char *cp; 294 295 if (rmtstate != TS_OPEN) 296 return (NULL); 297 rmtcall("status", "S\n"); 298 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++) 299 *cp++ = rmtgetb(); 300 return (&mts); 301} 302 303int 304rmtioctl(cmd, count) 305 int cmd, count; 306{ 307 char buf[256]; 308 309 if (count < 0) 310 return (-1); 311 (void)sprintf(buf, "I%d\n%d\n", cmd, count); 312 return (rmtcall("ioctl", buf)); 313} 314 315static int 316rmtcall(cmd, buf) 317 char *cmd, *buf; 318{ 319 320 if (write(rmtape, buf, strlen(buf)) != strlen(buf)) 321 rmtconnaborted(); 322 return (rmtreply(cmd)); 323} 324 325static int 326rmtreply(cmd) 327 char *cmd; 328{ 329 register char *cp; 330 char code[30], emsg[BUFSIZ]; 331 332 rmtgets(code, sizeof (code)); 333 if (*code == 'E' || *code == 'F') { 334 rmtgets(emsg, sizeof (emsg)); 335 msg("%s: %s", cmd, emsg); 336 if (*code == 'F') { 337 rmtstate = TS_CLOSED; 338 return (-1); 339 } 340 return (-1); 341 } 342 if (*code != 'A') { 343 /* Kill trailing newline */ 344 cp = code + strlen(code); 345 if (cp > code && *--cp == '\n') 346 *cp = '\0'; 347 348 msg("Protocol to remote tape server botched (code \"%s\").\n", 349 code); 350 rmtconnaborted(); 351 } 352 return (atoi(code + 1)); 353} 354 355int 356rmtgetb() 357{ 358 char c; 359 360 if (read(rmtape, &c, 1) != 1) 361 rmtconnaborted(); 362 return (c); 363} 364 365/* Get a line (guaranteed to have a trailing newline). */ 366void 367rmtgets(line, len) 368 char *line; 369 int len; 370{ 371 register char *cp = line; 372 373 while (len > 1) { 374 *cp = rmtgetb(); 375 if (*cp == '\n') { 376 cp[1] = '\0'; 377 return; 378 } 379 cp++; 380 len--; 381 } 382 *cp = '\0'; 383 msg("Protocol to remote tape server botched.\n"); 384 msg("(rmtgets got \"%s\").\n", line); 385 rmtconnaborted(); 386} 387