| 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 |
#include "config.h" |
| 39 |
#include "dumprmt.h" |
| 40 |
|
| 41 |
#include <sys/param.h> |
| 42 |
#include <sys/mtio.h> |
| 43 |
#include <sys/time.h> |
| 44 |
#include <fcntl.h> |
| 45 |
#include <sys/types.h> |
| 46 |
#include "compat/include/bsdcompat.h" |
| 47 |
#include <signal.h> |
| 48 |
#include <stdint.h> |
| 49 |
#include <sys/select.h> |
| 50 |
#include <ctype.h> |
| 51 |
#include <errno.h> |
| 52 |
#include "compat/include/compaterr.h" |
| 53 |
#include <netdb.h> |
| 54 |
#include <pwd.h> |
| 55 |
#include <stdio.h> |
| 56 |
#include <stdlib.h> |
| 57 |
#include <string.h> |
| 58 |
#include <unistd.h> |
| 59 |
|
| 60 |
#include "common/rmtflags.h" |
| 61 |
|
| 62 |
#include "common/shared_globals.h" |
| 63 |
|
| 64 |
#define TS_CLOSED 0 |
| 65 |
#define TS_OPEN 1 |
| 66 |
|
| 67 |
static int rmtstate = TS_CLOSED; |
| 68 |
static int tormtape = -1; |
| 69 |
static int fromrmtape = -1; |
| 70 |
static const char *rmtpeer = 0; |
| 71 |
|
| 72 |
static int okname (const char *); |
| 73 |
static off_t rmtcall (const char *, const char *); |
| 74 |
static void rmtconnaborted (int); |
| 75 |
static int rmtgetb (void); |
| 76 |
static int rmtgetconn (void); |
| 77 |
static void rmtgets (char *, size_t); |
| 78 |
static off_t rmtreply (const char *); |
| 79 |
static int piped_child (const char **command); |
| 80 |
|
| 81 |
static int errfd = -1; |
| 82 |
#ifndef errno |
| 83 |
extern int errno; |
| 84 |
#endif |
| 85 |
|
| 86 |
int |
| 87 |
rmthost(const char *host) |
| 88 |
{ |
| 89 |
if (rmtpeer) |
| 90 |
free((void *)(uintptr_t)rmtpeer); |
| 91 |
if ((rmtpeer = strdup(host)) == NULL) { |
| 92 |
if (abortifconnerr) { |
| 93 |
errx(1, "Memory allocation failure in rmthost"); |
| 94 |
} else { |
| 95 |
msg("Memory allocation failure in rmthost"); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
} |
| 99 |
signal(SIGPIPE, rmtconnaborted); |
| 100 |
return rmtgetconn(); |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
rmtconnaborted(UNUSED(int signo)) |
| 105 |
{ |
| 106 |
msg("Lost connection to remote host.\n"); |
| 107 |
if (errfd != -1) { |
| 108 |
fd_set r; |
| 109 |
struct timeval t; |
| 110 |
|
| 111 |
FD_ZERO(&r); |
| 112 |
FD_SET(errfd, &r); |
| 113 |
t.tv_sec = 0; |
| 114 |
t.tv_usec = 0; |
| 115 |
if (select(errfd + 1, &r, NULL, NULL, &t)) { |
| 116 |
int i; |
| 117 |
char buf[2048]; |
| 118 |
|
| 119 |
if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) { |
| 120 |
buf[i] = '\0'; |
| 121 |
msg("on %s: %s%s", rmtpeer, buf, |
| 122 |
buf[i - 1] == '\n' ? "" : "\n"); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
if (abortifconnerr) |
| 127 |
exit(X_ABORT); |
| 128 |
} |
| 129 |
|
| 130 |
static void cleanup_ptr(char**p) { |
| 131 |
free(*p); |
| 132 |
} |
| 133 |
|
| 134 |
static int |
| 135 |
rmtgetconn(void) |
| 136 |
{ |
| 137 |
char *cp; |
| 138 |
const char *rmt; |
| 139 |
static struct servent *sp = NULL; |
| 140 |
static struct passwd *pwd = NULL; |
| 141 |
const char *tuser; |
| 142 |
const char *rsh; |
| 143 |
char *rmtpeer_ __attribute__((cleanup(cleanup_ptr))) = strdup(rmtpeer); |
| 144 |
char *rmtpeercopy = rmtpeer_; |
| 145 |
|
| 146 |
if (rmtpeercopy == NULL) { |
| 147 |
if (abortifconnerr) { |
| 148 |
errx(1, "Memory allocation failure in rmtgetconn"); |
| 149 |
} else { |
| 150 |
msg("Memory allocation failure in rmtgetconn"); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
rsh = getenv("RSH"); |
| 156 |
|
| 157 |
if (!rsh && sp == NULL) { |
| 158 |
sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp"); |
| 159 |
if (sp == NULL) { |
| 160 |
if (abortifconnerr) { |
| 161 |
errx(1, "%s/tcp: unknown service", dokerberos ? "kshell" : "shell"); |
| 162 |
} else { |
| 163 |
msg("%s/tcp: unknown service", dokerberos ? "kshell" : "shell"); |
| 164 |
return 0; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
if (pwd == NULL) { |
| 169 |
pwd = getpwuid(getuid()); |
| 170 |
if (pwd == NULL) { |
| 171 |
if (abortifconnerr) { |
| 172 |
errx(1, "who are you?"); |
| 173 |
} else { |
| 174 |
msg("who are you?"); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
if ((cp = strchr(rmtpeercopy, '@')) != NULL) { |
| 180 |
tuser = rmtpeercopy; |
| 181 |
*cp = '\0'; |
| 182 |
if (!okname(tuser)) { |
| 183 |
if (abortifconnerr) { |
| 184 |
exit(X_STARTUP); |
| 185 |
} else { |
| 186 |
return 0; |
| 187 |
} |
| 188 |
} |
| 189 |
rmtpeercopy = ++cp; |
| 190 |
} else |
| 191 |
tuser = pwd->pw_name; |
| 192 |
if ((rmt = getenv("RMT")) == NULL) |
| 193 |
rmt = _PATH_RMT; |
| 194 |
|
| 195 |
if (rsh) { |
| 196 |
const char *rshcmd[6]; |
| 197 |
rshcmd[0] = rsh; |
| 198 |
rshcmd[1] = rmtpeercopy; |
| 199 |
rshcmd[2] = "-l"; |
| 200 |
rshcmd[3] = tuser; |
| 201 |
rshcmd[4] = rmt; |
| 202 |
rshcmd[5] = NULL; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if (setuid(getuid())) |
| 208 |
err(1, "cannot setuid"); |
| 209 |
if (setgid(getgid())) |
| 210 |
err(1, "cannot setgid"); |
| 211 |
|
| 212 |
if ((rshpid = piped_child(rshcmd)) < 0) { |
| 213 |
msg("cannot open connection\n"); |
| 214 |
return 0; |
| 215 |
} |
| 216 |
} |
| 217 |
else { |
| 218 |
msg("This version of dump does not support rcmd. Set RSH to access a remote tape."); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
(void)fprintf(stderr, "Connection to %s established.\n", rmtpeer); |
| 222 |
return 1; |
| 223 |
} |
| 224 |
|
| 225 |
static int |
| 226 |
okname(const char *cp0) |
| 227 |
{ |
| 228 |
const char *cp; |
| 229 |
int c; |
| 230 |
|
| 231 |
for (cp = cp0; *cp; cp++) { |
| 232 |
c = *cp; |
| 233 |
if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) { |
| 234 |
warnx("invalid user name %s\n", cp0); |
| 235 |
return (0); |
| 236 |
} |
| 237 |
} |
| 238 |
return (1); |
| 239 |
} |
| 240 |
|
| 241 |
int |
| 242 |
rmtopen(const char *tape, const int mode) |
| 243 |
{ |
| 244 |
char buf[MAXPATHLEN]; |
| 245 |
char *rmtflags; |
| 246 |
|
| 247 |
rmtflags = rmtflags_tochar(mode); |
| 248 |
(void)snprintf(buf, sizeof (buf), "O%s\n%d %s\n", |
| 249 |
tape, |
| 250 |
mode & O_ACCMODE, |
| 251 |
rmtflags); |
| 252 |
free(rmtflags); |
| 253 |
rmtstate = TS_OPEN; |
| 254 |
return (rmtcall(tape, buf)); |
| 255 |
} |
| 256 |
|
| 257 |
void |
| 258 |
rmtclose(void) |
| 259 |
{ |
| 260 |
|
| 261 |
if (rmtstate != TS_OPEN) |
| 262 |
return; |
| 263 |
rmtcall("close", "C\n"); |
| 264 |
rmtstate = TS_CLOSED; |
| 265 |
} |
| 266 |
|
| 267 |
int |
| 268 |
rmtread(char *buf, size_t count) |
| 269 |
{ |
| 270 |
char line[30]; |
| 271 |
int n, i; |
| 272 |
ssize_t cc; |
| 273 |
|
| 274 |
(void)snprintf(line, sizeof (line), "R%zu\n", count); |
| 275 |
n = rmtcall("read", line); |
| 276 |
if (n < 0) { |
| 277 |
|
| 278 |
errno = n; |
| 279 |
return (-1); |
| 280 |
} |
| 281 |
if (n > (ssize_t)count) { |
| 282 |
msg("Protocol error: remote read size %d exceeds requested %zu\n", |
| 283 |
n, count); |
| 284 |
errno = EPROTO; |
| 285 |
rmtconnaborted(0); |
| 286 |
return (-1); |
| 287 |
} |
| 288 |
|
| 289 |
for (i = 0; i < n; i += cc) { |
| 290 |
cc = read(fromrmtape, buf+i, n - i); |
| 291 |
if (cc <= 0) |
| 292 |
rmtconnaborted(0); |
| 293 |
} |
| 294 |
return (n); |
| 295 |
} |
| 296 |
|
| 297 |
int |
| 298 |
rmtwrite(const char *buf, size_t count) |
| 299 |
{ |
| 300 |
char line[30]; |
| 301 |
|
| 302 |
(void)snprintf(line, sizeof (line), "W%zu\n", count); |
| 303 |
if (write(tormtape, line, strlen(line)) != (ssize_t)strlen(line)) |
| 304 |
return -1; |
| 305 |
if (write(tormtape, buf, count) != (ssize_t)count) |
| 306 |
return -1; |
| 307 |
return (rmtreply("write")); |
| 308 |
} |
| 309 |
|
| 310 |
off_t |
| 311 |
rmtseek(off_t offset, int pos) |
| 312 |
{ |
| 313 |
char line[80]; |
| 314 |
|
| 315 |
(void)snprintf(line, sizeof (line), "L%jd\n%d\n", (intmax_t)offset, pos); |
| 316 |
return (rmtcall("seek", line)); |
| 317 |
} |
| 318 |
|
| 319 |
static struct mtget mts; |
| 320 |
|
| 321 |
struct mtget * |
| 322 |
rmtstatus(void) |
| 323 |
{ |
| 324 |
int i; |
| 325 |
char *cp; |
| 326 |
|
| 327 |
if (rmtstate != TS_OPEN) |
| 328 |
return (NULL); |
| 329 |
i = rmtcall("status", "S"); |
| 330 |
if (i < 0) return NULL; |
| 331 |
if (i != (int)sizeof(mts)) { |
| 332 |
msg("mtget sizes different between host (%zu) " |
| 333 |
"and remote tape (%d)", sizeof(mts), i); |
| 334 |
for ( ; i > 0; --i) |
| 335 |
rmtgetb(); |
| 336 |
return NULL; |
| 337 |
} |
| 338 |
for (i = 0, cp = (char *)&mts; i < (int)sizeof(mts); i++) |
| 339 |
*cp++ = rmtgetb(); |
| 340 |
return (&mts); |
| 341 |
} |
| 342 |
|
| 343 |
int |
| 344 |
rmtioctl(int cmd, int count) |
| 345 |
{ |
| 346 |
char buf[256]; |
| 347 |
|
| 348 |
if (count < 0) |
| 349 |
return (-1); |
| 350 |
(void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count); |
| 351 |
return (rmtcall("ioctl", buf)); |
| 352 |
} |
| 353 |
|
| 354 |
static off_t |
| 355 |
rmtcall(const char *cmd, const char *buf) |
| 356 |
{ |
| 357 |
|
| 358 |
if (write(tormtape, buf, strlen(buf)) != (ssize_t)strlen(buf)) |
| 359 |
rmtconnaborted(0); |
| 360 |
return (rmtreply(cmd)); |
| 361 |
} |
| 362 |
|
| 363 |
static off_t |
| 364 |
rmtreply(UNUSED(const char *cmd)) |
| 365 |
{ |
| 366 |
char *cp; |
| 367 |
char code[30], emsg[BUFSIZ]; |
| 368 |
|
| 369 |
rmtgets(code, sizeof (code)); |
| 370 |
if (*code == 'E' || *code == 'F') { |
| 371 |
rmtgets(emsg, sizeof (emsg)); |
| 372 |
|
| 373 |
errno = atoi(code + 1); |
| 374 |
if (*code == 'F') |
| 375 |
rmtstate = TS_CLOSED; |
| 376 |
return (-1); |
| 377 |
} |
| 378 |
if (*code != 'A') { |
| 379 |
|
| 380 |
cp = code + strlen(code); |
| 381 |
if (cp > code && *--cp == '\n') |
| 382 |
*cp = '\0'; |
| 383 |
|
| 384 |
msg("Protocol to remote tape server botched (code \"%s\").\n", |
| 385 |
code); |
| 386 |
rmtconnaborted(0); |
| 387 |
} |
| 388 |
return (off_t)(atoll(code + 1)); |
| 389 |
} |
| 390 |
|
| 391 |
static int |
| 392 |
rmtgetb(void) |
| 393 |
{ |
| 394 |
char c; |
| 395 |
|
| 396 |
if (read(fromrmtape, &c, 1) != 1) |
| 397 |
rmtconnaborted(0); |
| 398 |
return (c); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
static void |
| 403 |
rmtgets(char *line, size_t len) |
| 404 |
{ |
| 405 |
char *cp = line; |
| 406 |
|
| 407 |
while (len > 1) { |
| 408 |
*cp = rmtgetb(); |
| 409 |
if (*cp == '\n') { |
| 410 |
cp[1] = '\0'; |
| 411 |
return; |
| 412 |
} |
| 413 |
cp++; |
| 414 |
len--; |
| 415 |
} |
| 416 |
*cp = '\0'; |
| 417 |
msg("Protocol to remote tape server botched.\n"); |
| 418 |
msg("(rmtgets got \"%s\").\n", line); |
| 419 |
rmtconnaborted(0); |
| 420 |
} |
| 421 |
|
| 422 |
int piped_child(const char **command) { |
| 423 |
int pid; |
| 424 |
int to_child_pipe[2]; |
| 425 |
int from_child_pipe[2]; |
| 426 |
|
| 427 |
if (pipe (to_child_pipe) < 0) { |
| 428 |
msg ("cannot create pipe: %s\n", strerror(errno)); |
| 429 |
return -1; |
| 430 |
} |
| 431 |
if (pipe (from_child_pipe) < 0) { |
| 432 |
msg ("cannot create pipe: %s\n", strerror(errno)); |
| 433 |
return -1; |
| 434 |
} |
| 435 |
pid = fork (); |
| 436 |
if (pid < 0) { |
| 437 |
msg ("cannot fork: %s\n", strerror(errno)); |
| 438 |
return -1; |
| 439 |
} |
| 440 |
if (pid == 0) { |
| 441 |
if (dup2 (to_child_pipe[0], STDIN_FILENO) < 0) { |
| 442 |
msg ("cannot dup2 pipe: %s\n", strerror(errno)); |
| 443 |
exit(1); |
| 444 |
} |
| 445 |
if (close (to_child_pipe[1]) < 0) { |
| 446 |
msg ("cannot close pipe: %s\n", strerror(errno)); |
| 447 |
exit(1); |
| 448 |
} |
| 449 |
if (close (from_child_pipe[0]) < 0) { |
| 450 |
msg ("cannot close pipe: %s\n", strerror(errno)); |
| 451 |
exit(1); |
| 452 |
} |
| 453 |
if (dup2 (from_child_pipe[1], STDOUT_FILENO) < 0) { |
| 454 |
msg ("cannot dup2 pipe: %s\n", strerror(errno)); |
| 455 |
exit(1); |
| 456 |
} |
| 457 |
setpgid(0, getpid()); |
| 458 |
execvp (command[0], (char *const *)(uintptr_t)command); |
| 459 |
msg("cannot exec %s: %s\n", command[0], strerror(errno)); |
| 460 |
exit(1); |
| 461 |
} |
| 462 |
if (close (to_child_pipe[0]) < 0) { |
| 463 |
msg ("cannot close pipe: %s\n", strerror(errno)); |
| 464 |
return -1; |
| 465 |
} |
| 466 |
if (close (from_child_pipe[1]) < 0) { |
| 467 |
msg ("cannot close pipe: %s\n", strerror(errno)); |
| 468 |
return -1; |
| 469 |
} |
| 470 |
tormtape = to_child_pipe[1]; |
| 471 |
fromrmtape = from_child_pipe[0]; |
| 472 |
return pid; |
| 473 |
} |