]>
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
7 * $Id: dumprmt.c,v 1.5 1999/10/11 13:31:08 stelian Exp $
11 * Copyright (c) 1980, 1993
12 * The Regents of the University of California. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 static char sccsid
[] = "@(#)dumprmt.c 8.3 (Berkeley) 4/28/95";
47 static const char rcsid
[] =
48 "$Id: dumprmt.c,v 1.5 1999/10/11 13:31:08 stelian Exp $";
52 #include <sys/types.h>
53 #include <linux/types.h>
55 #include <sys/param.h>
57 #include <sys/socket.h>
60 #include <linux/ext2_fs.h>
61 #include <bsdcompat.h>
65 #include <sys/vnode.h>
67 #include <ufs/inode.h>
69 #include <ufs/ufs/dinode.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/tcp.h>
78 #include <protocols/dumprestore.h>
82 #include <compaterr.h>
93 #include <ext2fs/ext2fs.h>
96 #include "pathnames.h"
102 static int rmtstate
= TS_CLOSED
;
103 static int rmtape
= -1;
104 static const char *rmtpeer
= 0;
106 static int okname
__P((const char *));
107 static int rmtcall
__P((const char *, const char *));
108 static void rmtconnaborted
__P((int));
109 static int rmtgetb
__P((void));
110 static void rmtgetconn
__P((void));
111 static void rmtgets
__P((char *, size_t));
112 static int rmtreply
__P((const char *));
114 int krcmd
__P((char **, int /*u_short*/, char *, char *, int *, char *));
117 static int errfd
= -1;
118 extern int dokerberos
;
119 extern int ntrec
; /* blocking factor on tape */
125 rmthost(const char *host
)
128 free((void *)rmtpeer
);
129 if ((rmtpeer
= strdup(host
)) == NULL
)
131 signal(SIGPIPE
, rmtconnaborted
);
139 rmtconnaborted(int signo
)
141 msg("Lost connection to remote host.\n");
150 if (select(errfd
+ 1, &r
, NULL
, NULL
, &t
)) {
154 if ((i
= read(errfd
, buf
, sizeof(buf
) - 1)) > 0) {
156 msg("on %s: %s%s", rmtpeer
, buf
,
157 buf
[i
- 1] == '\n' ? "" : "\n");
169 register const char *rmt
;
170 static struct servent
*sp
= NULL
;
171 static struct passwd
*pwd
= NULL
;
178 sp
= getservbyname(dokerberos
? "kshell" : "shell", "tcp");
180 errx(1, "%s/tcp: unknown service",
181 dokerberos
? "kshell" : "shell");
182 pwd
= getpwuid(getuid());
184 errx(1, "who are you?");
186 if ((cp
= strchr(rmtpeer
, '@')) != NULL
) {
193 tuser
= pwd
->pw_name
;
194 if ((rmt
= getenv("RMT")) == NULL
)
199 rmtape
= krcmd((char **)&rmtpeer
, sp
->s_port
, tuser
, rmt
, &errfd
,
203 rmtape
= rcmd((char **)&rmtpeer
, (u_short
)sp
->s_port
, pwd
->pw_name
,
206 msg("login to %s as %s failed.\n", rmtpeer
, tuser
);
209 (void)fprintf(stderr
, "Connection to %s established.\n", rmtpeer
);
210 size
= ntrec
* TP_BSIZE
;
211 if (size
> 60 * 1024) /* XXX */
213 /* Leave some space for rmt request/response protocol */
215 while (size
> TP_BSIZE
&&
216 setsockopt(rmtape
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof (size
)) < 0)
218 (void)setsockopt(rmtape
, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
));
219 throughput
= IPTOS_THROUGHPUT
;
220 if (setsockopt(rmtape
, IPPROTO_IP
, IP_TOS
,
221 &throughput
, sizeof(throughput
)) < 0)
222 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
224 if (setsockopt(rmtape
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof (on
)) < 0)
225 perror("TCP_NODELAY setsockopt");
229 okname(const char *cp0
)
231 register const char *cp
;
234 for (cp
= cp0
; *cp
; cp
++) {
236 if (!isascii(c
) || !(isalnum(c
) || c
== '_' || c
== '-')) {
237 warnx("invalid user name %s\n", cp0
);
245 rmtopen(const char *tape
, int mode
)
247 char buf
[MAXPATHLEN
];
249 (void)snprintf(buf
, sizeof (buf
), "O%s\n%d\n", tape
, mode
);
251 return (rmtcall(tape
, buf
));
258 if (rmtstate
!= TS_OPEN
)
260 rmtcall("close", "C\n");
261 rmtstate
= TS_CLOSED
;
265 rmtread(char *buf
, size_t count
)
271 (void)snprintf(line
, sizeof (line
), "R%u\n", (unsigned)count
);
272 n
= rmtcall("read", line
);
274 /* rmtcall() properly sets errno for us on errors. */
276 for (i
= 0; i
< n
; i
+= cc
) {
277 cc
= read(rmtape
, buf
+i
, n
- i
);
285 rmtwrite(const char *buf
, size_t count
)
289 (void)snprintf(line
, sizeof (line
), "W%d\n", count
);
290 write(rmtape
, line
, strlen(line
));
291 write(rmtape
, buf
, count
);
292 return (rmtreply("write"));
296 rmtseek(int offset
, int pos
)
300 (void)snprintf(line
, sizeof (line
), "L%d\n%d\n", offset
, pos
);
301 return (rmtcall("seek", line
));
312 if (rmtstate
!= TS_OPEN
)
314 rmtcall("status", "S\n");
315 for (i
= 0, cp
= (char *)&mts
; i
< sizeof(mts
); i
++)
321 rmtioctl(int cmd
, int count
)
327 (void)snprintf(buf
, sizeof (buf
), "I%d\n%d\n", cmd
, count
);
328 return (rmtcall("ioctl", buf
));
332 rmtcall(const char *cmd
, const char *buf
)
335 if (write(rmtape
, buf
, strlen(buf
)) != strlen(buf
))
337 return (rmtreply(cmd
));
341 rmtreply(const char *cmd
)
344 char code
[30], emsg
[BUFSIZ
];
346 rmtgets(code
, sizeof (code
));
347 if (*code
== 'E' || *code
== 'F') {
348 rmtgets(emsg
, sizeof (emsg
));
349 msg("%s: %s", cmd
, emsg
);
350 errno
= atoi(code
+ 1);
352 rmtstate
= TS_CLOSED
;
356 /* Kill trailing newline */
357 cp
= code
+ strlen(code
);
358 if (cp
> code
&& *--cp
== '\n')
361 msg("Protocol to remote tape server botched (code \"%s\").\n",
365 return (atoi(code
+ 1));
373 if (read(rmtape
, &c
, 1) != 1)
378 /* Get a line (guaranteed to have a trailing newline). */
380 rmtgets(char *line
, size_t len
)
382 register char *cp
= line
;
394 msg("Protocol to remote tape server botched.\n");
395 msg("(rmtgets got \"%s\").\n", line
);