Home | History | Annotate | Line # | Download | only in dump
dumprmt.c revision 1.9
      1  1.9      cgd /*	$NetBSD: dumprmt.c,v 1.9 1995/03/18 14:54:59 cgd Exp $	*/
      2  1.9      cgd 
      3  1.1      cgd /*-
      4  1.6  mycroft  * Copyright (c) 1980, 1993
      5  1.6  mycroft  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1      cgd  * modification, are permitted provided that the following conditions
      9  1.1      cgd  * are met:
     10  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  */
     35  1.1      cgd 
     36  1.1      cgd #ifndef lint
     37  1.9      cgd #if 0
     38  1.9      cgd static char sccsid[] = "@(#)dumprmt.c	8.1 (Berkeley) 6/5/93";
     39  1.9      cgd #else
     40  1.9      cgd static char rcsid[] = "$NetBSD: dumprmt.c,v 1.9 1995/03/18 14:54:59 cgd Exp $";
     41  1.9      cgd #endif
     42  1.1      cgd #endif /* not lint */
     43  1.1      cgd 
     44  1.5      cgd #include <sys/param.h>
     45  1.5      cgd #include <sys/mtio.h>
     46  1.5      cgd #include <sys/ioctl.h>
     47  1.5      cgd #include <sys/socket.h>
     48  1.6  mycroft #include <sys/time.h>
     49  1.6  mycroft #ifdef sunos
     50  1.6  mycroft #include <sys/vnode.h>
     51  1.6  mycroft 
     52  1.6  mycroft #include <ufs/inode.h>
     53  1.5      cgd #else
     54  1.6  mycroft #include <ufs/ufs/dinode.h>
     55  1.5      cgd #endif
     56  1.1      cgd 
     57  1.1      cgd #include <netinet/in.h>
     58  1.6  mycroft #include <netinet/tcp.h>
     59  1.6  mycroft 
     60  1.6  mycroft #include <protocols/dumprestore.h>
     61  1.1      cgd 
     62  1.6  mycroft #include <ctype.h>
     63  1.7  mycroft #include <err.h>
     64  1.1      cgd #include <netdb.h>
     65  1.1      cgd #include <pwd.h>
     66  1.6  mycroft #include <signal.h>
     67  1.6  mycroft #include <stdio.h>
     68  1.1      cgd #ifdef __STDC__
     69  1.1      cgd #include <stdlib.h>
     70  1.1      cgd #include <string.h>
     71  1.6  mycroft #include <unistd.h>
     72  1.1      cgd #endif
     73  1.6  mycroft 
     74  1.1      cgd #include "pathnames.h"
     75  1.6  mycroft #include "dump.h"
     76  1.1      cgd 
     77  1.1      cgd #define	TS_CLOSED	0
     78  1.1      cgd #define	TS_OPEN		1
     79  1.1      cgd 
     80  1.1      cgd static	int rmtstate = TS_CLOSED;
     81  1.6  mycroft static	int rmtape;
     82  1.6  mycroft static	char *rmtpeer;
     83  1.1      cgd 
     84  1.6  mycroft static	int okname __P((char *));
     85  1.6  mycroft static	int rmtcall __P((char *, char *));
     86  1.6  mycroft static	void rmtconnaborted __P((/* int, int */));
     87  1.6  mycroft static	int rmtgetb __P((void));
     88  1.6  mycroft static	void rmtgetconn __P((void));
     89  1.6  mycroft static	void rmtgets __P((char *, int));
     90  1.6  mycroft static	int rmtreply __P((char *));
     91  1.1      cgd 
     92  1.6  mycroft extern	int ntrec;		/* blocking factor on tape */
     93  1.5      cgd 
     94  1.1      cgd int
     95  1.1      cgd rmthost(host)
     96  1.1      cgd 	char *host;
     97  1.1      cgd {
     98  1.1      cgd 
     99  1.6  mycroft 	rmtpeer = malloc(strlen(host) + 1);
    100  1.6  mycroft 	if (rmtpeer)
    101  1.6  mycroft 		strcpy(rmtpeer, host);
    102  1.6  mycroft 	else
    103  1.6  mycroft 		rmtpeer = host;
    104  1.1      cgd 	signal(SIGPIPE, rmtconnaborted);
    105  1.1      cgd 	rmtgetconn();
    106  1.1      cgd 	if (rmtape < 0)
    107  1.1      cgd 		return (0);
    108  1.1      cgd 	return (1);
    109  1.1      cgd }
    110  1.1      cgd 
    111  1.6  mycroft static void
    112  1.1      cgd rmtconnaborted()
    113  1.1      cgd {
    114  1.1      cgd 
    115  1.7  mycroft 	errx(1, "Lost connection to remote host.");
    116  1.1      cgd }
    117  1.1      cgd 
    118  1.1      cgd void
    119  1.1      cgd rmtgetconn()
    120  1.1      cgd {
    121  1.6  mycroft 	register char *cp;
    122  1.6  mycroft 	static struct servent *sp = NULL;
    123  1.6  mycroft 	static struct passwd *pwd = NULL;
    124  1.6  mycroft #ifdef notdef
    125  1.6  mycroft 	static int on = 1;
    126  1.6  mycroft #endif
    127  1.6  mycroft 	char *tuser;
    128  1.1      cgd 	int size;
    129  1.6  mycroft 	int maxseg;
    130  1.1      cgd 
    131  1.6  mycroft 	if (sp == NULL) {
    132  1.1      cgd 		sp = getservbyname("shell", "tcp");
    133  1.7  mycroft 		if (sp == NULL)
    134  1.7  mycroft 			errx(1, "shell/tcp: unknown service");
    135  1.6  mycroft 		pwd = getpwuid(getuid());
    136  1.7  mycroft 		if (pwd == NULL)
    137  1.7  mycroft 			errx(1, "who are you?");
    138  1.1      cgd 	}
    139  1.8  mycroft 	if ((cp = strchr(rmtpeer, '@')) != NULL) {
    140  1.6  mycroft 		tuser = rmtpeer;
    141  1.6  mycroft 		*cp = '\0';
    142  1.6  mycroft 		if (!okname(tuser))
    143  1.6  mycroft 			exit(1);
    144  1.6  mycroft 		rmtpeer = ++cp;
    145  1.6  mycroft 	} else
    146  1.6  mycroft 		tuser = pwd->pw_name;
    147  1.6  mycroft 	rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name, tuser,
    148  1.6  mycroft 	    _PATH_RMT, (int *)0);
    149  1.1      cgd 	size = ntrec * TP_BSIZE;
    150  1.6  mycroft 	if (size > 60 * 1024)		/* XXX */
    151  1.6  mycroft 		size = 60 * 1024;
    152  1.6  mycroft 	/* Leave some space for rmt request/response protocol */
    153  1.6  mycroft 	size += 2 * 1024;
    154  1.1      cgd 	while (size > TP_BSIZE &&
    155  1.1      cgd 	    setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
    156  1.6  mycroft 		    size -= TP_BSIZE;
    157  1.6  mycroft 	(void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
    158  1.6  mycroft 	maxseg = 1024;
    159  1.6  mycroft 	if (setsockopt(rmtape, IPPROTO_TCP, TCP_MAXSEG,
    160  1.6  mycroft 	    &maxseg, sizeof (maxseg)) < 0)
    161  1.6  mycroft 		perror("TCP_MAXSEG setsockopt");
    162  1.6  mycroft 
    163  1.6  mycroft #ifdef notdef
    164  1.6  mycroft 	if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
    165  1.6  mycroft 		perror("TCP_NODELAY setsockopt");
    166  1.6  mycroft #endif
    167  1.6  mycroft }
    168  1.6  mycroft 
    169  1.6  mycroft static int
    170  1.6  mycroft okname(cp0)
    171  1.6  mycroft 	char *cp0;
    172  1.6  mycroft {
    173  1.6  mycroft 	register char *cp;
    174  1.6  mycroft 	register int c;
    175  1.6  mycroft 
    176  1.6  mycroft 	for (cp = cp0; *cp; cp++) {
    177  1.6  mycroft 		c = *cp;
    178  1.6  mycroft 		if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
    179  1.7  mycroft 			warnx("invalid user name: %s", cp0);
    180  1.6  mycroft 			return (0);
    181  1.6  mycroft 		}
    182  1.6  mycroft 	}
    183  1.6  mycroft 	return (1);
    184  1.1      cgd }
    185  1.1      cgd 
    186  1.1      cgd int
    187  1.1      cgd rmtopen(tape, mode)
    188  1.1      cgd 	char *tape;
    189  1.1      cgd 	int mode;
    190  1.1      cgd {
    191  1.1      cgd 	char buf[256];
    192  1.1      cgd 
    193  1.1      cgd 	(void)sprintf(buf, "O%s\n%d\n", tape, mode);
    194  1.1      cgd 	rmtstate = TS_OPEN;
    195  1.1      cgd 	return (rmtcall(tape, buf));
    196  1.1      cgd }
    197  1.1      cgd 
    198  1.1      cgd void
    199  1.1      cgd rmtclose()
    200  1.1      cgd {
    201  1.1      cgd 
    202  1.1      cgd 	if (rmtstate != TS_OPEN)
    203  1.1      cgd 		return;
    204  1.1      cgd 	rmtcall("close", "C\n");
    205  1.1      cgd 	rmtstate = TS_CLOSED;
    206  1.1      cgd }
    207  1.1      cgd 
    208  1.1      cgd int
    209  1.1      cgd rmtread(buf, count)
    210  1.1      cgd 	char *buf;
    211  1.1      cgd 	int count;
    212  1.1      cgd {
    213  1.1      cgd 	char line[30];
    214  1.1      cgd 	int n, i, cc;
    215  1.1      cgd 	extern errno;
    216  1.1      cgd 
    217  1.1      cgd 	(void)sprintf(line, "R%d\n", count);
    218  1.1      cgd 	n = rmtcall("read", line);
    219  1.1      cgd 	if (n < 0) {
    220  1.1      cgd 		errno = n;
    221  1.1      cgd 		return (-1);
    222  1.1      cgd 	}
    223  1.1      cgd 	for (i = 0; i < n; i += cc) {
    224  1.1      cgd 		cc = read(rmtape, buf+i, n - i);
    225  1.1      cgd 		if (cc <= 0) {
    226  1.1      cgd 			rmtconnaborted();
    227  1.1      cgd 		}
    228  1.1      cgd 	}
    229  1.1      cgd 	return (n);
    230  1.1      cgd }
    231  1.1      cgd 
    232  1.1      cgd int
    233  1.1      cgd rmtwrite(buf, count)
    234  1.1      cgd 	char *buf;
    235  1.1      cgd 	int count;
    236  1.1      cgd {
    237  1.1      cgd 	char line[30];
    238  1.1      cgd 
    239  1.1      cgd 	(void)sprintf(line, "W%d\n", count);
    240  1.1      cgd 	write(rmtape, line, strlen(line));
    241  1.1      cgd 	write(rmtape, buf, count);
    242  1.1      cgd 	return (rmtreply("write"));
    243  1.1      cgd }
    244  1.1      cgd 
    245  1.1      cgd void
    246  1.1      cgd rmtwrite0(count)
    247  1.1      cgd 	int count;
    248  1.1      cgd {
    249  1.1      cgd 	char line[30];
    250  1.1      cgd 
    251  1.1      cgd 	(void)sprintf(line, "W%d\n", count);
    252  1.1      cgd 	write(rmtape, line, strlen(line));
    253  1.1      cgd }
    254  1.1      cgd 
    255  1.1      cgd void
    256  1.1      cgd rmtwrite1(buf, count)
    257  1.1      cgd 	char *buf;
    258  1.1      cgd 	int count;
    259  1.1      cgd {
    260  1.1      cgd 
    261  1.1      cgd 	write(rmtape, buf, count);
    262  1.1      cgd }
    263  1.1      cgd 
    264  1.1      cgd int
    265  1.1      cgd rmtwrite2()
    266  1.1      cgd {
    267  1.1      cgd 
    268  1.1      cgd 	return (rmtreply("write"));
    269  1.1      cgd }
    270  1.1      cgd 
    271  1.1      cgd int
    272  1.1      cgd rmtseek(offset, pos)
    273  1.1      cgd 	int offset, pos;
    274  1.1      cgd {
    275  1.1      cgd 	char line[80];
    276  1.1      cgd 
    277  1.1      cgd 	(void)sprintf(line, "L%d\n%d\n", offset, pos);
    278  1.1      cgd 	return (rmtcall("seek", line));
    279  1.1      cgd }
    280  1.1      cgd 
    281  1.1      cgd struct	mtget mts;
    282  1.1      cgd 
    283  1.1      cgd struct mtget *
    284  1.1      cgd rmtstatus()
    285  1.1      cgd {
    286  1.1      cgd 	register int i;
    287  1.1      cgd 	register char *cp;
    288  1.1      cgd 
    289  1.1      cgd 	if (rmtstate != TS_OPEN)
    290  1.6  mycroft 		return (NULL);
    291  1.1      cgd 	rmtcall("status", "S\n");
    292  1.1      cgd 	for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
    293  1.1      cgd 		*cp++ = rmtgetb();
    294  1.1      cgd 	return (&mts);
    295  1.1      cgd }
    296  1.1      cgd 
    297  1.1      cgd int
    298  1.1      cgd rmtioctl(cmd, count)
    299  1.1      cgd 	int cmd, count;
    300  1.1      cgd {
    301  1.1      cgd 	char buf[256];
    302  1.1      cgd 
    303  1.1      cgd 	if (count < 0)
    304  1.1      cgd 		return (-1);
    305  1.1      cgd 	(void)sprintf(buf, "I%d\n%d\n", cmd, count);
    306  1.1      cgd 	return (rmtcall("ioctl", buf));
    307  1.1      cgd }
    308  1.1      cgd 
    309  1.6  mycroft static int
    310  1.1      cgd rmtcall(cmd, buf)
    311  1.1      cgd 	char *cmd, *buf;
    312  1.1      cgd {
    313  1.1      cgd 
    314  1.1      cgd 	if (write(rmtape, buf, strlen(buf)) != strlen(buf))
    315  1.1      cgd 		rmtconnaborted();
    316  1.1      cgd 	return (rmtreply(cmd));
    317  1.1      cgd }
    318  1.1      cgd 
    319  1.6  mycroft static int
    320  1.1      cgd rmtreply(cmd)
    321  1.1      cgd 	char *cmd;
    322  1.1      cgd {
    323  1.6  mycroft 	register char *cp;
    324  1.1      cgd 	char code[30], emsg[BUFSIZ];
    325  1.1      cgd 
    326  1.1      cgd 	rmtgets(code, sizeof (code));
    327  1.1      cgd 	if (*code == 'E' || *code == 'F') {
    328  1.1      cgd 		rmtgets(emsg, sizeof (emsg));
    329  1.6  mycroft 		msg("%s: %s", cmd, emsg);
    330  1.1      cgd 		if (*code == 'F') {
    331  1.1      cgd 			rmtstate = TS_CLOSED;
    332  1.1      cgd 			return (-1);
    333  1.1      cgd 		}
    334  1.1      cgd 		return (-1);
    335  1.1      cgd 	}
    336  1.1      cgd 	if (*code != 'A') {
    337  1.6  mycroft 		/* Kill trailing newline */
    338  1.6  mycroft 		cp = code + strlen(code);
    339  1.6  mycroft 		if (cp > code && *--cp == '\n')
    340  1.6  mycroft 			*cp = '\0';
    341  1.6  mycroft 
    342  1.6  mycroft 		msg("Protocol to remote tape server botched (code \"%s\").\n",
    343  1.1      cgd 		    code);
    344  1.1      cgd 		rmtconnaborted();
    345  1.1      cgd 	}
    346  1.1      cgd 	return (atoi(code + 1));
    347  1.1      cgd }
    348  1.1      cgd 
    349  1.1      cgd int
    350  1.1      cgd rmtgetb()
    351  1.1      cgd {
    352  1.1      cgd 	char c;
    353  1.1      cgd 
    354  1.1      cgd 	if (read(rmtape, &c, 1) != 1)
    355  1.1      cgd 		rmtconnaborted();
    356  1.1      cgd 	return (c);
    357  1.1      cgd }
    358  1.1      cgd 
    359  1.6  mycroft /* Get a line (guaranteed to have a trailing newline). */
    360  1.1      cgd void
    361  1.6  mycroft rmtgets(line, len)
    362  1.6  mycroft 	char *line;
    363  1.1      cgd 	int len;
    364  1.1      cgd {
    365  1.6  mycroft 	register char *cp = line;
    366  1.1      cgd 
    367  1.1      cgd 	while (len > 1) {
    368  1.1      cgd 		*cp = rmtgetb();
    369  1.1      cgd 		if (*cp == '\n') {
    370  1.6  mycroft 			cp[1] = '\0';
    371  1.1      cgd 			return;
    372  1.1      cgd 		}
    373  1.1      cgd 		cp++;
    374  1.1      cgd 		len--;
    375  1.1      cgd 	}
    376  1.6  mycroft 	*cp = '\0';
    377  1.6  mycroft 	msg("Protocol to remote tape server botched.\n");
    378  1.6  mycroft 	msg("(rmtgets got \"%s\").\n", line);
    379  1.1      cgd 	rmtconnaborted();
    380  1.1      cgd }
    381