]> git.wh0rd.org - dump.git/blame - common/dumprmt.c
Version 0.4b5.
[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[] =
47 "$Id: dumprmt.c,v 1.2 1999/10/11 12:53:20 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>
1227625a
SP
80#include <netdb.h>
81#include <pwd.h>
1227625a
SP
82#include <stdio.h>
83#ifdef __STDC__
84#include <stdlib.h>
85#include <string.h>
86#include <unistd.h>
87#endif
88
89#ifdef __linux__
90#include <ext2fs/ext2fs.h>
91#endif
92
93#include "pathnames.h"
94#include "dump.h"
95
96#define TS_CLOSED 0
97#define TS_OPEN 1
98
99static int rmtstate = TS_CLOSED;
100static int rmtape;
101static char *rmtpeer;
102
103static int okname __P((char *));
104static int rmtcall __P((char *, char *));
105static void rmtconnaborted __P((/* int, int */));
106static int rmtgetb __P((void));
107static void rmtgetconn __P((void));
108static void rmtgets __P((char *, int));
109static int rmtreply __P((char *));
b45f51d6
SP
110#ifdef KERBEROS
111int krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
112#endif
1227625a 113
b45f51d6
SP
114static int errfd = -1;
115extern int dokerberos;
1227625a
SP
116extern int ntrec; /* blocking factor on tape */
117
118int
119rmthost(host)
120 char *host;
121{
122
123 rmtpeer = malloc(strlen(host) + 1);
124 if (rmtpeer)
125 strcpy(rmtpeer, host);
126 else
127 rmtpeer = host;
128 signal(SIGPIPE, rmtconnaborted);
129 rmtgetconn();
130 if (rmtape < 0)
131 return (0);
132 return (1);
133}
134
135static void
136rmtconnaborted()
137{
b45f51d6
SP
138 msg("Lost connection to remote host.\n");
139 if (errfd != -1) {
140 fd_set r;
141 struct timeval t;
142
143 FD_ZERO(&r);
144 FD_SET(errfd, &r);
145 t.tv_sec = 0;
146 t.tv_usec = 0;
147 if (select(errfd + 1, &r, NULL, NULL, &t)) {
148 int i;
149 char buf[2048];
150
151 if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
152 buf[i] = '\0';
153 msg("on %s: %s%s", rmtpeer, buf,
154 buf[i - 1] == '\n' ? "" : "\n");
155 }
156 }
157 }
1227625a 158
b45f51d6 159 exit(X_ABORT);
1227625a
SP
160}
161
162void
163rmtgetconn()
164{
165 register char *cp;
b45f51d6 166 register const char *rmt;
1227625a
SP
167 static struct servent *sp = NULL;
168 static struct passwd *pwd = NULL;
1227625a
SP
169 char *tuser;
170 int size;
b45f51d6
SP
171 int throughput;
172 int on;
1227625a
SP
173
174 if (sp == NULL) {
b45f51d6
SP
175 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
176 if (sp == NULL) {
177 msg("%s/tcp: unknown service\n",
178 dokerberos ? "kshell" : "shell");
179 exit(X_STARTUP);
180 }
1227625a 181 pwd = getpwuid(getuid());
b45f51d6
SP
182 if (pwd == NULL) {
183 msg("who are you?\n");
184 exit(X_STARTUP);
185 }
1227625a
SP
186 }
187 if ((cp = strchr(rmtpeer, '@')) != NULL) {
188 tuser = rmtpeer;
189 *cp = '\0';
190 if (!okname(tuser))
b45f51d6 191 exit(X_STARTUP);
1227625a
SP
192 rmtpeer = ++cp;
193 } else
194 tuser = pwd->pw_name;
b45f51d6
SP
195 if ((rmt = getenv("RMT")) == NULL)
196 rmt = _PATH_RMT;
197 msg("");
198#ifdef KERBEROS
199 if (dokerberos)
200 rmtape = krcmd(&rmtpeer, sp->s_port, tuser, rmt, &errfd,
201 (char *)0);
202 else
203#endif
204 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
205 tuser, rmt, &errfd);
206 if (rmtape < 0) {
207 msg("login to %s as %s failed.\n", rmtpeer, tuser);
208 return;
209 }
210 (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
1227625a
SP
211 size = ntrec * TP_BSIZE;
212 if (size > 60 * 1024) /* XXX */
213 size = 60 * 1024;
214 /* Leave some space for rmt request/response protocol */
215 size += 2 * 1024;
216 while (size > TP_BSIZE &&
217 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
218 size -= TP_BSIZE;
219 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
b45f51d6
SP
220 throughput = IPTOS_THROUGHPUT;
221 if (setsockopt(rmtape, IPPROTO_IP, IP_TOS,
222 &throughput, sizeof(throughput)) < 0)
223 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
224 on = 1;
1227625a
SP
225 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
226 perror("TCP_NODELAY setsockopt");
1227625a
SP
227}
228
229static int
230okname(cp0)
231 char *cp0;
232{
233 register char *cp;
234 register int c;
235
236 for (cp = cp0; *cp; cp++) {
237 c = *cp;
238 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
b45f51d6 239 msg("invalid user name %s\n", cp0);
1227625a
SP
240 return (0);
241 }
242 }
243 return (1);
244}
245
246int
247rmtopen(tape, mode)
248 char *tape;
249 int mode;
250{
251 char buf[256];
252
b45f51d6 253 (void)snprintf(buf, sizeof (buf), "O%.226s\n%d\n", tape, mode);
1227625a
SP
254 rmtstate = TS_OPEN;
255 return (rmtcall(tape, buf));
256}
257
258void
259rmtclose()
260{
261
262 if (rmtstate != TS_OPEN)
263 return;
264 rmtcall("close", "C\n");
265 rmtstate = TS_CLOSED;
266}
267
268int
269rmtread(buf, count)
270 char *buf;
271 int count;
272{
273 char line[30];
274 int n, i, cc;
1227625a 275
b45f51d6 276 (void)snprintf(line, sizeof (line), "R%d\n", count);
1227625a 277 n = rmtcall("read", line);
b45f51d6
SP
278 if (n < 0)
279 /* rmtcall() properly sets errno for us on errors. */
280 return (n);
1227625a
SP
281 for (i = 0; i < n; i += cc) {
282 cc = read(rmtape, buf+i, n - i);
b45f51d6 283 if (cc <= 0)
1227625a 284 rmtconnaborted();
1227625a
SP
285 }
286 return (n);
287}
288
289int
290rmtwrite(buf, count)
291 char *buf;
292 int count;
293{
294 char line[30];
295
b45f51d6 296 (void)snprintf(line, sizeof (line), "W%d\n", count);
1227625a
SP
297 write(rmtape, line, strlen(line));
298 write(rmtape, buf, count);
299 return (rmtreply("write"));
300}
301
302void
303rmtwrite0(count)
304 int count;
305{
306 char line[30];
307
b45f51d6 308 (void)snprintf(line, sizeof (line), "W%d\n", count);
1227625a
SP
309 write(rmtape, line, strlen(line));
310}
311
312void
313rmtwrite1(buf, count)
314 char *buf;
315 int count;
316{
317
318 write(rmtape, buf, count);
319}
320
321int
322rmtwrite2()
323{
324
325 return (rmtreply("write"));
326}
327
328int
329rmtseek(offset, pos)
330 int offset, pos;
331{
332 char line[80];
333
b45f51d6 334 (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
1227625a
SP
335 return (rmtcall("seek", line));
336}
337
338struct mtget mts;
339
340struct mtget *
341rmtstatus()
342{
343 register int i;
344 register char *cp;
345
346 if (rmtstate != TS_OPEN)
347 return (NULL);
348 rmtcall("status", "S\n");
349 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
350 *cp++ = rmtgetb();
351 return (&mts);
352}
353
354int
355rmtioctl(cmd, count)
356 int cmd, count;
357{
358 char buf[256];
359
360 if (count < 0)
361 return (-1);
b45f51d6 362 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
1227625a
SP
363 return (rmtcall("ioctl", buf));
364}
365
366static int
367rmtcall(cmd, buf)
368 char *cmd, *buf;
369{
370
371 if (write(rmtape, buf, strlen(buf)) != strlen(buf))
372 rmtconnaborted();
373 return (rmtreply(cmd));
374}
375
376static int
377rmtreply(cmd)
378 char *cmd;
379{
380 register char *cp;
381 char code[30], emsg[BUFSIZ];
b45f51d6 382 extern int errno;
1227625a
SP
383
384 rmtgets(code, sizeof (code));
385 if (*code == 'E' || *code == 'F') {
386 rmtgets(emsg, sizeof (emsg));
387 msg("%s: %s", cmd, emsg);
b45f51d6
SP
388 errno = atoi(code + 1);
389 if (*code == 'F')
1227625a 390 rmtstate = TS_CLOSED;
1227625a
SP
391 return (-1);
392 }
393 if (*code != 'A') {
394 /* Kill trailing newline */
395 cp = code + strlen(code);
396 if (cp > code && *--cp == '\n')
397 *cp = '\0';
398
399 msg("Protocol to remote tape server botched (code \"%s\").\n",
400 code);
401 rmtconnaborted();
402 }
403 return (atoi(code + 1));
404}
405
406int
407rmtgetb()
408{
409 char c;
410
411 if (read(rmtape, &c, 1) != 1)
412 rmtconnaborted();
413 return (c);
414}
415
416/* Get a line (guaranteed to have a trailing newline). */
417void
418rmtgets(line, len)
419 char *line;
420 int len;
421{
422 register char *cp = line;
423
424 while (len > 1) {
425 *cp = rmtgetb();
426 if (*cp == '\n') {
427 cp[1] = '\0';
428 return;
429 }
430 cp++;
431 len--;
432 }
433 *cp = '\0';
434 msg("Protocol to remote tape server botched.\n");
435 msg("(rmtgets got \"%s\").\n", line);
436 rmtconnaborted();
437}