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 if (strchr(cp + 1, '.')) {
122 clp->m_addrlist[1].s_addr = inet_addr(cp+1);
125 int netmask = atoi(cp + 1);
126 if (0 < netmask && netmask <= 32) {
127 clp->m_addrlist[1].s_addr =
128 htonl ((uint32_t) ~0 << (32 - netmask));
131 xlog(L_FATAL, "invalid netmask `%s' for %s",
132 cp + 1, clp->m_hostname);
140 char **ap = hp->h_addr_list;
143 for (i = 0; *ap && i < NFSCLNT_ADDRMAX; i++, ap++) {
144 clp->m_addrlist[i] = *(struct in_addr *)*ap;
151 client_add(nfs_client *clp)
155 if (clp->m_type < 0 || clp->m_type >= MCL_MAXTYPES)
156 xlog(L_FATAL, "unknown client type in client_add");
157 cpp = clientlist + clp->m_type;
159 cpp = &((*cpp)->m_next);
165 client_release(nfs_client *clp)
167 if (clp->m_count <= 0)
168 xlog(L_FATAL, "client_free: m_count <= 0!");
175 nfs_client *clp, **head;
178 for (i = 0; i < MCL_MAXTYPES; i++) {
179 head = clientlist + i;
181 *head = (clp = *head)->m_next;
188 client_find(struct hostent *hp)
193 for (i = 0; i < MCL_MAXTYPES; i++) {
194 for (clp = clientlist[i]; clp; clp = clp->m_next) {
195 if (!client_check(clp, hp))
198 if (clp->m_type == MCL_FQDN)
200 return client_dup(clp, hp);
210 * Match a host (given its hostent record) to a client record. This
211 * is usually called from mountd.
214 client_check(nfs_client *clp, struct hostent *hp)
216 char *hname = (char *) hp->h_name;
217 char *cname = clp->m_hostname;
220 switch (clp->m_type) {
223 for (ap = hp->h_addr_list; *ap; ap++) {
224 if (client_checkaddr(clp, *(struct in_addr *) *ap))
229 if (wildmat(hname, cname))
232 for (ap = hp->h_aliases; *ap; ap++)
233 if (wildmat(*ap, cname))
243 /* First, try to match the hostname without
244 * splitting off the domain */
245 if (innetgr(cname+1, hname, NULL, NULL))
248 /* Okay, strip off the domain (if we have one) */
249 if ((dot = strchr(hname, '.')) == NULL)
253 match = innetgr(cname+1, hname, NULL, NULL);
264 xlog(L_FATAL, "internal: bad client type %d", clp->m_type);
271 client_checkaddr(nfs_client *clp, struct in_addr addr)
275 switch (clp->m_type) {
277 for (i = 0; i < clp->m_naddr; i++) {
278 if (clp->m_addrlist[i].s_addr == addr.s_addr)
283 return !((clp->m_addrlist[0].s_addr ^ addr.s_addr)
284 & clp->m_addrlist[1].s_addr);
290 client_gettype(char *ident)
294 if (ident[0] == '\0')
295 return MCL_ANONYMOUS;
296 if (ident[0] == '@') {
298 xlog(L_WARNING, "netgroup support not compiled in");
302 for (sp = ident; *sp; sp++) {
303 if (*sp == '*' || *sp == '?' || *sp == '[')
306 return MCL_SUBNETWORK;
307 if (*sp == '\\' && sp[1])