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