diff options
author | guenther <> | 2009-11-18 07:43:22 +0000 |
---|---|---|
committer | guenther <> | 2009-11-18 07:43:22 +0000 |
commit | e9648d2ce662fb0d79072496682efb0718c01e66 (patch) | |
tree | 69cb2944835feb73c511d3d6141bf71d95163b13 | |
parent | fe6c9378da6c3c76e86e1fa03619eda8e4da9408 (diff) | |
download | openbsd-e9648d2ce662fb0d79072496682efb0718c01e66.tar.gz openbsd-e9648d2ce662fb0d79072496682efb0718c01e66.tar.bz2 openbsd-e9648d2ce662fb0d79072496682efb0718c01e66.zip |
More shrinkage, a bit for ramdisks but mostly for static binaries:
- wrap with #ifndef NO_LOG_BAD_DNS_RESPONSES libc code that uses
p_class() and p_type() for diagnostics, then add that define to
libstub to avoid pulling in res_debug_syms.o
- split rcmd() and ruserok() into separate files, as nothing uses both
- split readdir_r() to its own file
- split syslog_r() from syslog(), as the latter needs localtime(); many
binaries no longer need to pull in all the time code after this; switch
from usleep() to nanosleep() while we're at it
(The profit of analysis of -Wl,-M,--cref output)
Chops 888kB from /bin and /sbin on i386
ok deraadt@, miod@
-rw-r--r-- | src/lib/libc/net/Makefile.inc | 4 | ||||
-rw-r--r-- | src/lib/libc/net/getaddrinfo.c | 4 | ||||
-rw-r--r-- | src/lib/libc/net/gethostnamadr.c | 4 | ||||
-rw-r--r-- | src/lib/libc/net/rcmd.c | 388 | ||||
-rw-r--r-- | src/lib/libc/net/ruserok.c | 437 |
5 files changed, 445 insertions, 392 deletions
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc index 244c266536..50924ec135 100644 --- a/src/lib/libc/net/Makefile.inc +++ b/src/lib/libc/net/Makefile.inc | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.45 2009/08/13 19:54:58 jmc Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.46 2009/11/18 07:43:22 guenther Exp $ |
2 | 2 | ||
3 | # net sources | 3 | # net sources |
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net |
@@ -13,7 +13,7 @@ SRCS+= base64.c freeaddrinfo.c gai_strerror.c getaddrinfo.c gethostnamadr.c \ | |||
13 | inet_lnaof.c inet_makeaddr.c inet_neta.c inet_netof.c inet_network.c \ | 13 | inet_lnaof.c inet_makeaddr.c inet_neta.c inet_netof.c inet_network.c \ |
14 | inet_net_ntop.c inet_net_pton.c inet_ntoa.c inet_ntop.c inet_pton.c \ | 14 | inet_net_ntop.c inet_net_pton.c inet_ntoa.c inet_ntop.c inet_pton.c \ |
15 | linkaddr.c net_addrcmp.c nsap_addr.c \ | 15 | linkaddr.c net_addrcmp.c nsap_addr.c \ |
16 | rcmd.c rresvport.c recv.c res_comp.c res_data.c res_debug.c \ | 16 | rcmd.c ruserok.c rresvport.c recv.c res_comp.c res_data.c res_debug.c \ |
17 | res_debug_syms.c \ | 17 | res_debug_syms.c \ |
18 | res_init.c res_mkquery.c res_query.c res_random.c res_send.c send.c \ | 18 | res_init.c res_mkquery.c res_query.c res_random.c res_send.c send.c \ |
19 | sethostent.c ethers.c rcmdsh.c | 19 | sethostent.c ethers.c rcmdsh.c |
diff --git a/src/lib/libc/net/getaddrinfo.c b/src/lib/libc/net/getaddrinfo.c index 98e247bc44..7040fa7c37 100644 --- a/src/lib/libc/net/getaddrinfo.c +++ b/src/lib/libc/net/getaddrinfo.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: getaddrinfo.c,v 1.70 2009/09/02 19:07:12 fgsch Exp $ */ | 1 | /* $OpenBSD: getaddrinfo.c,v 1.71 2009/11/18 07:43:22 guenther Exp $ */ |
2 | /* $KAME: getaddrinfo.c,v 1.31 2000/08/31 17:36:43 itojun Exp $ */ | 2 | /* $KAME: getaddrinfo.c,v 1.31 2000/08/31 17:36:43 itojun Exp $ */ |
3 | 3 | ||
4 | /* | 4 | /* |
@@ -1062,6 +1062,7 @@ getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, | |||
1062 | continue; | 1062 | continue; |
1063 | } | 1063 | } |
1064 | } else if (type != qtype) { | 1064 | } else if (type != qtype) { |
1065 | #ifndef NO_LOG_BAD_DNS_RESPONSES | ||
1065 | if (type != T_KEY && type != T_SIG) { | 1066 | if (type != T_KEY && type != T_SIG) { |
1066 | struct syslog_data sdata = SYSLOG_DATA_INIT; | 1067 | struct syslog_data sdata = SYSLOG_DATA_INIT; |
1067 | 1068 | ||
@@ -1070,6 +1071,7 @@ getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, | |||
1070 | qname, p_class(C_IN), p_type(qtype), | 1071 | qname, p_class(C_IN), p_type(qtype), |
1071 | p_type(type)); | 1072 | p_type(type)); |
1072 | } | 1073 | } |
1074 | #endif /* NO_LOG_BAD_DNS_RESPONSES */ | ||
1073 | cp += n; | 1075 | cp += n; |
1074 | continue; /* XXX - had_error++ ? */ | 1076 | continue; /* XXX - had_error++ ? */ |
1075 | } | 1077 | } |
diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c index d49b9cdd7d..f4e655eeaf 100644 --- a/src/lib/libc/net/gethostnamadr.c +++ b/src/lib/libc/net/gethostnamadr.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: gethostnamadr.c,v 1.72 2007/10/11 18:36:41 jakob Exp $ */ | 1 | /* $OpenBSD: gethostnamadr.c,v 1.73 2009/11/18 07:43:22 guenther Exp $ */ |
2 | /*- | 2 | /*- |
3 | * Copyright (c) 1985, 1988, 1993 | 3 | * Copyright (c) 1985, 1988, 1993 |
4 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -294,10 +294,12 @@ getanswer(const querybuf *answer, int anslen, const char *qname, int qtype) | |||
294 | continue; | 294 | continue; |
295 | } | 295 | } |
296 | if (type != qtype) { | 296 | if (type != qtype) { |
297 | #ifndef NO_LOG_BAD_DNS_RESPONSES | ||
297 | syslog(LOG_NOTICE|LOG_AUTH, | 298 | syslog(LOG_NOTICE|LOG_AUTH, |
298 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | 299 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", |
299 | qname, p_class(C_IN), p_type(qtype), | 300 | qname, p_class(C_IN), p_type(qtype), |
300 | p_type(type)); | 301 | p_type(type)); |
302 | #endif /* NO_LOG_BAD_DNS_RESPONSES */ | ||
301 | cp += n; | 303 | cp += n; |
302 | continue; /* XXX - had_error++ ? */ | 304 | continue; /* XXX - had_error++ ? */ |
303 | } | 305 | } |
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c index 30ca6710c4..d566e0ca4c 100644 --- a/src/lib/libc/net/rcmd.c +++ b/src/lib/libc/net/rcmd.c | |||
@@ -46,13 +46,6 @@ | |||
46 | #include <string.h> | 46 | #include <string.h> |
47 | #include <syslog.h> | 47 | #include <syslog.h> |
48 | #include <stdlib.h> | 48 | #include <stdlib.h> |
49 | #include <netgroup.h> | ||
50 | |||
51 | int __ivaliduser(FILE *, in_addr_t, const char *, const char *); | ||
52 | int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t, | ||
53 | const char *, const char *); | ||
54 | static int __icheckhost(struct sockaddr *, socklen_t, const char *); | ||
55 | static char *__gethostloop(struct sockaddr *, socklen_t); | ||
56 | 49 | ||
57 | int | 50 | int |
58 | rcmd(char **ahost, int rport, const char *locuser, const char *remuser, | 51 | rcmd(char **ahost, int rport, const char *locuser, const char *remuser, |
@@ -308,384 +301,3 @@ bad: | |||
308 | return (-1); | 301 | return (-1); |
309 | } | 302 | } |
310 | 303 | ||
311 | int __check_rhosts_file = 1; | ||
312 | char *__rcmd_errstr; | ||
313 | |||
314 | int | ||
315 | ruserok(const char *rhost, int superuser, const char *ruser, const char *luser) | ||
316 | { | ||
317 | struct addrinfo hints, *res, *r; | ||
318 | int error; | ||
319 | |||
320 | memset(&hints, 0, sizeof(hints)); | ||
321 | hints.ai_family = PF_UNSPEC; | ||
322 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
323 | error = getaddrinfo(rhost, "0", &hints, &res); | ||
324 | if (error) | ||
325 | return (-1); | ||
326 | |||
327 | for (r = res; r; r = r->ai_next) { | ||
328 | if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, | ||
329 | luser) == 0) { | ||
330 | freeaddrinfo(res); | ||
331 | return (0); | ||
332 | } | ||
333 | } | ||
334 | freeaddrinfo(res); | ||
335 | return (-1); | ||
336 | } | ||
337 | |||
338 | /* | ||
339 | * New .rhosts strategy: We are passed an ip address. We spin through | ||
340 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | ||
341 | * has ip addresses, we don't have to trust a nameserver. When it | ||
342 | * contains hostnames, we spin through the list of addresses the nameserver | ||
343 | * gives us and look for a match. | ||
344 | * | ||
345 | * Returns 0 if ok, -1 if not ok. | ||
346 | */ | ||
347 | int | ||
348 | iruserok(u_int32_t raddr, int superuser, const char *ruser, const char *luser) | ||
349 | { | ||
350 | struct sockaddr_in sin; | ||
351 | |||
352 | memset(&sin, 0, sizeof(sin)); | ||
353 | sin.sin_family = AF_INET; | ||
354 | sin.sin_len = sizeof(struct sockaddr_in); | ||
355 | memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); | ||
356 | return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser, | ||
357 | luser); | ||
358 | } | ||
359 | |||
360 | int | ||
361 | iruserok_sa(const void *raddr, int rlen, int superuser, const char *ruser, | ||
362 | const char *luser) | ||
363 | { | ||
364 | struct sockaddr *sa; | ||
365 | char *cp; | ||
366 | struct stat sbuf; | ||
367 | struct passwd *pwd; | ||
368 | FILE *hostf; | ||
369 | uid_t uid; | ||
370 | int first; | ||
371 | char pbuf[MAXPATHLEN]; | ||
372 | |||
373 | sa = (struct sockaddr *)raddr; | ||
374 | first = 1; | ||
375 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | ||
376 | again: | ||
377 | if (hostf) { | ||
378 | if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) { | ||
379 | (void)fclose(hostf); | ||
380 | return (0); | ||
381 | } | ||
382 | (void)fclose(hostf); | ||
383 | } | ||
384 | if (first == 1 && (__check_rhosts_file || superuser)) { | ||
385 | int len; | ||
386 | |||
387 | first = 0; | ||
388 | if ((pwd = getpwnam(luser)) == NULL) | ||
389 | return (-1); | ||
390 | len = snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir); | ||
391 | if (len < 0 || len >= sizeof pbuf) | ||
392 | return (-1); | ||
393 | |||
394 | /* | ||
395 | * Change effective uid while opening .rhosts. If root and | ||
396 | * reading an NFS mounted file system, can't read files that | ||
397 | * are protected read/write owner only. | ||
398 | */ | ||
399 | uid = geteuid(); | ||
400 | (void)seteuid(pwd->pw_uid); | ||
401 | hostf = fopen(pbuf, "r"); | ||
402 | (void)seteuid(uid); | ||
403 | |||
404 | if (hostf == NULL) | ||
405 | return (-1); | ||
406 | /* | ||
407 | * If not a regular file, or is owned by someone other than | ||
408 | * user or root or if writeable by anyone but the owner, quit. | ||
409 | */ | ||
410 | cp = NULL; | ||
411 | if (lstat(pbuf, &sbuf) < 0) | ||
412 | cp = ".rhosts lstat failed"; | ||
413 | else if (!S_ISREG(sbuf.st_mode)) | ||
414 | cp = ".rhosts not regular file"; | ||
415 | else if (fstat(fileno(hostf), &sbuf) < 0) | ||
416 | cp = ".rhosts fstat failed"; | ||
417 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | ||
418 | cp = "bad .rhosts owner"; | ||
419 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | ||
420 | cp = ".rhosts writable by other than owner"; | ||
421 | /* If there were any problems, quit. */ | ||
422 | if (cp) { | ||
423 | __rcmd_errstr = cp; | ||
424 | (void)fclose(hostf); | ||
425 | return (-1); | ||
426 | } | ||
427 | goto again; | ||
428 | } | ||
429 | return (-1); | ||
430 | } | ||
431 | |||
432 | /* | ||
433 | * XXX | ||
434 | * Don't make static, used by lpd(8). | ||
435 | * | ||
436 | * Returns 0 if ok, -1 if not ok. | ||
437 | */ | ||
438 | int | ||
439 | __ivaliduser(FILE *hostf, in_addr_t raddrl, const char *luser, | ||
440 | const char *ruser) | ||
441 | { | ||
442 | struct sockaddr_in sin; | ||
443 | |||
444 | memset(&sin, 0, sizeof(sin)); | ||
445 | sin.sin_family = AF_INET; | ||
446 | sin.sin_len = sizeof(struct sockaddr_in); | ||
447 | memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr)); | ||
448 | return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, | ||
449 | luser, ruser); | ||
450 | } | ||
451 | |||
452 | int | ||
453 | __ivaliduser_sa(FILE *hostf, struct sockaddr *raddr, socklen_t salen, | ||
454 | const char *luser, const char *ruser) | ||
455 | { | ||
456 | char *user, *p; | ||
457 | char *buf; | ||
458 | const char *auser, *ahost; | ||
459 | int hostok, userok; | ||
460 | char *rhost = (char *)-1; | ||
461 | char domain[MAXHOSTNAMELEN]; | ||
462 | size_t buflen; | ||
463 | |||
464 | getdomainname(domain, sizeof(domain)); | ||
465 | |||
466 | while ((buf = fgetln(hostf, &buflen))) { | ||
467 | p = buf; | ||
468 | if (*p == '#') | ||
469 | continue; | ||
470 | while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') { | ||
471 | if (!isprint(*p)) | ||
472 | goto bail; | ||
473 | *p = isupper(*p) ? tolower(*p) : *p; | ||
474 | p++; | ||
475 | } | ||
476 | if (p >= buf + buflen) | ||
477 | continue; | ||
478 | if (*p == ' ' || *p == '\t') { | ||
479 | *p++ = '\0'; | ||
480 | while (p < buf + buflen && (*p == ' ' || *p == '\t')) | ||
481 | p++; | ||
482 | if (p >= buf + buflen) | ||
483 | continue; | ||
484 | user = p; | ||
485 | while (p < buf + buflen && *p != '\n' && *p != ' ' && | ||
486 | *p != '\t') { | ||
487 | if (!isprint(*p)) | ||
488 | goto bail; | ||
489 | p++; | ||
490 | } | ||
491 | } else | ||
492 | user = p; | ||
493 | *p = '\0'; | ||
494 | |||
495 | if (p == buf) | ||
496 | continue; | ||
497 | |||
498 | auser = *user ? user : luser; | ||
499 | ahost = buf; | ||
500 | |||
501 | if (strlen(ahost) >= MAXHOSTNAMELEN) | ||
502 | continue; | ||
503 | |||
504 | /* | ||
505 | * innetgr() must lookup a hostname (we do not attempt | ||
506 | * to change the semantics so that netgroups may have | ||
507 | * #.#.#.# addresses in the list.) | ||
508 | */ | ||
509 | if (ahost[0] == '+') | ||
510 | switch (ahost[1]) { | ||
511 | case '\0': | ||
512 | hostok = 1; | ||
513 | break; | ||
514 | case '@': | ||
515 | if (rhost == (char *)-1) | ||
516 | rhost = __gethostloop(raddr, salen); | ||
517 | hostok = 0; | ||
518 | if (rhost) | ||
519 | hostok = innetgr(&ahost[2], rhost, | ||
520 | NULL, domain); | ||
521 | break; | ||
522 | default: | ||
523 | hostok = __icheckhost(raddr, salen, &ahost[1]); | ||
524 | break; | ||
525 | } | ||
526 | else if (ahost[0] == '-') | ||
527 | switch (ahost[1]) { | ||
528 | case '\0': | ||
529 | hostok = -1; | ||
530 | break; | ||
531 | case '@': | ||
532 | if (rhost == (char *)-1) | ||
533 | rhost = __gethostloop(raddr, salen); | ||
534 | hostok = 0; | ||
535 | if (rhost) | ||
536 | hostok = -innetgr(&ahost[2], rhost, | ||
537 | NULL, domain); | ||
538 | break; | ||
539 | default: | ||
540 | hostok = -__icheckhost(raddr, salen, &ahost[1]); | ||
541 | break; | ||
542 | } | ||
543 | else | ||
544 | hostok = __icheckhost(raddr, salen, ahost); | ||
545 | |||
546 | |||
547 | if (auser[0] == '+') | ||
548 | switch (auser[1]) { | ||
549 | case '\0': | ||
550 | userok = 1; | ||
551 | break; | ||
552 | case '@': | ||
553 | userok = innetgr(&auser[2], NULL, ruser, | ||
554 | domain); | ||
555 | break; | ||
556 | default: | ||
557 | userok = strcmp(ruser, &auser[1]) ? 0 : 1; | ||
558 | break; | ||
559 | } | ||
560 | else if (auser[0] == '-') | ||
561 | switch (auser[1]) { | ||
562 | case '\0': | ||
563 | userok = -1; | ||
564 | break; | ||
565 | case '@': | ||
566 | userok = -innetgr(&auser[2], NULL, ruser, | ||
567 | domain); | ||
568 | break; | ||
569 | default: | ||
570 | userok = strcmp(ruser, &auser[1]) ? 0 : -1; | ||
571 | break; | ||
572 | } | ||
573 | else | ||
574 | userok = strcmp(ruser, auser) ? 0 : 1; | ||
575 | |||
576 | /* Check if one component did not match */ | ||
577 | if (hostok == 0 || userok == 0) | ||
578 | continue; | ||
579 | |||
580 | /* Check if we got a forbidden pair */ | ||
581 | if (userok <= -1 || hostok <= -1) | ||
582 | return (-1); | ||
583 | |||
584 | /* Check if we got a valid pair */ | ||
585 | if (hostok >= 1 && userok >= 1) | ||
586 | return (0); | ||
587 | } | ||
588 | bail: | ||
589 | return (-1); | ||
590 | } | ||
591 | |||
592 | /* | ||
593 | * Returns "true" if match, 0 if no match. If we do not find any | ||
594 | * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. | ||
595 | */ | ||
596 | static int | ||
597 | __icheckhost(struct sockaddr *raddr, socklen_t salen, const char *lhost) | ||
598 | { | ||
599 | struct addrinfo hints, *res, *r; | ||
600 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
601 | int error; | ||
602 | const int niflags = NI_NUMERICHOST; | ||
603 | |||
604 | h1[0] = '\0'; | ||
605 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
606 | niflags) != 0) | ||
607 | return (0); | ||
608 | |||
609 | /* Resolve laddr into sockaddr */ | ||
610 | memset(&hints, 0, sizeof(hints)); | ||
611 | hints.ai_family = raddr->sa_family; | ||
612 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
613 | res = NULL; | ||
614 | error = getaddrinfo(lhost, "0", &hints, &res); | ||
615 | if (error) | ||
616 | return (0); | ||
617 | |||
618 | /* | ||
619 | * Try string comparisons between raddr and laddr. | ||
620 | */ | ||
621 | for (r = res; r; r = r->ai_next) { | ||
622 | h2[0] = '\0'; | ||
623 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
624 | NULL, 0, niflags) != 0) | ||
625 | continue; | ||
626 | if (strcmp(h1, h2) == 0) { | ||
627 | freeaddrinfo(res); | ||
628 | return (1); | ||
629 | } | ||
630 | } | ||
631 | |||
632 | /* No match. */ | ||
633 | freeaddrinfo(res); | ||
634 | return (0); | ||
635 | } | ||
636 | |||
637 | /* | ||
638 | * Return the hostname associated with the supplied address. | ||
639 | * Do a reverse lookup as well for security. If a loop cannot | ||
640 | * be found, pack the result of inet_ntoa() into the string. | ||
641 | */ | ||
642 | static char * | ||
643 | __gethostloop(struct sockaddr *raddr, socklen_t salen) | ||
644 | { | ||
645 | static char remotehost[NI_MAXHOST]; | ||
646 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
647 | struct addrinfo hints, *res, *r; | ||
648 | int error; | ||
649 | const int niflags = NI_NUMERICHOST; | ||
650 | |||
651 | h1[0] = remotehost[0] = '\0'; | ||
652 | if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), | ||
653 | NULL, 0, NI_NAMEREQD) != 0) | ||
654 | return (NULL); | ||
655 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
656 | niflags) != 0) | ||
657 | return (NULL); | ||
658 | |||
659 | /* | ||
660 | * Look up the name and check that the supplied | ||
661 | * address is in the list | ||
662 | */ | ||
663 | memset(&hints, 0, sizeof(hints)); | ||
664 | hints.ai_family = raddr->sa_family; | ||
665 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
666 | hints.ai_flags = AI_CANONNAME; | ||
667 | res = NULL; | ||
668 | error = getaddrinfo(remotehost, "0", &hints, &res); | ||
669 | if (error) | ||
670 | return (NULL); | ||
671 | |||
672 | for (r = res; r; r = r->ai_next) { | ||
673 | h2[0] = '\0'; | ||
674 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
675 | NULL, 0, niflags) != 0) | ||
676 | continue; | ||
677 | if (strcmp(h1, h2) == 0) { | ||
678 | freeaddrinfo(res); | ||
679 | return (remotehost); | ||
680 | } | ||
681 | } | ||
682 | |||
683 | /* | ||
684 | * either the DNS adminstrator has made a configuration | ||
685 | * mistake, or someone has attempted to spoof us | ||
686 | */ | ||
687 | syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", | ||
688 | h1, res->ai_canonname ? res->ai_canonname : remotehost); | ||
689 | freeaddrinfo(res); | ||
690 | return (NULL); | ||
691 | } | ||
diff --git a/src/lib/libc/net/ruserok.c b/src/lib/libc/net/ruserok.c new file mode 100644 index 0000000000..5ef078ac95 --- /dev/null +++ b/src/lib/libc/net/ruserok.c | |||
@@ -0,0 +1,437 @@ | |||
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. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/param.h> | ||
32 | #include <sys/socket.h> | ||
33 | #include <sys/stat.h> | ||
34 | |||
35 | #include <netinet/in.h> | ||
36 | #include <arpa/inet.h> | ||
37 | |||
38 | #include <signal.h> | ||
39 | #include <fcntl.h> | ||
40 | #include <netdb.h> | ||
41 | #include <unistd.h> | ||
42 | #include <pwd.h> | ||
43 | #include <errno.h> | ||
44 | #include <stdio.h> | ||
45 | #include <ctype.h> | ||
46 | #include <string.h> | ||
47 | #include <syslog.h> | ||
48 | #include <stdlib.h> | ||
49 | #include <netgroup.h> | ||
50 | |||
51 | int __ivaliduser(FILE *, in_addr_t, const char *, const char *); | ||
52 | int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t, | ||
53 | const char *, const char *); | ||
54 | static int __icheckhost(struct sockaddr *, socklen_t, const char *); | ||
55 | static char *__gethostloop(struct sockaddr *, socklen_t); | ||
56 | |||
57 | int __check_rhosts_file = 1; | ||
58 | char *__rcmd_errstr; | ||
59 | |||
60 | int | ||
61 | ruserok(const char *rhost, int superuser, const char *ruser, const char *luser) | ||
62 | { | ||
63 | struct addrinfo hints, *res, *r; | ||
64 | int error; | ||
65 | |||
66 | memset(&hints, 0, sizeof(hints)); | ||
67 | hints.ai_family = PF_UNSPEC; | ||
68 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
69 | error = getaddrinfo(rhost, "0", &hints, &res); | ||
70 | if (error) | ||
71 | return (-1); | ||
72 | |||
73 | for (r = res; r; r = r->ai_next) { | ||
74 | if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, | ||
75 | luser) == 0) { | ||
76 | freeaddrinfo(res); | ||
77 | return (0); | ||
78 | } | ||
79 | } | ||
80 | freeaddrinfo(res); | ||
81 | return (-1); | ||
82 | } | ||
83 | |||
84 | /* | ||
85 | * New .rhosts strategy: We are passed an ip address. We spin through | ||
86 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | ||
87 | * has ip addresses, we don't have to trust a nameserver. When it | ||
88 | * contains hostnames, we spin through the list of addresses the nameserver | ||
89 | * gives us and look for a match. | ||
90 | * | ||
91 | * Returns 0 if ok, -1 if not ok. | ||
92 | */ | ||
93 | int | ||
94 | iruserok(u_int32_t raddr, int superuser, const char *ruser, const char *luser) | ||
95 | { | ||
96 | struct sockaddr_in sin; | ||
97 | |||
98 | memset(&sin, 0, sizeof(sin)); | ||
99 | sin.sin_family = AF_INET; | ||
100 | sin.sin_len = sizeof(struct sockaddr_in); | ||
101 | memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); | ||
102 | return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser, | ||
103 | luser); | ||
104 | } | ||
105 | |||
106 | int | ||
107 | iruserok_sa(const void *raddr, int rlen, int superuser, const char *ruser, | ||
108 | const char *luser) | ||
109 | { | ||
110 | struct sockaddr *sa; | ||
111 | char *cp; | ||
112 | struct stat sbuf; | ||
113 | struct passwd *pwd; | ||
114 | FILE *hostf; | ||
115 | uid_t uid; | ||
116 | int first; | ||
117 | char pbuf[MAXPATHLEN]; | ||
118 | |||
119 | sa = (struct sockaddr *)raddr; | ||
120 | first = 1; | ||
121 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | ||
122 | again: | ||
123 | if (hostf) { | ||
124 | if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) { | ||
125 | (void)fclose(hostf); | ||
126 | return (0); | ||
127 | } | ||
128 | (void)fclose(hostf); | ||
129 | } | ||
130 | if (first == 1 && (__check_rhosts_file || superuser)) { | ||
131 | int len; | ||
132 | |||
133 | first = 0; | ||
134 | if ((pwd = getpwnam(luser)) == NULL) | ||
135 | return (-1); | ||
136 | len = snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir); | ||
137 | if (len < 0 || len >= sizeof pbuf) | ||
138 | return (-1); | ||
139 | |||
140 | /* | ||
141 | * Change effective uid while opening .rhosts. If root and | ||
142 | * reading an NFS mounted file system, can't read files that | ||
143 | * are protected read/write owner only. | ||
144 | */ | ||
145 | uid = geteuid(); | ||
146 | (void)seteuid(pwd->pw_uid); | ||
147 | hostf = fopen(pbuf, "r"); | ||
148 | (void)seteuid(uid); | ||
149 | |||
150 | if (hostf == NULL) | ||
151 | return (-1); | ||
152 | /* | ||
153 | * If not a regular file, or is owned by someone other than | ||
154 | * user or root or if writeable by anyone but the owner, quit. | ||
155 | */ | ||
156 | cp = NULL; | ||
157 | if (lstat(pbuf, &sbuf) < 0) | ||
158 | cp = ".rhosts lstat failed"; | ||
159 | else if (!S_ISREG(sbuf.st_mode)) | ||
160 | cp = ".rhosts not regular file"; | ||
161 | else if (fstat(fileno(hostf), &sbuf) < 0) | ||
162 | cp = ".rhosts fstat failed"; | ||
163 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | ||
164 | cp = "bad .rhosts owner"; | ||
165 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | ||
166 | cp = ".rhosts writable by other than owner"; | ||
167 | /* If there were any problems, quit. */ | ||
168 | if (cp) { | ||
169 | __rcmd_errstr = cp; | ||
170 | (void)fclose(hostf); | ||
171 | return (-1); | ||
172 | } | ||
173 | goto again; | ||
174 | } | ||
175 | return (-1); | ||
176 | } | ||
177 | |||
178 | /* | ||
179 | * XXX | ||
180 | * Don't make static, used by lpd(8). | ||
181 | * | ||
182 | * Returns 0 if ok, -1 if not ok. | ||
183 | */ | ||
184 | int | ||
185 | __ivaliduser(FILE *hostf, in_addr_t raddrl, const char *luser, | ||
186 | const char *ruser) | ||
187 | { | ||
188 | struct sockaddr_in sin; | ||
189 | |||
190 | memset(&sin, 0, sizeof(sin)); | ||
191 | sin.sin_family = AF_INET; | ||
192 | sin.sin_len = sizeof(struct sockaddr_in); | ||
193 | memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr)); | ||
194 | return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, | ||
195 | luser, ruser); | ||
196 | } | ||
197 | |||
198 | int | ||
199 | __ivaliduser_sa(FILE *hostf, struct sockaddr *raddr, socklen_t salen, | ||
200 | const char *luser, const char *ruser) | ||
201 | { | ||
202 | char *user, *p; | ||
203 | char *buf; | ||
204 | const char *auser, *ahost; | ||
205 | int hostok, userok; | ||
206 | char *rhost = (char *)-1; | ||
207 | char domain[MAXHOSTNAMELEN]; | ||
208 | size_t buflen; | ||
209 | |||
210 | getdomainname(domain, sizeof(domain)); | ||
211 | |||
212 | while ((buf = fgetln(hostf, &buflen))) { | ||
213 | p = buf; | ||
214 | if (*p == '#') | ||
215 | continue; | ||
216 | while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') { | ||
217 | if (!isprint(*p)) | ||
218 | goto bail; | ||
219 | *p = isupper(*p) ? tolower(*p) : *p; | ||
220 | p++; | ||
221 | } | ||
222 | if (p >= buf + buflen) | ||
223 | continue; | ||
224 | if (*p == ' ' || *p == '\t') { | ||
225 | *p++ = '\0'; | ||
226 | while (p < buf + buflen && (*p == ' ' || *p == '\t')) | ||
227 | p++; | ||
228 | if (p >= buf + buflen) | ||
229 | continue; | ||
230 | user = p; | ||
231 | while (p < buf + buflen && *p != '\n' && *p != ' ' && | ||
232 | *p != '\t') { | ||
233 | if (!isprint(*p)) | ||
234 | goto bail; | ||
235 | p++; | ||
236 | } | ||
237 | } else | ||
238 | user = p; | ||
239 | *p = '\0'; | ||
240 | |||
241 | if (p == buf) | ||
242 | continue; | ||
243 | |||
244 | auser = *user ? user : luser; | ||
245 | ahost = buf; | ||
246 | |||
247 | if (strlen(ahost) >= MAXHOSTNAMELEN) | ||
248 | continue; | ||
249 | |||
250 | /* | ||
251 | * innetgr() must lookup a hostname (we do not attempt | ||
252 | * to change the semantics so that netgroups may have | ||
253 | * #.#.#.# addresses in the list.) | ||
254 | */ | ||
255 | if (ahost[0] == '+') | ||
256 | switch (ahost[1]) { | ||
257 | case '\0': | ||
258 | hostok = 1; | ||
259 | break; | ||
260 | case '@': | ||
261 | if (rhost == (char *)-1) | ||
262 | rhost = __gethostloop(raddr, salen); | ||
263 | hostok = 0; | ||
264 | if (rhost) | ||
265 | hostok = innetgr(&ahost[2], rhost, | ||
266 | NULL, domain); | ||
267 | break; | ||
268 | default: | ||
269 | hostok = __icheckhost(raddr, salen, &ahost[1]); | ||
270 | break; | ||
271 | } | ||
272 | else if (ahost[0] == '-') | ||
273 | switch (ahost[1]) { | ||
274 | case '\0': | ||
275 | hostok = -1; | ||
276 | break; | ||
277 | case '@': | ||
278 | if (rhost == (char *)-1) | ||
279 | rhost = __gethostloop(raddr, salen); | ||
280 | hostok = 0; | ||
281 | if (rhost) | ||
282 | hostok = -innetgr(&ahost[2], rhost, | ||
283 | NULL, domain); | ||
284 | break; | ||
285 | default: | ||
286 | hostok = -__icheckhost(raddr, salen, &ahost[1]); | ||
287 | break; | ||
288 | } | ||
289 | else | ||
290 | hostok = __icheckhost(raddr, salen, ahost); | ||
291 | |||
292 | |||
293 | if (auser[0] == '+') | ||
294 | switch (auser[1]) { | ||
295 | case '\0': | ||
296 | userok = 1; | ||
297 | break; | ||
298 | case '@': | ||
299 | userok = innetgr(&auser[2], NULL, ruser, | ||
300 | domain); | ||
301 | break; | ||
302 | default: | ||
303 | userok = strcmp(ruser, &auser[1]) ? 0 : 1; | ||
304 | break; | ||
305 | } | ||
306 | else if (auser[0] == '-') | ||
307 | switch (auser[1]) { | ||
308 | case '\0': | ||
309 | userok = -1; | ||
310 | break; | ||
311 | case '@': | ||
312 | userok = -innetgr(&auser[2], NULL, ruser, | ||
313 | domain); | ||
314 | break; | ||
315 | default: | ||
316 | userok = strcmp(ruser, &auser[1]) ? 0 : -1; | ||
317 | break; | ||
318 | } | ||
319 | else | ||
320 | userok = strcmp(ruser, auser) ? 0 : 1; | ||
321 | |||
322 | /* Check if one component did not match */ | ||
323 | if (hostok == 0 || userok == 0) | ||
324 | continue; | ||
325 | |||
326 | /* Check if we got a forbidden pair */ | ||
327 | if (userok <= -1 || hostok <= -1) | ||
328 | return (-1); | ||
329 | |||
330 | /* Check if we got a valid pair */ | ||
331 | if (hostok >= 1 && userok >= 1) | ||
332 | return (0); | ||
333 | } | ||
334 | bail: | ||
335 | return (-1); | ||
336 | } | ||
337 | |||
338 | /* | ||
339 | * Returns "true" if match, 0 if no match. If we do not find any | ||
340 | * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. | ||
341 | */ | ||
342 | static int | ||
343 | __icheckhost(struct sockaddr *raddr, socklen_t salen, const char *lhost) | ||
344 | { | ||
345 | struct addrinfo hints, *res, *r; | ||
346 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
347 | int error; | ||
348 | const int niflags = NI_NUMERICHOST; | ||
349 | |||
350 | h1[0] = '\0'; | ||
351 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
352 | niflags) != 0) | ||
353 | return (0); | ||
354 | |||
355 | /* Resolve laddr into sockaddr */ | ||
356 | memset(&hints, 0, sizeof(hints)); | ||
357 | hints.ai_family = raddr->sa_family; | ||
358 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
359 | res = NULL; | ||
360 | error = getaddrinfo(lhost, "0", &hints, &res); | ||
361 | if (error) | ||
362 | return (0); | ||
363 | |||
364 | /* | ||
365 | * Try string comparisons between raddr and laddr. | ||
366 | */ | ||
367 | for (r = res; r; r = r->ai_next) { | ||
368 | h2[0] = '\0'; | ||
369 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
370 | NULL, 0, niflags) != 0) | ||
371 | continue; | ||
372 | if (strcmp(h1, h2) == 0) { | ||
373 | freeaddrinfo(res); | ||
374 | return (1); | ||
375 | } | ||
376 | } | ||
377 | |||
378 | /* No match. */ | ||
379 | freeaddrinfo(res); | ||
380 | return (0); | ||
381 | } | ||
382 | |||
383 | /* | ||
384 | * Return the hostname associated with the supplied address. | ||
385 | * Do a reverse lookup as well for security. If a loop cannot | ||
386 | * be found, pack the result of inet_ntoa() into the string. | ||
387 | */ | ||
388 | static char * | ||
389 | __gethostloop(struct sockaddr *raddr, socklen_t salen) | ||
390 | { | ||
391 | static char remotehost[NI_MAXHOST]; | ||
392 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
393 | struct addrinfo hints, *res, *r; | ||
394 | int error; | ||
395 | const int niflags = NI_NUMERICHOST; | ||
396 | |||
397 | h1[0] = remotehost[0] = '\0'; | ||
398 | if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), | ||
399 | NULL, 0, NI_NAMEREQD) != 0) | ||
400 | return (NULL); | ||
401 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
402 | niflags) != 0) | ||
403 | return (NULL); | ||
404 | |||
405 | /* | ||
406 | * Look up the name and check that the supplied | ||
407 | * address is in the list | ||
408 | */ | ||
409 | memset(&hints, 0, sizeof(hints)); | ||
410 | hints.ai_family = raddr->sa_family; | ||
411 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
412 | hints.ai_flags = AI_CANONNAME; | ||
413 | res = NULL; | ||
414 | error = getaddrinfo(remotehost, "0", &hints, &res); | ||
415 | if (error) | ||
416 | return (NULL); | ||
417 | |||
418 | for (r = res; r; r = r->ai_next) { | ||
419 | h2[0] = '\0'; | ||
420 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
421 | NULL, 0, niflags) != 0) | ||
422 | continue; | ||
423 | if (strcmp(h1, h2) == 0) { | ||
424 | freeaddrinfo(res); | ||
425 | return (remotehost); | ||
426 | } | ||
427 | } | ||
428 | |||
429 | /* | ||
430 | * either the DNS adminstrator has made a configuration | ||
431 | * mistake, or someone has attempted to spoof us | ||
432 | */ | ||
433 | syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", | ||
434 | h1, res->ai_canonname ? res->ai_canonname : remotehost); | ||
435 | freeaddrinfo(res); | ||
436 | return (NULL); | ||
437 | } | ||