Update.
[glibc.git] / inet / rcmd.c
blobe54d894f84899ebc1fcbd1f221bbd9b9924bc6ca
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/poll.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 #include <alloca.h>
47 #include <signal.h>
48 #include <fcntl.h>
49 #include <netdb.h>
50 #include <unistd.h>
51 #include <pwd.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include <ctype.h>
55 #include <string.h>
58 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
59 static int __icheckhost __P((u_int32_t, char *)) internal_function;
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 hostbuf, *hp;
69 size_t hstbuflen;
70 char *tmphstbuf;
71 struct sockaddr_in sin, from;
72 struct pollfd pfd[2];
73 int32_t oldmask;
74 pid_t pid;
75 int s, lport, timo;
76 char c;
77 int herr;
79 pid = getpid();
81 hstbuflen = 1024;
82 tmphstbuf = __alloca (hstbuflen);
83 while (__gethostbyname_r (*ahost, &hostbuf, tmphstbuf, hstbuflen,
84 &hp, &herr) < 0)
85 if (herr != NETDB_INTERNAL || errno != ERANGE)
87 __set_h_errno (herr);
88 herror(*ahost);
89 return -1;
91 else
93 /* Enlarge the buffer. */
94 hstbuflen *= 2;
95 tmphstbuf = __alloca (hstbuflen);
98 pfd[0].events = POLLIN;
99 pfd[1].events = POLLIN;
101 *ahost = hp->h_name;
102 oldmask = sigblock(sigmask(SIGURG));
103 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
104 s = rresvport(&lport);
105 if (s < 0) {
106 if (errno == EAGAIN)
107 (void)fprintf(stderr,
108 _("rcmd: socket: All ports in use\n"));
109 else
110 (void)fprintf(stderr, "rcmd: socket: %m\n");
111 sigsetmask(oldmask);
112 return -1;
114 fcntl(s, F_SETOWN, pid);
115 sin.sin_family = hp->h_addrtype;
116 bcopy(hp->h_addr_list[0], &sin.sin_addr,
117 MIN (sizeof (sin.sin_addr), hp->h_length));
118 sin.sin_port = rport;
119 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
120 break;
121 (void)close(s);
122 if (errno == EADDRINUSE) {
123 lport--;
124 continue;
126 if (errno == ECONNREFUSED && timo <= 16) {
127 (void)sleep(timo);
128 timo *= 2;
129 continue;
131 if (hp->h_addr_list[1] != NULL) {
132 int oerrno = errno;
134 (void)fprintf(stderr, _("connect to address %s: "),
135 inet_ntoa(sin.sin_addr));
136 __set_errno (oerrno);
137 perror(0);
138 hp->h_addr_list++;
139 bcopy(hp->h_addr_list[0], &sin.sin_addr,
140 MIN (sizeof (sin.sin_addr), hp->h_length));
141 (void)fprintf(stderr, _("Trying %s...\n"),
142 inet_ntoa(sin.sin_addr));
143 continue;
145 (void)fprintf(stderr, "%s: %m\n", hp->h_name);
146 sigsetmask(oldmask);
147 return -1;
149 lport--;
150 if (fd2p == 0) {
151 write(s, "", 1);
152 lport = 0;
153 } else {
154 char num[8];
155 int s2 = rresvport(&lport), s3;
156 size_t len = sizeof(from);
158 if (s2 < 0)
159 goto bad;
160 listen(s2, 1);
161 (void)snprintf(num, sizeof(num), "%d", lport);
162 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
163 (void)fprintf(stderr,
164 _("rcmd: write (setting up stderr): %m\n"));
165 (void)close(s2);
166 goto bad;
168 pfd[0].fd = s;
169 pfd[1].fd = s2;
170 __set_errno (0);
171 if (__poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
172 if (errno != 0)
173 (void)fprintf(stderr,
174 _("rcmd: poll (setting up stderr): %m\n"));
175 else
176 (void)fprintf(stderr,
177 _("poll: protocol failure in circuit setup\n"));
178 (void)close(s2);
179 goto bad;
181 s3 = accept(s2, (struct sockaddr *)&from, &len);
182 (void)close(s2);
183 if (s3 < 0) {
184 (void)fprintf(stderr,
185 "rcmd: accept: %m\n");
186 lport = 0;
187 goto bad;
189 *fd2p = s3;
190 from.sin_port = ntohs((u_short)from.sin_port);
191 if (from.sin_family != AF_INET ||
192 from.sin_port >= IPPORT_RESERVED ||
193 from.sin_port < IPPORT_RESERVED / 2) {
194 (void)fprintf(stderr,
195 _("socket: protocol failure in circuit setup\n"));
196 goto bad2;
199 (void)write(s, locuser, strlen(locuser)+1);
200 (void)write(s, remuser, strlen(remuser)+1);
201 (void)write(s, cmd, strlen(cmd)+1);
202 if (read(s, &c, 1) != 1) {
203 (void)fprintf(stderr,
204 "rcmd: %s: %m\n", *ahost);
205 goto bad2;
207 if (c != 0) {
208 while (read(s, &c, 1) == 1) {
209 (void)write(STDERR_FILENO, &c, 1);
210 if (c == '\n')
211 break;
213 goto bad2;
215 sigsetmask(oldmask);
216 return s;
217 bad2:
218 if (lport)
219 (void)close(*fd2p);
220 bad:
221 (void)close(s);
222 sigsetmask(oldmask);
223 return -1;
227 rresvport(alport)
228 int *alport;
230 struct sockaddr_in sin;
231 int s;
233 sin.sin_family = AF_INET;
234 sin.sin_addr.s_addr = INADDR_ANY;
235 s = socket(AF_INET, SOCK_STREAM, 0);
236 if (s < 0)
237 return -1;
238 for (;;) {
239 sin.sin_port = htons((u_short)*alport);
240 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
241 return s;
242 if (errno != EADDRINUSE) {
243 (void)close(s);
244 return -1;
246 (*alport)--;
247 if (*alport == IPPORT_RESERVED/2) {
248 (void)close(s);
249 __set_errno (EAGAIN); /* close */
250 return -1;
255 int __check_rhosts_file = 1;
256 char *__rcmd_errstr;
259 ruserok(rhost, superuser, ruser, luser)
260 const char *rhost, *ruser, *luser;
261 int superuser;
263 struct hostent hostbuf, *hp;
264 size_t buflen;
265 char *buffer;
266 u_int32_t addr;
267 char **ap;
268 int herr;
270 buflen = 1024;
271 buffer = __alloca (buflen);
273 while (__gethostbyname_r (rhost, &hostbuf, buffer, buflen, &hp, &herr)
274 < 0)
275 if (herr != NETDB_INTERNAL || errno != ERANGE)
276 return -1;
277 else
279 /* Enlarge the buffer. */
280 buflen *= 2;
281 buffer = __alloca (buflen);
284 for (ap = hp->h_addr_list; *ap; ++ap) {
285 bcopy(*ap, &addr, sizeof(addr));
286 if (iruserok(addr, superuser, ruser, luser) == 0)
287 return 0;
289 return -1;
292 /* Extremely paranoid file open function. */
293 static FILE *
294 iruserfopen (char *file, uid_t okuser)
296 struct stat st;
297 char *cp = NULL;
298 FILE *res = NULL;
300 /* If not a regular file, if owned by someone other than user or
301 root, if writeable by anyone but the owner, or if hardlinked
302 anywhere, quit. */
303 cp = NULL;
304 if (__lxstat (_STAT_VER, file, &st))
305 cp = _("lstat failed");
306 else if (!S_ISREG (st.st_mode))
307 cp = _("not regular file");
308 else
310 res = fopen (file, "r");
311 if (!res)
312 cp = _("cannot open");
313 else if (__fxstat (_STAT_VER, fileno (res), &st) < 0)
314 cp = _("fstat failed");
315 else if (st.st_uid && st.st_uid != okuser)
316 cp = _("bad owner");
317 else if (st.st_mode & (S_IWGRP|S_IWOTH))
318 cp = _("writeable by other than owner");
319 else if (st.st_nlink > 1)
320 cp = _("hard linked somewhere");
323 /* If there were any problems, quit. */
324 if (cp != NULL)
326 __rcmd_errstr = cp;
327 if (res)
328 fclose (res);
329 return NULL;
332 return res;
336 * New .rhosts strategy: We are passed an ip address. We spin through
337 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
338 * has ip addresses, we don't have to trust a nameserver. When it
339 * contains hostnames, we spin through the list of addresses the nameserver
340 * gives us and look for a match.
342 * Returns 0 if ok, -1 if not ok.
345 iruserok (raddr, superuser, ruser, luser)
346 u_int32_t raddr;
347 int superuser;
348 const char *ruser, *luser;
350 FILE *hostf = NULL;
351 int isbad;
353 if (!superuser)
354 hostf = iruserfopen (_PATH_HEQUIV, 0);
356 if (hostf)
358 isbad = __ivaliduser (hostf, raddr, luser, ruser);
359 fclose (hostf);
361 if (!isbad)
362 return 0;
365 if (__check_rhosts_file || superuser)
367 char *pbuf;
368 struct passwd pwdbuf, *pwd;
369 size_t dirlen;
370 size_t buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
371 char *buffer = __alloca (buflen);
372 uid_t uid;
374 if (__getpwnam_r (luser, &pwdbuf, buffer, buflen, &pwd))
375 return -1;
377 dirlen = strlen (pwd->pw_dir);
378 pbuf = alloca (dirlen + sizeof "/.rhosts");
379 __mempcpy (__mempcpy (pbuf, pwd->pw_dir, dirlen),
380 "/.rhosts", sizeof "/.rhosts");
382 /* Change effective uid while reading .rhosts. If root and
383 reading an NFS mounted file system, can't read files that
384 are protected read/write owner only. */
385 uid = geteuid ();
386 seteuid (pwd->pw_uid);
387 hostf = iruserfopen (pbuf, pwd->pw_uid);
389 if (hostf != NULL)
391 isbad = __ivaliduser (hostf, raddr, luser, ruser);
392 fclose (hostf);
395 seteuid (uid);
396 return isbad;
398 return -1;
402 * XXX
403 * Don't make static, used by lpd(8).
405 * Returns 0 if ok, -1 if not ok.
408 __ivaliduser(hostf, raddr, luser, ruser)
409 FILE *hostf;
410 u_int32_t raddr;
411 const char *luser, *ruser;
413 register char *user, *p;
414 int ch;
415 char *buf = NULL;
416 size_t bufsize = 0;
417 ssize_t nread;
419 while ((nread = __getline (&buf, &bufsize, hostf)) > 0) {
420 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
421 p = buf;
422 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
423 *p = isupper(*p) ? tolower(*p) : *p;
424 p++;
426 if (*p == ' ' || *p == '\t') {
427 *p++ = '\0';
428 while (*p == ' ' || *p == '\t')
429 p++;
430 user = p;
431 while (*p != '\n' && *p != ' ' &&
432 *p != '\t' && *p != '\0')
433 p++;
434 } else
435 user = p;
436 *p = '\0';
437 if (__icheckhost(raddr, buf) &&
438 strcmp(ruser, *user ? user : luser) == 0) {
439 free (buf);
440 return 0;
443 if (buf != NULL)
444 free (buf);
445 return -1;
449 * Returns "true" if match, 0 if no match.
451 static int
452 internal_function
453 __icheckhost(raddr, lhost)
454 u_int32_t raddr;
455 register char *lhost;
457 struct hostent hostbuf, *hp;
458 size_t buflen;
459 char *buffer;
460 register u_int32_t laddr;
461 register char **pp;
462 int herr;
464 /* Try for raw ip address first. */
465 if (isdigit(*lhost) && (int32_t)(laddr = inet_addr(lhost)) != -1)
466 return raddr == laddr;
468 /* Better be a hostname. */
469 buflen = 1024;
470 buffer = __alloca (buflen);
471 while (__gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
472 < 0)
473 if (herr != NETDB_INTERNAL || errno != ERANGE)
474 return 0;
475 else
477 /* Enlarge the buffer. */
478 buflen *= 2;
479 buffer = __alloca (buflen);
482 /* Spin through ip addresses. */
483 for (pp = hp->h_addr_list; *pp; ++pp)
484 if (!bcmp(&raddr, *pp, sizeof(u_int32_t)))
485 return 1;
487 /* No match. */
488 return 0;