Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.38
      1 /* $NetBSD: kern_auth.c,v 1.38 2007/01/15 17:45:32 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. 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.38 2007/01/15 17:45:32 elad 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 void
    203 kauth_proc_fork(struct proc *parent, struct proc *child)
    204 {
    205 	/* mutex_enter(&parent->p_mutex); */
    206 	kauth_cred_hold(parent->p_cred);
    207 	child->p_cred = parent->p_cred;
    208 	/* mutex_exit(&parent->p_mutex); */
    209 }
    210 
    211 uid_t
    212 kauth_cred_getuid(kauth_cred_t cred)
    213 {
    214 	KASSERT(cred != NULL);
    215 
    216 	return (cred->cr_uid);
    217 }
    218 
    219 uid_t
    220 kauth_cred_geteuid(kauth_cred_t cred)
    221 {
    222 	KASSERT(cred != NULL);
    223 
    224 	return (cred->cr_euid);
    225 }
    226 
    227 uid_t
    228 kauth_cred_getsvuid(kauth_cred_t cred)
    229 {
    230 	KASSERT(cred != NULL);
    231 
    232 	return (cred->cr_svuid);
    233 }
    234 
    235 gid_t
    236 kauth_cred_getgid(kauth_cred_t cred)
    237 {
    238 	KASSERT(cred != NULL);
    239 
    240 	return (cred->cr_gid);
    241 }
    242 
    243 gid_t
    244 kauth_cred_getegid(kauth_cred_t cred)
    245 {
    246 	KASSERT(cred != NULL);
    247 
    248 	return (cred->cr_egid);
    249 }
    250 
    251 gid_t
    252 kauth_cred_getsvgid(kauth_cred_t cred)
    253 {
    254 	KASSERT(cred != NULL);
    255 
    256 	return (cred->cr_svgid);
    257 }
    258 
    259 void
    260 kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
    261 {
    262 	KASSERT(cred != NULL);
    263 	KASSERT(cred->cr_refcnt == 1);
    264 
    265 	cred->cr_uid = uid;
    266 }
    267 
    268 void
    269 kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
    270 {
    271 	KASSERT(cred != NULL);
    272 	KASSERT(cred->cr_refcnt == 1);
    273 
    274 	cred->cr_euid = uid;
    275 }
    276 
    277 void
    278 kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
    279 {
    280 	KASSERT(cred != NULL);
    281 	KASSERT(cred->cr_refcnt == 1);
    282 
    283 	cred->cr_svuid = uid;
    284 }
    285 
    286 void
    287 kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
    288 {
    289 	KASSERT(cred != NULL);
    290 	KASSERT(cred->cr_refcnt == 1);
    291 
    292 	cred->cr_gid = gid;
    293 }
    294 
    295 void
    296 kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
    297 {
    298 	KASSERT(cred != NULL);
    299 	KASSERT(cred->cr_refcnt == 1);
    300 
    301 	cred->cr_egid = gid;
    302 }
    303 
    304 void
    305 kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
    306 {
    307 	KASSERT(cred != NULL);
    308 	KASSERT(cred->cr_refcnt == 1);
    309 
    310 	cred->cr_svgid = gid;
    311 }
    312 
    313 /* Checks if gid is a member of the groups in cred. */
    314 int
    315 kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
    316 {
    317 	int i;
    318 
    319 	KASSERT(cred != NULL);
    320 	KASSERT(resultp != NULL);
    321 
    322 	*resultp = 0;
    323 
    324 	for (i = 0; i < cred->cr_ngroups; i++)
    325 		if (cred->cr_groups[i] == gid) {
    326 			*resultp = 1;
    327 			break;
    328 		}
    329 
    330 	return (0);
    331 }
    332 
    333 u_int
    334 kauth_cred_ngroups(kauth_cred_t cred)
    335 {
    336 	KASSERT(cred != NULL);
    337 
    338 	return (cred->cr_ngroups);
    339 }
    340 
    341 /*
    342  * Return the group at index idx from the groups in cred.
    343  */
    344 gid_t
    345 kauth_cred_group(kauth_cred_t cred, u_int idx)
    346 {
    347 	KASSERT(cred != NULL);
    348 	KASSERT(idx < cred->cr_ngroups);
    349 
    350 	return (cred->cr_groups[idx]);
    351 }
    352 
    353 /* XXX elad: gmuid is unused for now. */
    354 int
    355 kauth_cred_setgroups(kauth_cred_t cred, gid_t *grbuf, size_t len, uid_t gmuid)
    356 {
    357 	KASSERT(cred != NULL);
    358 	KASSERT(cred->cr_refcnt == 1);
    359 	KASSERT(len <= sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]));
    360 
    361 	if (len)
    362 		memcpy(cred->cr_groups, grbuf, len * sizeof(cred->cr_groups[0]));
    363 	memset(cred->cr_groups + len, 0xff,
    364 	    sizeof(cred->cr_groups) - (len * sizeof(cred->cr_groups[0])));
    365 
    366 	cred->cr_ngroups = len;
    367 
    368 	return (0);
    369 }
    370 
    371 int
    372 kauth_cred_getgroups(kauth_cred_t cred, gid_t *grbuf, size_t len)
    373 {
    374 	KASSERT(cred != NULL);
    375 	KASSERT(len <= cred->cr_ngroups);
    376 
    377 	memset(grbuf, 0xff, sizeof(*grbuf) * len);
    378 	memcpy(grbuf, cred->cr_groups, sizeof(*grbuf) * len);
    379 
    380 	return (0);
    381 }
    382 
    383 /*
    384  * Match uids in two credentials.
    385  */
    386 int
    387 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    388 {
    389 	KASSERT(cred1 != NULL);
    390 	KASSERT(cred2 != NULL);
    391 
    392 	if (cred1->cr_uid == cred2->cr_uid ||
    393 	    cred1->cr_euid == cred2->cr_uid ||
    394 	    cred1->cr_uid == cred2->cr_euid ||
    395 	    cred1->cr_euid == cred2->cr_euid)
    396 		return (1);
    397 
    398 	return (0);
    399 }
    400 
    401 u_int
    402 kauth_cred_getrefcnt(kauth_cred_t cred)
    403 {
    404 	KASSERT(cred != NULL);
    405 
    406 	return (cred->cr_refcnt);
    407 }
    408 
    409 /*
    410  * Convert userland credentials (struct uucred) to kauth_cred_t.
    411  * XXX: For NFS & puffs
    412  */
    413 void
    414 kauth_uucred_to_cred(kauth_cred_t cred, const struct uucred *uuc)
    415 {
    416 	KASSERT(cred != NULL);
    417 	KASSERT(uuc != NULL);
    418 
    419 	cred->cr_refcnt = 1;
    420 	cred->cr_uid = uuc->cr_uid;
    421 	cred->cr_euid = uuc->cr_uid;
    422 	cred->cr_svuid = uuc->cr_uid;
    423 	cred->cr_gid = uuc->cr_gid;
    424 	cred->cr_egid = uuc->cr_gid;
    425 	cred->cr_svgid = uuc->cr_gid;
    426 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    427 	kauth_cred_setgroups(cred, __UNCONST(uuc->cr_groups),
    428 	    cred->cr_ngroups, -1);
    429 }
    430 
    431 /*
    432  * Convert kauth_cred_t to userland credentials (struct uucred).
    433  * XXX: For NFS & puffs
    434  */
    435 void
    436 kauth_cred_to_uucred(struct uucred *uuc, const kauth_cred_t cred)
    437 {
    438 	KASSERT(cred != NULL);
    439 	KASSERT(uuc != NULL);
    440 	int ng;
    441 
    442 	ng = min(cred->cr_ngroups, NGROUPS);
    443 	uuc->cr_uid = cred->cr_euid;
    444 	uuc->cr_gid = cred->cr_egid;
    445 	uuc->cr_ngroups = ng;
    446 	kauth_cred_getgroups(cred, uuc->cr_groups, ng);
    447 }
    448 
    449 /*
    450  * Compare kauth_cred_t and uucred credentials.
    451  * XXX: Modelled after crcmp() for NFS.
    452  */
    453 int
    454 kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
    455 {
    456 	KASSERT(cred != NULL);
    457 	KASSERT(uuc != NULL);
    458 
    459 	if (cred->cr_euid == uuc->cr_uid &&
    460 	    cred->cr_egid == uuc->cr_gid &&
    461 	    cred->cr_ngroups == uuc->cr_ngroups) {
    462 		int i;
    463 
    464 		/* Check if all groups from uuc appear in cred. */
    465 		for (i = 0; i < uuc->cr_ngroups; i++) {
    466 			int ismember;
    467 
    468 			ismember = 0;
    469 			if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
    470 			    &ismember) != 0 || !ismember)
    471 				return (1);
    472 		}
    473 
    474 		return (0);
    475 	}
    476 
    477 	return (1);
    478 }
    479 
    480 /*
    481  * Make a struct ucred out of a kauth_cred_t.  For compatibility.
    482  */
    483 void
    484 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    485 {
    486 	KASSERT(cred != NULL);
    487 	KASSERT(uc != NULL);
    488 
    489 	uc->cr_ref = cred->cr_refcnt;
    490 	uc->cr_uid = cred->cr_euid;
    491 	uc->cr_gid = cred->cr_egid;
    492 	uc->cr_ngroups = min(cred->cr_ngroups,
    493 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    494 	memcpy(uc->cr_groups, cred->cr_groups,
    495 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    496 }
    497 
    498 /*
    499  * Make a struct pcred out of a kauth_cred_t.  For compatibility.
    500  */
    501 void
    502 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    503 {
    504 	KASSERT(cred != NULL);
    505 	KASSERT(pc != NULL);
    506 
    507 	pc->pc_ucred = NULL;
    508 	pc->p_ruid = cred->cr_uid;
    509 	pc->p_svuid = cred->cr_svuid;
    510 	pc->p_rgid = cred->cr_gid;
    511 	pc->p_svgid = cred->cr_svgid;
    512 	pc->p_refcnt = cred->cr_refcnt;
    513 }
    514 
    515 /*
    516  * Return kauth_cred_t for the current LWP.
    517  */
    518 kauth_cred_t
    519 kauth_cred_get(void)
    520 {
    521 	return (curlwp->l_cred);
    522 }
    523 
    524 /*
    525  * Returns a scope matching the provided id.
    526  * Requires the scope list lock to be held by the caller.
    527  */
    528 static kauth_scope_t
    529 kauth_ifindscope(const char *id)
    530 {
    531 	kauth_scope_t scope;
    532 
    533 	/* XXX: assert lock on scope list? */
    534 
    535 	scope = NULL;
    536 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    537 		if (strcmp(scope->id, id) == 0)
    538 			break;
    539 	}
    540 
    541 	return (scope);
    542 }
    543 
    544 /*
    545  * Register a new scope.
    546  *
    547  * id - identifier for the scope
    548  * callback - the scope's default listener
    549  * cookie - cookie to be passed to the listener(s)
    550  */
    551 kauth_scope_t
    552 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    553     void *cookie)
    554 {
    555 	kauth_scope_t scope;
    556 	kauth_listener_t listener = NULL; /* XXX gcc */
    557 
    558 	/* Sanitize input */
    559 	if (id == NULL)
    560 		return (NULL);
    561 
    562 	/* Allocate space for a new scope and listener. */
    563 	scope = kmem_alloc(sizeof(*scope), KM_SLEEP);
    564 	if (scope == NULL)
    565 		return NULL;
    566 	if (callback != NULL) {
    567 		listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    568 		if (listener == NULL) {
    569 			kmem_free(scope, sizeof(*scope));
    570 			return (NULL);
    571 		}
    572 	}
    573 
    574 	/*
    575 	 * Acquire scope list lock.
    576 	 *
    577 	 * XXXSMP insufficient locking.
    578 	 */
    579 	simple_lock(&scopes_lock);
    580 
    581 	/* Check we don't already have a scope with the same id */
    582 	if (kauth_ifindscope(id) != NULL) {
    583 		simple_unlock(&scopes_lock);
    584 
    585 		kmem_free(scope, sizeof(*scope));
    586 		if (callback != NULL)
    587 			kmem_free(listener, sizeof(*listener));
    588 
    589 		return (NULL);
    590 	}
    591 
    592 	/* Initialize new scope with parameters */
    593 	scope->id = id;
    594 	scope->cookie = cookie;
    595 	scope->nlisteners = 1;
    596 
    597 	SIMPLEQ_INIT(&scope->listenq);
    598 
    599 	/* Add default listener */
    600 	if (callback != NULL) {
    601 		listener->func = callback;
    602 		listener->scope = scope;
    603 		listener->refcnt = 0;
    604 		SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    605 	}
    606 
    607 	/* Insert scope to scopes list */
    608 	SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    609 
    610 	simple_unlock(&scopes_lock);
    611 
    612 	return (scope);
    613 }
    614 
    615 /*
    616  * Initialize the kernel authorization subsystem.
    617  *
    618  * Initialize the scopes list lock.
    619  * Register built-in scopes: generic, process.
    620  */
    621 void
    622 kauth_init(void)
    623 {
    624 	SIMPLEQ_INIT(&scope_list);
    625 	simple_lock_init(&scopes_lock);
    626 
    627 	/* Register generic scope. */
    628 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    629 	    NULL, NULL);
    630 
    631 	/* Register system scope. */
    632 	kauth_builtin_scope_system = kauth_register_scope(KAUTH_SCOPE_SYSTEM,
    633 	    NULL, NULL);
    634 
    635 	/* Register process scope. */
    636 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    637 	    NULL, NULL);
    638 
    639 	/* Register network scope. */
    640 	kauth_builtin_scope_network = kauth_register_scope(KAUTH_SCOPE_NETWORK,
    641 	    NULL, NULL);
    642 
    643 	/* Register machdep scope. */
    644 	kauth_builtin_scope_machdep = kauth_register_scope(KAUTH_SCOPE_MACHDEP,
    645 	    NULL, NULL);
    646 
    647 	/* Register device scope. */
    648 	kauth_builtin_scope_device = kauth_register_scope(KAUTH_SCOPE_DEVICE,
    649 	    NULL, NULL);
    650 }
    651 
    652 /*
    653  * Deregister a scope.
    654  * Requires scope list lock to be held by the caller.
    655  *
    656  * scope - the scope to deregister
    657  */
    658 void
    659 kauth_deregister_scope(kauth_scope_t scope)
    660 {
    661 	if (scope != NULL) {
    662 		/* Remove scope from list */
    663 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    664 		kmem_free(scope, sizeof(*scope));
    665 	}
    666 }
    667 
    668 /*
    669  * Register a listener.
    670  *
    671  * id - scope identifier.
    672  * callback - the callback routine for the listener.
    673  * cookie - cookie to pass unmoidfied to the callback.
    674  */
    675 kauth_listener_t
    676 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    677    void *cookie)
    678 {
    679 	kauth_scope_t scope;
    680 	kauth_listener_t listener;
    681 
    682 	/*
    683 	 * Find scope struct.
    684 	 *
    685 	 * XXXSMP insufficient locking.
    686 	 */
    687 	simple_lock(&scopes_lock);
    688 	scope = kauth_ifindscope(id);
    689 	simple_unlock(&scopes_lock);
    690 	if (scope == NULL)
    691 		return (NULL);
    692 
    693 	/* Allocate listener */
    694 	listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    695 	if (listener == NULL)
    696 		return (NULL);
    697 
    698 	/* Initialize listener with parameters */
    699 	listener->func = callback;
    700 	listener->refcnt = 0;
    701 
    702 	/* Add listener to scope */
    703 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    704 
    705 	/* Raise number of listeners on scope. */
    706 	scope->nlisteners++;
    707 	listener->scope = scope;
    708 
    709 	listeners_have_been_loaded = TRUE;
    710 
    711 	return (listener);
    712 }
    713 
    714 /*
    715  * Deregister a listener.
    716  *
    717  * listener - listener reference as returned from kauth_listen_scope().
    718  */
    719 void
    720 kauth_unlisten_scope(kauth_listener_t listener)
    721 {
    722 	if (listener != NULL) {
    723 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener,
    724 		    kauth_listener, listener_next);
    725 		listener->scope->nlisteners--;
    726 		kmem_free(listener, sizeof(*listener));
    727 	}
    728 }
    729 
    730 /*
    731  * Authorize a request.
    732  *
    733  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    734  *	   returned from kauth_register_scope().
    735  * credential - credentials of the user ("actor") making the request.
    736  * action - request identifier.
    737  * arg[0-3] - passed unmodified to listener(s).
    738  */
    739 int
    740 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    741 		       kauth_action_t action, void *arg0, void *arg1,
    742 		       void *arg2, void *arg3)
    743 {
    744 	kauth_listener_t listener;
    745 	int error, allow, fail;
    746 
    747 #if 0 /* defined(LOCKDEBUG) */
    748 	spinlock_switchcheck();
    749 	simple_lock_only_held(NULL, "kauth_authorize_action");
    750 #endif
    751 
    752 	KASSERT(cred != NULL);
    753 	KASSERT(action != 0);
    754 
    755 	/* Short-circuit requests coming from the kernel. */
    756 	if (cred == NOCRED || cred == FSCRED)
    757 		return (0);
    758 
    759 	KASSERT(scope != NULL);
    760 
    761 	if (!listeners_have_been_loaded) {
    762 		KASSERT(SIMPLEQ_EMPTY(&scope->listenq));
    763 
    764 		return (0);
    765 	}
    766 
    767 	fail = 0;
    768 	allow = 0;
    769 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    770 		error = listener->func(cred, action, scope->cookie, arg0,
    771 				       arg1, arg2, arg3);
    772 
    773 		if (error == KAUTH_RESULT_ALLOW)
    774 			allow = 1;
    775 		else if (error == KAUTH_RESULT_DENY)
    776 			fail = 1;
    777 	}
    778 
    779 	return ((allow && !fail) ? 0 : EPERM);
    780 };
    781 
    782 /*
    783  * Generic scope authorization wrapper.
    784  */
    785 int
    786 kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
    787 {
    788 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    789 	    action, arg0, NULL, NULL, NULL));
    790 }
    791 
    792 /*
    793  * System scope authorization wrapper.
    794  */
    795 int
    796 kauth_authorize_system(kauth_cred_t cred, kauth_action_t action,
    797     enum kauth_system_req req, void *arg1, void *arg2, void *arg3)
    798 {
    799 	return (kauth_authorize_action(kauth_builtin_scope_system, cred,
    800 	    action, (void *)req, arg1, arg2, arg3));
    801 }
    802 
    803 /*
    804  * Process scope authorization wrapper.
    805  */
    806 int
    807 kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
    808     struct proc *p, void *arg1, void *arg2, void *arg3)
    809 {
    810 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    811 	    action, p, arg1, arg2, arg3));
    812 }
    813 
    814 /*
    815  * Network scope authorization wrapper.
    816  */
    817 int
    818 kauth_authorize_network(kauth_cred_t cred, kauth_action_t action,
    819     enum kauth_network_req req, void *arg1, void *arg2, void *arg3)
    820 {
    821 	return (kauth_authorize_action(kauth_builtin_scope_network, cred,
    822 	    action, (void *)req, arg1, arg2, arg3));
    823 }
    824 
    825 int
    826 kauth_authorize_machdep(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_machdep, cred,
    830 	    action, arg0, arg1, arg2, arg3));
    831 }
    832 
    833 int
    834 kauth_authorize_device(kauth_cred_t cred, kauth_action_t action,
    835     void *arg0, void *arg1, void *arg2, void *arg3)
    836 {
    837 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    838 	    action, arg0, arg1, arg2, arg3));
    839 }
    840 
    841 int
    842 kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t action,
    843     struct tty *tty)
    844 {
    845 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    846 	    action, tty, NULL, NULL, NULL));
    847 }
    848 
    849 int
    850 kauth_authorize_device_spec(kauth_cred_t cred, enum kauth_device_req req,
    851     struct vnode *vp)
    852 {
    853 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    854 	    KAUTH_DEVICE_RAWIO_SPEC, (void *)req, vp, NULL, NULL));
    855 }
    856 
    857 int
    858 kauth_authorize_device_passthru(kauth_cred_t cred, dev_t dev, u_long bits,
    859     void *data)
    860 {
    861 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    862 	    KAUTH_DEVICE_RAWIO_PASSTHRU, (void *)bits, (void *)(u_long)dev,
    863 	    data, NULL));
    864 }
    865