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 * Match a host (given its hostent record) to a client record. This
234 * is usually called from mountd.
237 client_check(nfs_client *clp, struct hostent *hp)
239 char *hname = (char *) hp->h_name;
240 char *cname = clp->m_hostname;
243 switch (clp->m_type) {
246 for (ap = hp->h_addr_list; *ap; ap++) {
247 if (client_checkaddr(clp, *(struct in_addr *) *ap))
252 if (wildmat(hname, cname))
255 for (ap = hp->h_aliases; *ap; ap++)
256 if (wildmat(*ap, cname))
265 struct hostent *nhp = NULL;
266 struct sockaddr_in addr;
268 /* First, try to match the hostname without
269 * splitting off the domain */
270 if (innetgr(cname+1, hname, NULL, NULL))
273 /* If hname is ip address convert to FQDN */
274 if (inet_aton(hname, &addr.sin_addr) &&
275 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
276 sizeof(addr.sin_addr), AF_INET))) {
277 hname = (char *)nhp->h_name;
278 if (innetgr(cname+1, hname, NULL, NULL))
282 /* Okay, strip off the domain (if we have one) */
283 if ((dot = strchr(hname, '.')) == NULL)
287 match = innetgr(cname+1, hname, NULL, NULL);
298 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
305 client_checkaddr(nfs_client *clp, struct in_addr addr)
309 switch (clp->m_type) {
311 for (i = 0; i < clp->m_naddr; i++) {
312 if (clp->m_addrlist[i].s_addr == addr.s_addr)
317 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
318 & clp->m_addrlist[1].s_addr);
324 client_gettype(char *ident)
328 if (ident[0] == '\0' || strcmp(ident, "*")==0)
329 return MCL_ANONYMOUS;
330 if (ident[0] == '@') {
332 xlog(L_WARNING, "netgroup support not compiled in");
336 for (sp = ident; *sp; sp++) {
337 if (*sp == '*' || *sp == '?' || *sp == '[')
340 return MCL_SUBNETWORK;
341 if (*sp == '\\' && sp[1])