]> git.wh0rd.org - dump.git/blame - common/dumprmt.c
Restore uid and gid before invoking external RSH.
[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
ebcbe7f6 5 * Stelian Pop <pop@cybercable.fr>, 1999-2000
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[] =
2c168f5f 43 "$Id: dumprmt.c,v 1.12 2000/11/03 18:28:58 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;
0c62667d
SP
98static int tormtape = -1;
99static int fromrmtape = -1;
f34aca37 100int rshpid = -1;
ddd2ef55 101static const char *rmtpeer = 0;
1227625a 102
ddd2ef55
SP
103static int okname __P((const char *));
104static int rmtcall __P((const char *, const char *));
105static void rmtconnaborted __P((int));
1227625a 106static int rmtgetb __P((void));
0c62667d 107static int rmtgetconn __P((void));
ddd2ef55
SP
108static void rmtgets __P((char *, size_t));
109static int rmtreply __P((const char *));
0c62667d 110static int piped_child __P((const char **command));
b45f51d6
SP
111#ifdef KERBEROS
112int krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
113#endif
1227625a 114
b45f51d6
SP
115static int errfd = -1;
116extern int dokerberos;
1227625a 117extern int ntrec; /* blocking factor on tape */
ddd2ef55
SP
118#ifndef errno
119extern int errno;
120#endif
1227625a
SP
121
122int
ddd2ef55 123rmthost(const char *host)
1227625a 124{
1227625a 125 if (rmtpeer)
ddd2ef55
SP
126 free((void *)rmtpeer);
127 if ((rmtpeer = strdup(host)) == NULL)
1227625a
SP
128 rmtpeer = host;
129 signal(SIGPIPE, rmtconnaborted);
0c62667d 130 return rmtgetconn();
1227625a
SP
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
0c62667d 160static int
ddd2ef55 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;
0c62667d 168 const char *rsh;
1227625a 169 int size;
b45f51d6
SP
170 int throughput;
171 int on;
8d957ae7 172 char *rmtpeercopy;
1227625a 173
0c62667d
SP
174 rsh = getenv("RSH");
175
176 if (!rsh && 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");
0c62667d
SP
181 }
182 if (pwd == NULL) {
1227625a 183 pwd = getpwuid(getuid());
ddd2ef55
SP
184 if (pwd == NULL)
185 errx(1, "who are you?");
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("");
0c62667d
SP
198
199 if (rsh) {
200 const char *rshcmd[6];
201 rshcmd[0] = rsh;
202 rshcmd[1] = rmtpeer;
203 rshcmd[2] = "-l";
204 rshcmd[3] = tuser;
205 rshcmd[4] = rmt;
206 rshcmd[5] = NULL;
207
2c168f5f
SP
208 /* Restore the uid and gid. We really don't want
209 * to execute whatever is put into RSH variable with
210 * more priviledges than needed... */
211 setuid(getuid());
212 setgid(getgid());
213
f34aca37 214 if ((rshpid = piped_child(rshcmd)) < 0) {
0c62667d
SP
215 msg("cannot open connection\n");
216 return 0;
217 }
218 }
219 else {
8d957ae7
SP
220 /* Copy rmtpeer to rmtpeercopy to ignore the
221 return value from rcmd. I cannot figure if
222 this is this a bug in rcmd or in my code... */
223 rmtpeercopy = (char *)rmtpeer;
b45f51d6 224#ifdef KERBEROS
0c62667d 225 if (dokerberos)
8d957ae7 226 tormtape = krcmd(&rmtpeercopy, sp->s_port, tuser, rmt, &errfd,
0c62667d
SP
227 (char *)0);
228 else
b45f51d6 229#endif
8d957ae7 230 tormtape = rcmd(&rmtpeercopy, (u_short)sp->s_port, pwd->pw_name,
0c62667d
SP
231 tuser, rmt, &errfd);
232 if (tormtape < 0) {
233 msg("login to %s as %s failed.\n", rmtpeer, tuser);
234 return 0;
235 }
236 size = ntrec * TP_BSIZE;
237 if (size > 60 * 1024) /* XXX */
238 size = 60 * 1024;
239 /* Leave some space for rmt request/response protocol */
240 size += 2 * 1024;
241 while (size > TP_BSIZE &&
242 setsockopt(tormtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
243 size -= TP_BSIZE;
244 (void)setsockopt(tormtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
245 throughput = IPTOS_THROUGHPUT;
246 if (setsockopt(tormtape, IPPROTO_IP, IP_TOS,
247 &throughput, sizeof(throughput)) < 0)
248 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
249 on = 1;
250 if (setsockopt(tormtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
251 perror("TCP_NODELAY setsockopt");
252 fromrmtape = tormtape;
b45f51d6
SP
253 }
254 (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
0c62667d 255 return 1;
1227625a
SP
256}
257
258static int
ddd2ef55 259okname(const char *cp0)
1227625a 260{
ddd2ef55 261 register const char *cp;
1227625a
SP
262 register int c;
263
264 for (cp = cp0; *cp; cp++) {
265 c = *cp;
266 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
ddd2ef55 267 warnx("invalid user name %s\n", cp0);
1227625a
SP
268 return (0);
269 }
270 }
271 return (1);
272}
273
274int
ddd2ef55 275rmtopen(const char *tape, int mode)
1227625a 276{
ddd2ef55 277 char buf[MAXPATHLEN];
1227625a 278
ddd2ef55 279 (void)snprintf(buf, sizeof (buf), "O%s\n%d\n", tape, mode);
1227625a
SP
280 rmtstate = TS_OPEN;
281 return (rmtcall(tape, buf));
282}
283
284void
ddd2ef55 285rmtclose(void)
1227625a
SP
286{
287
288 if (rmtstate != TS_OPEN)
289 return;
290 rmtcall("close", "C\n");
291 rmtstate = TS_CLOSED;
292}
293
294int
ddd2ef55 295rmtread(char *buf, size_t count)
1227625a
SP
296{
297 char line[30];
ddd2ef55
SP
298 int n, i;
299 ssize_t cc;
1227625a 300
ddd2ef55 301 (void)snprintf(line, sizeof (line), "R%u\n", (unsigned)count);
1227625a 302 n = rmtcall("read", line);
b45f51d6
SP
303 if (n < 0)
304 /* rmtcall() properly sets errno for us on errors. */
305 return (n);
1227625a 306 for (i = 0; i < n; i += cc) {
0c62667d 307 cc = read(fromrmtape, buf+i, n - i);
b45f51d6 308 if (cc <= 0)
ddd2ef55 309 rmtconnaborted(0);
1227625a
SP
310 }
311 return (n);
312}
313
314int
ddd2ef55 315rmtwrite(const char *buf, size_t count)
1227625a
SP
316{
317 char line[30];
318
b45f51d6 319 (void)snprintf(line, sizeof (line), "W%d\n", count);
0c62667d
SP
320 write(tormtape, line, strlen(line));
321 write(tormtape, buf, count);
1227625a
SP
322 return (rmtreply("write"));
323}
324
1227625a 325int
ddd2ef55 326rmtseek(int offset, int pos)
1227625a
SP
327{
328 char line[80];
329
b45f51d6 330 (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
1227625a
SP
331 return (rmtcall("seek", line));
332}
333
334struct mtget mts;
335
336struct mtget *
ddd2ef55 337rmtstatus(void)
1227625a
SP
338{
339 register int i;
340 register char *cp;
341
342 if (rmtstate != TS_OPEN)
343 return (NULL);
344 rmtcall("status", "S\n");
345 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
346 *cp++ = rmtgetb();
347 return (&mts);
348}
349
350int
ddd2ef55 351rmtioctl(int cmd, int count)
1227625a
SP
352{
353 char buf[256];
354
355 if (count < 0)
356 return (-1);
b45f51d6 357 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
1227625a
SP
358 return (rmtcall("ioctl", buf));
359}
360
361static int
ddd2ef55 362rmtcall(const char *cmd, const char *buf)
1227625a
SP
363{
364
0c62667d 365 if (write(tormtape, buf, strlen(buf)) != strlen(buf))
ddd2ef55 366 rmtconnaborted(0);
1227625a
SP
367 return (rmtreply(cmd));
368}
369
370static int
ddd2ef55 371rmtreply(const char *cmd)
1227625a
SP
372{
373 register char *cp;
374 char code[30], emsg[BUFSIZ];
375
376 rmtgets(code, sizeof (code));
377 if (*code == 'E' || *code == 'F') {
378 rmtgets(emsg, sizeof (emsg));
379 msg("%s: %s", cmd, emsg);
b45f51d6
SP
380 errno = atoi(code + 1);
381 if (*code == 'F')
1227625a 382 rmtstate = TS_CLOSED;
1227625a
SP
383 return (-1);
384 }
385 if (*code != 'A') {
386 /* Kill trailing newline */
387 cp = code + strlen(code);
388 if (cp > code && *--cp == '\n')
389 *cp = '\0';
390
391 msg("Protocol to remote tape server botched (code \"%s\").\n",
392 code);
ddd2ef55 393 rmtconnaborted(0);
1227625a
SP
394 }
395 return (atoi(code + 1));
396}
397
ddd2ef55
SP
398static int
399rmtgetb(void)
1227625a
SP
400{
401 char c;
402
0c62667d 403 if (read(fromrmtape, &c, 1) != 1)
ddd2ef55 404 rmtconnaborted(0);
1227625a
SP
405 return (c);
406}
407
408/* Get a line (guaranteed to have a trailing newline). */
ddd2ef55
SP
409static void
410rmtgets(char *line, size_t len)
1227625a
SP
411{
412 register char *cp = line;
413
414 while (len > 1) {
415 *cp = rmtgetb();
416 if (*cp == '\n') {
417 cp[1] = '\0';
418 return;
419 }
420 cp++;
421 len--;
422 }
423 *cp = '\0';
424 msg("Protocol to remote tape server botched.\n");
425 msg("(rmtgets got \"%s\").\n", line);
ddd2ef55 426 rmtconnaborted(0);
1227625a 427}
0c62667d
SP
428
429int piped_child(const char **command) {
430 int pid;
431 int to_child_pipe[2];
432 int from_child_pipe[2];
433
434 if (pipe (to_child_pipe) < 0) {
435 msg ("cannot create pipe: %s\n", strerror(errno));
436 return -1;
437 }
438 if (pipe (from_child_pipe) < 0) {
439 msg ("cannot create pipe: %s\n", strerror(errno));
440 return -1;
441 }
442 pid = fork ();
443 if (pid < 0) {
444 msg ("cannot fork: %s\n", strerror(errno));
445 return -1;
446 }
447 if (pid == 0) {
448 if (dup2 (to_child_pipe[0], STDIN_FILENO) < 0) {
449 msg ("cannot dup2 pipe: %s\n", strerror(errno));
450 exit(1);
451 }
452 if (close (to_child_pipe[1]) < 0) {
453 msg ("cannot close pipe: %s\n", strerror(errno));
454 exit(1);
455 }
456 if (close (from_child_pipe[0]) < 0) {
457 msg ("cannot close pipe: %s\n", strerror(errno));
458 exit(1);
459 }
460 if (dup2 (from_child_pipe[1], STDOUT_FILENO) < 0) {
461 msg ("cannot dup2 pipe: %s\n", strerror(errno));
462 exit(1);
463 }
b80c25f8 464 setpgid(0, getpid());
0c62667d
SP
465 execvp (command[0], (char *const *) command);
466 msg("cannot exec %s: %s\n", command[0], strerror(errno));
467 exit(1);
468 }
469 if (close (to_child_pipe[0]) < 0) {
470 msg ("cannot close pipe: %s\n", strerror(errno));
471 return -1;
472 }
473 if (close (from_child_pipe[1]) < 0) {
474 msg ("cannot close pipe: %s\n", strerror(errno));
475 return -1;
476 }
477 tormtape = to_child_pipe[1];
478 fromrmtape = from_child_pipe[0];
479 return pid;
480}