]>
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 <pop@cybercable.fr>, 1999-2000
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char rcsid
[] =
43 "$Id: dumprmt.c,v 1.12 2000/11/03 18:28:58 stelian Exp $";
47 #include <sys/types.h>
48 #include <linux/types.h>
50 #include <sys/param.h>
52 #include <sys/socket.h>
55 #include <linux/ext2_fs.h>
56 #include <bsdcompat.h>
60 #include <sys/vnode.h>
62 #include <ufs/inode.h>
64 #include <ufs/ufs/dinode.h>
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/tcp.h>
73 #include <protocols/dumprestore.h>
77 #include <compaterr.h>
88 #include <ext2fs/ext2fs.h>
91 #include "pathnames.h"
97 static int rmtstate
= TS_CLOSED
;
98 static int tormtape
= -1;
99 static int fromrmtape
= -1;
101 static const char *rmtpeer
= 0;
103 static int okname
__P((const char *));
104 static int rmtcall
__P((const char *, const char *));
105 static void rmtconnaborted
__P((int));
106 static int rmtgetb
__P((void));
107 static int rmtgetconn
__P((void));
108 static void rmtgets
__P((char *, size_t));
109 static int rmtreply
__P((const char *));
110 static int piped_child
__P((const char **command
));
112 int krcmd
__P((char **, int /*u_short*/, char *, char *, int *, char *));
115 static int errfd
= -1;
116 extern int dokerberos
;
117 extern int ntrec
; /* blocking factor on tape */
123 rmthost(const char *host
)
126 free((void *)rmtpeer
);
127 if ((rmtpeer
= strdup(host
)) == NULL
)
129 signal(SIGPIPE
, rmtconnaborted
);
134 rmtconnaborted(int signo
)
136 msg("Lost connection to remote host.\n");
145 if (select(errfd
+ 1, &r
, NULL
, NULL
, &t
)) {
149 if ((i
= read(errfd
, buf
, sizeof(buf
) - 1)) > 0) {
151 msg("on %s: %s%s", rmtpeer
, buf
,
152 buf
[i
- 1] == '\n' ? "" : "\n");
164 register const char *rmt
;
165 static struct servent
*sp
= NULL
;
166 static struct passwd
*pwd
= NULL
;
176 if (!rsh
&& sp
== NULL
) {
177 sp
= getservbyname(dokerberos
? "kshell" : "shell", "tcp");
179 errx(1, "%s/tcp: unknown service",
180 dokerberos
? "kshell" : "shell");
183 pwd
= getpwuid(getuid());
185 errx(1, "who are you?");
187 if ((cp
= strchr(rmtpeer
, '@')) != NULL
) {
194 tuser
= pwd
->pw_name
;
195 if ((rmt
= getenv("RMT")) == NULL
)
200 const char *rshcmd
[6];
208 /* Restore the uid and gid. We really don't want
209 * to execute whatever is put into RSH variable with
210 * more priviledges than needed... */
214 if ((rshpid
= piped_child(rshcmd
)) < 0) {
215 msg("cannot open connection\n");
220 /* Copy rmtpeer to rmtpeercopy to ignore the
221 return value from rcmd. I cannot figure if
222 this is this a bug in rcmd or in my code... */
223 rmtpeercopy
= (char *)rmtpeer
;
226 tormtape
= krcmd(&rmtpeercopy
, sp
->s_port
, tuser
, rmt
, &errfd
,
230 tormtape
= rcmd(&rmtpeercopy
, (u_short
)sp
->s_port
, pwd
->pw_name
,
233 msg("login to %s as %s failed.\n", rmtpeer
, tuser
);
236 size
= ntrec
* TP_BSIZE
;
237 if (size
> 60 * 1024) /* XXX */
239 /* Leave some space for rmt request/response protocol */
241 while (size
> TP_BSIZE
&&
242 setsockopt(tormtape
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof (size
)) < 0)
244 (void)setsockopt(tormtape
, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
));
245 throughput
= IPTOS_THROUGHPUT
;
246 if (setsockopt(tormtape
, IPPROTO_IP
, IP_TOS
,
247 &throughput
, sizeof(throughput
)) < 0)
248 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
250 if (setsockopt(tormtape
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof (on
)) < 0)
251 perror("TCP_NODELAY setsockopt");
252 fromrmtape
= tormtape
;
254 (void)fprintf(stderr
, "Connection to %s established.\n", rmtpeer
);
259 okname(const char *cp0
)
261 register const char *cp
;
264 for (cp
= cp0
; *cp
; cp
++) {
266 if (!isascii(c
) || !(isalnum(c
) || c
== '_' || c
== '-')) {
267 warnx("invalid user name %s\n", cp0
);
275 rmtopen(const char *tape
, int mode
)
277 char buf
[MAXPATHLEN
];
279 (void)snprintf(buf
, sizeof (buf
), "O%s\n%d\n", tape
, mode
);
281 return (rmtcall(tape
, buf
));
288 if (rmtstate
!= TS_OPEN
)
290 rmtcall("close", "C\n");
291 rmtstate
= TS_CLOSED
;
295 rmtread(char *buf
, size_t count
)
301 (void)snprintf(line
, sizeof (line
), "R%u\n", (unsigned)count
);
302 n
= rmtcall("read", line
);
304 /* rmtcall() properly sets errno for us on errors. */
306 for (i
= 0; i
< n
; i
+= cc
) {
307 cc
= read(fromrmtape
, buf
+i
, n
- i
);
315 rmtwrite(const char *buf
, size_t count
)
319 (void)snprintf(line
, sizeof (line
), "W%d\n", count
);
320 write(tormtape
, line
, strlen(line
));
321 write(tormtape
, buf
, count
);
322 return (rmtreply("write"));
326 rmtseek(int offset
, int pos
)
330 (void)snprintf(line
, sizeof (line
), "L%d\n%d\n", offset
, pos
);
331 return (rmtcall("seek", line
));
342 if (rmtstate
!= TS_OPEN
)
344 rmtcall("status", "S\n");
345 for (i
= 0, cp
= (char *)&mts
; i
< sizeof(mts
); i
++)
351 rmtioctl(int cmd
, int count
)
357 (void)snprintf(buf
, sizeof (buf
), "I%d\n%d\n", cmd
, count
);
358 return (rmtcall("ioctl", buf
));
362 rmtcall(const char *cmd
, const char *buf
)
365 if (write(tormtape
, buf
, strlen(buf
)) != strlen(buf
))
367 return (rmtreply(cmd
));
371 rmtreply(const char *cmd
)
374 char code
[30], emsg
[BUFSIZ
];
376 rmtgets(code
, sizeof (code
));
377 if (*code
== 'E' || *code
== 'F') {
378 rmtgets(emsg
, sizeof (emsg
));
379 msg("%s: %s", cmd
, emsg
);
380 errno
= atoi(code
+ 1);
382 rmtstate
= TS_CLOSED
;
386 /* Kill trailing newline */
387 cp
= code
+ strlen(code
);
388 if (cp
> code
&& *--cp
== '\n')
391 msg("Protocol to remote tape server botched (code \"%s\").\n",
395 return (atoi(code
+ 1));
403 if (read(fromrmtape
, &c
, 1) != 1)
408 /* Get a line (guaranteed to have a trailing newline). */
410 rmtgets(char *line
, size_t len
)
412 register char *cp
= line
;
424 msg("Protocol to remote tape server botched.\n");
425 msg("(rmtgets got \"%s\").\n", line
);
429 int piped_child(const char **command
) {
431 int to_child_pipe
[2];
432 int from_child_pipe
[2];
434 if (pipe (to_child_pipe
) < 0) {
435 msg ("cannot create pipe: %s\n", strerror(errno
));
438 if (pipe (from_child_pipe
) < 0) {
439 msg ("cannot create pipe: %s\n", strerror(errno
));
444 msg ("cannot fork: %s\n", strerror(errno
));
448 if (dup2 (to_child_pipe
[0], STDIN_FILENO
) < 0) {
449 msg ("cannot dup2 pipe: %s\n", strerror(errno
));
452 if (close (to_child_pipe
[1]) < 0) {
453 msg ("cannot close pipe: %s\n", strerror(errno
));
456 if (close (from_child_pipe
[0]) < 0) {
457 msg ("cannot close pipe: %s\n", strerror(errno
));
460 if (dup2 (from_child_pipe
[1], STDOUT_FILENO
) < 0) {
461 msg ("cannot dup2 pipe: %s\n", strerror(errno
));
464 setpgid(0, getpid());
465 execvp (command
[0], (char *const *) command
);
466 msg("cannot exec %s: %s\n", command
[0], strerror(errno
));
469 if (close (to_child_pipe
[0]) < 0) {
470 msg ("cannot close pipe: %s\n", strerror(errno
));
473 if (close (from_child_pipe
[1]) < 0) {
474 msg ("cannot close pipe: %s\n", strerror(errno
));
477 tormtape
= to_child_pipe
[1];
478 fromrmtape
= from_child_pipe
[0];