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