2 * Copyright (c) 1999 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/lib/libposix1e/acl_from_text.c,v 1.1 2000/01/15 19:44:24 rwatson Exp $
29 * acl_from_text: convert a text-form ACL from a string to an acl_t
32 #include <sys/types.h>
34 #include <sys/errno.h>
39 #include "acl_support.h"
42 PM_BASE, /* initial, begin line, or after , */
43 PM_QUALIFIER, /* in qualifier field */
44 PM_PERM, /* in permission field */
45 PM_COMMENT, /* in comment */
49 string_skip_whitespace(char *string)
52 while (*string && ((*string == ' ') || (*string == '\t'))) {
59 string_trim_trailing_whitespace(char *string)
66 end = string + strlen(string) - 1;
68 while (end != string) {
69 if ((*end == ' ') || (*end == '\t')) {
81 acl_string_to_tag(char *tag, char *qualifier)
84 if (*qualifier == '\0') {
85 if ((!strcmp(tag, "user")) || (!strcmp(tag, "u"))) {
86 return (ACL_USER_OBJ);
88 if ((!strcmp(tag, "group")) || (!strcmp(tag, "g"))) {
89 return (ACL_GROUP_OBJ);
91 if ((!strcmp(tag, "mask")) || (!strcmp(tag, "m"))) {
94 if ((!strcmp(tag, "other")) || (!strcmp(tag, "o"))) {
99 if ((!strcmp(tag, "user")) || (!strcmp(tag, "u"))) {
102 if ((!strcmp(tag, "group")) || (!strcmp(tag, "g"))) {
110 * acl_from_text -- convert a string into an ACL
111 * postpone most validity checking until the end and cal acl_valid to do
115 acl_from_text(const char *buf_p)
121 char *mybuf_p, *line, *cur, *notcomment, *comment, *entry;
122 char *tag, *qualifier, *permission;
125 /* local copy we can mess up */
126 mybuf_p = strdup(buf_p);
139 /* outer loop: delimit at \n boundaries */
141 while ((line = strsep(&cur, "\n"))) {
142 /* now split the line on the first # to strip out comments */
144 notcomment = strsep(&comment, "#");
146 /* inner loop: delimit at , boundaries */
147 while ((entry = strsep(¬comment, ","))) {
148 /* now split into three :-delimited fields */
149 tag = strsep(&entry, ":");
151 /* printf("no tag\n"); */
155 tag = string_skip_whitespace(tag);
156 if ((*tag == '\0') && (!entry)) {
158 * is an entirely comment line, skip to next
163 string_trim_trailing_whitespace(tag);
165 qualifier = strsep(&entry, ":");
167 /* printf("no qualifier\n"); */
171 qualifier = string_skip_whitespace(qualifier);
172 string_trim_trailing_whitespace(qualifier);
174 permission = strsep(&entry, ":");
175 if ((!permission) || (entry)) {
176 /* printf("no permission, or more stuff\n"); */
180 permission = string_skip_whitespace(permission);
181 string_trim_trailing_whitespace(permission);
183 /* printf("[%s/%s/%s]\n", tag, qualifier,
186 t = acl_string_to_tag(tag, qualifier);
192 error = acl_string_to_perm(permission, &p);
203 if (*qualifier != '\0') {
212 error = acl_name_to_id(t, qualifier, &id);
222 error = acl_add_entry(acl, t, id, p);
229 /* XXX should we only return ACLs valid according to acl_valid? */
230 /* verify validity of the ACL we read in */
231 if (acl_valid(acl) == -1) {