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