* Copyright (c) 1983, 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char copyright
[] =
"@(#) Copyright (c) 1983, 1990, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)rsh.c 8.3 (Berkeley) %G%";
* $Source: mit/rsh/RCS/rsh.c,v $
* $Header: mit/rsh/RCS/rsh.c,v 5.1 89/07/31 19:28:59 kfall Exp Locker: kfall $
#include <kerberosIV/des.h>
#include <kerberosIV/krb.h>
int use_kerberos
= 1, doencrypt
;
char dst_realm_buf
[REALM_SZ
], *dest_realm
;
extern char *krb_realmofhost();
char *copyargs
__P((char **));
void talk
__P((int, long, pid_t
, int));
int argoff
, asrsh
, ch
, dflag
, nflag
, one
, rem
;
char *args
, *host
, *p
, *user
;
argoff
= asrsh
= dflag
= nflag
= 0;
/* if called as something other than "rsh", use it as the host name */
if (p
= strrchr(argv
[0], '/'))
/* handle "rsh host flags" */
if (!host
&& argc
> 2 && argv
[1][0] != '-') {
#define OPTIONS "8KLdek:l:nw"
#define OPTIONS "8KLdel:nw"
while ((ch
= getopt(argc
- argoff
, argv
+ argoff
, OPTIONS
)) != EOF
)
case 'L': /* -8Lew are ignored to allow rlogin aliases */
dest_realm
= dst_realm_buf
;
strncpy(dest_realm
, optarg
, REALM_SZ
);
/* if haven't gotten a host yet, do so */
if (!host
&& !(host
= argv
[optind
++]))
/* if no further arguments, must have been called as rlogin. */
execv(_PATH_RLOGIN
, argv
);
err(1, "can't exec %s", _PATH_RLOGIN
);
if (!(pw
= getpwuid(uid
= getuid())))
errx(1, "unknown user id");
sp
= getservbyname((doencrypt
? "ekshell" : "kshell"), "tcp");
warning("can't get entry for %s/tcp service",
doencrypt
? "ekshell" : "kshell");
sp
= getservbyname("shell", "tcp");
errx(1, "shell/tcp: unknown service");
/* fully qualify hostname (needed for krb_realmofhost) */
hp
= gethostbyname(host
);
if (hp
!= NULL
&& !(host
= strdup(hp
->h_name
)))
dest_realm
= krb_realmofhost(host
);
rem
= krcmd(&host
, sp
->s_port
, user
, args
, &rfd2
,
sp
= getservbyname("shell", "tcp");
errx(1, "shell/tcp: unknown service");
if (errno
== ECONNREFUSED
)
warning("remote host doesn't support Kerberos");
warning("can't provide Kerberos auth data");
errx(1, "the -x flag requires Kerberos authentication");
rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, args
, &rfd2
);
rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, args
, &rfd2
);
errx(1, "can't establish stderr");
if (setsockopt(rem
, SOL_SOCKET
, SO_DEBUG
, &one
,
if (setsockopt(rfd2
, SOL_SOCKET
, SO_DEBUG
, &one
,
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
);
(void)ioctl(rfd2
, FIONBIO
, &one
);
(void)ioctl(rem
, FIONBIO
, &one
);
talk(nflag
, omask
, pid
, rem
);
(void)kill(pid
, SIGKILL
);
talk(nflag
, omask
, pid
, rem
)
fd_set readfrom
, ready
, rembits
;
if (!nflag
&& pid
== 0) {
if ((cc
= read(0, buf
, sizeof buf
)) <= 0)
if (select(16, 0, &rembits
, 0, 0) < 0) {
if (!FD_ISSET(rem
, &rembits
))
if (errno
== EWOULDBLOCK
)
if (select(16, &ready
, 0, 0, 0) < 0) {
if (FD_ISSET(rfd2
, &ready
)) {
cc
= read(rfd2
, buf
, sizeof buf
);
if (errno
!= EWOULDBLOCK
)
if (FD_ISSET(rem
, &ready
)) {
cc
= read(rem
, buf
, sizeof buf
);
if (errno
!= EWOULDBLOCK
)
} while (FD_ISSET(rfd2
, &readfrom
) || FD_ISSET(rem
, &readfrom
));
(void)write(rfd2
, &signo
, 1);
(void)fprintf(stderr
, "rsh: warning, using standard rsh: ");
fmt
= va_arg(ap
, char *);
vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, ".\n");
for (ap
= argv
; *ap
; ++ap
)
if (!(args
= malloc((u_int
)cc
)))
for (p
= args
, ap
= argv
; *ap
; ++ap
) {
for (p
= strcpy(p
, *ap
); *p
; ++p
);
"usage: rsh [-nd%s]%s[-l login] host [command]\n",