Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.35
      1 /* $NetBSD: kern_auth.c,v 1.35 2006/12/26 10:43:44 elad Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2005, 2006 Elad Efrat <elad (at) NetBSD.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Elad Efrat.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: kern_auth.c,v 1.35 2006/12/26 10:43:44 elad Exp $");
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <sys/queue.h>
     39 #include <sys/time.h>
     40 #include <sys/proc.h>
     41 #include <sys/ucred.h>
     42 #include <sys/pool.h>
     43 #include <sys/kauth.h>
     44 #include <sys/acct.h>
     45 #include <sys/sysctl.h>
     46 #include <sys/kmem.h>
     47 
     48 /*
     49  * Credentials.
     50  */
     51 struct kauth_cred {
     52 	struct simplelock cr_lock;	/* lock on cr_refcnt */
     53 	u_int cr_refcnt;		/* reference count */
     54 	uid_t cr_uid;			/* user id */
     55 	uid_t cr_euid;			/* effective user id */
     56 	uid_t cr_svuid;			/* saved effective user id */
     57 	gid_t cr_gid;			/* group id */
     58 	gid_t cr_egid;			/* effective group id */
     59 	gid_t cr_svgid;			/* saved effective group id */
     60 	u_int cr_ngroups;		/* number of groups */
     61 	gid_t cr_groups[NGROUPS];	/* group memberships */
     62 };
     63 
     64 /*
     65  * Listener.
     66  */
     67 struct kauth_listener {
     68 	kauth_scope_callback_t		func;		/* callback */
     69 	kauth_scope_t			scope;		/* scope backpointer */
     70 	u_int				refcnt;		/* reference count */
     71 	SIMPLEQ_ENTRY(kauth_listener)	listener_next;	/* listener list */
     72 };
     73 
     74 /*
     75  * Scope.
     76  */
     77 struct kauth_scope {
     78 	const char		       *id;		/* scope name */
     79 	void			       *cookie;		/* user cookie */
     80 	u_int				nlisteners;	/* # of listeners */
     81 	SIMPLEQ_HEAD(, kauth_listener)	listenq;	/* listener list */
     82 	SIMPLEQ_ENTRY(kauth_scope)	next_scope;	/* scope list */
     83 };
     84 
     85 static POOL_INIT(kauth_cred_pool, sizeof(struct kauth_cred), 0, 0, 0,
     86     "kauthcredpl", &pool_allocator_nointr);
     87 
     88 /* List of scopes and its lock. */
     89 static SIMPLEQ_HEAD(, kauth_scope) scope_list;
     90 static struct simplelock scopes_lock;
     91 
     92 /* Built-in scopes: generic, process. */
     93 static kauth_scope_t kauth_builtin_scope_generic;
     94 static kauth_scope_t kauth_builtin_scope_system;
     95 static kauth_scope_t kauth_builtin_scope_process;
     96 static kauth_scope_t kauth_builtin_scope_network;
     97 static kauth_scope_t kauth_builtin_scope_machdep;
     98 static kauth_scope_t kauth_builtin_scope_device;
     99 
    100 static boolean_t listeners_have_been_loaded = FALSE;
    101 
    102 /* Allocate new, empty kauth credentials. */
    103 kauth_cred_t
    104 kauth_cred_alloc(void)
    105 {
    106 	kauth_cred_t cred;
    107 
    108 	cred = pool_get(&kauth_cred_pool, PR_WAITOK);
    109 	memset(cred, 0, sizeof(*cred));
    110 	simple_lock_init(&cred->cr_lock);
    111 	cred->cr_refcnt = 1;
    112 
    113 	return (cred);
    114 }
    115 
    116 /* Increment reference count to cred. */
    117 void
    118 kauth_cred_hold(kauth_cred_t cred)
    119 {
    120 	KASSERT(cred != NULL);
    121 	KASSERT(cred->cr_refcnt > 0);
    122 
    123         simple_lock(&cred->cr_lock);
    124         cred->cr_refcnt++;
    125         simple_unlock(&cred->cr_lock);
    126 }
    127 
    128 /* Decrease reference count to cred. If reached zero, free it. */
    129 void
    130 kauth_cred_free(kauth_cred_t cred)
    131 {
    132 	u_int refcnt;
    133 
    134 	KASSERT(cred != NULL);
    135 	KASSERT(cred->cr_refcnt > 0);
    136 
    137 	simple_lock(&cred->cr_lock);
    138 	refcnt = --cred->cr_refcnt;
    139 	simple_unlock(&cred->cr_lock);
    140 
    141 	if (refcnt == 0)
    142 		pool_put(&kauth_cred_pool, cred);
    143 }
    144 
    145 void
    146 kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
    147 {
    148 	KASSERT(from != NULL);
    149 	KASSERT(to != NULL);
    150 	KASSERT(from->cr_refcnt > 0);
    151 
    152 	to->cr_uid = from->cr_uid;
    153 	to->cr_euid = from->cr_euid;
    154 	to->cr_svuid = from->cr_svuid;
    155 	to->cr_gid = from->cr_gid;
    156 	to->cr_egid = from->cr_egid;
    157 	to->cr_svgid = from->cr_svgid;
    158 	to->cr_ngroups = from->cr_ngroups;
    159 	memcpy(to->cr_groups, from->cr_groups, sizeof(to->cr_groups));
    160 }
    161 
    162 /*
    163  * Duplicate cred and return a new kauth_cred_t.
    164  */
    165 kauth_cred_t
    166 kauth_cred_dup(kauth_cred_t cred)
    167 {
    168 	kauth_cred_t new_cred;
    169 
    170 	KASSERT(cred != NULL);
    171 	KASSERT(cred->cr_refcnt > 0);
    172 
    173 	new_cred = kauth_cred_alloc();
    174 
    175 	kauth_cred_clone(cred, new_cred);
    176 
    177 	return (new_cred);
    178 }
    179 
    180 /*
    181  * Similar to crcopy(), only on a kauth_cred_t.
    182  * XXX: Is this even needed? [kauth_cred_copy]
    183  */
    184 kauth_cred_t
    185 kauth_cred_copy(kauth_cred_t cred)
    186 {
    187 	kauth_cred_t new_cred;
    188 
    189 	KASSERT(cred != NULL);
    190 	KASSERT(cred->cr_refcnt > 0);
    191 
    192 	/* If the provided credentials already have one reference, use them. */
    193 	if (cred->cr_refcnt == 1)
    194 		return (cred);
    195 
    196 	new_cred = kauth_cred_alloc();
    197 
    198 	kauth_cred_clone(cred, new_cred);
    199 
    200 	kauth_cred_free(cred);
    201 
    202 	return (new_cred);
    203 }
    204 
    205 uid_t
    206 kauth_cred_getuid(kauth_cred_t cred)
    207 {
    208 	KASSERT(cred != NULL);
    209 
    210 	return (cred->cr_uid);
    211 }
    212 
    213 uid_t
    214 kauth_cred_geteuid(kauth_cred_t cred)
    215 {
    216 	KASSERT(cred != NULL);
    217 
    218 	return (cred->cr_euid);
    219 }
    220 
    221 uid_t
    222 kauth_cred_getsvuid(kauth_cred_t cred)
    223 {
    224 	KASSERT(cred != NULL);
    225 
    226 	return (cred->cr_svuid);
    227 }
    228 
    229 gid_t
    230 kauth_cred_getgid(kauth_cred_t cred)
    231 {
    232 	KASSERT(cred != NULL);
    233 
    234 	return (cred->cr_gid);
    235 }
    236 
    237 gid_t
    238 kauth_cred_getegid(kauth_cred_t cred)
    239 {
    240 	KASSERT(cred != NULL);
    241 
    242 	return (cred->cr_egid);
    243 }
    244 
    245 gid_t
    246 kauth_cred_getsvgid(kauth_cred_t cred)
    247 {
    248 	KASSERT(cred != NULL);
    249 
    250 	return (cred->cr_svgid);
    251 }
    252 
    253 void
    254 kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
    255 {
    256 	KASSERT(cred != NULL);
    257 	KASSERT(cred->cr_refcnt == 1);
    258 
    259 	cred->cr_uid = uid;
    260 }
    261 
    262 void
    263 kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
    264 {
    265 	KASSERT(cred != NULL);
    266 	KASSERT(cred->cr_refcnt == 1);
    267 
    268 	cred->cr_euid = uid;
    269 }
    270 
    271 void
    272 kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
    273 {
    274 	KASSERT(cred != NULL);
    275 	KASSERT(cred->cr_refcnt == 1);
    276 
    277 	cred->cr_svuid = uid;
    278 }
    279 
    280 void
    281 kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
    282 {
    283 	KASSERT(cred != NULL);
    284 	KASSERT(cred->cr_refcnt == 1);
    285 
    286 	cred->cr_gid = gid;
    287 }
    288 
    289 void
    290 kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
    291 {
    292 	KASSERT(cred != NULL);
    293 	KASSERT(cred->cr_refcnt == 1);
    294 
    295 	cred->cr_egid = gid;
    296 }
    297 
    298 void
    299 kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
    300 {
    301 	KASSERT(cred != NULL);
    302 	KASSERT(cred->cr_refcnt == 1);
    303 
    304 	cred->cr_svgid = gid;
    305 }
    306 
    307 /* Checks if gid is a member of the groups in cred. */
    308 int
    309 kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
    310 {
    311 	int i;
    312 
    313 	KASSERT(cred != NULL);
    314 	KASSERT(resultp != NULL);
    315 
    316 	*resultp = 0;
    317 
    318 	for (i = 0; i < cred->cr_ngroups; i++)
    319 		if (cred->cr_groups[i] == gid) {
    320 			*resultp = 1;
    321 			break;
    322 		}
    323 
    324 	return (0);
    325 }
    326 
    327 u_int
    328 kauth_cred_ngroups(kauth_cred_t cred)
    329 {
    330 	KASSERT(cred != NULL);
    331 
    332 	return (cred->cr_ngroups);
    333 }
    334 
    335 /*
    336  * Return the group at index idx from the groups in cred.
    337  */
    338 gid_t
    339 kauth_cred_group(kauth_cred_t cred, u_int idx)
    340 {
    341 	KASSERT(cred != NULL);
    342 	KASSERT(idx < cred->cr_ngroups);
    343 
    344 	return (cred->cr_groups[idx]);
    345 }
    346 
    347 /* XXX elad: gmuid is unused for now. */
    348 int
    349 kauth_cred_setgroups(kauth_cred_t cred, gid_t *grbuf, size_t len, uid_t gmuid)
    350 {
    351 	KASSERT(cred != NULL);
    352 	KASSERT(cred->cr_refcnt == 1);
    353 	KASSERT(len <= sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]));
    354 
    355 	if (len)
    356 		memcpy(cred->cr_groups, grbuf, len * sizeof(cred->cr_groups[0]));
    357 	memset(cred->cr_groups + len, 0xff,
    358 	    sizeof(cred->cr_groups) - (len * sizeof(cred->cr_groups[0])));
    359 
    360 	cred->cr_ngroups = len;
    361 
    362 	return (0);
    363 }
    364 
    365 int
    366 kauth_cred_getgroups(kauth_cred_t cred, gid_t *grbuf, size_t len)
    367 {
    368 	KASSERT(cred != NULL);
    369 	KASSERT(len <= cred->cr_ngroups);
    370 
    371 	memset(grbuf, 0xff, sizeof(*grbuf) * len);
    372 	memcpy(grbuf, cred->cr_groups, sizeof(*grbuf) * len);
    373 
    374 	return (0);
    375 }
    376 
    377 /*
    378  * Match uids in two credentials.
    379  */
    380 int
    381 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    382 {
    383 	KASSERT(cred1 != NULL);
    384 	KASSERT(cred2 != NULL);
    385 
    386 	if (cred1->cr_uid == cred2->cr_uid ||
    387 	    cred1->cr_euid == cred2->cr_uid ||
    388 	    cred1->cr_uid == cred2->cr_euid ||
    389 	    cred1->cr_euid == cred2->cr_euid)
    390 		return (1);
    391 
    392 	return (0);
    393 }
    394 
    395 u_int
    396 kauth_cred_getrefcnt(kauth_cred_t cred)
    397 {
    398 	KASSERT(cred != NULL);
    399 
    400 	return (cred->cr_refcnt);
    401 }
    402 
    403 /*
    404  * Convert userland credentials (struct uucred) to kauth_cred_t.
    405  * XXX: For NFS & puffs
    406  */
    407 void
    408 kauth_uucred_to_cred(kauth_cred_t cred, const struct uucred *uuc)
    409 {
    410 	KASSERT(cred != NULL);
    411 	KASSERT(uuc != NULL);
    412 
    413 	cred->cr_refcnt = 1;
    414 	cred->cr_uid = uuc->cr_uid;
    415 	cred->cr_euid = uuc->cr_uid;
    416 	cred->cr_svuid = uuc->cr_uid;
    417 	cred->cr_gid = uuc->cr_gid;
    418 	cred->cr_egid = uuc->cr_gid;
    419 	cred->cr_svgid = uuc->cr_gid;
    420 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    421 	kauth_cred_setgroups(cred, __UNCONST(uuc->cr_groups),
    422 	    cred->cr_ngroups, -1);
    423 }
    424 
    425 /*
    426  * Convert kauth_cred_t to userland credentials (struct uucred).
    427  * XXX: For NFS & puffs
    428  */
    429 void
    430 kauth_cred_to_uucred(struct uucred *uuc, const kauth_cred_t cred)
    431 {
    432 	KASSERT(cred != NULL);
    433 	KASSERT(uuc != NULL);
    434 	int ng;
    435 
    436 	ng = min(cred->cr_ngroups, NGROUPS);
    437 	uuc->cr_uid = cred->cr_euid;
    438 	uuc->cr_gid = cred->cr_egid;
    439 	uuc->cr_ngroups = ng;
    440 	kauth_cred_getgroups(cred, uuc->cr_groups, ng);
    441 }
    442 
    443 /*
    444  * Compare kauth_cred_t and uucred credentials.
    445  * XXX: Modelled after crcmp() for NFS.
    446  */
    447 int
    448 kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
    449 {
    450 	KASSERT(cred != NULL);
    451 	KASSERT(uuc != NULL);
    452 
    453 	if (cred->cr_euid == uuc->cr_uid &&
    454 	    cred->cr_egid == uuc->cr_gid &&
    455 	    cred->cr_ngroups == uuc->cr_ngroups) {
    456 		int i;
    457 
    458 		/* Check if all groups from uuc appear in cred. */
    459 		for (i = 0; i < uuc->cr_ngroups; i++) {
    460 			int ismember;
    461 
    462 			ismember = 0;
    463 			if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
    464 			    &ismember) != 0 || !ismember)
    465 				return (1);
    466 		}
    467 
    468 		return (0);
    469 	}
    470 
    471 	return (1);
    472 }
    473 
    474 /*
    475  * Make a struct ucred out of a kauth_cred_t.  For compatibility.
    476  */
    477 void
    478 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    479 {
    480 	KASSERT(cred != NULL);
    481 	KASSERT(uc != NULL);
    482 
    483 	uc->cr_ref = cred->cr_refcnt;
    484 	uc->cr_uid = cred->cr_euid;
    485 	uc->cr_gid = cred->cr_egid;
    486 	uc->cr_ngroups = min(cred->cr_ngroups,
    487 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    488 	memcpy(uc->cr_groups, cred->cr_groups,
    489 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    490 }
    491 
    492 /*
    493  * Make a struct pcred out of a kauth_cred_t.  For compatibility.
    494  */
    495 void
    496 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    497 {
    498 	KASSERT(cred != NULL);
    499 	KASSERT(pc != NULL);
    500 
    501 	pc->pc_ucred = NULL;
    502 	pc->p_ruid = cred->cr_uid;
    503 	pc->p_svuid = cred->cr_svuid;
    504 	pc->p_rgid = cred->cr_gid;
    505 	pc->p_svgid = cred->cr_svgid;
    506 	pc->p_refcnt = cred->cr_refcnt;
    507 }
    508 
    509 /*
    510  * Return kauth_cred_t for the current LWP.
    511  */
    512 kauth_cred_t
    513 kauth_cred_get(void)
    514 {
    515 	return (curlwp->l_cred);
    516 }
    517 
    518 /*
    519  * Returns a scope matching the provided id.
    520  * Requires the scope list lock to be held by the caller.
    521  */
    522 static kauth_scope_t
    523 kauth_ifindscope(const char *id)
    524 {
    525 	kauth_scope_t scope;
    526 
    527 	/* XXX: assert lock on scope list? */
    528 
    529 	scope = NULL;
    530 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    531 		if (strcmp(scope->id, id) == 0)
    532 			break;
    533 	}
    534 
    535 	return (scope);
    536 }
    537 
    538 /*
    539  * Register a new scope.
    540  *
    541  * id - identifier for the scope
    542  * callback - the scope's default listener
    543  * cookie - cookie to be passed to the listener(s)
    544  */
    545 kauth_scope_t
    546 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    547     void *cookie)
    548 {
    549 	kauth_scope_t scope;
    550 	kauth_listener_t listener = NULL; /* XXX gcc */
    551 
    552 	/* Sanitize input */
    553 	if (id == NULL)
    554 		return (NULL);
    555 
    556 	/* Allocate space for a new scope and listener. */
    557 	scope = kmem_alloc(sizeof(*scope), KM_SLEEP);
    558 	if (scope == NULL)
    559 		return NULL;
    560 	if (callback != NULL) {
    561 		listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    562 		if (listener == NULL) {
    563 			kmem_free(scope, sizeof(*scope));
    564 			return (NULL);
    565 		}
    566 	}
    567 
    568 	/*
    569 	 * Acquire scope list lock.
    570 	 *
    571 	 * XXXSMP insufficient locking.
    572 	 */
    573 	simple_lock(&scopes_lock);
    574 
    575 	/* Check we don't already have a scope with the same id */
    576 	if (kauth_ifindscope(id) != NULL) {
    577 		simple_unlock(&scopes_lock);
    578 
    579 		kmem_free(scope, sizeof(*scope));
    580 		if (callback != NULL)
    581 			kmem_free(listener, sizeof(*listener));
    582 
    583 		return (NULL);
    584 	}
    585 
    586 	/* Initialize new scope with parameters */
    587 	scope->id = id;
    588 	scope->cookie = cookie;
    589 	scope->nlisteners = 1;
    590 
    591 	SIMPLEQ_INIT(&scope->listenq);
    592 
    593 	/* Add default listener */
    594 	if (callback != NULL) {
    595 		listener->func = callback;
    596 		listener->scope = scope;
    597 		listener->refcnt = 0;
    598 		SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    599 	}
    600 
    601 	/* Insert scope to scopes list */
    602 	SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    603 
    604 	simple_unlock(&scopes_lock);
    605 
    606 	return (scope);
    607 }
    608 
    609 /*
    610  * Initialize the kernel authorization subsystem.
    611  *
    612  * Initialize the scopes list lock.
    613  * Register built-in scopes: generic, process.
    614  */
    615 void
    616 kauth_init(void)
    617 {
    618 	SIMPLEQ_INIT(&scope_list);
    619 	simple_lock_init(&scopes_lock);
    620 
    621 	/* Register generic scope. */
    622 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    623 	    NULL, NULL);
    624 
    625 	/* Register system scope. */
    626 	kauth_builtin_scope_system = kauth_register_scope(KAUTH_SCOPE_SYSTEM,
    627 	    NULL, NULL);
    628 
    629 	/* Register process scope. */
    630 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    631 	    NULL, NULL);
    632 
    633 	/* Register network scope. */
    634 	kauth_builtin_scope_network = kauth_register_scope(KAUTH_SCOPE_NETWORK,
    635 	    NULL, NULL);
    636 
    637 	/* Register machdep scope. */
    638 	kauth_builtin_scope_machdep = kauth_register_scope(KAUTH_SCOPE_MACHDEP,
    639 	    NULL, NULL);
    640 
    641 	/* Register device scope. */
    642 	kauth_builtin_scope_device = kauth_register_scope(KAUTH_SCOPE_DEVICE,
    643 	    NULL, NULL);
    644 }
    645 
    646 /*
    647  * Deregister a scope.
    648  * Requires scope list lock to be held by the caller.
    649  *
    650  * scope - the scope to deregister
    651  */
    652 void
    653 kauth_deregister_scope(kauth_scope_t scope)
    654 {
    655 	if (scope != NULL) {
    656 		/* Remove scope from list */
    657 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    658 	}
    659 }
    660 
    661 /*
    662  * Register a listener.
    663  *
    664  * id - scope identifier.
    665  * callback - the callback routine for the listener.
    666  * cookie - cookie to pass unmoidfied to the callback.
    667  */
    668 kauth_listener_t
    669 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    670    void *cookie)
    671 {
    672 	kauth_scope_t scope;
    673 	kauth_listener_t listener;
    674 
    675 	/*
    676 	 * Find scope struct.
    677 	 *
    678 	 * XXXSMP insufficient locking.
    679 	 */
    680 	simple_lock(&scopes_lock);
    681 	scope = kauth_ifindscope(id);
    682 	simple_unlock(&scopes_lock);
    683 	if (scope == NULL)
    684 		return (NULL);
    685 
    686 	/* Allocate listener */
    687 	listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    688 	if (listener == NULL)
    689 		return (NULL);
    690 
    691 	/* Initialize listener with parameters */
    692 	listener->func = callback;
    693 	listener->refcnt = 0;
    694 
    695 	/* Add listener to scope */
    696 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    697 
    698 	/* Raise number of listeners on scope. */
    699 	scope->nlisteners++;
    700 	listener->scope = scope;
    701 
    702 	listeners_have_been_loaded = TRUE;
    703 
    704 	return (listener);
    705 }
    706 
    707 /*
    708  * Deregister a listener.
    709  *
    710  * listener - listener reference as returned from kauth_listen_scope().
    711  */
    712 void
    713 kauth_unlisten_scope(kauth_listener_t listener)
    714 {
    715 	if (listener != NULL) {
    716 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener,
    717 		    kauth_listener, listener_next);
    718 		listener->scope->nlisteners--;
    719 	}
    720 }
    721 
    722 /*
    723  * Authorize a request.
    724  *
    725  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    726  *	   returned from kauth_register_scope().
    727  * credential - credentials of the user ("actor") making the request.
    728  * action - request identifier.
    729  * arg[0-3] - passed unmodified to listener(s).
    730  */
    731 int
    732 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    733 		       kauth_action_t action, void *arg0, void *arg1,
    734 		       void *arg2, void *arg3)
    735 {
    736 	kauth_listener_t listener;
    737 	int error, allow, fail;
    738 
    739 #if 0 /* defined(LOCKDEBUG) */
    740 	spinlock_switchcheck();
    741 	simple_lock_only_held(NULL, "kauth_authorize_action");
    742 #endif
    743 
    744 	KASSERT(cred != NULL);
    745 	KASSERT(action != 0);
    746 
    747 	/* Short-circuit requests coming from the kernel. */
    748 	if (cred == NOCRED || cred == FSCRED)
    749 		return (0);
    750 
    751 	KASSERT(scope != NULL);
    752 
    753 	if (!listeners_have_been_loaded) {
    754 		KASSERT(SIMPLEQ_EMPTY(&scope->listenq));
    755 
    756 		return (0);
    757 	}
    758 
    759 	fail = 0;
    760 	allow = 0;
    761 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    762 		error = listener->func(cred, action, scope->cookie, arg0,
    763 				       arg1, arg2, arg3);
    764 
    765 		if (error == KAUTH_RESULT_ALLOW)
    766 			allow = 1;
    767 		else if (error == KAUTH_RESULT_DENY)
    768 			fail = 1;
    769 	}
    770 
    771 	return ((allow && !fail) ? 0 : EPERM);
    772 };
    773 
    774 /*
    775  * Generic scope authorization wrapper.
    776  */
    777 int
    778 kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
    779 {
    780 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    781 	    action, arg0, NULL, NULL, NULL));
    782 }
    783 
    784 /*
    785  * System scope authorization wrapper.
    786  */
    787 int
    788 kauth_authorize_system(kauth_cred_t cred, kauth_action_t action,
    789     enum kauth_system_req req, void *arg1, void *arg2, void *arg3)
    790 {
    791 	return (kauth_authorize_action(kauth_builtin_scope_system, cred,
    792 	    action, (void *)req, arg1, arg2, arg3));
    793 }
    794 
    795 /*
    796  * Process scope authorization wrapper.
    797  */
    798 int
    799 kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
    800     struct proc *p, void *arg1, void *arg2, void *arg3)
    801 {
    802 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    803 	    action, p, arg1, arg2, arg3));
    804 }
    805 
    806 /*
    807  * Network scope authorization wrapper.
    808  */
    809 int
    810 kauth_authorize_network(kauth_cred_t cred, kauth_action_t action,
    811     enum kauth_network_req req, void *arg1, void *arg2, void *arg3)
    812 {
    813 	return (kauth_authorize_action(kauth_builtin_scope_network, cred,
    814 	    action, (void *)req, arg1, arg2, arg3));
    815 }
    816 
    817 int
    818 kauth_authorize_machdep(kauth_cred_t cred, kauth_action_t action,
    819     void *arg0, void *arg1, void *arg2, void *arg3)
    820 {
    821 	return (kauth_authorize_action(kauth_builtin_scope_machdep, cred,
    822 	    action, arg0, arg1, arg2, arg3));
    823 }
    824 
    825 int
    826 kauth_authorize_device(kauth_cred_t cred, kauth_action_t action,
    827     void *arg0, void *arg1, void *arg2, void *arg3)
    828 {
    829 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    830 	    action, arg0, arg1, arg2, arg3));
    831 }
    832 
    833 int
    834 kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t action,
    835     struct tty *tty)
    836 {
    837 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    838 	    action, tty, NULL, NULL, NULL));
    839 }
    840 
    841 int
    842 kauth_authorize_device_spec(kauth_cred_t cred, enum kauth_device_req req,
    843     struct vnode *vp)
    844 {
    845 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    846 	    KAUTH_DEVICE_RAWIO_SPEC, (void *)req, vp, NULL, NULL));
    847 }
    848 
    849 int
    850 kauth_authorize_device_passthru(kauth_cred_t cred, dev_t dev, u_long bits,
    851     void *data)
    852 {
    853 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    854 	    KAUTH_DEVICE_RAWIO_PASSTHRU, (void *)bits, (void *)(u_long)dev,
    855 	    data, NULL));
    856 }
    857