Home | History | Annotate | Line # | Download | only in kern
kern_auth.c revision 1.1.2.11
      1  1.1.2.11  elad /* $NetBSD: kern_auth.c,v 1.1.2.11 2006/03/10 11:53:55 elad Exp $ */
      2   1.1.2.1  elad 
      3   1.1.2.1  elad /*-
      4   1.1.2.1  elad  * Copyright (c) 2005, 2006 Elad Efrat <elad (at) NetBSD.org>
      5   1.1.2.1  elad  * All rights reserved.
      6   1.1.2.1  elad  *
      7   1.1.2.1  elad  * Redistribution and use in source and binary forms, with or without
      8   1.1.2.1  elad  * modification, are permitted provided that the following conditions
      9   1.1.2.1  elad  * are met:
     10   1.1.2.1  elad  * 1. Redistributions of source code must retain the above copyright
     11   1.1.2.1  elad  *    notice, this list of conditions and the following disclaimer.
     12   1.1.2.1  elad  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1.2.1  elad  *    notice, this list of conditions and the following disclaimer in the
     14   1.1.2.1  elad  *    documentation and/or other materials provided with the distribution.
     15   1.1.2.1  elad  * 3. All advertising materials mentioning features or use of this software
     16   1.1.2.1  elad  *    must display the following acknowledgement:
     17   1.1.2.1  elad  *      This product includes software developed by Elad Efrat.
     18   1.1.2.1  elad  * 4. The name of the author may not be used to endorse or promote products
     19   1.1.2.1  elad  *    derived from this software without specific prior written permission.
     20   1.1.2.1  elad  *
     21   1.1.2.1  elad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1.2.1  elad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1.2.1  elad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1.2.1  elad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1.2.1  elad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1.2.1  elad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1.2.1  elad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1.2.1  elad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1.2.1  elad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1.2.1  elad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1.2.1  elad  */
     32   1.1.2.1  elad 
     33   1.1.2.1  elad /*
     34   1.1.2.1  elad  * Todo:
     35   1.1.2.1  elad  *   - Garbage collection to pool_put() unused scopes/listeners.
     36   1.1.2.1  elad  */
     37   1.1.2.1  elad 
     38   1.1.2.1  elad #include <sys/types.h>
     39   1.1.2.1  elad #include <sys/param.h>
     40   1.1.2.1  elad #include <sys/queue.h>
     41   1.1.2.1  elad #include <sys/time.h>
     42   1.1.2.1  elad #include <sys/proc.h>
     43   1.1.2.1  elad #include <sys/ucred.h>
     44   1.1.2.1  elad #include <sys/pool.h>
     45   1.1.2.1  elad #include <sys/kauth.h>
     46   1.1.2.1  elad #include <sys/acct.h>
     47   1.1.2.1  elad 
     48   1.1.2.1  elad /*
     49   1.1.2.1  elad  * Credentials.
     50   1.1.2.1  elad  */
     51   1.1.2.1  elad struct kauth_cred {
     52   1.1.2.1  elad 	struct simplelock cr_lock;	/* lock */
     53   1.1.2.1  elad 	uint32_t cr_refcnt;		/* reference count */
     54   1.1.2.1  elad 	uid_t cr_uid;			/* user id */
     55   1.1.2.1  elad 	uid_t cr_euid;			/* effective user id */
     56   1.1.2.1  elad 	uid_t cr_svuid;			/* saved effective user id */
     57   1.1.2.1  elad 	gid_t cr_gid;			/* group id */
     58   1.1.2.1  elad 	gid_t cr_egid;			/* effective group id */
     59   1.1.2.1  elad 	gid_t cr_svgid;			/* saved effective group id */
     60   1.1.2.1  elad 	uint16_t cr_ngroups;		/* number of groups */
     61   1.1.2.1  elad 	gid_t cr_groups[NGROUPS];	/* group memberships */
     62   1.1.2.1  elad };
     63   1.1.2.1  elad 
     64   1.1.2.1  elad /*
     65   1.1.2.1  elad  * Listener.
     66   1.1.2.1  elad  */
     67   1.1.2.1  elad struct kauth_listener {
     68   1.1.2.1  elad 	kauth_scope_callback_t		func;		/* callback */
     69   1.1.2.1  elad 	kauth_scope_t			scope;		/* scope backpointer */
     70   1.1.2.1  elad 	uint32_t			refcnt;		/* reference count */
     71   1.1.2.1  elad 	SIMPLEQ_ENTRY(kauth_listener)	listener_next;	/* listener list */
     72   1.1.2.1  elad };
     73   1.1.2.1  elad 
     74   1.1.2.1  elad /*
     75   1.1.2.1  elad  * Scope.
     76   1.1.2.1  elad  */
     77   1.1.2.1  elad struct kauth_scope {
     78   1.1.2.1  elad 	const char		       *id;		/* scope name */
     79   1.1.2.1  elad 	void			       *cookie;		/* user cookie */
     80   1.1.2.1  elad 	uint32_t			nlisteners;	/* # of listeners */
     81   1.1.2.1  elad 	SIMPLEQ_HEAD(, kauth_listener)	listenq;	/* listener list */
     82   1.1.2.1  elad 	SIMPLEQ_ENTRY(kauth_scope)	next_scope;	/* scope list */
     83   1.1.2.1  elad };
     84   1.1.2.1  elad 
     85   1.1.2.1  elad POOL_INIT(kauth_scope_pool, sizeof(struct kauth_scope), 0, 0, 0,
     86   1.1.2.1  elad 	  "kauth_scopepl", &pool_allocator_nointr);
     87   1.1.2.1  elad POOL_INIT(kauth_listener_pool, sizeof(struct kauth_listener), 0, 0, 0,
     88   1.1.2.1  elad 	  "kauth_listenerpl", &pool_allocator_nointr);
     89   1.1.2.1  elad POOL_INIT(kauth_cred_pool, sizeof(struct kauth_cred), 0, 0, 0,
     90   1.1.2.1  elad 	  "kauth_credpl", &pool_allocator_nointr);
     91   1.1.2.1  elad 
     92   1.1.2.1  elad /* List of scopes and its lock. */
     93   1.1.2.1  elad SIMPLEQ_HEAD(, kauth_scope) scope_list;
     94   1.1.2.1  elad struct simplelock scopes_lock;
     95   1.1.2.1  elad 
     96   1.1.2.1  elad /* Built-in scopes: generic, process. */
     97   1.1.2.9  elad static kauth_scope_t kauth_builtin_scope_generic;
     98   1.1.2.9  elad static kauth_scope_t kauth_builtin_scope_process;
     99   1.1.2.1  elad 
    100   1.1.2.1  elad /* Allocate new, empty kauth credentials. */
    101   1.1.2.1  elad kauth_cred_t
    102   1.1.2.1  elad kauth_cred_alloc(void) {
    103   1.1.2.1  elad 	kauth_cred_t cred;
    104   1.1.2.1  elad 
    105   1.1.2.1  elad 	cred = pool_get(&kauth_cred_pool, PR_WAITOK);
    106   1.1.2.1  elad 	memset(cred, 0, sizeof(*cred));
    107   1.1.2.1  elad 	simple_lock_init(&cred->cr_lock);
    108   1.1.2.1  elad 	cred->cr_refcnt = 1;
    109   1.1.2.1  elad 
    110   1.1.2.1  elad 	return (cred);
    111   1.1.2.1  elad }
    112   1.1.2.1  elad 
    113   1.1.2.1  elad /* Increment reference count to cred. */
    114   1.1.2.1  elad void
    115   1.1.2.1  elad kauth_cred_hold(kauth_cred_t cred)
    116   1.1.2.1  elad {
    117   1.1.2.1  elad 	KASSERT(cred != NULL);
    118   1.1.2.1  elad 
    119   1.1.2.1  elad         simple_lock(&cred->cr_lock);
    120   1.1.2.1  elad         cred->cr_refcnt++;
    121   1.1.2.1  elad         simple_unlock(&cred->cr_lock);
    122   1.1.2.1  elad }
    123   1.1.2.1  elad 
    124   1.1.2.1  elad void
    125   1.1.2.1  elad kauth_cred_destroy(kauth_cred_t cred)
    126   1.1.2.1  elad {
    127   1.1.2.1  elad 	KASSERT(cred != NULL);
    128   1.1.2.1  elad 
    129   1.1.2.1  elad 	pool_put(&kauth_cred_pool, cred);
    130   1.1.2.1  elad }
    131   1.1.2.1  elad 
    132   1.1.2.1  elad /* Decrease reference count to cred. If reached zero, free it. */
    133   1.1.2.1  elad void
    134   1.1.2.1  elad kauth_cred_free(kauth_cred_t cred) {
    135   1.1.2.1  elad 	KASSERT(cred != NULL);
    136   1.1.2.1  elad 
    137   1.1.2.1  elad 	simple_lock(&cred->cr_lock);
    138   1.1.2.1  elad 	cred->cr_refcnt--;
    139   1.1.2.7  elad 	simple_unlock(&cred->cr_lock);
    140   1.1.2.7  elad 
    141   1.1.2.1  elad 	if (cred->cr_refcnt == 0)
    142   1.1.2.1  elad 		kauth_cred_destroy(cred);
    143   1.1.2.1  elad }
    144   1.1.2.1  elad 
    145   1.1.2.1  elad void
    146   1.1.2.1  elad kauth_cred_clone(kauth_cred_t from, kauth_cred_t to)
    147   1.1.2.1  elad {
    148   1.1.2.1  elad 	KASSERT(from != NULL);
    149   1.1.2.1  elad 	KASSERT(to != NULL);
    150   1.1.2.1  elad 
    151   1.1.2.1  elad 	to->cr_uid = from->cr_uid;
    152   1.1.2.1  elad 	to->cr_euid = from->cr_euid;
    153   1.1.2.1  elad 	to->cr_svuid = from->cr_svuid;
    154   1.1.2.1  elad 	to->cr_gid = from->cr_gid;
    155   1.1.2.1  elad 	to->cr_egid = from->cr_egid;
    156   1.1.2.1  elad 	to->cr_svgid = from->cr_svgid;
    157   1.1.2.1  elad 	to->cr_ngroups = from->cr_ngroups;
    158   1.1.2.1  elad 	memcpy(to->cr_groups, from->cr_groups,
    159   1.1.2.1  elad 	       sizeof(to->cr_groups));
    160   1.1.2.1  elad }
    161   1.1.2.1  elad 
    162   1.1.2.1  elad /*
    163   1.1.2.1  elad  * Duplicate cred and return a new kauth_cred_t.
    164   1.1.2.1  elad  */
    165   1.1.2.1  elad kauth_cred_t
    166   1.1.2.1  elad kauth_cred_dup(kauth_cred_t cred)
    167   1.1.2.1  elad {
    168   1.1.2.1  elad 	kauth_cred_t new_cred;
    169   1.1.2.1  elad 
    170   1.1.2.1  elad 	KASSERT(cred != NULL);
    171   1.1.2.1  elad 
    172   1.1.2.1  elad 	new_cred = kauth_cred_alloc();
    173   1.1.2.1  elad 
    174   1.1.2.1  elad 	kauth_cred_clone(cred, new_cred);
    175   1.1.2.1  elad 
    176   1.1.2.1  elad 	return (new_cred);
    177   1.1.2.1  elad }
    178   1.1.2.1  elad 
    179   1.1.2.1  elad /*
    180   1.1.2.1  elad  * Similar to crcopy(), only on a kauth_cred_t.
    181   1.1.2.1  elad  * XXX: Is this even needed? [kauth_cred_copy]
    182   1.1.2.1  elad  */
    183   1.1.2.1  elad kauth_cred_t
    184   1.1.2.1  elad kauth_cred_copy(kauth_cred_t cred)
    185   1.1.2.1  elad {
    186   1.1.2.1  elad 	kauth_cred_t new_cred;
    187   1.1.2.1  elad 
    188   1.1.2.1  elad 	KASSERT(cred != NULL);
    189   1.1.2.1  elad 
    190   1.1.2.1  elad 	/* If the provided credentials already have one reference, use them. */
    191   1.1.2.1  elad 	if (cred->cr_refcnt == 1)
    192   1.1.2.1  elad 		return (cred);
    193   1.1.2.1  elad 
    194   1.1.2.1  elad 	new_cred = kauth_cred_alloc();
    195   1.1.2.1  elad 
    196   1.1.2.1  elad 	kauth_cred_clone(cred, new_cred);
    197   1.1.2.1  elad 
    198   1.1.2.8  elad 	kauth_cred_free(cred);
    199   1.1.2.1  elad 
    200   1.1.2.1  elad 	return (new_cred);
    201   1.1.2.1  elad }
    202   1.1.2.1  elad 
    203   1.1.2.1  elad /*
    204   1.1.2.1  elad  * Zero a kauth_cred_t, and re-initialize the lock.
    205   1.1.2.1  elad  * XXX: Is this needed? [kauth_cred_zero]
    206   1.1.2.1  elad  */
    207   1.1.2.1  elad void
    208   1.1.2.1  elad kauth_cred_zero(kauth_cred_t cred)
    209   1.1.2.1  elad {
    210   1.1.2.1  elad 	KASSERT(cred != NULL);
    211   1.1.2.1  elad 
    212   1.1.2.1  elad 	memset(cred, 0, sizeof(*cred));
    213   1.1.2.1  elad 	simple_lock_init(&cred->cr_lock);
    214   1.1.2.1  elad }
    215   1.1.2.1  elad 
    216   1.1.2.1  elad uid_t
    217   1.1.2.1  elad kauth_cred_getuid(kauth_cred_t cred)
    218   1.1.2.1  elad {
    219   1.1.2.1  elad 	KASSERT(cred != NULL);
    220   1.1.2.1  elad 
    221   1.1.2.1  elad 	return (cred->cr_uid);
    222   1.1.2.1  elad }
    223   1.1.2.1  elad 
    224   1.1.2.1  elad uid_t
    225   1.1.2.1  elad kauth_cred_geteuid(kauth_cred_t cred)
    226   1.1.2.1  elad {
    227   1.1.2.1  elad 	KASSERT(cred != NULL);
    228   1.1.2.1  elad 
    229   1.1.2.1  elad 	return (cred->cr_euid);
    230   1.1.2.1  elad }
    231   1.1.2.1  elad 
    232   1.1.2.1  elad uid_t
    233   1.1.2.1  elad kauth_cred_getsvuid(kauth_cred_t cred)
    234   1.1.2.1  elad {
    235   1.1.2.1  elad 	KASSERT(cred != NULL);
    236   1.1.2.1  elad 
    237   1.1.2.1  elad 	return (cred->cr_svuid);
    238   1.1.2.1  elad }
    239   1.1.2.1  elad 
    240   1.1.2.1  elad gid_t
    241   1.1.2.1  elad kauth_cred_getgid(kauth_cred_t cred)
    242   1.1.2.1  elad {
    243   1.1.2.1  elad 	KASSERT(cred != NULL);
    244   1.1.2.1  elad 
    245   1.1.2.1  elad 	return (cred->cr_gid);
    246   1.1.2.1  elad }
    247   1.1.2.1  elad 
    248   1.1.2.1  elad gid_t
    249   1.1.2.1  elad kauth_cred_getegid(kauth_cred_t cred)
    250   1.1.2.1  elad {
    251   1.1.2.1  elad 	KASSERT(cred != NULL);
    252   1.1.2.1  elad 
    253   1.1.2.1  elad 	return (cred->cr_egid);
    254   1.1.2.1  elad }
    255   1.1.2.1  elad 
    256   1.1.2.1  elad gid_t
    257   1.1.2.1  elad kauth_cred_getsvgid(kauth_cred_t cred)
    258   1.1.2.1  elad {
    259   1.1.2.1  elad 	KASSERT(cred != NULL);
    260   1.1.2.1  elad 
    261   1.1.2.1  elad 	return (cred->cr_svgid);
    262   1.1.2.1  elad }
    263   1.1.2.1  elad 
    264   1.1.2.1  elad void
    265   1.1.2.1  elad kauth_cred_setuid(kauth_cred_t cred, uid_t uid)
    266   1.1.2.1  elad {
    267   1.1.2.1  elad 	KASSERT(cred != NULL);
    268   1.1.2.1  elad 	KASSERT(uid >= 0 && uid <= UID_MAX);
    269   1.1.2.1  elad 
    270   1.1.2.1  elad 	cred->cr_uid = uid;
    271   1.1.2.1  elad }
    272   1.1.2.1  elad 
    273   1.1.2.1  elad void
    274   1.1.2.1  elad kauth_cred_seteuid(kauth_cred_t cred, uid_t uid)
    275   1.1.2.1  elad {
    276   1.1.2.1  elad 	KASSERT(cred != NULL);
    277   1.1.2.1  elad 	KASSERT(uid >= 0 && uid <= UID_MAX);
    278   1.1.2.1  elad 
    279   1.1.2.1  elad 	cred->cr_euid = uid;
    280   1.1.2.1  elad }
    281   1.1.2.1  elad 
    282   1.1.2.1  elad void
    283   1.1.2.1  elad kauth_cred_setsvuid(kauth_cred_t cred, uid_t uid)
    284   1.1.2.1  elad {
    285   1.1.2.1  elad 	KASSERT(cred != NULL);
    286   1.1.2.1  elad 	KASSERT(uid >= 0 && uid <= UID_MAX);
    287   1.1.2.1  elad 
    288   1.1.2.1  elad 	cred->cr_svuid = uid;
    289   1.1.2.1  elad }
    290   1.1.2.1  elad 
    291   1.1.2.1  elad void
    292   1.1.2.1  elad kauth_cred_setgid(kauth_cred_t cred, gid_t gid)
    293   1.1.2.1  elad {
    294   1.1.2.1  elad 	KASSERT(cred != NULL);
    295   1.1.2.1  elad 	KASSERT(gid >= 0 && gid <= GID_MAX);
    296   1.1.2.1  elad 
    297   1.1.2.1  elad 	cred->cr_gid = gid;
    298   1.1.2.1  elad }
    299   1.1.2.1  elad 
    300   1.1.2.1  elad void
    301   1.1.2.1  elad kauth_cred_setegid(kauth_cred_t cred, gid_t gid)
    302   1.1.2.1  elad {
    303   1.1.2.1  elad 	KASSERT(cred != NULL);
    304   1.1.2.1  elad 	KASSERT(gid >= 0 && gid <= GID_MAX);
    305   1.1.2.1  elad 
    306   1.1.2.1  elad 	cred->cr_egid = gid;
    307   1.1.2.1  elad }
    308   1.1.2.1  elad 
    309   1.1.2.1  elad void
    310   1.1.2.1  elad kauth_cred_setsvgid(kauth_cred_t cred, gid_t gid)
    311   1.1.2.1  elad {
    312   1.1.2.1  elad 	KASSERT(cred != NULL);
    313   1.1.2.1  elad 	KASSERT(gid >= 0 && gid <= GID_MAX);
    314   1.1.2.1  elad 
    315   1.1.2.1  elad 	cred->cr_svgid = gid;
    316   1.1.2.1  elad }
    317   1.1.2.1  elad 
    318   1.1.2.1  elad /* Checks if gid is a member of the groups in cred. */
    319   1.1.2.1  elad int
    320   1.1.2.1  elad kauth_cred_groupmember(kauth_cred_t cred, gid_t gid)
    321   1.1.2.1  elad {
    322   1.1.2.1  elad 	int i;
    323   1.1.2.1  elad 
    324   1.1.2.1  elad 	KASSERT(cred != NULL);
    325   1.1.2.1  elad 	KASSERT(gid >= 0 && gid <= GID_MAX);
    326   1.1.2.1  elad 
    327   1.1.2.1  elad 	for (i = 0; i < cred->cr_ngroups; i++) {
    328   1.1.2.1  elad 		if (cred->cr_groups[i] == gid)
    329   1.1.2.1  elad 			return (1);
    330   1.1.2.1  elad 	}
    331   1.1.2.1  elad 
    332   1.1.2.1  elad 	return (0);
    333   1.1.2.1  elad }
    334   1.1.2.1  elad 
    335   1.1.2.1  elad uint16_t
    336   1.1.2.1  elad kauth_cred_ngroups(kauth_cred_t cred)
    337   1.1.2.1  elad {
    338   1.1.2.1  elad 	KASSERT(cred != NULL);
    339   1.1.2.1  elad 
    340   1.1.2.1  elad 	return (cred->cr_ngroups);
    341   1.1.2.1  elad }
    342   1.1.2.1  elad 
    343   1.1.2.1  elad /*
    344   1.1.2.1  elad  * Return the group at index idx from the groups in cred.
    345   1.1.2.1  elad  */
    346   1.1.2.1  elad gid_t
    347   1.1.2.1  elad kauth_cred_group(kauth_cred_t cred, uint16_t idx)
    348   1.1.2.1  elad {
    349   1.1.2.1  elad 	KASSERT(cred != NULL);
    350   1.1.2.1  elad 	KASSERT(idx < cred->cr_ngroups);
    351   1.1.2.1  elad 
    352   1.1.2.1  elad 	return (cred->cr_groups[idx]);
    353   1.1.2.1  elad }
    354   1.1.2.1  elad 
    355   1.1.2.1  elad /* XXX: Is this needed? [setngroups] */
    356   1.1.2.1  elad void
    357   1.1.2.1  elad kauth_cred_setngroups(kauth_cred_t cred, uint16_t ngroups)
    358   1.1.2.1  elad {
    359   1.1.2.1  elad 	KASSERT(cred != NULL);
    360   1.1.2.1  elad 	KASSERT(ngroups < NGROUPS);
    361   1.1.2.1  elad 
    362   1.1.2.1  elad 	cred->cr_ngroups = ngroups;
    363   1.1.2.1  elad }
    364   1.1.2.1  elad 
    365   1.1.2.1  elad /* Sort len groups in grbuf. */
    366   1.1.2.1  elad static void
    367   1.1.2.1  elad kauth_cred_sortgroups(gid_t *grbuf, size_t len)
    368   1.1.2.1  elad {
    369   1.1.2.1  elad 	int i, j;
    370   1.1.2.1  elad 	gid_t v;
    371   1.1.2.1  elad 
    372   1.1.2.1  elad 	KASSERT(grbuf != NULL);
    373   1.1.2.1  elad 
    374   1.1.2.1  elad 	/* Insertion sort. */
    375   1.1.2.1  elad 	for (i = 1; i < len; i++) {
    376   1.1.2.1  elad 		v = grbuf[i];
    377   1.1.2.1  elad 		/* find correct slot for value v, moving others up */
    378   1.1.2.1  elad 		for (j = i; --j >= 0 && v < grbuf[j];)
    379   1.1.2.1  elad 			grbuf[j + 1] = grbuf[j];
    380   1.1.2.1  elad 		grbuf[j + 1] = v;
    381   1.1.2.1  elad 	}
    382   1.1.2.1  elad }
    383   1.1.2.1  elad 
    384   1.1.2.1  elad /* Add group gid to groups in cred. Maintain order. */
    385   1.1.2.1  elad int
    386   1.1.2.1  elad kauth_cred_addgroup(kauth_cred_t cred, gid_t gid)
    387   1.1.2.1  elad {
    388   1.1.2.1  elad 	KASSERT(cred != NULL);
    389   1.1.2.1  elad 	KASSERT(gid >= 0 && gid <= GID_MAX);
    390   1.1.2.1  elad 
    391   1.1.2.2  elad 	simple_lock(&cred->cr_lock);
    392   1.1.2.1  elad 
    393   1.1.2.1  elad 	if (cred->cr_ngroups >=
    394   1.1.2.1  elad 	    (sizeof(cred->cr_groups) / sizeof(cred->cr_groups[0]))) {
    395   1.1.2.2  elad 		simple_unlock(&cred->cr_lock);
    396   1.1.2.1  elad 		return (E2BIG);
    397   1.1.2.1  elad 	}
    398   1.1.2.1  elad 
    399   1.1.2.1  elad 	if (kauth_cred_groupmember(cred, gid)) {
    400   1.1.2.2  elad 		simple_unlock(&cred->cr_lock);
    401   1.1.2.1  elad 		return (0);
    402   1.1.2.1  elad 	}
    403   1.1.2.1  elad 
    404   1.1.2.1  elad 	cred->cr_groups[cred->cr_ngroups++] = gid;
    405   1.1.2.1  elad 
    406   1.1.2.1  elad 	kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
    407   1.1.2.1  elad 
    408   1.1.2.2  elad 	simple_unlock(&cred->cr_lock);
    409   1.1.2.1  elad 
    410   1.1.2.1  elad 	return (0);
    411   1.1.2.1  elad }
    412   1.1.2.1  elad 
    413   1.1.2.1  elad /* Delete group gid from groups in cred. Maintain order. */
    414   1.1.2.1  elad int
    415   1.1.2.1  elad kauth_cred_delgroup(kauth_cred_t cred, gid_t gid)
    416   1.1.2.1  elad {
    417   1.1.2.1  elad 	KASSERT(cred != NULL);
    418   1.1.2.1  elad 	KASSERT(gid >= 0 && gid <= GID_MAX);
    419   1.1.2.1  elad 
    420   1.1.2.2  elad 	simple_lock(&cred->cr_lock);
    421   1.1.2.1  elad 
    422   1.1.2.2  elad 	if (!kauth_cred_groupmember(cred, gid)) {
    423   1.1.2.2  elad 		simple_unlock(&cred->cr_lock);
    424   1.1.2.1  elad 		return (0);
    425   1.1.2.2  elad 	}
    426   1.1.2.1  elad 
    427   1.1.2.1  elad 	if (cred->cr_ngroups == 1)
    428   1.1.2.1  elad 		cred->cr_groups[0] = (gid_t)-1; /* XXX */
    429   1.1.2.1  elad 	else {
    430   1.1.2.1  elad 		int i;
    431   1.1.2.1  elad 
    432   1.1.2.1  elad 		for (i = 0; i < cred->cr_ngroups; i++)
    433   1.1.2.1  elad 			if (cred->cr_groups[i] == gid) {
    434   1.1.2.1  elad 				cred->cr_groups[i] = cred->cr_groups[cred->cr_ngroups - 1];
    435   1.1.2.1  elad 				cred->cr_groups[cred->cr_ngroups - 1] = (gid_t)-1; /* XXX */
    436   1.1.2.1  elad 			}
    437   1.1.2.1  elad 	}
    438   1.1.2.1  elad 
    439   1.1.2.1  elad 	cred->cr_ngroups--;
    440   1.1.2.1  elad 
    441   1.1.2.1  elad 	kauth_cred_sortgroups(cred->cr_groups, cred->cr_ngroups);
    442   1.1.2.1  elad 
    443   1.1.2.2  elad 	simple_unlock(&cred->cr_lock);
    444   1.1.2.1  elad 
    445   1.1.2.1  elad 	return (0);
    446   1.1.2.1  elad }
    447   1.1.2.1  elad 
    448   1.1.2.1  elad /*
    449   1.1.2.1  elad  * Match uids in two credentials. Checks if cred1 can access stuff owned by
    450   1.1.2.1  elad  * cred2.
    451   1.1.2.1  elad  * XXX: root bypasses this!
    452   1.1.2.1  elad  */
    453   1.1.2.1  elad int
    454   1.1.2.1  elad kauth_cred_uidmatch(kauth_cred_t cred1, kauth_cred_t cred2)
    455   1.1.2.1  elad {
    456   1.1.2.1  elad 	KASSERT(cred1 != NULL);
    457   1.1.2.1  elad 	KASSERT(cred2 != NULL);
    458   1.1.2.1  elad 
    459   1.1.2.1  elad 	/* Are we root? */
    460   1.1.2.1  elad 	if (cred1->cr_euid == 0)
    461   1.1.2.1  elad 		return (1);
    462   1.1.2.1  elad 
    463   1.1.2.1  elad 	if (cred1->cr_uid == cred2->cr_uid ||
    464   1.1.2.1  elad 	    cred1->cr_euid == cred2->cr_uid ||
    465   1.1.2.1  elad 	    cred1->cr_uid == cred2->cr_euid ||
    466   1.1.2.1  elad 	    cred1->cr_euid == cred2->cr_euid)
    467   1.1.2.1  elad 		return (1);
    468   1.1.2.1  elad 
    469   1.1.2.1  elad 	return (0);
    470   1.1.2.1  elad }
    471   1.1.2.1  elad 
    472   1.1.2.1  elad uint32_t
    473   1.1.2.1  elad kauth_cred_getrefcnt(kauth_cred_t cred)
    474   1.1.2.1  elad {
    475   1.1.2.1  elad 	KASSERT(cred != NULL);
    476   1.1.2.1  elad 
    477   1.1.2.1  elad 	return (cred->cr_refcnt);
    478   1.1.2.1  elad }
    479   1.1.2.1  elad 
    480   1.1.2.1  elad void
    481   1.1.2.1  elad kauth_cred_setrefcnt(kauth_cred_t cred, uint32_t refcnt)
    482   1.1.2.1  elad {
    483   1.1.2.1  elad 	KASSERT(cred != NULL);
    484   1.1.2.1  elad 
    485   1.1.2.1  elad 	cred->cr_refcnt = refcnt;
    486   1.1.2.1  elad }
    487   1.1.2.1  elad 
    488   1.1.2.1  elad /* XXX: memcmp() wrapper needed for NFSW_SAMECRED() in nfs/nfs.h */
    489   1.1.2.1  elad int
    490   1.1.2.1  elad kauth_cred_memcmp(kauth_cred_t cred1, kauth_cred_t cred2)
    491   1.1.2.1  elad {
    492   1.1.2.1  elad 	KASSERT(cred1 != NULL);
    493   1.1.2.1  elad 	KASSERT(cred2 != NULL);
    494   1.1.2.1  elad 
    495   1.1.2.1  elad 	return (memcmp(cred1, cred2, sizeof(*cred1)));
    496   1.1.2.1  elad }
    497   1.1.2.1  elad 
    498   1.1.2.1  elad /*
    499   1.1.2.1  elad  * Convert userland credentials (struct uucred) to kauth_cred_t.
    500   1.1.2.1  elad  * XXX: For NFS code.
    501   1.1.2.1  elad  */
    502   1.1.2.1  elad void
    503   1.1.2.1  elad kauth_cred_convert(kauth_cred_t cred, const struct uucred *uuc)
    504   1.1.2.1  elad {
    505   1.1.2.1  elad 	int i;
    506   1.1.2.1  elad 
    507   1.1.2.1  elad 	KASSERT(cred != NULL);
    508   1.1.2.1  elad 	KASSERT(uuc != NULL);
    509   1.1.2.1  elad 
    510   1.1.2.1  elad 	cred->cr_refcnt = 0;
    511   1.1.2.1  elad 	cred->cr_euid = uuc->cr_uid;
    512   1.1.2.1  elad 	cred->cr_egid = uuc->cr_gid;
    513   1.1.2.1  elad 	cred->cr_ngroups = min(uuc->cr_ngroups, NGROUPS);
    514   1.1.2.1  elad 	for (i = 0; i < cred->cr_ngroups; i++)
    515   1.1.2.1  elad 		kauth_cred_addgroup(cred, uuc->cr_groups[i]);
    516   1.1.2.1  elad }
    517   1.1.2.1  elad 
    518   1.1.2.1  elad /*
    519   1.1.2.1  elad  * Compare kauth_cred_t and uucred credentials.
    520   1.1.2.1  elad  * XXX: Modelled after crcmp() for NFS.
    521   1.1.2.1  elad  */
    522   1.1.2.1  elad int
    523   1.1.2.1  elad kauth_cred_compare(kauth_cred_t cred, const struct uucred *uuc)
    524   1.1.2.1  elad {
    525   1.1.2.1  elad 	KASSERT(cred != NULL);
    526   1.1.2.1  elad 	KASSERT(uuc != NULL);
    527   1.1.2.1  elad 
    528   1.1.2.1  elad 	if (cred->cr_euid == uuc->cr_uid &&
    529   1.1.2.1  elad 	    cred->cr_egid == uuc->cr_gid &&
    530   1.1.2.1  elad 	    cred->cr_ngroups == uuc->cr_ngroups &&
    531   1.1.2.1  elad 	    !memcmp(cred->cr_groups, uuc->cr_groups,
    532   1.1.2.1  elad 		    cred->cr_ngroups * sizeof(cred->cr_groups[0])))
    533   1.1.2.1  elad 		return (1);
    534   1.1.2.1  elad 
    535   1.1.2.1  elad 	return (0);
    536   1.1.2.1  elad }
    537   1.1.2.1  elad 
    538   1.1.2.1  elad /*
    539   1.1.2.1  elad  * Make a struct ucred out of a kauth_cred_t.
    540   1.1.2.1  elad  * XXX: For sysctl.
    541   1.1.2.1  elad  */
    542   1.1.2.1  elad void
    543   1.1.2.1  elad kauth_cred_toucred(kauth_cred_t cred, struct ucred *uc)
    544   1.1.2.1  elad {
    545   1.1.2.1  elad 	KASSERT(cred != NULL);
    546   1.1.2.1  elad 	KASSERT(uc != NULL);
    547   1.1.2.1  elad 
    548   1.1.2.1  elad 	uc->cr_uid = cred->cr_euid;
    549   1.1.2.1  elad 	uc->cr_gid = cred->cr_egid;
    550   1.1.2.1  elad 	uc->cr_ngroups = min(cred->cr_ngroups,
    551   1.1.2.1  elad 			     sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
    552   1.1.2.1  elad 	memcpy(uc->cr_groups, cred->cr_groups,
    553   1.1.2.1  elad 	       uc->cr_ngroups * sizeof(uc->cr_groups[0]));
    554   1.1.2.1  elad }
    555   1.1.2.1  elad 
    556   1.1.2.1  elad /*
    557   1.1.2.1  elad  * Make a struct pcred out of a kauth_cred_t.
    558   1.1.2.1  elad  * XXX: For sysctl.
    559   1.1.2.1  elad  */
    560   1.1.2.1  elad void
    561   1.1.2.1  elad kauth_cred_topcred(kauth_cred_t cred, struct pcred *pc)
    562   1.1.2.1  elad {
    563   1.1.2.1  elad 	KASSERT(cred != NULL);
    564   1.1.2.1  elad 	KASSERT(pc != NULL);
    565   1.1.2.1  elad 
    566   1.1.2.1  elad 	pc->pc_ucred = (struct ucred *)cred; /* XXX this is just wrong */
    567   1.1.2.1  elad 	pc->p_ruid = cred->cr_uid;
    568   1.1.2.1  elad 	pc->p_svuid = cred->cr_svuid;
    569   1.1.2.1  elad 	pc->p_rgid = cred->cr_gid;
    570   1.1.2.1  elad 	pc->p_svgid = cred->cr_svgid;
    571   1.1.2.1  elad 	pc->p_refcnt = cred->cr_refcnt;
    572   1.1.2.1  elad }
    573   1.1.2.1  elad 
    574   1.1.2.1  elad /*
    575   1.1.2.5  elad  * Return kauth_cred_t for the current process.
    576   1.1.2.5  elad  */
    577   1.1.2.5  elad kauth_cred_t
    578   1.1.2.5  elad kauth_cred_get(void)
    579   1.1.2.5  elad {
    580   1.1.2.5  elad 	return (curproc->p_cred);
    581   1.1.2.5  elad }
    582   1.1.2.5  elad 
    583   1.1.2.5  elad /*
    584   1.1.2.1  elad  * Returns a scope matching the provided id.
    585   1.1.2.1  elad  * Requires the scope list lock to be held by the caller.
    586   1.1.2.1  elad  */
    587   1.1.2.1  elad static kauth_scope_t
    588   1.1.2.1  elad kauth_ifindscope(const char *id) {
    589   1.1.2.1  elad 	kauth_scope_t scope;
    590   1.1.2.1  elad 
    591   1.1.2.1  elad 	/* XXX: assert lock on scope list? */
    592   1.1.2.1  elad 
    593   1.1.2.1  elad 	scope = NULL;
    594   1.1.2.1  elad 	SIMPLEQ_FOREACH(scope, &scope_list, next_scope) {
    595   1.1.2.1  elad 		if (strcmp(scope->id, id) == 0)
    596   1.1.2.1  elad 			break;
    597   1.1.2.1  elad 	}
    598   1.1.2.1  elad 
    599   1.1.2.1  elad 	return (scope);
    600   1.1.2.1  elad }
    601   1.1.2.1  elad 
    602   1.1.2.1  elad /*
    603   1.1.2.1  elad  * Register a new scope.
    604   1.1.2.1  elad  *
    605   1.1.2.1  elad  * id - identifier for the scope
    606   1.1.2.1  elad  * callback - the scope's default listener
    607   1.1.2.1  elad  * cookie - cookie to be passed to the listener(s)
    608   1.1.2.1  elad  */
    609   1.1.2.1  elad kauth_scope_t
    610   1.1.2.1  elad kauth_register_scope(const char *id, kauth_scope_callback_t callback,
    611   1.1.2.1  elad 		     void *cookie)
    612   1.1.2.1  elad {
    613   1.1.2.1  elad 	kauth_scope_t scope;
    614   1.1.2.1  elad 	kauth_listener_t listener;
    615   1.1.2.1  elad 
    616   1.1.2.1  elad 	/* Sanitize input */
    617   1.1.2.1  elad 	if (id == NULL || callback == NULL)
    618   1.1.2.1  elad 		return (NULL);
    619   1.1.2.1  elad 
    620   1.1.2.4  elad 	/* Allocate space for a new scope and listener. */
    621   1.1.2.4  elad 	scope = pool_get(&kauth_scope_pool, PR_WAITOK);
    622   1.1.2.4  elad 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    623   1.1.2.4  elad 
    624   1.1.2.1  elad 	/* Acquire scope list lock. */
    625   1.1.2.1  elad 	simple_lock(&scopes_lock);
    626   1.1.2.1  elad 
    627   1.1.2.1  elad 	/* Check we don't already have a scope with the same id */
    628   1.1.2.1  elad 	if (kauth_ifindscope(id) != NULL) {
    629   1.1.2.1  elad 		simple_unlock(&scopes_lock);
    630   1.1.2.4  elad 
    631   1.1.2.4  elad 		pool_put(&kauth_scope_pool, scope);
    632   1.1.2.4  elad 		pool_put(&kauth_listener_pool, listener);
    633   1.1.2.4  elad 
    634   1.1.2.1  elad 		return (NULL);
    635   1.1.2.1  elad 	}
    636   1.1.2.1  elad 
    637   1.1.2.1  elad 	/* Initialize new scope with parameters */
    638   1.1.2.1  elad 	scope->id = id;
    639   1.1.2.1  elad 	scope->cookie = cookie;
    640   1.1.2.1  elad 	scope->nlisteners = 1;
    641   1.1.2.1  elad 
    642   1.1.2.1  elad 	/* Add default listener */
    643   1.1.2.1  elad 	listener->func = callback;
    644   1.1.2.1  elad 	listener->scope = scope;
    645   1.1.2.1  elad 	listener->refcnt = 0;
    646   1.1.2.1  elad 	SIMPLEQ_INIT(&scope->listenq);
    647   1.1.2.1  elad 	SIMPLEQ_INSERT_HEAD(&scope->listenq, listener, listener_next);
    648   1.1.2.1  elad 
    649   1.1.2.1  elad 	/* Insert scope to scopes list */
    650   1.1.2.1  elad 	if (SIMPLEQ_EMPTY(&scope_list))
    651   1.1.2.1  elad 		SIMPLEQ_INSERT_HEAD(&scope_list, scope, next_scope);
    652   1.1.2.1  elad 	else
    653   1.1.2.1  elad 		SIMPLEQ_INSERT_TAIL(&scope_list, scope, next_scope);
    654   1.1.2.1  elad 
    655   1.1.2.1  elad 	simple_unlock(&scopes_lock);
    656   1.1.2.1  elad 
    657   1.1.2.1  elad 	return (scope);
    658   1.1.2.1  elad }
    659   1.1.2.1  elad 
    660   1.1.2.1  elad /*
    661   1.1.2.1  elad  * Initialize the kernel authorization subsystem.
    662   1.1.2.1  elad  *
    663   1.1.2.1  elad  * Initialize the scopes list lock.
    664   1.1.2.1  elad  * Register built-in scopes: generic, process.
    665   1.1.2.1  elad  */
    666   1.1.2.1  elad void
    667   1.1.2.1  elad kauth_init(void)
    668   1.1.2.1  elad {
    669   1.1.2.1  elad 	simple_lock_init(&scopes_lock);
    670   1.1.2.1  elad 
    671   1.1.2.1  elad 	/* Register generic scope. */
    672   1.1.2.9  elad 	kauth_builtin_scope_generic = kauth_register_scope(KAUTH_SCOPE_GENERIC,
    673   1.1.2.9  elad 	    kauth_authorize_cb_generic, NULL);
    674   1.1.2.1  elad 
    675   1.1.2.1  elad 	/* Register process scope. */
    676   1.1.2.9  elad 	kauth_builtin_scope_process = kauth_register_scope(KAUTH_SCOPE_PROCESS,
    677   1.1.2.9  elad 	    kauth_authorize_cb_process, NULL);
    678   1.1.2.1  elad }
    679   1.1.2.1  elad 
    680   1.1.2.1  elad /*
    681   1.1.2.1  elad  * Deregister a scope.
    682   1.1.2.1  elad  * Requires scope list lock to be held by the caller.
    683   1.1.2.1  elad  *
    684   1.1.2.1  elad  * scope - the scope to deregister
    685   1.1.2.1  elad  */
    686   1.1.2.1  elad void
    687   1.1.2.1  elad kauth_deregister_scope(kauth_scope_t scope)
    688   1.1.2.1  elad {
    689   1.1.2.1  elad 	if (scope != NULL) {
    690   1.1.2.1  elad 		/* Remove scope from list */
    691   1.1.2.1  elad 		SIMPLEQ_REMOVE(&scope_list, scope, kauth_scope, next_scope);
    692   1.1.2.1  elad 	}
    693   1.1.2.1  elad }
    694   1.1.2.1  elad 
    695   1.1.2.1  elad /*
    696   1.1.2.1  elad  * Register a listener.
    697   1.1.2.1  elad  *
    698   1.1.2.1  elad  * id - scope identifier.
    699   1.1.2.1  elad  * callback - the callback routine for the listener.
    700   1.1.2.1  elad  * cookie - cookie to pass unmoidfied to the callback.
    701   1.1.2.1  elad  */
    702   1.1.2.1  elad kauth_listener_t
    703   1.1.2.1  elad kauth_listen_scope(const char *id, kauth_scope_callback_t callback,
    704   1.1.2.1  elad 		   void *cookie)
    705   1.1.2.1  elad {
    706   1.1.2.1  elad 	kauth_scope_t scope;
    707   1.1.2.1  elad 	kauth_listener_t listener;
    708   1.1.2.1  elad 
    709   1.1.2.1  elad 	/* Find scope struct */
    710   1.1.2.3  elad 	simple_lock(&scopes_lock);
    711   1.1.2.1  elad 	scope = kauth_ifindscope(id);
    712   1.1.2.3  elad 	simple_unlock(&scopes_lock);
    713   1.1.2.1  elad 	if (scope == NULL)
    714   1.1.2.1  elad 		return (NULL);
    715   1.1.2.1  elad 
    716   1.1.2.1  elad 	/* Allocate listener */
    717   1.1.2.1  elad 	listener = pool_get(&kauth_listener_pool, PR_WAITOK);
    718   1.1.2.1  elad 
    719   1.1.2.1  elad 	/* Initialize listener with parameters */
    720   1.1.2.1  elad 	listener->func = callback;
    721   1.1.2.1  elad 	listener->refcnt = 0;
    722   1.1.2.1  elad 
    723   1.1.2.1  elad 	/* Add listener to scope */
    724   1.1.2.1  elad 	SIMPLEQ_INSERT_TAIL(&scope->listenq, listener, listener_next);
    725   1.1.2.1  elad 
    726   1.1.2.1  elad 	/* Raise number of listeners on scope. */
    727   1.1.2.1  elad 	scope->nlisteners++;
    728   1.1.2.1  elad 	listener->scope = scope;
    729   1.1.2.1  elad 
    730   1.1.2.1  elad 	return (listener);
    731   1.1.2.1  elad }
    732   1.1.2.1  elad 
    733   1.1.2.1  elad /*
    734   1.1.2.1  elad  * Deregister a listener.
    735   1.1.2.1  elad  *
    736   1.1.2.1  elad  * listener - listener reference as returned from kauth_listen_scope().
    737   1.1.2.1  elad  */
    738   1.1.2.1  elad void
    739   1.1.2.1  elad kauth_unlisten_scope(kauth_listener_t listener)
    740   1.1.2.1  elad {
    741   1.1.2.1  elad 	if (listener != NULL) {
    742   1.1.2.1  elad 		SIMPLEQ_REMOVE(&listener->scope->listenq, listener, kauth_listener,
    743   1.1.2.1  elad 			       listener_next);
    744   1.1.2.1  elad 		listener->scope->nlisteners--;
    745   1.1.2.1  elad 	}
    746   1.1.2.1  elad }
    747   1.1.2.1  elad 
    748   1.1.2.1  elad /*
    749   1.1.2.1  elad  * Authorize a request.
    750   1.1.2.1  elad  *
    751   1.1.2.1  elad  * scope - the scope of the request as defined by KAUTH_SCOPE_* or as
    752   1.1.2.1  elad  *	   returned from kauth_register_scope().
    753   1.1.2.1  elad  * credential - credentials of the user ("actor") making the request.
    754   1.1.2.1  elad  * action - request identifier.
    755   1.1.2.1  elad  * arg[0-3] - passed unmodified to listener(s).
    756   1.1.2.1  elad  */
    757   1.1.2.1  elad int
    758   1.1.2.1  elad kauth_authorize_action(kauth_scope_t scope, kauth_cred_t cred,
    759   1.1.2.1  elad 		       kauth_action_t action, void *arg0, void *arg1,
    760   1.1.2.1  elad 		       void *arg2, void *arg3)
    761   1.1.2.1  elad {
    762   1.1.2.1  elad 	kauth_listener_t listener;
    763   1.1.2.1  elad 	int error, allow, fail;
    764   1.1.2.1  elad 
    765   1.1.2.1  elad 	/* Sanitize input */
    766   1.1.2.1  elad 	if (scope == NULL || cred == NULL)
    767   1.1.2.1  elad 		return (EFAULT);
    768   1.1.2.1  elad 	if (!action)
    769   1.1.2.1  elad 		return (EINVAL);
    770   1.1.2.1  elad 
    771   1.1.2.1  elad 	/*
    772   1.1.2.1  elad 	 * Each scope is associated with at least one listener. We need to
    773   1.1.2.1  elad 	 * traverse that list of listeners, as long as they return either
    774   1.1.2.1  elad 	 * KAUTH_REQUEST_DEFER or KAUTH_REQUEST_ALLOW.
    775   1.1.2.1  elad 	 */
    776   1.1.2.1  elad 	fail = 0;
    777   1.1.2.1  elad 	allow = 0;
    778   1.1.2.1  elad 	SIMPLEQ_FOREACH(listener, &scope->listenq, listener_next) {
    779   1.1.2.1  elad 		error = listener->func(cred, action, scope->cookie, arg0,
    780   1.1.2.1  elad 				       arg1, arg2, arg3);
    781   1.1.2.1  elad 
    782   1.1.2.1  elad 		if (error == KAUTH_RESULT_ALLOW)
    783   1.1.2.1  elad 			allow = 1;
    784   1.1.2.1  elad 		else if (error == KAUTH_RESULT_DENY)
    785   1.1.2.1  elad 			fail = 1;
    786   1.1.2.1  elad 	}
    787   1.1.2.1  elad 
    788   1.1.2.1  elad 	return ((allow && !fail) ? 0 : EPERM);
    789   1.1.2.1  elad };
    790   1.1.2.1  elad 
    791   1.1.2.1  elad /*
    792   1.1.2.1  elad  * Generic scope default callback.
    793   1.1.2.1  elad  */
    794   1.1.2.1  elad int
    795   1.1.2.1  elad kauth_authorize_cb_generic(kauth_cred_t cred, kauth_action_t action,
    796   1.1.2.1  elad 			   void *cookie, void *arg0, void *arg1, void *arg2,
    797   1.1.2.1  elad 			   void *arg3)
    798   1.1.2.1  elad {
    799   1.1.2.1  elad 	int error;
    800   1.1.2.1  elad 
    801   1.1.2.1  elad 	error = KAUTH_RESULT_DEFER;
    802   1.1.2.1  elad 
    803   1.1.2.1  elad 	switch (action) {
    804   1.1.2.1  elad 	case KAUTH_GENERIC_ISSUSER:
    805   1.1.2.1  elad 		/* Check if credential belongs to superuser. */
    806   1.1.2.1  elad 		if (cred->cr_euid == 0) {
    807   1.1.2.1  elad 			u_short *acflag = (u_short *)arg0;
    808   1.1.2.1  elad 
    809   1.1.2.1  elad 			if (acflag != NULL)
    810   1.1.2.1  elad 				*acflag |= ASU;
    811   1.1.2.1  elad 
    812   1.1.2.1  elad 			error = KAUTH_RESULT_ALLOW;
    813   1.1.2.1  elad 		} else
    814   1.1.2.1  elad 			error = KAUTH_RESULT_DENY;
    815   1.1.2.1  elad 		break;
    816   1.1.2.1  elad 	}
    817   1.1.2.1  elad 
    818   1.1.2.1  elad 	return (error);
    819   1.1.2.1  elad }
    820   1.1.2.1  elad 
    821   1.1.2.1  elad /*
    822   1.1.2.1  elad  * Generic scope authorization wrapper.
    823   1.1.2.1  elad  */
    824   1.1.2.1  elad int
    825   1.1.2.1  elad generic_authorize(kauth_cred_t cred, kauth_action_t action, void *arg0)
    826   1.1.2.1  elad {
    827   1.1.2.9  elad 	return (kauth_authorize_action(kauth_builtin_scope_generic, cred,
    828   1.1.2.9  elad 	    action, arg0, NULL, NULL, NULL));
    829   1.1.2.1  elad }
    830   1.1.2.1  elad 
    831   1.1.2.1  elad /*
    832   1.1.2.1  elad  * Process scope default callback.
    833   1.1.2.1  elad  */
    834   1.1.2.1  elad int
    835   1.1.2.1  elad kauth_authorize_cb_process(kauth_cred_t cred, kauth_action_t action,
    836   1.1.2.1  elad 			   void *cookie, void *arg0, void *arg1, void *arg2,
    837   1.1.2.1  elad 			   void *arg3)
    838   1.1.2.1  elad {
    839   1.1.2.1  elad 	struct proc *p;
    840   1.1.2.1  elad 	kauth_cred_t cred2;
    841   1.1.2.1  elad 	int error;
    842   1.1.2.1  elad 
    843   1.1.2.1  elad 	error = KAUTH_RESULT_DEFER;
    844   1.1.2.1  elad 
    845   1.1.2.1  elad 	p = arg0;
    846   1.1.2.1  elad 	cred2 = arg1;
    847   1.1.2.1  elad 
    848   1.1.2.1  elad 	switch (action) {
    849   1.1.2.1  elad 	case KAUTH_PROCESS_CANSIGNAL: {
    850   1.1.2.1  elad 		struct proc *to;
    851   1.1.2.1  elad 		int signum;
    852   1.1.2.1  elad 
    853   1.1.2.1  elad 		to = arg2;
    854   1.1.2.1  elad 		signum = (int)(unsigned long)arg3;
    855   1.1.2.1  elad 
    856   1.1.2.6  elad 		if (kauth_cred_uidmatch(cred, cred2)  ||
    857   1.1.2.1  elad 		    (signum == SIGCONT && (p->p_session == to->p_session)))
    858   1.1.2.1  elad 			error = KAUTH_RESULT_ALLOW;
    859   1.1.2.1  elad 		else
    860   1.1.2.1  elad 			error = KAUTH_RESULT_DEFER;
    861   1.1.2.1  elad 
    862   1.1.2.1  elad 		break;
    863   1.1.2.1  elad 		}
    864   1.1.2.1  elad 
    865   1.1.2.1  elad 	case KAUTH_PROCESS_CANPTRACE:
    866   1.1.2.6  elad 		if (kauth_cred_uidmatch(cred, cred2))
    867   1.1.2.1  elad 			error = KAUTH_RESULT_ALLOW;
    868   1.1.2.1  elad 		else
    869   1.1.2.1  elad 			error = KAUTH_RESULT_DENY;
    870   1.1.2.1  elad 		break;
    871   1.1.2.1  elad 
    872   1.1.2.1  elad 	case KAUTH_PROCESS_CANSEE:
    873   1.1.2.6  elad 		if (kauth_cred_uidmatch(cred, cred2))
    874   1.1.2.1  elad 			error = KAUTH_RESULT_ALLOW;
    875   1.1.2.1  elad 		else
    876   1.1.2.1  elad 			error = KAUTH_RESULT_DENY;
    877   1.1.2.1  elad 			/* arg2 - type of information [XXX NOTIMPL] */
    878   1.1.2.1  elad 		break;
    879   1.1.2.1  elad 	}
    880   1.1.2.1  elad 
    881   1.1.2.1  elad 	return (error);
    882   1.1.2.1  elad }
    883   1.1.2.9  elad 
    884   1.1.2.9  elad /*
    885   1.1.2.9  elad  * Process scope authorization wrapper.
    886   1.1.2.9  elad  */
    887   1.1.2.9  elad int
    888  1.1.2.11  elad kauth_authorize_process(kauth_cred_t cred, kauth_action_t action,
    889  1.1.2.11  elad     struct proc *p, void *arg1, void *arg2, void *arg3)
    890   1.1.2.9  elad {
    891   1.1.2.9  elad 	return (kauth_authorize_action(kauth_builtin_scope_process, cred,
    892   1.1.2.9  elad 	    action, p, arg1, arg2, arg3));
    893   1.1.2.9  elad }
    894