Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.40
      1 /* $NetBSD: kern_auth.c,v 1.40 2007/01/26 23:50:36 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.40 2007/01/26 23:50:36 elad Exp $");
     32 
     33 #include <sys/types.h>
     34 #include <sys/param.h>
     35 #include <sys/queue.h>
     36 #include <sys/proc.h>
     37 #include <sys/ucred.h>
     38 #include <sys/pool.h>
     39 #include <sys/kauth.h>
     40 #include <sys/kmem.h>
     41 
     42 /*
     43  * Credentials.
     44  */
     45 struct kauth_cred {
     46 	struct simplelock cr_lock;	/* lock on cr_refcnt */
     47 	u_int cr_refcnt;		/* reference count */
     48 	uid_t cr_uid;			/* user id */
     49 	uid_t cr_euid;			/* effective user id */
     50 	uid_t cr_svuid;			/* saved effective user id */
     51 	gid_t cr_gid;			/* group id */
     52 	gid_t cr_egid;			/* effective group id */
     53 	gid_t cr_svgid;			/* saved effective group id */
     54 	u_int cr_ngroups;		/* number of groups */
     55 	gid_t cr_groups[NGROUPS];	/* group memberships */
     56 };
     57 
     58 /*
     59  * Listener.
     60  */
     61 struct kauth_listener {
     62 	kauth_scope_callback_t		func;		/* callback */
     63 	kauth_scope_t			scope;		/* scope backpointer */
     64 	u_int				refcnt;		/* reference count */
     65 	SIMPLEQ_ENTRY(kauth_listener)	listener_next;	/* listener list */
     66 };
     67 
     68 /*
     69  * Scope.
     70  */
     71 struct kauth_scope {
     72 	const char		       *id;		/* scope name */
     73 	void			       *cookie;		/* user cookie */
     74 	u_int				nlisteners;	/* # of listeners */
     75 	SIMPLEQ_HEAD(, kauth_listener)	listenq;	/* listener list */
     76 	SIMPLEQ_ENTRY(kauth_scope)	next_scope;	/* scope list */
     77 };
     78 
     79 static POOL_INIT(kauth_cred_pool, sizeof(struct kauth_cred), 0, 0, 0,
     80     "kauthcredpl", &pool_allocator_nointr);
     81 
     82 /* List of scopes and its lock. */
     83 static SIMPLEQ_HEAD(, kauth_scope) scope_list;
     84 static struct simplelock scopes_lock;
     85 
     86 /* Built-in scopes: generic, process. */
     87 static kauth_scope_t kauth_builtin_scope_generic;
     88 static kauth_scope_t kauth_builtin_scope_system;
     89 static kauth_scope_t kauth_builtin_scope_process;
     90 static kauth_scope_t kauth_builtin_scope_network;
     91 static kauth_scope_t kauth_builtin_scope_machdep;
     92 static kauth_scope_t kauth_builtin_scope_device;
     93 
     94 static unsigned int nsecmodels = 0;
     95 
     96 /* Allocate new, empty kauth credentials. */
     97 kauth_cred_t
     98 kauth_cred_alloc(void)
     99 {
    100 	kauth_cred_t cred;
    101 
    102 	cred = pool_get(&kauth_cred_pool, PR_WAITOK);
    103 	memset(cred, 0, sizeof(*cred));
    104 	simple_lock_init(&cred->cr_lock);
    105 	cred->cr_refcnt = 1;
    106 
    107 	return (cred);
    108 }
    109 
    110 /* Increment reference count to cred. */
    111 void
    112 kauth_cred_hold(kauth_cred_t cred)
    113 {
    114 	KASSERT(cred != NULL);
    115 	KASSERT(cred->cr_refcnt > 0);
    116 
    117         simple_lock(&cred->cr_lock);
    118         cred->cr_refcnt++;
    119         simple_unlock(&cred->cr_lock);
    120 }
    121 
    122 /* Decrease reference count to cred. If reached zero, free it. */
    123 void
    124 kauth_cred_free(kauth_cred_t cred)
    125 {
    126 	u_int refcnt;
    127 
    128 	KASSERT(cred != NULL);
    129 	KASSERT(cred->cr_refcnt > 0);
    130 
    131 	simple_lock(&cred->cr_lock);
    132 	refcnt = --cred->cr_refcnt;
    133 	simple_unlock(&cred->cr_lock);
    134 
    135 	if (refcnt == 0)
    136 		pool_put(&kauth_cred_pool, cred);
    137 }
    138 
    139 void
    140 kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
    141 {
    142 	KASSERT(from != NULL);
    143 	KASSERT(to != NULL);
    144 	KASSERT(from->cr_refcnt > 0);
    145 
    146 	to->cr_uid = from->cr_uid;
    147 	to->cr_euid = from->cr_euid;
    148 	to->cr_svuid = from->cr_svuid;
    149 	to->cr_gid = from->cr_gid;
    150 	to->cr_egid = from->cr_egid;
    151 	to->cr_svgid = from->cr_svgid;
    152 	to->cr_ngroups = from->cr_ngroups;
    153 	memcpy(to->cr_groups, from->cr_groups, sizeof(to->cr_groups));
    154 }
    155 
    156 /*
    157  * Duplicate cred and return a new kauth_cred_t.
    158  */
    159 kauth_cred_t
    160 kauth_cred_dup(kauth_cred_t cred)
    161 {
    162 	kauth_cred_t new_cred;
    163 
    164 	KASSERT(cred != NULL);
    165 	KASSERT(cred->cr_refcnt > 0);
    166 
    167 	new_cred = kauth_cred_alloc();
    168 
    169 	kauth_cred_clone(cred, new_cred);
    170 
    171 	return (new_cred);
    172 }
    173 
    174 /*
    175  * Similar to crcopy(), only on a kauth_cred_t.
    176  * XXX: Is this even needed? [kauth_cred_copy]
    177  */
    178 kauth_cred_t
    179 kauth_cred_copy(kauth_cred_t cred)
    180 {
    181 	kauth_cred_t new_cred;
    182 
    183 	KASSERT(cred != NULL);
    184 	KASSERT(cred->cr_refcnt > 0);
    185 
    186 	/* If the provided credentials already have one reference, use them. */
    187 	if (cred->cr_refcnt == 1)
    188 		return (cred);
    189 
    190 	new_cred = kauth_cred_alloc();
    191 
    192 	kauth_cred_clone(cred, new_cred);
    193 
    194 	kauth_cred_free(cred);
    195 
    196 	return (new_cred);
    197 }
    198 
    199 void
    200 kauth_proc_fork(struct proc *parent, struct proc *child)
    201 {
    202 	/* mutex_enter(&parent->p_mutex); */
    203 	kauth_cred_hold(parent->p_cred);
    204 	child->p_cred = parent->p_cred;
    205 	/* mutex_exit(&parent->p_mutex); */
    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, uid_t gmuid)
    353 {
    354 	KASSERT(cred != NULL);
    355 	KASSERT(cred->cr_refcnt == 1);
    356 	KASSERT(len <= sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]));
    357 
    358 	if (len)
    359 		memcpy(cred->cr_groups, grbuf, len * sizeof(cred->cr_groups[0]));
    360 	memset(cred->cr_groups + len, 0xff,
    361 	    sizeof(cred->cr_groups) - (len * sizeof(cred->cr_groups[0])));
    362 
    363 	cred->cr_ngroups = len;
    364 
    365 	return (0);
    366 }
    367 
    368 int
    369 kauth_cred_getgroups(kauth_cred_t cred, gid_t *grbuf, size_t len)
    370 {
    371 	KASSERT(cred != NULL);
    372 	KASSERT(len <= cred->cr_ngroups);
    373 
    374 	memset(grbuf, 0xff, sizeof(*grbuf) * len);
    375 	memcpy(grbuf, cred->cr_groups, sizeof(*grbuf) * len);
    376 
    377 	return (0);
    378 }
    379 
    380 /*
    381  * Match uids in two credentials.
    382  */
    383 int
    384 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    385 {
    386 	KASSERT(cred1 != NULL);
    387 	KASSERT(cred2 != NULL);
    388 
    389 	if (cred1->cr_uid == cred2->cr_uid ||
    390 	    cred1->cr_euid == cred2->cr_uid ||
    391 	    cred1->cr_uid == cred2->cr_euid ||
    392 	    cred1->cr_euid == cred2->cr_euid)
    393 		return (1);
    394 
    395 	return (0);
    396 }
    397 
    398 u_int
    399 kauth_cred_getrefcnt(kauth_cred_t cred)
    400 {
    401 	KASSERT(cred != NULL);
    402 
    403 	return (cred->cr_refcnt);
    404 }
    405 
    406 /*
    407  * Convert userland credentials (struct uucred) to kauth_cred_t.
    408  * XXX: For NFS & puffs
    409  */
    410 void
    411 kauth_uucred_to_cred(kauth_cred_t cred, const struct uucred *uuc)
    412 {
    413 	KASSERT(cred != NULL);
    414 	KASSERT(uuc != NULL);
    415 
    416 	cred->cr_refcnt = 1;
    417 	cred->cr_uid = uuc->cr_uid;
    418 	cred->cr_euid = uuc->cr_uid;
    419 	cred->cr_svuid = uuc->cr_uid;
    420 	cred->cr_gid = uuc->cr_gid;
    421 	cred->cr_egid = uuc->cr_gid;
    422 	cred->cr_svgid = uuc->cr_gid;
    423 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    424 	kauth_cred_setgroups(cred, __UNCONST(uuc->cr_groups),
    425 	    cred->cr_ngroups, -1);
    426 }
    427 
    428 /*
    429  * Convert kauth_cred_t to userland credentials (struct uucred).
    430  * XXX: For NFS & puffs
    431  */
    432 void
    433 kauth_cred_to_uucred(struct uucred *uuc, const kauth_cred_t cred)
    434 {
    435 	KASSERT(cred != NULL);
    436 	KASSERT(uuc != NULL);
    437 	int ng;
    438 
    439 	ng = min(cred->cr_ngroups, NGROUPS);
    440 	uuc->cr_uid = cred->cr_euid;
    441 	uuc->cr_gid = cred->cr_egid;
    442 	uuc->cr_ngroups = ng;
    443 	kauth_cred_getgroups(cred, uuc->cr_groups, ng);
    444 }
    445 
    446 /*
    447  * Compare kauth_cred_t and uucred credentials.
    448  * XXX: Modelled after crcmp() for NFS.
    449  */
    450 int
    451 kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
    452 {
    453 	KASSERT(cred != NULL);
    454 	KASSERT(uuc != NULL);
    455 
    456 	if (cred->cr_euid == uuc->cr_uid &&
    457 	    cred->cr_egid == uuc->cr_gid &&
    458 	    cred->cr_ngroups == uuc->cr_ngroups) {
    459 		int i;
    460 
    461 		/* Check if all groups from uuc appear in cred. */
    462 		for (i = 0; i < uuc->cr_ngroups; i++) {
    463 			int ismember;
    464 
    465 			ismember = 0;
    466 			if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
    467 			    &ismember) != 0 || !ismember)
    468 				return (1);
    469 		}
    470 
    471 		return (0);
    472 	}
    473 
    474 	return (1);
    475 }
    476 
    477 /*
    478  * Make a struct ucred out of a kauth_cred_t.  For compatibility.
    479  */
    480 void
    481 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    482 {
    483 	KASSERT(cred != NULL);
    484 	KASSERT(uc != NULL);
    485 
    486 	uc->cr_ref = cred->cr_refcnt;
    487 	uc->cr_uid = cred->cr_euid;
    488 	uc->cr_gid = cred->cr_egid;
    489 	uc->cr_ngroups = min(cred->cr_ngroups,
    490 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    491 	memcpy(uc->cr_groups, cred->cr_groups,
    492 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    493 }
    494 
    495 /*
    496  * Make a struct pcred out of a kauth_cred_t.  For compatibility.
    497  */
    498 void
    499 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    500 {
    501 	KASSERT(cred != NULL);
    502 	KASSERT(pc != NULL);
    503 
    504 	pc->pc_ucred = NULL;
    505 	pc->p_ruid = cred->cr_uid;
    506 	pc->p_svuid = cred->cr_svuid;
    507 	pc->p_rgid = cred->cr_gid;
    508 	pc->p_svgid = cred->cr_svgid;
    509 	pc->p_refcnt = cred->cr_refcnt;
    510 }
    511 
    512 /*
    513  * Return kauth_cred_t for the current LWP.
    514  */
    515 kauth_cred_t
    516 kauth_cred_get(void)
    517 {
    518 	return (curlwp->l_cred);
    519 }
    520 
    521 /*
    522  * Returns a scope matching the provided id.
    523  * Requires the scope list lock to be held by the caller.
    524  */
    525 static kauth_scope_t
    526 kauth_ifindscope(const char *id)
    527 {
    528 	kauth_scope_t scope;
    529 
    530 	/* XXX: assert lock on scope list? */
    531 
    532 	scope = NULL;
    533 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    534 		if (strcmp(scope->id, id) == 0)
    535 			break;
    536 	}
    537 
    538 	return (scope);
    539 }
    540 
    541 /*
    542  * Register a new scope.
    543  *
    544  * id - identifier for the scope
    545  * callback - the scope's default listener
    546  * cookie - cookie to be passed to the listener(s)
    547  */
    548 kauth_scope_t
    549 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    550     void *cookie)
    551 {
    552 	kauth_scope_t scope;
    553 	kauth_listener_t listener = NULL; /* XXX gcc */
    554 
    555 	/* Sanitize input */
    556 	if (id == NULL)
    557 		return (NULL);
    558 
    559 	/* Allocate space for a new scope and listener. */
    560 	scope = kmem_alloc(sizeof(*scope), KM_SLEEP);
    561 	if (scope == NULL)
    562 		return NULL;
    563 	if (callback != NULL) {
    564 		listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    565 		if (listener == NULL) {
    566 			kmem_free(scope, sizeof(*scope));
    567 			return (NULL);
    568 		}
    569 	}
    570 
    571 	/*
    572 	 * Acquire scope list lock.
    573 	 *
    574 	 * XXXSMP insufficient locking.
    575 	 */
    576 	simple_lock(&scopes_lock);
    577 
    578 	/* Check we don't already have a scope with the same id */
    579 	if (kauth_ifindscope(id) != NULL) {
    580 		simple_unlock(&scopes_lock);
    581 
    582 		kmem_free(scope, sizeof(*scope));
    583 		if (callback != NULL)
    584 			kmem_free(listener, sizeof(*listener));
    585 
    586 		return (NULL);
    587 	}
    588 
    589 	/* Initialize new scope with parameters */
    590 	scope->id = id;
    591 	scope->cookie = cookie;
    592 	scope->nlisteners = 1;
    593 
    594 	SIMPLEQ_INIT(&scope->listenq);
    595 
    596 	/* Add default listener */
    597 	if (callback != NULL) {
    598 		listener->func = callback;
    599 		listener->scope = scope;
    600 		listener->refcnt = 0;
    601 		SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    602 	}
    603 
    604 	/* Insert scope to scopes list */
    605 	SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    606 
    607 	simple_unlock(&scopes_lock);
    608 
    609 	return (scope);
    610 }
    611 
    612 /*
    613  * Initialize the kernel authorization subsystem.
    614  *
    615  * Initialize the scopes list lock.
    616  * Register built-in scopes: generic, process.
    617  */
    618 void
    619 kauth_init(void)
    620 {
    621 	SIMPLEQ_INIT(&scope_list);
    622 	simple_lock_init(&scopes_lock);
    623 
    624 	/* Register generic scope. */
    625 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    626 	    NULL, NULL);
    627 
    628 	/* Register system scope. */
    629 	kauth_builtin_scope_system = kauth_register_scope(KAUTH_SCOPE_SYSTEM,
    630 	    NULL, NULL);
    631 
    632 	/* Register process scope. */
    633 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    634 	    NULL, NULL);
    635 
    636 	/* Register network scope. */
    637 	kauth_builtin_scope_network = kauth_register_scope(KAUTH_SCOPE_NETWORK,
    638 	    NULL, NULL);
    639 
    640 	/* Register machdep scope. */
    641 	kauth_builtin_scope_machdep = kauth_register_scope(KAUTH_SCOPE_MACHDEP,
    642 	    NULL, NULL);
    643 
    644 	/* Register device scope. */
    645 	kauth_builtin_scope_device = kauth_register_scope(KAUTH_SCOPE_DEVICE,
    646 	    NULL, NULL);
    647 }
    648 
    649 /*
    650  * Deregister a scope.
    651  * Requires scope list lock to be held by the caller.
    652  *
    653  * scope - the scope to deregister
    654  */
    655 void
    656 kauth_deregister_scope(kauth_scope_t scope)
    657 {
    658 	if (scope != NULL) {
    659 		/* Remove scope from list */
    660 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    661 		kmem_free(scope, sizeof(*scope));
    662 	}
    663 }
    664 
    665 /*
    666  * Register a listener.
    667  *
    668  * id - scope identifier.
    669  * callback - the callback routine for the listener.
    670  * cookie - cookie to pass unmoidfied to the callback.
    671  */
    672 kauth_listener_t
    673 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    674    void *cookie)
    675 {
    676 	kauth_scope_t scope;
    677 	kauth_listener_t listener;
    678 
    679 	/*
    680 	 * Find scope struct.
    681 	 *
    682 	 * XXXSMP insufficient locking.
    683 	 */
    684 	simple_lock(&scopes_lock);
    685 	scope = kauth_ifindscope(id);
    686 	simple_unlock(&scopes_lock);
    687 	if (scope == NULL)
    688 		return (NULL);
    689 
    690 	/* Allocate listener */
    691 	listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    692 	if (listener == NULL)
    693 		return (NULL);
    694 
    695 	/* Initialize listener with parameters */
    696 	listener->func = callback;
    697 	listener->refcnt = 0;
    698 
    699 	/* Add listener to scope */
    700 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    701 
    702 	/* Raise number of listeners on scope. */
    703 	scope->nlisteners++;
    704 	listener->scope = scope;
    705 
    706 	return (listener);
    707 }
    708 
    709 /*
    710  * Deregister a listener.
    711  *
    712  * listener - listener reference as returned from kauth_listen_scope().
    713  */
    714 void
    715 kauth_unlisten_scope(kauth_listener_t listener)
    716 {
    717 	if (listener != NULL) {
    718 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener,
    719 		    kauth_listener, listener_next);
    720 		listener->scope->nlisteners--;
    721 		kmem_free(listener, sizeof(*listener));
    722 	}
    723 }
    724 
    725 /*
    726  * Authorize a request.
    727  *
    728  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    729  *	   returned from kauth_register_scope().
    730  * credential - credentials of the user ("actor") making the request.
    731  * action - request identifier.
    732  * arg[0-3] - passed unmodified to listener(s).
    733  */
    734 int
    735 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    736 		       kauth_action_t action, void *arg0, void *arg1,
    737 		       void *arg2, void *arg3)
    738 {
    739 	kauth_listener_t listener;
    740 	int error, allow, fail;
    741 
    742 #if 0 /* defined(LOCKDEBUG) */
    743 	spinlock_switchcheck();
    744 	simple_lock_only_held(NULL, "kauth_authorize_action");
    745 #endif
    746 
    747 	KASSERT(cred != NULL);
    748 	KASSERT(action != 0);
    749 
    750 	/* Short-circuit requests coming from the kernel. */
    751 	if (cred == NOCRED || cred == FSCRED)
    752 		return (0);
    753 
    754 	KASSERT(scope != NULL);
    755 
    756 	fail = 0;
    757 	allow = 0;
    758 
    759 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    760 		error = listener->func(cred, action, scope->cookie, arg0,
    761 				       arg1, arg2, arg3);
    762 
    763 		if (error == KAUTH_RESULT_ALLOW)
    764 			allow = 1;
    765 		else if (error == KAUTH_RESULT_DENY)
    766 			fail = 1;
    767 	}
    768 
    769 	if (fail)
    770 		return (EPERM);
    771 
    772 	if (allow)
    773 		return (0);
    774 
    775 	if (!nsecmodels)
    776 		return (0);
    777 
    778 	return (EPERM);
    779 };
    780 
    781 /*
    782  * Generic scope authorization wrapper.
    783  */
    784 int
    785 kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
    786 {
    787 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    788 	    action, arg0, NULL, NULL, NULL));
    789 }
    790 
    791 /*
    792  * System scope authorization wrapper.
    793  */
    794 int
    795 kauth_authorize_system(kauth_cred_t cred, kauth_action_t action,
    796     enum kauth_system_req req, void *arg1, void *arg2, void *arg3)
    797 {
    798 	return (kauth_authorize_action(kauth_builtin_scope_system, cred,
    799 	    action, (void *)req, arg1, arg2, arg3));
    800 }
    801 
    802 /*
    803  * Process scope authorization wrapper.
    804  */
    805 int
    806 kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
    807     struct proc *p, void *arg1, void *arg2, void *arg3)
    808 {
    809 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    810 	    action, p, arg1, arg2, arg3));
    811 }
    812 
    813 /*
    814  * Network scope authorization wrapper.
    815  */
    816 int
    817 kauth_authorize_network(kauth_cred_t cred, kauth_action_t action,
    818     enum kauth_network_req req, void *arg1, void *arg2, void *arg3)
    819 {
    820 	return (kauth_authorize_action(kauth_builtin_scope_network, cred,
    821 	    action, (void *)req, arg1, arg2, arg3));
    822 }
    823 
    824 int
    825 kauth_authorize_machdep(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_machdep, cred,
    829 	    action, arg0, arg1, arg2, arg3));
    830 }
    831 
    832 int
    833 kauth_authorize_device(kauth_cred_t cred, kauth_action_t action,
    834     void *arg0, void *arg1, void *arg2, void *arg3)
    835 {
    836 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    837 	    action, arg0, arg1, arg2, arg3));
    838 }
    839 
    840 int
    841 kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t action,
    842     struct tty *tty)
    843 {
    844 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    845 	    action, tty, NULL, NULL, NULL));
    846 }
    847 
    848 int
    849 kauth_authorize_device_spec(kauth_cred_t cred, enum kauth_device_req req,
    850     struct vnode *vp)
    851 {
    852 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    853 	    KAUTH_DEVICE_RAWIO_SPEC, (void *)req, vp, NULL, NULL));
    854 }
    855 
    856 int
    857 kauth_authorize_device_passthru(kauth_cred_t cred, dev_t dev, u_long bits,
    858     void *data)
    859 {
    860 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    861 	    KAUTH_DEVICE_RAWIO_PASSTHRU, (void *)bits, (void *)(u_long)dev,
    862 	    data, NULL));
    863 }
    864 
    865 void
    866 secmodel_register(void)
    867 {
    868 	KASSERT(nsecmodels + 1 != 0);
    869 
    870 	nsecmodels++;
    871 }
    872 
    873 void
    874 secmodel_deregister(void)
    875 {
    876 	KASSERT(nsecmodels != 0);
    877 
    878 	nsecmodels--;
    879 }
    880