2 * support/export/client.c
4 * Maintain list of nfsd clients.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
25 /* netgroup stuff never seems to be defined in any header file. Linux is
28 #if !defined(__GLIBC__) || __GLIBC__ < 2
29 extern int innetgr(char *netgr, char *host, char *, char *);
32 static char *add_name(char *old, const char *add);
34 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
38 init_addrlist(nfs_client *clp, const struct hostent *hp)
47 for (i = 0; *ap != NULL && i < NFSCLNT_ADDRMAX; i++, ap++)
48 clp->m_addrlist[i] = *(struct in_addr *)*ap;
54 client_free(nfs_client *clp)
56 free(clp->m_hostname);
61 init_netmask(nfs_client *clp, const char *slash)
63 if (strchr(slash + 1, '.') != NULL)
64 clp->m_addrlist[1].s_addr = inet_addr(slash + 1);
66 int prefixlen = atoi(slash + 1);
67 if (0 < prefixlen && prefixlen <= 32)
68 clp->m_addrlist[1].s_addr =
69 htonl((uint32_t)~0 << (32 - prefixlen));
77 xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
82 init_subnetwork(nfs_client *clp)
84 static char slash32[] = "/32";
87 cp = strchr(clp->m_hostname, '/');
92 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
95 return init_netmask(clp, cp);
99 client_init(nfs_client *clp, const char *hname, const struct hostent *hp)
101 clp->m_hostname = strdup(hname);
102 if (clp->m_hostname == NULL)
109 if (clp->m_type == MCL_SUBNETWORK)
110 return init_subnetwork(clp);
112 init_addrlist(clp, hp);
117 client_add(nfs_client *clp)
121 cpp = &clientlist[clp->m_type];
123 cpp = &((*cpp)->m_next);
128 /* if canonical is set, then we *know* this is already a canonical name
129 * so hostname lookup is avoided.
130 * This is used when reading /proc/fs/nfs/exports
133 client_lookup(char *hname, int canonical)
135 nfs_client *clp = NULL;
137 struct hostent *hp = NULL;
139 htype = client_gettype(hname);
141 if (htype == MCL_FQDN && !canonical) {
143 hp = gethostbyname(hname);
144 if (hp == NULL || hp->h_addrtype != AF_INET) {
145 xlog(L_ERROR, "%s has non-inet addr", hname);
148 /* make sure we have canonical name */
149 hp2 = hostent_dup(hp);
150 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
153 hp = hostent_dup(hp);
154 /* but now we might not have all addresses... */
155 if (hp2->h_addr_list[1]) {
156 struct hostent *hp3 =
157 gethostbyname(hp->h_name);
160 hp = hostent_dup(hp3);
167 hname = (char *) hp->h_name;
169 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
170 if (client_check(clp, hp))
174 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
175 if (strcasecmp(hname, clp->m_hostname)==0)
181 clp = calloc(1, sizeof(*clp));
185 if (!client_init(clp, hname, NULL)) {
193 if (htype == MCL_FQDN && clp->m_naddr == 0)
194 init_addrlist(clp, hp);
204 client_dup(nfs_client *clp, struct hostent *hp)
208 new = (nfs_client *)malloc(sizeof(*new));
211 memcpy(new, clp, sizeof(*new));
212 new->m_type = MCL_FQDN;
213 new->m_hostname = NULL;
215 if (!client_init(new, hp->h_name, hp)) {
224 client_release(nfs_client *clp)
226 if (clp->m_count <= 0)
227 xlog(L_FATAL, "client_free: m_count <= 0!");
234 nfs_client *clp, **head;
237 for (i = 0; i < MCL_MAXTYPES; i++) {
238 head = clientlist + i;
240 *head = (clp = *head)->m_next;
247 client_resolve(struct in_addr addr)
249 struct hostent *he = NULL;
251 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
252 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
254 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
260 * client_compose - Make a list of cached hostnames that match an IP address
261 * @he: pointer to hostent containing IP address information to match
263 * Gather all known client hostnames that match the IP address, and sort
264 * the result into a comma-separated list.
266 * Returns a '\0'-terminated ASCII string containing a comma-separated
267 * sorted list of client hostnames, or NULL if no client records matched
268 * the IP address or memory could not be allocated. Caller must free the
269 * returned string with free(3).
272 client_compose(struct hostent *he)
277 for (i = 0 ; i < MCL_MAXTYPES; i++) {
279 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
280 if (!client_check(clp, he))
282 name = add_name(name, clp->m_hostname);
289 * client_member - check if @name is contained in the list @client
290 * @client: '\0'-terminated ASCII string containing
291 * comma-separated list of hostnames
292 * @name: '\0'-terminated ASCII string containing hostname to look for
294 * Returns 1 if @name was found in @client, otherwise zero is returned.
297 client_member(const char *client, const char *name)
299 size_t l = strlen(name);
302 if (strncmp(client, name, l) == 0 &&
303 (client[l] == ',' || client[l] == '\0'))
305 client = strchr(client, ',');
314 name_cmp(const char *a, const char *b)
316 /* compare strings a and b, but only upto ',' in a */
317 while (*a && *b && *a != ',' && *a == *b)
319 if (!*b && (!*a || *a == ','))
322 if (!*a || *a == ',') return -1;
327 add_name(char *old, const char *add)
329 size_t len = strlen(add) + 2;
332 if (old) len += strlen(old);
340 while (cp && *cp && name_cmp(cp, add) < 0) {
341 /* step cp forward over a name */
342 char *e = strchr(cp, ',');
346 cp = cp + strlen(cp);
348 strncpy(new, old, cp-old);
350 if (cp != old && !*cp)
362 * Check each address listed in @hp against each address
363 * stored in @clp. Return 1 if a match is found, otherwise
367 check_fqdn(const nfs_client *clp, const struct hostent *hp)
373 for (ap = hp->h_addr_list; *ap; ap++) {
374 addr = *(struct in_addr *)*ap;
376 for (i = 0; i < clp->m_naddr; i++)
377 if (clp->m_addrlist[i].s_addr == addr.s_addr)
384 * Check each address listed in @hp against the subnetwork or
385 * host address stored in @clp. Return 1 if an address in @hp
386 * matches the host address stored in @clp, otherwise zero.
389 check_subnetwork(const nfs_client *clp, const struct hostent *hp)
394 for (ap = hp->h_addr_list; *ap; ap++) {
395 addr = *(struct in_addr *)*ap;
397 if (!((clp->m_addrlist[0].s_addr ^ addr.s_addr) &
398 clp->m_addrlist[1].s_addr))
405 * Check if a wildcard nfs_client record matches the canonical name
406 * or the aliases of a host. Return 1 if a match is found, otherwise
410 check_wildcard(const nfs_client *clp, const struct hostent *hp)
412 char *cname = clp->m_hostname;
413 char *hname = hp->h_name;
416 if (wildmat(hname, cname))
419 /* See if hname aliases listed in /etc/hosts or nis[+]
420 * match the requested wildcard */
421 for (ap = hp->h_aliases; *ap; ap++) {
422 if (wildmat(*ap, cname))
430 * Check if @hp's hostname or aliases fall in a given netgroup.
431 * Return 1 if @hp represents a host in the netgroup, otherwise zero.
435 check_netgroup(const nfs_client *clp, const struct hostent *hp)
437 const char *netgroup = clp->m_hostname + 1;
438 const char *hname = hp->h_name;
439 struct hostent *nhp = NULL;
440 struct sockaddr_in addr;
444 /* First, try to match the hostname without
445 * splitting off the domain */
446 if (innetgr(netgroup, hname, NULL, NULL))
449 /* See if hname aliases listed in /etc/hosts or nis[+]
450 * match the requested netgroup */
451 for (i = 0; hp->h_aliases[i]; i++) {
452 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
456 /* If hname is ip address convert to FQDN */
457 if (inet_aton(hname, &addr.sin_addr) &&
458 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
459 sizeof(addr.sin_addr), AF_INET))) {
461 if (innetgr(netgroup, hname, NULL, NULL))
465 /* Okay, strip off the domain (if we have one) */
466 dot = strchr(hname, '.');
471 match = innetgr(netgroup, hname, NULL, NULL);
476 #else /* !HAVE_INNETGR */
478 check_netgroup(__attribute__((unused)) const nfs_client *clp,
479 __attribute__((unused)) const struct hostent *hp)
483 #endif /* !HAVE_INNETGR */
486 * client_check - check if IP address information matches a cached nfs_client
487 * @clp: pointer to a cached nfs_client record
488 * @hp: pointer to hostent containing host IP information
490 * Returns 1 if the address information matches the cached nfs_client,
494 client_check(nfs_client *clp, struct hostent *hp)
496 switch (clp->m_type) {
498 return check_fqdn(clp, hp);
500 return check_subnetwork(clp, hp);
502 return check_wildcard(clp, hp);
504 return check_netgroup(clp, hp);
510 xlog(D_GENERAL, "%s: unrecognized client type: %d",
511 __func__, clp->m_type);
518 client_gettype(char *ident)
522 if (ident[0] == '\0' || strcmp(ident, "*")==0)
523 return MCL_ANONYMOUS;
524 if (strncmp(ident, "gss/", 4) == 0)
526 if (ident[0] == '@') {
528 xlog(L_WARNING, "netgroup support not compiled in");
532 for (sp = ident; *sp; sp++) {
533 if (*sp == '*' || *sp == '?' || *sp == '[')
536 return MCL_SUBNETWORK;
537 if (*sp == '\\' && sp[1])
540 /* check for N.N.N.N */
542 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
543 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
544 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
545 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
546 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
547 return MCL_SUBNETWORK;