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, };
36 client_lookup(char *hname)
38 nfs_client *clp = NULL;
40 struct hostent *hp = NULL;
42 htype = client_gettype(hname);
44 if (htype == MCL_FQDN) {
45 hp = gethostbyname(hname);
46 if (hp == NULL || hp->h_addrtype != AF_INET) {
47 xlog(L_ERROR, "%s has non-inet addr", hname);
50 hp = hostent_dup (hp);
51 hname = (char *) hp->h_name;
53 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
54 if (client_check(clp, hp))
58 for (clp = clientlist[htype]; clp; clp = clp->m_next) {
59 if (strcmp(hname, clp->m_hostname)==0)
65 clp = (nfs_client *) xmalloc(sizeof(*clp));
66 memset(clp, 0, sizeof(*clp));
68 client_init(clp, hname, NULL);
72 if (htype == MCL_FQDN && clp->m_naddr == 0 && hp != NULL) {
73 char **ap = hp->h_addr_list;
76 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++)
77 clp->m_addrlist[i] = *(struct in_addr *)*ap;
88 client_dup(nfs_client *clp, struct hostent *hp)
92 new = (nfs_client *) xmalloc(sizeof(*new));
93 memcpy(new, clp, sizeof(*new));
94 new->m_type = MCL_FQDN;
96 client_init(new, (char *) hp->h_name, hp);
102 client_init(nfs_client *clp, const char *hname, struct hostent *hp)
105 strncpy(clp->m_hostname, hp->h_name,
106 sizeof (clp->m_hostname) - 1);
108 strncpy(clp->m_hostname, hname,
109 sizeof (clp->m_hostname) - 1);
111 clp->m_hostname[sizeof (clp->m_hostname) - 1] = '\0';
116 if (clp->m_type == MCL_SUBNETWORK) {
117 char *cp = strchr(clp->m_hostname, '/');
120 clp->m_addrlist[0].s_addr = inet_addr(clp->m_hostname);
121 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
127 char **ap = hp->h_addr_list;
130 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
131 clp->m_addrlist[i] = *(struct in_addr *)*ap;
138 client_add(nfs_client *clp)
142 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
143 xlog(L_FATAL, "unknown client type in client_add");
144 cpp = clientlist + clp->m_type;
146 cpp = &((*cpp)->m_next);
152 client_release(nfs_client *clp)
154 if (clp->m_count <= 0)
155 xlog(L_FATAL, "client_free: m_count <= 0!");
162 nfs_client *clp, **head;
165 for (i = 0; i < MCL_MAXTYPES; i++) {
166 head = clientlist + i;
168 *head = (clp = *head)->m_next;
175 client_find(struct hostent *hp)
180 for (i = 0; i < MCL_MAXTYPES; i++) {
181 for (clp = clientlist[i]; clp; clp = clp->m_next) {
182 if (!client_check(clp, hp))
185 if (clp->m_type == MCL_FQDN)
187 return client_dup(clp, hp);
197 * Match a host (given its hostent record) to a client record. This
198 * is usually called from mountd.
201 client_check(nfs_client *clp, struct hostent *hp)
203 char *hname = (char *) hp->h_name;
204 char *cname = clp->m_hostname;
207 switch (clp->m_type) {
210 for (ap = hp->h_addr_list; *ap; ap++) {
211 if (client_checkaddr(clp, *(struct in_addr *) *ap))
216 if (wildmat(hname, cname))
219 for (ap = hp->h_aliases; *ap; ap++)
220 if (wildmat(*ap, cname))
230 /* First, try to match the hostname without
231 * splitting off the domain */
232 if (innetgr(cname+1, hname, NULL, NULL))
235 /* Okay, strip off the domain (if we have one) */
236 if ((dot = strchr(hname, '.')) == NULL)
240 match = innetgr(cname+1, hname, NULL, dot + 1);
251 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
258 client_checkaddr(nfs_client *clp, struct in_addr addr)
262 switch (clp->m_type) {
264 for (i = 0; i < clp->m_naddr; i++) {
265 if (clp->m_addrlist[i].s_addr == addr.s_addr)
270 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
271 & clp->m_addrlist[1].s_addr);
277 client_gettype(char *ident)
281 if (ident[0] == '\0')
282 return MCL_ANONYMOUS;
283 if (ident[0] == '@') {
285 xlog(L_WARNING, "netgroup support not compiled in");
289 for (sp = ident; *sp; sp++) {
290 if (*sp == '*' || *sp == '?' || *sp == '[')
293 return MCL_SUBNETWORK;
294 if (*sp == '\\' && sp[1])