Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.18.2.5
      1 /* $NetBSD: kern_auth.c,v 1.18.2.5 2007/02/05 13:53:20 yamt 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. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: kern_auth.c,v 1.18.2.5 2007/02/05 13:53:20 yamt Exp $");
     32 
     33 #include <sys/types.h>
     34 #include <sys/param.h>
     35 #include <sys/queue.h>
     36 #include <sys/proc.h>
     37 #include <sys/ucred.h>
     38 #include <sys/pool.h>
     39 #include <sys/kauth.h>
     40 #include <sys/kmem.h>
     41 #include <sys/specificdata.h>
     42 #include <sys/rwlock.h>
     43 #include <sys/mutex.h>
     44 
     45 /*
     46  * Secmodel-specific credentials.
     47  */
     48 struct kauth_key {
     49 	const char *ks_secmodel;	/* secmodel */
     50 	specificdata_key_t ks_key;	/* key */
     51 };
     52 
     53 /*
     54  * Credentials.
     55  */
     56 struct kauth_cred {
     57 	kmutex_t cr_lock;		/* lock on cr_refcnt */
     58 	u_int cr_refcnt;		/* reference count */
     59 	uid_t cr_uid;			/* user id */
     60 	uid_t cr_euid;			/* effective user id */
     61 	uid_t cr_svuid;			/* saved effective user id */
     62 	gid_t cr_gid;			/* group id */
     63 	gid_t cr_egid;			/* effective group id */
     64 	gid_t cr_svgid;			/* saved effective group id */
     65 	u_int cr_ngroups;		/* number of groups */
     66 	gid_t cr_groups[NGROUPS];	/* group memberships */
     67 	specificdata_reference cr_sd;	/* specific data */
     68 };
     69 
     70 /*
     71  * Listener.
     72  */
     73 struct kauth_listener {
     74 	kauth_scope_callback_t		func;		/* callback */
     75 	kauth_scope_t			scope;		/* scope backpointer */
     76 	u_int				refcnt;		/* reference count */
     77 	SIMPLEQ_ENTRY(kauth_listener)	listener_next;	/* listener list */
     78 };
     79 
     80 /*
     81  * Scope.
     82  */
     83 struct kauth_scope {
     84 	const char		       *id;		/* scope name */
     85 	void			       *cookie;		/* user cookie */
     86 	u_int				nlisteners;	/* # of listeners */
     87 	SIMPLEQ_HEAD(, kauth_listener)	listenq;	/* listener list */
     88 	SIMPLEQ_ENTRY(kauth_scope)	next_scope;	/* scope list */
     89 };
     90 
     91 static int kauth_cred_hook(kauth_cred_t, kauth_action_t, void *, void *);
     92 
     93 static POOL_INIT(kauth_cred_pool, sizeof(struct kauth_cred), 0, 0, 0,
     94     "kauthcredpl", &pool_allocator_nointr);
     95 
     96 /* List of scopes and its lock. */
     97 static SIMPLEQ_HEAD(, kauth_scope) scope_list;
     98 
     99 /* Built-in scopes: generic, process. */
    100 static kauth_scope_t kauth_builtin_scope_generic;
    101 static kauth_scope_t kauth_builtin_scope_system;
    102 static kauth_scope_t kauth_builtin_scope_process;
    103 static kauth_scope_t kauth_builtin_scope_network;
    104 static kauth_scope_t kauth_builtin_scope_machdep;
    105 static kauth_scope_t kauth_builtin_scope_device;
    106 static kauth_scope_t kauth_builtin_scope_cred;
    107 
    108 static unsigned int nsecmodels = 0;
    109 
    110 static specificdata_domain_t kauth_domain;
    111 krwlock_t	kauth_lock;
    112 
    113 /* Allocate new, empty kauth credentials. */
    114 kauth_cred_t
    115 kauth_cred_alloc(void)
    116 {
    117 	kauth_cred_t cred;
    118 
    119 	cred = pool_get(&kauth_cred_pool, PR_WAITOK);
    120 	memset(cred, 0, sizeof(*cred));
    121 	mutex_init(&cred->cr_lock, MUTEX_DEFAULT, IPL_NONE);
    122 	cred->cr_refcnt = 1;
    123 	specificdata_init(kauth_domain, &cred->cr_sd);
    124 	kauth_cred_hook(cred, KAUTH_CRED_INIT, NULL, NULL);
    125 
    126 	return (cred);
    127 }
    128 
    129 /* Increment reference count to cred. */
    130 void
    131 kauth_cred_hold(kauth_cred_t cred)
    132 {
    133 	KASSERT(cred != NULL);
    134 	KASSERT(cred->cr_refcnt > 0);
    135 
    136         mutex_enter(&cred->cr_lock);
    137         cred->cr_refcnt++;
    138         mutex_exit(&cred->cr_lock);
    139 }
    140 
    141 /* Decrease reference count to cred. If reached zero, free it. */
    142 void
    143 kauth_cred_free(kauth_cred_t cred)
    144 {
    145 	u_int refcnt;
    146 
    147 	KASSERT(cred != NULL);
    148 	KASSERT(cred->cr_refcnt > 0);
    149 
    150 	mutex_enter(&cred->cr_lock);
    151 	refcnt = --cred->cr_refcnt;
    152 	mutex_exit(&cred->cr_lock);
    153 
    154 	if (refcnt == 0) {
    155 		kauth_cred_hook(cred, KAUTH_CRED_FREE, NULL, NULL);
    156 		specificdata_fini(kauth_domain, &cred->cr_sd);
    157 		mutex_destroy(&cred->cr_lock);
    158 		pool_put(&kauth_cred_pool, cred);
    159 	}
    160 }
    161 
    162 void
    163 kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
    164 {
    165 	KASSERT(from != NULL);
    166 	KASSERT(to != NULL);
    167 	KASSERT(from->cr_refcnt > 0);
    168 
    169 	to->cr_uid = from->cr_uid;
    170 	to->cr_euid = from->cr_euid;
    171 	to->cr_svuid = from->cr_svuid;
    172 	to->cr_gid = from->cr_gid;
    173 	to->cr_egid = from->cr_egid;
    174 	to->cr_svgid = from->cr_svgid;
    175 	to->cr_ngroups = from->cr_ngroups;
    176 	memcpy(to->cr_groups, from->cr_groups, sizeof(to->cr_groups));
    177 
    178 	kauth_cred_hook(from, KAUTH_CRED_COPY, to, NULL);
    179 }
    180 
    181 /*
    182  * Duplicate cred and return a new kauth_cred_t.
    183  */
    184 kauth_cred_t
    185 kauth_cred_dup(kauth_cred_t cred)
    186 {
    187 	kauth_cred_t new_cred;
    188 
    189 	KASSERT(cred != NULL);
    190 	KASSERT(cred->cr_refcnt > 0);
    191 
    192 	new_cred = kauth_cred_alloc();
    193 
    194 	kauth_cred_clone(cred, new_cred);
    195 
    196 	return (new_cred);
    197 }
    198 
    199 /*
    200  * Similar to crcopy(), only on a kauth_cred_t.
    201  * XXX: Is this even needed? [kauth_cred_copy]
    202  */
    203 kauth_cred_t
    204 kauth_cred_copy(kauth_cred_t cred)
    205 {
    206 	kauth_cred_t new_cred;
    207 
    208 	KASSERT(cred != NULL);
    209 	KASSERT(cred->cr_refcnt > 0);
    210 
    211 	/* If the provided credentials already have one reference, use them. */
    212 	if (cred->cr_refcnt == 1)
    213 		return (cred);
    214 
    215 	new_cred = kauth_cred_alloc();
    216 
    217 	kauth_cred_clone(cred, new_cred);
    218 
    219 	kauth_cred_free(cred);
    220 
    221 	return (new_cred);
    222 }
    223 
    224 void
    225 kauth_proc_fork(struct proc *parent, struct proc *child)
    226 {
    227 
    228 	mutex_enter(&parent->p_mutex);
    229 	kauth_cred_hold(parent->p_cred);
    230 	child->p_cred = parent->p_cred;
    231 	mutex_exit(&parent->p_mutex);
    232 
    233 	/* XXX: relies on parent process stalling during fork() */
    234 	kauth_cred_hook(parent->p_cred, KAUTH_CRED_FORK, parent,
    235 	    child);
    236 }
    237 
    238 uid_t
    239 kauth_cred_getuid(kauth_cred_t cred)
    240 {
    241 	KASSERT(cred != NULL);
    242 
    243 	return (cred->cr_uid);
    244 }
    245 
    246 uid_t
    247 kauth_cred_geteuid(kauth_cred_t cred)
    248 {
    249 	KASSERT(cred != NULL);
    250 
    251 	return (cred->cr_euid);
    252 }
    253 
    254 uid_t
    255 kauth_cred_getsvuid(kauth_cred_t cred)
    256 {
    257 	KASSERT(cred != NULL);
    258 
    259 	return (cred->cr_svuid);
    260 }
    261 
    262 gid_t
    263 kauth_cred_getgid(kauth_cred_t cred)
    264 {
    265 	KASSERT(cred != NULL);
    266 
    267 	return (cred->cr_gid);
    268 }
    269 
    270 gid_t
    271 kauth_cred_getegid(kauth_cred_t cred)
    272 {
    273 	KASSERT(cred != NULL);
    274 
    275 	return (cred->cr_egid);
    276 }
    277 
    278 gid_t
    279 kauth_cred_getsvgid(kauth_cred_t cred)
    280 {
    281 	KASSERT(cred != NULL);
    282 
    283 	return (cred->cr_svgid);
    284 }
    285 
    286 void
    287 kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
    288 {
    289 	KASSERT(cred != NULL);
    290 	KASSERT(cred->cr_refcnt == 1);
    291 
    292 	cred->cr_uid = uid;
    293 }
    294 
    295 void
    296 kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
    297 {
    298 	KASSERT(cred != NULL);
    299 	KASSERT(cred->cr_refcnt == 1);
    300 
    301 	cred->cr_euid = uid;
    302 }
    303 
    304 void
    305 kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
    306 {
    307 	KASSERT(cred != NULL);
    308 	KASSERT(cred->cr_refcnt == 1);
    309 
    310 	cred->cr_svuid = uid;
    311 }
    312 
    313 void
    314 kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
    315 {
    316 	KASSERT(cred != NULL);
    317 	KASSERT(cred->cr_refcnt == 1);
    318 
    319 	cred->cr_gid = gid;
    320 }
    321 
    322 void
    323 kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
    324 {
    325 	KASSERT(cred != NULL);
    326 	KASSERT(cred->cr_refcnt == 1);
    327 
    328 	cred->cr_egid = gid;
    329 }
    330 
    331 void
    332 kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
    333 {
    334 	KASSERT(cred != NULL);
    335 	KASSERT(cred->cr_refcnt == 1);
    336 
    337 	cred->cr_svgid = gid;
    338 }
    339 
    340 /* Checks if gid is a member of the groups in cred. */
    341 int
    342 kauth_cred_ismember_gid(kauth_cred_t cred, gid_t gid, int *resultp)
    343 {
    344 	int i;
    345 
    346 	KASSERT(cred != NULL);
    347 	KASSERT(resultp != NULL);
    348 
    349 	*resultp = 0;
    350 
    351 	for (i = 0; i < cred->cr_ngroups; i++)
    352 		if (cred->cr_groups[i] == gid) {
    353 			*resultp = 1;
    354 			break;
    355 		}
    356 
    357 	return (0);
    358 }
    359 
    360 u_int
    361 kauth_cred_ngroups(kauth_cred_t cred)
    362 {
    363 	KASSERT(cred != NULL);
    364 
    365 	return (cred->cr_ngroups);
    366 }
    367 
    368 /*
    369  * Return the group at index idx from the groups in cred.
    370  */
    371 gid_t
    372 kauth_cred_group(kauth_cred_t cred, u_int idx)
    373 {
    374 	KASSERT(cred != NULL);
    375 	KASSERT(idx < cred->cr_ngroups);
    376 
    377 	return (cred->cr_groups[idx]);
    378 }
    379 
    380 /* XXX elad: gmuid is unused for now. */
    381 int
    382 kauth_cred_setgroups(kauth_cred_t cred, gid_t *grbuf, size_t len, uid_t gmuid)
    383 {
    384 	KASSERT(cred != NULL);
    385 	KASSERT(cred->cr_refcnt == 1);
    386 	KASSERT(len <= sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]));
    387 
    388 	if (len)
    389 		memcpy(cred->cr_groups, grbuf, len * sizeof(cred->cr_groups[0]));
    390 	memset(cred->cr_groups + len, 0xff,
    391 	    sizeof(cred->cr_groups) - (len * sizeof(cred->cr_groups[0])));
    392 
    393 	cred->cr_ngroups = len;
    394 
    395 	return (0);
    396 }
    397 
    398 int
    399 kauth_cred_getgroups(kauth_cred_t cred, gid_t *grbuf, size_t len)
    400 {
    401 	KASSERT(cred != NULL);
    402 	KASSERT(len <= cred->cr_ngroups);
    403 
    404 	memset(grbuf, 0xff, sizeof(*grbuf) * len);
    405 	memcpy(grbuf, cred->cr_groups, sizeof(*grbuf) * len);
    406 
    407 	return (0);
    408 }
    409 
    410 int
    411 kauth_register_key(const char *secmodel, kauth_key_t *result)
    412 {
    413 	kauth_key_t k;
    414 	specificdata_key_t key;
    415 	int error;
    416 
    417 	KASSERT(result != NULL);
    418 
    419 	error = specificdata_key_create(kauth_domain, &key, NULL);
    420 	if (error)
    421 		return (error);
    422 
    423 	k = kmem_alloc(sizeof(*k), KM_SLEEP);
    424 	k->ks_secmodel = secmodel;
    425 	k->ks_key = key;
    426 
    427 	*result = k;
    428 
    429 	return (0);
    430 }
    431 
    432 int
    433 kauth_deregister_key(kauth_key_t key)
    434 {
    435 	KASSERT(key != NULL);
    436 
    437 	specificdata_key_delete(kauth_domain, key->ks_key);
    438 	kmem_free(key, sizeof(*key));
    439 
    440 	return (0);
    441 }
    442 
    443 void *
    444 kauth_cred_getdata(kauth_cred_t cred, kauth_key_t key)
    445 {
    446 	KASSERT(cred != NULL);
    447 	KASSERT(key != NULL);
    448 
    449 	return (specificdata_getspecific(kauth_domain, &cred->cr_sd,
    450 	    key->ks_key));
    451 }
    452 
    453 void
    454 kauth_cred_setdata(kauth_cred_t cred, kauth_key_t key, void *data)
    455 {
    456 	KASSERT(cred != NULL);
    457 	KASSERT(key != NULL);
    458 
    459 	specificdata_setspecific(kauth_domain, &cred->cr_sd, key->ks_key, data);
    460 }
    461 
    462 /*
    463  * Match uids in two credentials.
    464  */
    465 int
    466 kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    467 {
    468 	KASSERT(cred1 != NULL);
    469 	KASSERT(cred2 != NULL);
    470 
    471 	if (cred1->cr_uid == cred2->cr_uid ||
    472 	    cred1->cr_euid == cred2->cr_uid ||
    473 	    cred1->cr_uid == cred2->cr_euid ||
    474 	    cred1->cr_euid == cred2->cr_euid)
    475 		return (1);
    476 
    477 	return (0);
    478 }
    479 
    480 u_int
    481 kauth_cred_getrefcnt(kauth_cred_t cred)
    482 {
    483 	KASSERT(cred != NULL);
    484 
    485 	return (cred->cr_refcnt);
    486 }
    487 
    488 /*
    489  * Convert userland credentials (struct uucred) to kauth_cred_t.
    490  * XXX: For NFS & puffs
    491  */
    492 void
    493 kauth_uucred_to_cred(kauth_cred_t cred, const struct uucred *uuc)
    494 {
    495 	KASSERT(cred != NULL);
    496 	KASSERT(uuc != NULL);
    497 
    498 	cred->cr_refcnt = 1;
    499 	cred->cr_uid = uuc->cr_uid;
    500 	cred->cr_euid = uuc->cr_uid;
    501 	cred->cr_svuid = uuc->cr_uid;
    502 	cred->cr_gid = uuc->cr_gid;
    503 	cred->cr_egid = uuc->cr_gid;
    504 	cred->cr_svgid = uuc->cr_gid;
    505 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    506 	kauth_cred_setgroups(cred, __UNCONST(uuc->cr_groups),
    507 	    cred->cr_ngroups, -1);
    508 }
    509 
    510 /*
    511  * Convert kauth_cred_t to userland credentials (struct uucred).
    512  * XXX: For NFS & puffs
    513  */
    514 void
    515 kauth_cred_to_uucred(struct uucred *uuc, const kauth_cred_t cred)
    516 {
    517 	KASSERT(cred != NULL);
    518 	KASSERT(uuc != NULL);
    519 	int ng;
    520 
    521 	ng = min(cred->cr_ngroups, NGROUPS);
    522 	uuc->cr_uid = cred->cr_euid;
    523 	uuc->cr_gid = cred->cr_egid;
    524 	uuc->cr_ngroups = ng;
    525 	kauth_cred_getgroups(cred, uuc->cr_groups, ng);
    526 }
    527 
    528 /*
    529  * Compare kauth_cred_t and uucred credentials.
    530  * XXX: Modelled after crcmp() for NFS.
    531  */
    532 int
    533 kauth_cred_uucmp(kauth_cred_t cred, const struct uucred *uuc)
    534 {
    535 	KASSERT(cred != NULL);
    536 	KASSERT(uuc != NULL);
    537 
    538 	if (cred->cr_euid == uuc->cr_uid &&
    539 	    cred->cr_egid == uuc->cr_gid &&
    540 	    cred->cr_ngroups == uuc->cr_ngroups) {
    541 		int i;
    542 
    543 		/* Check if all groups from uuc appear in cred. */
    544 		for (i = 0; i < uuc->cr_ngroups; i++) {
    545 			int ismember;
    546 
    547 			ismember = 0;
    548 			if (kauth_cred_ismember_gid(cred, uuc->cr_groups[i],
    549 			    &ismember) != 0 || !ismember)
    550 				return (1);
    551 		}
    552 
    553 		return (0);
    554 	}
    555 
    556 	return (1);
    557 }
    558 
    559 /*
    560  * Make a struct ucred out of a kauth_cred_t.  For compatibility.
    561  */
    562 void
    563 kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    564 {
    565 	KASSERT(cred != NULL);
    566 	KASSERT(uc != NULL);
    567 
    568 	uc->cr_ref = cred->cr_refcnt;
    569 	uc->cr_uid = cred->cr_euid;
    570 	uc->cr_gid = cred->cr_egid;
    571 	uc->cr_ngroups = min(cred->cr_ngroups,
    572 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    573 	memcpy(uc->cr_groups, cred->cr_groups,
    574 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    575 }
    576 
    577 /*
    578  * Make a struct pcred out of a kauth_cred_t.  For compatibility.
    579  */
    580 void
    581 kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    582 {
    583 	KASSERT(cred != NULL);
    584 	KASSERT(pc != NULL);
    585 
    586 	pc->pc_ucred = NULL;
    587 	pc->p_ruid = cred->cr_uid;
    588 	pc->p_svuid = cred->cr_svuid;
    589 	pc->p_rgid = cred->cr_gid;
    590 	pc->p_svgid = cred->cr_svgid;
    591 	pc->p_refcnt = cred->cr_refcnt;
    592 }
    593 
    594 /*
    595  * Return kauth_cred_t for the current LWP.
    596  */
    597 kauth_cred_t
    598 kauth_cred_get(void)
    599 {
    600 	return (curlwp->l_cred);
    601 }
    602 
    603 /*
    604  * Returns a scope matching the provided id.
    605  * Requires the scope list lock to be held by the caller.
    606  */
    607 static kauth_scope_t
    608 kauth_ifindscope(const char *id)
    609 {
    610 	kauth_scope_t scope;
    611 
    612 	KASSERT(rw_lock_held(&kauth_lock));
    613 
    614 	scope = NULL;
    615 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    616 		if (strcmp(scope->id, id) == 0)
    617 			break;
    618 	}
    619 
    620 	return (scope);
    621 }
    622 
    623 /*
    624  * Register a new scope.
    625  *
    626  * id - identifier for the scope
    627  * callback - the scope's default listener
    628  * cookie - cookie to be passed to the listener(s)
    629  */
    630 kauth_scope_t
    631 kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    632     void *cookie)
    633 {
    634 	kauth_scope_t scope;
    635 	kauth_listener_t listener = NULL; /* XXX gcc */
    636 
    637 	/* Sanitize input */
    638 	if (id == NULL)
    639 		return (NULL);
    640 
    641 	/* Allocate space for a new scope and listener. */
    642 	scope = kmem_alloc(sizeof(*scope), KM_SLEEP);
    643 	if (scope == NULL)
    644 		return NULL;
    645 	if (callback != NULL) {
    646 		listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    647 		if (listener == NULL) {
    648 			kmem_free(scope, sizeof(*scope));
    649 			return (NULL);
    650 		}
    651 	}
    652 
    653 	/*
    654 	 * Acquire scope list lock.
    655 	 */
    656 	rw_enter(&kauth_lock, RW_WRITER);
    657 
    658 	/* Check we don't already have a scope with the same id */
    659 	if (kauth_ifindscope(id) != NULL) {
    660 		rw_exit(&kauth_lock);
    661 
    662 		kmem_free(scope, sizeof(*scope));
    663 		if (callback != NULL)
    664 			kmem_free(listener, sizeof(*listener));
    665 
    666 		return (NULL);
    667 	}
    668 
    669 	/* Initialize new scope with parameters */
    670 	scope->id = id;
    671 	scope->cookie = cookie;
    672 	scope->nlisteners = 1;
    673 
    674 	SIMPLEQ_INIT(&scope->listenq);
    675 
    676 	/* Add default listener */
    677 	if (callback != NULL) {
    678 		listener->func = callback;
    679 		listener->scope = scope;
    680 		listener->refcnt = 0;
    681 		SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    682 	}
    683 
    684 	/* Insert scope to scopes list */
    685 	SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    686 
    687 	rw_exit(&kauth_lock);
    688 
    689 	return (scope);
    690 }
    691 
    692 /*
    693  * Initialize the kernel authorization subsystem.
    694  *
    695  * Initialize the scopes list lock.
    696  * Create specificdata domain.
    697  * Register the credentials scope, used in kauth(9) internally.
    698  * Register built-in scopes: generic, system, process, network, machdep, device.
    699  */
    700 void
    701 kauth_init(void)
    702 {
    703 	SIMPLEQ_INIT(&scope_list);
    704 	rw_init(&kauth_lock);
    705 
    706 	/* Create specificdata domain. */
    707 	kauth_domain = specificdata_domain_create();
    708 
    709 	/* Register credentials scope. */
    710 	kauth_builtin_scope_cred =
    711 	    kauth_register_scope(KAUTH_SCOPE_CRED, NULL, NULL);
    712 
    713 	/* Register generic scope. */
    714 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    715 	    NULL, NULL);
    716 
    717 	/* Register system scope. */
    718 	kauth_builtin_scope_system = kauth_register_scope(KAUTH_SCOPE_SYSTEM,
    719 	    NULL, NULL);
    720 
    721 	/* Register process scope. */
    722 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    723 	    NULL, NULL);
    724 
    725 	/* Register network scope. */
    726 	kauth_builtin_scope_network = kauth_register_scope(KAUTH_SCOPE_NETWORK,
    727 	    NULL, NULL);
    728 
    729 	/* Register machdep scope. */
    730 	kauth_builtin_scope_machdep = kauth_register_scope(KAUTH_SCOPE_MACHDEP,
    731 	    NULL, NULL);
    732 
    733 	/* Register device scope. */
    734 	kauth_builtin_scope_device = kauth_register_scope(KAUTH_SCOPE_DEVICE,
    735 	    NULL, NULL);
    736 }
    737 
    738 /*
    739  * Deregister a scope.
    740  * Requires scope list lock to be held by the caller.
    741  *
    742  * scope - the scope to deregister
    743  */
    744 void
    745 kauth_deregister_scope(kauth_scope_t scope)
    746 {
    747 	if (scope != NULL) {
    748 		/* Remove scope from list */
    749 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    750 		kmem_free(scope, sizeof(*scope));
    751 	}
    752 }
    753 
    754 /*
    755  * Register a listener.
    756  *
    757  * id - scope identifier.
    758  * callback - the callback routine for the listener.
    759  * cookie - cookie to pass unmoidfied to the callback.
    760  */
    761 kauth_listener_t
    762 kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    763    void *cookie)
    764 {
    765 	kauth_scope_t scope;
    766 	kauth_listener_t listener;
    767 
    768 	listener = kmem_alloc(sizeof(*listener), KM_SLEEP);
    769 	if (listener == NULL)
    770 		return (NULL);
    771 
    772 	rw_enter(&kauth_lock, RW_WRITER);
    773 
    774 	/*
    775 	 * Find scope struct.
    776 	 */
    777 	scope = kauth_ifindscope(id);
    778 	if (scope == NULL) {
    779 		rw_exit(&kauth_lock);
    780 		kmem_free(listener, sizeof(*listener));
    781 		return (NULL);
    782 	}
    783 
    784 	/* Allocate listener */
    785 
    786 	/* Initialize listener with parameters */
    787 	listener->func = callback;
    788 	listener->refcnt = 0;
    789 
    790 	/* Add listener to scope */
    791 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    792 
    793 	/* Raise number of listeners on scope. */
    794 	scope->nlisteners++;
    795 	listener->scope = scope;
    796 
    797 	rw_exit(&kauth_lock);
    798 
    799 	return (listener);
    800 }
    801 
    802 /*
    803  * Deregister a listener.
    804  *
    805  * listener - listener reference as returned from kauth_listen_scope().
    806  */
    807 void
    808 kauth_unlisten_scope(kauth_listener_t listener)
    809 {
    810 
    811 	if (listener != NULL) {
    812 		rw_enter(&kauth_lock, RW_WRITER);
    813 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener,
    814 		    kauth_listener, listener_next);
    815 		listener->scope->nlisteners--;
    816 		rw_exit(&kauth_lock);
    817 		kmem_free(listener, sizeof(*listener));
    818 	}
    819 }
    820 
    821 /*
    822  * Authorize a request.
    823  *
    824  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    825  *	   returned from kauth_register_scope().
    826  * credential - credentials of the user ("actor") making the request.
    827  * action - request identifier.
    828  * arg[0-3] - passed unmodified to listener(s).
    829  */
    830 int
    831 kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    832 		       kauth_action_t action, void *arg0, void *arg1,
    833 		       void *arg2, void *arg3)
    834 {
    835 	kauth_listener_t listener;
    836 	int error, allow, fail;
    837 
    838 #if 0 /* defined(LOCKDEBUG) */
    839 	spinlock_switchcheck();
    840 	simple_lock_only_held(NULL, "kauth_authorize_action");
    841 #endif
    842 
    843 	KASSERT(cred != NULL);
    844 	KASSERT(action != 0);
    845 
    846 	/* Short-circuit requests coming from the kernel. */
    847 	if (cred == NOCRED || cred == FSCRED)
    848 		return (0);
    849 
    850 	KASSERT(scope != NULL);
    851 
    852 	fail = 0;
    853 	allow = 0;
    854 
    855 	/* rw_enter(&kauth_lock, RW_READER); XXX not yet */
    856 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    857 		error = listener->func(cred, action, scope->cookie, arg0,
    858 		    arg1, arg2, arg3);
    859 
    860 		if (error == KAUTH_RESULT_ALLOW)
    861 			allow = 1;
    862 		else if (error == KAUTH_RESULT_DENY)
    863 			fail = 1;
    864 	}
    865 	/* rw_exit(&kauth_lock); */
    866 
    867 	if (fail)
    868 		return (EPERM);
    869 
    870 	if (allow)
    871 		return (0);
    872 
    873 	if (!nsecmodels)
    874 		return (0);
    875 
    876 	return (EPERM);
    877 };
    878 
    879 /*
    880  * Generic scope authorization wrapper.
    881  */
    882 int
    883 kauth_authorize_generic(kauth_cred_t cred, kauth_action_t action, void *arg0)
    884 {
    885 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    886 	    action, arg0, NULL, NULL, NULL));
    887 }
    888 
    889 /*
    890  * System scope authorization wrapper.
    891  */
    892 int
    893 kauth_authorize_system(kauth_cred_t cred, kauth_action_t action,
    894     enum kauth_system_req req, void *arg1, void *arg2, void *arg3)
    895 {
    896 	return (kauth_authorize_action(kauth_builtin_scope_system, cred,
    897 	    action, (void *)req, arg1, arg2, arg3));
    898 }
    899 
    900 /*
    901  * Process scope authorization wrapper.
    902  */
    903 int
    904 kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
    905     struct proc *p, void *arg1, void *arg2, void *arg3)
    906 {
    907 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    908 	    action, p, arg1, arg2, arg3));
    909 }
    910 
    911 /*
    912  * Network scope authorization wrapper.
    913  */
    914 int
    915 kauth_authorize_network(kauth_cred_t cred, kauth_action_t action,
    916     enum kauth_network_req req, void *arg1, void *arg2, void *arg3)
    917 {
    918 	return (kauth_authorize_action(kauth_builtin_scope_network, cred,
    919 	    action, (void *)req, arg1, arg2, arg3));
    920 }
    921 
    922 int
    923 kauth_authorize_machdep(kauth_cred_t cred, kauth_action_t action,
    924     void *arg0, void *arg1, void *arg2, void *arg3)
    925 {
    926 	return (kauth_authorize_action(kauth_builtin_scope_machdep, cred,
    927 	    action, arg0, arg1, arg2, arg3));
    928 }
    929 
    930 int
    931 kauth_authorize_device(kauth_cred_t cred, kauth_action_t action,
    932     void *arg0, void *arg1, void *arg2, void *arg3)
    933 {
    934 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    935 	    action, arg0, arg1, arg2, arg3));
    936 }
    937 
    938 int
    939 kauth_authorize_device_tty(kauth_cred_t cred, kauth_action_t action,
    940     struct tty *tty)
    941 {
    942 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    943 	    action, tty, NULL, NULL, NULL));
    944 }
    945 
    946 int
    947 kauth_authorize_device_spec(kauth_cred_t cred, enum kauth_device_req req,
    948     struct vnode *vp)
    949 {
    950 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    951 	    KAUTH_DEVICE_RAWIO_SPEC, (void *)req, vp, NULL, NULL));
    952 }
    953 
    954 int
    955 kauth_authorize_device_passthru(kauth_cred_t cred, dev_t dev, u_long bits,
    956     void *data)
    957 {
    958 	return (kauth_authorize_action(kauth_builtin_scope_device, cred,
    959 	    KAUTH_DEVICE_RAWIO_PASSTHRU, (void *)bits, (void *)(u_long)dev,
    960 	    data, NULL));
    961 }
    962 
    963 static int
    964 kauth_cred_hook(kauth_cred_t cred, kauth_action_t action, void *arg0,
    965     void *arg1)
    966 {
    967 	int r;
    968 
    969 	r = kauth_authorize_action(kauth_builtin_scope_cred, cred, action,
    970 	    arg0, arg1, NULL, NULL);
    971 
    972 #ifdef DIAGNOSTIC
    973 	if (!SIMPLEQ_EMPTY(&kauth_builtin_scope_cred->listenq))
    974 		KASSERT(r == 0);
    975 #endif /* DIAGNOSTIC */
    976 
    977 	return (r);
    978 }
    979 
    980 void
    981 secmodel_register(void)
    982 {
    983 	KASSERT(nsecmodels + 1 != 0);
    984 
    985 	rw_enter(&kauth_lock, RW_WRITER);
    986 	nsecmodels++;
    987 	rw_exit(&kauth_lock);
    988 }
    989 
    990 void
    991 secmodel_deregister(void)
    992 {
    993 	KASSERT(nsecmodels != 0);
    994 
    995 	rw_enter(&kauth_lock, RW_WRITER);
    996 	nsecmodels--;
    997 	rw_exit(&kauth_lock);
    998 }
    999