Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.1.2.5
      1 /* $NetBSD: kern_auth.c,v 1.1.2.5 2006/03/08 17:31: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 	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  * Return kauth_cred_t for the current process.
    582  */
    583 kauth_cred_t
    584 kauth_cred_get(void)
    585 {
    586 	return (curproc->p_cred);
    587 }
    588 
    589 /*
    590  * Returns a scope matching the provided id.
    591  * Requires the scope list lock to be held by the caller.
    592  */
    593 static kauth_scope_t
    594 kauth_ifindscope(const char *id) {
    595 	kauth_scope_t scope;
    596 
    597 	/* XXX: assert lock on scope list? */
    598 
    599 	scope = NULL;
    600 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    601 		if (strcmp(scope->id, id) == 0)
    602 			break;
    603 	}
    604 
    605 	return (scope);
    606 }
    607 
    608 /*
    609  * Register a new scope.
    610  *
    611  * id - identifier for the scope
    612  * callback - the scope's default listener
    613  * cookie - cookie to be passed to the listener(s)
    614  */
    615 kauth_scope_t
    616 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    617 		     void *cookie)
    618 {
    619 	kauth_scope_t scope;
    620 	kauth_listener_t listener;
    621 
    622 	/* Sanitize input */
    623 	if (id == NULL || callback == NULL)
    624 		return (NULL);
    625 
    626 	/* Allocate space for a new scope and listener. */
    627 	scope = pool_get(&kauth_scope_pool, PR_WAITOK);
    628 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    629 
    630 	/* Acquire scope list lock. */
    631 	simple_lock(&scopes_lock);
    632 
    633 	/* Check we don't already have a scope with the same id */
    634 	if (kauth_ifindscope(id) != NULL) {
    635 		simple_unlock(&scopes_lock);
    636 
    637 		pool_put(&kauth_scope_pool, scope);
    638 		pool_put(&kauth_listener_pool, listener);
    639 
    640 		return (NULL);
    641 	}
    642 
    643 	/* Initialize new scope with parameters */
    644 	scope->id = id;
    645 	scope->cookie = cookie;
    646 	scope->nlisteners = 1;
    647 
    648 	/* Add default listener */
    649 	listener->func = callback;
    650 	listener->scope = scope;
    651 	listener->refcnt = 0;
    652 	SIMPLEQ_INIT(&scope->listenq);
    653 	SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    654 
    655 	/* Insert scope to scopes list */
    656 	if (SIMPLEQ_EMPTY(&scope_list))
    657 		SIMPLEQ_INSERT_HEAD(&scope_list, scope, next_scope);
    658 	else
    659 		SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    660 
    661 	simple_unlock(&scopes_lock);
    662 
    663 	return (scope);
    664 }
    665 
    666 /*
    667  * Initialize the kernel authorization subsystem.
    668  *
    669  * Initialize the scopes list lock.
    670  * Register built-in scopes: generic, process.
    671  */
    672 void
    673 kauth_init(void)
    674 {
    675 	simple_lock_init(&scopes_lock);
    676 
    677 	/* Register generic scope. */
    678 	builtin_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    679 					       kauth_authorize_cb_generic,
    680 					       NULL);
    681 
    682 	/* Register process scope. */
    683 	builtin_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    684 					       kauth_authorize_cb_process,
    685 					       NULL);
    686 }
    687 
    688 /*
    689  * Deregister a scope.
    690  * Requires scope list lock to be held by the caller.
    691  *
    692  * scope - the scope to deregister
    693  */
    694 void
    695 kauth_deregister_scope(kauth_scope_t scope)
    696 {
    697 	if (scope != NULL) {
    698 		/* Remove scope from list */
    699 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    700 	}
    701 }
    702 
    703 /*
    704  * Register a listener.
    705  *
    706  * id - scope identifier.
    707  * callback - the callback routine for the listener.
    708  * cookie - cookie to pass unmoidfied to the callback.
    709  */
    710 kauth_listener_t
    711 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    712 		   void *cookie)
    713 {
    714 	kauth_scope_t scope;
    715 	kauth_listener_t listener;
    716 
    717 	/* Find scope struct */
    718 	simple_lock(&scopes_lock);
    719 	scope = kauth_ifindscope(id);
    720 	simple_unlock(&scopes_lock);
    721 	if (scope == NULL)
    722 		return (NULL);
    723 
    724 	/* Allocate listener */
    725 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    726 
    727 	/* Initialize listener with parameters */
    728 	listener->func = callback;
    729 	listener->refcnt = 0;
    730 
    731 	/* Add listener to scope */
    732 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    733 
    734 	/* Raise number of listeners on scope. */
    735 	scope->nlisteners++;
    736 	listener->scope = scope;
    737 
    738 	return (listener);
    739 }
    740 
    741 /*
    742  * Deregister a listener.
    743  *
    744  * listener - listener reference as returned from kauth_listen_scope().
    745  */
    746 void
    747 kauth_unlisten_scope(kauth_listener_t listener)
    748 {
    749 	if (listener != NULL) {
    750 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener, kauth_listener,
    751 			       listener_next);
    752 		listener->scope->nlisteners--;
    753 	}
    754 }
    755 
    756 /*
    757  * Authorize a request.
    758  *
    759  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    760  *	   returned from kauth_register_scope().
    761  * credential - credentials of the user ("actor") making the request.
    762  * action - request identifier.
    763  * arg[0-3] - passed unmodified to listener(s).
    764  */
    765 int
    766 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    767 		       kauth_action_t action, void *arg0, void *arg1,
    768 		       void *arg2, void *arg3)
    769 {
    770 	kauth_listener_t listener;
    771 	int error, allow, fail;
    772 
    773 	/* Sanitize input */
    774 	if (scope == NULL || cred == NULL)
    775 		return (EFAULT);
    776 	if (!action)
    777 		return (EINVAL);
    778 
    779 	/*
    780 	 * Each scope is associated with at least one listener. We need to
    781 	 * traverse that list of listeners, as long as they return either
    782 	 * KAUTH_REQUEST_DEFER or KAUTH_REQUEST_ALLOW.
    783 	 */
    784 	fail = 0;
    785 	allow = 0;
    786 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    787 		error = listener->func(cred, action, scope->cookie, arg0,
    788 				       arg1, arg2, arg3);
    789 
    790 		if (error == KAUTH_RESULT_ALLOW)
    791 			allow = 1;
    792 		else if (error == KAUTH_RESULT_DENY)
    793 			fail = 1;
    794 	}
    795 
    796 	return ((allow && !fail) ? 0 : EPERM);
    797 };
    798 
    799 /*
    800  * Generic scope default callback.
    801  */
    802 int
    803 kauth_authorize_cb_generic(kauth_cred_t cred, kauth_action_t action,
    804 			   void *cookie, void *arg0, void *arg1, void *arg2,
    805 			   void *arg3)
    806 {
    807 	int error;
    808 
    809 	error = KAUTH_RESULT_DEFER;
    810 
    811 	switch (action) {
    812 	case KAUTH_GENERIC_ISSUSER:
    813 		/* Check if credential belongs to superuser. */
    814 		if (cred->cr_euid == 0) {
    815 			u_short *acflag = (u_short *)arg0;
    816 
    817 			if (acflag != NULL)
    818 				*acflag |= ASU;
    819 
    820 			error = KAUTH_RESULT_ALLOW;
    821 		} else
    822 			error = KAUTH_RESULT_DENY;
    823 		break;
    824 	}
    825 
    826 	return (error);
    827 }
    828 
    829 /*
    830  * Generic scope authorization wrapper.
    831  */
    832 int
    833 generic_authorize(kauth_cred_t cred, kauth_action_t action, void *arg0)
    834 {
    835 	return (kauth_authorize_action(builtin_generic, cred, action, arg0,
    836 				       NULL, NULL, NULL));
    837 }
    838 
    839 /*
    840  * Process scope default callback.
    841  */
    842 int
    843 kauth_authorize_cb_process(kauth_cred_t cred, kauth_action_t action,
    844 			   void *cookie, void *arg0, void *arg1, void *arg2,
    845 			   void *arg3)
    846 {
    847 	struct proc *p;
    848 	kauth_cred_t cred2;
    849 	int error;
    850 
    851 	error = KAUTH_RESULT_DEFER;
    852 
    853 	p = arg0;
    854 	cred2 = arg1;
    855 
    856 	switch (action) {
    857 	case KAUTH_PROCESS_CANSIGNAL: {
    858 		struct proc *to;
    859 		int signum;
    860 
    861 		to = arg2;
    862 		signum = (int)(unsigned long)arg3;
    863 
    864 		if ((kauth_cred_uidmatch(cred, cred2) == 0) ||
    865 		    (signum == SIGCONT && (p->p_session == to->p_session)))
    866 			error = KAUTH_RESULT_ALLOW;
    867 		else
    868 			error = KAUTH_RESULT_DEFER;
    869 
    870 		break;
    871 		}
    872 
    873 	case KAUTH_PROCESS_CANPTRACE:
    874 		if (kauth_cred_uidmatch(cred, cred2) == 0)
    875 			error = KAUTH_RESULT_ALLOW;
    876 		else
    877 			error = KAUTH_RESULT_DENY;
    878 		break;
    879 
    880 	case KAUTH_PROCESS_CANSEE:
    881 		if (kauth_cred_uidmatch(cred, cred2) == 0)
    882 			error = KAUTH_RESULT_ALLOW;
    883 		else
    884 			error = KAUTH_RESULT_DENY;
    885 			/* arg2 - type of information [XXX NOTIMPL] */
    886 		break;
    887 	}
    888 
    889 	return (error);
    890 }
    891