Imported Upstream version 0.4b43
[debian/dump] / 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 <stelian@popies.net>, 1999-2000
6  *      Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 #ifndef lint
39 static const char rcsid[] =
40         "$Id: dumprmt.c,v 1.30 2010/06/11 11:19:17 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <sys/param.h>
45 #include <sys/mtio.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <fcntl.h>
49 #ifdef __linux__
50 #include <sys/types.h>
51 #ifdef HAVE_EXT2FS_EXT2_FS_H
52 #include <ext2fs/ext2_fs.h>
53 #else
54 #include <linux/ext2_fs.h>
55 #endif
56 #include <bsdcompat.h>
57 #include <signal.h>
58 #elif defined sunos
59 #include <sys/vnode.h>
60
61 #include <ufs/inode.h>
62 #else
63 #include <ufs/ufs/dinode.h>
64 #endif
65
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/tcp.h>
70
71 #include <protocols/dumprestore.h>
72
73 #include <ctype.h>
74 #include <errno.h>
75 #include <compaterr.h>
76 #include <rmtflags.h>
77 #include <netdb.h>
78 #include <pwd.h>
79 #include <stdio.h>
80 #ifdef __STDC__
81 #include <stdlib.h>
82 #include <string.h>
83 #include <unistd.h>
84 #endif
85
86 #ifdef  __linux__
87 #include <ext2fs/ext2fs.h>
88 #endif
89
90 #include <pathnames.h>
91 #include "dump.h"       /* for X_STARTUP, X_ABORT etc */
92
93 #define TS_CLOSED       0
94 #define TS_OPEN         1
95
96 static  int rmtstate = TS_CLOSED;
97 static  int tormtape = -1;
98 static  int fromrmtape = -1;
99 int rshpid = -1;
100 static  const char *rmtpeer = 0;
101
102 static  int okname __P((const char *));
103 static  OFF_T rmtcall __P((const char *, const char *));
104 static  void rmtconnaborted __P((int));
105 static  int rmtgetb __P((void));
106 static  int rmtgetconn __P((void));
107 static  void rmtgets __P((char *, size_t));
108 static  OFF_T rmtreply __P((const char *));
109 static  int piped_child __P((const char **command));
110 #ifdef KERBEROS
111 int     krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
112 #endif
113
114 static  int errfd = -1;
115 extern  int dokerberos;
116 extern  int ntrec;              /* blocking factor on tape */
117 extern  int abortifconnerr;     /* set to 1 if this lib should exit on connection errors
118                                 otherwise just print a message using msg */
119 #ifndef errno
120 extern  int errno;
121 #endif
122
123 int
124 rmthost(const char *host)
125 {
126         if (rmtpeer)
127                 free((void *)rmtpeer);
128         if ((rmtpeer = strdup(host)) == NULL)
129                 rmtpeer = host;
130         signal(SIGPIPE, rmtconnaborted);
131         return rmtgetconn();
132 }
133
134 static void
135 rmtconnaborted(UNUSED(int signo))
136 {
137         msg("Lost connection to remote host.\n");
138         if (errfd != -1) {
139                 fd_set r;
140                 struct timeval t;
141
142                 FD_ZERO(&r);
143                 FD_SET(errfd, &r);
144                 t.tv_sec = 0;
145                 t.tv_usec = 0;
146                 if (select(errfd + 1, &r, NULL, NULL, &t)) {
147                         int i;
148                         char buf[2048];
149
150                         if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
151                                 buf[i] = '\0';
152                                 msg("on %s: %s%s", rmtpeer, buf,
153                                         buf[i - 1] == '\n' ? "" : "\n");
154                         }
155                 }
156         }
157         if (abortifconnerr)
158                 exit(X_ABORT);
159 }
160
161 static int
162 rmtgetconn(void)
163 {
164         char *cp;
165         const char *rmt;
166         static struct servent *sp = NULL;
167         static struct passwd *pwd = NULL;
168         const char *tuser;
169         const char *rsh;
170         int size;
171         int throughput;
172         int on;
173         char *rmtpeercopy;
174
175         rsh = getenv("RSH");
176
177         if (!rsh && sp == NULL) {
178                 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
179                 if (sp == NULL) {
180                         if (abortifconnerr) {
181                                 errx(1, "%s/tcp: unknown service", dokerberos ? "kshell" : "shell");
182                         } else {
183                                 msg("%s/tcp: unknown service", dokerberos ? "kshell" : "shell");
184                                 return 0;
185                         }
186                 }
187         }
188         if (pwd == NULL) {
189                 pwd = getpwuid(getuid());
190                 if (pwd == NULL) {
191                         if (abortifconnerr) {
192                                 errx(1, "who are you?");
193                         } else {
194                                 msg("who are you?");
195                                 return 0;
196                         }
197                 }
198         }
199         if ((cp = strchr(rmtpeer, '@')) != NULL) {
200                 tuser = rmtpeer;
201                 *cp = '\0';
202                 if (!okname(tuser)) {
203                         if (abortifconnerr) {
204                                 exit(X_STARTUP);
205                         } else {
206                                 return 0;
207                         }
208                 }
209                 rmtpeer = ++cp;
210         } else
211                 tuser = pwd->pw_name;
212         if ((rmt = getenv("RMT")) == NULL)
213                 rmt = _PATH_RMT;
214         msg("");
215
216         if (rsh) {
217                 const char *rshcmd[6];
218                 rshcmd[0] = rsh;
219                 rshcmd[1] = rmtpeer;
220                 rshcmd[2] = "-l";
221                 rshcmd[3] = tuser;
222                 rshcmd[4] = rmt;
223                 rshcmd[5] = NULL;
224
225                 /* Restore the uid and gid. We really don't want
226                  * to execute whatever is put into RSH variable with
227                  * more priviledges than needed... */
228                 setuid(getuid());
229                 setgid(getgid());
230
231                 if ((rshpid = piped_child(rshcmd)) < 0) {
232                         msg("cannot open connection\n");
233                         return 0;
234                 }
235         }
236         else {
237                 /* Copy rmtpeer to rmtpeercopy to ignore the
238                    return value from rcmd. I cannot figure if
239                    this is this a bug in rcmd or in my code... */
240                 rmtpeercopy = (char *)rmtpeer;
241 #ifdef KERBEROS
242                 if (dokerberos)
243                         tormtape = krcmd(&rmtpeercopy, sp->s_port, tuser, rmt, &errfd,
244                                        (char *)0);
245                 else
246 #endif
247                         tormtape = rcmd(&rmtpeercopy, (u_short)sp->s_port, pwd->pw_name,
248                                       tuser, rmt, &errfd);
249                 if (tormtape < 0) {
250                         msg("login to %s as %s failed.\n", rmtpeer, tuser);
251                         return 0;
252                 }
253                 size = ntrec * TP_BSIZE;
254                 if (size > 60 * 1024)           /* XXX */
255                         size = 60 * 1024;
256                 /* Leave some space for rmt request/response protocol */
257                 size += 2 * 1024;
258                 while (size > TP_BSIZE &&
259                     setsockopt(tormtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
260                             size -= TP_BSIZE;
261                 (void)setsockopt(tormtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
262                 throughput = IPTOS_THROUGHPUT;
263                 if (setsockopt(tormtape, IPPROTO_IP, IP_TOS,
264                     &throughput, sizeof(throughput)) < 0)
265                         perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
266                 on = 1;
267                 if (setsockopt(tormtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
268                         perror("TCP_NODELAY setsockopt");
269                 fromrmtape = tormtape;
270         }
271         (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
272         return 1;
273 }
274
275 static int
276 okname(const char *cp0)
277 {
278         const char *cp;
279         int c;
280
281         for (cp = cp0; *cp; cp++) {
282                 c = *cp;
283                 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
284                         warnx("invalid user name %s\n", cp0);
285                         return (0);
286                 }
287         }
288         return (1);
289 }
290
291 int
292 rmtopen(const char *tape, const int mode)
293 {
294         char buf[MAXPATHLEN];
295         char *rmtflags;
296
297         rmtflags = rmtflags_tochar(mode);
298         (void)snprintf(buf, sizeof (buf), "O%s\n%d %s\n", 
299                        tape, 
300                        mode & O_ACCMODE, 
301                        rmtflags);
302         free(rmtflags);
303         rmtstate = TS_OPEN;
304         return (rmtcall(tape, buf));
305 }
306
307 void
308 rmtclose(void)
309 {
310
311         if (rmtstate != TS_OPEN)
312                 return;
313         rmtcall("close", "C\n");
314         rmtstate = TS_CLOSED;
315 }
316
317 int
318 rmtread(char *buf, size_t count)
319 {
320         char line[30];
321         int n, i;
322         ssize_t cc;
323
324         (void)snprintf(line, sizeof (line), "R%u\n", (unsigned)count);
325         n = rmtcall("read", line);
326         if (n < 0) {
327                 /* rmtcall() properly sets errno for us on errors. */
328                 errno = n;
329                 return (-1);
330         }
331         for (i = 0; i < n; i += cc) {
332                 cc = read(fromrmtape, buf+i, n - i);
333                 if (cc <= 0)
334                         rmtconnaborted(0);
335         }
336         return (n);
337 }
338
339 int
340 rmtwrite(const char *buf, size_t count)
341 {
342         char line[30];
343
344         (void)snprintf(line, sizeof (line), "W%ld\n", (long)count);
345         if (write(tormtape, line, strlen(line)) != strlen(line))
346                 return -1;
347         if (write(tormtape, buf, count) != count)
348                 return -1;
349         return (rmtreply("write"));
350 }
351
352 OFF_T
353 rmtseek(OFF_T offset, int pos)
354 {
355         char line[80];
356
357         (void)snprintf(line, sizeof (line), "L%lld\n%d\n", (long long)offset, pos);
358         return (rmtcall("seek", line));
359 }
360
361 struct  mtget mts;
362
363 struct mtget *
364 rmtstatus(void)
365 {
366         int i;
367         char *cp;
368
369         if (rmtstate != TS_OPEN)
370                 return (NULL);
371         i = rmtcall("status", "S");
372         if (i < 0) return NULL;
373         if (i != (int)sizeof(mts)) {
374                 msg("mtget sizes different between host (%d) "
375                     "and remote tape (%d)", sizeof(mts), i);
376                 for ( /* empty */; i > 0; --i)
377                         rmtgetb();
378                 return NULL;
379         }
380         for (i = 0, cp = (char *)&mts; i < (int)sizeof(mts); i++)
381                 *cp++ = rmtgetb();
382         return (&mts);
383 }
384
385 int
386 rmtioctl(int cmd, int count)
387 {
388         char buf[256];
389
390         if (count < 0)
391                 return (-1);
392         (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
393         return (rmtcall("ioctl", buf));
394 }
395
396 static OFF_T
397 rmtcall(const char *cmd, const char *buf)
398 {
399
400         if (write(tormtape, buf, strlen(buf)) != (ssize_t)strlen(buf))
401                 rmtconnaborted(0);
402         return (rmtreply(cmd));
403 }
404
405 static OFF_T
406 rmtreply(const char *cmd)
407 {
408         char *cp;
409         char code[30], emsg[BUFSIZ];
410
411         rmtgets(code, sizeof (code));
412         if (*code == 'E' || *code == 'F') {
413                 rmtgets(emsg, sizeof (emsg));
414                 msg("%s: %s", cmd, emsg);
415                 errno = atoi(code + 1);
416                 if (*code == 'F')
417                         rmtstate = TS_CLOSED;
418                 return (-1);
419         }
420         if (*code != 'A') {
421                 /* Kill trailing newline */
422                 cp = code + strlen(code);
423                 if (cp > code && *--cp == '\n')
424                         *cp = '\0';
425
426                 msg("Protocol to remote tape server botched (code \"%s\").\n",
427                     code);
428                 rmtconnaborted(0);
429         }
430         return (OFF_T)(atoll(code + 1));
431 }
432
433 static int
434 rmtgetb(void)
435 {
436         char c;
437
438         if (read(fromrmtape, &c, 1) != 1)
439                 rmtconnaborted(0);
440         return (c);
441 }
442
443 /* Get a line (guaranteed to have a trailing newline). */
444 static void
445 rmtgets(char *line, size_t len)
446 {
447         char *cp = line;
448
449         while (len > 1) {
450                 *cp = rmtgetb();
451                 if (*cp == '\n') {
452                         cp[1] = '\0';
453                         return;
454                 }
455                 cp++;
456                 len--;
457         }
458         *cp = '\0';
459         msg("Protocol to remote tape server botched.\n");
460         msg("(rmtgets got \"%s\").\n", line);
461         rmtconnaborted(0);
462 }
463
464 int piped_child(const char **command) {
465         int pid;
466         int to_child_pipe[2];
467         int from_child_pipe[2];
468
469         if (pipe (to_child_pipe) < 0) {
470                 msg ("cannot create pipe: %s\n", strerror(errno));
471                 return -1;
472         }
473         if (pipe (from_child_pipe) < 0) {
474                 msg ("cannot create pipe: %s\n", strerror(errno)); 
475                 return -1;
476         }
477         pid = fork ();
478         if (pid < 0) {
479                 msg ("cannot fork: %s\n", strerror(errno));
480                 return -1;
481         }
482         if (pid == 0) {
483                 if (dup2 (to_child_pipe[0], STDIN_FILENO) < 0) {
484                         msg ("cannot dup2 pipe: %s\n", strerror(errno));
485                         exit(1);
486                 }
487                 if (close (to_child_pipe[1]) < 0) {
488                         msg ("cannot close pipe: %s\n", strerror(errno));
489                         exit(1);
490                 }
491                 if (close (from_child_pipe[0]) < 0) {
492                         msg ("cannot close pipe: %s\n", strerror(errno));
493                         exit(1);
494                 }
495                 if (dup2 (from_child_pipe[1], STDOUT_FILENO) < 0) {
496                         msg ("cannot dup2 pipe: %s\n", strerror(errno));
497                         exit(1);
498                 }
499                 setpgid(0, getpid());
500                 execvp (command[0], (char *const *) command);
501                 msg("cannot exec %s: %s\n", command[0], strerror(errno));
502                 exit(1);
503         }
504         if (close (to_child_pipe[0]) < 0) {
505                 msg ("cannot close pipe: %s\n", strerror(errno));
506                 return -1;
507         }
508         if (close (from_child_pipe[1]) < 0) {
509                 msg ("cannot close pipe: %s\n", strerror(errno));
510                 return -1;
511         }
512         tormtape = to_child_pipe[1];
513         fromrmtape = from_child_pipe[0];
514         return pid;
515 }