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