xref: /freebsd/usr.sbin/rmt/rmt.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #endif
40 #include <sys/cdefs.h>
41 /*
42  * rmt
43  */
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/mtio.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 static int	tape = -1;
55 
56 static char	*record;
57 static int	maxrecsize = -1;
58 
59 #define	SSIZE	64
60 static char	device[SSIZE];
61 static char	count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
62 
63 static char	resp[BUFSIZ];
64 
65 static FILE	*debug;
66 #define	DEBUG(f)	if (debug) fprintf(debug, f)
67 #define	DEBUG1(f,a)	if (debug) fprintf(debug, f, a)
68 #define	DEBUG2(f,a1,a2)	if (debug) fprintf(debug, f, a1, a2)
69 
70 static char	*checkbuf(char *, int);
71 static void	 error(int);
72 static void	 getstring(char *);
73 
74 int
75 main(int argc, char **argv)
76 {
77 	int rval;
78 	char c;
79 	int n, i, cc;
80 
81 	argc--, argv++;
82 	if (argc > 0) {
83 		debug = fopen(*argv, "w");
84 		if (debug == NULL) {
85 			DEBUG1("rmtd: error to open %s\n", *argv);
86 			exit(1);
87 		}
88 		(void)setbuf(debug, (char *)0);
89 	}
90 top:
91 	errno = 0;
92 	rval = 0;
93 	if (read(STDIN_FILENO, &c, 1) != 1)
94 		exit(0);
95 	switch (c) {
96 
97 	case 'O':
98 		if (tape >= 0)
99 			(void) close(tape);
100 		getstring(device);
101 		getstring(mode);
102 		DEBUG2("rmtd: O %s %s\n", device, mode);
103 		/*
104 		 * XXX the rmt protocol does not provide a means to
105 		 * specify the permission bits; allow rw for everyone,
106 		 * as modified by the users umask
107 		 */
108 		tape = open(device, atoi(mode), 0666);
109 		if (tape < 0)
110 			goto ioerror;
111 		goto respond;
112 
113 	case 'C':
114 		DEBUG("rmtd: C\n");
115 		getstring(device);		/* discard */
116 		if (close(tape) < 0)
117 			goto ioerror;
118 		tape = -1;
119 		goto respond;
120 
121 	case 'L':
122 		getstring(count);
123 		getstring(pos);
124 		DEBUG2("rmtd: L %s %s\n", count, pos);
125 		rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
126 		if (rval < 0)
127 			goto ioerror;
128 		goto respond;
129 
130 	case 'W':
131 		getstring(count);
132 		n = atoi(count);
133 		DEBUG1("rmtd: W %s\n", count);
134 		record = checkbuf(record, n);
135 		for (i = 0; i < n; i += cc) {
136 			cc = read(STDIN_FILENO, &record[i], n - i);
137 			if (cc <= 0) {
138 				DEBUG("rmtd: premature eof\n");
139 				exit(2);
140 			}
141 		}
142 		rval = write(tape, record, n);
143 		if (rval < 0)
144 			goto ioerror;
145 		goto respond;
146 
147 	case 'R':
148 		getstring(count);
149 		DEBUG1("rmtd: R %s\n", count);
150 		n = atoi(count);
151 		record = checkbuf(record, n);
152 		rval = read(tape, record, n);
153 		if (rval < 0)
154 			goto ioerror;
155 		(void)sprintf(resp, "A%d\n", rval);
156 		(void)write(STDOUT_FILENO, resp, strlen(resp));
157 		(void)write(STDOUT_FILENO, record, rval);
158 		goto top;
159 
160 	case 'I':
161 		getstring(op);
162 		getstring(count);
163 		DEBUG2("rmtd: I %s %s\n", op, count);
164 		{ struct mtop mtop;
165 		  mtop.mt_op = atoi(op);
166 		  mtop.mt_count = atoi(count);
167 		  if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
168 			goto ioerror;
169 		  rval = mtop.mt_count;
170 		}
171 		goto respond;
172 
173 	case 'S':		/* status */
174 		DEBUG("rmtd: S\n");
175 		{ struct mtget mtget;
176 		  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
177 			goto ioerror;
178 		  rval = sizeof (mtget);
179 		  if (rval > 24)	/* original mtget structure size */
180 			rval = 24;
181 		  (void)sprintf(resp, "A%d\n", rval);
182 		  (void)write(STDOUT_FILENO, resp, strlen(resp));
183 		  (void)write(STDOUT_FILENO, (char *)&mtget, rval);
184 		  goto top;
185 		}
186 
187         case 'V':               /* version */
188                 getstring(op);
189                 DEBUG1("rmtd: V %s\n", op);
190                 rval = 2;
191                 goto respond;
192 
193 	default:
194 		DEBUG1("rmtd: garbage command %c\n", c);
195 		exit(3);
196 	}
197 respond:
198 	DEBUG1("rmtd: A %d\n", rval);
199 	(void)sprintf(resp, "A%d\n", rval);
200 	(void)write(STDOUT_FILENO, resp, strlen(resp));
201 	goto top;
202 ioerror:
203 	error(errno);
204 	goto top;
205 }
206 
207 void
208 getstring(char *bp)
209 {
210 	int i;
211 	char *cp = bp;
212 
213 	for (i = 0; i < SSIZE; i++) {
214 		if (read(STDIN_FILENO, cp+i, 1) != 1)
215 			exit(0);
216 		if (cp[i] == '\n')
217 			break;
218 	}
219 	cp[i] = '\0';
220 }
221 
222 static char *
223 checkbuf(char *rec, int size)
224 {
225 
226 	if (size <= maxrecsize)
227 		return (rec);
228 	if (rec != NULL)
229 		free(rec);
230 	rec = malloc(size);
231 	if (rec == NULL) {
232 		DEBUG("rmtd: cannot allocate buffer space\n");
233 		exit(4);
234 	}
235 	maxrecsize = size;
236 	while (size > 1024 &&
237 	       setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
238 		size -= 1024;
239 	return (rec);
240 }
241 
242 static void
243 error(int num)
244 {
245 
246 	DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
247 	(void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
248 	(void)write(STDOUT_FILENO, resp, strlen(resp));
249 }
250