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 libc_hidden_proto (iruserok_af
)
104 static char ahostbuf
[NI_MAXHOST
];
107 rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, af
)
110 const char *locuser
, *remuser
, *cmd
;
114 char paddr
[INET6_ADDRSTRLEN
];
115 struct addrinfo hints
, *res
, *ai
;
116 struct sockaddr_storage from
;
117 struct pollfd pfd
[2];
120 int s
, lport
, timo
, error
;
126 if (af
!= AF_INET
&& af
!= AF_INET6
&& af
!= AF_UNSPEC
)
128 __set_errno (EAFNOSUPPORT
);
134 memset(&hints
, '\0', sizeof(hints
));
135 hints
.ai_flags
= AI_CANONNAME
;
136 hints
.ai_family
= af
;
137 hints
.ai_socktype
= SOCK_STREAM
;
138 (void)__snprintf(num
, sizeof(num
), "%d", ntohs(rport
));
139 error
= getaddrinfo(*ahost
, num
, &hints
, &res
);
142 if (_IO_fwide (stderr
, 0) > 0)
143 __fwprintf(stderr
, L
"rcmd: getaddrinfo: %s\n",
144 gai_strerror(error
));
147 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
148 gai_strerror(error
));
152 pfd
[0].events
= POLLIN
;
153 pfd
[1].events
= POLLIN
;
155 if (res
->ai_canonname
){
156 strncpy(ahostbuf
, res
->ai_canonname
, sizeof(ahostbuf
));
157 ahostbuf
[sizeof(ahostbuf
)-1] = '\0';
164 oldmask
= __sigblock(sigmask(SIGURG
));
165 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
168 s
= rresvport_af(&lport
, ai
->ai_family
);
170 if (errno
== EAGAIN
) {
172 if (_IO_fwide (stderr
, 0) > 0)
173 __fwprintf(stderr
, L
"%s",
174 _("rcmd: socket: All ports in use\n"));
177 fputs(_("rcmd: socket: All ports in use\n"),
181 if (_IO_fwide (stderr
, 0) > 0)
183 L
"rcmd: socket: %m\n");
186 fprintf(stderr
, "rcmd: socket: %m\n");
188 __sigsetmask(oldmask
);
192 __fcntl(s
, F_SETOWN
, pid
);
193 if (__connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) >= 0)
196 if (errno
== EADDRINUSE
) {
200 if (errno
== ECONNREFUSED
)
202 if (ai
->ai_next
!= NULL
) {
206 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
207 paddr
, sizeof(paddr
),
211 if (__asprintf (&buf
, _("connect to address %s: "),
215 if (_IO_fwide (stderr
, 0) > 0)
216 __fwprintf(stderr
, L
"%s", buf
);
222 __set_errno (oerrno
);
225 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
226 paddr
, sizeof(paddr
),
229 if (__asprintf (&buf
, _("Trying %s...\n"), paddr
) >= 0)
232 if (_IO_fwide (stderr
, 0) > 0)
233 __fwprintf (stderr
, L
"%s", buf
);
241 if (refused
&& timo
<= 16) {
250 if (_IO_fwide (stderr
, 0) > 0)
251 (void)__fwprintf(stderr
, L
"%s: %s\n", *ahost
,
253 errbuf
, sizeof (errbuf
)));
256 (void)fprintf(stderr
, "%s: %s\n", *ahost
,
258 errbuf
, sizeof (errbuf
)));
259 __sigsetmask(oldmask
);
268 int s2
= rresvport_af(&lport
, ai
->ai_family
), s3
;
269 socklen_t len
= ai
->ai_addrlen
;
274 (void)__snprintf(num
, sizeof(num
), "%d", lport
);
275 if (__write(s
, num
, strlen(num
)+1) != (ssize_t
)strlen(num
)+1) {
278 if (__asprintf (&buf
, _("\
279 rcmd: write (setting up stderr): %m\n")) >= 0)
282 if (_IO_fwide (stderr
, 0) > 0)
283 __fwprintf(stderr
, L
"%s", buf
);
295 if (__poll (pfd
, 2, -1) < 1 || (pfd
[1].revents
& POLLIN
) == 0){
299 && __asprintf(&buf
, _("\
300 rcmd: poll (setting up stderr): %m\n")) >= 0)
302 && __asprintf(&buf
, _("\
303 poll: protocol failure in circuit setup\n")) >= 0))
306 if (_IO_fwide (stderr
, 0) > 0)
307 __fwprintf (stderr
, L
"%s", buf
);
316 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
317 switch (from
.ss_family
) {
319 rport
= ntohs(((struct sockaddr_in
*)&from
)->sin_port
);
322 rport
= ntohs(((struct sockaddr_in6
*)&from
)->sin6_port
);
331 if (_IO_fwide (stderr
, 0) > 0)
332 (void)__fwprintf(stderr
,
333 L
"rcmd: accept: %m\n");
336 (void)fprintf(stderr
,
337 "rcmd: accept: %m\n");
343 if (rport
>= IPPORT_RESERVED
|| rport
< IPPORT_RESERVED
/ 2){
346 if (__asprintf(&buf
, _("\
347 socket: protocol failure in circuit setup\n")) >= 0)
350 if (_IO_fwide (stderr
, 0) > 0)
351 __fwprintf (stderr
, L
"%s", buf
);
360 (void)__write(s
, locuser
, strlen(locuser
)+1);
361 (void)__write(s
, remuser
, strlen(remuser
)+1);
362 (void)__write(s
, cmd
, strlen(cmd
)+1);
363 n
= __read(s
, &c
, 1);
368 && __asprintf(&buf
, _("rcmd: %s: short read"),
371 && __asprintf(&buf
, "rcmd: %s: %m\n", *ahost
) >= 0))
374 if (_IO_fwide (stderr
, 0) > 0)
375 __fwprintf (stderr
, L
"%s", buf
);
384 while (__read(s
, &c
, 1) == 1) {
385 (void)__write(STDERR_FILENO
, &c
, 1);
391 __sigsetmask(oldmask
);
396 (void)__close(*fd2p
);
399 __sigsetmask(oldmask
);
403 libc_hidden_def (rcmd_af
)
406 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
409 const char *locuser
, *remuser
, *cmd
;
412 return rcmd_af (ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, AF_INET
);
416 rresvport_af(alport
, family
)
420 struct sockaddr_storage ss
;
427 len
= sizeof(struct sockaddr_in
);
428 sport
= &((struct sockaddr_in
*)&ss
)->sin_port
;
431 len
= sizeof(struct sockaddr_in6
);
432 sport
= &((struct sockaddr_in6
*)&ss
)->sin6_port
;
435 __set_errno (EAFNOSUPPORT
);
438 s
= __socket(family
, SOCK_STREAM
, 0);
442 memset (&ss
, '\0', sizeof(ss
));
446 ss
.ss_family
= family
;
449 *sport
= htons((uint16_t) *alport
);
450 if (__bind(s
, (struct sockaddr
*)&ss
, len
) >= 0)
452 if (errno
!= EADDRINUSE
) {
457 if (*alport
== IPPORT_RESERVED
/2)
461 __set_errno (EAGAIN
);
464 libc_hidden_def (rresvport_af
)
470 return rresvport_af(alport
, AF_INET
);
473 int __check_rhosts_file
= 1;
477 ruserok_af(rhost
, superuser
, ruser
, luser
, af
)
478 const char *rhost
, *ruser
, *luser
;
482 struct addrinfo hints
, *res
, *res0
;
486 memset (&hints
, '\0', sizeof(hints
));
487 hints
.ai_family
= af
;
488 gai
= getaddrinfo(rhost
, NULL
, &hints
, &res0
);
492 for (res
=res0
; res
; res
=res
->ai_next
)
493 if (ruserok2_sa(res
->ai_addr
, res
->ai_addrlen
,
494 superuser
, ruser
, luser
, rhost
) == 0){
501 libc_hidden_def (ruserok_af
)
504 ruserok(rhost
, superuser
, ruser
, luser
)
505 const char *rhost
, *ruser
, *luser
;
508 return ruserok_af(rhost
, superuser
, ruser
, luser
, AF_INET
);
511 /* Extremely paranoid file open function. */
513 iruserfopen (const char *file
, uid_t okuser
)
519 /* If not a regular file, if owned by someone other than user or
520 root, if writeable by anyone but the owner, or if hardlinked
523 if (__lxstat64 (_STAT_VER
, file
, &st
))
524 cp
= _("lstat failed");
525 else if (!S_ISREG (st
.st_mode
))
526 cp
= _("not regular file");
529 res
= fopen (file
, "r");
531 cp
= _("cannot open");
532 else if (__fxstat64 (_STAT_VER
, fileno (res
), &st
) < 0)
533 cp
= _("fstat failed");
534 else if (st
.st_uid
&& st
.st_uid
!= okuser
)
536 else if (st
.st_mode
& (S_IWGRP
|S_IWOTH
))
537 cp
= _("writeable by other than owner");
538 else if (st
.st_nlink
> 1)
539 cp
= _("hard linked somewhere");
542 /* If there were any problems, quit. */
551 /* No threads use this stream. */
552 __fsetlocking (res
, FSETLOCKING_BYCALLER
);
558 * New .rhosts strategy: We are passed an ip address. We spin through
559 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
560 * has ip addresses, we don't have to trust a nameserver. When it
561 * contains hostnames, we spin through the list of addresses the nameserver
562 * gives us and look for a match.
564 * Returns 0 if ok, -1 if not ok.
567 ruserok2_sa (ra
, ralen
, superuser
, ruser
, luser
, rhost
)
571 const char *ruser
, *luser
, *rhost
;
577 hostf
= iruserfopen (_PATH_HEQUIV
, 0);
581 isbad
= __validuser2_sa (hostf
, ra
, ralen
, luser
, ruser
, rhost
);
588 if (__check_rhosts_file
|| superuser
)
591 struct passwd pwdbuf
, *pwd
;
593 size_t buflen
= __sysconf (_SC_GETPW_R_SIZE_MAX
);
594 char *buffer
= __alloca (buflen
);
597 if (__getpwnam_r (luser
, &pwdbuf
, buffer
, buflen
, &pwd
) != 0
601 dirlen
= strlen (pwd
->pw_dir
);
602 pbuf
= alloca (dirlen
+ sizeof "/.rhosts");
603 __mempcpy (__mempcpy (pbuf
, pwd
->pw_dir
, dirlen
),
604 "/.rhosts", sizeof "/.rhosts");
606 /* Change effective uid while reading .rhosts. If root and
607 reading an NFS mounted file system, can't read files that
608 are protected read/write owner only. */
610 seteuid (pwd
->pw_uid
);
611 hostf
= iruserfopen (pbuf
, pwd
->pw_uid
);
615 isbad
= __validuser2_sa (hostf
, ra
, ralen
, luser
, ruser
, rhost
);
625 * ruserok_sa() is now discussed on ipng, so
626 * currently disabled for external use
628 static int ruserok_sa(ra
, ralen
, superuser
, ruser
, luser
)
632 const char *ruser
, *luser
;
634 return ruserok2_sa(ra
, ralen
, superuser
, ruser
, luser
, "-");
637 /* This is the exported version. */
639 iruserok_af (raddr
, superuser
, ruser
, luser
, af
)
642 const char *ruser
, *luser
;
645 struct sockaddr_storage ra
;
648 memset (&ra
, '\0', sizeof(ra
));
651 ((struct sockaddr_in
*)&ra
)->sin_family
= AF_INET
;
652 memcpy (&(((struct sockaddr_in
*)&ra
)->sin_addr
), raddr
,
653 sizeof(struct in_addr
));
654 ralen
= sizeof(struct sockaddr_in
);
657 ((struct sockaddr_in6
*)&ra
)->sin6_family
= AF_INET6
;
658 memcpy (&(((struct sockaddr_in6
*)&ra
)->sin6_addr
), raddr
,
659 sizeof(struct in6_addr
));
660 ralen
= sizeof(struct sockaddr_in6
);
665 return ruserok_sa ((struct sockaddr
*)&ra
, ralen
, superuser
, ruser
, luser
);
667 libc_hidden_def (iruserok_af
)
670 iruserok (raddr
, superuser
, ruser
, luser
)
673 const char *ruser
, *luser
;
675 return iruserok_af (&raddr
, superuser
, ruser
, luser
, AF_INET
);
680 * Don't make static, used by lpd(8).
682 * This function is not used anymore. It is only present because lpd(8)
683 * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
684 * argument. This means that netgroups won't work in .rhost/hosts.equiv
685 * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
687 * Returns 0 if ok, -1 if not ok.
690 __ivaliduser(hostf
, raddr
, luser
, ruser
)
693 const char *luser
, *ruser
;
695 struct sockaddr_in ra
;
696 memset(&ra
, '\0', sizeof(ra
));
697 ra
.sin_family
= AF_INET
;
698 ra
.sin_addr
.s_addr
= raddr
;
699 return __validuser2_sa(hostf
, (struct sockaddr
*)&ra
, sizeof(ra
),
704 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
707 __checkhost_sa (struct sockaddr
*ra
, size_t ralen
, char *lhost
,
710 struct addrinfo hints
, *res0
, *res
;
711 char raddr
[INET6_ADDRSTRLEN
];
713 int negate
=1; /* Multiply return with this to get -1 instead of 1 */
715 /* Check nis netgroup. */
716 if (strncmp ("+@", lhost
, 2) == 0)
717 return innetgr (&lhost
[2], rhost
, NULL
, NULL
);
719 if (strncmp ("-@", lhost
, 2) == 0)
720 return -innetgr (&lhost
[2], rhost
, NULL
, NULL
);
723 if (strncmp ("-", lhost
,1) == 0) {
726 } else if (strcmp ("+",lhost
) == 0) {
727 return 1; /* asking for trouble, but ok.. */
730 /* Try for raw ip address first. */
732 if (getnameinfo(ra
, ralen
,
733 raddr
, sizeof(raddr
), NULL
, 0,
735 && strcmp(raddr
, lhost
) == 0)
738 /* Better be a hostname. */
740 memset(&hints
, '\0', sizeof(hints
));
741 hints
.ai_family
= ra
->sa_family
;
742 if (getaddrinfo(lhost
, NULL
, &hints
, &res0
) == 0){
743 /* Spin through ip addresses. */
744 for (res
= res0
; res
; res
= res
->ai_next
)
746 if (res
->ai_family
== ra
->sa_family
747 && !memcmp(res
->ai_addr
, ra
, res
->ai_addrlen
))
755 return negate
* match
;
758 /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
761 __icheckuser (const char *luser
, const char *ruser
)
764 luser is user entry from .rhosts/hosts.equiv file
765 ruser is user id on remote host
769 if (strncmp ("+@", luser
, 2) == 0)
770 return innetgr (&luser
[2], NULL
, ruser
, NULL
);
772 if (strncmp ("-@", luser
,2) == 0)
773 return -innetgr (&luser
[2], NULL
, ruser
, NULL
);
776 if (strncmp ("-", luser
, 1) == 0)
777 return -(strcmp (&luser
[1], ruser
) == 0);
780 if (strcmp ("+", luser
) == 0)
783 /* simple string match */
784 return strcmp (ruser
, luser
) == 0;
788 * Returns 1 for blank lines (or only comment lines) and 0 otherwise
793 while (*p
&& isspace (*p
)) {
797 return (*p
== '\0' || *p
== '#') ? 1 : 0 ;
801 * Returns 0 if positive match, -1 if _not_ ok.
804 __validuser2_sa(hostf
, ra
, ralen
, luser
, ruser
, rhost
)
808 const char *luser
, *ruser
, *rhost
;
810 register const char *user
;
817 while (__getline (&buf
, &bufsize
, hostf
) > 0) {
818 buf
[bufsize
- 1] = '\0'; /* Make sure it's terminated. */
821 /* Skip empty or comment lines */
826 /* Skip lines that are too long. */
827 if (strchr (p
, '\n') == NULL
) {
828 int ch
= getc_unlocked (hostf
);
830 while (ch
!= '\n' && ch
!= EOF
)
831 ch
= getc_unlocked (hostf
);
835 for (;*p
&& !isspace(*p
); ++p
) {
839 /* Next we want to find the permitted name for the remote user. */
840 if (*p
== ' ' || *p
== '\t') {
841 /* <nul> terminate hostname and skip spaces */
842 for (*p
++='\0'; *p
&& isspace (*p
); ++p
);
844 user
= p
; /* this is the user's name */
845 while (*p
&& !isspace (*p
))
846 ++p
; /* find end of user's name */
850 *p
= '\0'; /* <nul> terminate username (+host?) */
852 /* buf -> host(?) ; user -> username(?) */
854 /* First check host part */
855 hcheck
= __checkhost_sa (ra
, ralen
, buf
, rhost
);
861 /* Then check user part */
865 ucheck
= __icheckuser (user
, ruser
);
867 /* Positive 'host user' match? */
873 /* Negative 'host -user' match? */
877 /* Neither, go on looking for match */