2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/param.h>
40 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
58 int __ivaliduser
__P((FILE *, u_int32_t
, const char *, const char *));
59 static int __icheckhost
__P((u_int32_t
, char *)) internal_function
;
62 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
65 const char *locuser
, *remuser
, *cmd
;
68 struct hostent hostbuf
, *hp
;
71 struct sockaddr_in sin
, from
;
82 tmphstbuf
= __alloca (hstbuflen
);
83 while (__gethostbyname_r (*ahost
, &hostbuf
, tmphstbuf
, hstbuflen
,
85 if (herr
!= NETDB_INTERNAL
|| errno
!= ERANGE
)
93 /* Enlarge the buffer. */
95 tmphstbuf
= __alloca (hstbuflen
);
98 pfd
[0].events
= POLLIN
;
99 pfd
[1].events
= POLLIN
;
102 oldmask
= __sigblock(sigmask(SIGURG
));
103 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
104 s
= rresvport(&lport
);
107 (void)fprintf(stderr
,
108 _("rcmd: socket: All ports in use\n"));
110 (void)fprintf(stderr
, "rcmd: socket: %m\n");
111 __sigsetmask(oldmask
);
114 __fcntl(s
, F_SETOWN
, pid
);
115 sin
.sin_family
= hp
->h_addrtype
;
116 bcopy(hp
->h_addr_list
[0], &sin
.sin_addr
,
117 MIN (sizeof (sin
.sin_addr
), hp
->h_length
));
118 sin
.sin_port
= rport
;
119 if (__connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
122 if (errno
== EADDRINUSE
) {
126 if (errno
== ECONNREFUSED
&& timo
<= 16) {
131 if (hp
->h_addr_list
[1] != NULL
) {
134 (void)fprintf(stderr
, _("connect to address %s: "),
135 inet_ntoa(sin
.sin_addr
));
136 __set_errno (oerrno
);
139 bcopy(hp
->h_addr_list
[0], &sin
.sin_addr
,
140 MIN (sizeof (sin
.sin_addr
), hp
->h_length
));
141 (void)fprintf(stderr
, _("Trying %s...\n"),
142 inet_ntoa(sin
.sin_addr
));
145 (void)fprintf(stderr
, "%s: %m\n", hp
->h_name
);
146 __sigsetmask(oldmask
);
155 int s2
= rresvport(&lport
), s3
;
156 size_t len
= sizeof(from
);
161 (void)__snprintf(num
, sizeof(num
), "%d", lport
);
162 if (__write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
163 (void)fprintf(stderr
,
164 _("rcmd: write (setting up stderr): %m\n"));
171 if (__poll (pfd
, 2, -1) < 1 || (pfd
[1].revents
& POLLIN
) == 0){
173 (void)fprintf(stderr
,
174 _("rcmd: poll (setting up stderr): %m\n"));
176 (void)fprintf(stderr
,
177 _("poll: protocol failure in circuit setup\n"));
181 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
184 (void)fprintf(stderr
,
185 "rcmd: accept: %m\n");
190 from
.sin_port
= ntohs((u_short
)from
.sin_port
);
191 if (from
.sin_family
!= AF_INET
||
192 from
.sin_port
>= IPPORT_RESERVED
||
193 from
.sin_port
< IPPORT_RESERVED
/ 2) {
194 (void)fprintf(stderr
,
195 _("socket: protocol failure in circuit setup\n"));
199 (void)__write(s
, locuser
, strlen(locuser
)+1);
200 (void)__write(s
, remuser
, strlen(remuser
)+1);
201 (void)__write(s
, cmd
, strlen(cmd
)+1);
202 if (__read(s
, &c
, 1) != 1) {
203 (void)fprintf(stderr
,
204 "rcmd: %s: %m\n", *ahost
);
208 while (__read(s
, &c
, 1) == 1) {
209 (void)__write(STDERR_FILENO
, &c
, 1);
215 __sigsetmask(oldmask
);
219 (void)__close(*fd2p
);
222 __sigsetmask(oldmask
);
230 struct sockaddr_in sin
;
233 sin
.sin_family
= AF_INET
;
234 sin
.sin_addr
.s_addr
= INADDR_ANY
;
235 s
= __socket(AF_INET
, SOCK_STREAM
, 0);
239 sin
.sin_port
= htons((u_short
)*alport
);
240 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
242 if (errno
!= EADDRINUSE
) {
247 if (*alport
== IPPORT_RESERVED
/2) {
249 __set_errno (EAGAIN
); /* close */
255 int __check_rhosts_file
= 1;
259 ruserok(rhost
, superuser
, ruser
, luser
)
260 const char *rhost
, *ruser
, *luser
;
263 struct hostent hostbuf
, *hp
;
271 buffer
= __alloca (buflen
);
273 while (__gethostbyname_r (rhost
, &hostbuf
, buffer
, buflen
, &hp
, &herr
)
275 if (herr
!= NETDB_INTERNAL
|| errno
!= ERANGE
)
279 /* Enlarge the buffer. */
281 buffer
= __alloca (buflen
);
284 for (ap
= hp
->h_addr_list
; *ap
; ++ap
) {
285 bcopy(*ap
, &addr
, sizeof(addr
));
286 if (iruserok(addr
, superuser
, ruser
, luser
) == 0)
292 /* Extremely paranoid file open function. */
294 iruserfopen (char *file
, uid_t okuser
)
300 /* If not a regular file, if owned by someone other than user or
301 root, if writeable by anyone but the owner, or if hardlinked
304 if (__lxstat (_STAT_VER
, file
, &st
))
305 cp
= _("lstat failed");
306 else if (!S_ISREG (st
.st_mode
))
307 cp
= _("not regular file");
310 res
= fopen (file
, "r");
312 cp
= _("cannot open");
313 else if (__fxstat (_STAT_VER
, fileno (res
), &st
) < 0)
314 cp
= _("fstat failed");
315 else if (st
.st_uid
&& st
.st_uid
!= okuser
)
317 else if (st
.st_mode
& (S_IWGRP
|S_IWOTH
))
318 cp
= _("writeable by other than owner");
319 else if (st
.st_nlink
> 1)
320 cp
= _("hard linked somewhere");
323 /* If there were any problems, quit. */
336 * New .rhosts strategy: We are passed an ip address. We spin through
337 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
338 * has ip addresses, we don't have to trust a nameserver. When it
339 * contains hostnames, we spin through the list of addresses the nameserver
340 * gives us and look for a match.
342 * Returns 0 if ok, -1 if not ok.
345 iruserok (raddr
, superuser
, ruser
, luser
)
348 const char *ruser
, *luser
;
354 hostf
= iruserfopen (_PATH_HEQUIV
, 0);
358 isbad
= __ivaliduser (hostf
, raddr
, luser
, ruser
);
365 if (__check_rhosts_file
|| superuser
)
368 struct passwd pwdbuf
, *pwd
;
370 size_t buflen
= __sysconf (_SC_GETPW_R_SIZE_MAX
);
371 char *buffer
= __alloca (buflen
);
374 if (__getpwnam_r (luser
, &pwdbuf
, buffer
, buflen
, &pwd
))
377 dirlen
= strlen (pwd
->pw_dir
);
378 pbuf
= alloca (dirlen
+ sizeof "/.rhosts");
379 __mempcpy (__mempcpy (pbuf
, pwd
->pw_dir
, dirlen
),
380 "/.rhosts", sizeof "/.rhosts");
382 /* Change effective uid while reading .rhosts. If root and
383 reading an NFS mounted file system, can't read files that
384 are protected read/write owner only. */
386 seteuid (pwd
->pw_uid
);
387 hostf
= iruserfopen (pbuf
, pwd
->pw_uid
);
391 isbad
= __ivaliduser (hostf
, raddr
, luser
, ruser
);
403 * Don't make static, used by lpd(8).
405 * Returns 0 if ok, -1 if not ok.
408 __ivaliduser(hostf
, raddr
, luser
, ruser
)
411 const char *luser
, *ruser
;
413 register char *user
, *p
;
419 while ((nread
= __getline (&buf
, &bufsize
, hostf
)) > 0) {
420 buf
[bufsize
- 1] = '\0'; /* Make sure it's terminated. */
422 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
423 *p
= isupper(*p
) ? tolower(*p
) : *p
;
426 if (*p
== ' ' || *p
== '\t') {
428 while (*p
== ' ' || *p
== '\t')
431 while (*p
!= '\n' && *p
!= ' ' &&
432 *p
!= '\t' && *p
!= '\0')
437 if (__icheckhost(raddr
, buf
) &&
438 strcmp(ruser
, *user
? user
: luser
) == 0) {
449 * Returns "true" if match, 0 if no match.
453 __icheckhost(raddr
, lhost
)
455 register char *lhost
;
457 struct hostent hostbuf
, *hp
;
460 register u_int32_t laddr
;
464 /* Try for raw ip address first. */
465 if (isdigit(*lhost
) && (int32_t)(laddr
= inet_addr(lhost
)) != -1)
466 return raddr
== laddr
;
468 /* Better be a hostname. */
470 buffer
= __alloca (buflen
);
471 while (__gethostbyname_r (lhost
, &hostbuf
, buffer
, buflen
, &hp
, &herr
)
473 if (herr
!= NETDB_INTERNAL
|| errno
!= ERANGE
)
477 /* Enlarge the buffer. */
479 buffer
= __alloca (buflen
);
482 /* Spin through ip addresses. */
483 for (pp
= hp
->h_addr_list
; *pp
; ++pp
)
484 if (!memcmp(&raddr
, *pp
, sizeof(u_int32_t
)))