1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1983, 1990, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
2002-05-08 00:47:01 +00:00
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Portions of this software were developed for the FreeBSD Project by
|
|
|
|
* ThinkSec AS and NAI Labs, the Security Research Division of Network
|
|
|
|
* Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
|
|
|
|
* ("CBOSS"), as part of the DARPA CHATS research program.
|
1994-05-27 12:33:43 +00: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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-08-07 06:46:45 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1990, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2003-09-07 15:30:42 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2002-03-23 15:43:11 +00:00
|
|
|
static const char sccsid[] = "From: @(#)rsh.c 8.3 (Berkeley) 4/6/94";
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
2003-09-07 15:30:42 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-03-23 15:43:11 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1999-02-10 18:08:51 +00:00
|
|
|
#include <sys/param.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/file.h>
|
1996-09-06 05:24:05 +00:00
|
|
|
#include <sys/time.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1998-10-09 06:47:57 +00:00
|
|
|
#include <libutil.h>
|
2002-05-08 00:47:01 +00:00
|
|
|
#include <paths.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rsh - remote shell
|
|
|
|
*/
|
|
|
|
int rfd2;
|
|
|
|
|
2000-02-15 15:11:40 +00:00
|
|
|
int family = PF_UNSPEC;
|
2002-03-23 15:43:11 +00:00
|
|
|
char rlogin[] = "rlogin";
|
2000-02-15 15:11:40 +00:00
|
|
|
|
2002-08-13 16:25:38 +00:00
|
|
|
void connect_timeout(int);
|
2002-03-30 08:51:21 +00:00
|
|
|
char *copyargs(char * const *);
|
2002-03-22 01:33:25 +00:00
|
|
|
void sendsig(int);
|
|
|
|
void talk(int, long, pid_t, int, int);
|
|
|
|
void usage(void);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-03-23 15:43:11 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-03-30 08:51:21 +00:00
|
|
|
struct passwd const *pw;
|
|
|
|
struct servent const *sp;
|
1994-05-27 12:33:43 +00:00
|
|
|
long omask;
|
|
|
|
int argoff, asrsh, ch, dflag, nflag, one, rem;
|
1998-02-20 04:50:50 +00:00
|
|
|
pid_t pid = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
uid_t uid;
|
|
|
|
char *args, *host, *p, *user;
|
1996-09-06 05:24:05 +00:00
|
|
|
int timeout = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
argoff = asrsh = dflag = nflag = 0;
|
|
|
|
one = 1;
|
|
|
|
host = user = NULL;
|
|
|
|
|
|
|
|
/* if called as something other than "rsh", use it as the host name */
|
1997-09-28 08:57:24 +00:00
|
|
|
if ((p = strrchr(argv[0], '/')))
|
1994-05-27 12:33:43 +00:00
|
|
|
++p;
|
|
|
|
else
|
|
|
|
p = argv[0];
|
|
|
|
if (strcmp(p, "rsh"))
|
|
|
|
host = p;
|
|
|
|
else
|
|
|
|
asrsh = 1;
|
|
|
|
|
|
|
|
/* handle "rsh host flags" */
|
|
|
|
if (!host && argc > 2 && argv[1][0] != '-') {
|
|
|
|
host = argv[1];
|
|
|
|
argoff = 1;
|
|
|
|
}
|
|
|
|
|
2005-01-21 11:22:34 +00:00
|
|
|
#define OPTIONS "468Lde:l:nt:w"
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch(ch) {
|
2000-02-15 15:11:40 +00:00
|
|
|
case '4':
|
|
|
|
family = PF_INET;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '6':
|
|
|
|
family = PF_INET6;
|
|
|
|
break;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
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;
|
1996-09-06 05:24:05 +00:00
|
|
|
case 't':
|
|
|
|
timeout = atoi(optarg);
|
|
|
|
break;
|
1994-05-27 12:33:43 +00: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]) {
|
|
|
|
if (asrsh)
|
2002-03-23 15:43:11 +00:00
|
|
|
*argv = rlogin;
|
1994-05-27 12:33:43 +00:00
|
|
|
execv(_PATH_RLOGIN, argv);
|
|
|
|
err(1, "can't exec %s", _PATH_RLOGIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (!(pw = getpwuid(uid = getuid())))
|
|
|
|
errx(1, "unknown user id");
|
|
|
|
if (!user)
|
|
|
|
user = pw->pw_name;
|
|
|
|
|
|
|
|
args = copyargs(argv);
|
|
|
|
|
|
|
|
sp = NULL;
|
|
|
|
if (sp == NULL)
|
|
|
|
sp = getservbyname("shell", "tcp");
|
|
|
|
if (sp == NULL)
|
|
|
|
errx(1, "shell/tcp: unknown service");
|
|
|
|
|
2002-08-13 16:25:38 +00:00
|
|
|
if (timeout) {
|
|
|
|
signal(SIGALRM, connect_timeout);
|
|
|
|
alarm(timeout);
|
|
|
|
}
|
2000-01-25 14:52:10 +00:00
|
|
|
rem = rcmd_af(&host, sp->s_port, pw->pw_name, user, args, &rfd2,
|
2000-02-15 15:11:40 +00:00
|
|
|
family);
|
2002-08-13 16:25:38 +00:00
|
|
|
if (timeout) {
|
|
|
|
signal(SIGALRM, SIG_DFL);
|
|
|
|
alarm(0);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (rem < 0)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (rfd2 < 0)
|
|
|
|
errx(1, "can't establish stderr");
|
|
|
|
if (dflag) {
|
|
|
|
if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
|
|
|
|
sizeof(one)) < 0)
|
|
|
|
warn("setsockopt");
|
|
|
|
if (setsockopt(rfd2, SOL_SOCKET, SO_DEBUG, &one,
|
|
|
|
sizeof(one)) < 0)
|
|
|
|
warn("setsockopt");
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)setuid(uid);
|
|
|
|
omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGTERM));
|
|
|
|
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
|
|
|
|
(void)signal(SIGINT, sendsig);
|
|
|
|
if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
|
|
|
|
(void)signal(SIGQUIT, sendsig);
|
|
|
|
if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
|
|
|
|
(void)signal(SIGTERM, sendsig);
|
|
|
|
|
|
|
|
if (!nflag) {
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
err(1, "fork");
|
|
|
|
}
|
2002-05-08 00:47:01 +00:00
|
|
|
else
|
2005-05-11 02:49:03 +00:00
|
|
|
(void)shutdown(rem, SHUT_WR);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-10-16 16:10:46 +00:00
|
|
|
(void)ioctl(rfd2, FIONBIO, &one);
|
|
|
|
(void)ioctl(rem, FIONBIO, &one);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1996-09-06 05:24:05 +00:00
|
|
|
talk(nflag, omask, pid, rem, timeout);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (!nflag)
|
|
|
|
(void)kill(pid, SIGKILL);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-23 15:43:11 +00:00
|
|
|
talk(int nflag, long omask, pid_t pid, int rem, int timeout)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int cc, wc;
|
|
|
|
fd_set readfrom, ready, rembits;
|
2002-03-30 08:51:21 +00:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
const char *bp;
|
1996-09-06 05:24:05 +00:00
|
|
|
struct timeval tvtimeout;
|
1999-02-10 18:08:51 +00:00
|
|
|
int nfds, srval;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (!nflag && pid == 0) {
|
|
|
|
(void)close(rfd2);
|
|
|
|
|
|
|
|
reread: errno = 0;
|
|
|
|
if ((cc = read(0, buf, sizeof buf)) <= 0)
|
|
|
|
goto done;
|
|
|
|
bp = buf;
|
|
|
|
|
1995-05-30 06:41:30 +00:00
|
|
|
rewrite:
|
2002-09-09 16:14:03 +00:00
|
|
|
if (rem >= FD_SETSIZE)
|
|
|
|
errx(1, "descriptor too big");
|
1994-05-27 12:33:43 +00:00
|
|
|
FD_ZERO(&rembits);
|
|
|
|
FD_SET(rem, &rembits);
|
1999-02-10 18:08:51 +00:00
|
|
|
nfds = rem + 1;
|
|
|
|
if (select(nfds, 0, &rembits, 0, 0) < 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (errno != EINTR)
|
|
|
|
err(1, "select");
|
|
|
|
goto rewrite;
|
|
|
|
}
|
|
|
|
if (!FD_ISSET(rem, &rembits))
|
|
|
|
goto rewrite;
|
2002-10-16 16:10:46 +00:00
|
|
|
wc = write(rem, bp, cc);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (wc < 0) {
|
|
|
|
if (errno == EWOULDBLOCK)
|
|
|
|
goto rewrite;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
bp += wc;
|
|
|
|
cc -= wc;
|
|
|
|
if (cc == 0)
|
|
|
|
goto reread;
|
|
|
|
goto rewrite;
|
|
|
|
done:
|
2005-05-11 02:49:03 +00:00
|
|
|
(void)shutdown(rem, SHUT_WR);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1996-09-06 05:24:05 +00:00
|
|
|
tvtimeout.tv_sec = timeout;
|
|
|
|
tvtimeout.tv_usec = 0;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)sigsetmask(omask);
|
2002-09-09 16:14:03 +00:00
|
|
|
if (rfd2 >= FD_SETSIZE || rem >= FD_SETSIZE)
|
|
|
|
errx(1, "descriptor too big");
|
1994-05-27 12:33:43 +00:00
|
|
|
FD_ZERO(&readfrom);
|
|
|
|
FD_SET(rfd2, &readfrom);
|
|
|
|
FD_SET(rem, &readfrom);
|
1999-02-10 18:08:51 +00:00
|
|
|
nfds = MAX(rfd2+1, rem+1);
|
1994-05-27 12:33:43 +00:00
|
|
|
do {
|
|
|
|
ready = readfrom;
|
1996-09-06 05:24:05 +00:00
|
|
|
if (timeout) {
|
1999-02-10 18:08:51 +00:00
|
|
|
srval = select(nfds, &ready, 0, 0, &tvtimeout);
|
1996-09-06 05:24:05 +00:00
|
|
|
} else {
|
1999-02-10 18:08:51 +00:00
|
|
|
srval = select(nfds, &ready, 0, 0, 0);
|
1996-09-06 05:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (srval < 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (errno != EINTR)
|
|
|
|
err(1, "select");
|
|
|
|
continue;
|
|
|
|
}
|
1996-09-06 05:24:05 +00:00
|
|
|
if (srval == 0)
|
2003-09-07 15:30:42 +00:00
|
|
|
errx(1, "timeout reached (%d seconds)", timeout);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (FD_ISSET(rfd2, &ready)) {
|
|
|
|
errno = 0;
|
2002-10-16 16:10:46 +00:00
|
|
|
cc = read(rfd2, buf, sizeof buf);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (cc <= 0) {
|
|
|
|
if (errno != EWOULDBLOCK)
|
|
|
|
FD_CLR(rfd2, &readfrom);
|
|
|
|
} else
|
2001-07-26 11:02:39 +00:00
|
|
|
(void)write(STDERR_FILENO, buf, cc);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if (FD_ISSET(rem, &ready)) {
|
|
|
|
errno = 0;
|
2002-10-16 16:10:46 +00:00
|
|
|
cc = read(rem, buf, sizeof buf);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (cc <= 0) {
|
|
|
|
if (errno != EWOULDBLOCK)
|
|
|
|
FD_CLR(rem, &readfrom);
|
|
|
|
} else
|
2001-07-26 11:02:39 +00:00
|
|
|
(void)write(STDOUT_FILENO, buf, cc);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
} while (FD_ISSET(rfd2, &readfrom) || FD_ISSET(rem, &readfrom));
|
|
|
|
}
|
|
|
|
|
2002-08-13 16:25:38 +00:00
|
|
|
void
|
2010-12-20 08:47:43 +00:00
|
|
|
connect_timeout(int sig __unused)
|
2002-08-13 16:25:38 +00:00
|
|
|
{
|
|
|
|
char message[] = "timeout reached before connection completed.\n";
|
|
|
|
|
|
|
|
write(STDERR_FILENO, message, sizeof(message) - 1);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
void
|
2002-03-23 15:43:11 +00:00
|
|
|
sendsig(int sig)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
char signo;
|
|
|
|
|
|
|
|
signo = sig;
|
2002-10-16 16:10:46 +00:00
|
|
|
(void)write(rfd2, &signo, 1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2002-03-30 08:51:21 +00:00
|
|
|
copyargs(char * const *argv)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int cc;
|
2002-03-30 08:51:21 +00:00
|
|
|
char *args, *p;
|
|
|
|
char * const *ap;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
cc = 0;
|
|
|
|
for (ap = argv; *ap; ++ap)
|
|
|
|
cc += strlen(*ap) + 1;
|
|
|
|
if (!(args = malloc((u_int)cc)))
|
|
|
|
err(1, NULL);
|
|
|
|
for (p = args, ap = argv; *ap; ++ap) {
|
|
|
|
(void)strcpy(p, *ap);
|
|
|
|
for (p = strcpy(p, *ap); *p; ++p);
|
|
|
|
if (ap[1])
|
|
|
|
*p++ = ' ';
|
|
|
|
}
|
|
|
|
return (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-23 15:43:11 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
(void)fprintf(stderr,
|
2005-05-21 09:55:10 +00:00
|
|
|
"usage: rsh [-46dn] [-l username] [-t timeout] host [command]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|