#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/acl.h>
#include <aclutils.h>
struct entry {
int count;
uid_t *id;
};
struct entry_stat {
struct entry user_obj;
struct entry user;
struct entry group_obj;
struct entry group;
struct entry other_obj;
struct entry class_obj;
struct entry def_user_obj;
struct entry def_user;
struct entry def_group_obj;
struct entry def_group;
struct entry def_other_obj;
struct entry def_class_obj;
};
static void free_mem(struct entry_stat *);
static int check_dup(int, uid_t *, uid_t, struct entry_stat *);
static int
aclent_aclcheck(aclent_t *aclbufp, int nentries, int *which, int isdir)
{
struct entry_stat tally;
aclent_t *aclentp;
uid_t **idp;
int cnt;
*which = -1;
memset(&tally, '\0', sizeof (tally));
for (aclentp = aclbufp; nentries > 0; nentries--, aclentp++) {
switch (aclentp->a_type) {
case USER_OBJ:
if (tally.user_obj.count > 0) {
*which = (int)(aclentp - aclbufp);
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_USER_ERROR);
}
tally.user_obj.count = 1;
break;
case GROUP_OBJ:
if (tally.group_obj.count > 0) {
*which = (int)(aclentp - aclbufp);
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_GRP_ERROR);
}
tally.group_obj.count = 1;
break;
case OTHER_OBJ:
if (tally.other_obj.count > 0) {
*which = (int)(aclentp - aclbufp);
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_OTHER_ERROR);
}
tally.other_obj.count = 1;
break;
case CLASS_OBJ:
if (tally.class_obj.count > 0) {
*which = (int)(aclentp - aclbufp);
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_CLASS_ERROR);
}
tally.class_obj.count = 1;
break;
case USER:
case GROUP:
case DEF_USER:
case DEF_GROUP:
if (aclentp->a_type == DEF_USER) {
cnt = (tally.def_user.count)++;
idp = &(tally.def_user.id);
} else if (aclentp->a_type == DEF_GROUP) {
cnt = (tally.def_group.count)++;
idp = &(tally.def_group.id);
} else if (aclentp->a_type == USER) {
cnt = (tally.user.count)++;
idp = &(tally.user.id);
} else {
cnt = (tally.group.count)++;
idp = &(tally.group.id);
}
if (cnt == 0) {
*idp = calloc(nentries, sizeof (uid_t));
if (*idp == NULL)
return (EACL_MEM_ERROR);
} else {
if (check_dup(cnt, *idp, aclentp->a_id,
&tally) == -1) {
*which = (int)(aclentp - aclbufp);
return (EACL_DUPLICATE_ERROR);
}
}
(*idp)[cnt] = aclentp->a_id;
break;
case DEF_USER_OBJ:
if (tally.def_user_obj.count > 0) {
*which = (int)(aclentp - aclbufp);
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_USER_ERROR);
}
tally.def_user_obj.count = 1;
break;
case DEF_GROUP_OBJ:
if (tally.def_group_obj.count > 0) {
*which = (int)(aclentp - aclbufp);
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_GRP_ERROR);
}
tally.def_group_obj.count = 1;
break;
case DEF_OTHER_OBJ:
if (tally.def_other_obj.count > 0) {
*which = (int)(aclentp - aclbufp);
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_OTHER_ERROR);
}
tally.def_other_obj.count = 1;
break;
case DEF_CLASS_OBJ:
if (tally.def_class_obj.count > 0) {
*which = (int)(aclentp - aclbufp);
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_CLASS_ERROR);
}
tally.def_class_obj.count = 1;
break;
default:
(void) free_mem(&tally);
errno = EINVAL;
*which = (int)(aclentp - aclbufp);
return (EACL_ENTRY_ERROR);
}
}
if (tally.user.count > 0 || tally.group.count > 0)
if (tally.class_obj.count != 1) {
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_MISS_ERROR);
}
if (tally.def_user.count > 0 || tally.def_group.count > 0)
if (tally.def_class_obj.count != 1) {
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_MISS_ERROR);
}
if (tally.user_obj.count != 1 ||
tally.group_obj.count != 1 ||
tally.other_obj.count != 1) {
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_MISS_ERROR);
}
if (tally.def_user.count > 0 || tally.def_user_obj.count > 0 ||
tally.def_group.count > 0 || tally.def_group_obj.count > 0 ||
tally.def_class_obj.count > 0 || tally.def_other_obj.count > 0) {
if (isdir == 0) {
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_INHERIT_NOTDIR);
}
if (tally.def_user_obj.count != 1 ||
tally.def_group_obj.count != 1 ||
tally.def_other_obj.count != 1) {
(void) free_mem(&tally);
errno = EINVAL;
return (EACL_MISS_ERROR);
}
}
(void) free_mem(&tally);
return (0);
}
int
aclcheck(aclent_t *aclbufp, int nentries, int *which)
{
return (aclent_aclcheck(aclbufp, nentries, which, 1));
}
static void
free_mem(struct entry_stat *tallyp)
{
if ((tallyp->user).count > 0)
free((tallyp->user).id);
if ((tallyp->group).count > 0)
free((tallyp->group).id);
if ((tallyp->def_user).count > 0)
free((tallyp->def_user).id);
if ((tallyp->def_group).count > 0)
free((tallyp->def_group).id);
}
static int
check_dup(int count, uid_t *ids, uid_t newid, struct entry_stat *tallyp)
{
int i;
for (i = 0; i < count; i++) {
if (ids[i] == newid) {
errno = EINVAL;
(void) free_mem(tallyp);
return (-1);
}
}
return (0);
}
#define IFLAGS (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE| \
ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE)
static int
ace_aclcheck(acl_t *aclp, int isdir)
{
ace_t *acep;
int i;
int error = 0;
if (aclp->acl_cnt <= 0 || aclp->acl_cnt > MAX_ACL_ENTRIES)
return (EACL_COUNT_ERROR);
for (i = 0, acep = aclp->acl_aclp;
i != aclp->acl_cnt && error == 0; i++, acep++) {
switch (acep->a_flags & 0xf040) {
case 0:
case ACE_OWNER:
case ACE_EVERYONE:
case ACE_IDENTIFIER_GROUP:
case ACE_GROUP|ACE_IDENTIFIER_GROUP:
break;
default:
errno = EINVAL;
return (EACL_FLAGS_ERROR);
}
if (acep->a_flags &
(ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE)) {
if ((acep->a_flags & (ACE_FILE_INHERIT_ACE|
ACE_DIRECTORY_INHERIT_ACE)) == 0) {
errno = EINVAL;
return (EACL_INHERIT_ERROR);
}
break;
}
switch (acep->a_type) {
case ACE_ACCESS_ALLOWED_ACE_TYPE:
case ACE_ACCESS_DENIED_ACE_TYPE:
case ACE_SYSTEM_AUDIT_ACE_TYPE:
case ACE_SYSTEM_ALARM_ACE_TYPE:
break;
default:
errno = EINVAL;
return (EACL_ENTRY_ERROR);
}
if (acep->a_access_mask > ACE_ALL_PERMS) {
errno = EINVAL;
return (EACL_PERM_MASK_ERROR);
}
}
return (0);
}
int
acl_check(acl_t *aclp, int flag)
{
int error;
int where;
switch (aclp->acl_type) {
case ACLENT_T:
error = aclent_aclcheck(aclp->acl_aclp, aclp->acl_cnt,
&where, flag);
break;
case ACE_T:
error = ace_aclcheck(aclp, flag);
break;
default:
errno = EINVAL;
error = EACL_ENTRY_ERROR;
}
return (error);
}