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