rmt.c revision 83096
1/*
2 * Copyright (c) 1983, 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
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)rmt.c	8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45  "$FreeBSD: head/usr.sbin/rmt/rmt.c 83096 2001-09-05 17:05:31Z ache $";
46#endif /* not lint */
47
48/*
49 * rmt
50 */
51#include <sys/types.h>
52#include <sys/socket.h>
53#include <sys/mtio.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <sgtty.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62int	tape = -1;
63
64char	*record;
65int	maxrecsize = -1;
66
67#define	SSIZE	64
68char	device[SSIZE];
69char	count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
70
71char	resp[BUFSIZ];
72
73FILE	*debug;
74#define	DEBUG(f)	if (debug) fprintf(debug, f)
75#define	DEBUG1(f,a)	if (debug) fprintf(debug, f, a)
76#define	DEBUG2(f,a1,a2)	if (debug) fprintf(debug, f, a1, a2)
77
78char	*checkbuf __P((char *, int));
79void	 error __P((int));
80void	 getstring __P((char *));
81
82int
83main(argc, argv)
84	int argc;
85	char **argv;
86{
87	int rval;
88	char c;
89	int n, i, cc;
90
91	argc--, argv++;
92	if (argc > 0) {
93		debug = fopen(*argv, "w");
94		if (debug == 0)
95			exit(1);
96		(void)setbuf(debug, (char *)0);
97	}
98top:
99	errno = 0;
100	rval = 0;
101	if (read(STDIN_FILENO, &c, 1) != 1)
102		exit(0);
103	switch (c) {
104
105	case 'O':
106		if (tape >= 0)
107			(void) close(tape);
108		getstring(device);
109		getstring(mode);
110		DEBUG2("rmtd: O %s %s\n", device, mode);
111		/*
112		 * XXX the rmt protocol does not provide a means to
113		 * specify the permission bits; allow rw for everyone,
114		 * as modified by the users umask
115		 */
116		tape = open(device, atoi(mode), 0666);
117		if (tape < 0)
118			goto ioerror;
119		goto respond;
120
121	case 'C':
122		DEBUG("rmtd: C\n");
123		getstring(device);		/* discard */
124		if (close(tape) < 0)
125			goto ioerror;
126		tape = -1;
127		goto respond;
128
129	case 'L':
130		getstring(count);
131		getstring(pos);
132		DEBUG2("rmtd: L %s %s\n", count, pos);
133		rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
134		if (rval < 0)
135			goto ioerror;
136		goto respond;
137
138	case 'W':
139		getstring(count);
140		n = atoi(count);
141		DEBUG1("rmtd: W %s\n", count);
142		record = checkbuf(record, n);
143		for (i = 0; i < n; i += cc) {
144			cc = read(STDIN_FILENO, &record[i], n - i);
145			if (cc <= 0) {
146				DEBUG("rmtd: premature eof\n");
147				exit(2);
148			}
149		}
150		rval = write(tape, record, n);
151		if (rval < 0)
152			goto ioerror;
153		goto respond;
154
155	case 'R':
156		getstring(count);
157		DEBUG1("rmtd: R %s\n", count);
158		n = atoi(count);
159		record = checkbuf(record, n);
160		rval = read(tape, record, n);
161		if (rval < 0)
162			goto ioerror;
163		(void)sprintf(resp, "A%d\n", rval);
164		(void)write(STDOUT_FILENO, resp, strlen(resp));
165		(void)write(STDOUT_FILENO, record, rval);
166		goto top;
167
168	case 'I':
169		getstring(op);
170		getstring(count);
171		DEBUG2("rmtd: I %s %s\n", op, count);
172		{ struct mtop mtop;
173		  mtop.mt_op = atoi(op);
174		  mtop.mt_count = atoi(count);
175		  if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
176			goto ioerror;
177		  rval = mtop.mt_count;
178		}
179		goto respond;
180
181	case 'S':		/* status */
182		DEBUG("rmtd: S\n");
183		{ struct mtget mtget;
184		  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
185			goto ioerror;
186		  rval = sizeof (mtget);
187		  if (rval > 24)	/* original mtget structure size */
188			rval = 24;
189		  (void)sprintf(resp, "A%d\n", rval);
190		  (void)write(STDOUT_FILENO, resp, strlen(resp));
191		  (void)write(STDOUT_FILENO, (char *)&mtget, rval);
192		  goto top;
193		}
194
195        case 'V':               /* version */
196                getstring(op);
197                DEBUG1("rmtd: V %s\n", op);
198                rval = 2;
199                goto respond;
200
201	default:
202		DEBUG1("rmtd: garbage command %c\n", c);
203		exit(3);
204	}
205respond:
206	DEBUG1("rmtd: A %d\n", rval);
207	(void)sprintf(resp, "A%d\n", rval);
208	(void)write(STDOUT_FILENO, resp, strlen(resp));
209	goto top;
210ioerror:
211	error(errno);
212	goto top;
213}
214
215void
216getstring(bp)
217	char *bp;
218{
219	int i;
220	char *cp = bp;
221
222	for (i = 0; i < SSIZE; i++) {
223		if (read(STDIN_FILENO, cp+i, 1) != 1)
224			exit(0);
225		if (cp[i] == '\n')
226			break;
227	}
228	cp[i] = '\0';
229}
230
231char *
232checkbuf(record, size)
233	char *record;
234	int size;
235{
236
237	if (size <= maxrecsize)
238		return (record);
239	if (record != 0)
240		free(record);
241	record = malloc(size);
242	if (record == 0) {
243		DEBUG("rmtd: cannot allocate buffer space\n");
244		exit(4);
245	}
246	maxrecsize = size;
247	while (size > 1024 &&
248	       setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
249		size -= 1024;
250	return (record);
251}
252
253void
254error(num)
255	int num;
256{
257
258	DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
259	(void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
260	(void)write(STDOUT_FILENO, resp, strlen(resp));
261}
262