]> git.wh0rd.org - dump.git/blob - common/dumprmt.c
LFS compatibility.
[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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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
43 static const char rcsid[] =
44 "$Id: dumprmt.c,v 1.15 2000/12/21 11:14:53 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #ifdef __linux__
49 #include <sys/types.h>
50 #include <linux/types.h>
51 #endif
52 #include <sys/param.h>
53 #include <sys/mtio.h>
54 #include <sys/socket.h>
55 #include <sys/time.h>
56 #ifdef __linux__
57 #include <linux/ext2_fs.h>
58 #include <bsdcompat.h>
59 #include <signal.h>
60 #else
61 #ifdef sunos
62 #include <sys/vnode.h>
63
64 #include <ufs/inode.h>
65 #else
66 #include <ufs/ufs/dinode.h>
67 #endif
68 #endif
69
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/tcp.h>
74
75 #include <protocols/dumprestore.h>
76
77 #include <ctype.h>
78 #include <errno.h>
79 #include <compaterr.h>
80 #include <netdb.h>
81 #include <pwd.h>
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
99 static int rmtstate = TS_CLOSED;
100 static int tormtape = -1;
101 static int fromrmtape = -1;
102 int rshpid = -1;
103 static const char *rmtpeer = 0;
104
105 static int okname __P((const char *));
106 static int rmtcall __P((const char *, const char *));
107 static void rmtconnaborted __P((int));
108 static int rmtgetb __P((void));
109 static int rmtgetconn __P((void));
110 static void rmtgets __P((char *, size_t));
111 static int rmtreply __P((const char *));
112 static int piped_child __P((const char **command));
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 return rmtgetconn();
133 }
134
135 static void
136 rmtconnaborted(int signo)
137 {
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 }
158
159 exit(X_ABORT);
160 }
161
162 static int
163 rmtgetconn(void)
164 {
165 register char *cp;
166 register const char *rmt;
167 static struct servent *sp = NULL;
168 static struct passwd *pwd = NULL;
169 const char *tuser;
170 const char *rsh;
171 int size;
172 int throughput;
173 int on;
174 char *rmtpeercopy;
175
176 rsh = getenv("RSH");
177
178 if (!rsh && sp == NULL) {
179 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
180 if (sp == NULL)
181 errx(1, "%s/tcp: unknown service",
182 dokerberos ? "kshell" : "shell");
183 }
184 if (pwd == NULL) {
185 pwd = getpwuid(getuid());
186 if (pwd == NULL)
187 errx(1, "who are you?");
188 }
189 if ((cp = strchr(rmtpeer, '@')) != NULL) {
190 tuser = rmtpeer;
191 *cp = '\0';
192 if (!okname(tuser))
193 exit(X_STARTUP);
194 rmtpeer = ++cp;
195 } else
196 tuser = pwd->pw_name;
197 if ((rmt = getenv("RMT")) == NULL)
198 rmt = _PATH_RMT;
199 msg("");
200
201 if (rsh) {
202 const char *rshcmd[6];
203 rshcmd[0] = rsh;
204 rshcmd[1] = rmtpeer;
205 rshcmd[2] = "-l";
206 rshcmd[3] = tuser;
207 rshcmd[4] = rmt;
208 rshcmd[5] = NULL;
209
210 /* Restore the uid and gid. We really don't want
211 * to execute whatever is put into RSH variable with
212 * more priviledges than needed... */
213 setuid(getuid());
214 setgid(getgid());
215
216 if ((rshpid = piped_child(rshcmd)) < 0) {
217 msg("cannot open connection\n");
218 return 0;
219 }
220 }
221 else {
222 /* Copy rmtpeer to rmtpeercopy to ignore the
223 return value from rcmd. I cannot figure if
224 this is this a bug in rcmd or in my code... */
225 rmtpeercopy = (char *)rmtpeer;
226 #ifdef KERBEROS
227 if (dokerberos)
228 tormtape = krcmd(&rmtpeercopy, sp->s_port, tuser, rmt, &errfd,
229 (char *)0);
230 else
231 #endif
232 tormtape = rcmd(&rmtpeercopy, (u_short)sp->s_port, pwd->pw_name,
233 tuser, rmt, &errfd);
234 if (tormtape < 0) {
235 msg("login to %s as %s failed.\n", rmtpeer, tuser);
236 return 0;
237 }
238 size = ntrec * TP_BSIZE;
239 if (size > 60 * 1024) /* XXX */
240 size = 60 * 1024;
241 /* Leave some space for rmt request/response protocol */
242 size += 2 * 1024;
243 while (size > TP_BSIZE &&
244 setsockopt(tormtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
245 size -= TP_BSIZE;
246 (void)setsockopt(tormtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
247 throughput = IPTOS_THROUGHPUT;
248 if (setsockopt(tormtape, IPPROTO_IP, IP_TOS,
249 &throughput, sizeof(throughput)) < 0)
250 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
251 on = 1;
252 if (setsockopt(tormtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
253 perror("TCP_NODELAY setsockopt");
254 fromrmtape = tormtape;
255 }
256 (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
257 return 1;
258 }
259
260 static int
261 okname(const char *cp0)
262 {
263 register const char *cp;
264 register int c;
265
266 for (cp = cp0; *cp; cp++) {
267 c = *cp;
268 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
269 warnx("invalid user name %s\n", cp0);
270 return (0);
271 }
272 }
273 return (1);
274 }
275
276 int
277 rmtopen(const char *tape, int mode)
278 {
279 char buf[MAXPATHLEN];
280
281 (void)snprintf(buf, sizeof (buf), "O%s\n%d\n", tape, mode);
282 rmtstate = TS_OPEN;
283 return (rmtcall(tape, buf));
284 }
285
286 void
287 rmtclose(void)
288 {
289
290 if (rmtstate != TS_OPEN)
291 return;
292 rmtcall("close", "C\n");
293 rmtstate = TS_CLOSED;
294 }
295
296 int
297 rmtread(char *buf, size_t count)
298 {
299 char line[30];
300 int n, i;
301 ssize_t cc;
302
303 (void)snprintf(line, sizeof (line), "R%u\n", (unsigned)count);
304 n = rmtcall("read", line);
305 if (n < 0)
306 /* rmtcall() properly sets errno for us on errors. */
307 return (n);
308 for (i = 0; i < n; i += cc) {
309 cc = read(fromrmtape, buf+i, n - i);
310 if (cc <= 0)
311 rmtconnaborted(0);
312 }
313 return (n);
314 }
315
316 int
317 rmtwrite(const char *buf, size_t count)
318 {
319 char line[30];
320
321 (void)snprintf(line, sizeof (line), "W%d\n", count);
322 write(tormtape, line, strlen(line));
323 write(tormtape, buf, count);
324 return (rmtreply("write"));
325 }
326
327 int
328 rmtseek(int offset, int pos)
329 {
330 char line[80];
331
332 (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
333 return (rmtcall("seek", line));
334 }
335
336 struct mtget mts;
337
338 struct mtget *
339 rmtstatus(void)
340 {
341 register int i;
342 register char *cp;
343
344 if (rmtstate != TS_OPEN)
345 return (NULL);
346 rmtcall("status", "S\n");
347 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
348 *cp++ = rmtgetb();
349 return (&mts);
350 }
351
352 int
353 rmtioctl(int cmd, int count)
354 {
355 char buf[256];
356
357 if (count < 0)
358 return (-1);
359 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
360 return (rmtcall("ioctl", buf));
361 }
362
363 static int
364 rmtcall(const char *cmd, const char *buf)
365 {
366
367 if (write(tormtape, buf, strlen(buf)) != strlen(buf))
368 rmtconnaborted(0);
369 return (rmtreply(cmd));
370 }
371
372 static int
373 rmtreply(const char *cmd)
374 {
375 register char *cp;
376 char code[30], emsg[BUFSIZ];
377
378 rmtgets(code, sizeof (code));
379 if (*code == 'E' || *code == 'F') {
380 rmtgets(emsg, sizeof (emsg));
381 msg("%s: %s", cmd, emsg);
382 errno = atoi(code + 1);
383 if (*code == 'F')
384 rmtstate = TS_CLOSED;
385 return (-1);
386 }
387 if (*code != 'A') {
388 /* Kill trailing newline */
389 cp = code + strlen(code);
390 if (cp > code && *--cp == '\n')
391 *cp = '\0';
392
393 msg("Protocol to remote tape server botched (code \"%s\").\n",
394 code);
395 rmtconnaborted(0);
396 }
397 return (atoi(code + 1));
398 }
399
400 static int
401 rmtgetb(void)
402 {
403 char c;
404
405 if (read(fromrmtape, &c, 1) != 1)
406 rmtconnaborted(0);
407 return (c);
408 }
409
410 /* Get a line (guaranteed to have a trailing newline). */
411 static void
412 rmtgets(char *line, size_t len)
413 {
414 register char *cp = line;
415
416 while (len > 1) {
417 *cp = rmtgetb();
418 if (*cp == '\n') {
419 cp[1] = '\0';
420 return;
421 }
422 cp++;
423 len--;
424 }
425 *cp = '\0';
426 msg("Protocol to remote tape server botched.\n");
427 msg("(rmtgets got \"%s\").\n", line);
428 rmtconnaborted(0);
429 }
430
431 int piped_child(const char **command) {
432 int pid;
433 int to_child_pipe[2];
434 int from_child_pipe[2];
435
436 if (pipe (to_child_pipe) < 0) {
437 msg ("cannot create pipe: %s\n", strerror(errno));
438 return -1;
439 }
440 if (pipe (from_child_pipe) < 0) {
441 msg ("cannot create pipe: %s\n", strerror(errno));
442 return -1;
443 }
444 pid = fork ();
445 if (pid < 0) {
446 msg ("cannot fork: %s\n", strerror(errno));
447 return -1;
448 }
449 if (pid == 0) {
450 if (dup2 (to_child_pipe[0], STDIN_FILENO) < 0) {
451 msg ("cannot dup2 pipe: %s\n", strerror(errno));
452 exit(1);
453 }
454 if (close (to_child_pipe[1]) < 0) {
455 msg ("cannot close pipe: %s\n", strerror(errno));
456 exit(1);
457 }
458 if (close (from_child_pipe[0]) < 0) {
459 msg ("cannot close pipe: %s\n", strerror(errno));
460 exit(1);
461 }
462 if (dup2 (from_child_pipe[1], STDOUT_FILENO) < 0) {
463 msg ("cannot dup2 pipe: %s\n", strerror(errno));
464 exit(1);
465 }
466 setpgid(0, getpid());
467 execvp (command[0], (char *const *) command);
468 msg("cannot exec %s: %s\n", command[0], strerror(errno));
469 exit(1);
470 }
471 if (close (to_child_pipe[0]) < 0) {
472 msg ("cannot close pipe: %s\n", strerror(errno));
473 return -1;
474 }
475 if (close (from_child_pipe[1]) < 0) {
476 msg ("cannot close pipe: %s\n", strerror(errno));
477 return -1;
478 }
479 tormtape = to_child_pipe[1];
480 fromrmtape = from_child_pipe[0];
481 return pid;
482 }