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