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