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