2014-06-08 06:44:15 +04:00
|
|
|
/* $NetBSD: rsh.c,v 1.36 2014/06/08 02:44:15 enami Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1997-02-16 18:01:02 +03:00
|
|
|
* Copyright (c) 1983, 1990, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-06 23:31:28 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
2008-07-21 18:19:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1993, 1994\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-06 23:31:28 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)rsh.c 8.4 (Berkeley) 4/29/95";
|
|
|
|
#else
|
2014-06-08 06:44:15 +04:00
|
|
|
__RCSID("$NetBSD: rsh.c,v 1.36 2014/06/08 02:44:15 enami Exp $");
|
1997-07-06 23:31:28 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/file.h>
|
1997-05-26 18:29:36 +04:00
|
|
|
#include <poll.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2003-06-15 02:43:31 +04:00
|
|
|
#include <netinet/tcp.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <netdb.h>
|
|
|
|
|
1997-02-16 18:01:02 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2004-10-16 06:03:54 +04:00
|
|
|
#include <limits.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <pwd.h>
|
1995-06-27 04:31:00 +04:00
|
|
|
#include <signal.h>
|
2002-06-14 04:51:04 +04:00
|
|
|
#include <stdarg.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1997-02-16 18:01:02 +03:00
|
|
|
#include <stdlib.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <string.h>
|
1997-02-16 18:01:02 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "pathnames.h"
|
2004-10-16 06:03:54 +04:00
|
|
|
#include "getport.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rsh - remote shell
|
|
|
|
*/
|
1997-06-17 00:54:16 +04:00
|
|
|
int remerr;
|
|
|
|
|
|
|
|
static int sigs[] = { SIGINT, SIGTERM, SIGQUIT };
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2011-08-29 18:22:46 +04:00
|
|
|
static char *copyargs(char **);
|
2014-06-08 06:44:15 +04:00
|
|
|
#ifndef IN_RCMD
|
2011-08-29 18:22:46 +04:00
|
|
|
static void sendsig(int);
|
2014-06-08 06:44:15 +04:00
|
|
|
#endif
|
2011-08-29 18:22:46 +04:00
|
|
|
static int checkfd(struct pollfd *, int);
|
|
|
|
static void talk(int, sigset_t *, pid_t, int);
|
|
|
|
__dead static void usage(void);
|
1997-07-21 00:44:23 +04:00
|
|
|
#ifdef IN_RCMD
|
2002-06-14 04:51:04 +04:00
|
|
|
int orcmd(char **, int, const char *,
|
|
|
|
const char *, const char *, int *);
|
|
|
|
int orcmd_af(char **, int, const char *,
|
|
|
|
const char *, const char *, int *, int);
|
1997-07-21 00:44:23 +04:00
|
|
|
#endif
|
1997-02-16 18:01:02 +03:00
|
|
|
|
|
|
|
int
|
2002-06-14 04:51:04 +04:00
|
|
|
main(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
struct servent *sp;
|
1997-06-17 00:54:16 +04:00
|
|
|
sigset_t oset, nset;
|
2003-06-15 02:43:31 +04:00
|
|
|
struct protoent *proto;
|
1997-06-17 00:54:16 +04:00
|
|
|
|
1997-05-26 18:29:36 +04:00
|
|
|
#ifdef IN_RCMD
|
|
|
|
char *locuser = 0, *loop;
|
|
|
|
#endif /* IN_RCMD */
|
2009-01-18 03:37:53 +03:00
|
|
|
int argoff, asrsh, ch, dflag, nflag, one, rem;
|
|
|
|
size_t i;
|
2005-03-11 05:45:24 +03:00
|
|
|
int family = AF_UNSPEC;
|
1997-02-16 18:01:02 +03:00
|
|
|
pid_t pid;
|
|
|
|
uid_t uid;
|
1997-06-05 20:10:46 +04:00
|
|
|
char *args, *host, *p, *user, *name;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
argoff = asrsh = dflag = nflag = 0;
|
|
|
|
one = 1;
|
|
|
|
host = user = NULL;
|
2003-04-07 05:46:41 +04:00
|
|
|
sp = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-06-02 15:57:23 +04:00
|
|
|
#ifndef IN_RCMD
|
1997-05-26 18:29:36 +04:00
|
|
|
/*
|
1997-06-02 15:57:23 +04:00
|
|
|
* If called as something other than "rsh" use it as the host name,
|
|
|
|
* only for rsh.
|
1997-05-26 18:29:36 +04:00
|
|
|
*/
|
2001-02-20 02:03:42 +03:00
|
|
|
if (strcmp(getprogname(), "rsh") == 0)
|
1997-05-26 18:29:36 +04:00
|
|
|
asrsh = 1;
|
2001-02-20 02:03:42 +03:00
|
|
|
else {
|
|
|
|
host = strdup(getprogname());
|
|
|
|
if (host == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
1997-06-02 15:57:23 +04:00
|
|
|
#endif /* IN_RCMD */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* handle "rsh host flags" */
|
|
|
|
if (!host && argc > 2 && argv[1][0] != '-') {
|
|
|
|
host = argv[1];
|
|
|
|
argoff = 1;
|
|
|
|
}
|
|
|
|
|
1997-05-26 18:29:36 +04:00
|
|
|
#ifdef IN_RCMD
|
|
|
|
if ((loop = getenv("RCMD_LOOP")) && strcmp(loop, "YES") == 0)
|
|
|
|
warnx("rcmd appears to be looping!");
|
|
|
|
|
2010-10-02 13:24:16 +04:00
|
|
|
setenv("RCMD_LOOP", "YES", 1);
|
1997-05-26 18:29:36 +04:00
|
|
|
|
2005-03-11 05:45:24 +03:00
|
|
|
# define OPTIONS "468KLdel:np:u:w"
|
1997-05-26 18:29:36 +04:00
|
|
|
|
|
|
|
#else /* IN_RCMD */
|
|
|
|
|
2005-03-11 05:45:24 +03:00
|
|
|
# define OPTIONS "468KLdel:np:w"
|
1997-05-26 18:29:36 +04:00
|
|
|
|
|
|
|
#endif /* IN_RCMD */
|
|
|
|
|
|
|
|
if (!(pw = getpwuid(uid = getuid())))
|
|
|
|
errx(1, "unknown user id");
|
|
|
|
|
1997-06-05 20:10:46 +04:00
|
|
|
if ((name = strdup(pw->pw_name)) == NULL)
|
|
|
|
err(1, "malloc");
|
1997-06-17 00:54:16 +04:00
|
|
|
while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch(ch) {
|
2005-03-11 05:45:24 +03:00
|
|
|
case '4':
|
|
|
|
family = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
family = AF_INET6;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'K':
|
|
|
|
break;
|
|
|
|
case 'L': /* -8Lew are ignored to allow rlogin aliases */
|
|
|
|
case 'e':
|
|
|
|
case 'w':
|
|
|
|
case '8':
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
user = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
2003-04-07 05:46:41 +04:00
|
|
|
case 'p':
|
2004-10-16 06:03:54 +04:00
|
|
|
sp = getport(optarg, "tcp");
|
2003-04-07 05:46:41 +04:00
|
|
|
break;
|
1997-05-26 18:29:36 +04:00
|
|
|
#ifdef IN_RCMD
|
|
|
|
case 'u':
|
1997-06-05 20:10:46 +04:00
|
|
|
if (getuid() != 0 && optarg && name &&
|
|
|
|
strcmp(name, optarg) != 0)
|
1997-05-26 18:29:36 +04:00
|
|
|
errx(1,"only super user can use the -u option");
|
|
|
|
locuser = optarg;
|
|
|
|
break;
|
|
|
|
#endif /* IN_RCMD */
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
optind += argoff;
|
|
|
|
|
|
|
|
/* if haven't gotten a host yet, do so */
|
|
|
|
if (!host && !(host = argv[optind++]))
|
|
|
|
usage();
|
|
|
|
|
|
|
|
/* if no further arguments, must have been called as rlogin. */
|
|
|
|
if (!argv[optind]) {
|
1997-05-26 18:29:36 +04:00
|
|
|
#ifdef IN_RCMD
|
|
|
|
usage();
|
|
|
|
#else
|
1993-03-21 12:45:37 +03:00
|
|
|
if (asrsh)
|
2004-10-16 06:03:54 +04:00
|
|
|
*argv = __UNCONST("rlogin");
|
1993-03-21 12:45:37 +03:00
|
|
|
execv(_PATH_RLOGIN, argv);
|
1997-02-16 18:01:02 +03:00
|
|
|
err(1, "can't exec %s", _PATH_RLOGIN);
|
1997-05-26 18:29:36 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1997-02-16 18:01:02 +03:00
|
|
|
/* Accept user1@host format, though "-l user2" overrides user1 */
|
|
|
|
p = strchr(host, '@');
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
if (!user && p > host)
|
|
|
|
user = host;
|
|
|
|
host = p + 1;
|
|
|
|
if (*host == '\0')
|
|
|
|
usage();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (!user)
|
1997-06-05 20:10:46 +04:00
|
|
|
user = name;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
args = copyargs(argv);
|
|
|
|
|
|
|
|
if (sp == NULL)
|
|
|
|
sp = getservbyname("shell", "tcp");
|
1997-02-16 18:01:02 +03:00
|
|
|
if (sp == NULL)
|
|
|
|
errx(1, "shell/tcp: unknown service");
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-05-26 18:29:36 +04:00
|
|
|
|
|
|
|
#ifdef IN_RCMD
|
2000-01-31 17:19:34 +03:00
|
|
|
rem = orcmd_af(&host, sp->s_port, locuser ? locuser :
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
2000-01-31 17:19:34 +03:00
|
|
|
rem = rcmd_af(&host, sp->s_port,
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
2005-03-11 05:45:24 +03:00
|
|
|
name, user, args, &remerr, family);
|
1997-06-05 20:10:46 +04:00
|
|
|
(void)free(name);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (rem < 0)
|
|
|
|
exit(1);
|
|
|
|
|
1997-06-17 00:54:16 +04:00
|
|
|
if (remerr < 0)
|
1997-02-16 18:01:02 +03:00
|
|
|
errx(1, "can't establish stderr");
|
1993-03-21 12:45:37 +03:00
|
|
|
if (dflag) {
|
|
|
|
if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
|
|
|
|
sizeof(one)) < 0)
|
1997-05-26 18:29:36 +04:00
|
|
|
warn("setsockopt remote");
|
1997-06-17 00:54:16 +04:00
|
|
|
if (setsockopt(remerr, SOL_SOCKET, SO_DEBUG, &one,
|
1993-03-21 12:45:37 +03:00
|
|
|
sizeof(one)) < 0)
|
1997-05-26 18:29:36 +04:00
|
|
|
warn("setsockopt stderr");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-06-15 02:43:31 +04:00
|
|
|
proto = getprotobyname("tcp");
|
|
|
|
setsockopt(rem, proto->p_proto, TCP_NODELAY, &one, sizeof(one));
|
|
|
|
setsockopt(remerr, proto->p_proto, TCP_NODELAY, &one, sizeof(one));
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-01-14 02:02:28 +03:00
|
|
|
(void)setuid(uid);
|
1997-06-17 00:54:16 +04:00
|
|
|
|
2005-01-14 02:02:28 +03:00
|
|
|
(void)sigemptyset(&nset);
|
1997-06-17 00:54:16 +04:00
|
|
|
for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++)
|
2005-01-14 02:02:28 +03:00
|
|
|
(void)sigaddset(&nset, sigs[i]);
|
1997-06-17 00:54:16 +04:00
|
|
|
|
2005-01-14 02:02:28 +03:00
|
|
|
(void)sigprocmask(SIG_BLOCK, &nset, &oset);
|
1997-06-17 00:54:16 +04:00
|
|
|
|
2014-06-08 06:44:15 +04:00
|
|
|
#ifndef IN_RCMD
|
1997-06-17 00:54:16 +04:00
|
|
|
for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++) {
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
if (sa.sa_handler != SIG_IGN) {
|
2005-01-14 02:02:28 +03:00
|
|
|
sa.sa_handler = sendsig;
|
|
|
|
(void)sigaction(sigs[i], &sa, NULL);
|
1997-06-17 00:54:16 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 06:44:15 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (!nflag) {
|
|
|
|
pid = fork();
|
1997-02-16 18:01:02 +03:00
|
|
|
if (pid < 0)
|
|
|
|
err(1, "fork");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-06-17 00:54:16 +04:00
|
|
|
else
|
|
|
|
pid = -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2014-06-08 05:44:52 +04:00
|
|
|
(void)ioctl(remerr, FIONBIO, &one);
|
|
|
|
(void)ioctl(rem, FIONBIO, &one);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-06-17 00:54:16 +04:00
|
|
|
talk(nflag, &oset, pid, rem);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (!nflag)
|
|
|
|
(void)kill(pid, SIGKILL);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-08-29 18:22:46 +04:00
|
|
|
static int
|
2002-06-14 04:51:04 +04:00
|
|
|
checkfd(struct pollfd *fdp, int outfd)
|
1997-06-17 00:54:16 +04:00
|
|
|
{
|
|
|
|
int nr, nw;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
|
|
|
|
if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
|
|
|
|
return -1;
|
2014-06-08 06:02:41 +04:00
|
|
|
|
1997-06-17 00:54:16 +04:00
|
|
|
if ((fdp->revents & POLLIN) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
errno = 0;
|
2014-06-08 05:44:52 +04:00
|
|
|
nr = read(fdp->fd, buf, sizeof buf);
|
1997-06-17 00:54:16 +04:00
|
|
|
|
|
|
|
if (nr <= 0) {
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *bc = buf;
|
|
|
|
while (nr) {
|
|
|
|
if ((nw = write(outfd, bc, nr)) <= 0)
|
|
|
|
return -1;
|
|
|
|
nr -= nw;
|
|
|
|
bc += nw;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-29 18:22:46 +04:00
|
|
|
static void
|
2002-06-14 04:51:04 +04:00
|
|
|
talk(int nflag, sigset_t *oset, __pid_t pid, int rem)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-06-17 00:54:16 +04:00
|
|
|
int nr, nw, nfds;
|
2014-06-08 06:44:15 +04:00
|
|
|
struct pollfd fds[3], *fdp = &fds[0];
|
1997-02-16 18:01:02 +03:00
|
|
|
char *bp, buf[BUFSIZ];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (!nflag && pid == 0) {
|
1997-06-17 00:54:16 +04:00
|
|
|
(void)close(remerr);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-06-17 00:54:16 +04:00
|
|
|
fdp->events = POLLOUT|POLLNVAL|POLLERR|POLLHUP;
|
|
|
|
fdp->fd = rem;
|
|
|
|
nr = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
bp = buf;
|
|
|
|
|
1997-06-17 00:54:16 +04:00
|
|
|
for (;;) {
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (nr == 0) {
|
|
|
|
if ((nr = read(0, buf, sizeof buf)) == 0)
|
|
|
|
goto done;
|
|
|
|
if (nr == -1) {
|
|
|
|
if (errno == EIO)
|
|
|
|
goto done;
|
2006-03-18 00:52:41 +03:00
|
|
|
if (errno == EINTR) {
|
|
|
|
nr = 0;
|
1997-06-17 00:54:16 +04:00
|
|
|
continue;
|
2006-03-18 00:52:41 +03:00
|
|
|
}
|
1997-06-17 00:54:16 +04:00
|
|
|
err(1, "read");
|
|
|
|
}
|
|
|
|
bp = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
rewrite: if (poll(fdp, 1, INFTIM) == -1) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
err(1, "poll");
|
|
|
|
goto rewrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
|
1997-05-26 18:29:36 +04:00
|
|
|
err(1, "poll");
|
1997-06-17 00:54:16 +04:00
|
|
|
|
|
|
|
if ((fdp->revents & POLLOUT) == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
goto rewrite;
|
1997-06-17 00:54:16 +04:00
|
|
|
|
2014-06-08 05:44:52 +04:00
|
|
|
nw = write(rem, bp, nr);
|
1997-06-17 00:54:16 +04:00
|
|
|
|
|
|
|
if (nw < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
err(1, "write");
|
|
|
|
}
|
|
|
|
bp += nw;
|
|
|
|
nr -= nw;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
done:
|
|
|
|
(void)shutdown(rem, 1);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-06-08 06:44:15 +04:00
|
|
|
#ifdef IN_RCMD
|
1997-05-26 18:29:36 +04:00
|
|
|
fdp = &fds[0];
|
2014-06-08 06:44:15 +04:00
|
|
|
nfds = 3;
|
|
|
|
fds[0].events = POLLIN|POLLNVAL|POLLERR|POLLHUP;
|
|
|
|
fds[0].fd = 2;
|
|
|
|
#else
|
|
|
|
(void)sigprocmask(SIG_SETMASK, oset, NULL);
|
|
|
|
fdp = &fds[1];
|
1997-05-26 18:29:36 +04:00
|
|
|
nfds = 2;
|
2014-06-08 06:44:15 +04:00
|
|
|
fds[0].events = 0;
|
|
|
|
#endif
|
|
|
|
fds[1].events = fds[2].events = POLLIN|POLLNVAL|POLLERR|POLLHUP;
|
|
|
|
fds[1].fd = remerr;
|
|
|
|
fds[2].fd = rem;
|
1993-03-21 12:45:37 +03:00
|
|
|
do {
|
1997-06-17 00:54:16 +04:00
|
|
|
if (poll(fdp, nfds, INFTIM) == -1) {
|
1997-02-16 18:01:02 +03:00
|
|
|
if (errno != EINTR)
|
1997-05-26 18:29:36 +04:00
|
|
|
err(1, "poll");
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-08 06:44:15 +04:00
|
|
|
if ((fds[1].events != 0 && checkfd(&fds[1], 2) == -1)
|
|
|
|
#ifdef IN_RCMD
|
|
|
|
|| (fds[0].events != 0 && checkfd(&fds[0], remerr) == -1)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
nfds--;
|
|
|
|
fds[1].events = 0;
|
|
|
|
#ifdef IN_RCMD
|
1997-06-17 00:54:16 +04:00
|
|
|
nfds--;
|
|
|
|
fds[0].events = 0;
|
2014-06-08 06:44:15 +04:00
|
|
|
#endif
|
|
|
|
fdp = &fds[2];
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2014-06-08 06:44:15 +04:00
|
|
|
if (fds[2].events != 0 && checkfd(&fds[2], 1) == -1) {
|
1997-06-17 00:54:16 +04:00
|
|
|
nfds--;
|
2014-06-08 06:44:15 +04:00
|
|
|
fds[2].events = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-06-17 00:54:16 +04:00
|
|
|
}
|
|
|
|
while (nfds);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2014-06-08 06:44:15 +04:00
|
|
|
#ifndef IN_RCMD
|
2011-08-29 18:22:46 +04:00
|
|
|
static void
|
2002-06-14 04:51:04 +04:00
|
|
|
sendsig(int sig)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-02-16 18:01:02 +03:00
|
|
|
char signo;
|
|
|
|
|
|
|
|
signo = sig;
|
2014-06-08 06:02:41 +04:00
|
|
|
(void)write(remerr, &signo, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2014-06-08 06:44:15 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
2011-08-29 18:22:46 +04:00
|
|
|
static char *
|
2002-06-14 04:51:04 +04:00
|
|
|
copyargs(char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-02-16 18:01:02 +03:00
|
|
|
int cc;
|
2003-05-15 04:47:48 +04:00
|
|
|
char **ap, *args, *p, *ep;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
cc = 0;
|
|
|
|
for (ap = argv; *ap; ++ap)
|
|
|
|
cc += strlen(*ap) + 1;
|
1997-02-16 18:01:02 +03:00
|
|
|
if (!(args = malloc((u_int)cc)))
|
1997-06-17 00:54:16 +04:00
|
|
|
err(1, "malloc");
|
2003-05-15 04:47:48 +04:00
|
|
|
ep = args + cc;
|
1997-05-26 18:29:36 +04:00
|
|
|
for (p = args, *p = '\0', ap = argv; *ap; ++ap) {
|
2003-05-15 04:47:48 +04:00
|
|
|
(void)strlcpy(p, *ap, ep - p);
|
1997-05-26 18:29:36 +04:00
|
|
|
p += strlen(p);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (ap[1])
|
|
|
|
*p++ = ' ';
|
|
|
|
}
|
1997-05-26 18:29:36 +04:00
|
|
|
*p = '\0';
|
1997-02-16 18:01:02 +03:00
|
|
|
return (args);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2011-08-29 18:22:46 +04:00
|
|
|
static void
|
2002-06-14 04:51:04 +04:00
|
|
|
usage(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-02-16 18:01:02 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(stderr,
|
2006-03-24 02:49:07 +03:00
|
|
|
"usage: %s [-46dn] [-l login] [-p port]%s [login@]host command\n",
|
|
|
|
getprogname(),
|
1997-05-26 18:29:36 +04:00
|
|
|
#ifdef IN_RCMD
|
2006-03-24 02:49:07 +03:00
|
|
|
" [-u locuser]"
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
2006-03-24 02:49:07 +03:00
|
|
|
""
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
1997-05-26 18:29:36 +04:00
|
|
|
);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|