]>
git.saurik.com Git - apple/libinfo.git/blob - util.subproj/rcmd.c
2 * Copyright (c) 1999-2018 Apple 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 "libinfo_common.h"
67 #include <sys/param.h>
68 #include <sys/socket.h>
71 #include <arpa/inet.h>
85 #include <rpcsvc/yp_prot.h>
86 #include <rpcsvc/ypclnt.h>
89 #include <arpa/nameser_compat.h>
92 /* wrapper for KAME-special getnameinfo() */
93 #ifndef NI_WITHSCOPEID
94 #define NI_WITHSCOPEID 0
101 extern int innetgr
__P(( const char *, const char *, const char *, const char * ));
102 extern int bindresvport_sa(int, struct sockaddr
*);
104 #define max(a, b) ((a > b) ? a : b)
106 int __ivaliduser
__P((FILE *, u_int32_t
, const char *, const char *));
107 int __ivaliduser_af
__P((FILE *,const void *, const char *, const char *,
109 int __ivaliduser_sa
__P((FILE *, const struct sockaddr
*, socklen_t
,
110 const char *,const char *));
111 static int __icheckhost
__P((const struct sockaddr
*, socklen_t
,
116 rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, af
)
119 const char *locuser
, *remuser
, *cmd
;
123 struct addrinfo hints
, *res
, *ai
;
124 struct sockaddr_storage from
;
128 int s
, aport
, lport
, timo
, error
;
131 char num
[8], paddr
[NI_MAXHOST
];
132 static char canonnamebuf
[MAXDNAME
]; /* is it proper here? */
136 memset(&hints
, 0, sizeof(hints
));
137 hints
.ai_flags
= AI_CANONNAME
;
138 hints
.ai_family
= af
;
139 hints
.ai_socktype
= SOCK_STREAM
;
140 hints
.ai_protocol
= 0;
141 (void)snprintf(num
, sizeof(num
), "%d", ntohs(rport
));
142 error
= getaddrinfo(*ahost
, num
, &hints
, &res
);
144 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
145 gai_strerror(error
));
146 if (error
== EAI_SYSTEM
)
147 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
152 if (res
->ai_canonname
153 && strlen(res
->ai_canonname
) + 1 < sizeof(canonnamebuf
)) {
154 strncpy(canonnamebuf
, res
->ai_canonname
, sizeof(canonnamebuf
));
155 *ahost
= canonnamebuf
;
158 for (ai
= res
; ai
; ai
= ai
->ai_next
)
162 oldmask
= sigblock(sigmask(SIGURG
));
163 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
164 s
= rresvport_af(&lport
, ai
->ai_family
);
166 if (errno
!= EAGAIN
&& ai
->ai_next
) {
171 (void)fprintf(stderr
,
172 "rcmd: socket: All ports in use\n");
174 (void)fprintf(stderr
, "rcmd: socket: %s\n",
180 fcntl(s
, F_SETOWN
, pid
);
181 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) >= 0)
184 if (errno
== EADDRINUSE
) {
188 if (errno
== ECONNREFUSED
)
190 if (ai
->ai_next
== NULL
&& (!refused
|| timo
> 16)) {
191 (void)fprintf(stderr
, "%s: %s\n",
192 *ahost
, strerror(errno
));
200 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
201 paddr
, sizeof(paddr
),
203 NI_NUMERICHOST
|NI_WITHSCOPEID
);
204 (void)fprintf(stderr
, "connect to address %s: ",
209 if ((ai
= ai
->ai_next
) == NULL
) {
210 /* refused && timo <= 16 */
211 struct timespec time_to_sleep
, time_remaining
;
213 time_to_sleep
.tv_sec
= timo
;
214 time_to_sleep
.tv_nsec
= 0;
215 (void)nanosleep(&time_to_sleep
, &time_remaining
);
221 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
222 paddr
, sizeof(paddr
),
224 NI_NUMERICHOST
|NI_WITHSCOPEID
);
225 fprintf(stderr
, "Trying %s...\n", paddr
);
230 * try to rresvport() to the same port. This will make rresvport()
231 * fail it's first bind, resulting in it choosing a random port.
240 int s2
= rresvport_af(&lport
, ai
->ai_family
), s3
;
241 unsigned int len
= ai
->ai_addrlen
;
247 (void)snprintf(num
, sizeof(num
), "%d", lport
);
248 if (write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
249 (void)fprintf(stderr
,
250 "rcmd: write (setting up stderr): %s\n",
256 if(nfds
> FD_SETSIZE
) {
257 fprintf(stderr
, "rcmd: too many files\n");
266 if (select(nfds
, &reads
, 0, 0, 0) < 1 || !FD_ISSET(s2
, &reads
)){
268 (void)fprintf(stderr
,
269 "rcmd: select (setting up stderr): %s\n",
272 (void)fprintf(stderr
,
273 "select: protocol failure in circuit setup\n");
277 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
278 switch (from
.ss_family
) {
280 aport
= ntohs(((struct sockaddr_in
*)&from
)->sin_port
);
284 aport
= ntohs(((struct sockaddr_in6
*)&from
)->sin6_port
);
288 aport
= 0; /* error */
292 * XXX careful for ftp bounce attacks. If discovered, shut them
293 * down and check for the real auxiliary channel to connect.
301 (void)fprintf(stderr
,
302 "rcmd: accept: %s\n", strerror(errno
));
307 if (aport
>= IPPORT_RESERVED
|| aport
< IPPORT_RESERVED
/ 2) {
308 (void)fprintf(stderr
,
309 "socket: protocol failure in circuit setup.\n");
313 (void)write(s
, locuser
, strlen(locuser
)+1);
314 (void)write(s
, remuser
, strlen(remuser
)+1);
315 (void)write(s
, cmd
, strlen(cmd
)+1);
316 if (read(s
, &c
, 1) != 1) {
317 (void)fprintf(stderr
,
318 "rcmd: %s: %s\n", *ahost
, strerror(errno
));
322 while (read(s
, &c
, 1) == 1) {
323 (void)write(STDERR_FILENO
, &c
, 1);
344 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
347 const char *locuser
, *remuser
, *cmd
;
350 return rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, AF_INET
);
358 return rresvport_af(port
, AF_INET
);
363 rresvport_af(alport
, family
)
367 struct sockaddr_storage ss
;
370 memset(&ss
, 0, sizeof(ss
));
371 ss
.ss_family
= family
;
374 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in
);
375 sport
= &((struct sockaddr_in
*)&ss
)->sin_port
;
376 ((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
= INADDR_ANY
;
380 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in6
);
381 sport
= &((struct sockaddr_in6
*)&ss
)->sin6_port
;
382 ((struct sockaddr_in6
*)&ss
)->sin6_addr
= in6addr_any
;
386 errno
= EAFNOSUPPORT
;
390 s
= socket(ss
.ss_family
, SOCK_STREAM
, 0);
393 #if 0 /* compat_exact_traditional_rresvport_semantics */
394 sin
.sin_port
= htons((u_short
)*alport
);
395 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
397 if (errno
!= EADDRINUSE
) {
403 if (bindresvport_sa(s
, (struct sockaddr
*)&ss
) == -1) {
407 *alport
= (int)ntohs(*sport
);
411 int __check_rhosts_file
= 1;
414 /* Guess at the size of a password buffer for getpwnam_r (see lookup.subproj/lu_group.c) */
415 #define MAXPWBUF (MAXLOGNAME + 1 + _PASSWORD_LEN + 1 + MAXPATHLEN + 1 + MAXPATHLEN + 1 + 4098)
418 * AF independent extension of iruserok.
420 * Returns 0 if ok, -1 if not ok.
424 iruserok_sa(ra
, rlen
, superuser
, ruser
, luser
)
428 const char *ruser
, *luser
;
432 struct passwd p
, *pwd
;
436 char pbuf
[MAXPATHLEN
];
437 const struct sockaddr
*raddr
;
438 struct sockaddr_storage ss
;
439 char pwbuf
[MAXPWBUF
];
441 /* avoid alignment issue */
442 if (rlen
> sizeof(ss
))
444 memcpy(&ss
, ra
, rlen
);
445 raddr
= (struct sockaddr
*)&ss
;
448 hostf
= superuser
? NULL
: fopen(_PATH_HEQUIV
, "r");
451 if (__ivaliduser_sa(hostf
, raddr
, rlen
, luser
, ruser
) == 0) {
457 if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
460 memset(&p
, 0, sizeof(struct passwd
));
461 memset(pwbuf
, 0, sizeof(pwbuf
));
464 status
= getpwnam_r(luser
, &p
, pwbuf
, MAXPWBUF
, &pwd
);
465 if (status
!= 0) return -1;
467 (void)strcpy(pbuf
, pwd
->pw_dir
);
468 (void)strcat(pbuf
, "/.rhosts");
471 * Change effective uid while opening .rhosts. If root and
472 * reading an NFS mounted file system, can't read files that
473 * are protected read/write owner only.
476 (void)seteuid(pwd
->pw_uid
);
477 hostf
= fopen(pbuf
, "r");
483 * If not a regular file, or is owned by someone other than
484 * user or root or if writeable by anyone but the owner, quit.
487 if (lstat(pbuf
, &sbuf
) < 0)
488 cp
= ".rhosts lstat failed";
489 else if (!S_ISREG(sbuf
.st_mode
))
490 cp
= ".rhosts not regular file";
491 else if (fstat(fileno(hostf
), &sbuf
) < 0)
492 cp
= ".rhosts fstat failed";
493 else if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
)
494 cp
= "bad .rhosts owner";
495 else if (sbuf
.st_mode
& (S_IWGRP
|S_IWOTH
))
496 cp
= ".rhosts writeable by other than owner";
497 /* If there were any problems, quit. */
510 ruserok(rhost
, superuser
, ruser
, luser
)
511 const char *rhost
, *ruser
, *luser
;
514 struct addrinfo hints
, *res
, *r
;
517 memset(&hints
, 0, sizeof(hints
));
518 hints
.ai_family
= PF_UNSPEC
;
519 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
520 error
= getaddrinfo(rhost
, "0", &hints
, &res
);
524 for (r
= res
; r
; r
= r
->ai_next
) {
525 if (iruserok_sa(r
->ai_addr
, r
->ai_addrlen
, superuser
, ruser
,
536 * New .rhosts strategy: We are passed an ip address. We spin through
537 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
538 * has ip addresses, we don't have to trust a nameserver. When it
539 * contains hostnames, we spin through the list of addresses the nameserver
540 * gives us and look for a match.
542 * Returns 0 if ok, -1 if not ok.
546 iruserok(raddr
, superuser
, ruser
, luser
)
549 const char *ruser
, *luser
;
551 struct sockaddr_in sin
;
553 memset(&sin
, 0, sizeof(sin
));
554 sin
.sin_family
= AF_INET
;
555 sin
.sin_len
= sizeof(struct sockaddr_in
);
556 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
557 return iruserok_sa((struct sockaddr
*)&sin
, sin
.sin_len
, superuser
,
563 * Don't make static, used by lpd(8).
565 * Returns 0 if ok, -1 if not ok.
568 __ivaliduser(hostf
, raddr
, luser
, ruser
)
571 const char *luser
, *ruser
;
573 struct sockaddr_in sin
;
575 memset(&sin
, 0, sizeof(sin
));
576 sin
.sin_family
= AF_INET
;
577 sin
.sin_len
= sizeof(struct sockaddr_in
);
578 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
579 return __ivaliduser_sa(hostf
, (struct sockaddr
*)&sin
, sin
.sin_len
,
584 * Returns 0 if ok, -1 if not ok.
589 __ivaliduser_af(hostf
, raddr
, luser
, ruser
, af
, len
)
592 const char *luser
, *ruser
;
595 struct sockaddr
*sa
= NULL
;
596 struct sockaddr_in
*sin
= NULL
;
598 struct sockaddr_in6
*sin6
= NULL
;
600 struct sockaddr_storage ss
;
602 memset(&ss
, 0, sizeof(ss
));
605 if (len
!= sizeof(sin
->sin_addr
))
607 sin
= (struct sockaddr_in
*)&ss
;
608 sin
->sin_family
= AF_INET
;
609 sin
->sin_len
= sizeof(struct sockaddr_in
);
610 memcpy(&sin
->sin_addr
, raddr
, sizeof(sin
->sin_addr
));
614 if (len
!= sizeof(sin6
->sin6_addr
))
616 /* you will lose scope info */
617 sin6
= (struct sockaddr_in6
*)&ss
;
618 sin6
->sin6_family
= AF_INET6
;
619 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
620 memcpy(&sin6
->sin6_addr
, raddr
, sizeof(sin6
->sin6_addr
));
627 sa
= (struct sockaddr
*)&ss
;
628 return __ivaliduser_sa(hostf
, sa
, sa
->sa_len
, luser
, ruser
);
632 * Returns 0 if ok, -1 if not ok.
635 __ivaliduser_sa(hostf
, raddr
, salen
, luser
, ruser
)
637 const struct sockaddr
*raddr
;
639 const char *luser
, *ruser
;
641 register char *user
, *p
;
643 char buf
[MAXHOSTNAMELEN
+ 128]; /* host + login */
644 char hname
[MAXHOSTNAMELEN
];
645 /* Presumed guilty until proven innocent. */
646 int userok
= 0, hostok
= 0;
650 if (yp_get_default_domain(&ypdomain
))
653 #define ypdomain NULL
656 * We try to get the hostname back for netgroup matching.
657 * However, the .rosts file may contain IP addresses as well
658 * as names, so a name is not always required.
660 getnameinfo(raddr
, salen
, hname
, sizeof(hname
), NULL
, 0, NI_NAMEREQD
);
662 while (fgets(buf
, sizeof(buf
), hostf
)) {
664 /* Skip lines that are too long. */
665 if (strchr(p
, '\n') == NULL
) {
666 while ((ch
= getc(hostf
)) != '\n' && ch
!= EOF
);
669 if (*p
== '\n' || *p
== '#') {
673 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
674 *p
= isupper((unsigned char)*p
) ? tolower((unsigned char)*p
) : *p
;
677 if (*p
== ' ' || *p
== '\t') {
679 while (*p
== ' ' || *p
== '\t')
682 while (*p
!= '\n' && *p
!= ' ' &&
683 *p
!= '\t' && *p
!= '\0')
689 * Do +/- and +@/-@ checking. This looks really nasty,
690 * but it matches SunOS's behavior so far as I can tell.
694 if (!buf
[1]) { /* '+' matches all hosts */
698 if (buf
[1] == '@') /* match a host by netgroup */
699 hostok
= innetgr((char *)&buf
[2],
700 (char *)&hname
, NULL
, ypdomain
);
701 else /* match a host by addr */
702 hostok
= __icheckhost(raddr
, salen
,
705 case '-': /* reject '-' hosts and all their users */
707 if (innetgr((char *)&buf
[2],
708 (char *)&hname
, NULL
, ypdomain
))
711 if (__icheckhost(raddr
, salen
,
716 default: /* if no '+' or '-', do a simple match */
717 hostok
= __icheckhost(raddr
, salen
, buf
);
722 if (!*(user
+1)) { /* '+' matches all users */
726 if (*(user
+1) == '@') /* match a user by netgroup */
727 userok
= innetgr(user
+2, NULL
, ruser
, ypdomain
);
728 else /* match a user by direct specification */
729 userok
= !(strcmp(ruser
, user
+1));
731 case '-': /* if we matched a hostname, */
732 if (hostok
) { /* check for user field rejections */
735 if (*(user
+1) == '@') {
736 if (innetgr(user
+2, NULL
,
740 if (!strcmp(ruser
, user
+1))
745 default: /* no rejections: try to match the user */
747 userok
= !(strcmp(ruser
,*user
? user
: luser
));
750 if (hostok
&& userok
)
757 * Returns "true" if match, 0 if no match.
759 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
763 __icheckhost(raddr
, salen
, lhost
)
764 const struct sockaddr
*raddr
;
768 struct sockaddr_in sin
;
769 struct sockaddr_in6
*sin6
;
770 struct addrinfo hints
, *res
, *r
;
772 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
774 if (raddr
->sa_family
== AF_INET6
) {
775 sin6
= (struct sockaddr_in6
*)raddr
;
776 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
)) {
777 memset(&sin
, 0, sizeof(sin
));
778 sin
.sin_family
= AF_INET
;
779 sin
.sin_len
= sizeof(struct sockaddr_in
);
780 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
781 sizeof(sin
.sin_addr
));
782 raddr
= (struct sockaddr
*)&sin
;
788 if (getnameinfo(raddr
, salen
, h1
, sizeof(h1
), NULL
, 0,
789 NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
792 /* Resolve laddr into sockaddr */
793 memset(&hints
, 0, sizeof(hints
));
794 hints
.ai_family
= raddr
->sa_family
;
795 hints
.ai_socktype
= SOCK_DGRAM
; /*XXX dummy*/
797 error
= getaddrinfo(lhost
, "0", &hints
, &res
);
801 for (r
= res
; r
; r
= r
->ai_next
) {
803 if (getnameinfo(r
->ai_addr
, r
->ai_addrlen
, h2
, sizeof(h2
),
804 NULL
, 0, NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
806 if (strcmp(h1
, h2
) == 0) {