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;
56 client_free(nfs_client *clp)
58 xfree(clp->m_hostname);
62 /* if canonical is set, then we *know* this is already a canonical name
63 * so hostname lookup is avoided.
64 * This is used when reading /proc/fs/nfs/exports
67 client_lookup(char *hname, int canonical)
69 nfs_client *clp = NULL;
71 struct hostent *hp = NULL;
73 htype = client_gettype(hname);
75 if (htype == MCL_FQDN && !canonical) {
77 hp = gethostbyname(hname);
78 if (hp == NULL || hp->h_addrtype != AF_INET) {
79 xlog(L_ERROR, "%s has non-inet addr", hname);
82 /* make sure we have canonical name */
83 hp2 = hostent_dup(hp);
84 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
88 /* but now we might not have all addresses... */
89 if (hp2->h_addr_list[1]) {
91 gethostbyname(hp->h_name);
94 hp = hostent_dup(hp3);
101 hname = (char *) hp->h_name;
103 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
104 if (client_check(clp, hp))
108 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
109 if (strcasecmp(hname, clp->m_hostname)==0)
115 clp = (nfs_client *) xmalloc(sizeof(*clp));
116 memset(clp, 0, sizeof(*clp));
118 client_init(clp, hname, NULL);
122 if (htype == MCL_FQDN && clp->m_naddr == 0)
123 init_addrlist(clp, hp);
132 client_dup(nfs_client *clp, struct hostent *hp)
136 new = (nfs_client *) xmalloc(sizeof(*new));
137 memcpy(new, clp, sizeof(*new));
138 new->m_type = MCL_FQDN;
139 new->m_hostname = NULL;
141 client_init(new, (char *) hp->h_name, hp);
147 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
149 xfree(clp->m_hostname);
151 clp->m_hostname = xstrdup(hp->h_name);
153 clp->m_hostname = xstrdup(hname);
159 if (clp->m_type == MCL_SUBNETWORK) {
160 char *cp = strchr(clp->m_hostname, '/');
161 static char slash32[] = "/32";
163 if(!cp) cp = slash32;
165 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
166 if (strchr(cp + 1, '.')) {
167 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
170 int netmask = atoi(cp + 1);
171 if (0 < netmask && netmask <= 32) {
172 clp->m_addrlist[1].s_addr =
173 htonl ((uint32_t) ~0 << (32 - netmask));
176 xlog(L_FATAL, "invalid netmask `%s' for %s",
177 cp + 1, clp->m_hostname);
184 init_addrlist(clp, hp);
188 client_add(nfs_client *clp)
192 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
193 xlog(L_FATAL, "unknown client type in client_add");
194 cpp = clientlist + clp->m_type;
196 cpp = &((*cpp)->m_next);
202 client_release(nfs_client *clp)
204 if (clp->m_count <= 0)
205 xlog(L_FATAL, "client_free: m_count <= 0!");
212 nfs_client *clp, **head;
215 for (i = 0; i < MCL_MAXTYPES; i++) {
216 head = clientlist + i;
218 *head = (clp = *head)->m_next;
225 client_resolve(struct in_addr addr)
227 struct hostent *he = NULL;
229 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
230 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
232 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
238 * client_compose - Make a list of cached hostnames that match an IP address
239 * @he: pointer to hostent containing IP address information to match
241 * Gather all known client hostnames that match the IP address, and sort
242 * the result into a comma-separated list.
244 * Returns a '\0'-terminated ASCII string containing a comma-separated
245 * sorted list of client hostnames, or NULL if no client records matched
246 * the IP address or memory could not be allocated. Caller must free the
247 * returned string with free(3).
250 client_compose(struct hostent *he)
255 for (i = 0 ; i < MCL_MAXTYPES; i++) {
257 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
258 if (!client_check(clp, he))
260 name = add_name(name, clp->m_hostname);
267 * client_member - check if @name is contained in the list @client
268 * @client: '\0'-terminated ASCII string containing
269 * comma-separated list of hostnames
270 * @name: '\0'-terminated ASCII string containing hostname to look for
272 * Returns 1 if @name was found in @client, otherwise zero is returned.
275 client_member(const char *client, const char *name)
277 size_t l = strlen(name);
280 if (strncmp(client, name, l) == 0 &&
281 (client[l] == ',' || client[l] == '\0'))
283 client = strchr(client, ',');
292 name_cmp(const char *a, const char *b)
294 /* compare strings a and b, but only upto ',' in a */
295 while (*a && *b && *a != ',' && *a == *b)
297 if (!*b && (!*a || *a == ','))
300 if (!*a || *a == ',') return -1;
305 add_name(char *old, const char *add)
307 size_t len = strlen(add) + 2;
310 if (old) len += strlen(old);
318 while (cp && *cp && name_cmp(cp, add) < 0) {
319 /* step cp forward over a name */
320 char *e = strchr(cp, ',');
324 cp = cp + strlen(cp);
326 strncpy(new, old, cp-old);
328 if (cp != old && !*cp)
340 * Check each address listed in @hp against each address
341 * stored in @clp. Return 1 if a match is found, otherwise
345 check_fqdn(const nfs_client *clp, const struct hostent *hp)
351 for (ap = hp->h_addr_list; *ap; ap++) {
352 addr = *(struct in_addr *)*ap;
354 for (i = 0; i < clp->m_naddr; i++)
355 if (clp->m_addrlist[i].s_addr == addr.s_addr)
362 * Check each address listed in @hp against the subnetwork or
363 * host address stored in @clp. Return 1 if an address in @hp
364 * matches the host address stored in @clp, otherwise zero.
367 check_subnetwork(const nfs_client *clp, const struct hostent *hp)
372 for (ap = hp->h_addr_list; *ap; ap++) {
373 addr = *(struct in_addr *)*ap;
375 if (!((clp->m_addrlist[0].s_addr ^ addr.s_addr) &
376 clp->m_addrlist[1].s_addr))
383 * Check if a wildcard nfs_client record matches the canonical name
384 * or the aliases of a host. Return 1 if a match is found, otherwise
388 check_wildcard(const nfs_client *clp, const struct hostent *hp)
390 char *cname = clp->m_hostname;
391 char *hname = hp->h_name;
394 if (wildmat(hname, cname))
397 /* See if hname aliases listed in /etc/hosts or nis[+]
398 * match the requested wildcard */
399 for (ap = hp->h_aliases; *ap; ap++) {
400 if (wildmat(*ap, cname))
408 * Check if @hp's hostname or aliases fall in a given netgroup.
409 * Return 1 if @hp represents a host in the netgroup, otherwise zero.
413 check_netgroup(const nfs_client *clp, const struct hostent *hp)
415 const char *netgroup = clp->m_hostname + 1;
416 const char *hname = hp->h_name;
417 struct hostent *nhp = NULL;
418 struct sockaddr_in addr;
422 /* First, try to match the hostname without
423 * splitting off the domain */
424 if (innetgr(netgroup, hname, NULL, NULL))
427 /* See if hname aliases listed in /etc/hosts or nis[+]
428 * match the requested netgroup */
429 for (i = 0; hp->h_aliases[i]; i++) {
430 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
434 /* If hname is ip address convert to FQDN */
435 if (inet_aton(hname, &addr.sin_addr) &&
436 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
437 sizeof(addr.sin_addr), AF_INET))) {
439 if (innetgr(netgroup, hname, NULL, NULL))
443 /* Okay, strip off the domain (if we have one) */
444 dot = strchr(hname, '.');
449 match = innetgr(netgroup, hname, NULL, NULL);
454 #else /* !HAVE_INNETGR */
456 check_netgroup(__attribute__((unused)) const nfs_client *clp,
457 __attribute__((unused)) const struct hostent *hp)
461 #endif /* !HAVE_INNETGR */
464 * client_check - check if IP address information matches a cached nfs_client
465 * @clp: pointer to a cached nfs_client record
466 * @hp: pointer to hostent containing host IP information
468 * Returns 1 if the address information matches the cached nfs_client,
472 client_check(nfs_client *clp, struct hostent *hp)
474 switch (clp->m_type) {
476 return check_fqdn(clp, hp);
478 return check_subnetwork(clp, hp);
480 return check_wildcard(clp, hp);
482 return check_netgroup(clp, hp);
488 xlog(D_GENERAL, "%s: unrecognized client type: %d",
489 __func__, clp->m_type);
496 client_gettype(char *ident)
500 if (ident[0] == '\0' || strcmp(ident, "*")==0)
501 return MCL_ANONYMOUS;
502 if (strncmp(ident, "gss/", 4) == 0)
504 if (ident[0] == '@') {
506 xlog(L_WARNING, "netgroup support not compiled in");
510 for (sp = ident; *sp; sp++) {
511 if (*sp == '*' || *sp == '?' || *sp == '[')
514 return MCL_SUBNETWORK;
515 if (*sp == '\\' && sp[1])
518 /* check for N.N.N.N */
520 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
521 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
522 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
523 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
524 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
525 return MCL_SUBNETWORK;