2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/lib/libc/net/rcmd.c,v 1.23.2.7 2002/08/26 16:17:49 jdp Exp $
30 * $DragonFly: src/lib/libc/net/rcmd.c,v 1.7 2005/11/13 02:04:47 swildner Exp $
32 * @(#)rcmd.c 8.3 (Berkeley) 3/26/94
35 #include "namespace.h"
36 #include <sys/param.h>
37 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
55 #include <rpcsvc/yp_prot.h>
56 #include <rpcsvc/ypclnt.h>
58 #include <arpa/nameser.h>
59 #include "un-namespace.h"
61 /* wrapper for KAME-special getnameinfo() */
62 #ifndef NI_WITHSCOPEID
63 #define NI_WITHSCOPEID 0
66 extern int innetgr ( const char *, const char *, const char *, const char * );
68 #define max(a, b) ((a > b) ? a : b)
70 int __ivaliduser (FILE *, u_int32_t
, const char *, const char *);
71 int __ivaliduser_af (FILE *,const void *, const char *, const char *,
73 int __ivaliduser_sa (FILE *, const struct sockaddr
*, socklen_t
,
74 const char *,const char *);
75 static int __icheckhost (const struct sockaddr
*, socklen_t
,
78 char paddr
[NI_MAXHOST
];
81 rcmd(char **ahost
, int rport
, const char *locuser
, const char *remuser
,
82 const char *cmd
, int *fd2p
)
84 return rcmd_af(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, AF_INET
);
88 rcmd_af(char **ahost
, int rport
, const char *locuser
, const char *remuser
,
89 const char *cmd
, int *fd2p
, int af
)
91 struct addrinfo hints
, *res
, *ai
;
92 struct sockaddr_storage from
;
94 sigset_t oldmask
, newmask
;
96 int s
, aport
, lport
, timo
, error
;
100 static char canonnamebuf
[MAXDNAME
]; /* is it proper here? */
102 /* call rcmdsh() with specified remote shell if appropriate. */
103 if (!issetugid() && (p
= getenv("RSH"))) {
104 struct servent
*sp
= getservbyname("shell", "tcp");
106 if (sp
&& sp
->s_port
== rport
)
107 return (rcmdsh(ahost
, rport
, locuser
, remuser
,
111 /* use rsh(1) if non-root and remote port is shell. */
113 struct servent
*sp
= getservbyname("shell", "tcp");
115 if (sp
&& sp
->s_port
== rport
)
116 return (rcmdsh(ahost
, rport
, locuser
, remuser
,
122 memset(&hints
, 0, sizeof(hints
));
123 hints
.ai_flags
= AI_CANONNAME
;
124 hints
.ai_family
= af
;
125 hints
.ai_socktype
= SOCK_STREAM
;
126 hints
.ai_protocol
= 0;
127 snprintf(num
, sizeof(num
), "%d", ntohs(rport
));
128 error
= getaddrinfo(*ahost
, num
, &hints
, &res
);
130 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
131 gai_strerror(error
));
132 if (error
== EAI_SYSTEM
)
133 fprintf(stderr
, "rcmd: getaddrinfo: %s\n",
138 if (res
->ai_canonname
139 && strlen(res
->ai_canonname
) + 1 < sizeof(canonnamebuf
)) {
140 strncpy(canonnamebuf
, res
->ai_canonname
, sizeof(canonnamebuf
));
141 *ahost
= canonnamebuf
;
144 for (ai
= res
; ai
; ai
= ai
->ai_next
)
148 sigemptyset(&newmask
);
149 sigaddset(&newmask
, SIGURG
);
150 _sigprocmask(SIG_BLOCK
, (const sigset_t
*)&newmask
, &oldmask
);
151 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
152 s
= rresvport_af(&lport
, ai
->ai_family
);
154 if (errno
!= EAGAIN
&& ai
->ai_next
) {
160 "rcmd: socket: All ports in use\n");
162 fprintf(stderr
, "rcmd: socket: %s\n",
165 _sigprocmask(SIG_SETMASK
, (const sigset_t
*)&oldmask
,
169 _fcntl(s
, F_SETOWN
, pid
);
170 if (_connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) >= 0)
173 if (errno
== EADDRINUSE
) {
177 if (errno
== ECONNREFUSED
)
179 if (ai
->ai_next
== NULL
&& (!refused
|| timo
> 16)) {
180 fprintf(stderr
, "%s: %s\n", *ahost
, strerror(errno
));
182 _sigprocmask(SIG_SETMASK
, (const sigset_t
*)&oldmask
,
189 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
190 paddr
, sizeof(paddr
),
192 NI_NUMERICHOST
|NI_WITHSCOPEID
);
193 fprintf(stderr
, "connect to address %s: ", paddr
);
197 if ((ai
= ai
->ai_next
) == NULL
) {
198 /* refused && timo <= 16 */
199 struct timespec time_to_sleep
, time_remaining
;
201 time_to_sleep
.tv_sec
= timo
;
202 time_to_sleep
.tv_nsec
= 0;
203 _nanosleep(&time_to_sleep
, &time_remaining
);
209 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
,
210 paddr
, sizeof(paddr
),
212 NI_NUMERICHOST
|NI_WITHSCOPEID
);
213 fprintf(stderr
, "Trying %s...\n", paddr
);
222 int s2
= rresvport_af(&lport
, ai
->ai_family
), s3
;
223 int len
= ai
->ai_addrlen
;
229 snprintf(num
, sizeof(num
), "%d", lport
);
230 if (_write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
232 "rcmd: write (setting up stderr): %s\n",
238 if(nfds
> FD_SETSIZE
) {
239 fprintf(stderr
, "rcmd: too many files\n");
248 if (_select(nfds
, &reads
, 0, 0, 0) < 1 || !FD_ISSET(s2
, &reads
)){
251 "rcmd: select (setting up stderr): %s\n",
255 "select: protocol failure in circuit setup\n");
259 s3
= _accept(s2
, (struct sockaddr
*)&from
, &len
);
260 switch (from
.ss_family
) {
262 aport
= ntohs(((struct sockaddr_in
*)&from
)->sin_port
);
266 aport
= ntohs(((struct sockaddr_in6
*)&from
)->sin6_port
);
270 aport
= 0; /* error */
274 * XXX careful for ftp bounce attacks. If discovered, shut them
275 * down and check for the real auxiliary channel to connect.
284 "rcmd: accept: %s\n", strerror(errno
));
289 if (aport
>= IPPORT_RESERVED
|| aport
< IPPORT_RESERVED
/ 2) {
291 "socket: protocol failure in circuit setup.\n");
295 _write(s
, locuser
, strlen(locuser
)+1);
296 _write(s
, remuser
, strlen(remuser
)+1);
297 _write(s
, cmd
, strlen(cmd
)+1);
298 if (_read(s
, &c
, 1) != 1) {
300 "rcmd: %s: %s\n", *ahost
, strerror(errno
));
304 while (_read(s
, &c
, 1) == 1) {
305 _write(STDERR_FILENO
, &c
, 1);
311 _sigprocmask(SIG_SETMASK
, (const sigset_t
*)&oldmask
, NULL
);
319 _sigprocmask(SIG_SETMASK
, (const sigset_t
*)&oldmask
, NULL
);
327 return rresvport_af(port
, AF_INET
);
331 rresvport_af(int *alport
, int family
)
334 struct sockaddr_storage ss
;
337 memset(&ss
, 0, sizeof(ss
));
338 ss
.ss_family
= family
;
341 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in
);
342 sport
= &((struct sockaddr_in
*)&ss
)->sin_port
;
343 ((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
= INADDR_ANY
;
347 ((struct sockaddr
*)&ss
)->sa_len
= sizeof(struct sockaddr_in6
);
348 sport
= &((struct sockaddr_in6
*)&ss
)->sin6_port
;
349 ((struct sockaddr_in6
*)&ss
)->sin6_addr
= in6addr_any
;
353 errno
= EAFNOSUPPORT
;
357 s
= _socket(ss
.ss_family
, SOCK_STREAM
, 0);
360 #if 0 /* compat_exact_traditional_rresvport_semantics */
361 sin
.sin_port
= htons((u_short
)*alport
);
362 if (_bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
364 if (errno
!= EADDRINUSE
) {
370 if (bindresvport_sa(s
, (struct sockaddr
*)&ss
) == -1) {
374 *alport
= (int)ntohs(*sport
);
378 int __check_rhosts_file
= 1;
382 ruserok(const char *rhost
, int superuser
, const char *ruser
, const char *luser
)
384 struct addrinfo hints
, *res
, *r
;
387 memset(&hints
, 0, sizeof(hints
));
388 hints
.ai_family
= PF_UNSPEC
;
389 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
390 error
= getaddrinfo(rhost
, "0", &hints
, &res
);
394 for (r
= res
; r
; r
= r
->ai_next
) {
395 if (iruserok_sa(r
->ai_addr
, r
->ai_addrlen
, superuser
, ruser
,
406 * New .rhosts strategy: We are passed an ip address. We spin through
407 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
408 * has ip addresses, we don't have to trust a nameserver. When it
409 * contains hostnames, we spin through the list of addresses the nameserver
410 * gives us and look for a match.
412 * Returns 0 if ok, -1 if not ok.
415 iruserok(unsigned long raddr
, int superuser
, const char *ruser
,
418 struct sockaddr_in sin
;
420 memset(&sin
, 0, sizeof(sin
));
421 sin
.sin_family
= AF_INET
;
422 sin
.sin_len
= sizeof(struct sockaddr_in
);
423 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
424 return iruserok_sa((struct sockaddr
*)&sin
, sin
.sin_len
, superuser
,
429 * AF independent extension of iruserok.
431 * Returns 0 if ok, -1 if not ok.
434 iruserok_sa(const void *ra
, int rlen
, int superuser
, const char *ruser
,
443 char pbuf
[MAXPATHLEN
];
444 const struct sockaddr
*raddr
;
445 struct sockaddr_storage ss
;
447 /* avoid alignment issue */
448 if (rlen
> sizeof(ss
))
450 memcpy(&ss
, ra
, rlen
);
451 raddr
= (struct sockaddr
*)&ss
;
454 hostf
= superuser
? NULL
: fopen(_PATH_HEQUIV
, "r");
457 if (__ivaliduser_sa(hostf
, raddr
, rlen
, luser
, ruser
) == 0) {
463 if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
465 if ((pwd
= getpwnam(luser
)) == NULL
)
467 strcpy(pbuf
, pwd
->pw_dir
);
468 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 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 * Don't make static, used by lpd(8).
512 * Returns 0 if ok, -1 if not ok.
515 __ivaliduser(FILE *hostf
, u_int32_t raddr
, const char *luser
, const char *ruser
)
517 struct sockaddr_in sin
;
519 memset(&sin
, 0, sizeof(sin
));
520 sin
.sin_family
= AF_INET
;
521 sin
.sin_len
= sizeof(struct sockaddr_in
);
522 memcpy(&sin
.sin_addr
, &raddr
, sizeof(sin
.sin_addr
));
523 return __ivaliduser_sa(hostf
, (struct sockaddr
*)&sin
, sin
.sin_len
,
528 * Returns 0 if ok, -1 if not ok.
533 __ivaliduser_af(FILE *hostf
, const void *raddr
, const char *luser
,
534 const char *ruser
, int af
, int len
)
536 struct sockaddr
*sa
= NULL
;
537 struct sockaddr_in
*sin
= NULL
;
539 struct sockaddr_in6
*sin6
= NULL
;
541 struct sockaddr_storage ss
;
543 memset(&ss
, 0, sizeof(ss
));
546 if (len
!= sizeof(sin
->sin_addr
))
548 sin
= (struct sockaddr_in
*)&ss
;
549 sin
->sin_family
= AF_INET
;
550 sin
->sin_len
= sizeof(struct sockaddr_in
);
551 memcpy(&sin
->sin_addr
, raddr
, sizeof(sin
->sin_addr
));
555 if (len
!= sizeof(sin6
->sin6_addr
))
557 /* you will lose scope info */
558 sin6
= (struct sockaddr_in6
*)&ss
;
559 sin6
->sin6_family
= AF_INET6
;
560 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
561 memcpy(&sin6
->sin6_addr
, raddr
, sizeof(sin6
->sin6_addr
));
568 sa
= (struct sockaddr
*)&ss
;
569 return __ivaliduser_sa(hostf
, sa
, sa
->sa_len
, luser
, ruser
);
573 * Returns 0 if ok, -1 if not ok.
576 __ivaliduser_sa(FILE *hostf
, const struct sockaddr
*raddr
, socklen_t salen
,
577 const char *luser
, const char *ruser
)
581 char buf
[MAXHOSTNAMELEN
+ 128]; /* host + login */
582 char hname
[MAXHOSTNAMELEN
];
583 /* Presumed guilty until proven innocent. */
584 int userok
= 0, hostok
= 0;
588 if (yp_get_default_domain(&ypdomain
))
591 #define ypdomain NULL
593 /* We need to get the damn hostname back for netgroup matching. */
594 if (getnameinfo(raddr
, salen
, hname
, sizeof(hname
), NULL
, 0,
598 while (fgets(buf
, sizeof(buf
), hostf
)) {
600 /* Skip lines that are too long. */
601 if (strchr(p
, '\n') == NULL
) {
602 while ((ch
= getc(hostf
)) != '\n' && ch
!= EOF
);
605 if (*p
== '\n' || *p
== '#') {
609 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
610 *p
= isupper((unsigned char)*p
) ? tolower((unsigned char)*p
) : *p
;
613 if (*p
== ' ' || *p
== '\t') {
615 while (*p
== ' ' || *p
== '\t')
618 while (*p
!= '\n' && *p
!= ' ' &&
619 *p
!= '\t' && *p
!= '\0')
625 * Do +/- and +@/-@ checking. This looks really nasty,
626 * but it matches SunOS's behavior so far as I can tell.
630 if (!buf
[1]) { /* '+' matches all hosts */
634 if (buf
[1] == '@') /* match a host by netgroup */
635 hostok
= hname
[0] != '\0' &&
636 innetgr(&buf
[2], hname
, NULL
, ypdomain
);
637 else /* match a host by addr */
638 hostok
= __icheckhost(raddr
, salen
,
641 case '-': /* reject '-' hosts and all their users */
643 if (hname
[0] == '\0' ||
644 innetgr(&buf
[2], hname
, NULL
, ypdomain
))
647 if (__icheckhost(raddr
, salen
,
652 default: /* if no '+' or '-', do a simple match */
653 hostok
= __icheckhost(raddr
, salen
, buf
);
658 if (!*(user
+1)) { /* '+' matches all users */
662 if (*(user
+1) == '@') /* match a user by netgroup */
663 userok
= innetgr(user
+2, NULL
, ruser
, ypdomain
);
664 else /* match a user by direct specification */
665 userok
= !(strcmp(ruser
, user
+1));
667 case '-': /* if we matched a hostname, */
668 if (hostok
) { /* check for user field rejections */
671 if (*(user
+1) == '@') {
672 if (innetgr(user
+2, NULL
,
676 if (!strcmp(ruser
, user
+1))
681 default: /* no rejections: try to match the user */
683 userok
= !(strcmp(ruser
,*user
? user
: luser
));
686 if (hostok
&& userok
)
693 * Returns "true" if match, 0 if no match.
695 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion
699 __icheckhost(const struct sockaddr
*raddr
, socklen_t salen
, const char *lhost
)
701 struct sockaddr_in sin
;
702 struct sockaddr_in6
*sin6
;
703 struct addrinfo hints
, *res
, *r
;
705 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
707 if (raddr
->sa_family
== AF_INET6
) {
708 sin6
= (struct sockaddr_in6
*)raddr
;
709 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
)) {
710 memset(&sin
, 0, sizeof(sin
));
711 sin
.sin_family
= AF_INET
;
712 sin
.sin_len
= sizeof(struct sockaddr_in
);
713 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
714 sizeof(sin
.sin_addr
));
715 raddr
= (struct sockaddr
*)&sin
;
721 if (getnameinfo(raddr
, salen
, h1
, sizeof(h1
), NULL
, 0,
722 NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
725 /* Resolve laddr into sockaddr */
726 memset(&hints
, 0, sizeof(hints
));
727 hints
.ai_family
= raddr
->sa_family
;
728 hints
.ai_socktype
= SOCK_DGRAM
; /*XXX dummy*/
730 error
= getaddrinfo(lhost
, "0", &hints
, &res
);
734 for (r
= res
; r
; r
= r
->ai_next
) {
736 if (getnameinfo(r
->ai_addr
, r
->ai_addrlen
, h2
, sizeof(h2
),
737 NULL
, 0, NI_NUMERICHOST
| NI_WITHSCOPEID
) != 0)
739 if (strcmp(h1
, h2
) == 0) {