* Copyright (c) 1983, 1990 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 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
"@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)rsh.c 5.24.1.1 (Berkeley) 8/20/91";
* $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();
int argoff
, asrsh
, ch
, dflag
, nflag
, one
, pid
, rem
, uid
;
char *args
, *host
, *user
, *copyargs();
argoff
= asrsh
= dflag
= nflag
= 0;
/* if called as something other than "rsh", use it as the host name */
if (p
= rindex(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
);
(void)fprintf(stderr
, "rsh: can't exec %s.\n", _PATH_RLOGIN
);
if (!(pw
= getpwuid(uid
= getuid()))) {
(void)fprintf(stderr
, "rsh: unknown user id.\n");
sp
= getservbyname((doencrypt
? "ekshell" : "kshell"), "tcp");
warning("can't get entry for %s/tcp service",
doencrypt
? "ekshell" : "kshell");
sp
= getservbyname("shell", "tcp");
(void)fprintf(stderr
, "rsh: shell/tcp: unknown service.\n");
dest_realm
= krb_realmofhost(host
);
rem
= krcmd(&host
, sp
->s_port
, user
, args
, &rfd2
,
sp
= getservbyname("shell", "tcp");
"rsh: unknown service shell/tcp.\n");
if (errno
== ECONNREFUSED
)
warning("remote host doesn't support Kerberos");
warning("can't provide Kerberos auth data");
"rsh: the -x flag requires Kerberos authentication.\n");
rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, args
, &rfd2
);
rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, args
, &rfd2
);
(void)fprintf(stderr
, "rsh: can't establish stderr.\n");
if (setsockopt(rem
, SOL_SOCKET
, SO_DEBUG
, &one
,
(void)fprintf(stderr
, "rsh: setsockopt: %s.\n",
if (setsockopt(rfd2
, SOL_SOCKET
, SO_DEBUG
, &one
,
(void)fprintf(stderr
, "rsh: setsockopt: %s.\n",
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
);
"rsh: fork: %s.\n", strerror(errno
));
(void)ioctl(rfd2
, FIONBIO
, &one
);
(void)ioctl(rem
, FIONBIO
, &one
);
talk(nflag
, omask
, pid
, rem
);
(void)kill(pid
, SIGKILL
);
talk(nflag
, omask
, pid
, rem
)
int readfrom
, ready
, rembits
;
if (!nflag
&& pid
== 0) {
if ((cc
= read(0, buf
, sizeof buf
)) <= 0)
rewrite
: rembits
= 1 << rem
;
if (select(16, 0, &rembits
, 0, 0) < 0) {
"rsh: select: %s.\n", strerror(errno
));
if ((rembits
& (1 << rem
)) == 0)
if (errno
== EWOULDBLOCK
)
readfrom
= (1 << rfd2
) | (1 << rem
);
if (select(16, &ready
, 0, 0, 0) < 0) {
"rsh: select: %s.\n", strerror(errno
));
if (ready
& (1 << rfd2
)) {
cc
= read(rfd2
, buf
, sizeof buf
);
if (errno
!= EWOULDBLOCK
)
readfrom
&= ~(1 << rfd2
);
if (ready
& (1 << rem
)) {
cc
= read(rem
, buf
, sizeof buf
);
if (errno
!= EWOULDBLOCK
)
(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
))) {
(void)fprintf(stderr
, "rsh: %s.\n", strerror(ENOMEM
));
for (p
= args
, ap
= argv
; *ap
; ++ap
) {
for (p
= strcpy(p
, *ap
); *p
; ++p
);
"usage: rsh [-nd%s]%s[-l login] host [command]\n",