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