update from main archive 960927
[glibc.git] / inet / rcmd.c
bloba9756d109aeff8ec41ca52d067aad717d4d886d9
1 /*
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
7 * are met:
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
45 #include <signal.h>
46 #include <fcntl.h>
47 #include <netdb.h>
48 #include <unistd.h>
49 #include <pwd.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include <ctype.h>
53 #include <string.h>
55 #define MIN(A, B) ((A) < (B) ? (A) : (B))
58 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
59 static int __icheckhost __P((u_int32_t, char *));
61 int
62 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
63 char **ahost;
64 u_short rport;
65 const char *locuser, *remuser, *cmd;
66 int *fd2p;
68 struct hostent *hp;
69 struct sockaddr_in sin, from;
70 fd_set reads;
71 int32_t oldmask;
72 pid_t pid;
73 int s, lport, timo;
74 char c;
76 pid = getpid();
77 hp = gethostbyname(*ahost);
78 if (hp == NULL) {
79 herror(*ahost);
80 return (-1);
82 *ahost = hp->h_name;
83 oldmask = sigblock(sigmask(SIGURG));
84 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
85 s = rresvport(&lport);
86 if (s < 0) {
87 if (errno == EAGAIN)
88 (void)fprintf(stderr,
89 _("rcmd: socket: All ports in use\n"));
90 else
91 (void)fprintf(stderr, "rcmd: socket: %s\n",
92 strerror(errno));
93 sigsetmask(oldmask);
94 return (-1);
96 fcntl(s, F_SETOWN, pid);
97 sin.sin_family = hp->h_addrtype;
98 bcopy(hp->h_addr_list[0], &sin.sin_addr,
99 MIN (sizeof (sin.sin_addr), hp->h_length));
100 sin.sin_port = rport;
101 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
102 break;
103 (void)close(s);
104 if (errno == EADDRINUSE) {
105 lport--;
106 continue;
108 if (errno == ECONNREFUSED && timo <= 16) {
109 (void)sleep(timo);
110 timo *= 2;
111 continue;
113 if (hp->h_addr_list[1] != NULL) {
114 int oerrno = errno;
116 (void)fprintf(stderr, _("connect to address %s: "),
117 inet_ntoa(sin.sin_addr));
118 __set_errno (oerrno);
119 perror(0);
120 hp->h_addr_list++;
121 bcopy(hp->h_addr_list[0], &sin.sin_addr,
122 MIN (sizeof (sin.sin_addr), hp->h_length));
123 (void)fprintf(stderr, _("Trying %s...\n"),
124 inet_ntoa(sin.sin_addr));
125 continue;
127 (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
128 sigsetmask(oldmask);
129 return (-1);
131 lport--;
132 if (fd2p == 0) {
133 write(s, "", 1);
134 lport = 0;
135 } else {
136 char num[8];
137 int s2 = rresvport(&lport), s3;
138 int len = sizeof(from);
140 if (s2 < 0)
141 goto bad;
142 listen(s2, 1);
143 (void)snprintf(num, sizeof(num), "%d", lport);
144 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
145 (void)fprintf(stderr,
146 _("rcmd: write (setting up stderr): %s\n"),
147 strerror(errno));
148 (void)close(s2);
149 goto bad;
151 FD_ZERO(&reads);
152 FD_SET(s, &reads);
153 FD_SET(s2, &reads);
154 __set_errno (0);
155 if (select(1 + (s > s2 ? s : s2), &reads, 0, 0, 0) < 1 ||
156 !FD_ISSET(s2, &reads)) {
157 if (errno != 0)
158 (void)fprintf(stderr,
159 _("rcmd: select (setting up stderr): %s\n"),
160 strerror(errno));
161 else
162 (void)fprintf(stderr,
163 _("select: protocol failure in circuit setup\n"));
164 (void)close(s2);
165 goto bad;
167 s3 = accept(s2, (struct sockaddr *)&from, &len);
168 (void)close(s2);
169 if (s3 < 0) {
170 (void)fprintf(stderr,
171 "rcmd: accept: %s\n", strerror(errno));
172 lport = 0;
173 goto bad;
175 *fd2p = s3;
176 from.sin_port = ntohs((u_short)from.sin_port);
177 if (from.sin_family != AF_INET ||
178 from.sin_port >= IPPORT_RESERVED ||
179 from.sin_port < IPPORT_RESERVED / 2) {
180 (void)fprintf(stderr,
181 _("socket: protocol failure in circuit setup\n"));
182 goto bad2;
185 (void)write(s, locuser, strlen(locuser)+1);
186 (void)write(s, remuser, strlen(remuser)+1);
187 (void)write(s, cmd, strlen(cmd)+1);
188 if (read(s, &c, 1) != 1) {
189 (void)fprintf(stderr,
190 "rcmd: %s: %s\n", *ahost, strerror(errno));
191 goto bad2;
193 if (c != 0) {
194 while (read(s, &c, 1) == 1) {
195 (void)write(STDERR_FILENO, &c, 1);
196 if (c == '\n')
197 break;
199 goto bad2;
201 sigsetmask(oldmask);
202 return (s);
203 bad2:
204 if (lport)
205 (void)close(*fd2p);
206 bad:
207 (void)close(s);
208 sigsetmask(oldmask);
209 return (-1);
213 rresvport(alport)
214 int *alport;
216 struct sockaddr_in sin;
217 int s;
219 sin.sin_family = AF_INET;
220 sin.sin_addr.s_addr = INADDR_ANY;
221 s = socket(AF_INET, SOCK_STREAM, 0);
222 if (s < 0)
223 return (-1);
224 for (;;) {
225 sin.sin_port = htons((u_short)*alport);
226 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
227 return (s);
228 if (errno != EADDRINUSE) {
229 (void)close(s);
230 return (-1);
232 (*alport)--;
233 if (*alport == IPPORT_RESERVED/2) {
234 (void)close(s);
235 __set_errno (EAGAIN); /* close */
236 return (-1);
241 int __check_rhosts_file = 1;
242 char *__rcmd_errstr;
245 ruserok(rhost, superuser, ruser, luser)
246 const char *rhost, *ruser, *luser;
247 int superuser;
249 struct hostent *hp;
250 u_int32_t addr;
251 char **ap;
253 if ((hp = gethostbyname(rhost)) == NULL)
254 return (-1);
255 for (ap = hp->h_addr_list; *ap; ++ap) {
256 bcopy(*ap, &addr, sizeof(addr));
257 if (iruserok(addr, superuser, ruser, luser) == 0)
258 return (0);
260 return (-1);
264 * New .rhosts strategy: We are passed an ip address. We spin through
265 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
266 * has ip addresses, we don't have to trust a nameserver. When it
267 * contains hostnames, we spin through the list of addresses the nameserver
268 * gives us and look for a match.
270 * Returns 0 if ok, -1 if not ok.
273 iruserok(raddr, superuser, ruser, luser)
274 u_int32_t raddr;
275 int superuser;
276 const char *ruser, *luser;
278 register char *cp;
279 struct stat sbuf;
280 struct passwd *pwd;
281 FILE *hostf;
282 uid_t uid;
283 int first;
285 first = 1;
286 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
287 again:
288 if (hostf) {
289 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
290 (void)fclose(hostf);
291 return (0);
293 (void)fclose(hostf);
295 if (first == 1 && (__check_rhosts_file || superuser)) {
296 char *pbuf;
297 size_t dirlen;
299 first = 0;
300 if ((pwd = getpwnam(luser)) == NULL)
301 return (-1);
303 dirlen = strlen (pwd->pw_dir);
304 pbuf = alloca (dirlen + sizeof "/.rhosts");
305 memcpy (pbuf, pwd->pw_dir, dirlen);
306 memcpy (pbuf + dirlen, "/.rhosts", sizeof "/.rhosts");
309 * Change effective uid while opening .rhosts. If root and
310 * reading an NFS mounted file system, can't read files that
311 * are protected read/write owner only.
313 uid = geteuid();
314 (void)seteuid(pwd->pw_uid);
315 hostf = fopen(pbuf, "r");
316 (void)seteuid(uid);
318 if (hostf == NULL)
319 return (-1);
321 * If not a regular file, or is owned by someone other than
322 * user or root or if writeable by anyone but the owner, quit.
324 cp = NULL;
325 if (lstat(pbuf, &sbuf) < 0)
326 cp = _(".rhosts lstat failed");
327 else if (!S_ISREG(sbuf.st_mode))
328 cp = _(".rhosts not regular file");
329 else if (fstat(fileno(hostf), &sbuf) < 0)
330 cp = _(".rhosts fstat failed");
331 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
332 cp = _("bad .rhosts owner");
333 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
334 cp = _(".rhosts writeable by other than owner");
335 /* If there were any problems, quit. */
336 if (cp) {
337 __rcmd_errstr = cp;
338 (void)fclose(hostf);
339 return (-1);
341 goto again;
343 return (-1);
347 * XXX
348 * Don't make static, used by lpd(8).
350 * Returns 0 if ok, -1 if not ok.
353 __ivaliduser(hostf, raddr, luser, ruser)
354 FILE *hostf;
355 u_int32_t raddr;
356 const char *luser, *ruser;
358 register char *user, *p;
359 int ch;
360 char *buf = NULL;
361 size_t bufsize = 0;
362 ssize_t nread;
364 while ((nread = getline (&buf, &bufsize, hostf)) > 0) {
365 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
366 p = buf;
367 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
368 *p = isupper(*p) ? tolower(*p) : *p;
369 p++;
371 if (*p == ' ' || *p == '\t') {
372 *p++ = '\0';
373 while (*p == ' ' || *p == '\t')
374 p++;
375 user = p;
376 while (*p != '\n' && *p != ' ' &&
377 *p != '\t' && *p != '\0')
378 p++;
379 } else
380 user = p;
381 *p = '\0';
382 if (__icheckhost(raddr, buf) &&
383 strcmp(ruser, *user ? user : luser) == 0) {
384 free (buf);
385 return (0);
388 free (buf);
389 return (-1);
393 * Returns "true" if match, 0 if no match.
395 static int
396 __icheckhost(raddr, lhost)
397 u_int32_t raddr;
398 register char *lhost;
400 register struct hostent *hp;
401 register u_int32_t laddr;
402 register char **pp;
404 /* Try for raw ip address first. */
405 if (isdigit(*lhost) && (int32_t)(laddr = inet_addr(lhost)) != -1)
406 return (raddr == laddr);
408 /* Better be a hostname. */
409 if ((hp = gethostbyname(lhost)) == NULL)
410 return (0);
412 /* Spin through ip addresses. */
413 for (pp = hp->h_addr_list; *pp; ++pp)
414 if (!bcmp(&raddr, *pp, sizeof(u_int32_t)))
415 return (1);
417 /* No match. */
418 return (0);