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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid
[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
32 #endif /* LIBC_SCCS and not lint */
34 #include <sys/param.h>
36 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
55 int __ivaliduser
__P ((FILE *, u_int32_t
, const char *, const char *));
56 static int __ivaliduser2
__P ((FILE *, u_int32_t
, const char *, const char *,
61 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
64 const char *locuser
, *remuser
, *cmd
;
67 struct hostent hostbuf
, *hp
;
70 struct sockaddr_in sin
, from
;
81 tmphstbuf
= __alloca (hstbuflen
);
82 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
)
276 if (herr
!= NETDB_INTERNAL
|| errno
!= ERANGE
)
280 /* Enlarge the buffer. */
282 buffer
= __alloca (buflen
);
285 for (ap
= hp
->h_addr_list
; *ap
; ++ap
) {
286 bcopy(*ap
, &addr
, sizeof(addr
));
287 if (iruserok(addr
, superuser
, ruser
, luser
, rhost
) == 0)
293 /* Extremely paranoid file open function. */
295 iruserfopen (char *file
, uid_t okuser
)
301 /* If not a regular file, if owned by someone other than user or
302 root, if writeable by anyone but the owner, or if hardlinked
305 if (__lxstat (_STAT_VER
, file
, &st
))
306 cp
= _("lstat failed");
307 else if (!S_ISREG (st
.st_mode
))
308 cp
= _("not regular file");
311 res
= fopen (file
, "r");
313 cp
= _("cannot open");
314 else if (__fxstat (_STAT_VER
, fileno (res
), &st
) < 0)
315 cp
= _("fstat failed");
316 else if (st
.st_uid
&& st
.st_uid
!= okuser
)
318 else if (st
.st_mode
& (S_IWGRP
|S_IWOTH
))
319 cp
= _("writeable by other than owner");
320 else if (st
.st_nlink
> 1)
321 cp
= _("hard linked somewhere");
324 /* If there were any problems, quit. */
337 * New .rhosts strategy: We are passed an ip address. We spin through
338 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
339 * has ip addresses, we don't have to trust a nameserver. When it
340 * contains hostnames, we spin through the list of addresses the nameserver
341 * gives us and look for a match.
343 * Returns 0 if ok, -1 if not ok.
346 iruserok2 (raddr
, superuser
, ruser
, luser
, rhost
)
349 const char *ruser
, *luser
, *rhost
;
355 hostf
= iruserfopen (_PATH_HEQUIV
, 0);
359 isbad
= __ivaliduser2 (hostf
, raddr
, luser
, ruser
, rhost
);
366 if (__check_rhosts_file
|| superuser
)
369 struct passwd pwdbuf
, *pwd
;
371 size_t buflen
= __sysconf (_SC_GETPW_R_SIZE_MAX
);
372 char *buffer
= __alloca (buflen
);
375 if (__getpwnam_r (luser
, &pwdbuf
, buffer
, buflen
, &pwd
) != 0
379 dirlen
= strlen (pwd
->pw_dir
);
380 pbuf
= alloca (dirlen
+ sizeof "/.rhosts");
381 __mempcpy (__mempcpy (pbuf
, pwd
->pw_dir
, dirlen
),
382 "/.rhosts", sizeof "/.rhosts");
384 /* Change effective uid while reading .rhosts. If root and
385 reading an NFS mounted file system, can't read files that
386 are protected read/write owner only. */
388 seteuid (pwd
->pw_uid
);
389 hostf
= iruserfopen (pbuf
, pwd
->pw_uid
);
393 isbad
= __ivaliduser2 (hostf
, raddr
, luser
, ruser
, rhost
);
403 /* This is the exported version. */
405 iruserok (raddr
, superuser
, ruser
, luser
)
408 const char *ruser
, *luser
;
410 return iruserok2 (raddr
, superuser
, ruser
, luser
, "-");
415 * Don't make static, used by lpd(8).
417 * This function is not used anymore. It is only present because lpd(8)
418 * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
419 * argument. This means that netgroups won't work in .rhost/hosts.equiv
420 * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
422 * Returns 0 if ok, -1 if not ok.
425 __ivaliduser(hostf
, raddr
, luser
, ruser
)
428 const char *luser
, *ruser
;
430 return __ivaliduser2(hostf
, raddr
, luser
, ruser
, "-");
434 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
437 __icheckhost (raddr
, lhost
, rhost
)
442 struct hostent hostbuf
, *hp
;
448 int negate
=1; /* Multiply return with this to get -1 instead of 1 */
451 /* Check nis netgroup. */
452 if (strncmp ("+@", lhost
, 2) == 0)
453 return innetgr (&lhost
[2], rhost
, NULL
, NULL
);
455 if (strncmp ("-@", lhost
, 2) == 0)
456 return -innetgr (&lhost
[2], rhost
, NULL
, NULL
);
459 if (strncmp ("-", lhost
,1) == 0) {
462 } else if (strcmp ("+",lhost
) == 0) {
463 return 1; /* asking for trouble, but ok.. */
466 /* Try for raw ip address first. */
467 if (isdigit (*lhost
) && (long) (laddr
= inet_addr (lhost
)) != -1)
468 return negate
* (! (raddr
^ laddr
));
470 /* Better be a hostname. */
472 buffer
= __alloca (buflen
);
474 while (__gethostbyname_r (lhost
, &hostbuf
, buffer
, buflen
, &hp
, &herr
)
476 if (herr
!= NETDB_INTERNAL
|| errno
!= ERANGE
)
479 /* Enlarge the buffer. */
481 buffer
= __alloca (buflen
);
484 __set_errno (save_errno
);
488 /* Spin through ip addresses. */
489 for (pp
= hp
->h_addr_list
; *pp
; ++pp
)
490 if (!memcmp (&raddr
, *pp
, sizeof (u_int32_t
)))
497 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
500 __icheckuser (luser
, ruser
)
501 const char *luser
, *ruser
;
504 luser is user entry from .rhosts/hosts.equiv file
505 ruser is user id on remote host
510 if (strncmp ("+@", luser
, 2) == 0)
511 return innetgr (&luser
[2], NULL
, ruser
, NULL
);
513 if (strncmp ("-@", luser
,2) == 0)
514 return -innetgr (&luser
[2], NULL
, ruser
, NULL
);
517 if (strncmp ("-", luser
, 1) == 0)
518 return -(strcmp (&luser
[1], ruser
) == 0);
521 if (strcmp ("+", luser
) == 0)
524 /* simple string match */
525 return strcmp (ruser
, luser
) == 0;
529 * Returns 1 for blank lines (or only comment lines) and 0 otherwise
535 while (*p
&& isspace (*p
)) {
539 return (*p
== '\0' || *p
== '#') ? 1 : 0 ;
543 * Returns 0 if positive match, -1 if _not_ ok.
546 __ivaliduser2(hostf
, raddr
, luser
, ruser
, rhost
)
549 const char *luser
, *ruser
, *rhost
;
551 register const char *user
;
558 while (__getline (&buf
, &bufsize
, hostf
) > 0) {
559 buf
[bufsize
- 1] = '\0'; /* Make sure it's terminated. */
562 /* Skip empty or comment lines */
567 /* Skip lines that are too long. */
568 if (strchr (p
, '\n') == NULL
) {
569 int ch
= getc_unlocked (hostf
);
571 while (ch
!= '\n' && ch
!= EOF
)
572 ch
= getc_unlocked (hostf
);
576 for (;*p
&& !isspace(*p
); ++p
) {
580 /* Next we want to find the permitted name for the remote user. */
581 if (*p
== ' ' || *p
== '\t') {
582 /* <nul> terminate hostname and skip spaces */
583 for (*p
++='\0'; *p
&& isspace (*p
); ++p
);
585 user
= p
; /* this is the user's name */
586 while (*p
&& !isspace (*p
))
587 ++p
; /* find end of user's name */
591 *p
= '\0'; /* <nul> terminate username (+host?) */
593 /* buf -> host(?) ; user -> username(?) */
595 /* First check host part */
596 hcheck
= __icheckhost (raddr
, buf
, rhost
);
602 /* Then check user part */
606 ucheck
= __icheckuser (user
, ruser
);
608 /* Positive 'host user' match? */
614 /* Negative 'host -user' match? */
618 /* Neither, go on looking for match */