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,
65 hname = (char *) hp->h_name;
67 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
68 if (client_check(clp, hp))
72 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
73 if (strcmp(hname, clp->m_hostname)==0)
79 clp = (nfs_client *) xmalloc(sizeof(*clp));
80 memset(clp, 0, sizeof(*clp));
82 client_init(clp, hname, NULL);
86 if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
87 char **ap = hp->h_addr_list;
90 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
91 clp->m_addrlist[i] = *(struct in_addr *)*ap;
102 client_dup(nfs_client *clp, struct hostent *hp)
106 new = (nfs_client *) xmalloc(sizeof(*new));
107 memcpy(new, clp, sizeof(*new));
108 new->m_type = MCL_FQDN;
110 client_init(new, (char *) hp->h_name, hp);
116 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
119 strncpy(clp->m_hostname, hp->h_name,
120 sizeof (clp->m_hostname) - 1);
122 strncpy(clp->m_hostname, hname,
123 sizeof (clp->m_hostname) - 1);
125 clp->m_hostname[sizeof (clp->m_hostname) - 1] = '\0';
130 if (clp->m_type == MCL_SUBNETWORK) {
131 char *cp = strchr(clp->m_hostname, '/');
134 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
135 if (strchr(cp + 1, '.')) {
136 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
139 int netmask = atoi(cp + 1);
140 if (0 < netmask && netmask <= 32) {
141 clp->m_addrlist[1].s_addr =
142 htonl ((uint32_t) ~0 << (32 - netmask));
145 xlog(L_FATAL, "invalid netmask `%s' for %s",
146 cp + 1, clp->m_hostname);
154 char **ap = hp->h_addr_list;
157 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
158 clp->m_addrlist[i] = *(struct in_addr *)*ap;
165 client_add(nfs_client *clp)
169 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
170 xlog(L_FATAL, "unknown client type in client_add");
171 cpp = clientlist + clp->m_type;
173 cpp = &((*cpp)->m_next);
179 client_release(nfs_client *clp)
181 if (clp->m_count <= 0)
182 xlog(L_FATAL, "client_free: m_count <= 0!");
189 nfs_client *clp, **head;
192 for (i = 0; i < MCL_MAXTYPES; i++) {
193 head = clientlist + i;
195 *head = (clp = *head)->m_next;
202 client_find(struct hostent *hp)
207 for (i = 0; i < MCL_MAXTYPES; i++) {
208 for (clp = clientlist[i]; clp; clp = clp->m_next) {
209 if (!client_check(clp, hp))
212 if (clp->m_type == MCL_FQDN)
214 return client_dup(clp, hp);
224 * Match a host (given its hostent record) to a client record. This
225 * is usually called from mountd.
228 client_check(nfs_client *clp, struct hostent *hp)
230 char *hname = (char *) hp->h_name;
231 char *cname = clp->m_hostname;
234 switch (clp->m_type) {
237 for (ap = hp->h_addr_list; *ap; ap++) {
238 if (client_checkaddr(clp, *(struct in_addr *) *ap))
243 if (wildmat(hname, cname))
246 for (ap = hp->h_aliases; *ap; ap++)
247 if (wildmat(*ap, cname))
256 struct hostent *nhp = NULL;
257 struct sockaddr_in addr;
259 /* First, try to match the hostname without
260 * splitting off the domain */
261 if (innetgr(cname+1, hname, NULL, NULL))
264 /* If hname is ip address convert to FQDN */
265 if (inet_aton(hname, &addr.sin_addr) &&
266 (nhp = gethostbyaddr((const char *)&(addr.sin_addr),
267 sizeof(addr.sin_addr), AF_INET))) {
268 hname = (char *)nhp->h_name;
269 if (innetgr(cname+1, hname, NULL, NULL))
273 /* Okay, strip off the domain (if we have one) */
274 if ((dot = strchr(hname, '.')) == NULL)
278 match = innetgr(cname+1, hname, NULL, NULL);
289 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
296 client_checkaddr(nfs_client *clp, struct in_addr addr)
300 switch (clp->m_type) {
302 for (i = 0; i < clp->m_naddr; i++) {
303 if (clp->m_addrlist[i].s_addr == addr.s_addr)
308 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
309 & clp->m_addrlist[1].s_addr);
315 client_gettype(char *ident)
319 if (ident[0] == '\0' || strcmp(ident, "*")==0)
320 return MCL_ANONYMOUS;
321 if (ident[0] == '@') {
323 xlog(L_WARNING, "netgroup support not compiled in");
327 for (sp = ident; *sp; sp++) {
328 if (*sp == '*' || *sp == '?' || *sp == '[')
331 return MCL_SUBNETWORK;
332 if (*sp == '\\' && sp[1])