* Copyright (c) 1983, 1990 The Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)rsh.c 5.21 (Berkeley) 5/15/90";
* $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, encrypt
;
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:nwx"
#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
);
des_set_key(cred
.session
, schedule
);
/* 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((encrypt
? "ekshell" : "kshell"), "tcp");
warning("can't get entry for %s/tcp service",
encrypt
? "ekshell" : "kshell");
sp
= getservbyname("shell", "tcp");
(void)fprintf(stderr
, "rsh: shell/tcp: unknown service.\n");
dest_realm
= krb_realmofhost(host
);
rem
= krcmd_mutual(&host
, sp
->s_port
, user
, args
,
&rfd2
, dest_realm
, &cred
, schedule
);
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)
wc
= des_write(rem
, bp
, cc
);
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
= des_read(rfd2
, buf
, sizeof buf
);
cc
= read(rfd2
, buf
, sizeof buf
);
if (errno
!= EWOULDBLOCK
)
readfrom
&= ~(1 << rfd2
);
if (ready
& (1 << rem
)) {
cc
= des_read(rem
, buf
, sizeof buf
);
cc
= read(rem
, buf
, sizeof buf
);
if (errno
!= EWOULDBLOCK
)
(void)des_write(rfd2
, &signo
, 1);
(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 [-ndx]%s[-l login] host [command]\n",