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