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 $
27 *$DragonFly: src/lib/libposix1e/acl_from_text.c,v 1.3 2005/08/04 17:27:09 drhodus Exp $
30 * acl_from_text: convert a text-form ACL from a string to an acl_t
33 #include <sys/types.h>
35 #include <sys/errno.h>
40 #include "acl_support.h"
43 PM_BASE
, /* initial, begin line, or after , */
44 PM_QUALIFIER
, /* in qualifier field */
45 PM_PERM
, /* in permission field */
46 PM_COMMENT
, /* in comment */
50 string_skip_whitespace(char *string
)
53 while (*string
&& ((*string
== ' ') || (*string
== '\t'))) {
60 string_trim_trailing_whitespace(char *string
)
67 end
= string
+ strlen(string
) - 1;
69 while (end
!= string
) {
70 if ((*end
== ' ') || (*end
== '\t')) {
82 acl_string_to_tag(char *tag
, char *qualifier
)
85 if (*qualifier
== '\0') {
86 if ((!strcmp(tag
, "user")) || (!strcmp(tag
, "u"))) {
87 return (ACL_USER_OBJ
);
89 if ((!strcmp(tag
, "group")) || (!strcmp(tag
, "g"))) {
90 return (ACL_GROUP_OBJ
);
92 if ((!strcmp(tag
, "mask")) || (!strcmp(tag
, "m"))) {
95 if ((!strcmp(tag
, "other")) || (!strcmp(tag
, "o"))) {
100 if ((!strcmp(tag
, "user")) || (!strcmp(tag
, "u"))) {
103 if ((!strcmp(tag
, "group")) || (!strcmp(tag
, "g"))) {
111 * acl_from_text -- convert a string into an ACL
112 * postpone most validity checking until the end and cal acl_valid to do
116 acl_from_text(const char *buf_p
)
122 char *mybuf_p
, *line
, *cur
, *notcomment
, *comment
, *entry
;
123 char *tag
, *qualifier
, *permission
;
126 /* local copy we can mess up */
127 mybuf_p
= strdup(buf_p
);
140 /* outer loop: delimit at \n boundaries */
142 while ((line
= strsep(&cur
, "\n"))) {
143 /* now split the line on the first # to strip out comments */
145 notcomment
= strsep(&comment
, "#");
147 /* inner loop: delimit at , boundaries */
148 while ((entry
= strsep(¬comment
, ","))) {
149 /* now split into three :-delimited fields */
150 tag
= strsep(&entry
, ":");
152 /* printf("no tag\n"); */
156 tag
= string_skip_whitespace(tag
);
157 if ((*tag
== '\0') && (!entry
)) {
159 * is an entirely comment line, skip to next
164 string_trim_trailing_whitespace(tag
);
166 qualifier
= strsep(&entry
, ":");
168 /* printf("no qualifier\n"); */
172 qualifier
= string_skip_whitespace(qualifier
);
173 string_trim_trailing_whitespace(qualifier
);
175 permission
= strsep(&entry
, ":");
176 if ((!permission
) || (entry
)) {
177 /* printf("no permission, or more stuff\n"); */
181 permission
= string_skip_whitespace(permission
);
182 string_trim_trailing_whitespace(permission
);
184 /* printf("[%s/%s/%s]\n", tag, qualifier,
187 t
= acl_string_to_tag(tag
, qualifier
);
193 error
= acl_string_to_perm(permission
, &p
);
204 if (*qualifier
!= '\0') {
213 error
= acl_name_to_id(t
, qualifier
, &id
);
223 error
= acl_add_entry(acl
, t
, id
, p
);
230 /* XXX should we only return ACLs valid according to acl_valid? */
231 /* verify validity of the ACL we read in */
232 if (acl_valid(acl
) == -1) {