2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
10 * Copyright (c) 1980, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static char sccsid[] = "@(#)dumprmt.c 8.3 (Berkeley) 4/28/95";
46 static const char rcsid[] =
47 "$Id: dumprmt.c,v 1.2 1999/10/11 12:53:20 stelian Exp $";
51 #include <sys/types.h>
52 #include <linux/types.h>
54 #include <sys/param.h>
56 #include <sys/socket.h>
59 #include <linux/ext2_fs.h>
60 #include <bsdcompat.h>
64 #include <sys/vnode.h>
66 #include <ufs/inode.h>
68 #include <ufs/ufs/dinode.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/tcp.h>
77 #include <protocols/dumprestore.h>
90 #include <ext2fs/ext2fs.h>
93 #include "pathnames.h"
99 static int rmtstate = TS_CLOSED;
101 static char *rmtpeer;
103 static int okname __P((char *));
104 static int rmtcall __P((char *, char *));
105 static void rmtconnaborted __P((/* int, int */));
106 static int rmtgetb __P((void));
107 static void rmtgetconn __P((void));
108 static void rmtgets __P((char *, int));
109 static int rmtreply __P((char *));
111 int krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
114 static int errfd = -1;
115 extern int dokerberos;
116 extern int ntrec; /* blocking factor on tape */
123 rmtpeer = malloc(strlen(host) + 1);
125 strcpy(rmtpeer, host);
128 signal(SIGPIPE, rmtconnaborted);
138 msg("Lost connection to remote host.\n");
147 if (select(errfd + 1, &r, NULL, NULL, &t)) {
151 if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
153 msg("on %s: %s%s", rmtpeer, buf,
154 buf[i - 1] == '\n' ? "" : "\n");
166 register const char *rmt;
167 static struct servent *sp = NULL;
168 static struct passwd *pwd = NULL;
175 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
177 msg("%s/tcp: unknown service\n",
178 dokerberos ? "kshell" : "shell");
181 pwd = getpwuid(getuid());
183 msg("who are you?\n");
187 if ((cp = strchr(rmtpeer, '@')) != NULL) {
194 tuser = pwd->pw_name;
195 if ((rmt = getenv("RMT")) == NULL)
200 rmtape = krcmd(&rmtpeer, sp->s_port, tuser, rmt, &errfd,
204 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
207 msg("login to %s as %s failed.\n", rmtpeer, tuser);
210 (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
211 size = ntrec * TP_BSIZE;
212 if (size > 60 * 1024) /* XXX */
214 /* Leave some space for rmt request/response protocol */
216 while (size > TP_BSIZE &&
217 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
219 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
220 throughput = IPTOS_THROUGHPUT;
221 if (setsockopt(rmtape, IPPROTO_IP, IP_TOS,
222 &throughput, sizeof(throughput)) < 0)
223 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
225 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
226 perror("TCP_NODELAY setsockopt");
236 for (cp = cp0; *cp; cp++) {
238 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
239 msg("invalid user name %s\n", cp0);
253 (void)snprintf(buf, sizeof (buf), "O%.226s\n%d\n", tape, mode);
255 return (rmtcall(tape, buf));
262 if (rmtstate != TS_OPEN)
264 rmtcall("close", "C\n");
265 rmtstate = TS_CLOSED;
276 (void)snprintf(line, sizeof (line), "R%d\n", count);
277 n = rmtcall("read", line);
279 /* rmtcall() properly sets errno for us on errors. */
281 for (i = 0; i < n; i += cc) {
282 cc = read(rmtape, buf+i, n - i);
296 (void)snprintf(line, sizeof (line), "W%d\n", count);
297 write(rmtape, line, strlen(line));
298 write(rmtape, buf, count);
299 return (rmtreply("write"));
308 (void)snprintf(line, sizeof (line), "W%d\n", count);
309 write(rmtape, line, strlen(line));
313 rmtwrite1(buf, count)
318 write(rmtape, buf, count);
325 return (rmtreply("write"));
334 (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
335 return (rmtcall("seek", line));
346 if (rmtstate != TS_OPEN)
348 rmtcall("status", "S\n");
349 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
362 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
363 return (rmtcall("ioctl", buf));
371 if (write(rmtape, buf, strlen(buf)) != strlen(buf))
373 return (rmtreply(cmd));
381 char code[30], emsg[BUFSIZ];
384 rmtgets(code, sizeof (code));
385 if (*code == 'E' || *code == 'F') {
386 rmtgets(emsg, sizeof (emsg));
387 msg("%s: %s", cmd, emsg);
388 errno = atoi(code + 1);
390 rmtstate = TS_CLOSED;
394 /* Kill trailing newline */
395 cp = code + strlen(code);
396 if (cp > code && *--cp == '\n')
399 msg("Protocol to remote tape server botched (code \"%s\").\n",
403 return (atoi(code + 1));
411 if (read(rmtape, &c, 1) != 1)
416 /* Get a line (guaranteed to have a trailing newline). */
422 register char *cp = line;
434 msg("Protocol to remote tape server botched.\n");
435 msg("(rmtgets got \"%s\").\n", line);