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