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,
35 static int client_checkaddr(nfs_client *clp, struct in_addr addr);
37 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
40 /* if canonical is set, then we *know* this is already a canonical name
41 * so hostname lookup is avoided.
42 * This is used when reading /proc/fs/nfs/exports
45 client_lookup(char *hname, int canonical)
47 nfs_client *clp = NULL;
49 struct hostent *hp = NULL;
51 htype = client_gettype(hname);
53 if (htype == MCL_FQDN && !canonical) {
55 hp = gethostbyname(hname);
56 if (hp == NULL || hp->h_addrtype != AF_INET) {
57 xlog(L_ERROR, "%s has non-inet addr", hname);
60 /* make sure we have canonical name */
61 hp2 = hostent_dup(hp);
62 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
66 /* but now we might not have all addresses... */
67 if (hp2->h_addr_list[1]) {
69 gethostbyname(hp->h_name);
72 hp = hostent_dup(hp3);
79 hname = (char *) hp->h_name;
81 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
82 if (client_check(clp, hp))
86 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
87 if (strcasecmp(hname, clp->m_hostname)==0)
93 clp = (nfs_client *) xmalloc(sizeof(*clp));
94 memset(clp, 0, sizeof(*clp));
96 client_init(clp, hname, NULL);
100 if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
101 char **ap = hp->h_addr_list;
104 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
105 clp->m_addrlist[i] = *(struct in_addr *)*ap;
116 client_dup(nfs_client *clp, struct hostent *hp)
120 new = (nfs_client *) xmalloc(sizeof(*new));
121 memcpy(new, clp, sizeof(*new));
122 new->m_type = MCL_FQDN;
123 new->m_hostname = NULL;
125 client_init(new, (char *) hp->h_name, hp);
131 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
133 xfree(clp->m_hostname);
135 clp->m_hostname = xstrdup(hp->h_name);
137 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 * Match a host (given its hostent record) to a client record. This
355 * is usually called from mountd.
358 client_check(nfs_client *clp, struct hostent *hp)
360 char *hname = (char *) hp->h_name;
361 char *cname = clp->m_hostname;
364 switch (clp->m_type) {
366 return check_fqdn(clp, hp);
368 for (ap = hp->h_addr_list; *ap; ap++) {
369 if (client_checkaddr(clp, *(struct in_addr *) *ap))
374 if (wildmat(hname, cname))
377 for (ap = hp->h_aliases; *ap; ap++)
378 if (wildmat(*ap, cname))
387 struct hostent *nhp = NULL;
388 struct sockaddr_in addr;
390 /* First, try to match the hostname without
391 * splitting off the domain */
392 if (innetgr(cname+1, hname, NULL, NULL))
395 /* try the aliases as well */
396 for (i = 0; hp->h_aliases[i]; i++) {
397 if (innetgr(cname+1, hp->h_aliases[i], NULL, NULL))
401 /* If hname is ip address convert to FQDN */
402 if (inet_aton(hname, &addr.sin_addr) &&
403 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
404 sizeof(addr.sin_addr), AF_INET))) {
405 hname = (char *)nhp->h_name;
406 if (innetgr(cname+1, hname, NULL, NULL))
410 /* Okay, strip off the domain (if we have one) */
411 if ((dot = strchr(hname, '.')) == NULL)
415 match = innetgr(cname+1, hname, NULL, NULL);
428 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
435 client_checkaddr(nfs_client *clp, struct in_addr addr)
437 switch (clp->m_type) {
439 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
440 & clp->m_addrlist[1].s_addr);
446 client_gettype(char *ident)
450 if (ident[0] == '\0' || strcmp(ident, "*")==0)
451 return MCL_ANONYMOUS;
452 if (strncmp(ident, "gss/", 4) == 0)
454 if (ident[0] == '@') {
456 xlog(L_WARNING, "netgroup support not compiled in");
460 for (sp = ident; *sp; sp++) {
461 if (*sp == '*' || *sp == '?' || *sp == '[')
464 return MCL_SUBNETWORK;
465 if (*sp == '\\' && sp[1])
468 /* check for N.N.N.N */
470 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
471 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
472 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
473 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
474 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
475 return MCL_SUBNETWORK;