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