xref: /freebsd/usr.sbin/rmt/rmt.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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
35 static 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
42 static char sccsid[] = "@(#)rmt.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45 	"$Id: rmt.c,v 1.1 1998/05/15 12:09:06 hans Exp hans $";
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 
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 __P((char *, int));
79 void	 error __P((int));
80 void	 getstring __P((char *));
81 
82 int
83 main(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 	}
98 top:
99 	errno = 0;
100 	rval = 0;
101 	if (read(0, &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)atol(count), 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(0, &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(1, resp, strlen(resp));
165 		(void)write(1, 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 		  (void)sprintf(resp, "A%d\n", rval);
188 		  (void)write(1, resp, strlen(resp));
189 		  (void)write(1, (char *)&mtget, sizeof (mtget));
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(1, 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(0, 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(record, size)
231 	char *record;
232 	int size;
233 {
234 
235 	if (size <= maxrecsize)
236 		return (record);
237 	if (record != 0)
238 		free(record);
239 	record = malloc(size);
240 	if (record == 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 (record);
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(1, resp, strlen(resp));
259 }
260