]>
git.saurik.com Git - apple/libc.git/blob - gen/rcmd.c
b884c9e884946fdf1a6877eb8cbf9e13fa26c8b7
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1983, 1993, 1994
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 #if defined(LIBC_SCCS) && !defined(lint)
56 static char sccsid
[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
57 #endif /* LIBC_SCCS and not lint */
59 #include <sys/param.h>
60 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
76 int __ivaliduser
__P((FILE *, u_long
, const char *, const char *));
77 static int __icheckhost
__P((u_long
, char *));
80 rcmd(ahost
, rport
, locuser
, remuser
, cmd
, fd2p
)
83 const char *locuser
, *remuser
, *cmd
;
87 struct sockaddr_in sin
, from
;
95 hp
= gethostbyname(*ahost
);
101 oldmask
= sigblock(sigmask(SIGURG
));
102 for (timo
= 1, lport
= IPPORT_RESERVED
- 1;;) {
103 s
= rresvport(&lport
);
106 (void)fprintf(stderr
,
107 "rcmd: socket: All ports in use\n");
109 (void)fprintf(stderr
, "rcmd: socket: %s\n",
114 fcntl(s
, F_SETOWN
, pid
);
115 sin
.sin_family
= hp
->h_addrtype
;
116 bcopy(hp
->h_addr_list
[0], &sin
.sin_addr
, hp
->h_length
);
117 sin
.sin_port
= rport
;
118 if (connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
121 if (errno
== EADDRINUSE
) {
125 if (errno
== ECONNREFUSED
&& timo
<= 16) {
130 if (hp
->h_addr_list
[1] != NULL
) {
133 (void)fprintf(stderr
, "connect to address %s: ",
134 inet_ntoa(sin
.sin_addr
));
138 bcopy(hp
->h_addr_list
[0], &sin
.sin_addr
, hp
->h_length
);
139 (void)fprintf(stderr
, "Trying %s...\n",
140 inet_ntoa(sin
.sin_addr
));
143 (void)fprintf(stderr
, "%s: %s\n", hp
->h_name
, strerror(errno
));
153 int s2
= rresvport(&lport
), s3
;
154 int len
= sizeof(from
);
159 (void)snprintf(num
, sizeof(num
), "%d", lport
);
160 if (write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
161 (void)fprintf(stderr
,
162 "rcmd: write (setting up stderr): %s\n",
171 if (select(32, &reads
, 0, 0, 0) < 1 || !FD_ISSET(s2
, &reads
)) {
173 (void)fprintf(stderr
,
174 "rcmd: select (setting up stderr): %s\n",
177 (void)fprintf(stderr
,
178 "select: protocol failure in circuit setup\n");
182 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
185 (void)fprintf(stderr
,
186 "rcmd: accept: %s\n", strerror(errno
));
191 from
.sin_port
= ntohs((u_short
)from
.sin_port
);
192 if (from
.sin_family
!= AF_INET
||
193 from
.sin_port
>= IPPORT_RESERVED
||
194 from
.sin_port
< IPPORT_RESERVED
/ 2) {
195 (void)fprintf(stderr
,
196 "socket: protocol failure in circuit setup.\n");
200 (void)write(s
, locuser
, strlen(locuser
)+1);
201 (void)write(s
, remuser
, strlen(remuser
)+1);
202 (void)write(s
, cmd
, strlen(cmd
)+1);
203 if (read(s
, &c
, 1) != 1) {
204 (void)fprintf(stderr
,
205 "rcmd: %s: %s\n", *ahost
, strerror(errno
));
209 while (read(s
, &c
, 1) == 1) {
210 (void)write(STDERR_FILENO
, &c
, 1);
231 struct sockaddr_in sin
;
234 sin
.sin_family
= AF_INET
;
235 sin
.sin_addr
.s_addr
= INADDR_ANY
;
236 s
= socket(AF_INET
, SOCK_STREAM
, 0);
240 sin
.sin_port
= htons((u_short
)*alport
);
241 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
243 if (errno
!= EADDRINUSE
) {
248 if (*alport
== IPPORT_RESERVED
/2) {
250 errno
= EAGAIN
; /* close */
256 int __check_rhosts_file
= 1;
260 ruserok(rhost
, superuser
, ruser
, luser
)
261 const char *rhost
, *ruser
, *luser
;
268 if ((hp
= gethostbyname(rhost
)) == NULL
)
270 for (ap
= hp
->h_addr_list
; *ap
; ++ap
) {
271 bcopy(*ap
, &addr
, sizeof(addr
));
272 if (iruserok(addr
, superuser
, ruser
, luser
) == 0)
279 * New .rhosts strategy: We are passed an ip address. We spin through
280 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
281 * has ip addresses, we don't have to trust a nameserver. When it
282 * contains hostnames, we spin through the list of addresses the nameserver
283 * gives us and look for a match.
285 * Returns 0 if ok, -1 if not ok.
288 iruserok(raddr
, superuser
, ruser
, luser
)
291 const char *ruser
, *luser
;
299 char pbuf
[MAXPATHLEN
];
302 hostf
= superuser
? NULL
: fopen(_PATH_HEQUIV
, "r");
305 if (__ivaliduser(hostf
, raddr
, luser
, ruser
) == 0) {
311 if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
313 if ((pwd
= getpwnam(luser
)) == NULL
)
315 (void)strcpy(pbuf
, pwd
->pw_dir
);
316 (void)strcat(pbuf
, "/.rhosts");
319 * Change effective uid while opening .rhosts. If root and
320 * reading an NFS mounted file system, can't read files that
321 * are protected read/write owner only.
324 (void)seteuid(pwd
->pw_uid
);
325 hostf
= fopen(pbuf
, "r");
331 * If not a regular file, or is owned by someone other than
332 * user or root or if writeable by anyone but the owner, quit.
335 if (lstat(pbuf
, &sbuf
) < 0)
336 cp
= ".rhosts lstat failed";
337 else if (!S_ISREG(sbuf
.st_mode
))
338 cp
= ".rhosts not regular file";
339 else if (fstat(fileno(hostf
), &sbuf
) < 0)
340 cp
= ".rhosts fstat failed";
341 else if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
)
342 cp
= "bad .rhosts owner";
343 else if (sbuf
.st_mode
& (S_IWGRP
|S_IWOTH
))
344 cp
= ".rhosts writeable by other than owner";
345 /* If there were any problems, quit. */
358 * Don't make static, used by lpd(8).
360 * Returns 0 if ok, -1 if not ok.
363 __ivaliduser(hostf
, raddr
, luser
, ruser
)
366 const char *luser
, *ruser
;
368 register char *user
, *p
;
370 char buf
[MAXHOSTNAMELEN
+ 128]; /* host + login */
372 while (fgets(buf
, sizeof(buf
), hostf
)) {
374 /* Skip lines that are too long. */
375 if (strchr(p
, '\n') == NULL
) {
376 while ((ch
= getc(hostf
)) != '\n' && ch
!= EOF
);
379 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
380 *p
= isupper(*p
) ? tolower(*p
) : *p
;
383 if (*p
== ' ' || *p
== '\t') {
385 while (*p
== ' ' || *p
== '\t')
388 while (*p
!= '\n' && *p
!= ' ' &&
389 *p
!= '\t' && *p
!= '\0')
394 if (__icheckhost(raddr
, buf
) &&
395 strcmp(ruser
, *user
? user
: luser
) == 0) {
403 * Returns "true" if match, 0 if no match.
406 __icheckhost(raddr
, lhost
)
408 register char *lhost
;
410 register struct hostent
*hp
;
411 register u_long laddr
;
414 /* Try for raw ip address first. */
415 if (isdigit(*lhost
) && (long)(laddr
= inet_addr(lhost
)) != -1)
416 return (raddr
== laddr
);
418 /* Better be a hostname. */
419 if ((hp
= gethostbyname(lhost
)) == NULL
)
422 /* Spin through ip addresses. */
423 for (pp
= hp
->h_addr_list
; *pp
; ++pp
)
424 if (!bcmp(&raddr
, *pp
, sizeof(u_long
)))