2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
33 #include <sys/param.h>
35 #include <sys/socket.h>
38 #include <sys/vnode.h>
40 #include <ufs/inode.h>
42 #include <vfs/ufs/dinode.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/tcp.h>
50 #include <protocols/dumprestore.h>
62 #include "pathnames.h"
68 static int rmtstate = TS_CLOSED;
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 *);
80 int krcmd(char **, int /*u_short*/, char *, char *, int *, char *);
83 static int errfd = -1;
86 rmthost(const char *hostname)
89 if ((rmtpeer = strdup(hostname)) == NULL)
90 err(1, "strdup failed");
91 signal(SIGPIPE, rmtconnaborted);
99 rmtconnaborted(int signo __unused)
101 msg("Lost connection to remote host.\n");
110 if (select(errfd + 1, &r, NULL, NULL, &t)) {
114 if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
116 msg("on %s: %s%s", rmtpeer, buf,
117 buf[i - 1] == '\n' ? "" : "\n");
130 static struct servent *sp = NULL;
131 static struct passwd *pwd = NULL;
138 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
140 msg("%s/tcp: unknown service\n",
141 dokerberos ? "kshell" : "shell");
144 pwd = getpwuid(getuid());
146 msg("who are you?\n");
150 if ((cp = strchr(rmtpeer, '@')) != NULL) {
157 tuser = pwd->pw_name;
158 if ((rmt = getenv("RMT")) == NULL)
163 rmtape = krcmd(&rmtpeer, sp->s_port, tuser, rmt, &errfd, NULL);
166 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
169 msg("login to %s as %s failed.\n", rmtpeer, tuser);
172 fprintf(stderr, "Connection to %s established.\n", rmtpeer);
173 size = ntrec * TP_BSIZE;
174 if (size > 60 * 1024) /* XXX */
176 /* Leave some space for rmt request/response protocol */
178 while (size > TP_BSIZE &&
179 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
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");
187 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
188 perror("TCP_NODELAY setsockopt");
197 for (cp = cp0; *cp; cp++) {
199 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
200 msg("invalid user name %s\n", cp0);
208 rmtopen(const char *rtape, int mode)
212 snprintf(buf, sizeof (buf), "O%.226s\n%d\n", rtape, mode);
214 return (rmtcall(rtape, buf));
221 if (rmtstate != TS_OPEN)
223 rmtcall("close", "C\n");
224 rmtstate = TS_CLOSED;
229 rmtread(char *buf, int count)
234 snprintf(line, sizeof (line), "R%d\n", count);
235 n = rmtcall("read", line);
237 /* rmtcall() properly sets errno for us on errors. */
239 for (i = 0; i < n; i += cc) {
240 cc = read(rmtape, buf+i, n - i);
248 rmtseek(int offset, int pos)
252 snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
253 return (rmtcall("seek", line));
257 rmtioctl(int cmd, int count)
263 snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
264 return (rmtcall("ioctl", buf));
266 #endif /* RRESTORE */
269 rmtwrite(const void *buf, int count)
273 snprintf(line, sizeof (line), "W%d\n", count);
274 write(rmtape, line, strlen(line));
275 write(rmtape, buf, count);
276 return (rmtreply("write"));
280 rmtcall(const char *cmd, const char *buf)
282 ssize_t len = (ssize_t)strlen(buf);
284 if (write(rmtape, buf, len) != len)
286 return (rmtreply(cmd));
290 rmtreply(const char *cmd)
293 char code[30], emsg[BUFSIZ];
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);
301 rmtstate = TS_CLOSED;
305 /* Kill trailing newline */
306 cp = code + strlen(code);
307 if (cp > code && *--cp == '\n')
310 msg("Protocol to remote tape server botched (code \"%s\").\n",
314 return (atoi(code + 1));
322 if (read(rmtape, &c, 1) != 1)
327 /* Get a line (guaranteed to have a trailing newline). */
329 rmtgets(char *line, int len)
343 msg("Protocol to remote tape server botched.\n");
344 msg("(rmtgets got \"%s\").\n", line);