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