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