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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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
43 static const char rcsid[] =
44 "$Id: dumprmt.c,v 1.18 2001/08/13 16:17:52 stelian Exp $";
48 #include <sys/param.h>
50 #include <sys/socket.h>
53 #include <sys/types.h>
54 #ifdef HAVE_EXT2FS_EXT2_FS_H
55 #include <ext2fs/ext2_fs.h>
57 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
62 #include <sys/vnode.h>
64 #include <ufs/inode.h>
66 #include <ufs/ufs/dinode.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/tcp.h>
74 #include <protocols/dumprestore.h>
78 #include <compaterr.h>
89 #include <ext2fs/ext2fs.h>
92 #include <pathnames.h>
93 #include "dump.h" /* for X_STARTUP, X_ABORT etc */
98 static int rmtstate = TS_CLOSED;
99 static int tormtape = -1;
100 static int fromrmtape = -1;
102 static const char *rmtpeer = 0;
104 static int okname __P((const char *));
105 static int rmtcall __P((const char *, const char *));
106 static void rmtconnaborted __P((int));
107 static int rmtgetb __P((void));
108 static int rmtgetconn __P((void));
109 static void rmtgets __P((char *, size_t));
110 static int rmtreply __P((const char *));
111 static int piped_child __P((const char **command));
113 int krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
116 static int errfd = -1;
117 extern int dokerberos;
118 extern int ntrec; /* blocking factor on tape */
124 rmthost(const char *host)
127 free((void *)rmtpeer);
128 if ((rmtpeer = strdup(host)) == NULL)
130 signal(SIGPIPE, rmtconnaborted);
135 rmtconnaborted(int signo)
137 msg("Lost connection to remote host.\n");
146 if (select(errfd + 1, &r, NULL, NULL, &t)) {
150 if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
152 msg("on %s: %s%s", rmtpeer, buf,
153 buf[i - 1] == '\n' ? "" : "\n");
165 register const char *rmt;
166 static struct servent *sp = NULL;
167 static struct passwd *pwd = NULL;
177 if (!rsh && sp == NULL) {
178 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
180 errx(1, "%s/tcp: unknown service",
181 dokerberos ? "kshell" : "shell");
184 pwd = getpwuid(getuid());
186 errx(1, "who are you?");
188 if ((cp = strchr(rmtpeer, '@')) != NULL) {
195 tuser = pwd->pw_name;
196 if ((rmt = getenv("RMT")) == NULL)
201 const char *rshcmd[6];
209 /* Restore the uid and gid. We really don't want
210 * to execute whatever is put into RSH variable with
211 * more priviledges than needed... */
215 if ((rshpid = piped_child(rshcmd)) < 0) {
216 msg("cannot open connection\n");
221 /* Copy rmtpeer to rmtpeercopy to ignore the
222 return value from rcmd. I cannot figure if
223 this is this a bug in rcmd or in my code... */
224 rmtpeercopy = (char *)rmtpeer;
227 tormtape = krcmd(&rmtpeercopy, sp->s_port, tuser, rmt, &errfd,
231 tormtape = rcmd(&rmtpeercopy, (u_short)sp->s_port, pwd->pw_name,
234 msg("login to %s as %s failed.\n", rmtpeer, tuser);
237 size = ntrec * TP_BSIZE;
238 if (size > 60 * 1024) /* XXX */
240 /* Leave some space for rmt request/response protocol */
242 while (size > TP_BSIZE &&
243 setsockopt(tormtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
245 (void)setsockopt(tormtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
246 throughput = IPTOS_THROUGHPUT;
247 if (setsockopt(tormtape, IPPROTO_IP, IP_TOS,
248 &throughput, sizeof(throughput)) < 0)
249 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
251 if (setsockopt(tormtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
252 perror("TCP_NODELAY setsockopt");
253 fromrmtape = tormtape;
255 (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
260 okname(const char *cp0)
262 register const char *cp;
265 for (cp = cp0; *cp; cp++) {
267 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
268 warnx("invalid user name %s\n", cp0);
276 rmtopen(const char *tape, int mode)
278 char buf[MAXPATHLEN];
280 (void)snprintf(buf, sizeof (buf), "O%s\n%d\n", tape, mode);
282 return (rmtcall(tape, buf));
289 if (rmtstate != TS_OPEN)
291 rmtcall("close", "C\n");
292 rmtstate = TS_CLOSED;
296 rmtread(char *buf, size_t count)
302 (void)snprintf(line, sizeof (line), "R%u\n", (unsigned)count);
303 n = rmtcall("read", line);
305 /* rmtcall() properly sets errno for us on errors. */
307 for (i = 0; i < n; i += cc) {
308 cc = read(fromrmtape, buf+i, n - i);
316 rmtwrite(const char *buf, size_t count)
320 (void)snprintf(line, sizeof (line), "W%ld\n", (long)count);
321 write(tormtape, line, strlen(line));
322 write(tormtape, buf, count);
323 return (rmtreply("write"));
327 rmtseek(int offset, int pos)
331 (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
332 return (rmtcall("seek", line));
343 if (rmtstate != TS_OPEN)
345 rmtcall("status", "S\n");
346 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
352 rmtioctl(int cmd, int count)
358 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
359 return (rmtcall("ioctl", buf));
363 rmtcall(const char *cmd, const char *buf)
366 if (write(tormtape, buf, strlen(buf)) != strlen(buf))
368 return (rmtreply(cmd));
372 rmtreply(const char *cmd)
375 char code[30], emsg[BUFSIZ];
377 rmtgets(code, sizeof (code));
378 if (*code == 'E' || *code == 'F') {
379 rmtgets(emsg, sizeof (emsg));
380 msg("%s: %s", cmd, emsg);
381 errno = atoi(code + 1);
383 rmtstate = TS_CLOSED;
387 /* Kill trailing newline */
388 cp = code + strlen(code);
389 if (cp > code && *--cp == '\n')
392 msg("Protocol to remote tape server botched (code \"%s\").\n",
396 return (atoi(code + 1));
404 if (read(fromrmtape, &c, 1) != 1)
409 /* Get a line (guaranteed to have a trailing newline). */
411 rmtgets(char *line, size_t len)
413 register char *cp = line;
425 msg("Protocol to remote tape server botched.\n");
426 msg("(rmtgets got \"%s\").\n", line);
430 int piped_child(const char **command) {
432 int to_child_pipe[2];
433 int from_child_pipe[2];
435 if (pipe (to_child_pipe) < 0) {
436 msg ("cannot create pipe: %s\n", strerror(errno));
439 if (pipe (from_child_pipe) < 0) {
440 msg ("cannot create pipe: %s\n", strerror(errno));
445 msg ("cannot fork: %s\n", strerror(errno));
449 if (dup2 (to_child_pipe[0], STDIN_FILENO) < 0) {
450 msg ("cannot dup2 pipe: %s\n", strerror(errno));
453 if (close (to_child_pipe[1]) < 0) {
454 msg ("cannot close pipe: %s\n", strerror(errno));
457 if (close (from_child_pipe[0]) < 0) {
458 msg ("cannot close pipe: %s\n", strerror(errno));
461 if (dup2 (from_child_pipe[1], STDOUT_FILENO) < 0) {
462 msg ("cannot dup2 pipe: %s\n", strerror(errno));
465 setpgid(0, getpid());
466 execvp (command[0], (char *const *) command);
467 msg("cannot exec %s: %s\n", command[0], strerror(errno));
470 if (close (to_child_pipe[0]) < 0) {
471 msg ("cannot close pipe: %s\n", strerror(errno));
474 if (close (from_child_pipe[1]) < 0) {
475 msg ("cannot close pipe: %s\n", strerror(errno));
478 tormtape = to_child_pipe[1];
479 fromrmtape = from_child_pipe[0];