1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
|
/*
* support/export/client.c
*
* Maintain list of nfsd clients.
*
* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <netdb.h>
#include <errno.h>
#include "sockaddr.h"
#include "misc.h"
#include "nfslib.h"
#include "exportfs.h"
/* netgroup stuff never seems to be defined in any header file. Linux is
* not alone in this.
*/
#if !defined(__GLIBC__) || __GLIBC__ < 2
extern int innetgr(char *netgr, char *host, char *, char *);
#endif
static char *add_name(char *old, const char *add);
nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
static void
init_addrlist(nfs_client *clp, const struct addrinfo *ai)
{
int i;
if (ai == NULL)
return;
for (i = 0; (ai != NULL) && (i < NFSCLNT_ADDRMAX); i++) {
set_addrlist(clp, i, ai->ai_addr);
ai = ai->ai_next;
}
clp->m_naddr = i;
}
static void
client_free(nfs_client *clp)
{
free(clp->m_hostname);
free(clp);
}
static int
init_netmask4(nfs_client *clp, const char *slash)
{
struct sockaddr_in sin = {
.sin_family = AF_INET,
};
uint32_t shift;
/*
* Decide what kind of netmask was specified. If there's
* no '/' present, assume the netmask is all ones. If
* there is a '/' and at least one '.', look for a spelled-
* out netmask. Otherwise, assume it was a prefixlen.
*/
if (slash == NULL)
shift = 0;
else {
unsigned long prefixlen;
if (strchr(slash + 1, '.') != NULL) {
if (inet_pton(AF_INET, slash + 1,
&sin.sin_addr.s_addr) == 0)
goto out_badmask;
set_addrlist_in(clp, 1, &sin);
return 1;
} else {
char *endptr;
prefixlen = strtoul(slash + 1, &endptr, 10);
if (*endptr != '\0' && prefixlen != ULONG_MAX &&
errno != ERANGE)
goto out_badprefix;
}
if (prefixlen > 32)
goto out_badprefix;
shift = 32 - (uint32_t)prefixlen;
}
/*
* Now construct the full netmask bitmask in a sockaddr_in,
* and plant it in the nfs_client record.
*/
sin.sin_addr.s_addr = htonl((uint32_t)~0 << shift);
set_addrlist_in(clp, 1, &sin);
return 1;
out_badmask:
xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
return 0;
out_badprefix:
xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
return 0;
}
#ifdef IPV6_SUPPORTED
static int
init_netmask6(nfs_client *clp, const char *slash)
{
struct sockaddr_in6 sin6 = {
.sin6_family = AF_INET6,
};
unsigned long prefixlen;
uint32_t shift;
int i;
/*
* Decide what kind of netmask was specified. If there's
* no '/' present, assume the netmask is all ones. If
* there is a '/' and at least one ':', look for a spelled-
* out netmask. Otherwise, assume it was a prefixlen.
*/
if (slash == NULL)
prefixlen = 128;
else {
if (strchr(slash + 1, ':') != NULL) {
if (!inet_pton(AF_INET6, slash + 1, &sin6.sin6_addr))
goto out_badmask;
set_addrlist_in6(clp, 1, &sin6);
return 1;
} else {
char *endptr;
prefixlen = strtoul(slash + 1, &endptr, 10);
if (*endptr != '\0' && prefixlen != ULONG_MAX &&
errno != ERANGE)
goto out_badprefix;
}
if (prefixlen > 128)
goto out_badprefix;
}
/*
* Now construct the full netmask bitmask in a sockaddr_in6,
* and plant it in the nfs_client record.
*/
for (i = 0; prefixlen > 32; i++) {
sin6.sin6_addr.s6_addr32[i] = 0xffffffff;
prefixlen -= 32;
}
shift = 32 - (uint32_t)prefixlen;
sin6.sin6_addr.s6_addr32[i] = htonl((uint32_t)~0 << shift);
set_addrlist_in6(clp, 1, &sin6);
return 1;
out_badmask:
xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
return 0;
out_badprefix:
xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
return 0;
}
#else /* IPV6_SUPPORTED */
static int
init_netmask6(nfs_client *UNUSED(clp), const char *UNUSED(slash))
{
return 0;
}
#endif /* IPV6_SUPPORTED */
/*
* Parse the network mask for M_SUBNETWORK type clients.
*
* Return TRUE if successful, or FALSE if some error occurred.
*/
static int
init_subnetwork(nfs_client *clp)
{
struct addrinfo *ai;
sa_family_t family;
int result = 0;
char *slash;
slash = strchr(clp->m_hostname, '/');
if (slash != NULL) {
*slash = '\0';
ai = host_pton(clp->m_hostname);
*slash = '/';
} else
ai = host_pton(clp->m_hostname);
if (ai == NULL) {
xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
return result;
}
set_addrlist(clp, 0, ai->ai_addr);
family = ai->ai_addr->sa_family;
nfs_freeaddrinfo(ai);
switch (family) {
case AF_INET:
result = init_netmask4(clp, slash);
break;
case AF_INET6:
result = init_netmask6(clp, slash);
break;
default:
xlog(L_ERROR, "Unsupported address family for %s",
clp->m_hostname);
}
return result;
}
static int
client_init(nfs_client *clp, const char *hname, const struct addrinfo *ai)
{
clp->m_hostname = strdup(hname);
if (clp->m_hostname == NULL)
return 0;
clp->m_exported = 0;
clp->m_count = 0;
clp->m_naddr = 0;
if (clp->m_type == MCL_SUBNETWORK)
return init_subnetwork(clp);
init_addrlist(clp, ai);
return 1;
}
static void
client_add(nfs_client *clp)
{
nfs_client **cpp;
cpp = &clientlist[clp->m_type];
while (*cpp != NULL)
cpp = &((*cpp)->m_next);
clp->m_next = NULL;
*cpp = clp;
}
/**
* client_lookup - look for @hname in our list of cached nfs_clients
* @hname: '\0'-terminated ASCII string containing hostname to look for
* @canonical: if set, @hname is known to be canonical DNS name
*
* Returns pointer to a matching or freshly created nfs_client. NULL
* is returned if some problem occurs.
*/
nfs_client *
client_lookup(char *hname, int canonical)
{
nfs_client *clp = NULL;
int htype;
struct addrinfo *ai = NULL;
htype = client_gettype(hname);
if (htype == MCL_FQDN && !canonical) {
ai = host_addrinfo(hname);
if (!ai) {
xlog(L_WARNING, "Failed to resolve %s", hname);
goto out;
}
hname = ai->ai_canonname;
for (clp = clientlist[htype]; clp; clp = clp->m_next)
if (client_check(clp, ai))
break;
} else {
for (clp = clientlist[htype]; clp; clp = clp->m_next) {
if (strcasecmp(hname, clp->m_hostname)==0)
break;
}
}
if (clp == NULL) {
clp = calloc(1, sizeof(*clp));
if (clp == NULL)
goto out;
clp->m_type = htype;
if (!client_init(clp, hname, NULL)) {
client_free(clp);
clp = NULL;
goto out;
}
client_add(clp);
}
if (htype == MCL_FQDN && clp->m_naddr == 0)
init_addrlist(clp, ai);
out:
nfs_freeaddrinfo(ai);
return clp;
}
/**
* client_dup - create a copy of an nfs_client
* @clp: pointer to nfs_client to copy
* @ai: pointer to addrinfo used to initialize the new client's addrlist
*
* Returns a dynamically allocated nfs_client if successful, or
* NULL if some problem occurs. Caller must free the returned
* nfs_client with free(3).
*/
nfs_client *
client_dup(const nfs_client *clp, const struct addrinfo *ai)
{
nfs_client *new;
new = (nfs_client *)malloc(sizeof(*new));
if (new == NULL)
return NULL;
memcpy(new, clp, sizeof(*new));
new->m_type = MCL_FQDN;
new->m_hostname = NULL;
if (!client_init(new, ai->ai_canonname, ai)) {
client_free(new);
return NULL;
}
client_add(new);
return new;
}
/**
* client_release - drop a reference to an nfs_client record
*
*/
void
client_release(nfs_client *clp)
{
if (clp->m_count <= 0)
xlog(L_FATAL, "client_free: m_count <= 0!");
clp->m_count--;
}
/**
* client_freeall - deallocate all nfs_client records
*
*/
void
client_freeall(void)
{
nfs_client *clp, **head;
int i;
for (i = 0; i < MCL_MAXTYPES; i++) {
head = clientlist + i;
while (*head) {
*head = (clp = *head)->m_next;
client_free(clp);
}
}
}
/**
* client_resolve - look up an IP address
* @sap: pointer to socket address to resolve
*
* Returns an addrinfo structure, or NULL if some problem occurred.
* Caller must free the result with freeaddrinfo(3).
*/
struct addrinfo *
client_resolve(const struct sockaddr *sap)
{
struct addrinfo *ai = NULL;
if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
ai = host_reliable_addrinfo(sap);
if (ai == NULL)
ai = host_numeric_addrinfo(sap);
return ai;
}
/**
* client_compose - Make a list of cached hostnames that match an IP address
* @ai: pointer to addrinfo containing IP address information to match
*
* Gather all known client hostnames that match the IP address, and sort
* the result into a comma-separated list.
*
* Returns a '\0'-terminated ASCII string containing a comma-separated
* sorted list of client hostnames, or NULL if no client records matched
* the IP address or memory could not be allocated. Caller must free the
* returned string with free(3).
*/
char *
client_compose(const struct addrinfo *ai)
{
char *name = NULL;
int i;
for (i = 0 ; i < MCL_MAXTYPES; i++) {
nfs_client *clp;
for (clp = clientlist[i]; clp ; clp = clp->m_next) {
if (!client_check(clp, ai))
continue;
name = add_name(name, clp->m_hostname);
}
}
return name;
}
/**
* client_member - check if @name is contained in the list @client
* @client: '\0'-terminated ASCII string containing
* comma-separated list of hostnames
* @name: '\0'-terminated ASCII string containing hostname to look for
*
* Returns 1 if @name was found in @client, otherwise zero is returned.
*/
int
client_member(const char *client, const char *name)
{
size_t l = strlen(name);
while (*client) {
if (strncmp(client, name, l) == 0 &&
(client[l] == ',' || client[l] == '\0'))
return 1;
client = strchr(client, ',');
if (client == NULL)
return 0;
client++;
}
return 0;
}
static int
name_cmp(const char *a, const char *b)
{
/* compare strings a and b, but only upto ',' in a */
while (*a && *b && *a != ',' && *a == *b)
a++, b++;
if (!*b && (!*a || *a == ','))
return 0;
if (!*b) return 1;
if (!*a || *a == ',') return -1;
return *a - *b;
}
static char *
add_name(char *old, const char *add)
{
size_t len = strlen(add) + 2;
char *new;
char *cp;
if (old) len += strlen(old);
new = malloc(len);
if (!new) {
free(old);
return NULL;
}
cp = old;
while (cp && *cp && name_cmp(cp, add) < 0) {
/* step cp forward over a name */
char *e = strchr(cp, ',');
if (e)
cp = e+1;
else
cp = cp + strlen(cp);
}
len = cp-old;
if (old && len > 0) {
strncpy(new, old, len);
new[cp-old] = 0;
} else {
new[0] = 0;
}
if (cp != old && !*cp)
strcat(new, ",");
strcat(new, add);
if (cp && *cp) {
strcat(new, ",");
strcat(new, cp);
}
free(old);
return new;
}
/*
* Check each address listed in @ai against each address
* stored in @clp. Return 1 if a match is found, otherwise
* zero.
*/
static int
check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
{
int i;
for (; ai; ai = ai->ai_next)
for (i = 0; i < clp->m_naddr; i++)
if (nfs_compare_sockaddr(ai->ai_addr,
get_addrlist(clp, i)))
return 1;
return 0;
}
static _Bool
mask_match(const uint32_t a, const uint32_t b, const uint32_t m)
{
return ((a ^ b) & m) == 0;
}
static int
check_subnet_v4(const struct sockaddr_in *address,
const struct sockaddr_in *mask, const struct addrinfo *ai)
{
for (; ai; ai = ai->ai_next) {
struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
if (sin->sin_family != AF_INET)
continue;
if (mask_match(address->sin_addr.s_addr,
sin->sin_addr.s_addr,
mask->sin_addr.s_addr))
return 1;
}
return 0;
}
#ifdef IPV6_SUPPORTED
static int
check_subnet_v6(const struct sockaddr_in6 *address,
const struct sockaddr_in6 *mask, const struct addrinfo *ai)
{
for (; ai; ai = ai->ai_next) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
if (sin6->sin6_family != AF_INET6)
continue;
if (mask_match(address->sin6_addr.s6_addr32[0],
sin6->sin6_addr.s6_addr32[0],
mask->sin6_addr.s6_addr32[0]) &&
mask_match(address->sin6_addr.s6_addr32[1],
sin6->sin6_addr.s6_addr32[1],
mask->sin6_addr.s6_addr32[1]) &&
mask_match(address->sin6_addr.s6_addr32[2],
sin6->sin6_addr.s6_addr32[2],
mask->sin6_addr.s6_addr32[2]) &&
mask_match(address->sin6_addr.s6_addr32[3],
sin6->sin6_addr.s6_addr32[3],
mask->sin6_addr.s6_addr32[3]))
return 1;
}
return 0;
}
#else /* !IPV6_SUPPORTED */
static int
check_subnet_v6(const struct sockaddr_in6 *UNUSED(address),
const struct sockaddr_in6 *UNUSED(mask),
const struct addrinfo *UNUSED(ai))
{
return 0;
}
#endif /* !IPV6_SUPPORTED */
/*
* Check each address listed in @ai against the subnetwork or
* host address stored in @clp. Return 1 if an address in @hp
* matches the host address stored in @clp, otherwise zero.
*/
static int
check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
{
switch (get_addrlist(clp, 0)->sa_family) {
case AF_INET:
return check_subnet_v4(get_addrlist_in(clp, 0),
get_addrlist_in(clp, 1), ai);
case AF_INET6:
return check_subnet_v6(get_addrlist_in6(clp, 0),
get_addrlist_in6(clp, 1), ai);
}
return 0;
}
/*
* Check if a wildcard nfs_client record matches the canonical name
* or the aliases of a host. Return 1 if a match is found, otherwise
* zero.
*/
static int
check_wildcard(const nfs_client *clp, const struct addrinfo *ai)
{
char *hname, *cname = clp->m_hostname;
struct hostent *hp;
char **ap;
int match;
match = 0;
hname = host_canonname(ai->ai_addr);
if (hname == NULL)
goto out;
if (wildmat(hname, cname)) {
match = 1;
goto out;
}
/* See if hname aliases listed in /etc/hosts or nis[+]
* match the requested wildcard */
hp = gethostbyname(hname);
if (hp != NULL) {
for (ap = hp->h_aliases; *ap; ap++)
if (wildmat(*ap, cname)) {
match = 1;
goto out;
}
}
out:
free(hname);
return match;
}
/*
* Check if @ai's hostname or aliases fall in a given netgroup.
* Return 1 if @ai represents a host in the netgroup, otherwise
* zero.
*/
#ifdef HAVE_INNETGR
static int
check_netgroup(const nfs_client *clp, const struct addrinfo *ai)
{
const char *netgroup = clp->m_hostname + 1;
struct addrinfo *tmp = NULL;
struct hostent *hp;
char *dot, *hname, *ip;
int i, match;
match = 0;
hname = host_canonname(ai->ai_addr);
if (hname == NULL)
goto out;
/* First, try to match the hostname without
* splitting off the domain */
if (innetgr(netgroup, hname, NULL, NULL)) {
match = 1;
goto out;
}
/* See if hname aliases listed in /etc/hosts or nis[+]
* match the requested netgroup */
hp = gethostbyname(hname);
if (hp != NULL) {
for (i = 0; hp->h_aliases[i]; i++)
if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL)) {
match = 1;
goto out;
}
}
/* If hname happens to be an IP address, convert it
* to a the canonical DNS name bound to this address. */
tmp = host_pton(hname);
if (tmp != NULL) {
char *cname = host_canonname(tmp->ai_addr);
nfs_freeaddrinfo(tmp);
/* The resulting FQDN may be in our netgroup. */
if (cname != NULL) {
free(hname);
hname = cname;
if (innetgr(netgroup, hname, NULL, NULL)) {
match = 1;
goto out;
}
}
}
/* check whether the IP itself is in the netgroup */
ip = calloc(INET6_ADDRSTRLEN, 1);
if (inet_ntop(ai->ai_family, &(((struct sockaddr_in *)ai->ai_addr)->sin_addr), ip, INET6_ADDRSTRLEN) == ip) {
if (innetgr(netgroup, ip, NULL, NULL)) {
free(hname);
hname = ip;
match = 1;
goto out;
}
}
free(ip);
/* Okay, strip off the domain (if we have one) */
dot = strchr(hname, '.');
if (dot == NULL)
goto out;
*dot = '\0';
match = innetgr(netgroup, hname, NULL, NULL);
out:
free(hname);
return match;
}
#else /* !HAVE_INNETGR */
static int
check_netgroup(__attribute__((unused)) const nfs_client *clp,
__attribute__((unused)) const struct addrinfo *ai)
{
return 0;
}
#endif /* !HAVE_INNETGR */
/**
* client_check - check if IP address information matches a cached nfs_client
* @clp: pointer to a cached nfs_client record
* @ai: pointer to addrinfo to compare it with
*
* Returns 1 if the address information matches the cached nfs_client,
* otherwise zero.
*/
int
client_check(const nfs_client *clp, const struct addrinfo *ai)
{
switch (clp->m_type) {
case MCL_FQDN:
return check_fqdn(clp, ai);
case MCL_SUBNETWORK:
return check_subnetwork(clp, ai);
case MCL_WILDCARD:
return check_wildcard(clp, ai);
case MCL_NETGROUP:
return check_netgroup(clp, ai);
case MCL_ANONYMOUS:
return 1;
case MCL_GSS:
return 0;
default:
xlog(D_GENERAL, "%s: unrecognized client type: %d",
__func__, clp->m_type);
}
return 0;
}
/**
* client_gettype - determine type of nfs_client given an identifier
* @ident: '\0'-terminated ASCII string containing a client identifier
*
* Returns the type of nfs_client record that would be used for
* this client.
*/
int
client_gettype(char *ident)
{
char *sp;
if (ident[0] == '\0' || strcmp(ident, "*")==0)
return MCL_ANONYMOUS;
if (strncmp(ident, "gss/", 4) == 0)
return MCL_GSS;
if (ident[0] == '@') {
#ifndef HAVE_INNETGR
xlog(L_WARNING, "netgroup support not compiled in");
#endif
return MCL_NETGROUP;
}
for (sp = ident; *sp; sp++) {
if (*sp == '*' || *sp == '?' || *sp == '[')
return MCL_WILDCARD;
if (*sp == '/')
return MCL_SUBNETWORK;
if (*sp == '\\' && sp[1])
sp++;
}
return MCL_FQDN;
}
|