#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/priv.h>
#include <sys/vnode.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/acl.h>
int
vaccess_acl_posix1e(__enum_uint8(vtype) type, uid_t file_uid, gid_t file_gid,
struct acl *acl, accmode_t accmode, struct ucred *cred)
{
struct acl_entry *acl_other, *acl_mask;
accmode_t dac_granted;
accmode_t priv_granted;
accmode_t acl_mask_granted;
int group_matched, i;
KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
("invalid bit in accmode"));
KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
("VAPPEND without VWRITE"));
priv_granted = 0;
if (type == VDIR) {
if ((accmode & VEXEC) && !priv_check_cred(cred, PRIV_VFS_LOOKUP))
priv_granted |= VEXEC;
} else {
if ((accmode & VEXEC) && (acl_posix1e_acl_to_mode(acl) &
(S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
!priv_check_cred(cred, PRIV_VFS_EXEC))
priv_granted |= VEXEC;
}
if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ))
priv_granted |= VREAD;
if (((accmode & VWRITE) || (accmode & VAPPEND)) &&
!priv_check_cred(cred, PRIV_VFS_WRITE))
priv_granted |= (VWRITE | VAPPEND);
if ((accmode & VADMIN) && !priv_check_cred(cred, PRIV_VFS_ADMIN))
priv_granted |= VADMIN;
acl_mask = acl_other = NULL;
for (i = 0; i < acl->acl_cnt; i++) {
switch (acl->acl_entry[i].ae_tag) {
case ACL_USER_OBJ:
if (file_uid != cred->cr_uid)
break;
dac_granted = 0;
dac_granted |= VADMIN;
if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
dac_granted |= VEXEC;
if (acl->acl_entry[i].ae_perm & ACL_READ)
dac_granted |= VREAD;
if (acl->acl_entry[i].ae_perm & ACL_WRITE)
dac_granted |= (VWRITE | VAPPEND);
if ((accmode & dac_granted) == accmode)
return (0);
if ((accmode & (dac_granted | priv_granted)) ==
accmode) {
return (0);
}
goto error;
case ACL_MASK:
acl_mask = &acl->acl_entry[i];
break;
case ACL_OTHER:
acl_other = &acl->acl_entry[i];
break;
default:
break;
}
}
if (acl_other == NULL) {
printf("vaccess_acl_posix1e: ACL_OTHER missing\n");
return (EPERM);
}
if (acl_mask != NULL) {
acl_mask_granted = 0;
if (acl_mask->ae_perm & ACL_EXECUTE)
acl_mask_granted |= VEXEC;
if (acl_mask->ae_perm & ACL_READ)
acl_mask_granted |= VREAD;
if (acl_mask->ae_perm & ACL_WRITE)
acl_mask_granted |= (VWRITE | VAPPEND);
} else
acl_mask_granted = VEXEC | VREAD | VWRITE | VAPPEND;
for (i = 0; i < acl->acl_cnt; i++) {
switch (acl->acl_entry[i].ae_tag) {
case ACL_USER:
if (acl->acl_entry[i].ae_id != cred->cr_uid)
break;
dac_granted = 0;
if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
dac_granted |= VEXEC;
if (acl->acl_entry[i].ae_perm & ACL_READ)
dac_granted |= VREAD;
if (acl->acl_entry[i].ae_perm & ACL_WRITE)
dac_granted |= (VWRITE | VAPPEND);
dac_granted &= acl_mask_granted;
if ((accmode & dac_granted) == accmode)
return (0);
if ((accmode & (dac_granted | priv_granted)) !=
accmode)
goto error;
return (0);
}
}
group_matched = 0;
for (i = 0; i < acl->acl_cnt; i++) {
switch (acl->acl_entry[i].ae_tag) {
case ACL_GROUP_OBJ:
if (!groupmember(file_gid, cred))
break;
dac_granted = 0;
if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
dac_granted |= VEXEC;
if (acl->acl_entry[i].ae_perm & ACL_READ)
dac_granted |= VREAD;
if (acl->acl_entry[i].ae_perm & ACL_WRITE)
dac_granted |= (VWRITE | VAPPEND);
dac_granted &= acl_mask_granted;
if ((accmode & dac_granted) == accmode)
return (0);
group_matched = 1;
break;
case ACL_GROUP:
if (!groupmember(acl->acl_entry[i].ae_id, cred))
break;
dac_granted = 0;
if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
dac_granted |= VEXEC;
if (acl->acl_entry[i].ae_perm & ACL_READ)
dac_granted |= VREAD;
if (acl->acl_entry[i].ae_perm & ACL_WRITE)
dac_granted |= (VWRITE | VAPPEND);
dac_granted &= acl_mask_granted;
if ((accmode & dac_granted) == accmode)
return (0);
group_matched = 1;
break;
default:
break;
}
}
if (group_matched == 1) {
for (i = 0; i < acl->acl_cnt; i++) {
switch (acl->acl_entry[i].ae_tag) {
case ACL_GROUP_OBJ:
if (!groupmember(file_gid, cred))
break;
dac_granted = 0;
if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
dac_granted |= VEXEC;
if (acl->acl_entry[i].ae_perm & ACL_READ)
dac_granted |= VREAD;
if (acl->acl_entry[i].ae_perm & ACL_WRITE)
dac_granted |= (VWRITE | VAPPEND);
dac_granted &= acl_mask_granted;
if ((accmode & (dac_granted | priv_granted))
!= accmode)
break;
return (0);
case ACL_GROUP:
if (!groupmember(acl->acl_entry[i].ae_id,
cred))
break;
dac_granted = 0;
if (acl->acl_entry[i].ae_perm & ACL_EXECUTE)
dac_granted |= VEXEC;
if (acl->acl_entry[i].ae_perm & ACL_READ)
dac_granted |= VREAD;
if (acl->acl_entry[i].ae_perm & ACL_WRITE)
dac_granted |= (VWRITE | VAPPEND);
dac_granted &= acl_mask_granted;
if ((accmode & (dac_granted | priv_granted))
!= accmode)
break;
return (0);
default:
break;
}
}
goto error;
}
dac_granted = 0;
if (acl_other->ae_perm & ACL_EXECUTE)
dac_granted |= VEXEC;
if (acl_other->ae_perm & ACL_READ)
dac_granted |= VREAD;
if (acl_other->ae_perm & ACL_WRITE)
dac_granted |= (VWRITE | VAPPEND);
if ((accmode & dac_granted) == accmode)
return (0);
if ((accmode & (dac_granted | priv_granted)) == accmode) {
return (0);
}
error:
return ((accmode & VADMIN) ? EPERM : EACCES);
}
acl_perm_t
acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
{
acl_perm_t perm = 0;
switch(tag) {
case ACL_USER_OBJ:
if (mode & S_IXUSR)
perm |= ACL_EXECUTE;
if (mode & S_IRUSR)
perm |= ACL_READ;
if (mode & S_IWUSR)
perm |= ACL_WRITE;
return (perm);
case ACL_GROUP_OBJ:
if (mode & S_IXGRP)
perm |= ACL_EXECUTE;
if (mode & S_IRGRP)
perm |= ACL_READ;
if (mode & S_IWGRP)
perm |= ACL_WRITE;
return (perm);
case ACL_OTHER:
if (mode & S_IXOTH)
perm |= ACL_EXECUTE;
if (mode & S_IROTH)
perm |= ACL_READ;
if (mode & S_IWOTH)
perm |= ACL_WRITE;
return (perm);
default:
printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag);
return (0);
}
}
struct acl_entry
acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
{
struct acl_entry acl_entry;
acl_entry.ae_tag = tag;
acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
acl_entry.ae_entry_type = 0;
acl_entry.ae_flags = 0;
switch(tag) {
case ACL_USER_OBJ:
acl_entry.ae_id = uid;
break;
case ACL_GROUP_OBJ:
acl_entry.ae_id = gid;
break;
case ACL_OTHER:
acl_entry.ae_id = ACL_UNDEFINED_ID;
break;
default:
acl_entry.ae_id = ACL_UNDEFINED_ID;
printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
}
return (acl_entry);
}
mode_t
acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
{
mode_t mode;
mode = 0;
if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
mode |= S_IXUSR;
if (acl_user_obj_entry->ae_perm & ACL_READ)
mode |= S_IRUSR;
if (acl_user_obj_entry->ae_perm & ACL_WRITE)
mode |= S_IWUSR;
if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
mode |= S_IXGRP;
if (acl_group_obj_entry->ae_perm & ACL_READ)
mode |= S_IRGRP;
if (acl_group_obj_entry->ae_perm & ACL_WRITE)
mode |= S_IWGRP;
if (acl_other_entry->ae_perm & ACL_EXECUTE)
mode |= S_IXOTH;
if (acl_other_entry->ae_perm & ACL_READ)
mode |= S_IROTH;
if (acl_other_entry->ae_perm & ACL_WRITE)
mode |= S_IWOTH;
return (mode);
}
mode_t
acl_posix1e_acl_to_mode(struct acl *acl)
{
struct acl_entry *acl_mask, *acl_user_obj, *acl_group_obj, *acl_other;
int i;
acl_user_obj = acl_group_obj = acl_other = acl_mask = NULL;
for (i = 0; i < acl->acl_cnt; i++) {
switch (acl->acl_entry[i].ae_tag) {
case ACL_USER_OBJ:
acl_user_obj = &acl->acl_entry[i];
break;
case ACL_GROUP_OBJ:
acl_group_obj = &acl->acl_entry[i];
break;
case ACL_OTHER:
acl_other = &acl->acl_entry[i];
break;
case ACL_MASK:
acl_mask = &acl->acl_entry[i];
break;
case ACL_USER:
case ACL_GROUP:
break;
default:
panic("acl_posix1e_acl_to_mode: bad ae_tag");
}
}
if (acl_user_obj == NULL || acl_group_obj == NULL || acl_other == NULL)
panic("acl_posix1e_acl_to_mode: missing base ae_tags");
if (acl_mask != NULL)
return (acl_posix1e_perms_to_mode(acl_user_obj, acl_mask,
acl_other));
else
return (acl_posix1e_perms_to_mode(acl_user_obj, acl_group_obj,
acl_other));
}
int
acl_posix1e_check(struct acl *acl)
{
int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
int num_acl_mask, num_acl_other, i;
num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
num_acl_mask = num_acl_other = 0;
if (acl->acl_cnt > ACL_MAX_ENTRIES)
return (EINVAL);
for (i = 0; i < acl->acl_cnt; i++) {
switch(acl->acl_entry[i].ae_tag) {
case ACL_USER_OBJ:
acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_user_obj++;
break;
case ACL_GROUP_OBJ:
acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_group_obj++;
break;
case ACL_USER:
if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_user++;
break;
case ACL_GROUP:
if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_group++;
break;
case ACL_OTHER:
acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_other++;
break;
case ACL_MASK:
acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID;
if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_mask++;
break;
default:
return (EINVAL);
}
if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
ACL_PERM_BITS)
return (EINVAL);
}
if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
(num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
return (EINVAL);
if (((num_acl_group != 0) || (num_acl_user != 0)) &&
(num_acl_mask != 1))
return (EINVAL);
return (0);
}
mode_t
acl_posix1e_newfilemode(mode_t cmode, struct acl *dacl)
{
mode_t mode;
mode = cmode;
mode &= ACL_PRESERVE_MASK;
mode |= (ACL_OVERRIDE_MASK & cmode & acl_posix1e_acl_to_mode(dacl));
return (mode);
}
static int
acl_posix1e_modload(module_t mod, int what, void *arg)
{
int ret;
ret = 0;
switch (what) {
case MOD_LOAD:
case MOD_SHUTDOWN:
break;
case MOD_QUIESCE:
ret = 0;
break;
case MOD_UNLOAD:
ret = 0;
break;
default:
ret = EINVAL;
break;
}
return (ret);
}
static moduledata_t acl_posix1e_mod = {
"acl_posix1e",
acl_posix1e_modload,
NULL
};
DECLARE_MODULE(acl_posix1e, acl_posix1e_mod, SI_SUB_VFS, SI_ORDER_FIRST);
MODULE_VERSION(acl_posix1e, 1);