| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
2 |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#include "config.h" |
| 42 |
#include "rmt.h" |
| 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 |
#include <inttypes.h> |
| 54 |
#include <sys/ioctl.h> |
| 55 |
|
| 56 |
#include "common/rmtflags.h" |
| 57 |
|
| 58 |
static int tape = -1; |
| 59 |
|
| 60 |
static char *record; |
| 61 |
static int maxrecsize = -1; |
| 62 |
|
| 63 |
#define SSIZE 128 |
| 64 |
static char device[SSIZE]; |
| 65 |
static char count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE]; |
| 66 |
|
| 67 |
static char resp[BUFSIZ]; |
| 68 |
|
| 69 |
static FILE *debug; |
| 70 |
#define DEBUG(...) do { if (debug) fprintf(debug, __VA_ARGS__); } while(0) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
> |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static int rmt_version = 0; |
| 87 |
#define RMTI_VERSION -1 |
| 88 |
#define RMT_VERSION 1 |
| 89 |
|
| 90 |
|
| 91 |
#define RMTI_CACHE 0 |
| 92 |
#define RMTI_NOCACHE 1 |
| 93 |
#define RMTI_RETEN 2 |
| 94 |
#define RMTI_ERASE 3 |
| 95 |
#define RMTI_EOM 4 |
| 96 |
#define RMTI_NBSF 5 |
| 97 |
|
| 98 |
|
| 99 |
#define MTS_TYPE 'T' |
| 100 |
#define MTS_DSREG 'D' |
| 101 |
#define MTS_ERREG 'E' |
| 102 |
#define MTS_RESID 'R' |
| 103 |
#define MTS_FILENO 'F' |
| 104 |
#define MTS_BLKNO 'B' |
| 105 |
#define MTS_FLAGS 'f' |
| 106 |
#define MTS_BF 'b' |
| 107 |
|
| 108 |
static char *checkbuf (char *, int); |
| 109 |
static void error (int); |
| 110 |
static void getstring (char *); |
| 111 |
static unsigned long swaplong (unsigned long inv); |
| 112 |
#ifdef ERMT |
| 113 |
char *cipher (char *, int, int); |
| 114 |
void decrypt (void); |
| 115 |
#endif |
| 116 |
|
| 117 |
int |
| 118 |
main(int argc, char *argv[]) |
| 119 |
{ |
| 120 |
off_t rval = 0; |
| 121 |
char c; |
| 122 |
int n, i, cc, oflags; |
| 123 |
unsigned long block = 0; |
| 124 |
char *cp; |
| 125 |
|
| 126 |
int magtape = 0; |
| 127 |
|
| 128 |
#ifdef ERMT |
| 129 |
if (argc > 1 && strcmp(argv[1], "-d") == 0) |
| 130 |
decrypt(); |
| 131 |
#endif |
| 132 |
|
| 133 |
if (argc > 2 && strcmp(argv[1], "-c") == 0) |
| 134 |
argc -= 2, argv += 2; |
| 135 |
argc--, argv++; |
| 136 |
if (argc > 0) { |
| 137 |
debug = fopen(*argv, "w"); |
| 138 |
if (debug == 0) |
| 139 |
exit(1); |
| 140 |
(void)setbuf(debug, (char *)0); |
| 141 |
} |
| 142 |
top: |
| 143 |
errno = 0; |
| 144 |
rval = 0; |
| 145 |
if (read(0, &c, 1) != 1) |
| 146 |
exit(0); |
| 147 |
switch (c) { |
| 148 |
|
| 149 |
case 'O': |
| 150 |
if (tape >= 0) |
| 151 |
(void) close(tape); |
| 152 |
getstring(device); |
| 153 |
getstring(filemode); |
| 154 |
DEBUG("rmtd: O %s %s\n", device, filemode); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
oflags = rmtflags_toint(filemode); |
| 159 |
#ifdef O_TEXT |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if ((oflags & O_TEXT) == 0) |
| 164 |
oflags |= O_BINARY; |
| 165 |
#endif |
| 166 |
DEBUG("rmtd: O %s %d\n", device, oflags); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
tape = open(device, oflags, 0666); |
| 173 |
if (tape < 0) |
| 174 |
goto ioerror; |
| 175 |
block = 0; |
| 176 |
{ |
| 177 |
struct mtget mt_stat; |
| 178 |
magtape = ioctl(tape, MTIOCGET, (char *)&mt_stat) == 0; |
| 179 |
} |
| 180 |
goto respond; |
| 181 |
|
| 182 |
case 'C': |
| 183 |
DEBUG("rmtd: C (%lu blocks)\n", block); |
| 184 |
getstring(device); |
| 185 |
if (close(tape) < 0) |
| 186 |
goto ioerror; |
| 187 |
tape = -1; |
| 188 |
block = 0; |
| 189 |
goto respond; |
| 190 |
|
| 191 |
case 'L': |
| 192 |
getstring(count); |
| 193 |
getstring(pos); |
| 194 |
DEBUG("rmtd: L %s %s\n", count, pos); |
| 195 |
if (!magtape) { |
| 196 |
switch (atoi(pos)) { |
| 197 |
case SEEK_SET: |
| 198 |
case SEEK_CUR: |
| 199 |
case SEEK_END: |
| 200 |
rval = lseek(tape, (off_t)atoll(count), atoi(pos)); |
| 201 |
break; |
| 202 |
#ifdef USE_QFA |
| 203 |
case LSEEK_GET_TAPEPOS: |
| 204 |
rval = lseek(tape, (off_t)0, SEEK_CUR); |
| 205 |
break; |
| 206 |
case LSEEK_GO2_TAPEPOS: |
| 207 |
rval = lseek(tape, (off_t)atoll(count), SEEK_SET); |
| 208 |
break; |
| 209 |
case LSEEK_IS_MAGTAPE: |
| 210 |
rval = 0; |
| 211 |
break; |
| 212 |
#endif |
| 213 |
default: |
| 214 |
errno = EINVAL; |
| 215 |
goto ioerror; |
| 216 |
break; |
| 217 |
} |
| 218 |
} |
| 219 |
else { |
| 220 |
switch (atoi(pos)) { |
| 221 |
case SEEK_SET: |
| 222 |
case SEEK_CUR: |
| 223 |
case SEEK_END: |
| 224 |
rval = lseek(tape, (off_t)atoll(count), atoi(pos)); |
| 225 |
break; |
| 226 |
#ifdef USE_QFA |
| 227 |
case LSEEK_GET_TAPEPOS: |
| 228 |
case LSEEK_GO2_TAPEPOS: |
| 229 |
{ |
| 230 |
struct mtop buf; |
| 231 |
long mtpos; |
| 232 |
|
| 233 |
buf.mt_op = MTSETDRVBUFFER; |
| 234 |
buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL; |
| 235 |
if (ioctl(tape, MTIOCTOP, &buf) < 0) { |
| 236 |
goto ioerror; |
| 237 |
} |
| 238 |
|
| 239 |
if (atoi(pos) == LSEEK_GET_TAPEPOS) { |
| 240 |
if (ioctl(tape, MTIOCPOS, &mtpos) < 0) { |
| 241 |
goto ioerror; |
| 242 |
} |
| 243 |
rval = (off_t)mtpos; |
| 244 |
} else { |
| 245 |
buf.mt_op = MTSEEK; |
| 246 |
buf.mt_count = atoi(count); |
| 247 |
if (ioctl(tape, MTIOCTOP, &buf) < 0) { |
| 248 |
goto ioerror; |
| 249 |
} |
| 250 |
rval = (off_t)buf.mt_count; |
| 251 |
} |
| 252 |
} |
| 253 |
break; |
| 254 |
case LSEEK_IS_MAGTAPE: |
| 255 |
rval = 1; |
| 256 |
break; |
| 257 |
#endif |
| 258 |
default: |
| 259 |
errno = EINVAL; |
| 260 |
goto ioerror; |
| 261 |
} |
| 262 |
} |
| 263 |
if (rval < 0) |
| 264 |
goto ioerror; |
| 265 |
goto respond; |
| 266 |
|
| 267 |
case 'W': |
| 268 |
getstring(count); |
| 269 |
n = atoi(count); |
| 270 |
if (n < 1) |
| 271 |
exit(2); |
| 272 |
DEBUG("rmtd: W %s (block = %lu)\n", count, block); |
| 273 |
record = checkbuf(record, n); |
| 274 |
for (i = 0; i < n; i += cc) { |
| 275 |
cc = read(0, &record[i], n - i); |
| 276 |
if (cc <= 0) { |
| 277 |
DEBUG("rmtd: premature eof\n"); |
| 278 |
exit(2); |
| 279 |
} |
| 280 |
} |
| 281 |
#ifdef ERMT |
| 282 |
if ((cp = cipher(record, n, 1)) == NULL) |
| 283 |
goto ioerror; |
| 284 |
#else |
| 285 |
cp = record; |
| 286 |
#endif |
| 287 |
rval = write(tape, cp, n); |
| 288 |
if (rval < 0) |
| 289 |
goto ioerror; |
| 290 |
block += n >> 10; |
| 291 |
goto respond; |
| 292 |
|
| 293 |
case 'R': |
| 294 |
getstring(count); |
| 295 |
DEBUG("rmtd: R %s (block %lu)\n", count, block); |
| 296 |
n = atoi(count); |
| 297 |
record = checkbuf(record, n); |
| 298 |
rval = read(tape, record, n); |
| 299 |
if (rval < 0) |
| 300 |
goto ioerror; |
| 301 |
#ifdef ERMT |
| 302 |
if ((cp = cipher(record, rval, 0)) == NULL) |
| 303 |
goto ioerror; |
| 304 |
#else |
| 305 |
cp = record; |
| 306 |
#endif |
| 307 |
(void)sprintf(resp, "A%jd\n", (intmax_t)rval); |
| 308 |
if (write(1, resp, strlen(resp)) != (ssize_t)strlen(resp)) |
| 309 |
goto ioerror; |
| 310 |
if (write(1, cp, rval) != rval) |
| 311 |
goto ioerror; |
| 312 |
block += n >> 10; |
| 313 |
goto top; |
| 314 |
|
| 315 |
case 'I': |
| 316 |
getstring(op); |
| 317 |
getstring(count); |
| 318 |
DEBUG("rmtd: I %s %s\n", op, count); |
| 319 |
if (atoi(op) == RMTI_VERSION) { |
| 320 |
rval = RMT_VERSION; |
| 321 |
rmt_version = 1; |
| 322 |
} |
| 323 |
else { |
| 324 |
struct mtop mtop; |
| 325 |
mtop.mt_op = -1; |
| 326 |
if (rmt_version) { |
| 327 |
|
| 328 |
switch (atoi(op)) { |
| 329 |
#ifdef MTWEOF |
| 330 |
case 0: |
| 331 |
mtop.mt_op = MTWEOF; |
| 332 |
break; |
| 333 |
#endif |
| 334 |
#ifdef MTFSF |
| 335 |
case 1: |
| 336 |
mtop.mt_op = MTFSF; |
| 337 |
break; |
| 338 |
#endif |
| 339 |
#ifdef MTBSF |
| 340 |
case 2: |
| 341 |
mtop.mt_op = MTBSF; |
| 342 |
break; |
| 343 |
#endif |
| 344 |
#ifdef MTFSR |
| 345 |
case 3: |
| 346 |
mtop.mt_op = MTFSR; |
| 347 |
break; |
| 348 |
#endif |
| 349 |
#ifdef MTBSR |
| 350 |
case 4: |
| 351 |
mtop.mt_op = MTBSR; |
| 352 |
break; |
| 353 |
#endif |
| 354 |
#ifdef MTREW |
| 355 |
case 5: |
| 356 |
mtop.mt_op = MTREW; |
| 357 |
break; |
| 358 |
#endif |
| 359 |
#ifdef MTOFFL |
| 360 |
case 6: |
| 361 |
mtop.mt_op = MTOFFL; |
| 362 |
break; |
| 363 |
#endif |
| 364 |
#ifdef MTNOP |
| 365 |
case 7: |
| 366 |
mtop.mt_op = MTNOP; |
| 367 |
break; |
| 368 |
#endif |
| 369 |
#ifdef MTRETEN |
| 370 |
case 8: |
| 371 |
mtop.mt_op = MTRETEN; |
| 372 |
break; |
| 373 |
#endif |
| 374 |
#ifdef MTERASE |
| 375 |
case 9: |
| 376 |
mtop.mt_op = MTERASE; |
| 377 |
break; |
| 378 |
#endif |
| 379 |
#ifdef MTEOM |
| 380 |
case 10: |
| 381 |
mtop.mt_op = MTEOM; |
| 382 |
break; |
| 383 |
#endif |
| 384 |
} |
| 385 |
if (mtop.mt_op == -1) { |
| 386 |
errno = EINVAL; |
| 387 |
goto ioerror; |
| 388 |
} |
| 389 |
} |
| 390 |
else { |
| 391 |
|
| 392 |
mtop.mt_op = atoi(op); |
| 393 |
} |
| 394 |
mtop.mt_count = atoi(count); |
| 395 |
if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0) { |
| 396 |
goto ioerror; |
| 397 |
} |
| 398 |
rval = mtop.mt_count; |
| 399 |
} |
| 400 |
goto respond; |
| 401 |
|
| 402 |
case 'i': |
| 403 |
{ struct mtop mtop; |
| 404 |
|
| 405 |
getstring (op); |
| 406 |
getstring (count); |
| 407 |
DEBUG ("rmtd: i %s %s\n", op, count); |
| 408 |
switch (atoi(op)) { |
| 409 |
#ifdef MTCACHE |
| 410 |
case RMTI_CACHE: |
| 411 |
mtop.mt_op = MTCACHE; |
| 412 |
break; |
| 413 |
#endif |
| 414 |
#ifdef MTNOCACHE |
| 415 |
case RMTI_NOCACHE: |
| 416 |
mtop.mt_op = MTNOCACHE; |
| 417 |
break; |
| 418 |
#endif |
| 419 |
#ifdef MTRETEN |
| 420 |
case RMTI_RETEN: |
| 421 |
mtop.mt_op = MTRETEN; |
| 422 |
break; |
| 423 |
#endif |
| 424 |
#ifdef MTERASE |
| 425 |
case RMTI_ERASE: |
| 426 |
mtop.mt_op = MTERASE; |
| 427 |
break; |
| 428 |
#endif |
| 429 |
#ifdef MTEOM |
| 430 |
case RMTI_EOM: |
| 431 |
mtop.mt_op = MTEOM; |
| 432 |
break; |
| 433 |
#endif |
| 434 |
#ifdef MTNBSF |
| 435 |
case RMTI_NBSF: |
| 436 |
mtop.mt_op = MTNBSF; |
| 437 |
break; |
| 438 |
#endif |
| 439 |
default: |
| 440 |
errno = EINVAL; |
| 441 |
goto ioerror; |
| 442 |
} |
| 443 |
mtop.mt_count = atoi (count); |
| 444 |
if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0) { |
| 445 |
goto ioerror; |
| 446 |
} |
| 447 |
|
| 448 |
rval = mtop.mt_count; |
| 449 |
|
| 450 |
goto respond; |
| 451 |
} |
| 452 |
|
| 453 |
case 'S': |
| 454 |
DEBUG("rmtd: S\n"); |
| 455 |
{ struct mtget mtget; |
| 456 |
|
| 457 |
if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0) { |
| 458 |
goto ioerror; |
| 459 |
} |
| 460 |
|
| 461 |
if (rmt_version) { |
| 462 |
rval = sizeof(mtget); |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
mtget.mt_type = swaplong(mtget.mt_type); |
| 468 |
mtget.mt_resid = swaplong(mtget.mt_resid); |
| 469 |
mtget.mt_dsreg = swaplong(mtget.mt_dsreg); |
| 470 |
mtget.mt_gstat = swaplong(mtget.mt_gstat); |
| 471 |
mtget.mt_erreg = swaplong(mtget.mt_erreg); |
| 472 |
mtget.mt_fileno = swaplong(mtget.mt_fileno); |
| 473 |
mtget.mt_blkno = swaplong(mtget.mt_blkno); |
| 474 |
(void)sprintf(resp, "A%jd\n", (intmax_t)rval); |
| 475 |
if (write(1, resp, strlen(resp)) != (ssize_t)strlen(resp)) |
| 476 |
goto ioerror; |
| 477 |
if (write(1, (char *)&mtget, sizeof (mtget)) != sizeof(mtget)) |
| 478 |
goto ioerror; |
| 479 |
} else { |
| 480 |
rval = sizeof (mtget); |
| 481 |
(void)sprintf(resp, "A%jd\n", (intmax_t)rval); |
| 482 |
if (write(1, resp, strlen(resp)) != (ssize_t)strlen(resp)) |
| 483 |
goto ioerror; |
| 484 |
if (write(1, (char *)&mtget, sizeof (mtget)) != sizeof(mtget)) |
| 485 |
goto ioerror; |
| 486 |
} |
| 487 |
goto top; |
| 488 |
} |
| 489 |
|
| 490 |
case 's': |
| 491 |
{ char s; |
| 492 |
struct mtget mtget; |
| 493 |
|
| 494 |
DEBUG ("rmtd: s\n"); |
| 495 |
|
| 496 |
if (read (0, &s, 1) != 1) |
| 497 |
goto top; |
| 498 |
DEBUG ("rmtd: s %d\n", s); |
| 499 |
|
| 500 |
if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0) { |
| 501 |
goto ioerror; |
| 502 |
} |
| 503 |
|
| 504 |
switch (s) { |
| 505 |
case MTS_TYPE: |
| 506 |
rval = mtget.mt_type; |
| 507 |
break; |
| 508 |
case MTS_DSREG: |
| 509 |
rval = mtget.mt_dsreg; |
| 510 |
break; |
| 511 |
case MTS_ERREG: |
| 512 |
rval = mtget.mt_erreg; |
| 513 |
break; |
| 514 |
case MTS_RESID: |
| 515 |
rval = mtget.mt_resid; |
| 516 |
break; |
| 517 |
case MTS_FILENO: |
| 518 |
rval = mtget.mt_fileno; |
| 519 |
break; |
| 520 |
case MTS_BLKNO: |
| 521 |
rval = mtget.mt_blkno; |
| 522 |
break; |
| 523 |
case MTS_FLAGS: |
| 524 |
rval = mtget.mt_gstat; |
| 525 |
break; |
| 526 |
case MTS_BF: |
| 527 |
rval = 0; |
| 528 |
break; |
| 529 |
default: |
| 530 |
errno = EINVAL; |
| 531 |
goto ioerror; |
| 532 |
} |
| 533 |
|
| 534 |
goto respond; |
| 535 |
} |
| 536 |
|
| 537 |
case 'V': |
| 538 |
getstring(op); |
| 539 |
DEBUG("rmtd: V %s\n", op); |
| 540 |
rval = 2; |
| 541 |
goto respond; |
| 542 |
|
| 543 |
default: |
| 544 |
DEBUG("rmtd: garbage command %c\n", c); |
| 545 |
exit(3); |
| 546 |
} |
| 547 |
respond: |
| 548 |
DEBUG("rmtd: A %jd\n", (intmax_t)rval); |
| 549 |
(void)sprintf(resp, "A%jd\n", (intmax_t)rval); |
| 550 |
if (write(1, resp, strlen(resp)) != (ssize_t)strlen(resp)) |
| 551 |
goto ioerror; |
| 552 |
goto top; |
| 553 |
ioerror: |
| 554 |
error(errno); |
| 555 |
goto top; |
| 556 |
} |
| 557 |
|
| 558 |
static void getstring(char *bp) |
| 559 |
{ |
| 560 |
int i; |
| 561 |
char *cp = bp; |
| 562 |
|
| 563 |
for (i = 0; i < SSIZE - 1; i++) { |
| 564 |
if (read(0, cp+i, 1) != 1) |
| 565 |
exit(0); |
| 566 |
if (cp[i] == '\n') |
| 567 |
break; |
| 568 |
} |
| 569 |
cp[i] = '\0'; |
| 570 |
} |
| 571 |
|
| 572 |
static char * |
| 573 |
checkbuf(char *record, int size) |
| 574 |
{ |
| 575 |
|
| 576 |
if (size <= maxrecsize) |
| 577 |
return (record); |
| 578 |
if (record != 0) |
| 579 |
free(record); |
| 580 |
record = malloc(size); |
| 581 |
if (record == 0) { |
| 582 |
DEBUG("rmtd: cannot allocate buffer space\n"); |
| 583 |
exit(4); |
| 584 |
} |
| 585 |
maxrecsize = size; |
| 586 |
while (size > 1024 && |
| 587 |
setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0) |
| 588 |
size -= 1024; |
| 589 |
return (record); |
| 590 |
} |
| 591 |
|
| 592 |
static void |
| 593 |
error(int num) |
| 594 |
{ |
| 595 |
|
| 596 |
DEBUG("rmtd: E %d (%s)\n", num, strerror(num)); |
| 597 |
(void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num)); |
| 598 |
if (write(1, resp, strlen(resp)) != (ssize_t)strlen(resp)) |
| 599 |
DEBUG("rmtd: write error\n"); |
| 600 |
} |
| 601 |
|
| 602 |
static unsigned long |
| 603 |
swaplong(unsigned long inv) |
| 604 |
{ |
| 605 |
union lconv { |
| 606 |
unsigned long ul; |
| 607 |
unsigned char uc[4]; |
| 608 |
}; |
| 609 |
union lconv *inp, outv; |
| 610 |
|
| 611 |
inp = (union lconv *)&inv; |
| 612 |
|
| 613 |
outv.uc[0] = inp->uc[3]; |
| 614 |
outv.uc[1] = inp->uc[2]; |
| 615 |
outv.uc[2] = inp->uc[1]; |
| 616 |
outv.uc[3] = inp->uc[0]; |
| 617 |
|
| 618 |
return (outv.ul); |
| 619 |
} |