diff options
author | deraadt <> | 1995-10-18 08:42:23 +0000 |
---|---|---|
committer | deraadt <> | 1995-10-18 08:42:23 +0000 |
commit | 0527d29da443886d92e9a418180c5b25a5f8d270 (patch) | |
tree | 86b3a64928451a669cefa27900e5884036b4e349 /src/lib/libc/net/rcmd.c | |
download | openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.tar.gz openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.tar.bz2 openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.zip |
initial import of NetBSD tree
Diffstat (limited to 'src/lib/libc/net/rcmd.c')
-rw-r--r-- | src/lib/libc/net/rcmd.c | 517 |
1 files changed, 517 insertions, 0 deletions
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c new file mode 100644 index 0000000000..e0310031b0 --- /dev/null +++ b/src/lib/libc/net/rcmd.c | |||
@@ -0,0 +1,517 @@ | |||
1 | /* $NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993, 1994 | ||
5 | * The Regents of the University of California. All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; | ||
39 | #else | ||
40 | static char *rcsid = "$NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/param.h> | ||
45 | #include <sys/socket.h> | ||
46 | #include <sys/stat.h> | ||
47 | |||
48 | #include <netinet/in.h> | ||
49 | #include <arpa/inet.h> | ||
50 | |||
51 | #include <signal.h> | ||
52 | #include <fcntl.h> | ||
53 | #include <netdb.h> | ||
54 | #include <unistd.h> | ||
55 | #include <pwd.h> | ||
56 | #include <errno.h> | ||
57 | #include <stdio.h> | ||
58 | #include <ctype.h> | ||
59 | #include <string.h> | ||
60 | |||
61 | int __ivaliduser __P((FILE *, u_long, const char *, const char *)); | ||
62 | static int __icheckhost __P((u_long, const char *)); | ||
63 | |||
64 | int | ||
65 | rcmd(ahost, rport, locuser, remuser, cmd, fd2p) | ||
66 | char **ahost; | ||
67 | u_short rport; | ||
68 | const char *locuser, *remuser, *cmd; | ||
69 | int *fd2p; | ||
70 | { | ||
71 | struct hostent *hp; | ||
72 | struct sockaddr_in sin, from; | ||
73 | fd_set reads; | ||
74 | long oldmask; | ||
75 | pid_t pid; | ||
76 | int s, lport, timo; | ||
77 | char c; | ||
78 | |||
79 | pid = getpid(); | ||
80 | hp = gethostbyname(*ahost); | ||
81 | if (hp == NULL) { | ||
82 | herror(*ahost); | ||
83 | return (-1); | ||
84 | } | ||
85 | *ahost = hp->h_name; | ||
86 | oldmask = sigblock(sigmask(SIGURG)); | ||
87 | for (timo = 1, lport = IPPORT_RESERVED - 1;;) { | ||
88 | s = rresvport(&lport); | ||
89 | if (s < 0) { | ||
90 | if (errno == EAGAIN) | ||
91 | (void)fprintf(stderr, | ||
92 | "rcmd: socket: All ports in use\n"); | ||
93 | else | ||
94 | (void)fprintf(stderr, "rcmd: socket: %s\n", | ||
95 | strerror(errno)); | ||
96 | sigsetmask(oldmask); | ||
97 | return (-1); | ||
98 | } | ||
99 | fcntl(s, F_SETOWN, pid); | ||
100 | sin.sin_len = sizeof(struct sockaddr_in); | ||
101 | sin.sin_family = hp->h_addrtype; | ||
102 | sin.sin_port = rport; | ||
103 | bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); | ||
104 | if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) | ||
105 | break; | ||
106 | (void)close(s); | ||
107 | if (errno == EADDRINUSE) { | ||
108 | lport--; | ||
109 | continue; | ||
110 | } | ||
111 | if (errno == ECONNREFUSED && timo <= 16) { | ||
112 | (void)sleep(timo); | ||
113 | timo *= 2; | ||
114 | continue; | ||
115 | } | ||
116 | if (hp->h_addr_list[1] != NULL) { | ||
117 | int oerrno = errno; | ||
118 | |||
119 | (void)fprintf(stderr, "connect to address %s: ", | ||
120 | inet_ntoa(sin.sin_addr)); | ||
121 | errno = oerrno; | ||
122 | perror(0); | ||
123 | hp->h_addr_list++; | ||
124 | bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); | ||
125 | (void)fprintf(stderr, "Trying %s...\n", | ||
126 | inet_ntoa(sin.sin_addr)); | ||
127 | continue; | ||
128 | } | ||
129 | (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); | ||
130 | sigsetmask(oldmask); | ||
131 | return (-1); | ||
132 | } | ||
133 | lport--; | ||
134 | if (fd2p == 0) { | ||
135 | write(s, "", 1); | ||
136 | lport = 0; | ||
137 | } else { | ||
138 | char num[8]; | ||
139 | int s2 = rresvport(&lport), s3; | ||
140 | int len = sizeof(from); | ||
141 | |||
142 | if (s2 < 0) | ||
143 | goto bad; | ||
144 | listen(s2, 1); | ||
145 | (void)snprintf(num, sizeof(num), "%d", lport); | ||
146 | if (write(s, num, strlen(num)+1) != strlen(num)+1) { | ||
147 | (void)fprintf(stderr, | ||
148 | "rcmd: write (setting up stderr): %s\n", | ||
149 | strerror(errno)); | ||
150 | (void)close(s2); | ||
151 | goto bad; | ||
152 | } | ||
153 | FD_ZERO(&reads); | ||
154 | FD_SET(s, &reads); | ||
155 | FD_SET(s2, &reads); | ||
156 | errno = 0; | ||
157 | if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 || | ||
158 | !FD_ISSET(s2, &reads)) { | ||
159 | if (errno != 0) | ||
160 | (void)fprintf(stderr, | ||
161 | "rcmd: select (setting up stderr): %s\n", | ||
162 | strerror(errno)); | ||
163 | else | ||
164 | (void)fprintf(stderr, | ||
165 | "select: protocol failure in circuit setup\n"); | ||
166 | (void)close(s2); | ||
167 | goto bad; | ||
168 | } | ||
169 | s3 = accept(s2, (struct sockaddr *)&from, &len); | ||
170 | (void)close(s2); | ||
171 | if (s3 < 0) { | ||
172 | (void)fprintf(stderr, | ||
173 | "rcmd: accept: %s\n", strerror(errno)); | ||
174 | lport = 0; | ||
175 | goto bad; | ||
176 | } | ||
177 | *fd2p = s3; | ||
178 | from.sin_port = ntohs(from.sin_port); | ||
179 | if (from.sin_family != AF_INET || | ||
180 | from.sin_port >= IPPORT_RESERVED || | ||
181 | from.sin_port < IPPORT_RESERVED / 2) { | ||
182 | (void)fprintf(stderr, | ||
183 | "socket: protocol failure in circuit setup.\n"); | ||
184 | goto bad2; | ||
185 | } | ||
186 | } | ||
187 | (void)write(s, locuser, strlen(locuser)+1); | ||
188 | (void)write(s, remuser, strlen(remuser)+1); | ||
189 | (void)write(s, cmd, strlen(cmd)+1); | ||
190 | if (read(s, &c, 1) != 1) { | ||
191 | (void)fprintf(stderr, | ||
192 | "rcmd: %s: %s\n", *ahost, strerror(errno)); | ||
193 | goto bad2; | ||
194 | } | ||
195 | if (c != 0) { | ||
196 | while (read(s, &c, 1) == 1) { | ||
197 | (void)write(STDERR_FILENO, &c, 1); | ||
198 | if (c == '\n') | ||
199 | break; | ||
200 | } | ||
201 | goto bad2; | ||
202 | } | ||
203 | sigsetmask(oldmask); | ||
204 | return (s); | ||
205 | bad2: | ||
206 | if (lport) | ||
207 | (void)close(*fd2p); | ||
208 | bad: | ||
209 | (void)close(s); | ||
210 | sigsetmask(oldmask); | ||
211 | return (-1); | ||
212 | } | ||
213 | |||
214 | int | ||
215 | rresvport(alport) | ||
216 | int *alport; | ||
217 | { | ||
218 | struct sockaddr_in sin; | ||
219 | int s; | ||
220 | |||
221 | sin.sin_len = sizeof(struct sockaddr_in); | ||
222 | sin.sin_family = AF_INET; | ||
223 | sin.sin_addr.s_addr = INADDR_ANY; | ||
224 | s = socket(AF_INET, SOCK_STREAM, 0); | ||
225 | if (s < 0) | ||
226 | return (-1); | ||
227 | for (;;) { | ||
228 | sin.sin_port = htons((u_short)*alport); | ||
229 | if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) | ||
230 | return (s); | ||
231 | if (errno != EADDRINUSE) { | ||
232 | (void)close(s); | ||
233 | return (-1); | ||
234 | } | ||
235 | (*alport)--; | ||
236 | if (*alport == IPPORT_RESERVED/2) { | ||
237 | (void)close(s); | ||
238 | errno = EAGAIN; /* close */ | ||
239 | return (-1); | ||
240 | } | ||
241 | } | ||
242 | } | ||
243 | |||
244 | int __check_rhosts_file = 1; | ||
245 | char *__rcmd_errstr; | ||
246 | |||
247 | int | ||
248 | ruserok(rhost, superuser, ruser, luser) | ||
249 | const char *rhost, *ruser, *luser; | ||
250 | int superuser; | ||
251 | { | ||
252 | struct hostent *hp; | ||
253 | char **ap; | ||
254 | int i; | ||
255 | #define MAXADDRS 35 | ||
256 | u_long addrs[MAXADDRS + 1]; | ||
257 | |||
258 | if ((hp = gethostbyname(rhost)) == NULL) | ||
259 | return (-1); | ||
260 | for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i) | ||
261 | bcopy(*ap, &addrs[i], sizeof(addrs[i])); | ||
262 | addrs[i] = 0; | ||
263 | |||
264 | for (i = 0; i < MAXADDRS && addrs[i]; i++) | ||
265 | if (iruserok(addrs[i], superuser, ruser, luser) == 0) | ||
266 | return (0); | ||
267 | return (-1); | ||
268 | } | ||
269 | |||
270 | /* | ||
271 | * New .rhosts strategy: We are passed an ip address. We spin through | ||
272 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | ||
273 | * has ip addresses, we don't have to trust a nameserver. When it | ||
274 | * contains hostnames, we spin through the list of addresses the nameserver | ||
275 | * gives us and look for a match. | ||
276 | * | ||
277 | * Returns 0 if ok, -1 if not ok. | ||
278 | */ | ||
279 | int | ||
280 | iruserok(raddr, superuser, ruser, luser) | ||
281 | u_long raddr; | ||
282 | int superuser; | ||
283 | const char *ruser, *luser; | ||
284 | { | ||
285 | register char *cp; | ||
286 | struct stat sbuf; | ||
287 | struct passwd *pwd; | ||
288 | FILE *hostf; | ||
289 | uid_t uid; | ||
290 | int first; | ||
291 | char pbuf[MAXPATHLEN]; | ||
292 | |||
293 | first = 1; | ||
294 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | ||
295 | again: | ||
296 | if (hostf) { | ||
297 | if (__ivaliduser(hostf, raddr, luser, ruser) == 0) { | ||
298 | (void)fclose(hostf); | ||
299 | return (0); | ||
300 | } | ||
301 | (void)fclose(hostf); | ||
302 | } | ||
303 | if (first == 1 && (__check_rhosts_file || superuser)) { | ||
304 | first = 0; | ||
305 | if ((pwd = getpwnam(luser)) == NULL) | ||
306 | return (-1); | ||
307 | (void)strcpy(pbuf, pwd->pw_dir); | ||
308 | (void)strcat(pbuf, "/.rhosts"); | ||
309 | |||
310 | /* | ||
311 | * Change effective uid while opening .rhosts. If root and | ||
312 | * reading an NFS mounted file system, can't read files that | ||
313 | * are protected read/write owner only. | ||
314 | */ | ||
315 | uid = geteuid(); | ||
316 | (void)seteuid(pwd->pw_uid); | ||
317 | hostf = fopen(pbuf, "r"); | ||
318 | (void)seteuid(uid); | ||
319 | |||
320 | if (hostf == NULL) | ||
321 | return (-1); | ||
322 | /* | ||
323 | * If not a regular file, or is owned by someone other than | ||
324 | * user or root or if writeable by anyone but the owner, quit. | ||
325 | */ | ||
326 | cp = NULL; | ||
327 | if (lstat(pbuf, &sbuf) < 0) | ||
328 | cp = ".rhosts lstat failed"; | ||
329 | else if (!S_ISREG(sbuf.st_mode)) | ||
330 | cp = ".rhosts not regular file"; | ||
331 | else if (fstat(fileno(hostf), &sbuf) < 0) | ||
332 | cp = ".rhosts fstat failed"; | ||
333 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | ||
334 | cp = "bad .rhosts owner"; | ||
335 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | ||
336 | cp = ".rhosts writeable by other than owner"; | ||
337 | /* If there were any problems, quit. */ | ||
338 | if (cp) { | ||
339 | __rcmd_errstr = cp; | ||
340 | (void)fclose(hostf); | ||
341 | return (-1); | ||
342 | } | ||
343 | goto again; | ||
344 | } | ||
345 | return (-1); | ||
346 | } | ||
347 | |||
348 | /* | ||
349 | * XXX | ||
350 | * Don't make static, used by lpd(8). | ||
351 | * | ||
352 | * Returns 0 if ok, -1 if not ok. | ||
353 | */ | ||
354 | int | ||
355 | __ivaliduser(hostf, raddr, luser, ruser) | ||
356 | FILE *hostf; | ||
357 | u_long raddr; | ||
358 | const char *luser, *ruser; | ||
359 | { | ||
360 | register char *user, *p; | ||
361 | int ch; | ||
362 | char buf[MAXHOSTNAMELEN + 128]; /* host + login */ | ||
363 | const char *auser, *ahost; | ||
364 | int hostok, userok; | ||
365 | char rhost[MAXHOSTNAMELEN]; | ||
366 | struct hostent *hp; | ||
367 | char domain[MAXHOSTNAMELEN]; | ||
368 | |||
369 | getdomainname(domain, sizeof(domain)); | ||
370 | |||
371 | while (fgets(buf, sizeof(buf), hostf)) { | ||
372 | p = buf; | ||
373 | /* Skip lines that are too long. */ | ||
374 | if (strchr(p, '\n') == NULL) { | ||
375 | while ((ch = getc(hostf)) != '\n' && ch != EOF); | ||
376 | continue; | ||
377 | } | ||
378 | while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { | ||
379 | *p = isupper(*p) ? tolower(*p) : *p; | ||
380 | p++; | ||
381 | } | ||
382 | if (*p == ' ' || *p == '\t') { | ||
383 | *p++ = '\0'; | ||
384 | while (*p == ' ' || *p == '\t') | ||
385 | p++; | ||
386 | user = p; | ||
387 | while (*p != '\n' && *p != ' ' && | ||
388 | *p != '\t' && *p != '\0') | ||
389 | p++; | ||
390 | } else | ||
391 | user = p; | ||
392 | *p = '\0'; | ||
393 | |||
394 | if (p == buf) | ||
395 | continue; | ||
396 | |||
397 | auser = *user ? user : luser; | ||
398 | ahost = buf; | ||
399 | |||
400 | if ((hp = gethostbyaddr((char *) &raddr, | ||
401 | sizeof(raddr), AF_INET)) == NULL) { | ||
402 | abort(); | ||
403 | return -1; | ||
404 | } | ||
405 | (void) strncpy(rhost, hp->h_name, sizeof(rhost)); | ||
406 | rhost[sizeof(rhost) - 1] = '\0'; | ||
407 | |||
408 | if (ahost[0] == '+') | ||
409 | switch (ahost[1]) { | ||
410 | case '\0': | ||
411 | hostok = 1; | ||
412 | break; | ||
413 | |||
414 | case '@': | ||
415 | hostok = innetgr(&ahost[2], rhost, NULL, | ||
416 | domain); | ||
417 | break; | ||
418 | |||
419 | default: | ||
420 | hostok = __icheckhost(raddr, &ahost[1]); | ||
421 | break; | ||
422 | } | ||
423 | else if (ahost[0] == '-') | ||
424 | switch (ahost[1]) { | ||
425 | case '\0': | ||
426 | hostok = -1; | ||
427 | break; | ||
428 | |||
429 | case '@': | ||
430 | hostok = -innetgr(&ahost[2], rhost, NULL, | ||
431 | domain); | ||
432 | break; | ||
433 | |||
434 | default: | ||
435 | hostok = -__icheckhost(raddr, &ahost[1]); | ||
436 | break; | ||
437 | } | ||
438 | else | ||
439 | hostok = __icheckhost(raddr, ahost); | ||
440 | |||
441 | |||
442 | if (auser[0] == '+') | ||
443 | switch (auser[1]) { | ||
444 | case '\0': | ||
445 | userok = 1; | ||
446 | break; | ||
447 | |||
448 | case '@': | ||
449 | userok = innetgr(&auser[2], NULL, ruser, | ||
450 | domain); | ||
451 | break; | ||
452 | |||
453 | default: | ||
454 | userok = strcmp(ruser, &auser[1]) == 0; | ||
455 | break; | ||
456 | } | ||
457 | else if (auser[0] == '-') | ||
458 | switch (auser[1]) { | ||
459 | case '\0': | ||
460 | userok = -1; | ||
461 | break; | ||
462 | |||
463 | case '@': | ||
464 | userok = -innetgr(&auser[2], NULL, ruser, | ||
465 | domain); | ||
466 | break; | ||
467 | |||
468 | default: | ||
469 | userok = -(strcmp(ruser, &auser[1]) == 0); | ||
470 | break; | ||
471 | } | ||
472 | else | ||
473 | userok = strcmp(ruser, auser) == 0; | ||
474 | |||
475 | /* Check if one component did not match */ | ||
476 | if (hostok == 0 || userok == 0) | ||
477 | continue; | ||
478 | |||
479 | /* Check if we got a forbidden pair */ | ||
480 | if (userok == -1 || hostok == -1) | ||
481 | return -1; | ||
482 | |||
483 | /* Check if we got a valid pair */ | ||
484 | if (hostok == 1 && userok == 1) | ||
485 | return 0; | ||
486 | } | ||
487 | return -1; | ||
488 | } | ||
489 | |||
490 | /* | ||
491 | * Returns "true" if match, 0 if no match. | ||
492 | */ | ||
493 | static int | ||
494 | __icheckhost(raddr, lhost) | ||
495 | u_long raddr; | ||
496 | const char *lhost; | ||
497 | { | ||
498 | register struct hostent *hp; | ||
499 | register u_long laddr; | ||
500 | register char **pp; | ||
501 | |||
502 | /* Try for raw ip address first. */ | ||
503 | if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1) | ||
504 | return (raddr == laddr); | ||
505 | |||
506 | /* Better be a hostname. */ | ||
507 | if ((hp = gethostbyname(lhost)) == NULL) | ||
508 | return (0); | ||
509 | |||
510 | /* Spin through ip addresses. */ | ||
511 | for (pp = hp->h_addr_list; *pp; ++pp) | ||
512 | if (!bcmp(&raddr, *pp, sizeof(u_long))) | ||
513 | return (1); | ||
514 | |||
515 | /* No match. */ | ||
516 | return (0); | ||
517 | } | ||