]>
git.wh0rd.org - dump.git/blob - common/dumprmt.c
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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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.25 2003/02/12 11:02:29 stelian Exp $";
48 #include <sys/param.h>
50 #include <sys/socket.h>
54 #include <sys/types.h>
55 #ifdef HAVE_EXT2FS_EXT2_FS_H
56 #include <ext2fs/ext2_fs.h>
58 #include <linux/ext2_fs.h>
60 #include <bsdcompat.h>
63 #include <sys/vnode.h>
65 #include <ufs/inode.h>
67 #include <ufs/ufs/dinode.h>
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/tcp.h>
75 #include <protocols/dumprestore.h>
79 #include <compaterr.h>
91 #include <ext2fs/ext2fs.h>
94 #include <pathnames.h>
95 #include "dump.h" /* for X_STARTUP, X_ABORT etc */
100 static int rmtstate
= TS_CLOSED
;
101 static int tormtape
= -1;
102 static int fromrmtape
= -1;
104 static const char *rmtpeer
= 0;
106 static int okname
__P((const char *));
107 static OFF_T rmtcall
__P((const char *, const char *));
108 static void rmtconnaborted
__P((int));
109 static int rmtgetb
__P((void));
110 static int rmtgetconn
__P((void));
111 static void rmtgets
__P((char *, size_t));
112 static OFF_T rmtreply
__P((const char *));
113 static int piped_child
__P((const char **command
));
115 int krcmd
__P((char **, int /*u_short*/, char *, char *, int *, char *));
118 static int errfd
= -1;
119 extern int dokerberos
;
120 extern int ntrec
; /* blocking factor on tape */
126 rmthost(const char *host
)
129 free((void *)rmtpeer
);
130 if ((rmtpeer
= strdup(host
)) == NULL
)
132 signal(SIGPIPE
, rmtconnaborted
);
137 rmtconnaborted(UNUSED(int signo
))
139 msg("Lost connection to remote host.\n");
148 if (select(errfd
+ 1, &r
, NULL
, NULL
, &t
)) {
152 if ((i
= read(errfd
, buf
, sizeof(buf
) - 1)) > 0) {
154 msg("on %s: %s%s", rmtpeer
, buf
,
155 buf
[i
- 1] == '\n' ? "" : "\n");
168 static struct servent
*sp
= NULL
;
169 static struct passwd
*pwd
= NULL
;
179 if (!rsh
&& sp
== NULL
) {
180 sp
= getservbyname(dokerberos
? "kshell" : "shell", "tcp");
182 errx(1, "%s/tcp: unknown service",
183 dokerberos
? "kshell" : "shell");
186 pwd
= getpwuid(getuid());
188 errx(1, "who are you?");
190 if ((cp
= strchr(rmtpeer
, '@')) != NULL
) {
197 tuser
= pwd
->pw_name
;
198 if ((rmt
= getenv("RMT")) == NULL
)
203 const char *rshcmd
[6];
211 /* Restore the uid and gid. We really don't want
212 * to execute whatever is put into RSH variable with
213 * more priviledges than needed... */
217 if ((rshpid
= piped_child(rshcmd
)) < 0) {
218 msg("cannot open connection\n");
223 /* Copy rmtpeer to rmtpeercopy to ignore the
224 return value from rcmd. I cannot figure if
225 this is this a bug in rcmd or in my code... */
226 rmtpeercopy
= (char *)rmtpeer
;
229 tormtape
= krcmd(&rmtpeercopy
, sp
->s_port
, tuser
, rmt
, &errfd
,
233 tormtape
= rcmd(&rmtpeercopy
, (u_short
)sp
->s_port
, pwd
->pw_name
,
236 msg("login to %s as %s failed.\n", rmtpeer
, tuser
);
239 size
= ntrec
* TP_BSIZE
;
240 if (size
> 60 * 1024) /* XXX */
242 /* Leave some space for rmt request/response protocol */
244 while (size
> TP_BSIZE
&&
245 setsockopt(tormtape
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof (size
)) < 0)
247 (void)setsockopt(tormtape
, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
));
248 throughput
= IPTOS_THROUGHPUT
;
249 if (setsockopt(tormtape
, IPPROTO_IP
, IP_TOS
,
250 &throughput
, sizeof(throughput
)) < 0)
251 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
253 if (setsockopt(tormtape
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof (on
)) < 0)
254 perror("TCP_NODELAY setsockopt");
255 fromrmtape
= tormtape
;
257 (void)fprintf(stderr
, "Connection to %s established.\n", rmtpeer
);
262 okname(const char *cp0
)
267 for (cp
= cp0
; *cp
; cp
++) {
269 if (!isascii(c
) || !(isalnum(c
) || c
== '_' || c
== '-')) {
270 warnx("invalid user name %s\n", cp0
);
278 rmtopen(const char *tape
, const int mode
)
280 char buf
[MAXPATHLEN
];
283 rmtflags
= rmtflags_tochar(mode
);
284 (void)snprintf(buf
, sizeof (buf
), "O%s\n%d %s\n",
290 return (rmtcall(tape
, buf
));
297 if (rmtstate
!= TS_OPEN
)
299 rmtcall("close", "C\n");
300 rmtstate
= TS_CLOSED
;
304 rmtread(char *buf
, size_t count
)
310 (void)snprintf(line
, sizeof (line
), "R%u\n", (unsigned)count
);
311 n
= rmtcall("read", line
);
313 /* rmtcall() properly sets errno for us on errors. */
315 for (i
= 0; i
< n
; i
+= cc
) {
316 cc
= read(fromrmtape
, buf
+i
, n
- i
);
324 rmtwrite(const char *buf
, size_t count
)
328 (void)snprintf(line
, sizeof (line
), "W%ld\n", (long)count
);
329 write(tormtape
, line
, strlen(line
));
330 write(tormtape
, buf
, count
);
331 return (rmtreply("write"));
335 rmtseek(OFF_T offset
, int pos
)
339 (void)snprintf(line
, sizeof (line
), "L%lld\n%d\n", (long long)offset
, pos
);
340 return (rmtcall("seek", line
));
351 if (rmtstate
!= TS_OPEN
)
353 rmtcall("status", "S\n");
354 for (i
= 0, cp
= (char *)&mts
; i
< (int)sizeof(mts
); i
++)
360 rmtioctl(int cmd
, int count
)
366 (void)snprintf(buf
, sizeof (buf
), "I%d\n%d\n", cmd
, count
);
367 return (rmtcall("ioctl", buf
));
371 rmtcall(const char *cmd
, const char *buf
)
374 if (write(tormtape
, buf
, strlen(buf
)) != (ssize_t
)strlen(buf
))
376 return (rmtreply(cmd
));
380 rmtreply(const char *cmd
)
383 char code
[30], emsg
[BUFSIZ
];
385 rmtgets(code
, sizeof (code
));
386 if (*code
== 'E' || *code
== 'F') {
387 rmtgets(emsg
, sizeof (emsg
));
388 msg("%s: %s", cmd
, emsg
);
389 errno
= atoi(code
+ 1);
391 rmtstate
= TS_CLOSED
;
395 /* Kill trailing newline */
396 cp
= code
+ strlen(code
);
397 if (cp
> code
&& *--cp
== '\n')
400 msg("Protocol to remote tape server botched (code \"%s\").\n",
404 return (OFF_T
)(atoll(code
+ 1));
412 if (read(fromrmtape
, &c
, 1) != 1)
417 /* Get a line (guaranteed to have a trailing newline). */
419 rmtgets(char *line
, size_t len
)
433 msg("Protocol to remote tape server botched.\n");
434 msg("(rmtgets got \"%s\").\n", line
);
438 int piped_child(const char **command
) {
440 int to_child_pipe
[2];
441 int from_child_pipe
[2];
443 if (pipe (to_child_pipe
) < 0) {
444 msg ("cannot create pipe: %s\n", strerror(errno
));
447 if (pipe (from_child_pipe
) < 0) {
448 msg ("cannot create pipe: %s\n", strerror(errno
));
453 msg ("cannot fork: %s\n", strerror(errno
));
457 if (dup2 (to_child_pipe
[0], STDIN_FILENO
) < 0) {
458 msg ("cannot dup2 pipe: %s\n", strerror(errno
));
461 if (close (to_child_pipe
[1]) < 0) {
462 msg ("cannot close pipe: %s\n", strerror(errno
));
465 if (close (from_child_pipe
[0]) < 0) {
466 msg ("cannot close pipe: %s\n", strerror(errno
));
469 if (dup2 (from_child_pipe
[1], STDOUT_FILENO
) < 0) {
470 msg ("cannot dup2 pipe: %s\n", strerror(errno
));
473 setpgid(0, getpid());
474 execvp (command
[0], (char *const *) command
);
475 msg("cannot exec %s: %s\n", command
[0], strerror(errno
));
478 if (close (to_child_pipe
[0]) < 0) {
479 msg ("cannot close pipe: %s\n", strerror(errno
));
482 if (close (from_child_pipe
[1]) < 0) {
483 msg ("cannot close pipe: %s\n", strerror(errno
));
486 tormtape
= to_child_pipe
[1];
487 fromrmtape
= from_child_pipe
[0];