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