2 * support/export/client.c
4 * Maintain list of nfsd clients.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
11 #include <sys/types.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
22 /* netgroup stuff never seems to be defined in any header file. Linux is
25 #if !defined(__GLIBC__) || __GLIBC__ < 2
26 extern int innetgr(char *netgr, char *host, char *, char *);
28 static void client_init(nfs_client *clp, const char *hname,
30 static int client_checkaddr(nfs_client *clp, struct in_addr addr);
32 nfs_client *clientlist[MCL_MAXTYPES] = { NULL, };
35 /* if canonical is set, then we *know* this is already a canonical name
36 * so hostname lookup is avoided.
37 * This is used when reading /proc/fs/nfs/exports
40 client_lookup(char *hname, int canonical)
42 nfs_client *clp = NULL;
44 struct hostent *hp = NULL;
46 htype = client_gettype(hname);
48 if (htype == MCL_FQDN && !canonical) {
50 hp = gethostbyname(hname);
51 if (hp == NULL || hp->h_addrtype != AF_INET) {
52 xlog(L_ERROR, "%s has non-inet addr", hname);
55 /* make sure we have canonical name */
56 hp2 = hostent_dup(hp);
57 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
61 /* but now we might not have all addresses... */
62 if (hp2->h_addr_list[1]) {
64 gethostbyname(hp->h_name);
67 hp = hostent_dup(hp3);
74 hname = (char *) hp->h_name;
76 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
77 if (client_check(clp, hp))
81 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
82 if (strcasecmp(hname, clp->m_hostname)==0)
88 clp = (nfs_client *) xmalloc(sizeof(*clp));
89 memset(clp, 0, sizeof(*clp));
91 client_init(clp, hname, NULL);
95 if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
96 char **ap = hp->h_addr_list;
99 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
100 clp->m_addrlist[i] = *(struct in_addr *)*ap;
111 client_dup(nfs_client *clp, struct hostent *hp)
115 new = (nfs_client *) xmalloc(sizeof(*new));
116 memcpy(new, clp, sizeof(*new));
117 new->m_type = MCL_FQDN;
119 client_init(new, (char *) hp->h_name, hp);
125 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
128 strncpy(clp->m_hostname, hp->h_name,
129 sizeof (clp->m_hostname) - 1);
131 strncpy(clp->m_hostname, hname,
132 sizeof (clp->m_hostname) - 1);
134 clp->m_hostname[sizeof (clp->m_hostname) - 1] = '\0';
139 if (clp->m_type == MCL_SUBNETWORK) {
140 char *cp = strchr(clp->m_hostname, '/');
141 static char slash32[] = "/32";
143 if(!cp) cp = slash32;
145 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
146 if (strchr(cp + 1, '.')) {
147 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
150 int netmask = atoi(cp + 1);
151 if (0 < netmask && netmask <= 32) {
152 clp->m_addrlist[1].s_addr =
153 htonl ((uint32_t) ~0 << (32 - netmask));
156 xlog(L_FATAL, "invalid netmask `%s' for %s",
157 cp + 1, clp->m_hostname);
165 char **ap = hp->h_addr_list;
168 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
169 clp->m_addrlist[i] = *(struct in_addr *)*ap;
176 client_add(nfs_client *clp)
180 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
181 xlog(L_FATAL, "unknown client type in client_add");
182 cpp = clientlist + clp->m_type;
184 cpp = &((*cpp)->m_next);
190 client_release(nfs_client *clp)
192 if (clp->m_count <= 0)
193 xlog(L_FATAL, "client_free: m_count <= 0!");
200 nfs_client *clp, **head;
203 for (i = 0; i < MCL_MAXTYPES; i++) {
204 head = clientlist + i;
206 *head = (clp = *head)->m_next;
213 client_find(struct hostent *hp)
218 for (i = 0; i < MCL_MAXTYPES; i++) {
219 for (clp = clientlist[i]; clp; clp = clp->m_next) {
220 if (!client_check(clp, hp))
223 if (clp->m_type == MCL_FQDN)
225 return client_dup(clp, hp);
235 * Find client name given an IP address
236 * This is found by gathering all known names that match that IP address,
237 * sorting them and joining them with '+'
240 static char *add_name(char *old, char *add);
243 client_compose(struct in_addr addr)
245 struct hostent *he = NULL;
249 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
250 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
252 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
254 for (i = 0 ; i < MCL_MAXTYPES; i++) {
256 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
257 if (!client_check(clp, he))
259 name = add_name(name, clp->m_hostname);
266 client_member(char *client, char *name)
268 /* check if "client" (a ',' separated list of names)
269 * contains 'name' as a member
271 int l = strlen(name);
273 if (strncmp(client, name, l) == 0 &&
274 (client[l] == ',' || client[l] == '\0'))
276 client = strchr(client, ',');
286 name_cmp(char *a, char *b)
288 /* compare strings a and b, but only upto ',' in a */
289 while (*a && *b && *a != ',' && *a == *b)
291 if (!*b && (!*a || !a == ',') )
294 if (!*a || *a == ',') return -1;
299 add_name(char *old, char *add)
301 int len = strlen(add)+2;
304 if (old) len += strlen(old);
312 while (cp && *cp && name_cmp(cp, add) < 0) {
313 /* step cp forward over a name */
314 char *e = strchr(cp, ',');
318 cp = cp + strlen(cp);
320 strncpy(new, old, cp-old);
322 if (cp != old && !*cp)
333 * Match a host (given its hostent record) to a client record. This
334 * is usually called from mountd.
337 client_check(nfs_client *clp, struct hostent *hp)
339 char *hname = (char *) hp->h_name;
340 char *cname = clp->m_hostname;
343 switch (clp->m_type) {
346 for (ap = hp->h_addr_list; *ap; ap++) {
347 if (client_checkaddr(clp, *(struct in_addr *) *ap))
352 if (wildmat(hname, cname))
355 for (ap = hp->h_aliases; *ap; ap++)
356 if (wildmat(*ap, cname))
365 struct hostent *nhp = NULL;
366 struct sockaddr_in addr;
368 /* First, try to match the hostname without
369 * splitting off the domain */
370 if (innetgr(cname+1, hname, NULL, NULL))
373 /* If hname is ip address convert to FQDN */
374 if (inet_aton(hname, &addr.sin_addr) &&
375 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
376 sizeof(addr.sin_addr), AF_INET))) {
377 hname = (char *)nhp->h_name;
378 if (innetgr(cname+1, hname, NULL, NULL))
382 /* Okay, strip off the domain (if we have one) */
383 if ((dot = strchr(hname, '.')) == NULL)
387 match = innetgr(cname+1, hname, NULL, NULL);
400 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
407 client_checkaddr(nfs_client *clp, struct in_addr addr)
411 switch (clp->m_type) {
413 for (i = 0; i < clp->m_naddr; i++) {
414 if (clp->m_addrlist[i].s_addr == addr.s_addr)
419 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
420 & clp->m_addrlist[1].s_addr);
426 client_gettype(char *ident)
430 if (ident[0] == '\0' || strcmp(ident, "*")==0)
431 return MCL_ANONYMOUS;
432 if (strncmp(ident, "gss/", 4) == 0)
434 if (ident[0] == '@') {
436 xlog(L_WARNING, "netgroup support not compiled in");
440 for (sp = ident; *sp; sp++) {
441 if (*sp == '*' || *sp == '?' || *sp == '[')
444 return MCL_SUBNETWORK;
445 if (*sp == '\\' && sp[1])
448 /* check for N.N.N.N */
450 if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
451 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
452 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
453 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
454 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
455 return MCL_SUBNETWORK;