* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
* 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
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)rcmd.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
int __ivaliduser
__P((FILE *, u_long
, const char *, const char *));
static int __icheckhost
__P((u_long
, char *));
rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
const char *locuser
, *remuser
, *cmd
;
struct sockaddr_in sin
, from
;
hp
= gethostbyname(*ahost
);
oldmask
= sigblock(sigmask(SIGURG
));
for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
"rcmd: socket: All ports in use\n");
(void)fprintf(stderr
, "rcmd: socket: %s\n",
sin
.sin_family
= hp
->h_addrtype
;
bcopy(hp
->h_addr_list
[0], &sin
.sin_addr
, hp
->h_length
);
if (connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
if (errno
== EADDRINUSE
) {
if (errno
== ECONNREFUSED
&& timo
<= 16) {
if (hp
->h_addr_list
[1] != NULL
) {
(void)fprintf(stderr
, "connect to address %s: ",
inet_ntoa(sin
.sin_addr
));
bcopy(hp
->h_addr_list
[0], &sin
.sin_addr
, hp
->h_length
);
(void)fprintf(stderr
, "Trying %s...\n",
inet_ntoa(sin
.sin_addr
));
(void)fprintf(stderr
, "%s: %s\n", hp
->h_name
, strerror(errno
));
int s2
= rresvport(&lport
), s3
;
(void)snprintf(num
, sizeof(num
), "%d", lport
);
if (write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
"rcmd: write (setting up stderr): %s\n",
if (select(32, &reads
, 0, 0, 0) < 1 || !FD_ISSET(s2
, &reads
)) {
"rcmd: select (setting up stderr): %s\n",
"select: protocol failure in circuit setup\n");
s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
"rcmd: accept: %s\n", strerror(errno
));
from
.sin_port
= ntohs((u_short
)from
.sin_port
);
if (from
.sin_family
!= AF_INET
||
from
.sin_port
>= IPPORT_RESERVED
||
from
.sin_port
< IPPORT_RESERVED
/ 2) {
"socket: protocol failure in circuit setup.\n");
(void)write(s
, locuser
, strlen(locuser
)+1);
(void)write(s
, remuser
, strlen(remuser
)+1);
(void)write(s
, cmd
, strlen(cmd
)+1);
if (read(s
, &c
, 1) != 1) {
"rcmd: %s: %s\n", *ahost
, strerror(errno
));
while (read(s
, &c
, 1) == 1) {
(void)write(STDERR_FILENO
, &c
, 1);
sin
.sin_family
= AF_INET
;
sin
.sin_addr
.s_addr
= INADDR_ANY
;
s
= socket(AF_INET
, SOCK_STREAM
, 0);
sin
.sin_port
= htons((u_short
)*alport
);
if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
if (errno
!= EADDRINUSE
) {
if (*alport
== IPPORT_RESERVED
/2) {
errno
= EAGAIN
; /* close */
int __check_rhosts_file
= 1;
ruserok(rhost
, superuser
, ruser
, luser
)
const char *rhost
, *ruser
, *luser
;
if ((hp
= gethostbyname(rhost
)) == NULL
)
for (ap
= hp
->h_addr_list
; *ap
; ++ap
) {
bcopy(*ap
, &addr
, sizeof(addr
));
if (iruserok(addr
, superuser
, ruser
, luser
) == 0)
* New .rhosts strategy: We are passed an ip address. We spin through
* hosts.equiv and .rhosts looking for a match. When the .rhosts only
* has ip addresses, we don't have to trust a nameserver. When it
* contains hostnames, we spin through the list of addresses the nameserver
* gives us and look for a match.
* Returns 0 if ok, -1 if not ok.
iruserok(raddr
, superuser
, ruser
, luser
)
const char *ruser
, *luser
;
hostf
= superuser
? NULL
: fopen(_PATH_HEQUIV
, "r");
if (__ivaliduser(hostf
, raddr
, luser
, ruser
) == 0) {
if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
if ((pwd
= getpwnam(luser
)) == NULL
)
(void)strcpy(pbuf
, pwd
->pw_dir
);
(void)strcat(pbuf
, "/.rhosts");
* Change effective uid while opening .rhosts. If root and
* reading an NFS mounted file system, can't read files that
* are protected read/write owner only.
(void)seteuid(pwd
->pw_uid
);
hostf
= fopen(pbuf
, "r");
* If not a regular file, or is owned by someone other than
* user or root or if writeable by anyone but the owner, quit.
if (lstat(pbuf
, &sbuf
) < 0)
cp
= ".rhosts lstat failed";
else if (!S_ISREG(sbuf
.st_mode
))
cp
= ".rhosts not regular file";
else if (fstat(fileno(hostf
), &sbuf
) < 0)
cp
= ".rhosts fstat failed";
else if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
)
cp
= "bad .rhosts owner";
else if (sbuf
.st_mode
& (S_IWGRP
|S_IWOTH
))
cp
= ".rhosts writeable by other than owner";
/* If there were any problems, quit. */
* Don't make static, used by lpd(8).
* Returns 0 if ok, -1 if not ok.
__ivaliduser(hostf
, raddr
, luser
, ruser
)
const char *luser
, *ruser
;
char ahost
[MAXHOSTNAMELEN
];
while (fgets(ahost
, sizeof(ahost
), hostf
)) {
while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
*p
= isupper(*p
) ? tolower(*p
) : *p
;
if (*p
== ' ' || *p
== '\t') {
while (*p
== ' ' || *p
== '\t')
while (*p
!= '\n' && *p
!= ' ' &&
*p
!= '\t' && *p
!= '\0')
if (__icheckhost(raddr
, ahost
) &&
strcmp(ruser
, *user
? user
: luser
) == 0) {
* Returns "true" if match, 0 if no match.
__icheckhost(raddr
, lhost
)
register struct hostent
*hp
;
/* Try for raw ip address first. */
if (isdigit(*lhost
) && (long)(laddr
= inet_addr(lhost
)) != -1)
/* Better be a hostname. */
if ((hp
= gethostbyname(lhost
)) == NULL
)
/* Spin through ip addresses. */
for (pp
= hp
->h_addr_list
; *pp
; ++pp
)
if (!bcmp(&raddr
, *pp
, sizeof(u_long
)))