2 * Copyright (C) 1998 WIDE Project.
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. Neither the name of the project 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 PROJECT 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 PROJECT 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 * Copyright (c) 1983, 1993, 1994
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #if defined(LIBC_SCCS) && !defined(lint)
59 static char sccsid
[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
60 #endif /* LIBC_SCCS and not lint */
62 #include <sys/param.h>
64 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
78 #include <stdio_ext.h>
88 int __ivaliduser (FILE *, u_int32_t
, const char *, const char *);
89 static int __validuser2_sa (FILE *, struct sockaddr
*, size_t,
90 const char *, const char *, const char *);
91 static int ruserok2_sa (struct sockaddr
*ra
, size_t ralen
,
92 int superuser
, const char *ruser
,
93 const char *luser
, const char *rhost
);
94 static int ruserok_sa (struct sockaddr
*ra
, size_t ralen
,
95 int superuser
, const char *ruser
,
97 int iruserok_af (const void *raddr
, int superuser
, const char *ruser
,
98 const char *luser
, sa_family_t af
);
99 int iruserok (u_int32_t raddr
, int superuser
, const char *ruser
,
102 static char ahostbuf
[NI_MAXHOST
];
105 rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, af
)
108 const char *locuser
, *remuser
, *cmd
;
112 char paddr
[INET6_ADDRSTRLEN
];
113 struct addrinfo hints
, *res
, *ai
;
114 struct sockaddr_storage from
;
115 struct pollfd pfd
[2];
118 int s
, lport
, timo
, error
;
124 if (af
!= AF_INET
&& af
!= AF_INET6
&& af
!= AF_UNSPEC
)
126 __set_errno (EAFNOSUPPORT
);
132 memset(&hints
, '\0', sizeof(hints
));
133 hints
.ai_flags
= AI_CANONNAME
;
134 hints
.ai_family
= af
;
135 hints
.ai_socktype
= SOCK_STREAM
;
136 (void)__snprintf(num
, sizeof(num
), "%d", ntohs(rport
));
137 error
= getaddrinfo(*ahost
, num
, &hints
, &res
);
140 if (_IO_fwide (stderr
, 0) > 0)
141 __fwprintf(stderr
, L
"rcmd: getaddrinfo: %s\n",
142 gai_strerror(error
));
145 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
146 gai_strerror(error
));
150 pfd
[0].events
= POLLIN
;
151 pfd
[1].events
= POLLIN
;
153 if (res
->ai_canonname
){
154 strncpy(ahostbuf
, res
->ai_canonname
, sizeof(ahostbuf
));
155 ahostbuf
[sizeof(ahostbuf
)-1] = '\0';
162 oldmask
= __sigblock(sigmask(SIGURG
));
163 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
166 s
= rresvport_af(&lport
, ai
->ai_family
);
168 if (errno
== EAGAIN
) {
170 if (_IO_fwide (stderr
, 0) > 0)
171 __fwprintf(stderr
, L
"%s",
172 _("rcmd: socket: All ports in use\n"));
175 fputs(_("rcmd: socket: All ports in use\n"),
179 if (_IO_fwide (stderr
, 0) > 0)
181 L
"rcmd: socket: %m\n");
184 fprintf(stderr
, "rcmd: socket: %m\n");
186 __sigsetmask(oldmask
);
190 __fcntl(s
, F_SETOWN
, pid
);
191 if (__connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) >= 0)
194 if (errno
== EADDRINUSE
) {
198 if (errno
== ECONNREFUSED
)
200 if (ai
->ai_next
!= NULL
) {
204 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
205 paddr
, sizeof(paddr
),
209 __asprintf (&buf
, _("connect to address %s: "), paddr
);
211 if (_IO_fwide (stderr
, 0) > 0)
212 __fwprintf(stderr
, L
"%s", buf
);
216 __set_errno (oerrno
);
219 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
220 paddr
, sizeof(paddr
),
223 __asprintf (&buf
, _("Trying %s...\n"), paddr
);
225 if (_IO_fwide (stderr
, 0) > 0)
226 __fwprintf (stderr
, L
"%s", buf
);
233 if (refused
&& timo
<= 16) {
242 if (_IO_fwide (stderr
, 0) > 0)
243 (void)__fwprintf(stderr
, L
"%s: %s\n", *ahost
,
245 errbuf
, sizeof (errbuf
)));
248 (void)fprintf(stderr
, "%s: %s\n", *ahost
,
250 errbuf
, sizeof (errbuf
)));
251 __sigsetmask(oldmask
);
260 int s2
= rresvport_af(&lport
, ai
->ai_family
), s3
;
261 socklen_t len
= ai
->ai_addrlen
;
266 (void)__snprintf(num
, sizeof(num
), "%d", lport
);
267 if (__write(s
, num
, strlen(num
)+1) != (ssize_t
)strlen(num
)+1) {
270 __asprintf (&buf
, _("\
271 rcmd: write (setting up stderr): %m\n"));
273 if (_IO_fwide (stderr
, 0) > 0)
274 __fwprintf(stderr
, L
"%s", buf
);
285 if (__poll (pfd
, 2, -1) < 1 || (pfd
[1].revents
& POLLIN
) == 0){
290 _("rcmd: poll (setting up stderr): %m\n"));
293 _("poll: protocol failure in circuit setup\n"));
295 if (_IO_fwide (stderr
, 0) > 0)
296 __fwprintf (stderr
, L
"%s", buf
);
304 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
305 switch (from
.ss_family
) {
307 rport
= ntohs(((struct sockaddr_in
*)&from
)->sin_port
);
310 rport
= ntohs(((struct sockaddr_in6
*)&from
)->sin6_port
);
319 if (_IO_fwide (stderr
, 0) > 0)
320 (void)__fwprintf(stderr
,
321 L
"rcmd: accept: %m\n");
324 (void)fprintf(stderr
,
325 "rcmd: accept: %m\n");
331 if (rport
>= IPPORT_RESERVED
|| rport
< IPPORT_RESERVED
/ 2){
335 _("socket: protocol failure in circuit setup\n"));
337 if (_IO_fwide (stderr
, 0) > 0)
338 __fwprintf (stderr
, L
"%s", buf
);
346 (void)__write(s
, locuser
, strlen(locuser
)+1);
347 (void)__write(s
, remuser
, strlen(remuser
)+1);
348 (void)__write(s
, cmd
, strlen(cmd
)+1);
349 n
= __read(s
, &c
, 1);
354 __asprintf(&buf
, _("rcmd: %s: short read"), *ahost
);
356 __asprintf(&buf
, "rcmd: %s: %m\n", *ahost
);
358 if (_IO_fwide (stderr
, 0) > 0)
359 __fwprintf (stderr
, L
"%s", buf
);
367 while (__read(s
, &c
, 1) == 1) {
368 (void)__write(STDERR_FILENO
, &c
, 1);
374 __sigsetmask(oldmask
);
379 (void)__close(*fd2p
);
382 __sigsetmask(oldmask
);
388 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
391 const char *locuser
, *remuser
, *cmd
;
394 return rcmd_af (ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, AF_INET
);
398 rresvport_af(alport
, family
)
402 struct sockaddr_storage ss
;
409 len
= sizeof(struct sockaddr_in
);
410 sport
= &((struct sockaddr_in
*)&ss
)->sin_port
;
413 len
= sizeof(struct sockaddr_in6
);
414 sport
= &((struct sockaddr_in6
*)&ss
)->sin6_port
;
417 __set_errno (EAFNOSUPPORT
);
420 s
= __socket(family
, SOCK_STREAM
, 0);
424 memset (&ss
, '\0', sizeof(ss
));
428 ss
.ss_family
= family
;
431 *sport
= htons((uint16_t) *alport
);
432 if (bind(s
, (struct sockaddr
*)&ss
, len
) >= 0)
434 if (errno
!= EADDRINUSE
) {
439 if (*alport
== IPPORT_RESERVED
/2)
443 __set_errno (EAGAIN
);
451 return rresvport_af(alport
, AF_INET
);
454 int __check_rhosts_file
= 1;
458 ruserok_af(rhost
, superuser
, ruser
, luser
, af
)
459 const char *rhost
, *ruser
, *luser
;
463 struct addrinfo hints
, *res
, *res0
;
467 memset (&hints
, '\0', sizeof(hints
));
468 hints
.ai_family
= af
;
469 gai
= getaddrinfo(rhost
, NULL
, &hints
, &res0
);
473 for (res
=res0
; res
; res
=res
->ai_next
)
474 if (ruserok2_sa(res
->ai_addr
, res
->ai_addrlen
,
475 superuser
, ruser
, luser
, rhost
) == 0){
483 ruserok(rhost
, superuser
, ruser
, luser
)
484 const char *rhost
, *ruser
, *luser
;
487 return ruserok_af(rhost
, superuser
, ruser
, luser
, AF_INET
);
490 /* Extremely paranoid file open function. */
492 iruserfopen (const char *file
, uid_t okuser
)
498 /* If not a regular file, if owned by someone other than user or
499 root, if writeable by anyone but the owner, or if hardlinked
502 if (__lxstat64 (_STAT_VER
, file
, &st
))
503 cp
= _("lstat failed");
504 else if (!S_ISREG (st
.st_mode
))
505 cp
= _("not regular file");
508 res
= fopen (file
, "r");
510 cp
= _("cannot open");
511 else if (__fxstat64 (_STAT_VER
, fileno (res
), &st
) < 0)
512 cp
= _("fstat failed");
513 else if (st
.st_uid
&& st
.st_uid
!= okuser
)
515 else if (st
.st_mode
& (S_IWGRP
|S_IWOTH
))
516 cp
= _("writeable by other than owner");
517 else if (st
.st_nlink
> 1)
518 cp
= _("hard linked somewhere");
521 /* If there were any problems, quit. */
530 /* No threads use this stream. */
531 __fsetlocking (res
, FSETLOCKING_BYCALLER
);
537 * New .rhosts strategy: We are passed an ip address. We spin through
538 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
539 * has ip addresses, we don't have to trust a nameserver. When it
540 * contains hostnames, we spin through the list of addresses the nameserver
541 * gives us and look for a match.
543 * Returns 0 if ok, -1 if not ok.
546 ruserok2_sa (ra
, ralen
, superuser
, ruser
, luser
, rhost
)
550 const char *ruser
, *luser
, *rhost
;
556 hostf
= iruserfopen (_PATH_HEQUIV
, 0);
560 isbad
= __validuser2_sa (hostf
, ra
, ralen
, luser
, ruser
, rhost
);
567 if (__check_rhosts_file
|| superuser
)
570 struct passwd pwdbuf
, *pwd
;
572 size_t buflen
= __sysconf (_SC_GETPW_R_SIZE_MAX
);
573 char *buffer
= __alloca (buflen
);
576 if (__getpwnam_r (luser
, &pwdbuf
, buffer
, buflen
, &pwd
) != 0
580 dirlen
= strlen (pwd
->pw_dir
);
581 pbuf
= alloca (dirlen
+ sizeof "/.rhosts");
582 __mempcpy (__mempcpy (pbuf
, pwd
->pw_dir
, dirlen
),
583 "/.rhosts", sizeof "/.rhosts");
585 /* Change effective uid while reading .rhosts. If root and
586 reading an NFS mounted file system, can't read files that
587 are protected read/write owner only. */
589 seteuid (pwd
->pw_uid
);
590 hostf
= iruserfopen (pbuf
, pwd
->pw_uid
);
594 isbad
= __validuser2_sa (hostf
, ra
, ralen
, luser
, ruser
, rhost
);
604 * ruserok_sa() is now discussed on ipng, so
605 * currently disabled for external use
607 static int ruserok_sa(ra
, ralen
, superuser
, ruser
, luser
)
611 const char *ruser
, *luser
;
613 return ruserok2_sa(ra
, ralen
, superuser
, ruser
, luser
, "-");
616 /* This is the exported version. */
618 iruserok_af (raddr
, superuser
, ruser
, luser
, af
)
621 const char *ruser
, *luser
;
624 struct sockaddr_storage ra
;
627 memset (&ra
, '\0', sizeof(ra
));
630 ((struct sockaddr_in
*)&ra
)->sin_family
= AF_INET
;
631 memcpy (&(((struct sockaddr_in
*)&ra
)->sin_addr
), raddr
,
632 sizeof(struct in_addr
));
633 ralen
= sizeof(struct sockaddr_in
);
636 ((struct sockaddr_in6
*)&ra
)->sin6_family
= AF_INET6
;
637 memcpy (&(((struct sockaddr_in6
*)&ra
)->sin6_addr
), raddr
,
638 sizeof(struct in6_addr
));
639 ralen
= sizeof(struct sockaddr_in6
);
644 return ruserok_sa ((struct sockaddr
*)&ra
, ralen
, superuser
, ruser
, luser
);
647 iruserok (raddr
, superuser
, ruser
, luser
)
650 const char *ruser
, *luser
;
652 return iruserok_af (&raddr
, superuser
, ruser
, luser
, AF_INET
);
657 * Don't make static, used by lpd(8).
659 * This function is not used anymore. It is only present because lpd(8)
660 * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
661 * argument. This means that netgroups won't work in .rhost/hosts.equiv
662 * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
664 * Returns 0 if ok, -1 if not ok.
667 __ivaliduser(hostf
, raddr
, luser
, ruser
)
670 const char *luser
, *ruser
;
672 struct sockaddr_in ra
;
673 memset(&ra
, '\0', sizeof(ra
));
674 ra
.sin_family
= AF_INET
;
675 ra
.sin_addr
.s_addr
= raddr
;
676 return __validuser2_sa(hostf
, (struct sockaddr
*)&ra
, sizeof(ra
),
681 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
684 __checkhost_sa (struct sockaddr
*ra
, size_t ralen
, char *lhost
,
687 struct addrinfo hints
, *res0
, *res
;
688 char raddr
[INET6_ADDRSTRLEN
];
690 int negate
=1; /* Multiply return with this to get -1 instead of 1 */
692 /* Check nis netgroup. */
693 if (strncmp ("+@", lhost
, 2) == 0)
694 return innetgr (&lhost
[2], rhost
, NULL
, NULL
);
696 if (strncmp ("-@", lhost
, 2) == 0)
697 return -innetgr (&lhost
[2], rhost
, NULL
, NULL
);
700 if (strncmp ("-", lhost
,1) == 0) {
703 } else if (strcmp ("+",lhost
) == 0) {
704 return 1; /* asking for trouble, but ok.. */
707 /* Try for raw ip address first. */
709 if (getnameinfo(ra
, ralen
,
710 raddr
, sizeof(raddr
), NULL
, 0,
712 && strcmp(raddr
, lhost
) == 0)
715 /* Better be a hostname. */
717 memset(&hints
, '\0', sizeof(hints
));
718 hints
.ai_family
= ra
->sa_family
;
719 if (getaddrinfo(lhost
, NULL
, &hints
, &res0
) == 0){
720 /* Spin through ip addresses. */
721 for (res
= res0
; res
; res
= res
->ai_next
)
723 if (res
->ai_family
== ra
->sa_family
724 && !memcmp(res
->ai_addr
, ra
, res
->ai_addrlen
))
732 return negate
* match
;
735 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
738 __icheckuser (const char *luser
, const char *ruser
)
741 luser is user entry from .rhosts/hosts.equiv file
742 ruser is user id on remote host
746 if (strncmp ("+@", luser
, 2) == 0)
747 return innetgr (&luser
[2], NULL
, ruser
, NULL
);
749 if (strncmp ("-@", luser
,2) == 0)
750 return -innetgr (&luser
[2], NULL
, ruser
, NULL
);
753 if (strncmp ("-", luser
, 1) == 0)
754 return -(strcmp (&luser
[1], ruser
) == 0);
757 if (strcmp ("+", luser
) == 0)
760 /* simple string match */
761 return strcmp (ruser
, luser
) == 0;
765 * Returns 1 for blank lines (or only comment lines) and 0 otherwise
770 while (*p
&& isspace (*p
)) {
774 return (*p
== '\0' || *p
== '#') ? 1 : 0 ;
778 * Returns 0 if positive match, -1 if _not_ ok.
781 __validuser2_sa(hostf
, ra
, ralen
, luser
, ruser
, rhost
)
785 const char *luser
, *ruser
, *rhost
;
787 register const char *user
;
794 while (__getline (&buf
, &bufsize
, hostf
) > 0) {
795 buf
[bufsize
- 1] = '\0'; /* Make sure it's terminated. */
798 /* Skip empty or comment lines */
803 /* Skip lines that are too long. */
804 if (strchr (p
, '\n') == NULL
) {
805 int ch
= getc_unlocked (hostf
);
807 while (ch
!= '\n' && ch
!= EOF
)
808 ch
= getc_unlocked (hostf
);
812 for (;*p
&& !isspace(*p
); ++p
) {
816 /* Next we want to find the permitted name for the remote user. */
817 if (*p
== ' ' || *p
== '\t') {
818 /* <nul> terminate hostname and skip spaces */
819 for (*p
++='\0'; *p
&& isspace (*p
); ++p
);
821 user
= p
; /* this is the user's name */
822 while (*p
&& !isspace (*p
))
823 ++p
; /* find end of user's name */
827 *p
= '\0'; /* <nul> terminate username (+host?) */
829 /* buf -> host(?) ; user -> username(?) */
831 /* First check host part */
832 hcheck
= __checkhost_sa (ra
, ralen
, buf
, rhost
);
838 /* Then check user part */
842 ucheck
= __icheckuser (user
, ruser
);
844 /* Positive 'host user' match? */
850 /* Negative 'host -user' match? */
854 /* Neither, go on looking for match */