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