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