diff options
Diffstat (limited to 'src/lib/libc/net/rcmd.c')
-rw-r--r-- | src/lib/libc/net/rcmd.c | 733 |
1 files changed, 733 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..be68c599d8 --- /dev/null +++ b/src/lib/libc/net/rcmd.c | |||
@@ -0,0 +1,733 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. | ||
3 | * Copyright (c) 1983, 1993, 1994 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. All advertising materials mentioning features or use of this software | ||
15 | * must display the following acknowledgement: | ||
16 | * This product includes software developed by the University of | ||
17 | * California, Berkeley and its contributors. | ||
18 | * This product includes software developed by Theo de Raadt. | ||
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 | static char *rcsid = "$OpenBSD: rcmd.c,v 1.43 2002/05/24 21:22:37 deraadt Exp $"; | ||
38 | #endif /* LIBC_SCCS and not lint */ | ||
39 | |||
40 | #include <sys/param.h> | ||
41 | #include <sys/socket.h> | ||
42 | #include <sys/stat.h> | ||
43 | |||
44 | #include <netinet/in.h> | ||
45 | #include <arpa/inet.h> | ||
46 | |||
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> | ||
56 | #include <syslog.h> | ||
57 | #include <stdlib.h> | ||
58 | #include <netgroup.h> | ||
59 | |||
60 | int __ivaliduser(FILE *, in_addr_t, const char *, const char *); | ||
61 | int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t, | ||
62 | const char *, const char *); | ||
63 | static int __icheckhost(struct sockaddr *, socklen_t, const char *); | ||
64 | static char *__gethostloop(struct sockaddr *, socklen_t); | ||
65 | |||
66 | int | ||
67 | rcmd(ahost, rport, locuser, remuser, cmd, fd2p) | ||
68 | char **ahost; | ||
69 | in_port_t rport; | ||
70 | const char *locuser, *remuser, *cmd; | ||
71 | int *fd2p; | ||
72 | { | ||
73 | return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); | ||
74 | } | ||
75 | |||
76 | int | ||
77 | rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) | ||
78 | char **ahost; | ||
79 | in_port_t rport; | ||
80 | const char *locuser, *remuser, *cmd; | ||
81 | int *fd2p; | ||
82 | int af; | ||
83 | { | ||
84 | static char hbuf[MAXHOSTNAMELEN]; | ||
85 | char pbuf[NI_MAXSERV]; | ||
86 | struct addrinfo hints, *res, *r; | ||
87 | int error; | ||
88 | struct sockaddr_storage from; | ||
89 | fd_set *readsp = NULL; | ||
90 | sigset_t oldmask, mask; | ||
91 | pid_t pid; | ||
92 | int s, lport, timo; | ||
93 | char c, *p; | ||
94 | int refused; | ||
95 | |||
96 | /* call rcmdsh() with specified remote shell if appropriate. */ | ||
97 | if (!issetugid() && (p = getenv("RSH")) && *p) { | ||
98 | struct servent *sp = getservbyname("shell", "tcp"); | ||
99 | |||
100 | if (sp && sp->s_port == rport) | ||
101 | return (rcmdsh(ahost, rport, locuser, remuser, | ||
102 | cmd, p)); | ||
103 | } | ||
104 | |||
105 | /* use rsh(1) if non-root and remote port is shell. */ | ||
106 | if (geteuid()) { | ||
107 | struct servent *sp = getservbyname("shell", "tcp"); | ||
108 | |||
109 | if (sp && sp->s_port == rport) | ||
110 | return (rcmdsh(ahost, rport, locuser, remuser, | ||
111 | cmd, NULL)); | ||
112 | } | ||
113 | |||
114 | pid = getpid(); | ||
115 | snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); | ||
116 | memset(&hints, 0, sizeof(hints)); | ||
117 | hints.ai_family = af; | ||
118 | hints.ai_socktype = SOCK_STREAM; | ||
119 | hints.ai_flags = AI_CANONNAME; | ||
120 | error = getaddrinfo(*ahost, pbuf, &hints, &res); | ||
121 | if (error) { | ||
122 | #if 0 | ||
123 | warnx("%s: %s", *ahost, gai_strerror(error)); | ||
124 | #endif | ||
125 | return (-1); | ||
126 | } | ||
127 | if (res->ai_canonname) { | ||
128 | strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); | ||
129 | *ahost = hbuf; | ||
130 | } else | ||
131 | ; /*XXX*/ | ||
132 | |||
133 | r = res; | ||
134 | refused = 0; | ||
135 | sigemptyset(&mask); | ||
136 | sigaddset(&mask, SIGURG); | ||
137 | oldmask = sigprocmask(SIG_BLOCK, &mask, &oldmask); | ||
138 | for (timo = 1, lport = IPPORT_RESERVED - 1;;) { | ||
139 | s = rresvport_af(&lport, r->ai_family); | ||
140 | if (s < 0) { | ||
141 | if (errno == EAGAIN) | ||
142 | (void)fprintf(stderr, | ||
143 | "rcmd: socket: All ports in use\n"); | ||
144 | else | ||
145 | (void)fprintf(stderr, "rcmd: socket: %s\n", | ||
146 | strerror(errno)); | ||
147 | if (r->ai_next) { | ||
148 | r = r->ai_next; | ||
149 | continue; | ||
150 | } else { | ||
151 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
152 | freeaddrinfo(res); | ||
153 | return (-1); | ||
154 | } | ||
155 | } | ||
156 | fcntl(s, F_SETOWN, pid); | ||
157 | if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) | ||
158 | break; | ||
159 | (void)close(s); | ||
160 | if (errno == EADDRINUSE) { | ||
161 | lport--; | ||
162 | continue; | ||
163 | } | ||
164 | if (errno == ECONNREFUSED) | ||
165 | refused++; | ||
166 | if (r->ai_next) { | ||
167 | int oerrno = errno; | ||
168 | char hbuf[NI_MAXHOST]; | ||
169 | #ifdef NI_WITHSCOPEID | ||
170 | const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; | ||
171 | #else | ||
172 | const int niflags = NI_NUMERICHOST; | ||
173 | #endif | ||
174 | |||
175 | hbuf[0] = '\0'; | ||
176 | if (getnameinfo(r->ai_addr, r->ai_addrlen, | ||
177 | hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) | ||
178 | strlcpy(hbuf, "(invalid)", sizeof hbuf); | ||
179 | (void)fprintf(stderr, "connect to address %s: ", hbuf); | ||
180 | errno = oerrno; | ||
181 | perror(0); | ||
182 | r = r->ai_next; | ||
183 | hbuf[0] = '\0'; | ||
184 | if (getnameinfo(r->ai_addr, r->ai_addrlen, | ||
185 | hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) | ||
186 | strlcpy(hbuf, "(invalid)", sizeof hbuf); | ||
187 | (void)fprintf(stderr, "Trying %s...\n", hbuf); | ||
188 | continue; | ||
189 | } | ||
190 | if (refused && timo <= 16) { | ||
191 | (void)sleep(timo); | ||
192 | timo *= 2; | ||
193 | r = res; | ||
194 | refused = 0; | ||
195 | continue; | ||
196 | } | ||
197 | (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, | ||
198 | strerror(errno)); | ||
199 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
200 | freeaddrinfo(res); | ||
201 | return (-1); | ||
202 | } | ||
203 | /* given "af" can be PF_UNSPEC, we need the real af for "s" */ | ||
204 | af = r->ai_family; | ||
205 | freeaddrinfo(res); | ||
206 | #if 0 | ||
207 | /* | ||
208 | * try to rresvport() to the same port. This will make rresvport() | ||
209 | * fail it's first bind, resulting in it choosing a random port. | ||
210 | */ | ||
211 | lport--; | ||
212 | #endif | ||
213 | if (fd2p == 0) { | ||
214 | write(s, "", 1); | ||
215 | lport = 0; | ||
216 | } else { | ||
217 | char num[8]; | ||
218 | int s2 = rresvport_af(&lport, af), s3; | ||
219 | int len = sizeof(from); | ||
220 | int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask); | ||
221 | |||
222 | if (s2 < 0) | ||
223 | goto bad; | ||
224 | readsp = (fd_set *)malloc(fdssize); | ||
225 | if (readsp == NULL) | ||
226 | goto bad; | ||
227 | listen(s2, 1); | ||
228 | (void)snprintf(num, sizeof(num), "%d", lport); | ||
229 | if (write(s, num, strlen(num)+1) != strlen(num)+1) { | ||
230 | (void)fprintf(stderr, | ||
231 | "rcmd: write (setting up stderr): %s\n", | ||
232 | strerror(errno)); | ||
233 | (void)close(s2); | ||
234 | goto bad; | ||
235 | } | ||
236 | again: | ||
237 | bzero(readsp, fdssize); | ||
238 | FD_SET(s, readsp); | ||
239 | FD_SET(s2, readsp); | ||
240 | errno = 0; | ||
241 | if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 || | ||
242 | !FD_ISSET(s2, readsp)) { | ||
243 | if (errno != 0) | ||
244 | (void)fprintf(stderr, | ||
245 | "rcmd: select (setting up stderr): %s\n", | ||
246 | strerror(errno)); | ||
247 | else | ||
248 | (void)fprintf(stderr, | ||
249 | "select: protocol failure in circuit setup\n"); | ||
250 | (void)close(s2); | ||
251 | goto bad; | ||
252 | } | ||
253 | s3 = accept(s2, (struct sockaddr *)&from, &len); | ||
254 | /* | ||
255 | * XXX careful for ftp bounce attacks. If discovered, shut them | ||
256 | * down and check for the real auxiliary channel to connect. | ||
257 | */ | ||
258 | switch (from.ss_family) { | ||
259 | case AF_INET: | ||
260 | case AF_INET6: | ||
261 | if (getnameinfo((struct sockaddr *)&from, len, | ||
262 | NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 && | ||
263 | atoi(num) != 20) { | ||
264 | break; | ||
265 | } | ||
266 | close(s3); | ||
267 | goto again; | ||
268 | default: | ||
269 | break; | ||
270 | } | ||
271 | (void)close(s2); | ||
272 | if (s3 < 0) { | ||
273 | (void)fprintf(stderr, | ||
274 | "rcmd: accept: %s\n", strerror(errno)); | ||
275 | lport = 0; | ||
276 | goto bad; | ||
277 | } | ||
278 | *fd2p = s3; | ||
279 | switch (from.ss_family) { | ||
280 | case AF_INET: | ||
281 | case AF_INET6: | ||
282 | if (getnameinfo((struct sockaddr *)&from, len, | ||
283 | NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 || | ||
284 | (atoi(num) >= IPPORT_RESERVED || | ||
285 | atoi(num) < IPPORT_RESERVED / 2)) { | ||
286 | (void)fprintf(stderr, | ||
287 | "socket: protocol failure in circuit setup.\n"); | ||
288 | goto bad2; | ||
289 | } | ||
290 | break; | ||
291 | default: | ||
292 | break; | ||
293 | } | ||
294 | } | ||
295 | (void)write(s, locuser, strlen(locuser)+1); | ||
296 | (void)write(s, remuser, strlen(remuser)+1); | ||
297 | (void)write(s, cmd, strlen(cmd)+1); | ||
298 | if (read(s, &c, 1) != 1) { | ||
299 | (void)fprintf(stderr, | ||
300 | "rcmd: %s: %s\n", *ahost, strerror(errno)); | ||
301 | goto bad2; | ||
302 | } | ||
303 | if (c != 0) { | ||
304 | while (read(s, &c, 1) == 1) { | ||
305 | (void)write(STDERR_FILENO, &c, 1); | ||
306 | if (c == '\n') | ||
307 | break; | ||
308 | } | ||
309 | goto bad2; | ||
310 | } | ||
311 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
312 | free(readsp); | ||
313 | return (s); | ||
314 | bad2: | ||
315 | if (lport) | ||
316 | (void)close(*fd2p); | ||
317 | bad: | ||
318 | if (readsp) | ||
319 | free(readsp); | ||
320 | (void)close(s); | ||
321 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
322 | return (-1); | ||
323 | } | ||
324 | |||
325 | int __check_rhosts_file = 1; | ||
326 | char *__rcmd_errstr; | ||
327 | |||
328 | int | ||
329 | ruserok(rhost, superuser, ruser, luser) | ||
330 | const char *rhost, *ruser, *luser; | ||
331 | int superuser; | ||
332 | { | ||
333 | struct addrinfo hints, *res, *r; | ||
334 | int error; | ||
335 | |||
336 | memset(&hints, 0, sizeof(hints)); | ||
337 | hints.ai_family = PF_UNSPEC; | ||
338 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
339 | error = getaddrinfo(rhost, "0", &hints, &res); | ||
340 | if (error) | ||
341 | return (-1); | ||
342 | |||
343 | for (r = res; r; r = r->ai_next) { | ||
344 | if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, | ||
345 | luser) == 0) { | ||
346 | freeaddrinfo(res); | ||
347 | return (0); | ||
348 | } | ||
349 | } | ||
350 | freeaddrinfo(res); | ||
351 | return (-1); | ||
352 | } | ||
353 | |||
354 | /* | ||
355 | * New .rhosts strategy: We are passed an ip address. We spin through | ||
356 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | ||
357 | * has ip addresses, we don't have to trust a nameserver. When it | ||
358 | * contains hostnames, we spin through the list of addresses the nameserver | ||
359 | * gives us and look for a match. | ||
360 | * | ||
361 | * Returns 0 if ok, -1 if not ok. | ||
362 | */ | ||
363 | int | ||
364 | iruserok(raddr, superuser, ruser, luser) | ||
365 | u_int32_t raddr; | ||
366 | int superuser; | ||
367 | const char *ruser, *luser; | ||
368 | { | ||
369 | struct sockaddr_in sin; | ||
370 | |||
371 | memset(&sin, 0, sizeof(sin)); | ||
372 | sin.sin_family = AF_INET; | ||
373 | sin.sin_len = sizeof(struct sockaddr_in); | ||
374 | memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); | ||
375 | return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser, | ||
376 | luser); | ||
377 | } | ||
378 | |||
379 | int | ||
380 | iruserok_sa(raddr, rlen, superuser, ruser, luser) | ||
381 | const void *raddr; | ||
382 | int rlen; | ||
383 | int superuser; | ||
384 | const char *ruser, *luser; | ||
385 | { | ||
386 | struct sockaddr *sa; | ||
387 | register char *cp; | ||
388 | struct stat sbuf; | ||
389 | struct passwd *pwd; | ||
390 | FILE *hostf; | ||
391 | uid_t uid; | ||
392 | int first; | ||
393 | char pbuf[MAXPATHLEN]; | ||
394 | |||
395 | sa = (struct sockaddr *)raddr; | ||
396 | first = 1; | ||
397 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | ||
398 | again: | ||
399 | if (hostf) { | ||
400 | if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) { | ||
401 | (void)fclose(hostf); | ||
402 | return (0); | ||
403 | } | ||
404 | (void)fclose(hostf); | ||
405 | } | ||
406 | if (first == 1 && (__check_rhosts_file || superuser)) { | ||
407 | first = 0; | ||
408 | if ((pwd = getpwnam(luser)) == NULL) | ||
409 | return (-1); | ||
410 | snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir); | ||
411 | |||
412 | /* | ||
413 | * Change effective uid while opening .rhosts. If root and | ||
414 | * reading an NFS mounted file system, can't read files that | ||
415 | * are protected read/write owner only. | ||
416 | */ | ||
417 | uid = geteuid(); | ||
418 | (void)seteuid(pwd->pw_uid); | ||
419 | hostf = fopen(pbuf, "r"); | ||
420 | (void)seteuid(uid); | ||
421 | |||
422 | if (hostf == NULL) | ||
423 | return (-1); | ||
424 | /* | ||
425 | * If not a regular file, or is owned by someone other than | ||
426 | * user or root or if writeable by anyone but the owner, quit. | ||
427 | */ | ||
428 | cp = NULL; | ||
429 | if (lstat(pbuf, &sbuf) < 0) | ||
430 | cp = ".rhosts lstat failed"; | ||
431 | else if (!S_ISREG(sbuf.st_mode)) | ||
432 | cp = ".rhosts not regular file"; | ||
433 | else if (fstat(fileno(hostf), &sbuf) < 0) | ||
434 | cp = ".rhosts fstat failed"; | ||
435 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | ||
436 | cp = "bad .rhosts owner"; | ||
437 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | ||
438 | cp = ".rhosts writeable by other than owner"; | ||
439 | /* If there were any problems, quit. */ | ||
440 | if (cp) { | ||
441 | __rcmd_errstr = cp; | ||
442 | (void)fclose(hostf); | ||
443 | return (-1); | ||
444 | } | ||
445 | goto again; | ||
446 | } | ||
447 | return (-1); | ||
448 | } | ||
449 | |||
450 | /* | ||
451 | * XXX | ||
452 | * Don't make static, used by lpd(8). | ||
453 | * | ||
454 | * Returns 0 if ok, -1 if not ok. | ||
455 | */ | ||
456 | int | ||
457 | __ivaliduser(hostf, raddrl, luser, ruser) | ||
458 | FILE *hostf; | ||
459 | in_addr_t raddrl; | ||
460 | const char *luser, *ruser; | ||
461 | { | ||
462 | struct sockaddr_in sin; | ||
463 | |||
464 | memset(&sin, 0, sizeof(sin)); | ||
465 | sin.sin_family = AF_INET; | ||
466 | sin.sin_len = sizeof(struct sockaddr_in); | ||
467 | memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr)); | ||
468 | return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, | ||
469 | luser, ruser); | ||
470 | } | ||
471 | |||
472 | int | ||
473 | __ivaliduser_sa(hostf, raddr, salen, luser, ruser) | ||
474 | FILE *hostf; | ||
475 | struct sockaddr *raddr; | ||
476 | socklen_t salen; | ||
477 | const char *luser, *ruser; | ||
478 | { | ||
479 | register char *user, *p; | ||
480 | char *buf; | ||
481 | const char *auser, *ahost; | ||
482 | int hostok, userok; | ||
483 | char *rhost = (char *)-1; | ||
484 | char domain[MAXHOSTNAMELEN]; | ||
485 | size_t buflen; | ||
486 | |||
487 | getdomainname(domain, sizeof(domain)); | ||
488 | |||
489 | while ((buf = fgetln(hostf, &buflen))) { | ||
490 | p = buf; | ||
491 | if (*p == '#') | ||
492 | continue; | ||
493 | while (*p != '\n' && *p != ' ' && *p != '\t' && p < buf + buflen) { | ||
494 | if (!isprint(*p)) | ||
495 | goto bail; | ||
496 | *p = isupper(*p) ? tolower(*p) : *p; | ||
497 | p++; | ||
498 | } | ||
499 | if (p >= buf + buflen) | ||
500 | continue; | ||
501 | if (*p == ' ' || *p == '\t') { | ||
502 | *p++ = '\0'; | ||
503 | while ((*p == ' ' || *p == '\t') && p < buf + buflen) | ||
504 | p++; | ||
505 | if (p >= buf + buflen) | ||
506 | continue; | ||
507 | user = p; | ||
508 | while (*p != '\n' && *p != ' ' && | ||
509 | *p != '\t' && p < buf + buflen) { | ||
510 | if (!isprint(*p)) | ||
511 | goto bail; | ||
512 | p++; | ||
513 | } | ||
514 | } else | ||
515 | user = p; | ||
516 | *p = '\0'; | ||
517 | |||
518 | if (p == buf) | ||
519 | continue; | ||
520 | |||
521 | auser = *user ? user : luser; | ||
522 | ahost = buf; | ||
523 | |||
524 | if (strlen(ahost) >= MAXHOSTNAMELEN) | ||
525 | continue; | ||
526 | |||
527 | /* | ||
528 | * innetgr() must lookup a hostname (we do not attempt | ||
529 | * to change the semantics so that netgroups may have | ||
530 | * #.#.#.# addresses in the list.) | ||
531 | */ | ||
532 | if (ahost[0] == '+') | ||
533 | switch (ahost[1]) { | ||
534 | case '\0': | ||
535 | hostok = 1; | ||
536 | break; | ||
537 | case '@': | ||
538 | if (rhost == (char *)-1) | ||
539 | rhost = __gethostloop(raddr, salen); | ||
540 | hostok = 0; | ||
541 | if (rhost) | ||
542 | hostok = innetgr(&ahost[2], rhost, | ||
543 | NULL, domain); | ||
544 | break; | ||
545 | default: | ||
546 | hostok = __icheckhost(raddr, salen, &ahost[1]); | ||
547 | break; | ||
548 | } | ||
549 | else if (ahost[0] == '-') | ||
550 | switch (ahost[1]) { | ||
551 | case '\0': | ||
552 | hostok = -1; | ||
553 | break; | ||
554 | case '@': | ||
555 | if (rhost == (char *)-1) | ||
556 | rhost = __gethostloop(raddr, salen); | ||
557 | hostok = 0; | ||
558 | if (rhost) | ||
559 | hostok = -innetgr(&ahost[2], rhost, | ||
560 | NULL, domain); | ||
561 | break; | ||
562 | default: | ||
563 | hostok = -__icheckhost(raddr, salen, &ahost[1]); | ||
564 | break; | ||
565 | } | ||
566 | else | ||
567 | hostok = __icheckhost(raddr, salen, ahost); | ||
568 | |||
569 | |||
570 | if (auser[0] == '+') | ||
571 | switch (auser[1]) { | ||
572 | case '\0': | ||
573 | userok = 1; | ||
574 | break; | ||
575 | case '@': | ||
576 | userok = innetgr(&auser[2], NULL, ruser, | ||
577 | domain); | ||
578 | break; | ||
579 | default: | ||
580 | userok = strcmp(ruser, &auser[1]) ? 0 : 1; | ||
581 | break; | ||
582 | } | ||
583 | else if (auser[0] == '-') | ||
584 | switch (auser[1]) { | ||
585 | case '\0': | ||
586 | userok = -1; | ||
587 | break; | ||
588 | case '@': | ||
589 | userok = -innetgr(&auser[2], NULL, ruser, | ||
590 | domain); | ||
591 | break; | ||
592 | default: | ||
593 | userok = strcmp(ruser, &auser[1]) ? 0 : -1; | ||
594 | break; | ||
595 | } | ||
596 | else | ||
597 | userok = strcmp(ruser, auser) ? 0 : 1; | ||
598 | |||
599 | /* Check if one component did not match */ | ||
600 | if (hostok == 0 || userok == 0) | ||
601 | continue; | ||
602 | |||
603 | /* Check if we got a forbidden pair */ | ||
604 | if (userok <= -1 || hostok <= -1) | ||
605 | return (-1); | ||
606 | |||
607 | /* Check if we got a valid pair */ | ||
608 | if (hostok >= 1 && userok >= 1) | ||
609 | return (0); | ||
610 | } | ||
611 | bail: | ||
612 | return (-1); | ||
613 | } | ||
614 | |||
615 | /* | ||
616 | * Returns "true" if match, 0 if no match. If we do not find any | ||
617 | * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. | ||
618 | * | ||
619 | * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion | ||
620 | * if af == AF_INET6. | ||
621 | */ | ||
622 | static int | ||
623 | __icheckhost(raddr, salen, lhost) | ||
624 | struct sockaddr *raddr; | ||
625 | socklen_t salen; | ||
626 | const char *lhost; | ||
627 | { | ||
628 | struct addrinfo hints, *res, *r; | ||
629 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
630 | int error; | ||
631 | #ifdef NI_WITHSCOPEID | ||
632 | const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; | ||
633 | #else | ||
634 | const int niflags = NI_NUMERICHOST; | ||
635 | #endif | ||
636 | |||
637 | h1[0] = '\0'; | ||
638 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
639 | niflags) != 0) | ||
640 | return (0); | ||
641 | |||
642 | /* Resolve laddr into sockaddr */ | ||
643 | memset(&hints, 0, sizeof(hints)); | ||
644 | hints.ai_family = raddr->sa_family; | ||
645 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
646 | res = NULL; | ||
647 | error = getaddrinfo(lhost, "0", &hints, &res); | ||
648 | if (error) | ||
649 | return (0); | ||
650 | |||
651 | /* | ||
652 | * Try string comparisons between raddr and laddr. | ||
653 | */ | ||
654 | for (r = res; r; r = r->ai_next) { | ||
655 | h2[0] = '\0'; | ||
656 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
657 | NULL, 0, niflags) != 0) | ||
658 | continue; | ||
659 | if (strcmp(h1, h2) == 0) { | ||
660 | freeaddrinfo(res); | ||
661 | return (1); | ||
662 | } | ||
663 | } | ||
664 | |||
665 | /* No match. */ | ||
666 | freeaddrinfo(res); | ||
667 | return (0); | ||
668 | } | ||
669 | |||
670 | /* | ||
671 | * Return the hostname associated with the supplied address. | ||
672 | * Do a reverse lookup as well for security. If a loop cannot | ||
673 | * be found, pack the result of inet_ntoa() into the string. | ||
674 | * | ||
675 | * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion | ||
676 | * if af == AF_INET6. | ||
677 | */ | ||
678 | static char * | ||
679 | __gethostloop(raddr, salen) | ||
680 | struct sockaddr *raddr; | ||
681 | socklen_t salen; | ||
682 | { | ||
683 | static char remotehost[NI_MAXHOST]; | ||
684 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
685 | struct addrinfo hints, *res, *r; | ||
686 | int error; | ||
687 | #ifdef NI_WITHSCOPEID | ||
688 | const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; | ||
689 | #else | ||
690 | const int niflags = NI_NUMERICHOST; | ||
691 | #endif | ||
692 | |||
693 | h1[0] = remotehost[0] = '\0'; | ||
694 | if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), | ||
695 | NULL, 0, NI_NAMEREQD) != 0) | ||
696 | return (NULL); | ||
697 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
698 | niflags) != 0) | ||
699 | return (NULL); | ||
700 | |||
701 | /* | ||
702 | * Look up the name and check that the supplied | ||
703 | * address is in the list | ||
704 | */ | ||
705 | memset(&hints, 0, sizeof(hints)); | ||
706 | hints.ai_family = raddr->sa_family; | ||
707 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
708 | hints.ai_flags = AI_CANONNAME; | ||
709 | res = NULL; | ||
710 | error = getaddrinfo(remotehost, "0", &hints, &res); | ||
711 | if (error) | ||
712 | return (NULL); | ||
713 | |||
714 | for (r = res; r; r = r->ai_next) { | ||
715 | h2[0] = '\0'; | ||
716 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
717 | NULL, 0, niflags) != 0) | ||
718 | continue; | ||
719 | if (strcmp(h1, h2) == 0) { | ||
720 | freeaddrinfo(res); | ||
721 | return (remotehost); | ||
722 | } | ||
723 | } | ||
724 | |||
725 | /* | ||
726 | * either the DNS adminstrator has made a configuration | ||
727 | * mistake, or someone has attempted to spoof us | ||
728 | */ | ||
729 | syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", | ||
730 | h1, res->ai_canonname ? res->ai_canonname : remotehost); | ||
731 | freeaddrinfo(res); | ||
732 | return (NULL); | ||
733 | } | ||