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 @hp's hostname or aliases fall in a given netgroup.
375 * Return 1 if @hp represents a host in the netgroup, otherwise zero.
379 check_netgroup(const nfs_client *clp, const struct hostent *hp)
381 const char *netgroup = clp->m_hostname + 1;
382 const char *hname = hp->h_name;
383 struct hostent *nhp = NULL;
384 struct sockaddr_in addr;
388 /* First, try to match the hostname without
389 * splitting off the domain */
390 if (innetgr(netgroup, hname, NULL, NULL))
393 /* See if hname aliases listed in /etc/hosts or nis[+]
394 * match the requested netgroup */
395 for (i = 0; hp->h_aliases[i]; i++) {
396 if (innetgr(netgroup, hp->h_aliases[i], NULL, NULL))
400 /* If hname is ip address convert to FQDN */
401 if (inet_aton(hname, &addr.sin_addr) &&
402 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
403 sizeof(addr.sin_addr), AF_INET))) {
405 if (innetgr(netgroup, hname, NULL, NULL))
409 /* Okay, strip off the domain (if we have one) */
410 dot = strchr(hname, '.');
415 match = innetgr(netgroup, hname, NULL, NULL);
420 #else /* !HAVE_INNETGR */
422 check_netgroup(__attribute__((unused)) const nfs_client *clp,
423 __attribute__((unused)) const struct hostent *hp)
427 #endif /* !HAVE_INNETGR */
430 * Match a host (given its hostent record) to a client record. This
431 * is usually called from mountd.
434 client_check(nfs_client *clp, struct hostent *hp)
436 char *hname = (char *) hp->h_name;
437 char *cname = clp->m_hostname;
440 switch (clp->m_type) {
442 return check_fqdn(clp, hp);
444 return check_subnetwork(clp, hp);
446 if (wildmat(hname, cname))
449 for (ap = hp->h_aliases; *ap; ap++)
450 if (wildmat(*ap, cname))
455 return check_netgroup(clp, hp);
461 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
468 client_gettype(char *ident)
472 if (ident[0] == '\0' || strcmp(ident, "*")==0)
473 return MCL_ANONYMOUS;
474 if (strncmp(ident, "gss/", 4) == 0)
476 if (ident[0] == '@') {
478 xlog(L_WARNING, "netgroup support not compiled in");
482 for (sp = ident; *sp; sp++) {
483 if (*sp == '*' || *sp == '?' || *sp == '[')
486 return MCL_SUBNETWORK;
487 if (*sp == '\\' && sp[1])
490 /* check for N.N.N.N */
492 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
493 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
494 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
495 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
496 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
497 return MCL_SUBNETWORK;