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