xref: /titanic_52/usr/src/uts/common/os/sid.c (revision 0d2a8e5eea8ac6ea0f5c517f0c481329b57d5459)
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 /*
30  * Sid manipulation (stubs).
31  */
32 
33 #include <sys/atomic.h>
34 #include <sys/avl.h>
35 #include <sys/cmn_err.h>
36 #include <sys/kmem.h>
37 #include <sys/mutex.h>
38 #include <sys/sid.h>
39 #include <sys/sysmacros.h>
40 #include <sys/systm.h>
41 
42 static kmutex_t sid_lock;
43 static avl_tree_t sid_tree;
44 static boolean_t sid_inited = B_FALSE;
45 
46 static ksiddomain_t
47 *ksid_enterdomain(const char *dom)
48 {
49 	size_t len = strlen(dom) + 1;
50 	ksiddomain_t *res;
51 
52 	ASSERT(MUTEX_HELD(&sid_lock));
53 	res = kmem_alloc(sizeof (ksiddomain_t), KM_SLEEP);
54 	res->kd_len = (uint_t)len;
55 	res->kd_name = kmem_alloc(len, KM_SLEEP);
56 	bcopy(dom, res->kd_name, len);
57 
58 	res->kd_ref = 1;
59 
60 	avl_add(&sid_tree, res);
61 
62 	return (res);
63 }
64 
65 void
66 ksid_hold(ksid_t *ks)
67 {
68 	if (ks->ks_domain != NULL)
69 		ksiddomain_hold(ks->ks_domain);
70 }
71 
72 void
73 ksid_rele(ksid_t *ks)
74 {
75 	if (ks->ks_domain != NULL)
76 		ksiddomain_rele(ks->ks_domain);
77 }
78 
79 void
80 ksiddomain_hold(ksiddomain_t *kd)
81 {
82 	atomic_add_32(&kd->kd_ref, 1);
83 }
84 
85 void
86 ksiddomain_rele(ksiddomain_t *kd)
87 {
88 	if (atomic_add_32_nv(&kd->kd_ref, -1) == 0) {
89 		/*
90 		 * The kd reference can only be incremented from 0 when
91 		 * the sid_lock is held; so we lock and then check need to
92 		 * check for 0 again.
93 		 */
94 		mutex_enter(&sid_lock);
95 		if (kd->kd_ref == 0) {
96 			avl_remove(&sid_tree, kd);
97 			kmem_free(kd->kd_name, kd->kd_len);
98 			kmem_free(kd, sizeof (*kd));
99 		}
100 		mutex_exit(&sid_lock);
101 	}
102 }
103 
104 void
105 ksidlist_hold(ksidlist_t *ksl)
106 {
107 	atomic_add_32(&ksl->ksl_ref, 1);
108 }
109 
110 void
111 ksidlist_rele(ksidlist_t *ksl)
112 {
113 	if (atomic_add_32_nv(&ksl->ksl_ref, -1) == 0) {
114 		int i;
115 
116 		for (i = 0; i < ksl->ksl_nsid; i++)
117 			ksid_rele(&ksl->ksl_sids[i]);
118 
119 		kmem_free(ksl, KSIDLIST_MEM(ksl->ksl_nsid));
120 	}
121 }
122 
123 static int
124 ksid_cmp(const void *a, const void *b)
125 {
126 	const ksiddomain_t *ap = a;
127 	const ksiddomain_t *bp = b;
128 	int res;
129 
130 	res = strcmp(ap->kd_name, bp->kd_name);
131 	if (res > 0)
132 		return (1);
133 	if (res != 0)
134 		return (-1);
135 	return (0);
136 }
137 
138 /*
139  * Lookup the named domain in the AVL tree.
140  * If no entry is found, add the domain to the AVL tree.
141  * The domain is returned held and needs to be released
142  * when done.
143  */
144 ksiddomain_t
145 *ksid_lookupdomain(const char *dom)
146 {
147 	ksiddomain_t *res;
148 	ksiddomain_t tmpl;
149 
150 	mutex_enter(&sid_lock);
151 
152 	if (!sid_inited) {
153 		avl_create(&sid_tree, ksid_cmp, sizeof (ksiddomain_t),
154 		    offsetof(ksiddomain_t, kd_link));
155 
156 		res = ksid_enterdomain(dom);
157 		sid_inited = B_TRUE;
158 		mutex_exit(&sid_lock);
159 		return (res);
160 	}
161 
162 	tmpl.kd_name = (char *)dom;
163 
164 	res = avl_find(&sid_tree, &tmpl, NULL);
165 	if (res == NULL) {
166 		res = ksid_enterdomain(dom);
167 	} else {
168 		ksiddomain_hold(res);
169 	}
170 
171 	mutex_exit(&sid_lock);
172 	return (res);
173 }
174 
175 const char *
176 ksid_getdomain(ksid_t *ks)
177 {
178 	return (ks->ks_domain->kd_name);
179 }
180 
181 uint_t
182 ksid_getrid(ksid_t *ks)
183 {
184 	return (ks->ks_rid);
185 }
186 
187 int
188 ksid_lookup(uid_t id, ksid_t *res)
189 {
190 	uid_t tmp;
191 
192 	if (idmap_call_byid(id, res) == -1)
193 		return (-1);
194 
195 	tmp = idmap_call_bysid(res);
196 	if (tmp != id)
197 		cmn_err(CE_WARN, "The idmapper has gone bonkers");
198 	res->ks_id = id;
199 
200 	return (0);
201 }
202 
203 credsid_t *
204 kcrsid_alloc(void)
205 {
206 	credsid_t *kcr = kmem_zalloc(sizeof (*kcr), KM_SLEEP);
207 	kcr->kr_ref = 1;
208 	return (kcr);
209 }
210 
211 /*
212  * Returns a credsid_t with a refcount of 1.
213  */
214 static credsid_t *
215 kcrsid_dup(credsid_t *org)
216 {
217 	credsid_t *new;
218 	ksid_index_t ki;
219 
220 	if (org == NULL)
221 		return (kcrsid_alloc());
222 	if (org->kr_ref == 1)
223 		return (org);
224 	new = kcrsid_alloc();
225 
226 	/* Copy, then update reference counts */
227 	*new = *org;
228 	new->kr_ref = 1;
229 	for (ki = 0; ki < KSID_COUNT; ki++)
230 		ksid_hold(&new->kr_sidx[ki]);
231 
232 	if (new->kr_sidlist != NULL)
233 		ksidlist_hold(new->kr_sidlist);
234 
235 	kcrsid_rele(org);
236 	return (new);
237 }
238 
239 void
240 kcrsid_hold(credsid_t *kcr)
241 {
242 	atomic_add_32(&kcr->kr_ref, 1);
243 }
244 
245 void
246 kcrsid_rele(credsid_t *kcr)
247 {
248 	if (atomic_add_32_nv(&kcr->kr_ref, -1) == 0) {
249 		ksid_index_t i;
250 
251 		for (i = 0; i < KSID_COUNT; i++)
252 			ksid_rele(&kcr->kr_sidx[i]);
253 
254 		if (kcr->kr_sidlist != NULL)
255 			ksidlist_rele(kcr->kr_sidlist);
256 
257 		kmem_free(kcr, sizeof (*kcr));
258 	}
259 }
260 
261 /*
262  * Copy the SID credential into a previously allocated piece of memory.
263  */
264 void
265 kcrsidcopy_to(const credsid_t *okcr, credsid_t *nkcr)
266 {
267 	int i;
268 
269 	ASSERT(nkcr->kr_ref == 1);
270 
271 	if (okcr == NULL)
272 		return;
273 	*nkcr = *okcr;
274 	for (i = 0; i < KSID_COUNT; i++)
275 		ksid_hold(&nkcr->kr_sidx[i]);
276 	if (nkcr->kr_sidlist != NULL)
277 		ksidlist_hold(nkcr->kr_sidlist);
278 	nkcr->kr_ref = 1;
279 }
280 
281 static int
282 kcrsid_sidcount(const credsid_t *kcr)
283 {
284 	int cnt = 0;
285 	int i;
286 
287 	if (kcr == NULL)
288 		return (0);
289 
290 	for (i = 0; i < KSID_COUNT; i++)
291 		if (kcr->kr_sidx[i].ks_domain != NULL)
292 			cnt++;
293 
294 	if (kcr->kr_sidlist != NULL)
295 		cnt += kcr->kr_sidlist->ksl_nsid;
296 	return (cnt);
297 }
298 
299 /*
300  * Argument needs to be a ksid_t with a properly held ks_domain reference.
301  */
302 credsid_t *
303 kcrsid_setsid(credsid_t *okcr, ksid_t *ksp, ksid_index_t i)
304 {
305 	int ocnt = kcrsid_sidcount(okcr);
306 	credsid_t *nkcr;
307 
308 	/*
309 	 * Unset the particular ksid; if there are no other SIDs or if this
310 	 * is the last SID, remove the auxilary data structure.
311 	 */
312 	if (ksp == NULL) {
313 		if (ocnt == 0 ||
314 		    (ocnt == 1 && okcr->kr_sidx[i].ks_domain != NULL)) {
315 			if (okcr != NULL)
316 				kcrsid_rele(okcr);
317 			return (NULL);
318 		}
319 	}
320 	nkcr = kcrsid_dup(okcr);
321 	ksid_rele(&nkcr->kr_sidx[i]);
322 	if (ksp == NULL)
323 		bzero(&nkcr->kr_sidx[i], sizeof (ksid_t));
324 	else
325 		nkcr->kr_sidx[i] = *ksp;
326 
327 	return (nkcr);
328 }
329 
330 /*
331  * Argument needs to be a ksidlist_t with properly held ks_domain references
332  * and a reference count taking the new reference into account.
333  */
334 credsid_t *
335 kcrsid_setsidlist(credsid_t *okcr, ksidlist_t *ksl)
336 {
337 	int ocnt = kcrsid_sidcount(okcr);
338 	credsid_t *nkcr;
339 
340 	/*
341 	 * Unset the sidlist; if there are no further SIDs, remove the
342 	 * auxilary data structure.
343 	 */
344 	if (ksl == NULL) {
345 		if (ocnt == 0 || (okcr->kr_sidlist != NULL &&
346 		    ocnt == okcr->kr_sidlist->ksl_nsid)) {
347 			if (okcr != NULL)
348 				kcrsid_rele(okcr);
349 			return (NULL);
350 		}
351 	}
352 	nkcr = kcrsid_dup(okcr);
353 	if (nkcr->kr_sidlist != NULL)
354 		ksidlist_rele(nkcr->kr_sidlist);
355 
356 	nkcr->kr_sidlist = ksl;
357 	return (nkcr);
358 }
359 
360 ksidlist_t *
361 kcrsid_gidstosids(int ngrp, gid_t *grp)
362 {
363 	int i;
364 	ksidlist_t *list;
365 	int cnt;
366 
367 	if (ngrp == 0)
368 		return (NULL);
369 
370 	cnt = 0;
371 	list = kmem_zalloc(KSIDLIST_MEM(ngrp), KM_SLEEP);
372 
373 	list->ksl_nsid = ngrp;
374 	list->ksl_ref = 1;
375 
376 	for (i = 0; i < ngrp; i++) {
377 		if (grp[i] > MAXUID) {
378 			list->ksl_neid++;
379 			if (ksid_lookup(grp[i], &list->ksl_sids[i]) != 0) {
380 				while (--i >= 0)
381 					ksid_rele(&list->ksl_sids[i]);
382 				cnt = 0;
383 				break;
384 			}
385 			cnt++;
386 		} else {
387 			list->ksl_sids[i].ks_id = grp[i];
388 		}
389 	}
390 	if (cnt == 0) {
391 		kmem_free(list, KSIDLIST_MEM(ngrp));
392 		return (NULL);
393 	}
394 	return (list);
395 }
396