]>
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 <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/nameser8_compat.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 * ));
99 extern int bindresvport_sa(int, struct sockaddr
*);
101 #define max(a, b) ((a > b) ? a : b)
103 int __ivaliduser
__P((FILE *, u_int32_t
, const char *, const char *));
104 int __ivaliduser_af
__P((FILE *,const void *, const char *, const char *,
106 int __ivaliduser_sa
__P((FILE *, const struct sockaddr
*, socklen_t
,
107 const char *,const char *));
108 static int __icheckhost
__P((const struct sockaddr
*, socklen_t
,
112 rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, af
)
115 const char *locuser
, *remuser
, *cmd
;
119 struct addrinfo hints
, *res
, *ai
;
120 struct sockaddr_storage from
;
124 int s
, aport
, lport
, timo
, error
;
127 char num
[8], paddr
[NI_MAXHOST
];
128 static char canonnamebuf
[MAXDNAME
]; /* is it proper here? */
132 memset(&hints
, 0, sizeof(hints
));
133 hints
.ai_flags
= AI_CANONNAME
;
134 hints
.ai_family
= af
;
135 hints
.ai_socktype
= SOCK_STREAM
;
136 hints
.ai_protocol
= 0;
137 (void)snprintf(num
, sizeof(num
), "%d", ntohs(rport
));
138 error
= getaddrinfo(*ahost
, num
, &hints
, &res
);
140 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
141 gai_strerror(error
));
142 if (error
== EAI_SYSTEM
)
143 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
148 if (res
->ai_canonname
149 && strlen(res
->ai_canonname
) + 1 < sizeof(canonnamebuf
)) {
150 strncpy(canonnamebuf
, res
->ai_canonname
, sizeof(canonnamebuf
));
151 *ahost
= canonnamebuf
;
154 for (ai
= res
; ai
; ai
= ai
->ai_next
)
158 oldmask
= sigblock(sigmask(SIGURG
));
159 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
160 s
= rresvport_af(&lport
, ai
->ai_family
);
162 if (errno
!= EAGAIN
&& ai
->ai_next
) {
167 (void)fprintf(stderr
,
168 "rcmd: socket: All ports in use\n");
170 (void)fprintf(stderr
, "rcmd: socket: %s\n",
176 fcntl(s
, F_SETOWN
, pid
);
177 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) >= 0)
180 if (errno
== EADDRINUSE
) {
184 if (errno
== ECONNREFUSED
)
186 if (ai
->ai_next
== NULL
&& (!refused
|| timo
> 16)) {
187 (void)fprintf(stderr
, "%s: %s\n",
188 *ahost
, strerror(errno
));
196 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
197 paddr
, sizeof(paddr
),
199 NI_NUMERICHOST
|NI_WITHSCOPEID
);
200 (void)fprintf(stderr
, "connect to address %s: ",
205 if ((ai
= ai
->ai_next
) == NULL
) {
206 /* refused && timo <= 16 */
207 struct timespec time_to_sleep
, time_remaining
;
209 time_to_sleep
.tv_sec
= timo
;
210 time_to_sleep
.tv_nsec
= 0;
211 (void)nanosleep(&time_to_sleep
, &time_remaining
);
217 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
218 paddr
, sizeof(paddr
),
220 NI_NUMERICHOST
|NI_WITHSCOPEID
);
221 fprintf(stderr
, "Trying %s...\n", paddr
);
226 * try to rresvport() to the same port. This will make rresvport()
227 * fail it's first bind, resulting in it choosing a random port.
236 int s2
= rresvport_af(&lport
, ai
->ai_family
), s3
;
237 int len
= ai
->ai_addrlen
;
243 (void)snprintf(num
, sizeof(num
), "%d", lport
);
244 if (write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
245 (void)fprintf(stderr
,
246 "rcmd: write (setting up stderr): %s\n",
252 if(nfds
> FD_SETSIZE
) {
253 fprintf(stderr
, "rcmd: too many files\n");
262 if (select(nfds
, &reads
, 0, 0, 0) < 1 || !FD_ISSET(s2
, &reads
)){
264 (void)fprintf(stderr
,
265 "rcmd: select (setting up stderr): %s\n",
268 (void)fprintf(stderr
,
269 "select: protocol failure in circuit setup\n");
273 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
274 switch (from
.ss_family
) {
276 aport
= ntohs(((struct sockaddr_in
*)&from
)->sin_port
);
280 aport
= ntohs(((struct sockaddr_in6
*)&from
)->sin6_port
);
284 aport
= 0; /* error */
288 * XXX careful for ftp bounce attacks. If discovered, shut them
289 * down and check for the real auxiliary channel to connect.
297 (void)fprintf(stderr
,
298 "rcmd: accept: %s\n", strerror(errno
));
303 if (aport
>= IPPORT_RESERVED
|| aport
< IPPORT_RESERVED
/ 2) {
304 (void)fprintf(stderr
,
305 "socket: protocol failure in circuit setup.\n");
309 (void)write(s
, locuser
, strlen(locuser
)+1);
310 (void)write(s
, remuser
, strlen(remuser
)+1);
311 (void)write(s
, cmd
, strlen(cmd
)+1);
312 if (read(s
, &c
, 1) != 1) {
313 (void)fprintf(stderr
,
314 "rcmd: %s: %s\n", *ahost
, strerror(errno
));
318 while (read(s
, &c
, 1) == 1) {
319 (void)write(STDERR_FILENO
, &c
, 1);
339 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
342 const char *locuser
, *remuser
, *cmd
;
345 return rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, AF_INET
);
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 * AF independent extension of iruserok.
410 * Returns 0 if ok, -1 if not ok.
413 iruserok_sa(ra
, rlen
, superuser
, ruser
, luser
)
417 const char *ruser
, *luser
;
425 char pbuf
[MAXPATHLEN
];
426 const struct sockaddr
*raddr
;
427 struct sockaddr_storage ss
;
429 /* avoid alignment issue */
430 if (rlen
> sizeof(ss
))
432 memcpy(&ss
, ra
, rlen
);
433 raddr
= (struct sockaddr
*)&ss
;
436 hostf
= superuser
? NULL
: fopen(_PATH_HEQUIV
, "r");
439 if (__ivaliduser_sa(hostf
, raddr
, rlen
, luser
, ruser
) == 0) {
445 if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
447 if ((pwd
= getpwnam(luser
)) == NULL
)
449 (void)strcpy(pbuf
, pwd
->pw_dir
);
450 (void)strcat(pbuf
, "/.rhosts");
453 * Change effective uid while opening .rhosts. If root and
454 * reading an NFS mounted file system, can't read files that
455 * are protected read/write owner only.
458 (void)seteuid(pwd
->pw_uid
);
459 hostf
= fopen(pbuf
, "r");
465 * If not a regular file, or is owned by someone other than
466 * user or root or if writeable by anyone but the owner, quit.
469 if (lstat(pbuf
, &sbuf
) < 0)
470 cp
= ".rhosts lstat failed";
471 else if (!S_ISREG(sbuf
.st_mode
))
472 cp
= ".rhosts not regular file";
473 else if (fstat(fileno(hostf
), &sbuf
) < 0)
474 cp
= ".rhosts fstat failed";
475 else if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
)
476 cp
= "bad .rhosts owner";
477 else if (sbuf
.st_mode
& (S_IWGRP
|S_IWOTH
))
478 cp
= ".rhosts writeable by other than owner";
479 /* If there were any problems, quit. */
491 ruserok(rhost
, superuser
, ruser
, luser
)
492 const char *rhost
, *ruser
, *luser
;
495 struct addrinfo hints
, *res
, *r
;
498 memset(&hints
, 0, sizeof(hints
));
499 hints
.ai_family
= PF_UNSPEC
;
500 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
501 error
= getaddrinfo(rhost
, "0", &hints
, &res
);
505 for (r
= res
; r
; r
= r
->ai_next
) {
506 if (iruserok_sa(r
->ai_addr
, r
->ai_addrlen
, superuser
, ruser
,
517 * New .rhosts strategy: We are passed an ip address. We spin through
518 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
519 * has ip addresses, we don't have to trust a nameserver. When it
520 * contains hostnames, we spin through the list of addresses the nameserver
521 * gives us and look for a match.
523 * Returns 0 if ok, -1 if not ok.
526 iruserok(raddr
, superuser
, ruser
, luser
)
529 const char *ruser
, *luser
;
531 struct sockaddr_in sin
;
533 memset(&sin
, 0, sizeof(sin
));
534 sin
.sin_family
= AF_INET
;
535 sin
.sin_len
= sizeof(struct sockaddr_in
);
536 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
537 return iruserok_sa((struct sockaddr
*)&sin
, sin
.sin_len
, superuser
,
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
636 * We try to get the hostname back for netgroup matching.
637 * However, the .rosts file may contain IP addresses as well
638 * as names, so a name is not always required.
640 getnameinfo(raddr
, salen
, hname
, sizeof(hname
), NULL
, 0, NI_NAMEREQD
);
642 while (fgets(buf
, sizeof(buf
), hostf
)) {
644 /* Skip lines that are too long. */
645 if (strchr(p
, '\n') == NULL
) {
646 while ((ch
= getc(hostf
)) != '\n' && ch
!= EOF
);
649 if (*p
== '\n' || *p
== '#') {
653 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
654 *p
= isupper((unsigned char)*p
) ? tolower((unsigned char)*p
) : *p
;
657 if (*p
== ' ' || *p
== '\t') {
659 while (*p
== ' ' || *p
== '\t')
662 while (*p
!= '\n' && *p
!= ' ' &&
663 *p
!= '\t' && *p
!= '\0')
669 * Do +/- and +@/-@ checking. This looks really nasty,
670 * but it matches SunOS's behavior so far as I can tell.
674 if (!buf
[1]) { /* '+' matches all hosts */
678 if (buf
[1] == '@') /* match a host by netgroup */
679 hostok
= innetgr((char *)&buf
[2],
680 (char *)&hname
, NULL
, ypdomain
);
681 else /* match a host by addr */
682 hostok
= __icheckhost(raddr
, salen
,
685 case '-': /* reject '-' hosts and all their users */
687 if (innetgr((char *)&buf
[2],
688 (char *)&hname
, NULL
, ypdomain
))
691 if (__icheckhost(raddr
, salen
,
696 default: /* if no '+' or '-', do a simple match */
697 hostok
= __icheckhost(raddr
, salen
, buf
);
702 if (!*(user
+1)) { /* '+' matches all users */
706 if (*(user
+1) == '@') /* match a user by netgroup */
707 userok
= innetgr(user
+2, NULL
, ruser
, ypdomain
);
708 else /* match a user by direct specification */
709 userok
= !(strcmp(ruser
, user
+1));
711 case '-': /* if we matched a hostname, */
712 if (hostok
) { /* check for user field rejections */
715 if (*(user
+1) == '@') {
716 if (innetgr(user
+2, NULL
,
720 if (!strcmp(ruser
, user
+1))
725 default: /* no rejections: try to match the user */
727 userok
= !(strcmp(ruser
,*user
? user
: luser
));
730 if (hostok
&& userok
)
737 * Returns "true" if match, 0 if no match.
739 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
743 __icheckhost(raddr
, salen
, lhost
)
744 const struct sockaddr
*raddr
;
748 struct sockaddr_in sin
;
749 struct sockaddr_in6
*sin6
;
750 struct addrinfo hints
, *res
, *r
;
752 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
754 if (raddr
->sa_family
== AF_INET6
) {
755 sin6
= (struct sockaddr_in6
*)raddr
;
756 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
)) {
757 memset(&sin
, 0, sizeof(sin
));
758 sin
.sin_family
= AF_INET
;
759 sin
.sin_len
= sizeof(struct sockaddr_in
);
760 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
761 sizeof(sin
.sin_addr
));
762 raddr
= (struct sockaddr
*)&sin
;
768 if (getnameinfo(raddr
, salen
, h1
, sizeof(h1
), NULL
, 0,
769 NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
772 /* Resolve laddr into sockaddr */
773 memset(&hints
, 0, sizeof(hints
));
774 hints
.ai_family
= raddr
->sa_family
;
775 hints
.ai_socktype
= SOCK_DGRAM
; /*XXX dummy*/
777 error
= getaddrinfo(lhost
, "0", &hints
, &res
);
781 for (r
= res
; r
; r
= r
->ai_next
) {
783 if (getnameinfo(r
->ai_addr
, r
->ai_addrlen
, h2
, sizeof(h2
),
784 NULL
, 0, NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
786 if (strcmp(h1
, h2
) == 0) {