]>
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 * 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 <arpa/inet.h>
83 #include <rpcsvc/yp_prot.h>
84 #include <rpcsvc/ypclnt.h>
86 #include <arpa/nameser8_compat.h>
88 /* wrapper for KAME-special getnameinfo() */
89 #ifndef NI_WITHSCOPEID
90 #define NI_WITHSCOPEID 0
97 extern int innetgr
__P(( const char *, const char *, const char *, const char * ));
98 extern int bindresvport_sa(int, struct sockaddr
*);
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
,
111 rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, af
)
114 const char *locuser
, *remuser
, *cmd
;
118 struct addrinfo hints
, *res
, *ai
;
119 struct sockaddr_storage from
;
123 int s
, aport
, lport
, timo
, error
;
126 char num
[8], paddr
[NI_MAXHOST
];
127 static char canonnamebuf
[MAXDNAME
]; /* is it proper here? */
131 memset(&hints
, 0, sizeof(hints
));
132 hints
.ai_flags
= AI_CANONNAME
;
133 hints
.ai_family
= af
;
134 hints
.ai_socktype
= SOCK_STREAM
;
135 hints
.ai_protocol
= 0;
136 (void)snprintf(num
, sizeof(num
), "%d", ntohs(rport
));
137 error
= getaddrinfo(*ahost
, num
, &hints
, &res
);
139 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
140 gai_strerror(error
));
141 if (error
== EAI_SYSTEM
)
142 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
147 if (res
->ai_canonname
148 && strlen(res
->ai_canonname
) + 1 < sizeof(canonnamebuf
)) {
149 strncpy(canonnamebuf
, res
->ai_canonname
, sizeof(canonnamebuf
));
150 *ahost
= canonnamebuf
;
153 for (ai
= res
; ai
; ai
= ai
->ai_next
)
157 oldmask
= sigblock(sigmask(SIGURG
));
158 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
159 s
= rresvport_af(&lport
, ai
->ai_family
);
161 if (errno
!= EAGAIN
&& ai
->ai_next
) {
166 (void)fprintf(stderr
,
167 "rcmd: socket: All ports in use\n");
169 (void)fprintf(stderr
, "rcmd: socket: %s\n",
175 fcntl(s
, F_SETOWN
, pid
);
176 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) >= 0)
179 if (errno
== EADDRINUSE
) {
183 if (errno
== ECONNREFUSED
)
185 if (ai
->ai_next
== NULL
&& (!refused
|| timo
> 16)) {
186 (void)fprintf(stderr
, "%s: %s\n",
187 *ahost
, strerror(errno
));
195 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
196 paddr
, sizeof(paddr
),
198 NI_NUMERICHOST
|NI_WITHSCOPEID
);
199 (void)fprintf(stderr
, "connect to address %s: ",
204 if ((ai
= ai
->ai_next
) == NULL
) {
205 /* refused && timo <= 16 */
206 struct timespec time_to_sleep
, time_remaining
;
208 time_to_sleep
.tv_sec
= timo
;
209 time_to_sleep
.tv_nsec
= 0;
210 (void)nanosleep(&time_to_sleep
, &time_remaining
);
216 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
217 paddr
, sizeof(paddr
),
219 NI_NUMERICHOST
|NI_WITHSCOPEID
);
220 fprintf(stderr
, "Trying %s...\n", paddr
);
225 * try to rresvport() to the same port. This will make rresvport()
226 * fail it's first bind, resulting in it choosing a random port.
235 int s2
= rresvport_af(&lport
, ai
->ai_family
), s3
;
236 unsigned int len
= ai
->ai_addrlen
;
242 (void)snprintf(num
, sizeof(num
), "%d", lport
);
243 if (write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
244 (void)fprintf(stderr
,
245 "rcmd: write (setting up stderr): %s\n",
251 if(nfds
> FD_SETSIZE
) {
252 fprintf(stderr
, "rcmd: too many files\n");
261 if (select(nfds
, &reads
, 0, 0, 0) < 1 || !FD_ISSET(s2
, &reads
)){
263 (void)fprintf(stderr
,
264 "rcmd: select (setting up stderr): %s\n",
267 (void)fprintf(stderr
,
268 "select: protocol failure in circuit setup\n");
272 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
273 switch (from
.ss_family
) {
275 aport
= ntohs(((struct sockaddr_in
*)&from
)->sin_port
);
279 aport
= ntohs(((struct sockaddr_in6
*)&from
)->sin6_port
);
283 aport
= 0; /* error */
287 * XXX careful for ftp bounce attacks. If discovered, shut them
288 * down and check for the real auxiliary channel to connect.
296 (void)fprintf(stderr
,
297 "rcmd: accept: %s\n", strerror(errno
));
302 if (aport
>= IPPORT_RESERVED
|| aport
< IPPORT_RESERVED
/ 2) {
303 (void)fprintf(stderr
,
304 "socket: protocol failure in circuit setup.\n");
308 (void)write(s
, locuser
, strlen(locuser
)+1);
309 (void)write(s
, remuser
, strlen(remuser
)+1);
310 (void)write(s
, cmd
, strlen(cmd
)+1);
311 if (read(s
, &c
, 1) != 1) {
312 (void)fprintf(stderr
,
313 "rcmd: %s: %s\n", *ahost
, strerror(errno
));
317 while (read(s
, &c
, 1) == 1) {
318 (void)write(STDERR_FILENO
, &c
, 1);
338 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
341 const char *locuser
, *remuser
, *cmd
;
344 return rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, AF_INET
);
351 return rresvport_af(port
, AF_INET
);
355 rresvport_af(alport
, family
)
359 struct sockaddr_storage ss
;
362 memset(&ss
, 0, sizeof(ss
));
363 ss
.ss_family
= family
;
366 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in
);
367 sport
= &((struct sockaddr_in
*)&ss
)->sin_port
;
368 ((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
= INADDR_ANY
;
372 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in6
);
373 sport
= &((struct sockaddr_in6
*)&ss
)->sin6_port
;
374 ((struct sockaddr_in6
*)&ss
)->sin6_addr
= in6addr_any
;
378 errno
= EAFNOSUPPORT
;
382 s
= socket(ss
.ss_family
, SOCK_STREAM
, 0);
385 #if 0 /* compat_exact_traditional_rresvport_semantics */
386 sin
.sin_port
= htons((u_short
)*alport
);
387 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
389 if (errno
!= EADDRINUSE
) {
395 if (bindresvport_sa(s
, (struct sockaddr
*)&ss
) == -1) {
399 *alport
= (int)ntohs(*sport
);
403 int __check_rhosts_file
= 1;
406 /* Guess at the size of a password buffer for getpwnam_r (see lookup.subproj/lu_group.c) */
407 #define MAXPWBUF (MAXLOGNAME + 1 + _PASSWORD_LEN + 1 + MAXPATHLEN + 1 + MAXPATHLEN + 1 + 4098)
410 * AF independent extension of iruserok.
412 * Returns 0 if ok, -1 if not ok.
415 iruserok_sa(ra
, rlen
, superuser
, ruser
, luser
)
419 const char *ruser
, *luser
;
423 struct passwd p
, *pwd
;
427 char pbuf
[MAXPATHLEN
];
428 const struct sockaddr
*raddr
;
429 struct sockaddr_storage ss
;
430 char pwbuf
[MAXPWBUF
];
432 /* avoid alignment issue */
433 if (rlen
> sizeof(ss
))
435 memcpy(&ss
, ra
, rlen
);
436 raddr
= (struct sockaddr
*)&ss
;
439 hostf
= superuser
? NULL
: fopen(_PATH_HEQUIV
, "r");
442 if (__ivaliduser_sa(hostf
, raddr
, rlen
, luser
, ruser
) == 0) {
448 if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
451 memset(&p
, 0, sizeof(struct passwd
));
452 memset(pwbuf
, 0, sizeof(pwbuf
));
455 status
= getpwnam_r(luser
, &p
, pwbuf
, MAXPWBUF
, &pwd
);
456 if (status
!= 0) return -1;
458 (void)strcpy(pbuf
, pwd
->pw_dir
);
459 (void)strcat(pbuf
, "/.rhosts");
462 * Change effective uid while opening .rhosts. If root and
463 * reading an NFS mounted file system, can't read files that
464 * are protected read/write owner only.
467 (void)seteuid(pwd
->pw_uid
);
468 hostf
= fopen(pbuf
, "r");
474 * If not a regular file, or is owned by someone other than
475 * user or root or if writeable by anyone but the owner, quit.
478 if (lstat(pbuf
, &sbuf
) < 0)
479 cp
= ".rhosts lstat failed";
480 else if (!S_ISREG(sbuf
.st_mode
))
481 cp
= ".rhosts not regular file";
482 else if (fstat(fileno(hostf
), &sbuf
) < 0)
483 cp
= ".rhosts fstat failed";
484 else if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
)
485 cp
= "bad .rhosts owner";
486 else if (sbuf
.st_mode
& (S_IWGRP
|S_IWOTH
))
487 cp
= ".rhosts writeable by other than owner";
488 /* If there were any problems, quit. */
500 ruserok(rhost
, superuser
, ruser
, luser
)
501 const char *rhost
, *ruser
, *luser
;
504 struct addrinfo hints
, *res
, *r
;
507 memset(&hints
, 0, sizeof(hints
));
508 hints
.ai_family
= PF_UNSPEC
;
509 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
510 error
= getaddrinfo(rhost
, "0", &hints
, &res
);
514 for (r
= res
; r
; r
= r
->ai_next
) {
515 if (iruserok_sa(r
->ai_addr
, r
->ai_addrlen
, superuser
, ruser
,
526 * New .rhosts strategy: We are passed an ip address. We spin through
527 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
528 * has ip addresses, we don't have to trust a nameserver. When it
529 * contains hostnames, we spin through the list of addresses the nameserver
530 * gives us and look for a match.
532 * Returns 0 if ok, -1 if not ok.
535 iruserok(raddr
, superuser
, ruser
, luser
)
538 const char *ruser
, *luser
;
540 struct sockaddr_in sin
;
542 memset(&sin
, 0, sizeof(sin
));
543 sin
.sin_family
= AF_INET
;
544 sin
.sin_len
= sizeof(struct sockaddr_in
);
545 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
546 return iruserok_sa((struct sockaddr
*)&sin
, sin
.sin_len
, superuser
,
552 * Don't make static, used by lpd(8).
554 * Returns 0 if ok, -1 if not ok.
557 __ivaliduser(hostf
, raddr
, luser
, ruser
)
560 const char *luser
, *ruser
;
562 struct sockaddr_in sin
;
564 memset(&sin
, 0, sizeof(sin
));
565 sin
.sin_family
= AF_INET
;
566 sin
.sin_len
= sizeof(struct sockaddr_in
);
567 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
568 return __ivaliduser_sa(hostf
, (struct sockaddr
*)&sin
, sin
.sin_len
,
573 * Returns 0 if ok, -1 if not ok.
578 __ivaliduser_af(hostf
, raddr
, luser
, ruser
, af
, len
)
581 const char *luser
, *ruser
;
584 struct sockaddr
*sa
= NULL
;
585 struct sockaddr_in
*sin
= NULL
;
587 struct sockaddr_in6
*sin6
= NULL
;
589 struct sockaddr_storage ss
;
591 memset(&ss
, 0, sizeof(ss
));
594 if (len
!= sizeof(sin
->sin_addr
))
596 sin
= (struct sockaddr_in
*)&ss
;
597 sin
->sin_family
= AF_INET
;
598 sin
->sin_len
= sizeof(struct sockaddr_in
);
599 memcpy(&sin
->sin_addr
, raddr
, sizeof(sin
->sin_addr
));
603 if (len
!= sizeof(sin6
->sin6_addr
))
605 /* you will lose scope info */
606 sin6
= (struct sockaddr_in6
*)&ss
;
607 sin6
->sin6_family
= AF_INET6
;
608 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
609 memcpy(&sin6
->sin6_addr
, raddr
, sizeof(sin6
->sin6_addr
));
616 sa
= (struct sockaddr
*)&ss
;
617 return __ivaliduser_sa(hostf
, sa
, sa
->sa_len
, luser
, ruser
);
621 * Returns 0 if ok, -1 if not ok.
624 __ivaliduser_sa(hostf
, raddr
, salen
, luser
, ruser
)
626 const struct sockaddr
*raddr
;
628 const char *luser
, *ruser
;
630 register char *user
, *p
;
632 char buf
[MAXHOSTNAMELEN
+ 128]; /* host + login */
633 char hname
[MAXHOSTNAMELEN
];
634 /* Presumed guilty until proven innocent. */
635 int userok
= 0, hostok
= 0;
639 if (yp_get_default_domain(&ypdomain
))
642 #define ypdomain NULL
645 * We try to get the hostname back for netgroup matching.
646 * However, the .rosts file may contain IP addresses as well
647 * as names, so a name is not always required.
649 getnameinfo(raddr
, salen
, hname
, sizeof(hname
), NULL
, 0, NI_NAMEREQD
);
651 while (fgets(buf
, sizeof(buf
), hostf
)) {
653 /* Skip lines that are too long. */
654 if (strchr(p
, '\n') == NULL
) {
655 while ((ch
= getc(hostf
)) != '\n' && ch
!= EOF
);
658 if (*p
== '\n' || *p
== '#') {
662 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
663 *p
= isupper((unsigned char)*p
) ? tolower((unsigned char)*p
) : *p
;
666 if (*p
== ' ' || *p
== '\t') {
668 while (*p
== ' ' || *p
== '\t')
671 while (*p
!= '\n' && *p
!= ' ' &&
672 *p
!= '\t' && *p
!= '\0')
678 * Do +/- and +@/-@ checking. This looks really nasty,
679 * but it matches SunOS's behavior so far as I can tell.
683 if (!buf
[1]) { /* '+' matches all hosts */
687 if (buf
[1] == '@') /* match a host by netgroup */
688 hostok
= innetgr((char *)&buf
[2],
689 (char *)&hname
, NULL
, ypdomain
);
690 else /* match a host by addr */
691 hostok
= __icheckhost(raddr
, salen
,
694 case '-': /* reject '-' hosts and all their users */
696 if (innetgr((char *)&buf
[2],
697 (char *)&hname
, NULL
, ypdomain
))
700 if (__icheckhost(raddr
, salen
,
705 default: /* if no '+' or '-', do a simple match */
706 hostok
= __icheckhost(raddr
, salen
, buf
);
711 if (!*(user
+1)) { /* '+' matches all users */
715 if (*(user
+1) == '@') /* match a user by netgroup */
716 userok
= innetgr(user
+2, NULL
, ruser
, ypdomain
);
717 else /* match a user by direct specification */
718 userok
= !(strcmp(ruser
, user
+1));
720 case '-': /* if we matched a hostname, */
721 if (hostok
) { /* check for user field rejections */
724 if (*(user
+1) == '@') {
725 if (innetgr(user
+2, NULL
,
729 if (!strcmp(ruser
, user
+1))
734 default: /* no rejections: try to match the user */
736 userok
= !(strcmp(ruser
,*user
? user
: luser
));
739 if (hostok
&& userok
)
746 * Returns "true" if match, 0 if no match.
748 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
752 __icheckhost(raddr
, salen
, lhost
)
753 const struct sockaddr
*raddr
;
757 struct sockaddr_in sin
;
758 struct sockaddr_in6
*sin6
;
759 struct addrinfo hints
, *res
, *r
;
761 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
763 if (raddr
->sa_family
== AF_INET6
) {
764 sin6
= (struct sockaddr_in6
*)raddr
;
765 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
)) {
766 memset(&sin
, 0, sizeof(sin
));
767 sin
.sin_family
= AF_INET
;
768 sin
.sin_len
= sizeof(struct sockaddr_in
);
769 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
770 sizeof(sin
.sin_addr
));
771 raddr
= (struct sockaddr
*)&sin
;
777 if (getnameinfo(raddr
, salen
, h1
, sizeof(h1
), NULL
, 0,
778 NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
781 /* Resolve laddr into sockaddr */
782 memset(&hints
, 0, sizeof(hints
));
783 hints
.ai_family
= raddr
->sa_family
;
784 hints
.ai_socktype
= SOCK_DGRAM
; /*XXX dummy*/
786 error
= getaddrinfo(lhost
, "0", &hints
, &res
);
790 for (r
= res
; r
; r
= r
->ai_next
) {
792 if (getnameinfo(r
->ai_addr
, r
->ai_addrlen
, h2
, sizeof(h2
),
793 NULL
, 0, NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
795 if (strcmp(h1
, h2
) == 0) {