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