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>
24 /* netgroup stuff never seems to be defined in any header file. Linux is
27 #if !defined(__GLIBC__) || __GLIBC__ < 2
28 extern int innetgr(char *netgr, char *host, char *, char *);
30 static void client_init(nfs_client *clp, const char *hname,
32 static int client_checkaddr(nfs_client *clp, struct in_addr addr);
34 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
37 /* if canonical is set, then we *know* this is already a canonical name
38 * so hostname lookup is avoided.
39 * This is used when reading /proc/fs/nfs/exports
42 client_lookup(char *hname, int canonical)
44 nfs_client *clp = NULL;
46 struct hostent *hp = NULL;
48 htype = client_gettype(hname);
50 if (htype == MCL_FQDN && !canonical) {
52 hp = gethostbyname(hname);
53 if (hp == NULL || hp->h_addrtype != AF_INET) {
54 xlog(L_ERROR, "%s has non-inet addr", hname);
57 /* make sure we have canonical name */
58 hp2 = hostent_dup(hp);
59 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
63 /* but now we might not have all addresses... */
64 if (hp2->h_addr_list[1]) {
66 gethostbyname(hp->h_name);
69 hp = hostent_dup(hp3);
76 hname = (char *) hp->h_name;
78 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
79 if (client_check(clp, hp))
83 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
84 if (strcasecmp(hname, clp->m_hostname)==0)
90 clp = (nfs_client *) xmalloc(sizeof(*clp));
91 memset(clp, 0, sizeof(*clp));
93 client_init(clp, hname, NULL);
97 if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
98 char **ap = hp->h_addr_list;
101 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
102 clp->m_addrlist[i] = *(struct in_addr *)*ap;
113 client_dup(nfs_client *clp, struct hostent *hp)
117 new = (nfs_client *) xmalloc(sizeof(*new));
118 memcpy(new, clp, sizeof(*new));
119 new->m_type = MCL_FQDN;
121 client_init(new, (char *) hp->h_name, hp);
127 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
130 strncpy(clp->m_hostname, hp->h_name,
131 sizeof (clp->m_hostname) - 1);
133 strncpy(clp->m_hostname, hname,
134 sizeof (clp->m_hostname) - 1);
136 clp->m_hostname[sizeof (clp->m_hostname) - 1] = '\0';
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;
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 * Find client name given an IP address
238 * This is found by gathering all known names that match that IP address,
239 * sorting them and joining them with '+'
242 static char *add_name(char *old, char *add);
245 client_compose(struct in_addr addr)
247 struct hostent *he = NULL;
251 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
252 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
254 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
256 for (i = 0 ; i < MCL_MAXTYPES; i++) {
258 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
259 if (!client_check(clp, he))
261 name = add_name(name, clp->m_hostname);
268 client_member(char *client, char *name)
270 /* check if "client" (a ',' separated list of names)
271 * contains 'name' as a member
273 int l = strlen(name);
275 if (strncmp(client, name, l) == 0 &&
276 (client[l] == ',' || client[l] == '\0'))
278 client = strchr(client, ',');
288 name_cmp(char *a, char *b)
290 /* compare strings a and b, but only upto ',' in a */
291 while (*a && *b && *a != ',' && *a == *b)
293 if (!*b && (!*a || !a == ',') )
296 if (!*a || *a == ',') return -1;
301 add_name(char *old, char *add)
303 int len = strlen(add)+2;
306 if (old) len += strlen(old);
314 while (cp && *cp && name_cmp(cp, add) < 0) {
315 /* step cp forward over a name */
316 char *e = strchr(cp, ',');
320 cp = cp + strlen(cp);
322 strncpy(new, old, cp-old);
324 if (cp != old && !*cp)
335 * Match a host (given its hostent record) to a client record. This
336 * is usually called from mountd.
339 client_check(nfs_client *clp, struct hostent *hp)
341 char *hname = (char *) hp->h_name;
342 char *cname = clp->m_hostname;
345 switch (clp->m_type) {
348 for (ap = hp->h_addr_list; *ap; ap++) {
349 if (client_checkaddr(clp, *(struct in_addr *) *ap))
354 if (wildmat(hname, cname))
357 for (ap = hp->h_aliases; *ap; ap++)
358 if (wildmat(*ap, cname))
367 struct hostent *nhp = NULL;
368 struct sockaddr_in addr;
370 /* First, try to match the hostname without
371 * splitting off the domain */
372 if (innetgr(cname+1, hname, NULL, NULL))
375 /* If hname is ip address convert to FQDN */
376 if (inet_aton(hname, &addr.sin_addr) &&
377 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
378 sizeof(addr.sin_addr), AF_INET))) {
379 hname = (char *)nhp->h_name;
380 if (innetgr(cname+1, hname, NULL, NULL))
384 /* Okay, strip off the domain (if we have one) */
385 if ((dot = strchr(hname, '.')) == NULL)
389 match = innetgr(cname+1, hname, NULL, NULL);
402 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
409 client_checkaddr(nfs_client *clp, struct in_addr addr)
413 switch (clp->m_type) {
415 for (i = 0; i < clp->m_naddr; i++) {
416 if (clp->m_addrlist[i].s_addr == addr.s_addr)
421 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
422 & clp->m_addrlist[1].s_addr);
428 client_gettype(char *ident)
432 if (ident[0] == '\0' || strcmp(ident, "*")==0)
433 return MCL_ANONYMOUS;
434 if (strncmp(ident, "gss/", 4) == 0)
436 if (ident[0] == '@') {
438 xlog(L_WARNING, "netgroup support not compiled in");
442 for (sp = ident; *sp; sp++) {
443 if (*sp == '*' || *sp == '?' || *sp == '[')
446 return MCL_SUBNETWORK;
447 if (*sp == '\\' && sp[1])
450 /* check for N.N.N.N */
452 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
453 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
454 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
455 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
456 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
457 return MCL_SUBNETWORK;