]> git.wh0rd.org - dump.git/blame - common/dumprmt.c
Added CVS Id.
[dump.git] / common / dumprmt.c
CommitLineData
1227625a
SP
1/*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
b45f51d6
SP
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
1227625a 6 *
ec387a12 7 * $Id: dumprmt.c,v 1.5 1999/10/11 13:31:08 stelian Exp $
1227625a
SP
8 */
9
10/*-
11 * Copyright (c) 1980, 1993
12 * The Regents of the University of California. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
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.
29 *
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
40 * SUCH DAMAGE.
41 */
42
43#ifndef lint
b45f51d6
SP
44#if 0
45static char sccsid[] = "@(#)dumprmt.c 8.3 (Berkeley) 4/28/95";
46#endif
47static const char rcsid[] =
ec387a12 48 "$Id: dumprmt.c,v 1.5 1999/10/11 13:31:08 stelian Exp $";
1227625a
SP
49#endif /* not lint */
50
b45f51d6
SP
51#ifdef __linux__
52#include <sys/types.h>
53#include <linux/types.h>
54#endif
1227625a
SP
55#include <sys/param.h>
56#include <sys/mtio.h>
1227625a
SP
57#include <sys/socket.h>
58#include <sys/time.h>
59#ifdef __linux__
60#include <linux/ext2_fs.h>
61#include <bsdcompat.h>
b45f51d6 62#include <signal.h>
1227625a
SP
63#else
64#ifdef sunos
65#include <sys/vnode.h>
66
67#include <ufs/inode.h>
68#else
69#include <ufs/ufs/dinode.h>
70#endif
71#endif
72
73#include <netinet/in.h>
b45f51d6
SP
74#include <netinet/in_systm.h>
75#include <netinet/ip.h>
1227625a
SP
76#include <netinet/tcp.h>
77
78#include <protocols/dumprestore.h>
79
80#include <ctype.h>
ddd2ef55
SP
81#include <errno.h>
82#include <compaterr.h>
1227625a
SP
83#include <netdb.h>
84#include <pwd.h>
1227625a
SP
85#include <stdio.h>
86#ifdef __STDC__
87#include <stdlib.h>
88#include <string.h>
89#include <unistd.h>
90#endif
91
92#ifdef __linux__
93#include <ext2fs/ext2fs.h>
94#endif
95
96#include "pathnames.h"
97#include "dump.h"
98
99#define TS_CLOSED 0
100#define TS_OPEN 1
101
102static int rmtstate = TS_CLOSED;
ddd2ef55
SP
103static int rmtape = -1;
104static const char *rmtpeer = 0;
1227625a 105
ddd2ef55
SP
106static int okname __P((const char *));
107static int rmtcall __P((const char *, const char *));
108static void rmtconnaborted __P((int));
1227625a
SP
109static int rmtgetb __P((void));
110static void rmtgetconn __P((void));
ddd2ef55
SP
111static void rmtgets __P((char *, size_t));
112static int rmtreply __P((const char *));
b45f51d6
SP
113#ifdef KERBEROS
114int krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
115#endif
1227625a 116
b45f51d6
SP
117static int errfd = -1;
118extern int dokerberos;
1227625a 119extern int ntrec; /* blocking factor on tape */
ddd2ef55
SP
120#ifndef errno
121extern int errno;
122#endif
1227625a
SP
123
124int
ddd2ef55 125rmthost(const char *host)
1227625a 126{
1227625a 127 if (rmtpeer)
ddd2ef55
SP
128 free((void *)rmtpeer);
129 if ((rmtpeer = strdup(host)) == NULL)
1227625a
SP
130 rmtpeer = host;
131 signal(SIGPIPE, rmtconnaborted);
132 rmtgetconn();
133 if (rmtape < 0)
134 return (0);
135 return (1);
136}
137
138static void
ddd2ef55 139rmtconnaborted(int signo)
1227625a 140{
b45f51d6
SP
141 msg("Lost connection to remote host.\n");
142 if (errfd != -1) {
143 fd_set r;
144 struct timeval t;
145
146 FD_ZERO(&r);
147 FD_SET(errfd, &r);
148 t.tv_sec = 0;
149 t.tv_usec = 0;
150 if (select(errfd + 1, &r, NULL, NULL, &t)) {
151 int i;
152 char buf[2048];
153
154 if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
155 buf[i] = '\0';
156 msg("on %s: %s%s", rmtpeer, buf,
157 buf[i - 1] == '\n' ? "" : "\n");
158 }
159 }
160 }
1227625a 161
b45f51d6 162 exit(X_ABORT);
1227625a
SP
163}
164
ddd2ef55
SP
165static void
166rmtgetconn(void)
1227625a
SP
167{
168 register char *cp;
b45f51d6 169 register const char *rmt;
1227625a
SP
170 static struct servent *sp = NULL;
171 static struct passwd *pwd = NULL;
ddd2ef55 172 const char *tuser;
1227625a 173 int size;
b45f51d6
SP
174 int throughput;
175 int on;
1227625a
SP
176
177 if (sp == NULL) {
b45f51d6 178 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
ddd2ef55
SP
179 if (sp == NULL)
180 errx(1, "%s/tcp: unknown service",
b45f51d6 181 dokerberos ? "kshell" : "shell");
1227625a 182 pwd = getpwuid(getuid());
ddd2ef55
SP
183 if (pwd == NULL)
184 errx(1, "who are you?");
1227625a
SP
185 }
186 if ((cp = strchr(rmtpeer, '@')) != NULL) {
187 tuser = rmtpeer;
188 *cp = '\0';
189 if (!okname(tuser))
b45f51d6 190 exit(X_STARTUP);
1227625a
SP
191 rmtpeer = ++cp;
192 } else
193 tuser = pwd->pw_name;
b45f51d6
SP
194 if ((rmt = getenv("RMT")) == NULL)
195 rmt = _PATH_RMT;
196 msg("");
197#ifdef KERBEROS
198 if (dokerberos)
ddd2ef55 199 rmtape = krcmd((char **)&rmtpeer, sp->s_port, tuser, rmt, &errfd,
b45f51d6
SP
200 (char *)0);
201 else
202#endif
ddd2ef55 203 rmtape = rcmd((char **)&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
b45f51d6
SP
204 tuser, rmt, &errfd);
205 if (rmtape < 0) {
206 msg("login to %s as %s failed.\n", rmtpeer, tuser);
207 return;
208 }
209 (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
1227625a
SP
210 size = ntrec * TP_BSIZE;
211 if (size > 60 * 1024) /* XXX */
212 size = 60 * 1024;
213 /* Leave some space for rmt request/response protocol */
214 size += 2 * 1024;
215 while (size > TP_BSIZE &&
216 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
217 size -= TP_BSIZE;
218 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
b45f51d6
SP
219 throughput = IPTOS_THROUGHPUT;
220 if (setsockopt(rmtape, IPPROTO_IP, IP_TOS,
221 &throughput, sizeof(throughput)) < 0)
222 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
223 on = 1;
1227625a
SP
224 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
225 perror("TCP_NODELAY setsockopt");
1227625a
SP
226}
227
228static int
ddd2ef55 229okname(const char *cp0)
1227625a 230{
ddd2ef55 231 register const char *cp;
1227625a
SP
232 register int c;
233
234 for (cp = cp0; *cp; cp++) {
235 c = *cp;
236 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
ddd2ef55 237 warnx("invalid user name %s\n", cp0);
1227625a
SP
238 return (0);
239 }
240 }
241 return (1);
242}
243
244int
ddd2ef55 245rmtopen(const char *tape, int mode)
1227625a 246{
ddd2ef55 247 char buf[MAXPATHLEN];
1227625a 248
ddd2ef55 249 (void)snprintf(buf, sizeof (buf), "O%s\n%d\n", tape, mode);
1227625a
SP
250 rmtstate = TS_OPEN;
251 return (rmtcall(tape, buf));
252}
253
254void
ddd2ef55 255rmtclose(void)
1227625a
SP
256{
257
258 if (rmtstate != TS_OPEN)
259 return;
260 rmtcall("close", "C\n");
261 rmtstate = TS_CLOSED;
262}
263
264int
ddd2ef55 265rmtread(char *buf, size_t count)
1227625a
SP
266{
267 char line[30];
ddd2ef55
SP
268 int n, i;
269 ssize_t cc;
1227625a 270
ddd2ef55 271 (void)snprintf(line, sizeof (line), "R%u\n", (unsigned)count);
1227625a 272 n = rmtcall("read", line);
b45f51d6
SP
273 if (n < 0)
274 /* rmtcall() properly sets errno for us on errors. */
275 return (n);
1227625a
SP
276 for (i = 0; i < n; i += cc) {
277 cc = read(rmtape, buf+i, n - i);
b45f51d6 278 if (cc <= 0)
ddd2ef55 279 rmtconnaborted(0);
1227625a
SP
280 }
281 return (n);
282}
283
284int
ddd2ef55 285rmtwrite(const char *buf, size_t count)
1227625a
SP
286{
287 char line[30];
288
b45f51d6 289 (void)snprintf(line, sizeof (line), "W%d\n", count);
1227625a
SP
290 write(rmtape, line, strlen(line));
291 write(rmtape, buf, count);
292 return (rmtreply("write"));
293}
294
1227625a 295int
ddd2ef55 296rmtseek(int offset, int pos)
1227625a
SP
297{
298 char line[80];
299
b45f51d6 300 (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
1227625a
SP
301 return (rmtcall("seek", line));
302}
303
304struct mtget mts;
305
306struct mtget *
ddd2ef55 307rmtstatus(void)
1227625a
SP
308{
309 register int i;
310 register char *cp;
311
312 if (rmtstate != TS_OPEN)
313 return (NULL);
314 rmtcall("status", "S\n");
315 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
316 *cp++ = rmtgetb();
317 return (&mts);
318}
319
320int
ddd2ef55 321rmtioctl(int cmd, int count)
1227625a
SP
322{
323 char buf[256];
324
325 if (count < 0)
326 return (-1);
b45f51d6 327 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
1227625a
SP
328 return (rmtcall("ioctl", buf));
329}
330
331static int
ddd2ef55 332rmtcall(const char *cmd, const char *buf)
1227625a
SP
333{
334
335 if (write(rmtape, buf, strlen(buf)) != strlen(buf))
ddd2ef55 336 rmtconnaborted(0);
1227625a
SP
337 return (rmtreply(cmd));
338}
339
340static int
ddd2ef55 341rmtreply(const char *cmd)
1227625a
SP
342{
343 register char *cp;
344 char code[30], emsg[BUFSIZ];
345
346 rmtgets(code, sizeof (code));
347 if (*code == 'E' || *code == 'F') {
348 rmtgets(emsg, sizeof (emsg));
349 msg("%s: %s", cmd, emsg);
b45f51d6
SP
350 errno = atoi(code + 1);
351 if (*code == 'F')
1227625a 352 rmtstate = TS_CLOSED;
1227625a
SP
353 return (-1);
354 }
355 if (*code != 'A') {
356 /* Kill trailing newline */
357 cp = code + strlen(code);
358 if (cp > code && *--cp == '\n')
359 *cp = '\0';
360
361 msg("Protocol to remote tape server botched (code \"%s\").\n",
362 code);
ddd2ef55 363 rmtconnaborted(0);
1227625a
SP
364 }
365 return (atoi(code + 1));
366}
367
ddd2ef55
SP
368static int
369rmtgetb(void)
1227625a
SP
370{
371 char c;
372
373 if (read(rmtape, &c, 1) != 1)
ddd2ef55 374 rmtconnaborted(0);
1227625a
SP
375 return (c);
376}
377
378/* Get a line (guaranteed to have a trailing newline). */
ddd2ef55
SP
379static void
380rmtgets(char *line, size_t len)
1227625a
SP
381{
382 register char *cp = line;
383
384 while (len > 1) {
385 *cp = rmtgetb();
386 if (*cp == '\n') {
387 cp[1] = '\0';
388 return;
389 }
390 cp++;
391 len--;
392 }
393 *cp = '\0';
394 msg("Protocol to remote tape server botched.\n");
395 msg("(rmtgets got \"%s\").\n", line);
ddd2ef55 396 rmtconnaborted(0);
1227625a 397}