Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.1.2.23
      1 /* $NetBSD: kern_auth.c,v 1.1.2.23 2006/03/12 23:41:07 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_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
    308 {
    309 	int high, low, mid;
    310 
    311 	KASSERT(cred != NULL);
    312 	KASSERT(gid >= 0 && gid <= GID_MAX);
    313 	KASSERT(resultp != NULL);
    314 
    315 	*resultp = 0;
    316 
    317 	high = cred->cr_ngroups;
    318 	low = -1;
    319 
    320 	while (high - low > 1) {
    321 		mid = (high + low) / 2;
    322 		if (cred->cr_groups[mid] == gid) {
    323 			*resultp = 1;
    324 			break;
    325 		}
    326 		if (cred->cr_groups[mid] > gid)
    327 			high = mid;
    328 		else
    329 			low = mid;
    330 	}
    331 
    332 	return (0);
    333 }
    334 
    335 uint16_t
    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, uint16_t idx)
    348 {
    349 	KASSERT(cred != NULL);
    350 	KASSERT(idx < cred->cr_ngroups);
    351 
    352 	return (cred->cr_groups[idx]);
    353 }
    354 
    355 /* Sort len groups in grbuf. */
    356 static void
    357 kauth_cred_sortgroups(gid_t *grbuf, size_t len)
    358 {
    359 	int i, j;
    360 	gid_t v;
    361 
    362 	KASSERT(grbuf != NULL);
    363 
    364 	/* Insertion sort. */
    365 	for (i = 1; i < len; i++) {
    366 		v = grbuf[i];
    367 		/* find correct slot for value v, moving others up */
    368 		for (j = i; --j >= 0 && v < grbuf[j];)
    369 			grbuf[j + 1] = grbuf[j];
    370 		grbuf[j + 1] = v;
    371 	}
    372 }
    373 
    374 /* Add group gid to groups in cred. Maintain order. */
    375 int
    376 kauth_cred_addgroup(kauth_cred_t cred, gid_t gid)
    377 {
    378 	int ismember = 0;
    379 
    380 	KASSERT(cred != NULL);
    381 	KASSERT(gid >= 0 && gid <= GID_MAX);
    382 
    383 	simple_lock(&cred->cr_lock);
    384 
    385 	if (cred->cr_ngroups >=
    386 	    (sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]))) {
    387 		simple_unlock(&cred->cr_lock);
    388 		return (E2BIG);
    389 	}
    390 
    391 	if (kauth_cred_ismember_gid(cred, gid, &ismember) == 0 && ismember) {
    392 		simple_unlock(&cred->cr_lock);
    393 		return (0);
    394 	}
    395 
    396 	cred->cr_groups[cred->cr_ngroups++] = gid;
    397 
    398 	kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
    399 
    400 	simple_unlock(&cred->cr_lock);
    401 
    402 	return (0);
    403 }
    404 
    405 /* Delete group gid from groups in cred. Maintain order. */
    406 int
    407 kauth_cred_delgroup(kauth_cred_t cred, gid_t gid)
    408 {
    409 	int ismember = 0;
    410 
    411 	KASSERT(cred != NULL);
    412 	KASSERT(gid >= 0 && gid <= GID_MAX);
    413 
    414 	simple_lock(&cred->cr_lock);
    415 
    416 	if (kauth_cred_ismember_gid(cred, gid, &ismember) != 0 || !ismember) {
    417 		simple_unlock(&cred->cr_lock);
    418 		return (0);
    419 	}
    420 
    421 	if (cred->cr_ngroups == 1)
    422 		cred->cr_groups[0] = (gid_t)-1; /* XXX */
    423 	else {
    424 		int i;
    425 
    426 		for (i = 0; i < cred->cr_ngroups; i++)
    427 			if (cred->cr_groups[i] == gid) {
    428 				cred->cr_groups[i] = cred->cr_groups[cred->cr_ngroups - 1];
    429 				cred->cr_groups[cred->cr_ngroups - 1] = (gid_t)-1; /* XXX */
    430 			}
    431 	}
    432 
    433 	cred->cr_ngroups--;
    434 
    435 	kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
    436 
    437 	simple_unlock(&cred->cr_lock);
    438 
    439 	return (0);
    440 }
    441 
    442 /*
    443  * Match uids in two credentials. Checks if cred1 can access stuff owned by
    444  * cred2.
    445  * XXX: root bypasses this!
    446  */
    447 static int
    448 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    449 {
    450 	KASSERT(cred1 != NULL);
    451 	KASSERT(cred2 != NULL);
    452 
    453 	/* Are we root? */
    454 	if (cred1->cr_euid == 0)
    455 		return (1);
    456 
    457 	if (cred1->cr_uid == cred2->cr_uid ||
    458 	    cred1->cr_euid == cred2->cr_uid ||
    459 	    cred1->cr_uid == cred2->cr_euid ||
    460 	    cred1->cr_euid == cred2->cr_euid)
    461 		return (1);
    462 
    463 	return (0);
    464 }
    465 
    466 uint32_t
    467 kauth_cred_getrefcnt(kauth_cred_t cred)
    468 {
    469 	KASSERT(cred != NULL);
    470 
    471 	return (cred->cr_refcnt);
    472 }
    473 
    474 /*
    475  * Convert userland credentials (struct uucred) to kauth_cred_t.
    476  * XXX: For NFS code.
    477  */
    478 void
    479 kauth_cred_uucvt(kauth_cred_t cred, const struct uucred *uuc)
    480 {
    481 	int i;
    482 
    483 	KASSERT(cred != NULL);
    484 	KASSERT(uuc != NULL);
    485 
    486 	cred->cr_refcnt = 1;
    487 	cred->cr_euid = uuc->cr_uid;
    488 	cred->cr_egid = uuc->cr_gid;
    489 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    490 	for (i = 0; i < cred->cr_ngroups; i++)
    491 		kauth_cred_addgroup(cred, uuc->cr_groups[i]);
    492 }
    493 
    494 /*
    495  * Compare kauth_cred_t and uucred credentials.
    496  * XXX: Modelled after crcmp() for NFS.
    497  */
    498 int
    499 kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
    500 {
    501 	KASSERT(cred != NULL);
    502 	KASSERT(uuc != NULL);
    503 
    504 	if (cred->cr_euid == uuc->cr_uid &&
    505 	    cred->cr_egid == uuc->cr_gid &&
    506 	    cred->cr_ngroups == uuc->cr_ngroups) {
    507 		int i;
    508 
    509 		/* Check if all groups from uuc appear in cred. */
    510 		for (i = 0; i < uuc->cr_ngroups; i++) {
    511 			int ismember;
    512 
    513 			ismember = 0;
    514 			if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
    515 			    &ismember) != 0 || !ismember)
    516 				return (0);
    517 		}
    518 
    519 		return (1);
    520 	}
    521 
    522 	return (0);
    523 }
    524 
    525 /*
    526  * Make a struct ucred out of a kauth_cred_t.
    527  * XXX: For sysctl.
    528  */
    529 void
    530 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    531 {
    532 	KASSERT(cred != NULL);
    533 	KASSERT(uc != NULL);
    534 
    535 	uc->cr_uid = cred->cr_euid;
    536 	uc->cr_gid = cred->cr_egid;
    537 	uc->cr_ngroups = min(cred->cr_ngroups,
    538 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    539 	memcpy(uc->cr_groups, cred->cr_groups,
    540 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    541 }
    542 
    543 /*
    544  * Make a struct pcred out of a kauth_cred_t.
    545  * XXX: For sysctl.
    546  */
    547 void
    548 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    549 {
    550 	KASSERT(cred != NULL);
    551 	KASSERT(pc != NULL);
    552 
    553 	pc->pc_ucred = (struct ucred *)cred; /* XXX this is just wrong */
    554 	pc->p_ruid = cred->cr_uid;
    555 	pc->p_svuid = cred->cr_svuid;
    556 	pc->p_rgid = cred->cr_gid;
    557 	pc->p_svgid = cred->cr_svgid;
    558 	pc->p_refcnt = cred->cr_refcnt;
    559 }
    560 
    561 /*
    562  * Return kauth_cred_t for the current process.
    563  */
    564 kauth_cred_t
    565 kauth_cred_get(void)
    566 {
    567 	return (curproc->p_cred);
    568 }
    569 
    570 /*
    571  * Returns a scope matching the provided id.
    572  * Requires the scope list lock to be held by the caller.
    573  */
    574 static kauth_scope_t
    575 kauth_ifindscope(const char *id) {
    576 	kauth_scope_t scope;
    577 
    578 	/* XXX: assert lock on scope list? */
    579 
    580 	scope = NULL;
    581 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    582 		if (strcmp(scope->id, id) == 0)
    583 			break;
    584 	}
    585 
    586 	return (scope);
    587 }
    588 
    589 /*
    590  * Register a new scope.
    591  *
    592  * id - identifier for the scope
    593  * callback - the scope's default listener
    594  * cookie - cookie to be passed to the listener(s)
    595  */
    596 kauth_scope_t
    597 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    598 		     void *cookie)
    599 {
    600 	kauth_scope_t scope;
    601 	kauth_listener_t listener;
    602 
    603 	/* Sanitize input */
    604 	if (id == NULL || callback == NULL)
    605 		return (NULL);
    606 
    607 	/* Allocate space for a new scope and listener. */
    608 	scope = pool_get(&kauth_scope_pool, PR_WAITOK);
    609 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    610 
    611 	/* Acquire scope list lock. */
    612 	simple_lock(&scopes_lock);
    613 
    614 	/* Check we don't already have a scope with the same id */
    615 	if (kauth_ifindscope(id) != NULL) {
    616 		simple_unlock(&scopes_lock);
    617 
    618 		pool_put(&kauth_scope_pool, scope);
    619 		pool_put(&kauth_listener_pool, listener);
    620 
    621 		return (NULL);
    622 	}
    623 
    624 	/* Initialize new scope with parameters */
    625 	scope->id = id;
    626 	scope->cookie = cookie;
    627 	scope->nlisteners = 1;
    628 
    629 	/* Add default listener */
    630 	listener->func = callback;
    631 	listener->scope = scope;
    632 	listener->refcnt = 0;
    633 	SIMPLEQ_INIT(&scope->listenq);
    634 	SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    635 
    636 	/* Insert scope to scopes list */
    637 	if (SIMPLEQ_EMPTY(&scope_list))
    638 		SIMPLEQ_INSERT_HEAD(&scope_list, scope, next_scope);
    639 	else
    640 		SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    641 
    642 	simple_unlock(&scopes_lock);
    643 
    644 	return (scope);
    645 }
    646 
    647 /*
    648  * Initialize the kernel authorization subsystem.
    649  *
    650  * Initialize the scopes list lock.
    651  * Register built-in scopes: generic, process.
    652  */
    653 void
    654 kauth_init(void)
    655 {
    656 	simple_lock_init(&scopes_lock);
    657 
    658 	/* Register generic scope. */
    659 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    660 	    kauth_authorize_cb_generic, NULL);
    661 
    662 	/* Register process scope. */
    663 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    664 	    kauth_authorize_cb_process, NULL);
    665 }
    666 
    667 /*
    668  * Deregister a scope.
    669  * Requires scope list lock to be held by the caller.
    670  *
    671  * scope - the scope to deregister
    672  */
    673 void
    674 kauth_deregister_scope(kauth_scope_t scope)
    675 {
    676 	if (scope != NULL) {
    677 		/* Remove scope from list */
    678 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    679 	}
    680 }
    681 
    682 /*
    683  * Register a listener.
    684  *
    685  * id - scope identifier.
    686  * callback - the callback routine for the listener.
    687  * cookie - cookie to pass unmoidfied to the callback.
    688  */
    689 kauth_listener_t
    690 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    691 		   void *cookie)
    692 {
    693 	kauth_scope_t scope;
    694 	kauth_listener_t listener;
    695 
    696 	/* Find scope struct */
    697 	simple_lock(&scopes_lock);
    698 	scope = kauth_ifindscope(id);
    699 	simple_unlock(&scopes_lock);
    700 	if (scope == NULL)
    701 		return (NULL);
    702 
    703 	/* Allocate listener */
    704 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    705 
    706 	/* Initialize listener with parameters */
    707 	listener->func = callback;
    708 	listener->refcnt = 0;
    709 
    710 	/* Add listener to scope */
    711 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    712 
    713 	/* Raise number of listeners on scope. */
    714 	scope->nlisteners++;
    715 	listener->scope = scope;
    716 
    717 	return (listener);
    718 }
    719 
    720 /*
    721  * Deregister a listener.
    722  *
    723  * listener - listener reference as returned from kauth_listen_scope().
    724  */
    725 void
    726 kauth_unlisten_scope(kauth_listener_t listener)
    727 {
    728 	if (listener != NULL) {
    729 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener, kauth_listener,
    730 			       listener_next);
    731 		listener->scope->nlisteners--;
    732 	}
    733 }
    734 
    735 /*
    736  * Authorize a request.
    737  *
    738  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    739  *	   returned from kauth_register_scope().
    740  * credential - credentials of the user ("actor") making the request.
    741  * action - request identifier.
    742  * arg[0-3] - passed unmodified to listener(s).
    743  */
    744 int
    745 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    746 		       kauth_action_t action, void *arg0, void *arg1,
    747 		       void *arg2, void *arg3)
    748 {
    749 	kauth_listener_t listener;
    750 	int error, allow, fail;
    751 
    752 	/* Sanitize input */
    753 	if (scope == NULL || cred == NULL)
    754 		return (EFAULT);
    755 	if (!action)
    756 		return (EINVAL);
    757 
    758 	/*
    759 	 * Each scope is associated with at least one listener. We need to
    760 	 * traverse that list of listeners, as long as they return either
    761 	 * KAUTH_REQUEST_DEFER or KAUTH_REQUEST_ALLOW.
    762 	 */
    763 	fail = 0;
    764 	allow = 0;
    765 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    766 		error = listener->func(cred, action, scope->cookie, arg0,
    767 				       arg1, arg2, arg3);
    768 
    769 		if (error == KAUTH_RESULT_ALLOW)
    770 			allow = 1;
    771 		else if (error == KAUTH_RESULT_DENY)
    772 			fail = 1;
    773 	}
    774 
    775 	return ((allow && !fail) ? 0 : EPERM);
    776 };
    777 
    778 /*
    779  * Generic scope default callback.
    780  */
    781 int
    782 kauth_authorize_cb_generic(kauth_cred_t cred, kauth_action_t action,
    783 			   void *cookie, void *arg0, void *arg1, void *arg2,
    784 			   void *arg3)
    785 {
    786 	int error;
    787 
    788 	error = KAUTH_RESULT_DEFER;
    789 
    790 	switch (action) {
    791 	case KAUTH_GENERIC_ISSUSER:
    792 		/* Check if credential belongs to superuser. */
    793 		if (cred->cr_euid == 0) {
    794 			u_short *acflag = (u_short *)arg0;
    795 
    796 			if (acflag != NULL)
    797 				*acflag |= ASU;
    798 
    799 			error = KAUTH_RESULT_ALLOW;
    800 		} else
    801 			error = KAUTH_RESULT_DENY;
    802 		break;
    803 	}
    804 
    805 	return (error);
    806 }
    807 
    808 /*
    809  * Generic scope authorization wrapper.
    810  */
    811 int
    812 kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
    813 {
    814 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    815 	    action, arg0, NULL, NULL, NULL));
    816 }
    817 
    818 /*
    819  * Process scope default callback.
    820  */
    821 int
    822 kauth_authorize_cb_process(kauth_cred_t cred, kauth_action_t action,
    823 			   void *cookie, void *arg0, void *arg1, void *arg2,
    824 			   void *arg3)
    825 {
    826 	struct proc *p;
    827 	kauth_cred_t cred2;
    828 	int error;
    829 
    830 	error = KAUTH_RESULT_DEFER;
    831 
    832 	p = arg0;
    833 	cred2 = arg1;
    834 
    835 	switch (action) {
    836 	case KAUTH_PROCESS_CANSIGNAL: {
    837 		struct proc *to;
    838 		int signum;
    839 
    840 		to = arg2;
    841 		signum = (int)(unsigned long)arg3;
    842 
    843 		if (kauth_cred_uidmatch(cred, cred2)  ||
    844 		    (signum == SIGCONT && (p->p_session == to->p_session)))
    845 			error = KAUTH_RESULT_ALLOW;
    846 		else
    847 			error = KAUTH_RESULT_DEFER;
    848 
    849 		break;
    850 		}
    851 
    852 	case KAUTH_PROCESS_CANPTRACE:
    853 		if (kauth_cred_uidmatch(cred, cred2))
    854 			error = KAUTH_RESULT_ALLOW;
    855 		else
    856 			error = KAUTH_RESULT_DENY;
    857 		break;
    858 
    859 	case KAUTH_PROCESS_CANSEE:
    860 		if (kauth_cred_uidmatch(cred, cred2))
    861 			error = KAUTH_RESULT_ALLOW;
    862 		else
    863 			error = KAUTH_RESULT_DENY;
    864 			/* arg2 - type of information [XXX NOTIMPL] */
    865 		break;
    866 	}
    867 
    868 	return (error);
    869 }
    870 
    871 /*
    872  * Process scope authorization wrapper.
    873  */
    874 int
    875 kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
    876     struct proc *p, void *arg1, void *arg2, void *arg3)
    877 {
    878 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    879 	    action, p, arg1, arg2, arg3));
    880 }
    881