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;
122 client_init(new, (char *) hp->h_name, hp);
128 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
131 strncpy(clp->m_hostname, hp->h_name,
132 sizeof (clp->m_hostname) - 1);
134 strncpy(clp->m_hostname, hname,
135 sizeof (clp->m_hostname) - 1);
137 clp->m_hostname[sizeof (clp->m_hostname) - 1] = '\0';
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;
216 client_find(struct hostent *hp)
221 for (i = 0; i < MCL_MAXTYPES; i++) {
222 for (clp = clientlist[i]; clp; clp = clp->m_next) {
223 if (!client_check(clp, hp))
226 if (clp->m_type == MCL_FQDN)
228 return client_dup(clp, hp);
238 * Find client name given an IP address
239 * This is found by gathering all known names that match that IP address,
240 * sorting them and joining them with '+'
243 static char *add_name(char *old, char *add);
246 client_compose(struct in_addr addr)
248 struct hostent *he = NULL;
252 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
253 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
255 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
257 for (i = 0 ; i < MCL_MAXTYPES; i++) {
259 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
260 if (!client_check(clp, he))
262 name = add_name(name, clp->m_hostname);
269 client_member(char *client, char *name)
271 /* check if "client" (a ',' separated list of names)
272 * contains 'name' as a member
274 int l = strlen(name);
276 if (strncmp(client, name, l) == 0 &&
277 (client[l] == ',' || client[l] == '\0'))
279 client = strchr(client, ',');
289 name_cmp(char *a, char *b)
291 /* compare strings a and b, but only upto ',' in a */
292 while (*a && *b && *a != ',' && *a == *b)
294 if (!*b && (!*a || !a == ',') )
297 if (!*a || *a == ',') return -1;
302 add_name(char *old, char *add)
304 int len = strlen(add)+2;
307 if (old) len += strlen(old);
315 while (cp && *cp && name_cmp(cp, add) < 0) {
316 /* step cp forward over a name */
317 char *e = strchr(cp, ',');
321 cp = cp + strlen(cp);
323 strncpy(new, old, cp-old);
325 if (cp != old && !*cp)
336 * Match a host (given its hostent record) to a client record. This
337 * is usually called from mountd.
340 client_check(nfs_client *clp, struct hostent *hp)
342 char *hname = (char *) hp->h_name;
343 char *cname = clp->m_hostname;
346 switch (clp->m_type) {
349 for (ap = hp->h_addr_list; *ap; ap++) {
350 if (client_checkaddr(clp, *(struct in_addr *) *ap))
355 if (wildmat(hname, cname))
358 for (ap = hp->h_aliases; *ap; ap++)
359 if (wildmat(*ap, cname))
368 struct hostent *nhp = NULL;
369 struct sockaddr_in addr;
371 /* First, try to match the hostname without
372 * splitting off the domain */
373 if (innetgr(cname+1, hname, NULL, NULL))
376 /* If hname is ip address convert to FQDN */
377 if (inet_aton(hname, &addr.sin_addr) &&
378 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
379 sizeof(addr.sin_addr), AF_INET))) {
380 hname = (char *)nhp->h_name;
381 if (innetgr(cname+1, hname, NULL, NULL))
385 /* Okay, strip off the domain (if we have one) */
386 if ((dot = strchr(hname, '.')) == NULL)
390 match = innetgr(cname+1, hname, NULL, NULL);
403 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
410 client_checkaddr(nfs_client *clp, struct in_addr addr)
414 switch (clp->m_type) {
416 for (i = 0; i < clp->m_naddr; i++) {
417 if (clp->m_addrlist[i].s_addr == addr.s_addr)
422 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
423 & clp->m_addrlist[1].s_addr);
429 client_gettype(char *ident)
433 if (ident[0] == '\0' || strcmp(ident, "*")==0)
434 return MCL_ANONYMOUS;
435 if (strncmp(ident, "gss/", 4) == 0)
437 if (ident[0] == '@') {
439 xlog(L_WARNING, "netgroup support not compiled in");
443 for (sp = ident; *sp; sp++) {
444 if (*sp == '*' || *sp == '?' || *sp == '[')
447 return MCL_SUBNETWORK;
448 if (*sp == '\\' && sp[1])
451 /* check for N.N.N.N */
453 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 != '.') return MCL_FQDN;
456 sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
457 /* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
458 return MCL_SUBNETWORK;