Home | History | Annotate | Line # | Download | only in dump
dumprmt.c revision 1.29
      1 /*	$NetBSD: dumprmt.c,v 1.29 2001/12/25 12:06:26 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.29 2001/12/25 12:06:26 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 #include <ufs/ufs/dinode.h>
     51 
     52 #include <netinet/in.h>
     53 #include <netinet/in_systm.h>
     54 #include <netinet/ip.h>
     55 #include <netinet/tcp.h>
     56 
     57 #include <protocols/dumprestore.h>
     58 
     59 #include <ctype.h>
     60 #include <err.h>
     61 #include <errno.h>
     62 #include <netdb.h>
     63 #include <pwd.h>
     64 #include <signal.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <unistd.h>
     69 
     70 #include "pathnames.h"
     71 #include "dump.h"
     72 
     73 #define	TS_CLOSED	0
     74 #define	TS_OPEN		1
     75 
     76 static	int rmtstate = TS_CLOSED;
     77 static	int rmtape;
     78 static	char *rmtpeer;
     79 
     80 static	int	okname(char *);
     81 static	int	rmtcall(char *, char *, int);
     82 static	void	rmtconnaborted(int);
     83 static	int	rmtgetb(void);
     84 static	void	rmtgetconn(void);
     85 static	void	rmtgets(char *, int);
     86 	int	rmtread(char *, int);
     87 static	int	rmtreply(char *, int);
     88 	int	rmtseek(int, int);
     89 
     90 extern	int ntrec;		/* blocking factor on tape */
     91 
     92 int
     93 rmthost(char *host)
     94 {
     95 
     96 	if ((rmtpeer = strdup(host)) == NULL)
     97 		err(X_STARTUP, "strdup");
     98 	signal(SIGPIPE, rmtconnaborted);
     99 	rmtgetconn();
    100 	if (rmtape < 0)
    101 		return (0);
    102 	return (1);
    103 }
    104 
    105 static void
    106 rmtconnaborted(int dummy)
    107 {
    108 
    109 	errx(X_ABORT, "Lost connection to remote host.");
    110 }
    111 
    112 void
    113 rmtgetconn(void)
    114 {
    115 	char *cp;
    116 	static struct servent *sp = NULL;
    117 	static struct passwd *pwd = NULL;
    118 	char *tuser, *name;
    119 	int size, opt;
    120 
    121 	if (sp == NULL) {
    122 		sp = getservbyname("shell", "tcp");
    123 		if (sp == NULL)
    124 			errx(X_STARTUP, "shell/tcp: unknown service");
    125 		pwd = getpwuid(getuid());
    126 		if (pwd == NULL)
    127 			errx(X_STARTUP, "who are you?");
    128 	}
    129 	if ((name = strdup(pwd->pw_name)) == NULL)
    130 		err(X_STARTUP, "strdup");
    131 	if ((cp = strchr(rmtpeer, '@')) != NULL) {
    132 		tuser = rmtpeer;
    133 		*cp = '\0';
    134 		if (!okname(tuser))
    135 			exit(X_STARTUP);
    136 		rmtpeer = ++cp;
    137 	} else
    138 		tuser = name;
    139 
    140 	rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, tuser, _PATH_RMT,
    141 	    (int *)0);
    142 	(void)free(name);
    143 	if (rmtape < 0)
    144 		return;
    145 
    146 	size = ntrec * TP_BSIZE;
    147 	if (size > 60 * 1024)		/* XXX */
    148 		size = 60 * 1024;
    149 	/* Leave some space for rmt request/response protocol */
    150 	size += 2 * 1024;
    151 	while (size > TP_BSIZE &&
    152 	    setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
    153 		    size -= TP_BSIZE;
    154 	(void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
    155 
    156 	opt = IPTOS_THROUGHPUT;
    157 	(void)setsockopt(rmtape, IPPROTO_IP, IP_TOS, &opt, sizeof (opt));
    158 
    159 	opt = 1;
    160 	(void)setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof (opt));
    161 }
    162 
    163 static int
    164 okname(char *cp0)
    165 {
    166 	char *cp;
    167 	int c;
    168 
    169 	for (cp = cp0; *cp; cp++) {
    170 		c = *cp;
    171 		if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
    172 			warnx("invalid user name: %s", cp0);
    173 			return (0);
    174 		}
    175 	}
    176 	return (1);
    177 }
    178 
    179 int
    180 rmtopen(char *tapedevice, int mode, int verbose)
    181 {
    182 	char buf[256];
    183 
    184 	(void)snprintf(buf, sizeof buf, "O%s\n%d\n", tapedevice, mode);
    185 	rmtstate = TS_OPEN;
    186 	return (rmtcall(tapedevice, buf, verbose));
    187 }
    188 
    189 void
    190 rmtclose(void)
    191 {
    192 
    193 	if (rmtstate != TS_OPEN)
    194 		return;
    195 	rmtcall("close", "C\n", 1);
    196 	rmtstate = TS_CLOSED;
    197 }
    198 
    199 int
    200 rmtread(char *buf, int count)
    201 {
    202 	char line[30];
    203 	int n, i, cc;
    204 
    205 	(void)snprintf(line, sizeof line, "R%d\n", count);
    206 	n = rmtcall("read", line, 1);
    207 	if (n < 0) {
    208 		/* rmtcall() properly sets errno for us on errors. */
    209 		return (n);
    210 	}
    211 	for (i = 0; i < n; i += cc) {
    212 		cc = read(rmtape, buf+i, n - i);
    213 		if (cc <= 0) {
    214 			rmtconnaborted(0);
    215 		}
    216 	}
    217 	return (n);
    218 }
    219 
    220 int
    221 rmtwrite(char *buf, int count)
    222 {
    223 	char line[30];
    224 
    225 	(void)snprintf(line, sizeof line, "W%d\n", count);
    226 	write(rmtape, line, strlen(line));
    227 	write(rmtape, buf, count);
    228 	return (rmtreply("write", 1));
    229 }
    230 
    231 #if 0		/* XXX unused? */
    232 void
    233 rmtwrite0(int count)
    234 {
    235 	char line[30];
    236 
    237 	(void)snprintf(line, sizeof line, "W%d\n", count);
    238 	write(rmtape, line, strlen(line));
    239 }
    240 
    241 void
    242 rmtwrite1(char *buf, int count)
    243 {
    244 
    245 	write(rmtape, buf, count);
    246 }
    247 
    248 int
    249 rmtwrite2(void)
    250 {
    251 
    252 	return (rmtreply("write", 1));
    253 }
    254 #endif
    255 
    256 int
    257 rmtseek(int offset, int pos)
    258 {
    259 	char line[80];
    260 
    261 	(void)snprintf(line, sizeof line, "L%d\n%d\n", offset, pos);
    262 	return (rmtcall("seek", line, 1));
    263 }
    264 
    265 
    266 #if 0		/* XXX unused? */
    267 struct mtget *
    268 rmtstatus(void)
    269 {
    270 	struct	mtget mts;
    271 	int i;
    272 	char *cp;
    273 
    274 	if (rmtstate != TS_OPEN)
    275 		return (NULL);
    276 	rmtcall("status", "S\n", 1);
    277 	for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
    278 		*cp++ = rmtgetb();
    279 	return (&mts);
    280 }
    281 #endif
    282 
    283 int
    284 rmtioctl(int cmd, int count)
    285 {
    286 	char buf[256];
    287 
    288 	if (count < 0)
    289 		return (-1);
    290 	(void)snprintf(buf, sizeof buf, "I%d\n%d\n", cmd, count);
    291 	return (rmtcall("ioctl", buf, 1));
    292 }
    293 
    294 static int
    295 rmtcall(char *cmd, char *buf, int verbose)
    296 {
    297 
    298 	if (write(rmtape, buf, strlen(buf)) != strlen(buf))
    299 		rmtconnaborted(0);
    300 	return (rmtreply(cmd, verbose));
    301 }
    302 
    303 static int
    304 rmtreply(char *cmd, int verbose)
    305 {
    306 	char *cp;
    307 	char code[30], emsg[BUFSIZ];
    308 
    309 	rmtgets(code, sizeof (code));
    310 	if (*code == 'E' || *code == 'F') {
    311 		rmtgets(emsg, sizeof (emsg));
    312 		if (verbose)
    313 			msg("%s: %s", cmd, emsg);
    314 		errno = atoi(code + 1);
    315 		if (*code == 'F')
    316 			rmtstate = TS_CLOSED;
    317 		return (-1);
    318 	}
    319 	if (*code != 'A') {
    320 		/* Kill trailing newline */
    321 		cp = code + strlen(code);
    322 		if (cp > code && *--cp == '\n')
    323 			*cp = '\0';
    324 
    325 		msg("Protocol to remote tape server botched (code \"%s\").\n",
    326 		    code);
    327 		rmtconnaborted(0);
    328 	}
    329 	return (atoi(code + 1));
    330 }
    331 
    332 int
    333 rmtgetb(void)
    334 {
    335 	char c;
    336 
    337 	if (read(rmtape, &c, 1) != 1)
    338 		rmtconnaborted(0);
    339 	return (c);
    340 }
    341 
    342 /* Get a line (guaranteed to have a trailing newline). */
    343 void
    344 rmtgets(char *line, int len)
    345 {
    346 	char *cp = line;
    347 
    348 	while (len > 1) {
    349 		*cp = rmtgetb();
    350 		if (*cp == '\n') {
    351 			cp[1] = '\0';
    352 			return;
    353 		}
    354 		cp++;
    355 		len--;
    356 	}
    357 	*cp = '\0';
    358 	msg("Protocol to remote tape server botched.\n");
    359 	msg("(rmtgets got \"%s\").\n", line);
    360 	rmtconnaborted(0);
    361 }
    362