Home | History | Annotate | Line # | Download | only in dump
dumprmt.c revision 1.19
      1 /*	$NetBSD: dumprmt.c,v 1.19 1997/09/16 06:41:20 lukem 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.19 1997/09/16 06:41:20 lukem 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 <netdb.h>
     67 #include <pwd.h>
     68 #include <signal.h>
     69 #include <stdio.h>
     70 #ifdef __STDC__
     71 #include <stdlib.h>
     72 #include <string.h>
     73 #include <unistd.h>
     74 #endif
     75 
     76 #include "pathnames.h"
     77 #include "dump.h"
     78 
     79 #define	TS_CLOSED	0
     80 #define	TS_OPEN		1
     81 
     82 static	int rmtstate = TS_CLOSED;
     83 static	int rmtape;
     84 static	char *rmtpeer;
     85 
     86 static	int	okname __P((char *));
     87 static	int	rmtcall __P((char *, char *));
     88 	void	rmtclose __P((void));
     89 static	void	rmtconnaborted __P((int));
     90 static	int	rmtgetb __P((void));
     91 static	void	rmtgetconn __P((void));
     92 static	void	rmtgets __P((char *, int));
     93 	int	rmthost __P((char *));
     94 	int	rmtioctl __P((int, int));
     95 	int	rmtopen __P((char *, int));
     96 	int	rmtread __P((char *, int));
     97 static	int	rmtreply __P((char *));
     98 	int	rmtseek __P((int, int));
     99 	int	rmtwrite __P((char *, int));
    100 
    101 extern	int ntrec;		/* blocking factor on tape */
    102 
    103 int
    104 rmthost(host)
    105 	char *host;
    106 {
    107 
    108 	rmtpeer = malloc(strlen(host) + 1);
    109 	if (rmtpeer)
    110 		strcpy(rmtpeer, host);
    111 	else
    112 		rmtpeer = host;
    113 	signal(SIGPIPE, rmtconnaborted);
    114 	rmtgetconn();
    115 	if (rmtape < 0)
    116 		return (0);
    117 	return (1);
    118 }
    119 
    120 static void
    121 rmtconnaborted(dummy)
    122 	int dummy;
    123 {
    124 
    125 	errx(1, "Lost connection to remote host.");
    126 }
    127 
    128 void
    129 rmtgetconn()
    130 {
    131 	char *cp;
    132 	static struct servent *sp = NULL;
    133 	static struct passwd *pwd = NULL;
    134 #ifdef notdef
    135 	static int on = 1;
    136 #endif
    137 	char *tuser, *name;
    138 	int size;
    139 
    140 	if (sp == NULL) {
    141 		sp = getservbyname("shell", "tcp");
    142 		if (sp == NULL)
    143 			errx(1, "shell/tcp: unknown service");
    144 		pwd = getpwuid(getuid());
    145 		if (pwd == NULL)
    146 			errx(1, "who are you?");
    147 	}
    148 	if ((name = strdup(pwd->pw_name)) == NULL)
    149 		err(1, "malloc");
    150 	if ((cp = strchr(rmtpeer, '@')) != NULL) {
    151 		tuser = rmtpeer;
    152 		*cp = '\0';
    153 		if (!okname(tuser))
    154 			exit(1);
    155 		rmtpeer = ++cp;
    156 	} else
    157 		tuser = name;
    158 
    159 	rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, tuser, _PATH_RMT,
    160 	    (int *)0);
    161 	(void)free(name);
    162 	if (rmtape < 0)
    163 		return;
    164 
    165 	size = ntrec * TP_BSIZE;
    166 	if (size > 60 * 1024)		/* XXX */
    167 		size = 60 * 1024;
    168 	/* Leave some space for rmt request/response protocol */
    169 	size += 2 * 1024;
    170 	while (size > TP_BSIZE &&
    171 	    setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
    172 		    size -= TP_BSIZE;
    173 	(void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
    174 }
    175 
    176 static int
    177 okname(cp0)
    178 	char *cp0;
    179 {
    180 	char *cp;
    181 	int c;
    182 
    183 	for (cp = cp0; *cp; cp++) {
    184 		c = *cp;
    185 		if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
    186 			warnx("invalid user name: %s", cp0);
    187 			return (0);
    188 		}
    189 	}
    190 	return (1);
    191 }
    192 
    193 int
    194 rmtopen(tape, mode)
    195 	char *tape;
    196 	int mode;
    197 {
    198 	char buf[256];
    199 
    200 	(void)snprintf(buf, sizeof buf, "O%s\n%d\n", tape, mode);
    201 	rmtstate = TS_OPEN;
    202 	return (rmtcall(tape, buf));
    203 }
    204 
    205 void
    206 rmtclose()
    207 {
    208 
    209 	if (rmtstate != TS_OPEN)
    210 		return;
    211 	rmtcall("close", "C\n");
    212 	rmtstate = TS_CLOSED;
    213 }
    214 
    215 int
    216 rmtread(buf, count)
    217 	char *buf;
    218 	int count;
    219 {
    220 	char line[30];
    221 	int n, i, cc;
    222 	extern errno;
    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