]>
git.saurik.com Git - apple/libinfo.git/blob - util.subproj/rcmd.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved.
27 * Copyright (c) 1983, 1993, 1994
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * This product includes software developed by Theo de Raadt.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * $FreeBSD: src/lib/libc/net/rcmd.c,v 1.23.2.5 2001/03/05 10:47:11 obrien Exp $
62 #if defined(LIBC_SCCS) && !defined(lint)
63 static char sccsid
[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
64 #endif /* LIBC_SCCS and not lint */
66 #include <sys/param.h>
67 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
72 #include <netinfo/ni_util.h>
85 #include <rpcsvc/yp_prot.h>
86 #include <rpcsvc/ypclnt.h>
88 #include <arpa/nameser8_compat.h>
90 /* wrapper for KAME-special getnameinfo() */
91 #ifndef NI_WITHSCOPEID
92 #define NI_WITHSCOPEID 0
99 extern int innetgr
__P(( const char *, const char *, const char *, const char * ));
100 extern int bindresvport_sa(int, struct sockaddr
*);
102 #define max(a, b) ((a > b) ? a : b)
104 int __ivaliduser
__P((FILE *, u_int32_t
, const char *, const char *));
105 int __ivaliduser_af
__P((FILE *,const void *, const char *, const char *,
107 int __ivaliduser_sa
__P((FILE *, const struct sockaddr
*, socklen_t
,
108 const char *,const char *));
109 static int __icheckhost
__P((const struct sockaddr
*, socklen_t
,
113 rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, af
)
116 const char *locuser
, *remuser
, *cmd
;
120 struct addrinfo hints
, *res
, *ai
;
121 struct sockaddr_storage from
;
125 int s
, aport
, lport
, timo
, error
;
128 char num
[8], paddr
[NI_MAXHOST
];
129 static char canonnamebuf
[MAXDNAME
]; /* is it proper here? */
133 memset(&hints
, 0, sizeof(hints
));
134 hints
.ai_flags
= AI_CANONNAME
;
135 hints
.ai_family
= af
;
136 hints
.ai_socktype
= SOCK_STREAM
;
137 hints
.ai_protocol
= 0;
138 (void)snprintf(num
, sizeof(num
), "%d", ntohs(rport
));
139 error
= getaddrinfo(*ahost
, num
, &hints
, &res
);
141 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
142 gai_strerror(error
));
143 if (error
== EAI_SYSTEM
)
144 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
149 if (res
->ai_canonname
150 && strlen(res
->ai_canonname
) + 1 < sizeof(canonnamebuf
)) {
151 strncpy(canonnamebuf
, res
->ai_canonname
, sizeof(canonnamebuf
));
152 *ahost
= canonnamebuf
;
155 for (ai
= res
; ai
; ai
= ai
->ai_next
)
159 oldmask
= sigblock(sigmask(SIGURG
));
160 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
161 s
= rresvport_af(&lport
, ai
->ai_family
);
163 if (errno
!= EAGAIN
&& ai
->ai_next
) {
168 (void)fprintf(stderr
,
169 "rcmd: socket: All ports in use\n");
171 (void)fprintf(stderr
, "rcmd: socket: %s\n",
177 fcntl(s
, F_SETOWN
, pid
);
178 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) >= 0)
181 if (errno
== EADDRINUSE
) {
185 if (errno
== ECONNREFUSED
)
187 if (ai
->ai_next
== NULL
&& (!refused
|| timo
> 16)) {
188 (void)fprintf(stderr
, "%s: %s\n",
189 *ahost
, strerror(errno
));
197 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
198 paddr
, sizeof(paddr
),
200 NI_NUMERICHOST
|NI_WITHSCOPEID
);
201 (void)fprintf(stderr
, "connect to address %s: ",
206 if ((ai
= ai
->ai_next
) == NULL
) {
207 /* refused && timo <= 16 */
208 struct timespec time_to_sleep
, time_remaining
;
210 time_to_sleep
.tv_sec
= timo
;
211 time_to_sleep
.tv_nsec
= 0;
212 (void)nanosleep(&time_to_sleep
, &time_remaining
);
218 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
219 paddr
, sizeof(paddr
),
221 NI_NUMERICHOST
|NI_WITHSCOPEID
);
222 fprintf(stderr
, "Trying %s...\n", paddr
);
227 * try to rresvport() to the same port. This will make rresvport()
228 * fail it's first bind, resulting in it choosing a random port.
237 int s2
= rresvport_af(&lport
, ai
->ai_family
), s3
;
238 int len
= ai
->ai_addrlen
;
244 (void)snprintf(num
, sizeof(num
), "%d", lport
);
245 if (write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
246 (void)fprintf(stderr
,
247 "rcmd: write (setting up stderr): %s\n",
253 if(nfds
> FD_SETSIZE
) {
254 fprintf(stderr
, "rcmd: too many files\n");
263 if (select(nfds
, &reads
, 0, 0, 0) < 1 || !FD_ISSET(s2
, &reads
)){
265 (void)fprintf(stderr
,
266 "rcmd: select (setting up stderr): %s\n",
269 (void)fprintf(stderr
,
270 "select: protocol failure in circuit setup\n");
274 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
275 switch (from
.ss_family
) {
277 aport
= ntohs(((struct sockaddr_in
*)&from
)->sin_port
);
281 aport
= ntohs(((struct sockaddr_in6
*)&from
)->sin6_port
);
285 aport
= 0; /* error */
289 * XXX careful for ftp bounce attacks. If discovered, shut them
290 * down and check for the real auxiliary channel to connect.
298 (void)fprintf(stderr
,
299 "rcmd: accept: %s\n", strerror(errno
));
304 if (aport
>= IPPORT_RESERVED
|| aport
< IPPORT_RESERVED
/ 2) {
305 (void)fprintf(stderr
,
306 "socket: protocol failure in circuit setup.\n");
310 (void)write(s
, locuser
, strlen(locuser
)+1);
311 (void)write(s
, remuser
, strlen(remuser
)+1);
312 (void)write(s
, cmd
, strlen(cmd
)+1);
313 if (read(s
, &c
, 1) != 1) {
314 (void)fprintf(stderr
,
315 "rcmd: %s: %s\n", *ahost
, strerror(errno
));
319 while (read(s
, &c
, 1) == 1) {
320 (void)write(STDERR_FILENO
, &c
, 1);
340 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
343 const char *locuser
, *remuser
, *cmd
;
346 return rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, AF_INET
);
353 return rresvport_af(port
, AF_INET
);
357 rresvport_af(alport
, family
)
361 struct sockaddr_storage ss
;
364 memset(&ss
, 0, sizeof(ss
));
365 ss
.ss_family
= family
;
368 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in
);
369 sport
= &((struct sockaddr_in
*)&ss
)->sin_port
;
370 ((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
= INADDR_ANY
;
374 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in6
);
375 sport
= &((struct sockaddr_in6
*)&ss
)->sin6_port
;
376 ((struct sockaddr_in6
*)&ss
)->sin6_addr
= in6addr_any
;
380 errno
= EAFNOSUPPORT
;
384 s
= socket(ss
.ss_family
, SOCK_STREAM
, 0);
387 #if 0 /* compat_exact_traditional_rresvport_semantics */
388 sin
.sin_port
= htons((u_short
)*alport
);
389 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
391 if (errno
!= EADDRINUSE
) {
397 if (bindresvport_sa(s
, (struct sockaddr
*)&ss
) == -1) {
401 *alport
= (int)ntohs(*sport
);
405 int __check_rhosts_file
= 1;
409 * AF independent extension of iruserok.
411 * Returns 0 if ok, -1 if not ok.
414 iruserok_sa(ra
, rlen
, superuser
, ruser
, luser
)
418 const char *ruser
, *luser
;
426 char pbuf
[MAXPATHLEN
];
427 const struct sockaddr
*raddr
;
428 struct sockaddr_storage ss
;
430 /* avoid alignment issue */
431 if (rlen
> sizeof(ss
))
433 memcpy(&ss
, ra
, rlen
);
434 raddr
= (struct sockaddr
*)&ss
;
437 hostf
= superuser
? NULL
: fopen(_PATH_HEQUIV
, "r");
440 if (__ivaliduser_sa(hostf
, raddr
, rlen
, luser
, ruser
) == 0) {
446 if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
448 if ((pwd
= getpwnam(luser
)) == NULL
)
450 (void)strcpy(pbuf
, pwd
->pw_dir
);
451 (void)strcat(pbuf
, "/.rhosts");
454 * Change effective uid while opening .rhosts. If root and
455 * reading an NFS mounted file system, can't read files that
456 * are protected read/write owner only.
459 (void)seteuid(pwd
->pw_uid
);
460 hostf
= fopen(pbuf
, "r");
466 * If not a regular file, or is owned by someone other than
467 * user or root or if writeable by anyone but the owner, quit.
470 if (lstat(pbuf
, &sbuf
) < 0)
471 cp
= ".rhosts lstat failed";
472 else if (!S_ISREG(sbuf
.st_mode
))
473 cp
= ".rhosts not regular file";
474 else if (fstat(fileno(hostf
), &sbuf
) < 0)
475 cp
= ".rhosts fstat failed";
476 else if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
)
477 cp
= "bad .rhosts owner";
478 else if (sbuf
.st_mode
& (S_IWGRP
|S_IWOTH
))
479 cp
= ".rhosts writeable by other than owner";
480 /* If there were any problems, quit. */
492 ruserok(rhost
, superuser
, ruser
, luser
)
493 const char *rhost
, *ruser
, *luser
;
496 struct addrinfo hints
, *res
, *r
;
499 memset(&hints
, 0, sizeof(hints
));
500 hints
.ai_family
= PF_UNSPEC
;
501 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
502 error
= getaddrinfo(rhost
, "0", &hints
, &res
);
506 for (r
= res
; r
; r
= r
->ai_next
) {
507 if (iruserok_sa(r
->ai_addr
, r
->ai_addrlen
, superuser
, ruser
,
518 * New .rhosts strategy: We are passed an ip address. We spin through
519 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
520 * has ip addresses, we don't have to trust a nameserver. When it
521 * contains hostnames, we spin through the list of addresses the nameserver
522 * gives us and look for a match.
524 * Returns 0 if ok, -1 if not ok.
527 iruserok(raddr
, superuser
, ruser
, luser
)
530 const char *ruser
, *luser
;
532 struct sockaddr_in sin
;
534 memset(&sin
, 0, sizeof(sin
));
535 sin
.sin_family
= AF_INET
;
536 sin
.sin_len
= sizeof(struct sockaddr_in
);
537 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
538 return iruserok_sa((struct sockaddr
*)&sin
, sin
.sin_len
, superuser
,
544 * Don't make static, used by lpd(8).
546 * Returns 0 if ok, -1 if not ok.
549 __ivaliduser(hostf
, raddr
, luser
, ruser
)
552 const char *luser
, *ruser
;
554 struct sockaddr_in sin
;
556 memset(&sin
, 0, sizeof(sin
));
557 sin
.sin_family
= AF_INET
;
558 sin
.sin_len
= sizeof(struct sockaddr_in
);
559 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
560 return __ivaliduser_sa(hostf
, (struct sockaddr
*)&sin
, sin
.sin_len
,
565 * Returns 0 if ok, -1 if not ok.
570 __ivaliduser_af(hostf
, raddr
, luser
, ruser
, af
, len
)
573 const char *luser
, *ruser
;
576 struct sockaddr
*sa
= NULL
;
577 struct sockaddr_in
*sin
= NULL
;
579 struct sockaddr_in6
*sin6
= NULL
;
581 struct sockaddr_storage ss
;
583 memset(&ss
, 0, sizeof(ss
));
586 if (len
!= sizeof(sin
->sin_addr
))
588 sin
= (struct sockaddr_in
*)&ss
;
589 sin
->sin_family
= AF_INET
;
590 sin
->sin_len
= sizeof(struct sockaddr_in
);
591 memcpy(&sin
->sin_addr
, raddr
, sizeof(sin
->sin_addr
));
595 if (len
!= sizeof(sin6
->sin6_addr
))
597 /* you will lose scope info */
598 sin6
= (struct sockaddr_in6
*)&ss
;
599 sin6
->sin6_family
= AF_INET6
;
600 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
601 memcpy(&sin6
->sin6_addr
, raddr
, sizeof(sin6
->sin6_addr
));
608 sa
= (struct sockaddr
*)&ss
;
609 return __ivaliduser_sa(hostf
, sa
, sa
->sa_len
, luser
, ruser
);
613 * Returns 0 if ok, -1 if not ok.
616 __ivaliduser_sa(hostf
, raddr
, salen
, luser
, ruser
)
618 const struct sockaddr
*raddr
;
620 const char *luser
, *ruser
;
622 register char *user
, *p
;
624 char buf
[MAXHOSTNAMELEN
+ 128]; /* host + login */
625 char hname
[MAXHOSTNAMELEN
];
626 /* Presumed guilty until proven innocent. */
627 int userok
= 0, hostok
= 0;
631 if (yp_get_default_domain(&ypdomain
))
634 #define ypdomain NULL
637 * We try to get the hostname back for netgroup matching.
638 * However, the .rosts file may contain IP addresses as well
639 * as names, so a name is not always required.
641 getnameinfo(raddr
, salen
, hname
, sizeof(hname
), NULL
, 0, NI_NAMEREQD
);
643 while (fgets(buf
, sizeof(buf
), hostf
)) {
645 /* Skip lines that are too long. */
646 if (strchr(p
, '\n') == NULL
) {
647 while ((ch
= getc(hostf
)) != '\n' && ch
!= EOF
);
650 if (*p
== '\n' || *p
== '#') {
654 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
655 *p
= isupper((unsigned char)*p
) ? tolower((unsigned char)*p
) : *p
;
658 if (*p
== ' ' || *p
== '\t') {
660 while (*p
== ' ' || *p
== '\t')
663 while (*p
!= '\n' && *p
!= ' ' &&
664 *p
!= '\t' && *p
!= '\0')
670 * Do +/- and +@/-@ checking. This looks really nasty,
671 * but it matches SunOS's behavior so far as I can tell.
675 if (!buf
[1]) { /* '+' matches all hosts */
679 if (buf
[1] == '@') /* match a host by netgroup */
680 hostok
= innetgr((char *)&buf
[2],
681 (char *)&hname
, NULL
, ypdomain
);
682 else /* match a host by addr */
683 hostok
= __icheckhost(raddr
, salen
,
686 case '-': /* reject '-' hosts and all their users */
688 if (innetgr((char *)&buf
[2],
689 (char *)&hname
, NULL
, ypdomain
))
692 if (__icheckhost(raddr
, salen
,
697 default: /* if no '+' or '-', do a simple match */
698 hostok
= __icheckhost(raddr
, salen
, buf
);
703 if (!*(user
+1)) { /* '+' matches all users */
707 if (*(user
+1) == '@') /* match a user by netgroup */
708 userok
= innetgr(user
+2, NULL
, ruser
, ypdomain
);
709 else /* match a user by direct specification */
710 userok
= !(strcmp(ruser
, user
+1));
712 case '-': /* if we matched a hostname, */
713 if (hostok
) { /* check for user field rejections */
716 if (*(user
+1) == '@') {
717 if (innetgr(user
+2, NULL
,
721 if (!strcmp(ruser
, user
+1))
726 default: /* no rejections: try to match the user */
728 userok
= !(strcmp(ruser
,*user
? user
: luser
));
731 if (hostok
&& userok
)
738 * Returns "true" if match, 0 if no match.
740 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
744 __icheckhost(raddr
, salen
, lhost
)
745 const struct sockaddr
*raddr
;
749 struct sockaddr_in sin
;
750 struct sockaddr_in6
*sin6
;
751 struct addrinfo hints
, *res
, *r
;
753 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
755 if (raddr
->sa_family
== AF_INET6
) {
756 sin6
= (struct sockaddr_in6
*)raddr
;
757 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
)) {
758 memset(&sin
, 0, sizeof(sin
));
759 sin
.sin_family
= AF_INET
;
760 sin
.sin_len
= sizeof(struct sockaddr_in
);
761 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
762 sizeof(sin
.sin_addr
));
763 raddr
= (struct sockaddr
*)&sin
;
769 if (getnameinfo(raddr
, salen
, h1
, sizeof(h1
), NULL
, 0,
770 NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
773 /* Resolve laddr into sockaddr */
774 memset(&hints
, 0, sizeof(hints
));
775 hints
.ai_family
= raddr
->sa_family
;
776 hints
.ai_socktype
= SOCK_DGRAM
; /*XXX dummy*/
778 error
= getaddrinfo(lhost
, "0", &hints
, &res
);
782 for (r
= res
; r
; r
= r
->ai_next
) {
784 if (getnameinfo(r
->ai_addr
, r
->ai_addrlen
, h2
, sizeof(h2
),
785 NULL
, 0, NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
787 if (strcmp(h1
, h2
) == 0) {