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, };
39 /* if canonical is set, then we *know* this is already a canonical name
40 * so hostname lookup is avoided.
41 * This is used when reading /proc/fs/nfs/exports
44 client_lookup(char *hname, int canonical)
46 nfs_client *clp = NULL;
48 struct hostent *hp = NULL;
50 htype = client_gettype(hname);
52 if (htype == MCL_FQDN && !canonical) {
54 hp = gethostbyname(hname);
55 if (hp == NULL || hp->h_addrtype != AF_INET) {
56 xlog(L_ERROR, "%s has non-inet addr", hname);
59 /* make sure we have canonical name */
60 hp2 = hostent_dup(hp);
61 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
65 /* but now we might not have all addresses... */
66 if (hp2->h_addr_list[1]) {
68 gethostbyname(hp->h_name);
71 hp = hostent_dup(hp3);
78 hname = (char *) hp->h_name;
80 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
81 if (client_check(clp, hp))
85 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
86 if (strcasecmp(hname, clp->m_hostname)==0)
92 clp = (nfs_client *) xmalloc(sizeof(*clp));
93 memset(clp, 0, sizeof(*clp));
95 client_init(clp, hname, NULL);
99 if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
100 char **ap = hp->h_addr_list;
103 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
104 clp->m_addrlist[i] = *(struct in_addr *)*ap;
115 client_dup(nfs_client *clp, struct hostent *hp)
119 new = (nfs_client *) xmalloc(sizeof(*new));
120 memcpy(new, clp, sizeof(*new));
121 new->m_type = MCL_FQDN;
122 new->m_hostname = NULL;
124 client_init(new, (char *) hp->h_name, hp);
130 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
132 xfree(clp->m_hostname);
134 clp->m_hostname = xstrdup(hp->h_name);
136 clp->m_hostname = xstrdup(hname);
141 if (clp->m_type == MCL_SUBNETWORK) {
142 char *cp = strchr(clp->m_hostname, '/');
143 static char slash32[] = "/32";
145 if(!cp) cp = slash32;
147 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
148 if (strchr(cp + 1, '.')) {
149 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
152 int netmask = atoi(cp + 1);
153 if (0 < netmask && netmask <= 32) {
154 clp->m_addrlist[1].s_addr =
155 htonl ((uint32_t) ~0 << (32 - netmask));
158 xlog(L_FATAL, "invalid netmask `%s' for %s",
159 cp + 1, clp->m_hostname);
167 char **ap = hp->h_addr_list;
170 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
171 clp->m_addrlist[i] = *(struct in_addr *)*ap;
178 client_add(nfs_client *clp)
182 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
183 xlog(L_FATAL, "unknown client type in client_add");
184 cpp = clientlist + clp->m_type;
186 cpp = &((*cpp)->m_next);
192 client_release(nfs_client *clp)
194 if (clp->m_count <= 0)
195 xlog(L_FATAL, "client_free: m_count <= 0!");
202 nfs_client *clp, **head;
205 for (i = 0; i < MCL_MAXTYPES; i++) {
206 head = clientlist + i;
208 *head = (clp = *head)->m_next;
209 xfree(clp->m_hostname);
216 client_resolve(struct in_addr addr)
218 struct hostent *he = NULL;
220 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
221 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
223 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
229 * client_compose - Make a list of cached hostnames that match an IP address
230 * @he: pointer to hostent containing IP address information to match
232 * Gather all known client hostnames that match the IP address, and sort
233 * the result into a comma-separated list.
235 * Returns a '\0'-terminated ASCII string containing a comma-separated
236 * sorted list of client hostnames, or NULL if no client records matched
237 * the IP address or memory could not be allocated. Caller must free the
238 * returned string with free(3).
241 client_compose(struct hostent *he)
246 for (i = 0 ; i < MCL_MAXTYPES; i++) {
248 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
249 if (!client_check(clp, he))
251 name = add_name(name, clp->m_hostname);
258 * client_member - check if @name is contained in the list @client
259 * @client: '\0'-terminated ASCII string containing
260 * comma-separated list of hostnames
261 * @name: '\0'-terminated ASCII string containing hostname to look for
263 * Returns 1 if @name was found in @client, otherwise zero is returned.
266 client_member(const char *client, const char *name)
268 size_t l = strlen(name);
271 if (strncmp(client, name, l) == 0 &&
272 (client[l] == ',' || client[l] == '\0'))
274 client = strchr(client, ',');
283 name_cmp(const char *a, const char *b)
285 /* compare strings a and b, but only upto ',' in a */
286 while (*a && *b && *a != ',' && *a == *b)
288 if (!*b && (!*a || *a == ','))
291 if (!*a || *a == ',') return -1;
296 add_name(char *old, const char *add)
298 size_t len = strlen(add) + 2;
301 if (old) len += strlen(old);
309 while (cp && *cp && name_cmp(cp, add) < 0) {
310 /* step cp forward over a name */
311 char *e = strchr(cp, ',');
315 cp = cp + strlen(cp);
317 strncpy(new, old, cp-old);
319 if (cp != old && !*cp)
331 * Check each address listed in @hp against each address
332 * stored in @clp. Return 1 if a match is found, otherwise
336 check_fqdn(const nfs_client *clp, const struct hostent *hp)
342 for (ap = hp->h_addr_list; *ap; ap++) {
343 addr = *(struct in_addr *)*ap;
345 for (i = 0; i < clp->m_naddr; i++)
346 if (clp->m_addrlist[i].s_addr == addr.s_addr)
353 * Check each address listed in @hp against the subnetwork or
354 * host address stored in @clp. Return 1 if an address in @hp
355 * matches the host address stored in @clp, otherwise zero.
358 check_subnetwork(const nfs_client *clp, const struct hostent *hp)
363 for (ap = hp->h_addr_list; *ap; ap++) {
364 addr = *(struct in_addr *)*ap;
366 if (!((clp->m_addrlist[0].s_addr ^ addr.s_addr) &
367 clp->m_addrlist[1].s_addr))
374 * Check if a wildcard nfs_client record matches the canonical name
375 * or the aliases of a host. Return 1 if a match is found, otherwise
379 check_wildcard(const nfs_client *clp, const struct hostent *hp)
381 char *cname = clp->m_hostname;
382 char *hname = hp->h_name;
385 if (wildmat(hname, cname))
388 /* See if hname aliases listed in /etc/hosts or nis[+]
389 * match the requested wildcard */
390 for (ap = hp->h_aliases; *ap; ap++) {
391 if (wildmat(*ap, cname))
399 * Check if @hp's hostname or aliases fall in a given netgroup.
400 * Return 1 if @hp represents a host in the netgroup, otherwise zero.
404 check_netgroup(const nfs_client *clp, const struct hostent *hp)
406 const char *netgroup = clp->m_hostname + 1;
407 const char *hname = hp->h_name;
408 struct hostent *nhp = NULL;
409 struct sockaddr_in addr;
413 /* First, try to match the hostname without
414 * splitting off the domain */
415 if (innetgr(netgroup, hname, NULL, NULL))
418 /* See if hname aliases listed in /etc/hosts or nis[+]
419 * match the requested netgroup */
420 for (i = 0; hp->h_aliases[i]; i++) {
421 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
425 /* If hname is ip address convert to FQDN */
426 if (inet_aton(hname, &addr.sin_addr) &&
427 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
428 sizeof(addr.sin_addr), AF_INET))) {
430 if (innetgr(netgroup, hname, NULL, NULL))
434 /* Okay, strip off the domain (if we have one) */
435 dot = strchr(hname, '.');
440 match = innetgr(netgroup, hname, NULL, NULL);
445 #else /* !HAVE_INNETGR */
447 check_netgroup(__attribute__((unused)) const nfs_client *clp,
448 __attribute__((unused)) const struct hostent *hp)
452 #endif /* !HAVE_INNETGR */
455 * Match a host (given its hostent record) to a client record. This
456 * is usually called from mountd.
459 client_check(nfs_client *clp, struct hostent *hp)
461 switch (clp->m_type) {
463 return check_fqdn(clp, hp);
465 return check_subnetwork(clp, hp);
467 return check_wildcard(clp, hp);
469 return check_netgroup(clp, hp);
475 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
482 client_gettype(char *ident)
486 if (ident[0] == '\0' || strcmp(ident, "*")==0)
487 return MCL_ANONYMOUS;
488 if (strncmp(ident, "gss/", 4) == 0)
490 if (ident[0] == '@') {
492 xlog(L_WARNING, "netgroup support not compiled in");
496 for (sp = ident; *sp; sp++) {
497 if (*sp == '*' || *sp == '?' || *sp == '[')
500 return MCL_SUBNETWORK;
501 if (*sp == '\\' && sp[1])
504 /* check for N.N.N.N */
506 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
507 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
508 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
509 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
510 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
511 return MCL_SUBNETWORK;