]>
Commit | Line | Data |
---|---|---|
3d9156a7 A |
1 | /* |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | #include <sys/appleapiopts.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/acl.h> | |
27 | #include <errno.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | ||
31 | #include "aclvar.h" | |
32 | ||
33 | int | |
34 | acl_copy_entry(acl_entry_t dest, acl_entry_t src) | |
35 | { | |
36 | /* validate arguments */ | |
37 | _ACL_VALIDATE_ENTRY(dest); | |
38 | _ACL_VALIDATE_ENTRY(src); | |
39 | if (dest == src) { | |
40 | errno = EINVAL; | |
41 | return(-1); | |
42 | } | |
43 | bcopy(src, dest, sizeof(*src)); | |
44 | return(0); | |
45 | } | |
46 | ||
47 | int | |
48 | acl_create_entry_np(acl_t *acl_p, acl_entry_t *entry_p, int index) | |
49 | { | |
50 | struct _acl *ap = *acl_p; | |
51 | int i; | |
52 | ||
53 | /* validate arguments */ | |
54 | _ACL_VALIDATE_ACL(ap); | |
55 | if (ap->a_entries >= ACL_MAX_ENTRIES) { | |
56 | errno = ENOMEM; | |
57 | return(-1); | |
58 | } | |
59 | if (index == ACL_LAST_ENTRY) | |
60 | index = ap->a_entries; | |
61 | if (index > ap->a_entries) { | |
62 | errno = ERANGE; | |
63 | return(-1); | |
64 | } | |
65 | ||
66 | /* move following entries out of the way */ | |
67 | for (i = ap->a_entries; i > index; i--) | |
68 | ap->a_ace[i] = ap->a_ace[i - 1]; | |
69 | ap->a_entries++; | |
70 | ||
71 | /* initialise new entry */ | |
72 | ap->a_ace[index].ae_magic = _ACL_ENTRY_MAGIC; | |
73 | ap->a_ace[index].ae_tag = ACL_UNDEFINED_TAG; | |
74 | ||
75 | *entry_p = &ap->a_ace[index]; | |
76 | return(0); | |
77 | } | |
78 | ||
79 | int | |
80 | acl_create_entry(acl_t *acl_p, acl_entry_t *entry_p) | |
81 | { | |
82 | return(acl_create_entry_np(acl_p, entry_p, ACL_LAST_ENTRY)); | |
83 | } | |
84 | ||
85 | int | |
86 | acl_delete_entry(acl_t acl, acl_entry_t entry) | |
87 | { | |
88 | int i; | |
89 | ||
90 | _ACL_VALIDATE_ACL(acl); | |
91 | _ACL_VALIDATE_ENTRY(entry); | |
92 | _ACL_VALIDATE_ENTRY_CONTAINED(acl, entry); | |
93 | ||
94 | /* copy following entries down & invalidate last slot */ | |
95 | acl->a_entries--; | |
96 | for (i = entry - &acl->a_ace[0]; i < acl->a_entries; i++) | |
97 | acl->a_ace[i] = acl->a_ace[i + 1]; | |
98 | acl->a_ace[acl->a_entries].ae_magic = 0; | |
99 | /* Sync up the iterator's position if necessary */ | |
100 | if (acl->a_last_get >= (entry - &acl->a_ace[0])) | |
101 | acl->a_last_get--; | |
102 | ||
103 | return(0); | |
104 | } | |
105 | ||
106 | int | |
107 | acl_get_entry(acl_t acl, int entry_id, acl_entry_t *entry_p) | |
108 | { | |
109 | ||
110 | _ACL_VALIDATE_ACL(acl); | |
111 | if ((entry_id != ACL_FIRST_ENTRY) && | |
112 | (entry_id != ACL_NEXT_ENTRY) && | |
113 | (entry_id != ACL_LAST_ENTRY) && | |
114 | ((entry_id < 0) || (entry_id >= acl->a_entries))) { | |
115 | errno = EINVAL; | |
116 | return(-1); | |
117 | } | |
118 | if (entry_id == ACL_FIRST_ENTRY) | |
119 | entry_id = 0; | |
120 | else | |
121 | if (entry_id == ACL_NEXT_ENTRY) { | |
122 | entry_id = acl->a_last_get + 1; | |
123 | } | |
124 | else | |
125 | if (entry_id == ACL_LAST_ENTRY) | |
126 | entry_id = acl->a_entries - 1; | |
127 | ||
128 | if (entry_id >= acl->a_entries) { | |
129 | errno = EINVAL; | |
130 | return (-1); | |
131 | } | |
132 | ||
133 | *entry_p = &acl->a_ace[entry_id]; | |
134 | acl->a_last_get = entry_id; | |
135 | ||
136 | return(0); | |
137 | } | |
138 | ||
139 | void * | |
140 | acl_get_qualifier(acl_entry_t entry) | |
141 | { | |
142 | acl_tag_t tag_type; | |
143 | void *result; | |
144 | int error; | |
145 | ||
146 | result = NULL; | |
147 | if (!_ACL_VALID_ENTRY(entry)) { | |
148 | errno = EINVAL; | |
149 | } else if ((error = acl_get_tag_type(entry, &tag_type)) != 0) { | |
150 | /* errno is set by acl_get_tag_type */ | |
151 | } else { | |
152 | switch(tag_type) { | |
153 | case ACL_EXTENDED_ALLOW: | |
154 | case ACL_EXTENDED_DENY: | |
155 | if ((result = malloc(sizeof(guid_t))) != NULL) | |
156 | bcopy(&entry->ae_applicable, result, sizeof(guid_t)); | |
157 | break; | |
158 | default: | |
159 | errno = EINVAL; | |
160 | break; | |
161 | } | |
162 | } | |
163 | return(result); | |
164 | } | |
165 | ||
166 | int | |
167 | acl_get_tag_type(acl_entry_t entry, acl_tag_t *tag_type_p) | |
168 | { | |
169 | _ACL_VALIDATE_ENTRY(entry); | |
170 | ||
171 | *tag_type_p = entry->ae_tag; | |
172 | return(0); | |
173 | } | |
174 | ||
175 | int | |
176 | acl_set_qualifier(acl_entry_t entry, const void *tag_qualifier_p) | |
177 | { | |
178 | acl_tag_t tag_type; | |
179 | int error; | |
180 | ||
181 | _ACL_VALIDATE_ENTRY(entry); | |
182 | if ((error = acl_get_tag_type(entry, &tag_type)) != 0) | |
183 | return(error); | |
184 | ||
185 | switch(tag_type) { | |
186 | case ACL_EXTENDED_ALLOW: | |
187 | case ACL_EXTENDED_DENY: | |
188 | bcopy(tag_qualifier_p, &entry->ae_applicable, sizeof(guid_t)); | |
189 | error = 0; | |
190 | break; | |
191 | default: | |
192 | error = EINVAL; | |
193 | } | |
194 | return(error); | |
195 | } | |
196 | ||
197 | int | |
198 | acl_set_tag_type(acl_entry_t entry, acl_tag_t tag_type) | |
199 | { | |
200 | _ACL_VALIDATE_ENTRY(entry); | |
201 | ||
202 | switch(tag_type) { | |
203 | case ACL_EXTENDED_ALLOW: | |
204 | case ACL_EXTENDED_DENY: | |
205 | entry->ae_tag = tag_type; | |
206 | break; | |
207 | default: | |
208 | return(EINVAL); | |
209 | } | |
210 | return(0); | |
211 | } |