xref: /titanic_52/usr/src/cmd/fs.d/nfs/mountd/netgroup.c (revision 6a1af1a67532df169a657cce07140be64bdea084)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <string.h>
33 #include <syslog.h>
34 #include <sys/param.h>
35 #include <rpc/rpc.h>
36 #include <sys/stat.h>
37 #include <netconfig.h>
38 #include <netdir.h>
39 #include <sys/file.h>
40 #include <sys/time.h>
41 #include <sys/errno.h>
42 #include <sys/resource.h>
43 #include <rpcsvc/mount.h>
44 #include <sys/pathconf.h>
45 #include <sys/systeminfo.h>
46 #include <sys/utsname.h>
47 #include <signal.h>
48 #include <locale.h>
49 #include <unistd.h>
50 #include <thread.h>
51 #include <sharefs/share.h>
52 #include "../lib/sharetab.h"
53 #include "mountd.h"
54 
55 struct cache_entry {
56 	char	*cache_host;
57 	time_t	cache_time;
58 	int	cache_belong;
59 	char	**cache_grl;
60 	int	cache_grc;
61 	struct cache_entry *cache_next;
62 };
63 
64 static struct cache_entry *cache_head;
65 
66 #define	VALID_TIME	60  /* seconds */
67 
68 static rwlock_t cache_lock;	/* protect the cache chain */
69 
70 static void cache_free(struct cache_entry *entry);
71 static int cache_check(char *host, char **grl, int grc, int *belong);
72 static void cache_enter(char *host, char **grl, int grc, int belong);
73 
74 
75 void
76 netgroup_init()
77 {
78 	(void) rwlock_init(&cache_lock, USYNC_THREAD, NULL);
79 }
80 
81 /*
82  * Check whether any of the hostnames in clnames are
83  * members (or non-members) of the netgroups in glist.
84  * Since the innetgr lookup is rather expensive, the
85  * result is cached. The cached entry is valid only
86  * for VALID_TIME seconds.  This works well because
87  * typically these lookups occur in clusters when
88  * a client is mounting.
89  *
90  * Note that this routine establishes a host membership
91  * in a list of netgroups - we've no idea just which
92  * netgroup in the list it is a member of.
93  *
94  * glist is a character array containing grc strings
95  * representing netgroup names (optionally prefixed
96  * with '-'). Each string is ended with '\0'  and
97  * followed immediately by the next string.
98  */
99 int
100 netgroup_check(struct nd_hostservlist *clnames, char  *glist, int grc)
101 {
102 	char **grl;
103 	char *gr;
104 	int nhosts = clnames->h_cnt;
105 	char *host0, *host;
106 	int i, j, n;
107 	int response;
108 	int belong = 0;
109 	static char *domain;
110 
111 	if (domain == NULL) {
112 		int	ssize;
113 
114 		domain = exmalloc(SYS_NMLN);
115 		ssize = sysinfo(SI_SRPC_DOMAIN, domain, SYS_NMLN);
116 		if (ssize > SYS_NMLN) {
117 			free(domain);
118 			domain = exmalloc(ssize);
119 			ssize = sysinfo(SI_SRPC_DOMAIN, domain, ssize);
120 		}
121 		/* Check for error in syscall or NULL domain name */
122 		if (ssize <= 1) {
123 			syslog(LOG_ERR, "No default domain set");
124 			return (0);
125 		}
126 	}
127 
128 	grl = calloc(grc, sizeof (char *));
129 	if (grl == NULL)
130 		return (0);
131 
132 	for (i = 0, gr = glist; i < grc && !belong; ) {
133 		/*
134 		 * If the netgroup name has a '-' prepended
135 		 * then a match of this name implies a failure
136 		 * instead of success.
137 		 */
138 		response = (*gr != '-') ? 1 : 0;
139 
140 		/*
141 		 * Subsequent names with or without a '-' (but no mix)
142 		 * can be grouped together for a single check.
143 		 */
144 		for (n = 0; i < grc; i++, n++, gr += strlen(gr) + 1) {
145 			if (response && *gr == '-' || !response && *gr != '-')
146 				break;
147 
148 			grl[n] = response ? gr : gr + 1;
149 		}
150 
151 		host0 = clnames->h_hostservs[0].h_host;
152 
153 		/*
154 		 * If not in cache check the netgroup for each
155 		 * of the hosts names (usually just one).
156 		 * Enter the result into the cache.
157 		 */
158 		if (!cache_check(host0, grl, n, &belong)) {
159 			for (j = 0; j < nhosts && !belong; j++) {
160 				host = clnames->h_hostservs[j].h_host;
161 
162 				if (__multi_innetgr(n, grl,
163 					1, &host,
164 					0, NULL,
165 					1, &domain))
166 					belong = 1;
167 			}
168 
169 			cache_enter(host0, grl, n, belong);
170 		}
171 	}
172 
173 	free(grl);
174 	return (belong ? response : 0);
175 }
176 
177 /*
178  * Free a cache entry and all entries
179  * further down the chain since they
180  * will also be expired.
181  */
182 static void
183 cache_free(struct cache_entry *entry)
184 {
185 	struct cache_entry *ce, *next;
186 	int i;
187 
188 	for (ce = entry; ce; ce = next) {
189 		if (ce->cache_host)
190 			free(ce->cache_host);
191 		for (i = 0; i < ce->cache_grc; i++)
192 			if (ce->cache_grl[i])
193 				free(ce->cache_grl[i]);
194 		if (ce->cache_grl)
195 			free(ce->cache_grl);
196 		next = ce->cache_next;
197 		free(ce);
198 	}
199 }
200 
201 /*
202  * Search the entries in the cache chain looking
203  * for an entry with a matching hostname and group
204  * list.  If a match is found then return the "belong"
205  * value which may be 1 or 0 depending on whether the
206  * client is a member of the list or not.  This is
207  * both a positive and negative cache.
208  *
209  * Cache entries have a validity of VALID_TIME seconds.
210  * If we find an expired entry then blow away the entry
211  * and the rest of the chain since entries further down
212  * the chain will be expired too because we always add
213  * new entries to the head of the chain.
214  */
215 static int
216 cache_check(char *host, char **grl, int grc, int *belong)
217 {
218 	struct cache_entry *ce, *prev;
219 	time_t timenow = time(NULL);
220 	int i;
221 
222 	(void) rw_rdlock(&cache_lock);
223 
224 	for (ce = cache_head; ce; ce = ce->cache_next) {
225 
226 		/*
227 		 * If we find a stale entry, there can't
228 		 * be any valid entries from here on.
229 		 * Acquire a write lock, search the chain again
230 		 * and delete the stale entry and all following
231 		 * entries.
232 		 */
233 		if (timenow > ce->cache_time) {
234 			(void) rw_unlock(&cache_lock);
235 			(void) rw_wrlock(&cache_lock);
236 
237 			for (prev = NULL, ce = cache_head; ce;
238 			    prev = ce, ce = ce->cache_next)
239 				if (timenow > ce->cache_time)
240 					break;
241 
242 			if (ce != NULL) {
243 				if (prev)
244 					prev->cache_next = NULL;
245 				else
246 					cache_head = NULL;
247 
248 				cache_free(ce);
249 			}
250 			(void) rw_unlock(&cache_lock);
251 
252 			return (0);
253 		}
254 		if (ce->cache_grc != grc)
255 			continue;	/* no match */
256 
257 		if (strcasecmp(host, ce->cache_host) != 0)
258 			continue;	/* no match */
259 
260 		for (i = 0; i < grc; i++)
261 			if (strcasecmp(ce->cache_grl[i], grl[i]) != 0)
262 				break;	/* no match */
263 		if (i < grc)
264 			continue;
265 
266 		*belong = ce->cache_belong;
267 		(void) rw_unlock(&cache_lock);
268 
269 		return (1);
270 	}
271 
272 	(void) rw_unlock(&cache_lock);
273 
274 	return (0);
275 }
276 
277 /*
278  * Put a new entry in the cache chain by
279  * prepending it to the front.
280  * If there isn't enough memory then just give up.
281  */
282 static void
283 cache_enter(char *host, char **grl, int grc, int belong)
284 {
285 	struct cache_entry *entry;
286 	int i;
287 
288 	entry = malloc(sizeof (*entry));
289 	if (entry == NULL)
290 		return;
291 
292 	(void) memset((caddr_t)entry, 0, sizeof (*entry));
293 	entry->cache_host = strdup(host);
294 	if (entry->cache_host == NULL) {
295 		cache_free(entry);
296 		return;
297 	}
298 
299 	entry->cache_time = time(NULL) + VALID_TIME;
300 	entry->cache_belong = belong;
301 	entry->cache_grl = malloc(grc * sizeof (char *));
302 	if (entry->cache_grl == NULL) {
303 		cache_free(entry);
304 		return;
305 	}
306 
307 	for (i = 0; i < grc; i++) {
308 		entry->cache_grl[i] = strdup(grl[i]);
309 		if (entry->cache_grl[i] == NULL) {
310 			entry->cache_grc = i;
311 			cache_free(entry);
312 			return;
313 		}
314 	}
315 
316 	entry->cache_grc = grc;
317 
318 	(void) rw_wrlock(&cache_lock);
319 	entry->cache_next = cache_head;
320 	cache_head = entry;
321 	(void) rw_unlock(&cache_lock);
322 }
323 
324 /*
325  * Full cache flush
326  */
327 void
328 netgrp_cache_flush(void)
329 {
330 	(void) rw_wrlock(&cache_lock);
331 	cache_free(cache_head);
332 	cache_head = NULL;
333 	(void) rw_unlock(&cache_lock);
334 }
335