]>
git.wh0rd.org - dump.git/blob - common/dumprmt.c
6a79d0761be16c21956a4a1659ee6fd109793cd5
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.4 1999/10/11 13:08:06 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>
81 #include <compaterr.h>
92 #include <ext2fs/ext2fs.h>
95 #include "pathnames.h"
101 static int rmtstate
= TS_CLOSED
;
102 static int rmtape
= -1;
103 static const char *rmtpeer
= 0;
105 static int okname
__P((const char *));
106 static int rmtcall
__P((const char *, const char *));
107 static void rmtconnaborted
__P((int));
108 static int rmtgetb
__P((void));
109 static void rmtgetconn
__P((void));
110 static void rmtgets
__P((char *, size_t));
111 static int rmtreply
__P((const char *));
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
);
138 rmtconnaborted(int signo
)
140 msg("Lost connection to remote host.\n");
149 if (select(errfd
+ 1, &r
, NULL
, NULL
, &t
)) {
153 if ((i
= read(errfd
, buf
, sizeof(buf
) - 1)) > 0) {
155 msg("on %s: %s%s", rmtpeer
, buf
,
156 buf
[i
- 1] == '\n' ? "" : "\n");
168 register const char *rmt
;
169 static struct servent
*sp
= NULL
;
170 static struct passwd
*pwd
= NULL
;
177 sp
= getservbyname(dokerberos
? "kshell" : "shell", "tcp");
179 errx(1, "%s/tcp: unknown service",
180 dokerberos
? "kshell" : "shell");
181 pwd
= getpwuid(getuid());
183 errx(1, "who are you?");
185 if ((cp
= strchr(rmtpeer
, '@')) != NULL
) {
192 tuser
= pwd
->pw_name
;
193 if ((rmt
= getenv("RMT")) == NULL
)
198 rmtape
= krcmd((char **)&rmtpeer
, sp
->s_port
, tuser
, rmt
, &errfd
,
202 rmtape
= rcmd((char **)&rmtpeer
, (u_short
)sp
->s_port
, pwd
->pw_name
,
205 msg("login to %s as %s failed.\n", rmtpeer
, tuser
);
208 (void)fprintf(stderr
, "Connection to %s established.\n", rmtpeer
);
209 size
= ntrec
* TP_BSIZE
;
210 if (size
> 60 * 1024) /* XXX */
212 /* Leave some space for rmt request/response protocol */
214 while (size
> TP_BSIZE
&&
215 setsockopt(rmtape
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof (size
)) < 0)
217 (void)setsockopt(rmtape
, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
));
218 throughput
= IPTOS_THROUGHPUT
;
219 if (setsockopt(rmtape
, IPPROTO_IP
, IP_TOS
,
220 &throughput
, sizeof(throughput
)) < 0)
221 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
223 if (setsockopt(rmtape
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof (on
)) < 0)
224 perror("TCP_NODELAY setsockopt");
228 okname(const char *cp0
)
230 register const char *cp
;
233 for (cp
= cp0
; *cp
; cp
++) {
235 if (!isascii(c
) || !(isalnum(c
) || c
== '_' || c
== '-')) {
236 warnx("invalid user name %s\n", cp0
);
244 rmtopen(const char *tape
, int mode
)
246 char buf
[MAXPATHLEN
];
248 (void)snprintf(buf
, sizeof (buf
), "O%s\n%d\n", tape
, mode
);
250 return (rmtcall(tape
, buf
));
257 if (rmtstate
!= TS_OPEN
)
259 rmtcall("close", "C\n");
260 rmtstate
= TS_CLOSED
;
264 rmtread(char *buf
, size_t count
)
270 (void)snprintf(line
, sizeof (line
), "R%u\n", (unsigned)count
);
271 n
= rmtcall("read", line
);
273 /* rmtcall() properly sets errno for us on errors. */
275 for (i
= 0; i
< n
; i
+= cc
) {
276 cc
= read(rmtape
, buf
+i
, n
- i
);
284 rmtwrite(const char *buf
, size_t count
)
288 (void)snprintf(line
, sizeof (line
), "W%d\n", count
);
289 write(rmtape
, line
, strlen(line
));
290 write(rmtape
, buf
, count
);
291 return (rmtreply("write"));
295 rmtseek(int offset
, int pos
)
299 (void)snprintf(line
, sizeof (line
), "L%d\n%d\n", offset
, pos
);
300 return (rmtcall("seek", line
));
311 if (rmtstate
!= TS_OPEN
)
313 rmtcall("status", "S\n");
314 for (i
= 0, cp
= (char *)&mts
; i
< sizeof(mts
); i
++)
320 rmtioctl(int cmd
, int count
)
326 (void)snprintf(buf
, sizeof (buf
), "I%d\n%d\n", cmd
, count
);
327 return (rmtcall("ioctl", buf
));
331 rmtcall(const char *cmd
, const char *buf
)
334 if (write(rmtape
, buf
, strlen(buf
)) != strlen(buf
))
336 return (rmtreply(cmd
));
340 rmtreply(const char *cmd
)
343 char code
[30], emsg
[BUFSIZ
];
345 rmtgets(code
, sizeof (code
));
346 if (*code
== 'E' || *code
== 'F') {
347 rmtgets(emsg
, sizeof (emsg
));
348 msg("%s: %s", cmd
, emsg
);
349 errno
= atoi(code
+ 1);
351 rmtstate
= TS_CLOSED
;
355 /* Kill trailing newline */
356 cp
= code
+ strlen(code
);
357 if (cp
> code
&& *--cp
== '\n')
360 msg("Protocol to remote tape server botched (code \"%s\").\n",
364 return (atoi(code
+ 1));
372 if (read(rmtape
, &c
, 1) != 1)
377 /* Get a line (guaranteed to have a trailing newline). */
379 rmtgets(char *line
, size_t len
)
381 register char *cp
= line
;
393 msg("Protocol to remote tape server botched.\n");
394 msg("(rmtgets got \"%s\").\n", line
);