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 *);
31 static void client_init(nfs_client *clp, const char *hname,
33 static int client_checkaddr(nfs_client *clp, struct in_addr addr);
35 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
38 /* if canonical is set, then we *know* this is already a canonical name
39 * so hostname lookup is avoided.
40 * This is used when reading /proc/fs/nfs/exports
43 client_lookup(char *hname, int canonical)
45 nfs_client *clp = NULL;
47 struct hostent *hp = NULL;
49 htype = client_gettype(hname);
51 if (htype == MCL_FQDN && !canonical) {
53 hp = gethostbyname(hname);
54 if (hp == NULL || hp->h_addrtype != AF_INET) {
55 xlog(L_ERROR, "%s has non-inet addr", hname);
58 /* make sure we have canonical name */
59 hp2 = hostent_dup(hp);
60 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
64 /* but now we might not have all addresses... */
65 if (hp2->h_addr_list[1]) {
67 gethostbyname(hp->h_name);
70 hp = hostent_dup(hp3);
77 hname = (char *) hp->h_name;
79 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
80 if (client_check(clp, hp))
84 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
85 if (strcasecmp(hname, clp->m_hostname)==0)
91 clp = (nfs_client *) xmalloc(sizeof(*clp));
92 memset(clp, 0, sizeof(*clp));
94 client_init(clp, hname, NULL);
98 if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
99 char **ap = hp->h_addr_list;
102 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
103 clp->m_addrlist[i] = *(struct in_addr *)*ap;
114 client_dup(nfs_client *clp, struct hostent *hp)
118 new = (nfs_client *) xmalloc(sizeof(*new));
119 memcpy(new, clp, sizeof(*new));
120 new->m_type = MCL_FQDN;
121 new->m_hostname = NULL;
123 client_init(new, (char *) hp->h_name, hp);
129 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
131 xfree(clp->m_hostname);
133 clp->m_hostname = xstrdup(hp->h_name);
135 clp->m_hostname = xstrdup(hname);
140 if (clp->m_type == MCL_SUBNETWORK) {
141 char *cp = strchr(clp->m_hostname, '/');
142 static char slash32[] = "/32";
144 if(!cp) cp = slash32;
146 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
147 if (strchr(cp + 1, '.')) {
148 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
151 int netmask = atoi(cp + 1);
152 if (0 < netmask && netmask <= 32) {
153 clp->m_addrlist[1].s_addr =
154 htonl ((uint32_t) ~0 << (32 - netmask));
157 xlog(L_FATAL, "invalid netmask `%s' for %s",
158 cp + 1, clp->m_hostname);
166 char **ap = hp->h_addr_list;
169 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
170 clp->m_addrlist[i] = *(struct in_addr *)*ap;
177 client_add(nfs_client *clp)
181 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
182 xlog(L_FATAL, "unknown client type in client_add");
183 cpp = clientlist + clp->m_type;
185 cpp = &((*cpp)->m_next);
191 client_release(nfs_client *clp)
193 if (clp->m_count <= 0)
194 xlog(L_FATAL, "client_free: m_count <= 0!");
201 nfs_client *clp, **head;
204 for (i = 0; i < MCL_MAXTYPES; i++) {
205 head = clientlist + i;
207 *head = (clp = *head)->m_next;
208 xfree(clp->m_hostname);
215 client_find(struct hostent *hp)
220 for (i = 0; i < MCL_MAXTYPES; i++) {
221 for (clp = clientlist[i]; clp; clp = clp->m_next) {
222 if (!client_check(clp, hp))
225 if (clp->m_type == MCL_FQDN)
227 return client_dup(clp, hp);
237 client_resolve(struct in_addr addr)
239 struct hostent *he = NULL;
241 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
242 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
244 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
250 * Find client name given an IP address
251 * This is found by gathering all known names that match that IP address,
252 * sorting them and joining them with '+'
255 static char *add_name(char *old, char *add);
258 client_compose(struct hostent *he)
263 for (i = 0 ; i < MCL_MAXTYPES; i++) {
265 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
266 if (!client_check(clp, he))
268 name = add_name(name, clp->m_hostname);
275 client_member(char *client, char *name)
277 /* check if "client" (a ',' separated list of names)
278 * contains 'name' as a member
280 int l = strlen(name);
282 if (strncmp(client, name, l) == 0 &&
283 (client[l] == ',' || client[l] == '\0'))
285 client = strchr(client, ',');
295 name_cmp(char *a, char *b)
297 /* compare strings a and b, but only upto ',' in a */
298 while (*a && *b && *a != ',' && *a == *b)
300 if (!*b && (!*a || !a == ',') )
303 if (!*a || *a == ',') return -1;
308 add_name(char *old, char *add)
310 int len = strlen(add)+2;
313 if (old) len += strlen(old);
321 while (cp && *cp && name_cmp(cp, add) < 0) {
322 /* step cp forward over a name */
323 char *e = strchr(cp, ',');
327 cp = cp + strlen(cp);
329 strncpy(new, old, cp-old);
331 if (cp != old && !*cp)
343 * Match a host (given its hostent record) to a client record. This
344 * is usually called from mountd.
347 client_check(nfs_client *clp, struct hostent *hp)
349 char *hname = (char *) hp->h_name;
350 char *cname = clp->m_hostname;
353 switch (clp->m_type) {
356 for (ap = hp->h_addr_list; *ap; ap++) {
357 if (client_checkaddr(clp, *(struct in_addr *) *ap))
362 if (wildmat(hname, cname))
365 for (ap = hp->h_aliases; *ap; ap++)
366 if (wildmat(*ap, cname))
375 struct hostent *nhp = NULL;
376 struct sockaddr_in addr;
378 /* First, try to match the hostname without
379 * splitting off the domain */
380 if (innetgr(cname+1, hname, NULL, NULL))
383 /* If hname is ip address convert to FQDN */
384 if (inet_aton(hname, &addr.sin_addr) &&
385 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
386 sizeof(addr.sin_addr), AF_INET))) {
387 hname = (char *)nhp->h_name;
388 if (innetgr(cname+1, hname, NULL, NULL))
392 /* Okay, strip off the domain (if we have one) */
393 if ((dot = strchr(hname, '.')) == NULL)
397 match = innetgr(cname+1, hname, NULL, NULL);
410 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
417 client_checkaddr(nfs_client *clp, struct in_addr addr)
421 switch (clp->m_type) {
423 for (i = 0; i < clp->m_naddr; i++) {
424 if (clp->m_addrlist[i].s_addr == addr.s_addr)
429 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
430 & clp->m_addrlist[1].s_addr);
436 client_gettype(char *ident)
440 if (ident[0] == '\0' || strcmp(ident, "*")==0)
441 return MCL_ANONYMOUS;
442 if (strncmp(ident, "gss/", 4) == 0)
444 if (ident[0] == '@') {
446 xlog(L_WARNING, "netgroup support not compiled in");
450 for (sp = ident; *sp; sp++) {
451 if (*sp == '*' || *sp == '?' || *sp == '[')
454 return MCL_SUBNETWORK;
455 if (*sp == '\\' && sp[1])
458 /* check for N.N.N.N */
460 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
461 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
462 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
463 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
464 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
465 return MCL_SUBNETWORK;