1 /*- 2 * Copyright (c) 1980 The Regents of the University of California. 3 * 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 /* from: static char sccsid[] = "@(#)dumprmt.c 5.15 (Berkeley) 6/18/92"; */ 36 static char *rcsid = "$Id: dumprmt.c,v 1.5 1993/12/22 10:24:42 cgd Exp $"; 37 #endif /* not lint */ 38 39 #ifdef sunos 40 #include <stdio.h> 41 #include <ctype.h> 42 #include <sys/param.h> 43 #include <sys/mtio.h> 44 #include <sys/ioctl.h> 45 #include <sys/socket.h> 46 #include <sys/stat.h> 47 #else 48 #include <sys/param.h> 49 #include <sys/mtio.h> 50 #include <sys/ioctl.h> 51 #include <sys/socket.h> 52 #include <sys/time.h> 53 #include <stdio.h> 54 #endif 55 #include <ufs/dinode.h> 56 #include <signal.h> 57 58 #include <netinet/in.h> 59 60 #include <netdb.h> 61 #include <protocols/dumprestore.h> 62 #include <pwd.h> 63 #ifdef __STDC__ 64 #include <unistd.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #endif 68 #include "pathnames.h" 69 70 #define TS_CLOSED 0 71 #define TS_OPEN 1 72 73 static int rmtstate = TS_CLOSED; 74 int rmtape; 75 void rmtgetconn(); 76 void rmtconnaborted(); 77 int rmtreply(); 78 int rmtgetb(); 79 void rmtgets(); 80 int rmtcall(); 81 char *rmtpeer; 82 83 extern int ntrec; /* blocking factor on tape */ 84 extern void msg(); 85 86 extern void exit(); 87 88 int 89 rmthost(host) 90 char *host; 91 { 92 93 rmtpeer = host; 94 signal(SIGPIPE, rmtconnaborted); 95 rmtgetconn(); 96 if (rmtape < 0) 97 return (0); 98 return (1); 99 } 100 101 void 102 rmtconnaborted() 103 { 104 105 (void) fprintf(stderr, "rdump: Lost connection to remote host.\n"); 106 (void) exit(1); 107 } 108 109 void 110 rmtgetconn() 111 { 112 static struct servent *sp = 0; 113 struct passwd *pw; 114 char *name = "root"; 115 int size; 116 117 if (sp == 0) { 118 sp = getservbyname("shell", "tcp"); 119 if (sp == 0) { 120 (void) fprintf(stderr, 121 "rdump: shell/tcp: unknown service\n"); 122 (void) exit(1); 123 } 124 } 125 pw = getpwuid(getuid()); 126 if (pw && pw->pw_name) 127 name = pw->pw_name; 128 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, name, _PATH_RMT, 129 (int *)0); 130 size = ntrec * TP_BSIZE; 131 while (size > TP_BSIZE && 132 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0) 133 size -= TP_BSIZE; 134 } 135 136 int 137 rmtopen(tape, mode) 138 char *tape; 139 int mode; 140 { 141 char buf[256]; 142 143 (void)sprintf(buf, "O%s\n%d\n", tape, mode); 144 rmtstate = TS_OPEN; 145 return (rmtcall(tape, buf)); 146 } 147 148 void 149 rmtclose() 150 { 151 152 if (rmtstate != TS_OPEN) 153 return; 154 rmtcall("close", "C\n"); 155 rmtstate = TS_CLOSED; 156 } 157 158 int 159 rmtread(buf, count) 160 char *buf; 161 int count; 162 { 163 char line[30]; 164 int n, i, cc; 165 extern errno; 166 167 (void)sprintf(line, "R%d\n", count); 168 n = rmtcall("read", line); 169 if (n < 0) { 170 errno = n; 171 return (-1); 172 } 173 for (i = 0; i < n; i += cc) { 174 cc = read(rmtape, buf+i, n - i); 175 if (cc <= 0) { 176 rmtconnaborted(); 177 } 178 } 179 return (n); 180 } 181 182 int 183 rmtwrite(buf, count) 184 char *buf; 185 int count; 186 { 187 char line[30]; 188 189 (void)sprintf(line, "W%d\n", count); 190 write(rmtape, line, strlen(line)); 191 write(rmtape, buf, count); 192 return (rmtreply("write")); 193 } 194 195 void 196 rmtwrite0(count) 197 int count; 198 { 199 char line[30]; 200 201 (void)sprintf(line, "W%d\n", count); 202 write(rmtape, line, strlen(line)); 203 } 204 205 void 206 rmtwrite1(buf, count) 207 char *buf; 208 int count; 209 { 210 211 write(rmtape, buf, count); 212 } 213 214 int 215 rmtwrite2() 216 { 217 218 return (rmtreply("write")); 219 } 220 221 int 222 rmtseek(offset, pos) 223 int offset, pos; 224 { 225 char line[80]; 226 227 (void)sprintf(line, "L%d\n%d\n", offset, pos); 228 return (rmtcall("seek", line)); 229 } 230 231 struct mtget mts; 232 233 struct mtget * 234 rmtstatus() 235 { 236 register int i; 237 register char *cp; 238 239 if (rmtstate != TS_OPEN) 240 return (0); 241 rmtcall("status", "S\n"); 242 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++) 243 *cp++ = rmtgetb(); 244 return (&mts); 245 } 246 247 int 248 rmtioctl(cmd, count) 249 int cmd, count; 250 { 251 char buf[256]; 252 253 if (count < 0) 254 return (-1); 255 (void)sprintf(buf, "I%d\n%d\n", cmd, count); 256 return (rmtcall("ioctl", buf)); 257 } 258 259 int 260 rmtcall(cmd, buf) 261 char *cmd, *buf; 262 { 263 264 if (write(rmtape, buf, strlen(buf)) != strlen(buf)) 265 rmtconnaborted(); 266 return (rmtreply(cmd)); 267 } 268 269 int 270 rmtreply(cmd) 271 char *cmd; 272 { 273 char code[30], emsg[BUFSIZ]; 274 275 rmtgets(code, sizeof (code)); 276 if (*code == 'E' || *code == 'F') { 277 rmtgets(emsg, sizeof (emsg)); 278 msg("%s: %s\n", cmd, emsg, code + 1); 279 if (*code == 'F') { 280 rmtstate = TS_CLOSED; 281 return (-1); 282 } 283 return (-1); 284 } 285 if (*code != 'A') { 286 msg("Protocol to remote tape server botched (code %s?).\n", 287 code); 288 rmtconnaborted(); 289 } 290 return (atoi(code + 1)); 291 } 292 293 int 294 rmtgetb() 295 { 296 char c; 297 298 if (read(rmtape, &c, 1) != 1) 299 rmtconnaborted(); 300 return (c); 301 } 302 303 void 304 rmtgets(cp, len) 305 char *cp; 306 int len; 307 { 308 309 while (len > 1) { 310 *cp = rmtgetb(); 311 if (*cp == '\n') { 312 cp[1] = 0; 313 return; 314 } 315 cp++; 316 len--; 317 } 318 msg("Protocol to remote tape server botched (in rmtgets).\n"); 319 rmtconnaborted(); 320 } 321