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);
142 if (clp->m_type == MCL_SUBNETWORK) {
143 char *cp = strchr(clp->m_hostname, '/');
144 static char slash32[] = "/32";
146 if(!cp) cp = slash32;
148 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
149 if (strchr(cp + 1, '.')) {
150 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
153 int netmask = atoi(cp + 1);
154 if (0 < netmask && netmask <= 32) {
155 clp->m_addrlist[1].s_addr =
156 htonl ((uint32_t) ~0 << (32 - netmask));
159 xlog(L_FATAL, "invalid netmask `%s' for %s",
160 cp + 1, clp->m_hostname);
168 char **ap = hp->h_addr_list;
171 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
172 clp->m_addrlist[i] = *(struct in_addr *)*ap;
179 client_add(nfs_client *clp)
183 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
184 xlog(L_FATAL, "unknown client type in client_add");
185 cpp = clientlist + clp->m_type;
187 cpp = &((*cpp)->m_next);
193 client_release(nfs_client *clp)
195 if (clp->m_count <= 0)
196 xlog(L_FATAL, "client_free: m_count <= 0!");
203 nfs_client *clp, **head;
206 for (i = 0; i < MCL_MAXTYPES; i++) {
207 head = clientlist + i;
209 *head = (clp = *head)->m_next;
210 xfree(clp->m_hostname);
217 client_resolve(struct in_addr addr)
219 struct hostent *he = NULL;
221 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
222 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
224 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
230 * client_compose - Make a list of cached hostnames that match an IP address
231 * @he: pointer to hostent containing IP address information to match
233 * Gather all known client hostnames that match the IP address, and sort
234 * the result into a comma-separated list.
236 * Returns a '\0'-terminated ASCII string containing a comma-separated
237 * sorted list of client hostnames, or NULL if no client records matched
238 * the IP address or memory could not be allocated. Caller must free the
239 * returned string with free(3).
242 client_compose(struct hostent *he)
247 for (i = 0 ; i < MCL_MAXTYPES; i++) {
249 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
250 if (!client_check(clp, he))
252 name = add_name(name, clp->m_hostname);
259 * client_member - check if @name is contained in the list @client
260 * @client: '\0'-terminated ASCII string containing
261 * comma-separated list of hostnames
262 * @name: '\0'-terminated ASCII string containing hostname to look for
264 * Returns 1 if @name was found in @client, otherwise zero is returned.
267 client_member(const char *client, const char *name)
269 size_t l = strlen(name);
272 if (strncmp(client, name, l) == 0 &&
273 (client[l] == ',' || client[l] == '\0'))
275 client = strchr(client, ',');
284 name_cmp(const char *a, const char *b)
286 /* compare strings a and b, but only upto ',' in a */
287 while (*a && *b && *a != ',' && *a == *b)
289 if (!*b && (!*a || *a == ','))
292 if (!*a || *a == ',') return -1;
297 add_name(char *old, const char *add)
299 size_t len = strlen(add) + 2;
302 if (old) len += strlen(old);
310 while (cp && *cp && name_cmp(cp, add) < 0) {
311 /* step cp forward over a name */
312 char *e = strchr(cp, ',');
316 cp = cp + strlen(cp);
318 strncpy(new, old, cp-old);
320 if (cp != old && !*cp)
332 * Check each address listed in @hp against each address
333 * stored in @clp. Return 1 if a match is found, otherwise
337 check_fqdn(const nfs_client *clp, const struct hostent *hp)
343 for (ap = hp->h_addr_list; *ap; ap++) {
344 addr = *(struct in_addr *)*ap;
346 for (i = 0; i < clp->m_naddr; i++)
347 if (clp->m_addrlist[i].s_addr == addr.s_addr)
354 * Check each address listed in @hp against the subnetwork or
355 * host address stored in @clp. Return 1 if an address in @hp
356 * matches the host address stored in @clp, otherwise zero.
359 check_subnetwork(const nfs_client *clp, const struct hostent *hp)
364 for (ap = hp->h_addr_list; *ap; ap++) {
365 addr = *(struct in_addr *)*ap;
367 if (!((clp->m_addrlist[0].s_addr ^ addr.s_addr) &
368 clp->m_addrlist[1].s_addr))
375 * Check if a wildcard nfs_client record matches the canonical name
376 * or the aliases of a host. Return 1 if a match is found, otherwise
380 check_wildcard(const nfs_client *clp, const struct hostent *hp)
382 char *cname = clp->m_hostname;
383 char *hname = hp->h_name;
386 if (wildmat(hname, cname))
389 /* See if hname aliases listed in /etc/hosts or nis[+]
390 * match the requested wildcard */
391 for (ap = hp->h_aliases; *ap; ap++) {
392 if (wildmat(*ap, cname))
400 * Check if @hp's hostname or aliases fall in a given netgroup.
401 * Return 1 if @hp represents a host in the netgroup, otherwise zero.
405 check_netgroup(const nfs_client *clp, const struct hostent *hp)
407 const char *netgroup = clp->m_hostname + 1;
408 const char *hname = hp->h_name;
409 struct hostent *nhp = NULL;
410 struct sockaddr_in addr;
414 /* First, try to match the hostname without
415 * splitting off the domain */
416 if (innetgr(netgroup, hname, NULL, NULL))
419 /* See if hname aliases listed in /etc/hosts or nis[+]
420 * match the requested netgroup */
421 for (i = 0; hp->h_aliases[i]; i++) {
422 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
426 /* If hname is ip address convert to FQDN */
427 if (inet_aton(hname, &addr.sin_addr) &&
428 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
429 sizeof(addr.sin_addr), AF_INET))) {
431 if (innetgr(netgroup, hname, NULL, NULL))
435 /* Okay, strip off the domain (if we have one) */
436 dot = strchr(hname, '.');
441 match = innetgr(netgroup, hname, NULL, NULL);
446 #else /* !HAVE_INNETGR */
448 check_netgroup(__attribute__((unused)) const nfs_client *clp,
449 __attribute__((unused)) const struct hostent *hp)
453 #endif /* !HAVE_INNETGR */
456 * client_check - check if IP address information matches a cached nfs_client
457 * @clp: pointer to a cached nfs_client record
458 * @hp: pointer to hostent containing host IP information
460 * Returns 1 if the address information matches the cached nfs_client,
464 client_check(nfs_client *clp, struct hostent *hp)
466 switch (clp->m_type) {
468 return check_fqdn(clp, hp);
470 return check_subnetwork(clp, hp);
472 return check_wildcard(clp, hp);
474 return check_netgroup(clp, hp);
480 xlog(D_GENERAL, "%s: unrecognized client type: %d",
481 __func__, clp->m_type);
488 client_gettype(char *ident)
492 if (ident[0] == '\0' || strcmp(ident, "*")==0)
493 return MCL_ANONYMOUS;
494 if (strncmp(ident, "gss/", 4) == 0)
496 if (ident[0] == '@') {
498 xlog(L_WARNING, "netgroup support not compiled in");
502 for (sp = ident; *sp; sp++) {
503 if (*sp == '*' || *sp == '?' || *sp == '[')
506 return MCL_SUBNETWORK;
507 if (*sp == '\\' && sp[1])
510 /* check for N.N.N.N */
512 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
513 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
514 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
515 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
516 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
517 return MCL_SUBNETWORK;