2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved.
26 * Copyright (c) 1983, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * This product includes software developed by Theo de Raadt.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * $FreeBSD: src/lib/libc/net/rcmd.c,v 1.23.2.5 2001/03/05 10:47:11 obrien Exp $
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char sccsid
[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
63 #endif /* LIBC_SCCS and not lint */
65 #include <sys/param.h>
66 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #include <netinfo/ni_util.h>
84 #include <rpcsvc/yp_prot.h>
85 #include <rpcsvc/ypclnt.h>
87 #include <arpa/nameser.h>
89 /* wrapper for KAME-special getnameinfo() */
90 #ifndef NI_WITHSCOPEID
91 #define NI_WITHSCOPEID 0
98 extern int innetgr
__P(( const char *, const char *, const char *, const char * ));
100 #define max(a, b) ((a > b) ? a : b)
102 int __ivaliduser
__P((FILE *, u_int32_t
, const char *, const char *));
103 int __ivaliduser_af
__P((FILE *,const void *, const char *, const char *,
105 int __ivaliduser_sa
__P((FILE *, const struct sockaddr
*, socklen_t
,
106 const char *,const char *));
107 static int __icheckhost
__P((const struct sockaddr
*, socklen_t
,
112 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
115 const char *locuser
, *remuser
, *cmd
;
118 return rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, AF_INET
);
122 rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, af
)
125 const char *locuser
, *remuser
, *cmd
;
129 struct addrinfo hints
, *res
, *ai
;
130 struct sockaddr_storage from
;
134 int s
, aport
, lport
, timo
, error
;
137 char num
[8], paddr
[NI_MAXHOST
];
138 static char canonnamebuf
[MAXDNAME
]; /* is it proper here? */
142 memset(&hints
, 0, sizeof(hints
));
143 hints
.ai_flags
= AI_CANONNAME
;
144 hints
.ai_family
= af
;
145 hints
.ai_socktype
= SOCK_STREAM
;
146 hints
.ai_protocol
= 0;
147 (void)snprintf(num
, sizeof(num
), "%d", ntohs(rport
));
148 error
= getaddrinfo(*ahost
, num
, &hints
, &res
);
150 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
151 gai_strerror(error
));
152 if (error
== EAI_SYSTEM
)
153 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
158 if (res
->ai_canonname
159 && strlen(res
->ai_canonname
) + 1 < sizeof(canonnamebuf
)) {
160 strncpy(canonnamebuf
, res
->ai_canonname
, sizeof(canonnamebuf
));
161 *ahost
= canonnamebuf
;
164 for (ai
= res
; ai
; ai
= ai
->ai_next
)
168 oldmask
= sigblock(sigmask(SIGURG
));
169 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
170 s
= rresvport_af(&lport
, ai
->ai_family
);
172 if (errno
!= EAGAIN
&& ai
->ai_next
) {
177 (void)fprintf(stderr
,
178 "rcmd: socket: All ports in use\n");
180 (void)fprintf(stderr
, "rcmd: socket: %s\n",
186 fcntl(s
, F_SETOWN
, pid
);
187 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) >= 0)
190 if (errno
== EADDRINUSE
) {
194 if (errno
== ECONNREFUSED
)
196 if (ai
->ai_next
== NULL
&& (!refused
|| timo
> 16)) {
197 (void)fprintf(stderr
, "%s: %s\n",
198 *ahost
, strerror(errno
));
206 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
207 paddr
, sizeof(paddr
),
209 NI_NUMERICHOST
|NI_WITHSCOPEID
);
210 (void)fprintf(stderr
, "connect to address %s: ",
215 if ((ai
= ai
->ai_next
) == NULL
) {
216 /* refused && timo <= 16 */
217 struct timespec time_to_sleep
, time_remaining
;
219 time_to_sleep
.tv_sec
= timo
;
220 time_to_sleep
.tv_nsec
= 0;
221 (void)nanosleep(&time_to_sleep
, &time_remaining
);
227 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
228 paddr
, sizeof(paddr
),
230 NI_NUMERICHOST
|NI_WITHSCOPEID
);
231 fprintf(stderr
, "Trying %s...\n", paddr
);
236 * try to rresvport() to the same port. This will make rresvport()
237 * fail it's first bind, resulting in it choosing a random port.
246 int s2
= rresvport_af(&lport
, ai
->ai_family
), s3
;
247 int len
= ai
->ai_addrlen
;
253 (void)snprintf(num
, sizeof(num
), "%d", lport
);
254 if (write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
255 (void)fprintf(stderr
,
256 "rcmd: write (setting up stderr): %s\n",
262 if(nfds
> FD_SETSIZE
) {
263 fprintf(stderr
, "rcmd: too many files\n");
272 if (select(nfds
, &reads
, 0, 0, 0) < 1 || !FD_ISSET(s2
, &reads
)){
274 (void)fprintf(stderr
,
275 "rcmd: select (setting up stderr): %s\n",
278 (void)fprintf(stderr
,
279 "select: protocol failure in circuit setup\n");
283 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
284 switch (from
.ss_family
) {
286 aport
= ntohs(((struct sockaddr_in
*)&from
)->sin_port
);
290 aport
= ntohs(((struct sockaddr_in6
*)&from
)->sin6_port
);
294 aport
= 0; /* error */
298 * XXX careful for ftp bounce attacks. If discovered, shut them
299 * down and check for the real auxiliary channel to connect.
307 (void)fprintf(stderr
,
308 "rcmd: accept: %s\n", strerror(errno
));
313 if (aport
>= IPPORT_RESERVED
|| aport
< IPPORT_RESERVED
/ 2) {
314 (void)fprintf(stderr
,
315 "socket: protocol failure in circuit setup.\n");
319 (void)write(s
, locuser
, strlen(locuser
)+1);
320 (void)write(s
, remuser
, strlen(remuser
)+1);
321 (void)write(s
, cmd
, strlen(cmd
)+1);
322 if (read(s
, &c
, 1) != 1) {
323 (void)fprintf(stderr
,
324 "rcmd: %s: %s\n", *ahost
, strerror(errno
));
328 while (read(s
, &c
, 1) == 1) {
329 (void)write(STDERR_FILENO
, &c
, 1);
352 return rresvport_af(port
, AF_INET
);
356 rresvport_af(alport
, family
)
360 struct sockaddr_storage ss
;
363 memset(&ss
, 0, sizeof(ss
));
364 ss
.ss_family
= family
;
367 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in
);
368 sport
= &((struct sockaddr_in
*)&ss
)->sin_port
;
369 ((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
= INADDR_ANY
;
373 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in6
);
374 sport
= &((struct sockaddr_in6
*)&ss
)->sin6_port
;
375 ((struct sockaddr_in6
*)&ss
)->sin6_addr
= in6addr_any
;
379 errno
= EAFNOSUPPORT
;
383 s
= socket(ss
.ss_family
, SOCK_STREAM
, 0);
386 #if 0 /* compat_exact_traditional_rresvport_semantics */
387 sin
.sin_port
= htons((u_short
)*alport
);
388 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
390 if (errno
!= EADDRINUSE
) {
396 if (bindresvport_sa(s
, (struct sockaddr
*)&ss
) == -1) {
400 *alport
= (int)ntohs(*sport
);
404 int __check_rhosts_file
= 1;
408 ruserok(rhost
, superuser
, ruser
, luser
)
409 const char *rhost
, *ruser
, *luser
;
412 struct addrinfo hints
, *res
, *r
;
415 memset(&hints
, 0, sizeof(hints
));
416 hints
.ai_family
= PF_UNSPEC
;
417 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
418 error
= getaddrinfo(rhost
, "0", &hints
, &res
);
422 for (r
= res
; r
; r
= r
->ai_next
) {
423 if (iruserok_sa(r
->ai_addr
, r
->ai_addrlen
, superuser
, ruser
,
434 * New .rhosts strategy: We are passed an ip address. We spin through
435 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
436 * has ip addresses, we don't have to trust a nameserver. When it
437 * contains hostnames, we spin through the list of addresses the nameserver
438 * gives us and look for a match.
440 * Returns 0 if ok, -1 if not ok.
443 iruserok(raddr
, superuser
, ruser
, luser
)
446 const char *ruser
, *luser
;
448 struct sockaddr_in sin
;
450 memset(&sin
, 0, sizeof(sin
));
451 sin
.sin_family
= AF_INET
;
452 sin
.sin_len
= sizeof(struct sockaddr_in
);
453 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
454 return iruserok_sa((struct sockaddr
*)&sin
, sin
.sin_len
, superuser
,
459 * AF independent extension of iruserok.
461 * Returns 0 if ok, -1 if not ok.
464 iruserok_sa(ra
, rlen
, superuser
, ruser
, luser
)
468 const char *ruser
, *luser
;
476 char pbuf
[MAXPATHLEN
];
477 const struct sockaddr
*raddr
;
478 struct sockaddr_storage ss
;
480 /* avoid alignment issue */
481 if (rlen
> sizeof(ss
))
483 memcpy(&ss
, ra
, rlen
);
484 raddr
= (struct sockaddr
*)&ss
;
487 hostf
= superuser ? NULL
: fopen(_PATH_HEQUIV
, "r");
490 if (__ivaliduser_sa(hostf
, raddr
, rlen
, luser
, ruser
) == 0) {
496 if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
498 if ((pwd
= getpwnam(luser
)) == NULL
)
500 (void)strcpy(pbuf
, pwd
->pw_dir
);
501 (void)strcat(pbuf
, "/.rhosts");
504 * Change effective uid while opening .rhosts. If root and
505 * reading an NFS mounted file system, can't read files that
506 * are protected read/write owner only.
509 (void)seteuid(pwd
->pw_uid
);
510 hostf
= fopen(pbuf
, "r");
516 * If not a regular file, or is owned by someone other than
517 * user or root or if writeable by anyone but the owner, quit.
520 if (lstat(pbuf
, &sbuf
) < 0)
521 cp
= ".rhosts lstat failed";
522 else if (!S_ISREG(sbuf
.st_mode
))
523 cp
= ".rhosts not regular file";
524 else if (fstat(fileno(hostf
), &sbuf
) < 0)
525 cp
= ".rhosts fstat failed";
526 else if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
)
527 cp
= "bad .rhosts owner";
528 else if (sbuf
.st_mode
& (S_IWGRP
|S_IWOTH
))
529 cp
= ".rhosts writeable by other than owner";
530 /* If there were any problems, quit. */
543 * Don't make static, used by lpd(8).
545 * Returns 0 if ok, -1 if not ok.
548 __ivaliduser(hostf
, raddr
, luser
, ruser
)
551 const char *luser
, *ruser
;
553 struct sockaddr_in sin
;
555 memset(&sin
, 0, sizeof(sin
));
556 sin
.sin_family
= AF_INET
;
557 sin
.sin_len
= sizeof(struct sockaddr_in
);
558 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
559 return __ivaliduser_sa(hostf
, (struct sockaddr
*)&sin
, sin
.sin_len
,
564 * Returns 0 if ok, -1 if not ok.
569 __ivaliduser_af(hostf
, raddr
, luser
, ruser
, af
, len
)
572 const char *luser
, *ruser
;
575 struct sockaddr
*sa
= NULL
;
576 struct sockaddr_in
*sin
= NULL
;
578 struct sockaddr_in6
*sin6
= NULL
;
580 struct sockaddr_storage ss
;
582 memset(&ss
, 0, sizeof(ss
));
585 if (len
!= sizeof(sin
->sin_addr
))
587 sin
= (struct sockaddr_in
*)&ss
;
588 sin
->sin_family
= AF_INET
;
589 sin
->sin_len
= sizeof(struct sockaddr_in
);
590 memcpy(&sin
->sin_addr
, raddr
, sizeof(sin
->sin_addr
));
594 if (len
!= sizeof(sin6
->sin6_addr
))
596 /* you will lose scope info */
597 sin6
= (struct sockaddr_in6
*)&ss
;
598 sin6
->sin6_family
= AF_INET6
;
599 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
600 memcpy(&sin6
->sin6_addr
, raddr
, sizeof(sin6
->sin6_addr
));
607 sa
= (struct sockaddr
*)&ss
;
608 return __ivaliduser_sa(hostf
, sa
, sa
->sa_len
, luser
, ruser
);
612 * Returns 0 if ok, -1 if not ok.
615 __ivaliduser_sa(hostf
, raddr
, salen
, luser
, ruser
)
617 const struct sockaddr
*raddr
;
619 const char *luser
, *ruser
;
621 register char *user
, *p
;
623 char buf
[MAXHOSTNAMELEN
+ 128]; /* host + login */
624 char hname
[MAXHOSTNAMELEN
];
625 /* Presumed guilty until proven innocent. */
626 int userok
= 0, hostok
= 0;
630 if (yp_get_default_domain(&ypdomain
))
633 #define ypdomain NULL
635 /* We need to get the damn hostname back for netgroup matching. */
636 if (getnameinfo(raddr
, salen
, hname
, sizeof(hname
), NULL
, 0,
640 while (fgets(buf
, sizeof(buf
), hostf
)) {
642 /* Skip lines that are too long. */
643 if (strchr(p
, '\n') == NULL
) {
644 while ((ch
= getc(hostf
)) != '\n' && ch
!= EOF
);
647 if (*p
== '\n' || *p
== '#') {
651 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
652 *p
= isupper((unsigned char)*p
) ?
tolower((unsigned char)*p
) : *p
;
655 if (*p
== ' ' || *p
== '\t') {
657 while (*p
== ' ' || *p
== '\t')
660 while (*p
!= '\n' && *p
!= ' ' &&
661 *p
!= '\t' && *p
!= '\0')
667 * Do +/- and +@/-@ checking. This looks really nasty,
668 * but it matches SunOS's behavior so far as I can tell.
672 if (!buf
[1]) { /* '+' matches all hosts */
676 if (buf
[1] == '@') /* match a host by netgroup */
677 hostok
= innetgr((char *)&buf
[2],
678 (char *)&hname
, NULL
, ypdomain
);
679 else /* match a host by addr */
680 hostok
= __icheckhost(raddr
, salen
,
683 case '-': /* reject '-' hosts and all their users */
685 if (innetgr((char *)&buf
[2],
686 (char *)&hname
, NULL
, ypdomain
))
689 if (__icheckhost(raddr
, salen
,
694 default: /* if no '+' or '-', do a simple match */
695 hostok
= __icheckhost(raddr
, salen
, buf
);
700 if (!*(user
+1)) { /* '+' matches all users */
704 if (*(user
+1) == '@') /* match a user by netgroup */
705 userok
= innetgr(user
+2, NULL
, ruser
, ypdomain
);
706 else /* match a user by direct specification */
707 userok
= !(strcmp(ruser
, user
+1));
709 case '-': /* if we matched a hostname, */
710 if (hostok
) { /* check for user field rejections */
713 if (*(user
+1) == '@') {
714 if (innetgr(user
+2, NULL
,
718 if (!strcmp(ruser
, user
+1))
723 default: /* no rejections: try to match the user */
725 userok
= !(strcmp(ruser
,*user ? user
: luser
));
728 if (hostok
&& userok
)
735 * Returns "true" if match, 0 if no match.
737 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
741 __icheckhost(raddr
, salen
, lhost
)
742 const struct sockaddr
*raddr
;
746 struct sockaddr_in sin
;
747 struct sockaddr_in6
*sin6
;
748 struct addrinfo hints
, *res
, *r
;
750 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
752 if (raddr
->sa_family
== AF_INET6
) {
753 sin6
= (struct sockaddr_in6
*)raddr
;
754 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
)) {
755 memset(&sin
, 0, sizeof(sin
));
756 sin
.sin_family
= AF_INET
;
757 sin
.sin_len
= sizeof(struct sockaddr_in
);
758 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
759 sizeof(sin
.sin_addr
));
760 raddr
= (struct sockaddr
*)&sin
;
766 if (getnameinfo(raddr
, salen
, h1
, sizeof(h1
), NULL
, 0,
767 NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
770 /* Resolve laddr into sockaddr */
771 memset(&hints
, 0, sizeof(hints
));
772 hints
.ai_family
= raddr
->sa_family
;
773 hints
.ai_socktype
= SOCK_DGRAM
; /*XXX dummy*/
775 error
= getaddrinfo(lhost
, "0", &hints
, &res
);
779 for (r
= res
; r
; r
= r
->ai_next
) {
781 if (getnameinfo(r
->ai_addr
, r
->ai_addrlen
, h2
, sizeof(h2
),
782 NULL
, 0, NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
784 if (strcmp(h1
, h2
) == 0) {