#include <sys/types.h>
#include <sys/acl.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "acl_support.h"
enum PARSE_MODE {
PM_BASE,
PM_QUALIFIER,
PM_PERM,
PM_COMMENT,
};
static char *
string_skip_whitespace(char *string)
{
while (*string && ((*string == ' ') || (*string == '\t'))) {
string++;
}
return (string);
}
static void
string_trim_trailing_whitespace(char *string)
{
char *end;
if (*string == '\0')
return;
end = string + strlen(string) - 1;
while (end != string) {
if ((*end == ' ') || (*end == '\t')) {
*end = '\0';
end--;
} else {
return;
}
}
return;
}
acl_tag_t
acl_string_to_tag(char *tag, char *qualifier)
{
if (*qualifier == '\0') {
if ((!strcmp(tag, "user")) || (!strcmp(tag, "u"))) {
return (ACL_USER_OBJ);
} else
if ((!strcmp(tag, "group")) || (!strcmp(tag, "g"))) {
return (ACL_GROUP_OBJ);
} else
if ((!strcmp(tag, "mask")) || (!strcmp(tag, "m"))) {
return (ACL_MASK);
} else
if ((!strcmp(tag, "other")) || (!strcmp(tag, "o"))) {
return (ACL_OTHER);
} else
return(-1);
} else {
if ((!strcmp(tag, "user")) || (!strcmp(tag, "u"))) {
return(ACL_USER);
} else
if ((!strcmp(tag, "group")) || (!strcmp(tag, "g"))) {
return(ACL_GROUP);
} else
return(-1);
}
}
acl_t
acl_from_text(const char *buf_p)
{
acl_tag_t t;
acl_perm_t p;
acl_t acl;
uid_t id;
char *mybuf_p, *line, *cur, *notcomment, *comment, *entry;
char *tag, *qualifier, *permission;
int error;
mybuf_p = strdup(buf_p);
if (!mybuf_p) {
errno = ENOMEM;
return(0);
}
acl = acl_init(3);
if (!acl) {
free(mybuf_p);
errno = ENOMEM;
return(0);
}
cur = mybuf_p;
while ((line = strsep(&cur, "\n"))) {
comment = line;
notcomment = strsep(&comment, "#");
while ((entry = strsep(¬comment, ","))) {
tag = strsep(&entry, ":");
if (!tag) {
errno = EINVAL;
goto error_label;
}
tag = string_skip_whitespace(tag);
if ((*tag == '\0') && (!entry)) {
continue;
}
string_trim_trailing_whitespace(tag);
qualifier = strsep(&entry, ":");
if (!qualifier) {
errno = EINVAL;
goto error_label;
}
qualifier = string_skip_whitespace(qualifier);
string_trim_trailing_whitespace(qualifier);
permission = strsep(&entry, ":");
if ((!permission) || (entry)) {
errno = EINVAL;
goto error_label;
}
permission = string_skip_whitespace(permission);
string_trim_trailing_whitespace(permission);
t = acl_string_to_tag(tag, qualifier);
if (t == -1) {
errno = EINVAL;
goto error_label;
}
error = acl_string_to_perm(permission, &p);
if (error == -1) {
errno = EINVAL;
goto error_label;
}
switch(t) {
case ACL_USER_OBJ:
case ACL_GROUP_OBJ:
case ACL_MASK:
case ACL_OTHER:
if (*qualifier != '\0') {
errno = EINVAL;
goto error_label;
}
id = 0;
break;
case ACL_USER:
case ACL_GROUP:
error = acl_name_to_id(t, qualifier, &id);
if (error == -1)
goto error_label;
break;
default:
errno = EINVAL;
goto error_label;
}
error = acl_add_entry(acl, t, id, p);
if (error == -1)
goto error_label;
}
}
#if 0
if (acl_valid(acl) == -1) {
errno = EINVAL;
goto error_label;
}
#endif
return(acl);
error_label:
acl_free(acl);
free(mybuf_p);
return(0);
}