2 * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * Copyright (c) 1983 Regents of the University of California.
8 * All rights reserved. The Berkeley software License Agreement
9 * specifies the terms and conditions for redistribution.
12 #pragma ident "%Z%%M% %I% %E% SMI"
17 #include <sys/param.h>
19 #include <sys/signal.h>
20 #include <sys/socket.h>
23 #include <netinet/in.h>
41 int s
, timo
= 1, pid
, oldmask
, retval
;
42 struct sockaddr_in sin
, from
;
44 int lport
= IPPORT_RESERVED
- 1;
48 hp
= gethostbyname(*ahost
);
50 fprintf(stderr
, "%s: unknown host\n", *ahost
);
54 oldmask
= sigblock(sigmask(SIGURG
));
56 s
= rresvport(&lport
);
59 fprintf(stderr
, "socket: All ports in use\n");
61 perror("rcmd: socket");
65 fcntl(s
, F_SETOWN
, pid
);
66 sin
.sin_family
= hp
->h_addrtype
;
67 bcopy(hp
->h_addr_list
[0], (caddr_t
)&sin
.sin_addr
, hp
->h_length
);
69 if (connect(s
, &sin
, sizeof (sin
)) >= 0)
72 if (errno
== EADDRINUSE
) {
76 if (errno
== ECONNREFUSED
&& timo
<= 16) {
81 if (hp
->h_addr_list
[1] != NULL
) {
85 "connect to address %s: ", inet_ntoa(sin
.sin_addr
));
89 bcopy(hp
->h_addr_list
[0], (caddr_t
)&sin
.sin_addr
,
91 fprintf(stderr
, "Trying %s...\n",
92 inet_ntoa(sin
.sin_addr
));
105 int s2
= rresvport(&lport
), s3
;
106 int len
= sizeof (from
);
111 (void) sprintf(num
, "%d", lport
);
112 if (write(s
, num
, strlen(num
)+1) != strlen(num
)+1) {
113 perror("write: setting up stderr");
117 s3
= accept(s2
, &from
, &len
);
125 from
.sin_port
= ntohs((u_short
)from
.sin_port
);
126 if (from
.sin_family
!= AF_INET
||
127 from
.sin_port
>= IPPORT_RESERVED
) {
129 "socket: protocol failure in circuit setup.\n");
133 (void) write(s
, locuser
, strlen(locuser
)+1);
134 (void) write(s
, remuser
, strlen(remuser
)+1);
135 (void) write(s
, cmd
, strlen(cmd
)+1);
136 retval
= read(s
, &c
, 1);
140 "Protocol error, %s closed connection\n", *ahost
);
141 } else if (retval
< 0) {
145 "Protocol error, %s sent %d bytes\n", *ahost
, retval
);
150 while (read(s
, &c
, 1) == 1) {
151 (void) write(2, &c
, 1);
169 rresvport(int *alport
)
171 struct sockaddr_in sin
;
174 sin
.sin_family
= AF_INET
;
175 sin
.sin_addr
.s_addr
= INADDR_ANY
;
176 s
= socket(AF_INET
, SOCK_STREAM
, 0);
180 sin
.sin_port
= htons((u_short
)*alport
);
181 if (bind(s
, (caddr_t
)&sin
, sizeof (sin
)) >= 0)
183 if (errno
!= EADDRINUSE
) {
188 if (*alport
== IPPORT_RESERVED
/2) {
190 errno
= EAGAIN
; /* close */
204 char fhost
[MAXHOSTNAMELEN
];
211 char pbuf
[MAXPATHLEN
];
219 baselen
= sp
- rhost
;
222 *p
++ = isupper(*sp
) ? tolower(*sp
++) : *sp
++;
227 /* check /etc/hosts.equiv */
229 if ((hostf
= fopen("/etc/hosts.equiv", "r")) != NULL
) {
230 if (!_validuser(hostf
, fhost
, luser
, ruser
, baselen
)) {
231 (void) fclose(hostf
);
234 (void) fclose(hostf
);
238 /* check ~/.rhosts */
240 if ((pwd
= getpwnam(luser
)) == NULL
)
242 (void)strcpy(pbuf
, pwd
->pw_dir
);
243 (void)strcat(pbuf
, "/.rhosts");
246 * Read .rhosts as the local user to avoid NFS mapping the root uid
247 * to something that can't read .rhosts.
250 (void) seteuid (pwd
->pw_uid
);
251 if ((hostf
= fopen(pbuf
, "r")) == NULL
) {
253 (void) seteuid (euid
);
256 (void)fstat(fileno(hostf
), &sbuf
);
257 if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
) {
260 (void) seteuid (euid
);
264 if (!_validuser(hostf
, fhost
, luser
, ruser
, baselen
)) {
265 (void) fclose(hostf
);
267 (void) seteuid (euid
);
271 (void) fclose(hostf
);
273 (void) seteuid (euid
);
278 _validuser(FILE *hostf
, char *rhost
, char *luser
, char *ruser
, int baselen
)
281 char ahost
[MAXHOSTNAMELEN
];
282 int hostmatch
, usermatch
;
285 if (domain
== NULL
) {
286 (void) yp_get_default_domain(&domain
);
288 while (fgets(ahost
, sizeof (ahost
), hostf
)) {
289 hostmatch
= usermatch
= 0; /* bugid fix 1033104 */
291 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
292 *p
= isupper(*p
) ? tolower(*p
) : *p
;
295 if (*p
== ' ' || *p
== '\t') {
297 while (*p
== ' ' || *p
== '\t')
300 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0')
305 if (ahost
[0] == '+' && ahost
[1] == 0)
307 else if (ahost
[0] == '+' && ahost
[1] == '@')
308 hostmatch
= innetgr(ahost
+ 2, rhost
,
310 else if (ahost
[0] == '-' && ahost
[1] == '@') {
311 if (innetgr(ahost
+ 2, rhost
, NULL
, domain
))
314 else if (ahost
[0] == '-') {
315 if (_checkhost(rhost
, ahost
+1, baselen
))
319 hostmatch
= _checkhost(rhost
, ahost
, baselen
);
321 if (user
[0] == '+' && user
[1] == 0)
323 else if (user
[0] == '+' && user
[1] == '@')
324 usermatch
= innetgr(user
+2, NULL
,
326 else if (user
[0] == '-' && user
[1] == '@') {
327 if (hostmatch
&& innetgr(user
+2, NULL
,
331 else if (user
[0] == '-') {
332 if (hostmatch
&& !strcmp(user
+1, ruser
))
336 usermatch
= !strcmp(user
, ruser
);
339 usermatch
= !strcmp(ruser
, luser
);
340 if (hostmatch
&& usermatch
)
347 _checkhost(char *rhost
, char *lhost
, int len
)
349 static char *ldomain
;
350 static char *domainp
;
354 if (ldomain
== NULL
) {
355 ldomain
= (char *)malloc(MAXHOSTNAMELEN
+1);
361 return(!strcmp(rhost
, lhost
));
362 if (strncmp(rhost
, lhost
, len
))
364 if (!strcmp(rhost
, lhost
))
366 if (*(lhost
+ len
) != '\0')
372 * "domainp" points after the first dot in the host name
374 if (gethostname(ldomain
, MAXHOSTNAMELEN
) == -1) {
378 ldomain
[MAXHOSTNAMELEN
] = NULL
;
379 if ((domainp
= index(ldomain
, '.')) == (char *)NULL
) {
386 *cp
= isupper(*cp
) ? tolower(*cp
) : *cp
;
390 return(!strcmp(domainp
, rhost
+ len
+1));