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