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, '/');
143 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
144 if (strchr(cp + 1, '.')) {
145 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
148 int netmask = atoi(cp + 1);
149 if (0 < netmask && netmask <= 32) {
150 clp->m_addrlist[1].s_addr =
151 htonl ((uint32_t) ~0 << (32 - netmask));
154 xlog(L_FATAL, "invalid netmask `%s' for %s",
155 cp + 1, clp->m_hostname);
163 char **ap = hp->h_addr_list;
166 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
167 clp->m_addrlist[i] = *(struct in_addr *)*ap;
174 client_add(nfs_client *clp)
178 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
179 xlog(L_FATAL, "unknown client type in client_add");
180 cpp = clientlist + clp->m_type;
182 cpp = &((*cpp)->m_next);
188 client_release(nfs_client *clp)
190 if (clp->m_count <= 0)
191 xlog(L_FATAL, "client_free: m_count <= 0!");
198 nfs_client *clp, **head;
201 for (i = 0; i < MCL_MAXTYPES; i++) {
202 head = clientlist + i;
204 *head = (clp = *head)->m_next;
211 client_find(struct hostent *hp)
216 for (i = 0; i < MCL_MAXTYPES; i++) {
217 for (clp = clientlist[i]; clp; clp = clp->m_next) {
218 if (!client_check(clp, hp))
221 if (clp->m_type == MCL_FQDN)
223 return client_dup(clp, hp);
233 * Find client name given an IP address
234 * This is found by gathering all known names that match that IP address,
235 * sorting them and joining them with '+'
238 static char *add_name(char *old, char *add);
241 client_compose(struct in_addr addr)
243 struct hostent *he = NULL;
247 if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
248 he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
250 he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
252 for (i = 0 ; i < MCL_MAXTYPES; i++) {
254 for (clp = clientlist[i]; clp ; clp = clp->m_next) {
255 if (!client_check(clp, he))
257 name = add_name(name, clp->m_hostname);
264 client_member(char *client, char *name)
266 /* check if "client" (a ',' separated list of names)
267 * contains 'name' as a member
269 int l = strlen(name);
271 if (strncmp(client, name, l) == 0 &&
272 (client[l] == ',' || client[l] == '\0'))
274 client = strchr(client, ',');
284 name_cmp(char *a, 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, char *add)
299 int 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)
331 * Match a host (given its hostent record) to a client record. This
332 * is usually called from mountd.
335 client_check(nfs_client *clp, struct hostent *hp)
337 char *hname = (char *) hp->h_name;
338 char *cname = clp->m_hostname;
341 switch (clp->m_type) {
344 for (ap = hp->h_addr_list; *ap; ap++) {
345 if (client_checkaddr(clp, *(struct in_addr *) *ap))
350 if (wildmat(hname, cname))
353 for (ap = hp->h_aliases; *ap; ap++)
354 if (wildmat(*ap, cname))
363 struct hostent *nhp = NULL;
364 struct sockaddr_in addr;
366 /* First, try to match the hostname without
367 * splitting off the domain */
368 if (innetgr(cname+1, hname, NULL, NULL))
371 /* If hname is ip address convert to FQDN */
372 if (inet_aton(hname, &addr.sin_addr) &&
373 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
374 sizeof(addr.sin_addr), AF_INET))) {
375 hname = (char *)nhp->h_name;
376 if (innetgr(cname+1, hname, NULL, NULL))
380 /* Okay, strip off the domain (if we have one) */
381 if ((dot = strchr(hname, '.')) == NULL)
385 match = innetgr(cname+1, hname, NULL, NULL);
396 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
403 client_checkaddr(nfs_client *clp, struct in_addr addr)
407 switch (clp->m_type) {
409 for (i = 0; i < clp->m_naddr; i++) {
410 if (clp->m_addrlist[i].s_addr == addr.s_addr)
415 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
416 & clp->m_addrlist[1].s_addr);
422 client_gettype(char *ident)
426 if (ident[0] == '\0' || strcmp(ident, "*")==0)
427 return MCL_ANONYMOUS;
428 if (ident[0] == '@') {
430 xlog(L_WARNING, "netgroup support not compiled in");
434 for (sp = ident; *sp; sp++) {
435 if (*sp == '*' || *sp == '?' || *sp == '[')
438 return MCL_SUBNETWORK;
439 if (*sp == '\\' && sp[1])