]> git.wh0rd.org - dump.git/blob - common/dumprmt.c
Added CVS Id.
[dump.git] / common / dumprmt.c
1 /*
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
6 *
7 * $Id: dumprmt.c,v 1.5 1999/10/11 13:31:08 stelian Exp $
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
44 #if 0
45 static char sccsid[] = "@(#)dumprmt.c 8.3 (Berkeley) 4/28/95";
46 #endif
47 static const char rcsid[] =
48 "$Id: dumprmt.c,v 1.5 1999/10/11 13:31:08 stelian Exp $";
49 #endif /* not lint */
50
51 #ifdef __linux__
52 #include <sys/types.h>
53 #include <linux/types.h>
54 #endif
55 #include <sys/param.h>
56 #include <sys/mtio.h>
57 #include <sys/socket.h>
58 #include <sys/time.h>
59 #ifdef __linux__
60 #include <linux/ext2_fs.h>
61 #include <bsdcompat.h>
62 #include <signal.h>
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>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/tcp.h>
77
78 #include <protocols/dumprestore.h>
79
80 #include <ctype.h>
81 #include <errno.h>
82 #include <compaterr.h>
83 #include <netdb.h>
84 #include <pwd.h>
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
102 static int rmtstate = TS_CLOSED;
103 static int rmtape = -1;
104 static const char *rmtpeer = 0;
105
106 static int okname __P((const char *));
107 static int rmtcall __P((const char *, const char *));
108 static void rmtconnaborted __P((int));
109 static int rmtgetb __P((void));
110 static void rmtgetconn __P((void));
111 static void rmtgets __P((char *, size_t));
112 static int rmtreply __P((const char *));
113 #ifdef KERBEROS
114 int krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
115 #endif
116
117 static int errfd = -1;
118 extern int dokerberos;
119 extern int ntrec; /* blocking factor on tape */
120 #ifndef errno
121 extern int errno;
122 #endif
123
124 int
125 rmthost(const char *host)
126 {
127 if (rmtpeer)
128 free((void *)rmtpeer);
129 if ((rmtpeer = strdup(host)) == NULL)
130 rmtpeer = host;
131 signal(SIGPIPE, rmtconnaborted);
132 rmtgetconn();
133 if (rmtape < 0)
134 return (0);
135 return (1);
136 }
137
138 static void
139 rmtconnaborted(int signo)
140 {
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 }
161
162 exit(X_ABORT);
163 }
164
165 static void
166 rmtgetconn(void)
167 {
168 register char *cp;
169 register const char *rmt;
170 static struct servent *sp = NULL;
171 static struct passwd *pwd = NULL;
172 const char *tuser;
173 int size;
174 int throughput;
175 int on;
176
177 if (sp == NULL) {
178 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
179 if (sp == NULL)
180 errx(1, "%s/tcp: unknown service",
181 dokerberos ? "kshell" : "shell");
182 pwd = getpwuid(getuid());
183 if (pwd == NULL)
184 errx(1, "who are you?");
185 }
186 if ((cp = strchr(rmtpeer, '@')) != NULL) {
187 tuser = rmtpeer;
188 *cp = '\0';
189 if (!okname(tuser))
190 exit(X_STARTUP);
191 rmtpeer = ++cp;
192 } else
193 tuser = pwd->pw_name;
194 if ((rmt = getenv("RMT")) == NULL)
195 rmt = _PATH_RMT;
196 msg("");
197 #ifdef KERBEROS
198 if (dokerberos)
199 rmtape = krcmd((char **)&rmtpeer, sp->s_port, tuser, rmt, &errfd,
200 (char *)0);
201 else
202 #endif
203 rmtape = rcmd((char **)&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
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);
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));
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;
224 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
225 perror("TCP_NODELAY setsockopt");
226 }
227
228 static int
229 okname(const char *cp0)
230 {
231 register const char *cp;
232 register int c;
233
234 for (cp = cp0; *cp; cp++) {
235 c = *cp;
236 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
237 warnx("invalid user name %s\n", cp0);
238 return (0);
239 }
240 }
241 return (1);
242 }
243
244 int
245 rmtopen(const char *tape, int mode)
246 {
247 char buf[MAXPATHLEN];
248
249 (void)snprintf(buf, sizeof (buf), "O%s\n%d\n", tape, mode);
250 rmtstate = TS_OPEN;
251 return (rmtcall(tape, buf));
252 }
253
254 void
255 rmtclose(void)
256 {
257
258 if (rmtstate != TS_OPEN)
259 return;
260 rmtcall("close", "C\n");
261 rmtstate = TS_CLOSED;
262 }
263
264 int
265 rmtread(char *buf, size_t count)
266 {
267 char line[30];
268 int n, i;
269 ssize_t cc;
270
271 (void)snprintf(line, sizeof (line), "R%u\n", (unsigned)count);
272 n = rmtcall("read", line);
273 if (n < 0)
274 /* rmtcall() properly sets errno for us on errors. */
275 return (n);
276 for (i = 0; i < n; i += cc) {
277 cc = read(rmtape, buf+i, n - i);
278 if (cc <= 0)
279 rmtconnaborted(0);
280 }
281 return (n);
282 }
283
284 int
285 rmtwrite(const char *buf, size_t count)
286 {
287 char line[30];
288
289 (void)snprintf(line, sizeof (line), "W%d\n", count);
290 write(rmtape, line, strlen(line));
291 write(rmtape, buf, count);
292 return (rmtreply("write"));
293 }
294
295 int
296 rmtseek(int offset, int pos)
297 {
298 char line[80];
299
300 (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
301 return (rmtcall("seek", line));
302 }
303
304 struct mtget mts;
305
306 struct mtget *
307 rmtstatus(void)
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
320 int
321 rmtioctl(int cmd, int count)
322 {
323 char buf[256];
324
325 if (count < 0)
326 return (-1);
327 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
328 return (rmtcall("ioctl", buf));
329 }
330
331 static int
332 rmtcall(const char *cmd, const char *buf)
333 {
334
335 if (write(rmtape, buf, strlen(buf)) != strlen(buf))
336 rmtconnaborted(0);
337 return (rmtreply(cmd));
338 }
339
340 static int
341 rmtreply(const char *cmd)
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);
350 errno = atoi(code + 1);
351 if (*code == 'F')
352 rmtstate = TS_CLOSED;
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);
363 rmtconnaborted(0);
364 }
365 return (atoi(code + 1));
366 }
367
368 static int
369 rmtgetb(void)
370 {
371 char c;
372
373 if (read(rmtape, &c, 1) != 1)
374 rmtconnaborted(0);
375 return (c);
376 }
377
378 /* Get a line (guaranteed to have a trailing newline). */
379 static void
380 rmtgets(char *line, size_t len)
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);
396 rmtconnaborted(0);
397 }