Home | History | Annotate | Line # | Download | only in libpuffs
creds.c revision 1.12
      1 /*	$NetBSD: creds.c,v 1.12 2007/07/01 17:22:18 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the Ulla Tuominen Foundation.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 #if !defined(lint)
     32 __RCSID("$NetBSD: creds.c,v 1.12 2007/07/01 17:22:18 pooka Exp $");
     33 #endif /* !lint */
     34 
     35 /*
     36  * Interface for dealing with credits.
     37  */
     38 
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 
     42 #include <errno.h>
     43 #include <puffs.h>
     44 #include <string.h>
     45 
     46 #include "puffs_priv.h"
     47 
     48 #define UUCCRED(a) (a->pkcr_type == PUFFCRED_TYPE_UUC)
     49 #define INTCRED(a) (a->pkcr_type == PUFFCRED_TYPE_INTERNAL)
     50 
     51 int
     52 puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *ruid)
     53 {
     54 	PUFFS_MAKEKCRED(pkcr, pcr);
     55 
     56 	if (!UUCCRED(pkcr)) {
     57 		errno = EOPNOTSUPP;
     58 		return -1;
     59 	}
     60 	*ruid = pkcr->pkcr_uuc.cr_uid;
     61 
     62 	return 0;
     63 }
     64 
     65 int
     66 puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *rgid)
     67 {
     68 	PUFFS_MAKEKCRED(pkcr, pcr);
     69 
     70 	if (!UUCCRED(pkcr)) {
     71 		errno = EOPNOTSUPP;
     72 		return -1;
     73 	}
     74 	*rgid = pkcr->pkcr_uuc.cr_gid;
     75 
     76 	return 0;
     77 }
     78 
     79 int
     80 puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *rgids, short *ngids)
     81 {
     82 	PUFFS_MAKEKCRED(pkcr, pcr);
     83 	size_t ncopy;
     84 
     85 	if (!UUCCRED(pkcr)) {
     86 		errno = EOPNOTSUPP;
     87 		return -1;
     88 	}
     89 
     90 	ncopy = MIN(*ngids, NGROUPS);
     91 	(void)memcpy(rgids, pkcr->pkcr_uuc.cr_groups, sizeof(gid_t) * ncopy);
     92 	*ngids = (short)ncopy;
     93 
     94 	return 0;
     95 }
     96 
     97 int
     98 puffs_cred_isuid(const struct puffs_cred *pcr, uid_t uid)
     99 {
    100 	PUFFS_MAKEKCRED(pkcr, pcr);
    101 
    102 	return UUCCRED(pkcr) && pkcr->pkcr_uuc.cr_uid == uid;
    103 }
    104 
    105 int
    106 puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t gid)
    107 {
    108 	PUFFS_MAKEKCRED(pkcr, pcr);
    109 	short i;
    110 
    111 	if (!UUCCRED(pkcr))
    112 		return 0;
    113 
    114 	if (pkcr->pkcr_uuc.cr_gid == gid)
    115 		return 1;
    116 	for (i = 0; i < pkcr->pkcr_uuc.cr_ngroups; i++)
    117 		if (pkcr->pkcr_uuc.cr_groups[i] == gid)
    118 			return 1;
    119 
    120 	return 0;
    121 }
    122 
    123 int
    124 puffs_cred_isregular(const struct puffs_cred *pcr)
    125 {
    126 	PUFFS_MAKEKCRED(pkcr, pcr);
    127 
    128 	return UUCCRED(pkcr);
    129 }
    130 
    131 int
    132 puffs_cred_iskernel(const struct puffs_cred *pcr)
    133 {
    134 	PUFFS_MAKEKCRED(pkcr, pcr);
    135 
    136 	return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_NOCRED;
    137 }
    138 
    139 int
    140 puffs_cred_isfs(const struct puffs_cred *pcr)
    141 {
    142 	PUFFS_MAKEKCRED(pkcr, pcr);
    143 
    144 	return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_FSCRED;
    145 }
    146 
    147 int
    148 puffs_cred_isjuggernaut(const struct puffs_cred *pcr)
    149 {
    150 
    151 	return puffs_cred_isuid(pcr, 0) || puffs_cred_iskernel(pcr)
    152 	    || puffs_cred_isfs(pcr);
    153 }
    154 
    155 int
    156 puffs_cid_getpid(const struct puffs_cid *pcid, pid_t *pid)
    157 {
    158 	PUFFS_MAKEKCID(pkcid, pcid);
    159 
    160 	if (pkcid->pkcid_type == PUFFCID_TYPE_REAL) {
    161 		*pid = pkcid->pkcid_pid;
    162 		return 0;
    163 	} else
    164 		return ESRCH;
    165 }
    166 
    167 int
    168 puffs_cid_getlwpid(const struct puffs_cid *pcid, lwpid_t *lid)
    169 {
    170 	PUFFS_MAKEKCID(pkcid, pcid);
    171 
    172 	if (pkcid->pkcid_type == PUFFCID_TYPE_REAL) {
    173 		*lid = pkcid->pkcid_lwpid;
    174 		return 0;
    175 	} else
    176 		return ESRCH;
    177 }
    178 
    179 /*
    180  * Gerneic routine for checking file access rights.  Modeled after
    181  * vaccess() in the kernel.
    182  */
    183 int
    184 puffs_access(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
    185 	mode_t acc_mode, const struct puffs_cred *pcr)
    186 {
    187 	mode_t mask;
    188 
    189 	/* megapower */
    190 	if (puffs_cred_iskernel(pcr) || puffs_cred_isfs(pcr))
    191 		return 0;
    192 
    193 	/* superuser, allow all except exec if *ALL* exec bits are unset */
    194 	if (puffs_cred_isuid(pcr, 0)) {
    195 		if ((acc_mode & PUFFS_VEXEC) && type != VDIR &&
    196 		    (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
    197 			return EACCES;
    198 		return 0;
    199 	}
    200 
    201 	mask = 0;
    202 	/* owner */
    203 	if (puffs_cred_isuid(pcr, uid)) {
    204 		if (acc_mode & PUFFS_VEXEC)
    205 			mask |= S_IXUSR;
    206 		if (acc_mode & PUFFS_VREAD)
    207 			mask |= S_IRUSR;
    208 		if (acc_mode & PUFFS_VWRITE)
    209 			mask |= S_IWUSR;
    210 	/* group */
    211 	} else if (puffs_cred_hasgroup(pcr, gid)) {
    212 		if (acc_mode & PUFFS_VEXEC)
    213 			mask |= S_IXGRP;
    214 		if (acc_mode & PUFFS_VREAD)
    215 			mask |= S_IRGRP;
    216 		if (acc_mode & PUFFS_VWRITE)
    217 			mask |= S_IWGRP;
    218 	/* other */
    219 	} else {
    220 		if (acc_mode & PUFFS_VEXEC)
    221 			mask |= S_IXOTH;
    222 		if (acc_mode & PUFFS_VREAD)
    223 			mask |= S_IROTH;
    224 		if (acc_mode & PUFFS_VWRITE)
    225 			mask |= S_IWOTH;
    226 	}
    227 
    228 	if ((file_mode & mask) == mask)
    229 		return 0;
    230 	else
    231 		return EACCES;
    232 }
    233 
    234 int
    235 puffs_access_chown(uid_t owner, gid_t group, uid_t newowner, gid_t newgroup,
    236 	const struct puffs_cred *pcr)
    237 {
    238 
    239 	if (newowner == (uid_t)PUFFS_VNOVAL)
    240 		newowner = owner;
    241 	if (newgroup == (gid_t)PUFFS_VNOVAL)
    242 		newgroup = group;
    243 
    244 	if ((!puffs_cred_isuid(pcr, owner) || newowner != owner ||
    245 	    ((newgroup != group && !puffs_cred_hasgroup(pcr, newgroup))))
    246 	    && !puffs_cred_isjuggernaut(pcr))
    247 		return EPERM;
    248 
    249 	return 0;
    250 }
    251 
    252 int
    253 puffs_access_chmod(uid_t owner, gid_t group, enum vtype type, mode_t mode,
    254 	const struct puffs_cred *pcr)
    255 {
    256 
    257 	if (!puffs_cred_isuid(pcr, owner) && !puffs_cred_isjuggernaut(pcr))
    258 		return EPERM;
    259 
    260 	if (!puffs_cred_isjuggernaut(pcr)) {
    261 		if (type != VDIR && (mode & S_ISTXT))
    262 			return EFTYPE;
    263 		if (!puffs_cred_hasgroup(pcr, group) && (mode & S_ISGID))
    264 			return EPERM;
    265 	}
    266 
    267 	return 0;
    268 }
    269 
    270 int
    271 puffs_access_times(uid_t uid, gid_t gid, mode_t mode, int va_utimes_null,
    272 	const struct puffs_cred *pcr)
    273 {
    274 
    275 	if (!puffs_cred_isuid(pcr, uid) && !puffs_cred_isjuggernaut(pcr)
    276 	    && (va_utimes_null == 0
    277 	      || puffs_access(VNON, mode, uid, gid, PUFFS_VWRITE, pcr) != 0))
    278 		return EPERM;
    279 
    280 	return 0;
    281 }
    282