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);
33 static void client_init(nfs_client *clp, const char *hname,
36 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
40 init_addrlist(nfs_client *clp, const struct hostent *hp)
49 for (i = 0; *ap != NULL && i < NFSCLNT_ADDRMAX; i++, ap++)
50 clp->m_addrlist[i] = *(struct in_addr *)*ap;
55 /* if canonical is set, then we *know* this is already a canonical name
56 * so hostname lookup is avoided.
57 * This is used when reading /proc/fs/nfs/exports
60 client_lookup(char *hname, int canonical)
62 nfs_client *clp = NULL;
64 struct hostent *hp = NULL;
66 htype = client_gettype(hname);
68 if (htype == MCL_FQDN && !canonical) {
70 hp = gethostbyname(hname);
71 if (hp == NULL || hp->h_addrtype != AF_INET) {
72 xlog(L_ERROR, "%s has non-inet addr", hname);
75 /* make sure we have canonical name */
76 hp2 = hostent_dup(hp);
77 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
81 /* but now we might not have all addresses... */
82 if (hp2->h_addr_list[1]) {
84 gethostbyname(hp->h_name);
87 hp = hostent_dup(hp3);
94 hname = (char *) hp->h_name;
96 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
97 if (client_check(clp, hp))
101 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
102 if (strcasecmp(hname, clp->m_hostname)==0)
108 clp = (nfs_client *) xmalloc(sizeof(*clp));
109 memset(clp, 0, sizeof(*clp));
111 client_init(clp, hname, NULL);
115 if (htype == MCL_FQDN && clp->m_naddr == 0)
116 init_addrlist(clp, hp);
125 client_dup(nfs_client *clp, struct hostent *hp)
129 new = (nfs_client *) xmalloc(sizeof(*new));
130 memcpy(new, clp, sizeof(*new));
131 new->m_type = MCL_FQDN;
132 new->m_hostname = NULL;
134 client_init(new, (char *) hp->h_name, hp);
140 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
142 xfree(clp->m_hostname);
144 clp->m_hostname = xstrdup(hp->h_name);
146 clp->m_hostname = xstrdup(hname);
152 if (clp->m_type == MCL_SUBNETWORK) {
153 char *cp = strchr(clp->m_hostname, '/');
154 static char slash32[] = "/32";
156 if(!cp) cp = slash32;
158 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
159 if (strchr(cp + 1, '.')) {
160 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
163 int netmask = atoi(cp + 1);
164 if (0 < netmask && netmask <= 32) {
165 clp->m_addrlist[1].s_addr =
166 htonl ((uint32_t) ~0 << (32 - netmask));
169 xlog(L_FATAL, "invalid netmask `%s' for %s",
170 cp + 1, clp->m_hostname);
177 init_addrlist(clp, hp);
181 client_add(nfs_client *clp)
185 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
186 xlog(L_FATAL, "unknown client type in client_add");
187 cpp = clientlist + clp->m_type;
189 cpp = &((*cpp)->m_next);
195 client_release(nfs_client *clp)
197 if (clp->m_count <= 0)
198 xlog(L_FATAL, "client_free: m_count <= 0!");
205 nfs_client *clp, **head;
208 for (i = 0; i < MCL_MAXTYPES; i++) {
209 head = clientlist + i;
211 *head = (clp = *head)->m_next;
212 xfree(clp->m_hostname);
219 client_resolve(struct in_addr addr)
221 struct hostent *he = NULL;
223 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
224 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
226 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
232 * client_compose - Make a list of cached hostnames that match an IP address
233 * @he: pointer to hostent containing IP address information to match
235 * Gather all known client hostnames that match the IP address, and sort
236 * the result into a comma-separated list.
238 * Returns a '\0'-terminated ASCII string containing a comma-separated
239 * sorted list of client hostnames, or NULL if no client records matched
240 * the IP address or memory could not be allocated. Caller must free the
241 * returned string with free(3).
244 client_compose(struct hostent *he)
249 for (i = 0 ; i < MCL_MAXTYPES; i++) {
251 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
252 if (!client_check(clp, he))
254 name = add_name(name, clp->m_hostname);
261 * client_member - check if @name is contained in the list @client
262 * @client: '\0'-terminated ASCII string containing
263 * comma-separated list of hostnames
264 * @name: '\0'-terminated ASCII string containing hostname to look for
266 * Returns 1 if @name was found in @client, otherwise zero is returned.
269 client_member(const char *client, const char *name)
271 size_t l = strlen(name);
274 if (strncmp(client, name, l) == 0 &&
275 (client[l] == ',' || client[l] == '\0'))
277 client = strchr(client, ',');
286 name_cmp(const char *a, const char *b)
288 /* compare strings a and b, but only upto ',' in a */
289 while (*a && *b && *a != ',' && *a == *b)
291 if (!*b && (!*a || *a == ','))
294 if (!*a || *a == ',') return -1;
299 add_name(char *old, const char *add)
301 size_t len = strlen(add) + 2;
304 if (old) len += strlen(old);
312 while (cp && *cp && name_cmp(cp, add) < 0) {
313 /* step cp forward over a name */
314 char *e = strchr(cp, ',');
318 cp = cp + strlen(cp);
320 strncpy(new, old, cp-old);
322 if (cp != old && !*cp)
334 * Check each address listed in @hp against each address
335 * stored in @clp. Return 1 if a match is found, otherwise
339 check_fqdn(const nfs_client *clp, const struct hostent *hp)
345 for (ap = hp->h_addr_list; *ap; ap++) {
346 addr = *(struct in_addr *)*ap;
348 for (i = 0; i < clp->m_naddr; i++)
349 if (clp->m_addrlist[i].s_addr == addr.s_addr)
356 * Check each address listed in @hp against the subnetwork or
357 * host address stored in @clp. Return 1 if an address in @hp
358 * matches the host address stored in @clp, otherwise zero.
361 check_subnetwork(const nfs_client *clp, const struct hostent *hp)
366 for (ap = hp->h_addr_list; *ap; ap++) {
367 addr = *(struct in_addr *)*ap;
369 if (!((clp->m_addrlist[0].s_addr ^ addr.s_addr) &
370 clp->m_addrlist[1].s_addr))
377 * Check if a wildcard nfs_client record matches the canonical name
378 * or the aliases of a host. Return 1 if a match is found, otherwise
382 check_wildcard(const nfs_client *clp, const struct hostent *hp)
384 char *cname = clp->m_hostname;
385 char *hname = hp->h_name;
388 if (wildmat(hname, cname))
391 /* See if hname aliases listed in /etc/hosts or nis[+]
392 * match the requested wildcard */
393 for (ap = hp->h_aliases; *ap; ap++) {
394 if (wildmat(*ap, cname))
402 * Check if @hp's hostname or aliases fall in a given netgroup.
403 * Return 1 if @hp represents a host in the netgroup, otherwise zero.
407 check_netgroup(const nfs_client *clp, const struct hostent *hp)
409 const char *netgroup = clp->m_hostname + 1;
410 const char *hname = hp->h_name;
411 struct hostent *nhp = NULL;
412 struct sockaddr_in addr;
416 /* First, try to match the hostname without
417 * splitting off the domain */
418 if (innetgr(netgroup, hname, NULL, NULL))
421 /* See if hname aliases listed in /etc/hosts or nis[+]
422 * match the requested netgroup */
423 for (i = 0; hp->h_aliases[i]; i++) {
424 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
428 /* If hname is ip address convert to FQDN */
429 if (inet_aton(hname, &addr.sin_addr) &&
430 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
431 sizeof(addr.sin_addr), AF_INET))) {
433 if (innetgr(netgroup, hname, NULL, NULL))
437 /* Okay, strip off the domain (if we have one) */
438 dot = strchr(hname, '.');
443 match = innetgr(netgroup, hname, NULL, NULL);
448 #else /* !HAVE_INNETGR */
450 check_netgroup(__attribute__((unused)) const nfs_client *clp,
451 __attribute__((unused)) const struct hostent *hp)
455 #endif /* !HAVE_INNETGR */
458 * client_check - check if IP address information matches a cached nfs_client
459 * @clp: pointer to a cached nfs_client record
460 * @hp: pointer to hostent containing host IP information
462 * Returns 1 if the address information matches the cached nfs_client,
466 client_check(nfs_client *clp, struct hostent *hp)
468 switch (clp->m_type) {
470 return check_fqdn(clp, hp);
472 return check_subnetwork(clp, hp);
474 return check_wildcard(clp, hp);
476 return check_netgroup(clp, hp);
482 xlog(D_GENERAL, "%s: unrecognized client type: %d",
483 __func__, clp->m_type);
490 client_gettype(char *ident)
494 if (ident[0] == '\0' || strcmp(ident, "*")==0)
495 return MCL_ANONYMOUS;
496 if (strncmp(ident, "gss/", 4) == 0)
498 if (ident[0] == '@') {
500 xlog(L_WARNING, "netgroup support not compiled in");
504 for (sp = ident; *sp; sp++) {
505 if (*sp == '*' || *sp == '?' || *sp == '[')
508 return MCL_SUBNETWORK;
509 if (*sp == '\\' && sp[1])
512 /* check for N.N.N.N */
514 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
515 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
516 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
517 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
518 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
519 return MCL_SUBNETWORK;