Home | History | Annotate | Line # | Download | only in umapfs
umap_subr.c revision 1.25
      1  1.25       dsl /*	$NetBSD: umap_subr.c,v 1.25 2007/06/30 15:27:03 dsl Exp $	*/
      2   1.2       cgd 
      3   1.1   mycroft /*
      4  1.15  wrstuden  * Copyright (c) 1999 National Aeronautics & Space Administration
      5  1.15  wrstuden  * All rights reserved.
      6  1.15  wrstuden  *
      7  1.15  wrstuden  * This software was written by William Studenmund of the
      8  1.17       wiz  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
      9  1.15  wrstuden  *
     10  1.15  wrstuden  * Redistribution and use in source and binary forms, with or without
     11  1.15  wrstuden  * modification, are permitted provided that the following conditions
     12  1.15  wrstuden  * are met:
     13  1.15  wrstuden  * 1. Redistributions of source code must retain the above copyright
     14  1.15  wrstuden  *    notice, this list of conditions and the following disclaimer.
     15  1.15  wrstuden  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.15  wrstuden  *    notice, this list of conditions and the following disclaimer in the
     17  1.15  wrstuden  *    documentation and/or other materials provided with the distribution.
     18  1.16     soren  * 3. Neither the name of the National Aeronautics & Space Administration
     19  1.15  wrstuden  *    nor the names of its contributors may be used to endorse or promote
     20  1.15  wrstuden  *    products derived from this software without specific prior written
     21  1.15  wrstuden  *    permission.
     22  1.15  wrstuden  *
     23  1.15  wrstuden  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
     24  1.15  wrstuden  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.15  wrstuden  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.15  wrstuden  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
     27  1.15  wrstuden  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     28  1.15  wrstuden  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.15  wrstuden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.15  wrstuden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.15  wrstuden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.15  wrstuden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.15  wrstuden  * POSSIBILITY OF SUCH DAMAGE.
     34  1.15  wrstuden  */
     35  1.15  wrstuden /*
     36  1.13      fvdl  * Copyright (c) 1992, 1993, 1995
     37   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     38   1.1   mycroft  *
     39   1.1   mycroft  * This code is derived from software donated to Berkeley by
     40   1.1   mycroft  * Jan-Simon Pendry.
     41   1.1   mycroft  *
     42   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     43   1.1   mycroft  * modification, are permitted provided that the following conditions
     44   1.1   mycroft  * are met:
     45   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     46   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     47   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     50  1.20       agc  * 3. Neither the name of the University nor the names of its contributors
     51   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     52   1.1   mycroft  *    without specific prior written permission.
     53   1.1   mycroft  *
     54   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.1   mycroft  * SUCH DAMAGE.
     65   1.1   mycroft  *
     66   1.1   mycroft  *	from: Id: lofs_subr.c, v 1.11 1992/05/30 10:05:43 jsp Exp
     67  1.13      fvdl  *	@(#)umap_subr.c	8.9 (Berkeley) 5/14/95
     68   1.1   mycroft  */
     69  1.18     lukem 
     70  1.18     lukem #include <sys/cdefs.h>
     71  1.25       dsl __KERNEL_RCSID(0, "$NetBSD: umap_subr.c,v 1.25 2007/06/30 15:27:03 dsl Exp $");
     72   1.1   mycroft 
     73   1.1   mycroft #include <sys/param.h>
     74   1.1   mycroft #include <sys/systm.h>
     75  1.13      fvdl #include <sys/proc.h>
     76   1.1   mycroft #include <sys/time.h>
     77   1.1   mycroft #include <sys/vnode.h>
     78   1.1   mycroft #include <sys/mount.h>
     79   1.1   mycroft #include <sys/namei.h>
     80   1.1   mycroft #include <sys/malloc.h>
     81  1.24      elad #include <sys/kauth.h>
     82  1.24      elad 
     83   1.4       cgd #include <miscfs/specfs/specdev.h>
     84   1.1   mycroft #include <miscfs/umapfs/umap.h>
     85   1.1   mycroft 
     86  1.22   xtraeme u_long umap_findid(u_long, u_long [][2], int);
     87  1.22   xtraeme int umap_node_alloc(struct mount *, struct vnode *,
     88  1.22   xtraeme 				struct vnode **);
     89   1.7  christos 
     90   1.1   mycroft /*
     91   1.1   mycroft  * umap_findid is called by various routines in umap_vnodeops.c to
     92   1.1   mycroft  * find a user or group id in a map.
     93   1.1   mycroft  */
     94  1.15  wrstuden u_long
     95   1.1   mycroft umap_findid(id, map, nentries)
     96   1.1   mycroft 	u_long id;
     97   1.1   mycroft 	u_long map[][2];
     98   1.1   mycroft 	int nentries;
     99   1.1   mycroft {
    100   1.1   mycroft 	int i;
    101   1.1   mycroft 
    102   1.1   mycroft 	/* Find uid entry in map */
    103   1.1   mycroft 	i = 0;
    104   1.1   mycroft 	while ((i<nentries) && ((map[i][0]) != id))
    105   1.1   mycroft 		i++;
    106   1.1   mycroft 
    107   1.1   mycroft 	if (i < nentries)
    108   1.1   mycroft 		return (map[i][1]);
    109   1.1   mycroft 	else
    110   1.1   mycroft 		return (-1);
    111   1.1   mycroft 
    112   1.1   mycroft }
    113   1.1   mycroft 
    114   1.1   mycroft /*
    115   1.1   mycroft  * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to
    116   1.1   mycroft  * find a user or group id in a map, in reverse.
    117   1.1   mycroft  */
    118   1.1   mycroft u_long
    119   1.1   mycroft umap_reverse_findid(id, map, nentries)
    120   1.1   mycroft 	u_long id;
    121   1.1   mycroft 	u_long map[][2];
    122   1.1   mycroft 	int nentries;
    123   1.1   mycroft {
    124   1.1   mycroft 	int i;
    125   1.1   mycroft 
    126   1.1   mycroft 	/* Find uid entry in map */
    127   1.1   mycroft 	i = 0;
    128   1.1   mycroft 	while ((i<nentries) && ((map[i][1]) != id))
    129   1.1   mycroft 		i++;
    130   1.1   mycroft 
    131   1.1   mycroft 	if (i < nentries)
    132   1.1   mycroft 		return (map[i][0]);
    133   1.1   mycroft 	else
    134   1.1   mycroft 		return (-1);
    135   1.1   mycroft 
    136   1.1   mycroft }
    137   1.1   mycroft 
    138   1.1   mycroft /* umap_mapids maps all of the ids in a credential, both user and group. */
    139   1.1   mycroft 
    140   1.1   mycroft void
    141   1.1   mycroft umap_mapids(v_mount, credp)
    142   1.1   mycroft 	struct mount *v_mount;
    143  1.24      elad 	kauth_cred_t credp;
    144   1.1   mycroft {
    145   1.1   mycroft 	int i, unentries, gnentries;
    146   1.5       cgd 	uid_t uid;
    147   1.5       cgd 	gid_t gid;
    148   1.7  christos 	u_long (*usermap)[2], (*groupmap)[2];
    149  1.24      elad 	gid_t groups[NGROUPS];
    150  1.24      elad 	uint16_t ngroups;
    151   1.8   thorpej 
    152   1.8   thorpej 	if (credp == NOCRED)
    153   1.8   thorpej 		return;
    154   1.1   mycroft 
    155   1.1   mycroft 	unentries =  MOUNTTOUMAPMOUNT(v_mount)->info_nentries;
    156   1.7  christos 	usermap =  MOUNTTOUMAPMOUNT(v_mount)->info_mapdata;
    157   1.1   mycroft 	gnentries =  MOUNTTOUMAPMOUNT(v_mount)->info_gnentries;
    158   1.7  christos 	groupmap =  MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata;
    159   1.1   mycroft 
    160   1.1   mycroft 	/* Find uid entry in map */
    161   1.1   mycroft 
    162  1.24      elad 	uid = (uid_t) umap_findid(kauth_cred_geteuid(credp), usermap, unentries);
    163   1.1   mycroft 
    164   1.1   mycroft 	if (uid != -1)
    165  1.24      elad 		kauth_cred_seteuid(credp, uid);
    166   1.1   mycroft 	else
    167  1.24      elad 		kauth_cred_seteuid(credp, (uid_t)NOBODY);
    168   1.1   mycroft 
    169   1.6       jtc #if 1
    170   1.6       jtc 	/* cr_gid is the same as cr_groups[0] in 4BSD, but not in NetBSD */
    171   1.1   mycroft 
    172   1.1   mycroft 	/* Find gid entry in map */
    173   1.1   mycroft 
    174  1.24      elad 	gid = (gid_t) umap_findid(kauth_cred_getegid(credp), groupmap, gnentries);
    175   1.1   mycroft 
    176   1.1   mycroft 	if (gid != -1)
    177  1.24      elad 		kauth_cred_setegid(credp, gid);
    178   1.1   mycroft 	else
    179  1.24      elad 		kauth_cred_setegid(credp, NULLGROUP);
    180   1.1   mycroft #endif
    181   1.1   mycroft 
    182  1.21     perry 	/* Now we must map each of the set of groups in the cr_groups
    183   1.1   mycroft 		structure. */
    184   1.1   mycroft 
    185  1.24      elad 	ngroups = kauth_cred_ngroups(credp);
    186  1.24      elad 	for (i = 0; i < ngroups; i++) {
    187  1.24      elad 		/* XXX elad: can't we just skip cases where gid == -1? */
    188  1.24      elad 		groups[i] = kauth_cred_group(credp, i);
    189  1.24      elad 		gid = (gid_t) umap_findid(groups[i],
    190   1.7  christos 					  groupmap, gnentries);
    191   1.1   mycroft 		if (gid != -1)
    192  1.24      elad 			groups[i] = gid;
    193   1.1   mycroft 		else
    194  1.24      elad 			groups[i] = NULLGROUP;
    195   1.1   mycroft 	}
    196  1.24      elad 
    197  1.25       dsl 	kauth_cred_setgroups(credp, groups, ngroups, -1, UIO_SYSSPACE);
    198   1.1   mycroft }
    199