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