Home | History | Annotate | Line # | Download | only in libpuffs
creds.c revision 1.11
      1 /*	$NetBSD: creds.c,v 1.11 2007/07/01 15:30:15 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.11 2007/07/01 15:30:15 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 /*
    156  * Gerneic routine for checking file access rights.  Modeled after
    157  * vaccess() in the kernel.
    158  */
    159 int
    160 puffs_access(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
    161 	mode_t acc_mode, const struct puffs_cred *pcr)
    162 {
    163 	mode_t mask;
    164 
    165 	/* megapower */
    166 	if (puffs_cred_iskernel(pcr) || puffs_cred_isfs(pcr))
    167 		return 0;
    168 
    169 	/* superuser, allow all except exec if *ALL* exec bits are unset */
    170 	if (puffs_cred_isuid(pcr, 0)) {
    171 		if ((acc_mode & PUFFS_VEXEC) && type != VDIR &&
    172 		    (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
    173 			return EACCES;
    174 		return 0;
    175 	}
    176 
    177 	mask = 0;
    178 	/* owner */
    179 	if (puffs_cred_isuid(pcr, uid)) {
    180 		if (acc_mode & PUFFS_VEXEC)
    181 			mask |= S_IXUSR;
    182 		if (acc_mode & PUFFS_VREAD)
    183 			mask |= S_IRUSR;
    184 		if (acc_mode & PUFFS_VWRITE)
    185 			mask |= S_IWUSR;
    186 	/* group */
    187 	} else if (puffs_cred_hasgroup(pcr, gid)) {
    188 		if (acc_mode & PUFFS_VEXEC)
    189 			mask |= S_IXGRP;
    190 		if (acc_mode & PUFFS_VREAD)
    191 			mask |= S_IRGRP;
    192 		if (acc_mode & PUFFS_VWRITE)
    193 			mask |= S_IWGRP;
    194 	/* other */
    195 	} else {
    196 		if (acc_mode & PUFFS_VEXEC)
    197 			mask |= S_IXOTH;
    198 		if (acc_mode & PUFFS_VREAD)
    199 			mask |= S_IROTH;
    200 		if (acc_mode & PUFFS_VWRITE)
    201 			mask |= S_IWOTH;
    202 	}
    203 
    204 	if ((file_mode & mask) == mask)
    205 		return 0;
    206 	else
    207 		return EACCES;
    208 }
    209 
    210 int
    211 puffs_access_chown(uid_t owner, gid_t group, uid_t newowner, gid_t newgroup,
    212 	const struct puffs_cred *pcr)
    213 {
    214 
    215 	if (newowner == (uid_t)PUFFS_VNOVAL)
    216 		newowner = owner;
    217 	if (newgroup == (gid_t)PUFFS_VNOVAL)
    218 		newgroup = group;
    219 
    220 	if ((!puffs_cred_isuid(pcr, owner) || newowner != owner ||
    221 	    ((newgroup != group && !puffs_cred_hasgroup(pcr, newgroup))))
    222 	    && !puffs_cred_isjuggernaut(pcr))
    223 		return EPERM;
    224 
    225 	return 0;
    226 }
    227 
    228 int
    229 puffs_access_chmod(uid_t owner, gid_t group, enum vtype type, mode_t mode,
    230 	const struct puffs_cred *pcr)
    231 {
    232 
    233 	if (!puffs_cred_isuid(pcr, owner) && !puffs_cred_isjuggernaut(pcr))
    234 		return EPERM;
    235 
    236 	if (!puffs_cred_isjuggernaut(pcr)) {
    237 		if (type != VDIR && (mode & S_ISTXT))
    238 			return EFTYPE;
    239 		if (!puffs_cred_hasgroup(pcr, group) && (mode & S_ISGID))
    240 			return EPERM;
    241 	}
    242 
    243 	return 0;
    244 }
    245 
    246 int
    247 puffs_access_times(uid_t uid, gid_t gid, mode_t mode, int va_utimes_null,
    248 	const struct puffs_cred *pcr)
    249 {
    250 
    251 	if (!puffs_cred_isuid(pcr, uid) && !puffs_cred_isjuggernaut(pcr)
    252 	    && (va_utimes_null == 0
    253 	      || puffs_access(VNON, mode, uid, gid, PUFFS_VWRITE, pcr) != 0))
    254 		return EPERM;
    255 
    256 	return 0;
    257 }
    258