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