]> sourceware.org Git - glibc.git/blob - inet/rcmd.c
Update.
[glibc.git] / inet / rcmd.c
1 /*
2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
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.
20 *
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.
32 */
33
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 */
37
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44
45 #include <alloca.h>
46 #include <signal.h>
47 #include <fcntl.h>
48 #include <netdb.h>
49 #include <unistd.h>
50 #include <pwd.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <ctype.h>
54 #include <string.h>
55
56
57 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
58 static int __icheckhost __P((u_int32_t, char *));
59
60 int
61 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
62 char **ahost;
63 u_short rport;
64 const char *locuser, *remuser, *cmd;
65 int *fd2p;
66 {
67 struct hostent hostbuf, *hp;
68 size_t hstbuflen;
69 char *tmphstbuf;
70 struct sockaddr_in sin, from;
71 fd_set reads;
72 int32_t oldmask;
73 pid_t pid;
74 int s, lport, timo;
75 char c;
76 int herr;
77
78 pid = getpid();
79
80 hstbuflen = 1024;
81 tmphstbuf = __alloca (hstbuflen);
82 while (__gethostbyname_r (*ahost, &hostbuf, tmphstbuf, hstbuflen,
83 &hp, &herr) < 0)
84 if (herr != NETDB_INTERNAL || errno != ERANGE)
85 {
86 __set_h_errno (herr);
87 herror(*ahost);
88 return -1;
89 }
90 else
91 {
92 /* Enlarge the buffer. */
93 hstbuflen *= 2;
94 tmphstbuf = __alloca (hstbuflen);
95 }
96
97 *ahost = hp->h_name;
98 oldmask = sigblock(sigmask(SIGURG));
99 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
100 s = rresvport(&lport);
101 if (s < 0) {
102 if (errno == EAGAIN)
103 (void)fprintf(stderr,
104 _("rcmd: socket: All ports in use\n"));
105 else
106 (void)fprintf(stderr, "rcmd: socket: %m\n");
107 sigsetmask(oldmask);
108 return (-1);
109 }
110 fcntl(s, F_SETOWN, pid);
111 sin.sin_family = hp->h_addrtype;
112 bcopy(hp->h_addr_list[0], &sin.sin_addr,
113 MIN (sizeof (sin.sin_addr), hp->h_length));
114 sin.sin_port = rport;
115 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
116 break;
117 (void)close(s);
118 if (errno == EADDRINUSE) {
119 lport--;
120 continue;
121 }
122 if (errno == ECONNREFUSED && timo <= 16) {
123 (void)sleep(timo);
124 timo *= 2;
125 continue;
126 }
127 if (hp->h_addr_list[1] != NULL) {
128 int oerrno = errno;
129
130 (void)fprintf(stderr, _("connect to address %s: "),
131 inet_ntoa(sin.sin_addr));
132 __set_errno (oerrno);
133 perror(0);
134 hp->h_addr_list++;
135 bcopy(hp->h_addr_list[0], &sin.sin_addr,
136 MIN (sizeof (sin.sin_addr), hp->h_length));
137 (void)fprintf(stderr, _("Trying %s...\n"),
138 inet_ntoa(sin.sin_addr));
139 continue;
140 }
141 (void)fprintf(stderr, "%s: %m\n", hp->h_name);
142 sigsetmask(oldmask);
143 return (-1);
144 }
145 lport--;
146 if (fd2p == 0) {
147 write(s, "", 1);
148 lport = 0;
149 } else {
150 char num[8];
151 int s2 = rresvport(&lport), s3;
152 int len = sizeof(from);
153
154 if (s2 < 0)
155 goto bad;
156 listen(s2, 1);
157 (void)snprintf(num, sizeof(num), "%d", lport);
158 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
159 (void)fprintf(stderr,
160 _("rcmd: write (setting up stderr): %m\n"));
161 (void)close(s2);
162 goto bad;
163 }
164 FD_ZERO(&reads);
165 FD_SET(s, &reads);
166 FD_SET(s2, &reads);
167 __set_errno (0);
168 if (select(1 + (s > s2 ? s : s2), &reads, 0, 0, 0) < 1 ||
169 !FD_ISSET(s2, &reads)) {
170 if (errno != 0)
171 (void)fprintf(stderr,
172 _("rcmd: select (setting up stderr): %m\n"));
173 else
174 (void)fprintf(stderr,
175 _("select: protocol failure in circuit setup\n"));
176 (void)close(s2);
177 goto bad;
178 }
179 s3 = accept(s2, (struct sockaddr *)&from, &len);
180 (void)close(s2);
181 if (s3 < 0) {
182 (void)fprintf(stderr,
183 "rcmd: accept: %m\n");
184 lport = 0;
185 goto bad;
186 }
187 *fd2p = s3;
188 from.sin_port = ntohs((u_short)from.sin_port);
189 if (from.sin_family != AF_INET ||
190 from.sin_port >= IPPORT_RESERVED ||
191 from.sin_port < IPPORT_RESERVED / 2) {
192 (void)fprintf(stderr,
193 _("socket: protocol failure in circuit setup\n"));
194 goto bad2;
195 }
196 }
197 (void)write(s, locuser, strlen(locuser)+1);
198 (void)write(s, remuser, strlen(remuser)+1);
199 (void)write(s, cmd, strlen(cmd)+1);
200 if (read(s, &c, 1) != 1) {
201 (void)fprintf(stderr,
202 "rcmd: %s: %m\n", *ahost);
203 goto bad2;
204 }
205 if (c != 0) {
206 while (read(s, &c, 1) == 1) {
207 (void)write(STDERR_FILENO, &c, 1);
208 if (c == '\n')
209 break;
210 }
211 goto bad2;
212 }
213 sigsetmask(oldmask);
214 return (s);
215 bad2:
216 if (lport)
217 (void)close(*fd2p);
218 bad:
219 (void)close(s);
220 sigsetmask(oldmask);
221 return (-1);
222 }
223
224 int
225 rresvport(alport)
226 int *alport;
227 {
228 struct sockaddr_in sin;
229 int s;
230
231 sin.sin_family = AF_INET;
232 sin.sin_addr.s_addr = INADDR_ANY;
233 s = socket(AF_INET, SOCK_STREAM, 0);
234 if (s < 0)
235 return (-1);
236 for (;;) {
237 sin.sin_port = htons((u_short)*alport);
238 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
239 return (s);
240 if (errno != EADDRINUSE) {
241 (void)close(s);
242 return (-1);
243 }
244 (*alport)--;
245 if (*alport == IPPORT_RESERVED/2) {
246 (void)close(s);
247 __set_errno (EAGAIN); /* close */
248 return (-1);
249 }
250 }
251 }
252
253 int __check_rhosts_file = 1;
254 char *__rcmd_errstr;
255
256 int
257 ruserok(rhost, superuser, ruser, luser)
258 const char *rhost, *ruser, *luser;
259 int superuser;
260 {
261 struct hostent hostbuf, *hp;
262 size_t buflen;
263 char *buffer;
264 u_int32_t addr;
265 char **ap;
266 int herr;
267
268 buflen = 1024;
269 buffer = __alloca (buflen);
270
271 while (__gethostbyname_r (rhost, &hostbuf, buffer, buflen, &hp, &herr)
272 < 0)
273 if (herr != NETDB_INTERNAL || errno != ERANGE)
274 return -1;
275 else
276 {
277 /* Enlarge the buffer. */
278 buflen *= 2;
279 buffer = __alloca (buflen);
280 }
281
282 for (ap = hp->h_addr_list; *ap; ++ap) {
283 bcopy(*ap, &addr, sizeof(addr));
284 if (iruserok(addr, superuser, ruser, luser) == 0)
285 return (0);
286 }
287 return (-1);
288 }
289
290 /*
291 * New .rhosts strategy: We are passed an ip address. We spin through
292 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
293 * has ip addresses, we don't have to trust a nameserver. When it
294 * contains hostnames, we spin through the list of addresses the nameserver
295 * gives us and look for a match.
296 *
297 * Returns 0 if ok, -1 if not ok.
298 */
299 int
300 iruserok(raddr, superuser, ruser, luser)
301 u_int32_t raddr;
302 int superuser;
303 const char *ruser, *luser;
304 {
305 register char *cp;
306 struct stat sbuf;
307 struct passwd pwdbuf, *pwd;
308 FILE *hostf;
309 uid_t uid;
310 int first;
311
312 first = 1;
313 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
314 again:
315 if (hostf) {
316 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
317 (void)fclose(hostf);
318 return (0);
319 }
320 (void)fclose(hostf);
321 }
322 if (first == 1 && (__check_rhosts_file || superuser)) {
323 char *pbuf;
324 size_t dirlen;
325 size_t buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
326 char *buffer = __alloca (buflen);
327
328 first = 0;
329 if (getpwnam_r (luser, &pwdbuf, buffer, buflen, &pwd) < 0)
330 return -1;
331
332 dirlen = strlen (pwd->pw_dir);
333 pbuf = alloca (dirlen + sizeof "/.rhosts");
334 memcpy (pbuf, pwd->pw_dir, dirlen);
335 memcpy (pbuf + dirlen, "/.rhosts", sizeof "/.rhosts");
336
337 /*
338 * Change effective uid while opening .rhosts. If root and
339 * reading an NFS mounted file system, can't read files that
340 * are protected read/write owner only.
341 */
342 if (euidaccess (pbuf, R_OK) != 0)
343 hostf = NULL;
344 else
345 hostf = fopen(pbuf, "r");
346
347 if (hostf == NULL)
348 return (-1);
349 /*
350 * If not a regular file, or is owned by someone other than
351 * user or root or if writeable by anyone but the owner, quit.
352 */
353 cp = NULL;
354 if (lstat(pbuf, &sbuf) < 0)
355 cp = _(".rhosts lstat failed");
356 else if (!S_ISREG(sbuf.st_mode))
357 cp = _(".rhosts not regular file");
358 else if (fstat(fileno(hostf), &sbuf) < 0)
359 cp = _(".rhosts fstat failed");
360 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
361 cp = _("bad .rhosts owner");
362 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
363 cp = _(".rhosts writeable by other than owner");
364 /* If there were any problems, quit. */
365 if (cp) {
366 __rcmd_errstr = cp;
367 (void)fclose(hostf);
368 return (-1);
369 }
370 goto again;
371 }
372 return (-1);
373 }
374
375 /*
376 * XXX
377 * Don't make static, used by lpd(8).
378 *
379 * Returns 0 if ok, -1 if not ok.
380 */
381 int
382 __ivaliduser(hostf, raddr, luser, ruser)
383 FILE *hostf;
384 u_int32_t raddr;
385 const char *luser, *ruser;
386 {
387 register char *user, *p;
388 int ch;
389 char *buf = NULL;
390 size_t bufsize = 0;
391 ssize_t nread;
392
393 while ((nread = __getline (&buf, &bufsize, hostf)) > 0) {
394 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
395 p = buf;
396 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
397 *p = isupper(*p) ? tolower(*p) : *p;
398 p++;
399 }
400 if (*p == ' ' || *p == '\t') {
401 *p++ = '\0';
402 while (*p == ' ' || *p == '\t')
403 p++;
404 user = p;
405 while (*p != '\n' && *p != ' ' &&
406 *p != '\t' && *p != '\0')
407 p++;
408 } else
409 user = p;
410 *p = '\0';
411 if (__icheckhost(raddr, buf) &&
412 strcmp(ruser, *user ? user : luser) == 0) {
413 free (buf);
414 return (0);
415 }
416 }
417 free (buf);
418 return (-1);
419 }
420
421 /*
422 * Returns "true" if match, 0 if no match.
423 */
424 static int
425 __icheckhost(raddr, lhost)
426 u_int32_t raddr;
427 register char *lhost;
428 {
429 register struct hostent hostbuf, *hp;
430 size_t buflen;
431 char *buffer;
432 register u_int32_t laddr;
433 register char **pp;
434 int herr;
435
436 /* Try for raw ip address first. */
437 if (isdigit(*lhost) && (int32_t)(laddr = inet_addr(lhost)) != -1)
438 return (raddr == laddr);
439
440 /* Better be a hostname. */
441 buflen = 1024;
442 buffer = __alloca (buflen);
443 while (__gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
444 < 0)
445 if (herr != NETDB_INTERNAL || errno != ERANGE)
446 return 0;
447 else
448 {
449 /* Enlarge the buffer. */
450 buflen *= 2;
451 buflen = __alloca (buflen);
452 }
453
454 /* Spin through ip addresses. */
455 for (pp = hp->h_addr_list; *pp; ++pp)
456 if (!bcmp(&raddr, *pp, sizeof(u_int32_t)))
457 return (1);
458
459 /* No match. */
460 return (0);
461 }
This page took 0.062365 seconds and 6 git commands to generate.