#include <sys/cdefs.h>
__RCSID("$NetBSD: rmtlib.c,v 1.29 2024/03/22 19:36:56 andvar Exp $");
#define RMTIOCTL 1
#include <sys/types.h>
#include <sys/stat.h>
#ifdef RMTIOCTL
#include <sys/ioctl.h>
#include <sys/mtio.h>
#endif
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#ifdef USE_REXEC
#include <netdb.h>
#endif
#define __RMTLIB_PRIVATE
#include <rmt.h>
#include "pathnames.h"
static int _rmt_close(int);
static int _rmt_ioctl(int, unsigned long, void *);
static off_t _rmt_lseek(int, off_t, int);
static int _rmt_open(const char *, int, int);
static ssize_t _rmt_read(int, void *, size_t);
static ssize_t _rmt_write(int, const void *, size_t);
static int command(int, const char *);
static int remdev(const char *);
static void rmtabort(int);
static int status(int);
#define BUFMAGIC 64
#define MAXUNIT 4
#define READ(fd) (Ctp[fd][0])
#define WRITE(fd) (Ptc[fd][1])
static int Ctp[MAXUNIT][2] = { {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1} };
static int Ptc[MAXUNIT][2] = { {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1} };
static void
rmtabort(int fildes)
{
close(READ(fildes));
close(WRITE(fildes));
READ(fildes) = -1;
WRITE(fildes) = -1;
}
static int
command(int fildes, const char *buf)
{
size_t blen;
sig_t pstat;
_DIAGASSERT(buf != NULL);
blen = strlen(buf);
pstat = signal(SIGPIPE, SIG_IGN);
if ((size_t)write(WRITE(fildes), buf, blen) == blen) {
signal(SIGPIPE, pstat);
return 0;
}
signal(SIGPIPE, pstat);
rmtabort(fildes);
errno = EIO;
return -1;
}
static int
status(int fildes)
{
int i;
char c, *cp;
char buffer[BUFMAGIC];
for (i = 0, cp = buffer; i < BUFMAGIC; i++, cp++) {
if (read(READ(fildes), cp, 1) != 1) {
rmtabort(fildes);
errno = EIO;
return -1;
}
if (*cp == '\n') {
*cp = 0;
break;
}
}
if (i == BUFMAGIC) {
rmtabort(fildes);
errno = EIO;
return -1;
}
for (cp = buffer; *cp; cp++)
if (*cp != ' ')
break;
if (*cp == 'E' || *cp == 'F') {
errno = atoi(cp + 1);
while (read(READ(fildes), &c, 1) == 1)
if (c == '\n')
break;
if (*cp == 'F')
rmtabort(fildes);
return -1;
}
if (*cp != 'A') {
rmtabort(fildes);
errno = EIO;
return -1;
}
return atoi(cp + 1);
}
#ifdef USE_REXEC
static int _rmt_rexec(const char *, const char *);
static int
_rmt_rexec(const char *host, const char *user)
{
struct servent *rexecserv;
_DIAGASSERT(host != NULL);
rexecserv = getservbyname("exec", "tcp");
if (rexecserv == NULL)
errx(1, "exec/tcp: service not available.");
if ((user != NULL) && *user == '\0')
user = NULL;
return rexec(&host, rexecserv->s_port, user, NULL,
"/etc/rmt", NULL);
}
#endif
#define MAXHOSTLEN 257
static int
_rmt_open(const char *path, int oflag, int mode)
{
int i;
char buffer[2 * BUFMAGIC];
char host[MAXHOSTLEN];
char device[BUFMAGIC];
char login[BUFMAGIC];
char *sys, *dev, *user;
const char *rshpath, *rsh;
_DIAGASSERT(path != NULL);
sys = host;
dev = device;
user = login;
for (i = 0; i < MAXUNIT; i++)
if (READ(i) == -1 && WRITE(i) == -1)
break;
if (i == MAXUNIT) {
errno = EMFILE;
return -1;
}
while (*path != '@'
#ifdef COMPAT
&& *path != '.'
#endif
&& *path != ':') {
*sys++ = *path++;
}
*sys = '\0';
path++;
if (*(path - 1) == '@') {
(void)strlcpy(user, host, sizeof(login));
sys = host;
while (*path != ':') {
*sys++ = *path++;
}
*sys = '\0';
path++;
}
#ifdef COMPAT
else if (*(path - 1) == '.') {
while (*path != ':') {
*user++ = *path++;
}
*user = '\0';
path++;
}
#endif
else
*user = '\0';
while (*path) {
*dev++ = *path++;
}
*dev = '\0';
#ifdef USE_REXEC
READ(i) = WRITE(i) = _rmt_rexec(host, login);
if (READ(i) < 0)
return -1;
#else
if (pipe(Ptc[i]) == -1 || pipe(Ctp[i]) == -1)
return -1;
switch (fork()) {
case -1:
return -1;
case 0:
close(0);
dup(Ptc[i][0]);
close(Ptc[i][0]); close(Ptc[i][1]);
close(1);
dup(Ctp[i][1]);
close(Ctp[i][0]); close(Ctp[i][1]);
(void) setuid(getuid());
(void) setgid(getgid());
if ((rshpath = getenv("RCMD_CMD")) == NULL)
rshpath = _PATH_RSH;
if ((rsh = strrchr(rshpath, '/')) == NULL)
rsh = rshpath;
else
rsh++;
if (*login) {
execl(rshpath, rsh, host, "-l", login, _PATH_RMT, NULL);
} else {
execl(rshpath, rsh, host, _PATH_RMT, NULL);
}
err(1, "Cannot exec %s", rshpath);
default:
break;
}
close(Ptc[i][0]); close(Ctp[i][1]);
#endif
(void)snprintf(buffer, sizeof(buffer), "O%s\n%d\n", device, oflag);
if (command(i, buffer) == -1 || status(i) == -1)
return -1;
return i;
}
static int
_rmt_close(int fildes)
{
int rc;
if (command(fildes, "C\n") != -1) {
rc = status(fildes);
rmtabort(fildes);
return rc;
}
return -1;
}
static ssize_t
_rmt_read(int fildes, void *buf, size_t nbyte)
{
size_t rc;
int rv;
ssize_t nread;
char *p;
char buffer[BUFMAGIC];
_DIAGASSERT(buf != NULL);
(void)snprintf(buffer, sizeof buffer, "R%zu\n", nbyte);
if (command(fildes, buffer) == -1 || (rv = status(fildes)) == -1)
return -1;
if (rv > (int)nbyte)
rv = (int)nbyte;
for (rc = rv, p = buf; rc > 0; rc -= nread, p += nread) {
if ((nread = read(READ(fildes), p, rc)) <= 0) {
rmtabort(fildes);
errno = EIO;
return -1;
}
}
return rv;
}
static ssize_t
_rmt_write(int fildes, const void *buf, size_t nbyte)
{
char buffer[BUFMAGIC];
sig_t pstat;
_DIAGASSERT(buf != NULL);
(void)snprintf(buffer, sizeof buffer, "W%zu\n", nbyte);
if (command(fildes, buffer) == -1)
return -1;
pstat = signal(SIGPIPE, SIG_IGN);
if ((size_t)write(WRITE(fildes), buf, nbyte) == nbyte) {
signal(SIGPIPE, pstat);
return status(fildes);
}
signal(SIGPIPE, pstat);
rmtabort(fildes);
errno = EIO;
return -1;
}
static off_t
_rmt_lseek(int fildes, off_t offset, int whence)
{
char buffer[BUFMAGIC];
(void)snprintf(buffer, sizeof buffer, "L%lld\n%d\n", (long long)offset,
whence);
if (command(fildes, buffer) == -1)
return -1;
return status(fildes);
}
#ifdef RMTIOCTL
static int
_rmt_ioctl(int fildes, unsigned long op, void *arg)
{
char c;
int rv;
size_t rc;
ssize_t cnt;
char buffer[BUFMAGIC], *p;
struct mtop *mtop = arg;
_DIAGASSERT(arg != NULL);
if (op == MTIOCTOP) {
(void)snprintf(buffer, sizeof buffer, "I%d\n%d\n",
mtop->mt_op, mtop->mt_count);
if (command(fildes, buffer) == -1)
return -1;
return status(fildes);
}
if (op != MTIOCGET) {
errno = EINVAL;
return -1;
}
if (command(fildes, "S") == -1 || (rv = status(fildes)) == -1)
return -1;
memset(arg, 0, sizeof(struct mtget));
for (rc = rv, p = arg; rc > 0; rc -= cnt, p += cnt) {
if ((cnt = read(READ(fildes), p, rc)) <= 0) {
rmtabort(fildes);
errno = EIO;
return -1;
}
}
if (((struct mtget *)(void *)p)->mt_type < 256)
return 0;
for (cnt = 0; cnt < rv; cnt += 2) {
c = p[cnt];
p[cnt] = p[cnt + 1];
p[cnt + 1] = c;
}
return 0;
}
#endif
#define REM_BIAS 128
static int
remdev(const char *path)
{
_DIAGASSERT(path != NULL);
if ((path = strchr(path, ':')) != NULL) {
if (strncmp(path + 1, "/dev/", 5) == 0) {
return 1;
}
}
return 0;
}
int
rmtopen(const char *path, int oflag, ...)
{
mode_t mode;
int fd;
va_list ap;
va_start(ap, oflag);
mode = va_arg(ap, mode_t);
va_end(ap);
_DIAGASSERT(path != NULL);
if (remdev(path)) {
fd = _rmt_open(path, oflag, (int)mode);
return (fd == -1) ? -1 : (fd + REM_BIAS);
} else {
return open(path, oflag, mode);
}
}
int
rmtaccess(const char *path, int amode)
{
_DIAGASSERT(path != NULL);
if (remdev(path)) {
return 0;
} else {
return access(path, amode);
}
}
int
isrmt(int fd)
{
int unbias = fd - REM_BIAS;
return (fd >= REM_BIAS) && unbias < MAXUNIT &&
(WRITE(unbias) != -1 || READ(unbias) != -1);
}
ssize_t
rmtread(int fildes, void *buf, size_t nbyte)
{
_DIAGASSERT(buf != NULL);
if (isrmt(fildes)) {
return _rmt_read(fildes - REM_BIAS, buf, nbyte);
} else {
return read(fildes, buf, nbyte);
}
}
ssize_t
rmtwrite(int fildes, const void *buf, size_t nbyte)
{
_DIAGASSERT(buf != NULL);
if (isrmt(fildes)) {
return _rmt_write(fildes - REM_BIAS, buf, nbyte);
} else {
return write(fildes, buf, nbyte);
}
}
off_t
rmtlseek(int fildes, off_t offset, int whence)
{
if (isrmt(fildes)) {
return _rmt_lseek(fildes - REM_BIAS, offset, whence);
} else {
return lseek(fildes, offset, whence);
}
}
int
rmtclose(int fildes)
{
if (isrmt(fildes)) {
return _rmt_close(fildes - REM_BIAS);
} else {
return close(fildes);
}
}
int
rmtioctl(int fildes, unsigned long request, ...)
{
void *arg;
va_list ap;
va_start(ap, request);
arg = va_arg(ap, void *);
va_end(ap);
if (isrmt(fildes)) {
#ifdef RMTIOCTL
return _rmt_ioctl(fildes - REM_BIAS, request, arg);
#else
errno = EOPNOTSUPP;
return -1;
#endif
} else {
return ioctl(fildes, request, arg);
}
}
int
rmtdup(int fildes)
{
if (isrmt(fildes)) {
errno = EOPNOTSUPP;
return -1;
} else {
return dup(fildes);
}
}
int
rmtfstat(int fildes, struct stat *buf)
{
_DIAGASSERT(buf != NULL);
if (isrmt(fildes)) {
errno = EOPNOTSUPP;
return -1;
} else {
return fstat(fildes, buf);
}
}
int
rmtstat(const char *path, struct stat *buf)
{
_DIAGASSERT(path != NULL);
_DIAGASSERT(buf != NULL);
if (remdev(path)) {
errno = EOPNOTSUPP;
return -1;
} else {
return stat(path, buf);
}
}
int
rmtcreat(const char *path, mode_t mode)
{
_DIAGASSERT(path != NULL);
if (remdev(path)) {
return rmtopen(path, O_WRONLY | O_CREAT, mode);
} else {
return open(path, O_CREAT | O_TRUNC | O_WRONLY, mode);
}
}
int
rmtfcntl(int fd, int cmd, ...)
{
void *arg;
va_list ap;
va_start(ap, cmd);
arg = va_arg(ap, void *);
va_end(ap);
if (isrmt(fd)) {
errno = EOPNOTSUPP;
return -1;
} else {
return fcntl(fd, cmd, arg);
}
}
int
rmtisatty(int fd)
{
if (isrmt(fd))
return 0;
else
return isatty(fd);
}
int
rmtlstat(const char *path, struct stat *buf)
{
_DIAGASSERT(path != NULL);
_DIAGASSERT(buf != NULL);
if (remdev(path)) {
errno = EOPNOTSUPP;
return -1;
} else {
return lstat(path, buf);
}
}