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