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