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