file_cmds-202.2.tar.gz
[apple/file_cmds.git] / chmod / chmod_acl.c
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <sys/stat.h>
42
43 #include <membership.h>
44 #include "chmod_acl.h"
45
46 extern void usage(void);
47
48 #ifdef __APPLE__
49 static struct {
50 acl_perm_t perm;
51 char *name;
52 int flags;
53 #define ACL_PERM_DIR (1<<0)
54 #define ACL_PERM_FILE (1<<1)
55 } acl_perms[] = {
56 {ACL_READ_DATA, "read", ACL_PERM_FILE},
57 {ACL_LIST_DIRECTORY, "list", ACL_PERM_DIR},
58 {ACL_WRITE_DATA, "write", ACL_PERM_FILE},
59 {ACL_ADD_FILE, "add_file", ACL_PERM_DIR},
60 {ACL_EXECUTE, "execute", ACL_PERM_FILE},
61 {ACL_SEARCH, "search", ACL_PERM_DIR},
62 {ACL_DELETE, "delete", ACL_PERM_FILE | ACL_PERM_DIR},
63 {ACL_APPEND_DATA, "append", ACL_PERM_FILE},
64 {ACL_ADD_SUBDIRECTORY, "add_subdirectory", ACL_PERM_DIR},
65 {ACL_DELETE_CHILD, "delete_child", ACL_PERM_DIR},
66 {ACL_READ_ATTRIBUTES, "readattr", ACL_PERM_FILE | ACL_PERM_DIR},
67 {ACL_WRITE_ATTRIBUTES, "writeattr", ACL_PERM_FILE | ACL_PERM_DIR},
68 {ACL_READ_EXTATTRIBUTES, "readextattr", ACL_PERM_FILE | ACL_PERM_DIR},
69 {ACL_WRITE_EXTATTRIBUTES, "writeextattr", ACL_PERM_FILE | ACL_PERM_DIR},
70 {ACL_READ_SECURITY, "readsecurity", ACL_PERM_FILE | ACL_PERM_DIR},
71 {ACL_WRITE_SECURITY, "writesecurity", ACL_PERM_FILE | ACL_PERM_DIR},
72 {ACL_CHANGE_OWNER, "chown", ACL_PERM_FILE | ACL_PERM_DIR},
73 {0, NULL, 0}
74 };
75
76 static struct {
77 acl_flag_t flag;
78 char *name;
79 int flags;
80 } acl_flags[] = {
81 {ACL_ENTRY_INHERITED, "inherited", ACL_PERM_FILE | ACL_PERM_DIR},
82 {ACL_ENTRY_FILE_INHERIT, "file_inherit", ACL_PERM_DIR},
83 {ACL_ENTRY_DIRECTORY_INHERIT, "directory_inherit", ACL_PERM_DIR},
84 {ACL_ENTRY_LIMIT_INHERIT, "limit_inherit", ACL_PERM_FILE | ACL_PERM_DIR},
85 {ACL_ENTRY_ONLY_INHERIT, "only_inherit", ACL_PERM_DIR},
86 {0, NULL, 0}
87 };
88
89 /* TBD - Many of these routines could potentially be considered for
90 * inclusion in a library. If that is done, either avoid use of "err"
91 * and implement a better fall-through strategy in case of errors,
92 * or use err_set_exit() and make various structures globals.
93 */
94
95 #define NAME_USER (1)
96 #define NAME_GROUP (2)
97 #define NAME_EITHER (NAME_USER | NAME_GROUP)
98
99 /* Perform a name to uuid mapping - calls through to memberd */
100
101 uuid_t *
102 name_to_uuid(char *tok, int nametype) {
103 struct passwd *tpass = NULL;
104 struct group *tgrp = NULL;
105 uuid_t *entryg = NULL;
106
107 if ((entryg = (uuid_t *) calloc(1,sizeof(uuid_t))) == NULL)
108 err(1, "Unable to allocate a uuid");
109
110 if (nametype & NAME_USER)
111 tpass = getpwnam(tok);
112
113 if (NULL == tpass && (nametype & NAME_GROUP))
114 tgrp = getgrnam(tok);
115
116 if (tpass) {
117 if (0 != mbr_uid_to_uuid(tpass->pw_uid, *entryg)) {
118 errx(1, "mbr_uid_to_uuid(): Unable to translate uid %d", tpass->pw_uid);
119 }
120 } else if (tgrp) {
121 if (0 != mbr_gid_to_uuid(tgrp->gr_gid, *entryg)) {
122 errx(1, "mbr_gid_to_uuid(): Unable to translate gid %d", tgrp->gr_gid);
123 }
124 } else {
125 errx(1, "Unable to translate '%s' to a UID/GID", tok);
126 }
127 return entryg;
128 }
129
130 /* Convert an acl entry in string form to an acl_entry_t */
131 int
132 parse_entry(char *entrybuf, acl_entry_t newent) {
133 char *tok;
134 char *pebuf;
135 uuid_t *entryg;
136
137 acl_tag_t tag;
138 acl_permset_t perms;
139 acl_flagset_t flags;
140 unsigned permcount = 0;
141 unsigned pindex = 0;
142 char *delimiter = " ";
143 int nametype = NAME_EITHER;
144
145 acl_get_permset(newent, &perms);
146 acl_get_flagset_np(newent, &flags);
147
148 pebuf = entrybuf;
149
150 if (0 == strncmp(entrybuf, "user:", 5)) {
151 nametype = NAME_USER;
152 pebuf += 5;
153 } else if (0 == strncmp(entrybuf, "group:", 6)) {
154 nametype = NAME_GROUP;
155 pebuf += 6;
156 }
157
158 if (strchr(pebuf, ':')) /* User/Group names can have spaces */
159 delimiter = ":";
160 tok = strsep(&pebuf, delimiter);
161
162 if ((tok == NULL) || *tok == '\0') {
163 errx(1, "Invalid entry format -- expected user or group name");
164 }
165
166 /* parse the name into a qualifier */
167 entryg = name_to_uuid(tok, nametype);
168
169 tok = strsep(&pebuf, ": "); /* Stick with delimiter? */
170 if ((tok == NULL) || *tok == '\0') {
171 errx(1, "Invalid entry format -- expected allow or deny");
172 }
173
174 /* is the verb 'allow' or 'deny'? */
175 if (!strcmp(tok, "allow")) {
176 tag = ACL_EXTENDED_ALLOW;
177 } else if (!strcmp(tok, "deny")) {
178 tag = ACL_EXTENDED_DENY;
179 } else {
180 errx(1, "Unknown tag type '%s'", tok);
181 }
182
183 /* parse permissions */
184 for (; (tok = strsep(&pebuf, ",")) != NULL;) {
185 if (*tok != '\0') {
186 /* is it a permission? */
187 for (pindex = 0; acl_perms[pindex].name != NULL; pindex++) {
188 if (!strcmp(acl_perms[pindex].name, tok)) {
189 /* got one */
190 acl_add_perm(perms, acl_perms[pindex].perm);
191 permcount++;
192 goto found;
193 }
194 }
195 /* is it a flag? */
196 for (pindex = 0; acl_flags[pindex].name != NULL; pindex++) {
197 if (!strcmp(acl_flags[pindex].name, tok)) {
198 /* got one */
199 acl_add_flag_np(flags, acl_flags[pindex].flag);
200 permcount++;
201 goto found;
202 }
203 }
204 errx(1,"Invalid permission type '%s'", tok);
205 found:
206 continue;
207 }
208 }
209 if (0 == permcount) {
210 errx(1, "No permissions specified");
211 }
212 acl_set_tag_type(newent, tag);
213 acl_set_qualifier(newent, entryg);
214 acl_set_permset(newent, perms);
215 acl_set_flagset_np(newent, flags);
216 free(entryg);
217
218 return(0);
219 }
220
221 /* Convert one or more acl entries in string form to an acl_t */
222 acl_t
223 parse_acl_entries(const char *input) {
224 acl_t acl_input;
225 acl_entry_t newent;
226 char *inbuf;
227 char *oinbuf;
228
229 char **bufp, *entryv[ACL_MAX_ENTRIES];
230 #if 0
231 /* XXX acl_from_text(), when implemented, will presumably use the canonical
232 * text representation format, which is what chmod should be using
233 * We may need to add an entry number to the input
234 */
235 /* Translate the user supplied ACL entry */
236 /* acl_input = acl_from_text(input); */
237 #else
238 inbuf = malloc(MAX_ACL_TEXT_SIZE);
239
240 if (inbuf == NULL)
241 err(1, "malloc() failed");
242 strncpy(inbuf, input, MAX_ACL_TEXT_SIZE);
243 inbuf[MAX_ACL_TEXT_SIZE - 1] = '\0';
244
245 if ((acl_input = acl_init(1)) == NULL)
246 err(1, "acl_init() failed");
247
248 oinbuf = inbuf;
249
250 for (bufp = entryv; (*bufp = strsep(&oinbuf, "\n")) != NULL;)
251 if (**bufp != '\0') {
252 if (0 != acl_create_entry(&acl_input, &newent))
253 err(1, "acl_create_entry() failed");
254 if (0 != parse_entry(*bufp, newent)) {
255 errx(1, "Failed parsing entry '%s'", *bufp);
256 }
257 if (++bufp >= &entryv[ACL_MAX_ENTRIES - 1]) {
258 errx(1, "Too many entries");
259 }
260 }
261
262 free(inbuf);
263 return acl_input;
264 #endif /* #if 0 */
265 }
266
267 /* XXX No Libc support for inherited entries and generation determination yet */
268 unsigned
269 get_inheritance_level(acl_entry_t entry) {
270 /* XXX to be implemented */
271 return 1;
272 }
273
274 /* Determine a "score" for an acl entry. The entry scores higher if it's
275 * tagged ACL_EXTENDED_DENY, and non-inherited entries are ranked higher
276 * than inherited entries.
277 */
278
279 int
280 score_acl_entry(acl_entry_t entry) {
281
282 acl_tag_t tag;
283 acl_flagset_t flags;
284 acl_permset_t perms;
285
286 int score = 0;
287
288 if (entry == NULL)
289 return (MINIMUM_TIER);
290
291 if (acl_get_tag_type(entry, &tag) != 0) {
292 err(1, "Malformed ACL entry, no tag present");
293 }
294 if (acl_get_flagset_np(entry, &flags) != 0){
295 err(1, "Unable to obtain flagset");
296 }
297 if (acl_get_permset(entry, &perms) != 0)
298 err(1, "Malformed ACL entry, no permset present");
299
300 switch(tag) {
301 case ACL_EXTENDED_ALLOW:
302 break;
303 case ACL_EXTENDED_DENY:
304 score++;
305 break;
306 default:
307 errx(1, "Unknown tag type %d present in ACL entry", tag);
308 /* NOTREACHED */
309 }
310
311 if (acl_get_flag_np(flags, ACL_ENTRY_INHERITED))
312 score += get_inheritance_level(entry) * INHERITANCE_TIER;
313
314 return score;
315 }
316
317 int
318 compare_acl_qualifiers(uuid_t *qa, uuid_t *qb) {
319 return bcmp(qa, qb, sizeof(uuid_t));
320 }
321
322 /* Compare two ACL permsets.
323 * Returns :
324 * MATCH_SUBSET if bperms is a subset of aperms
325 * MATCH_SUPERSET if bperms is a superset of aperms
326 * MATCH_PARTIAL if the two permsets have a common subset
327 * MATCH_EXACT if the two permsets are identical
328 * MATCH_NONE if they are disjoint
329 */
330
331 int
332 compare_acl_permsets(acl_permset_t aperms, acl_permset_t bperms)
333 {
334 int i;
335 /* TBD Implement other match levels as needed */
336 for (i = 0; acl_perms[i].name != NULL; i++) {
337 if (acl_get_perm_np(aperms, acl_perms[i].perm) !=
338 acl_get_perm_np(bperms, acl_perms[i].perm))
339 return MATCH_NONE;
340 }
341 return MATCH_EXACT;
342 }
343
344 int
345 compare_acl_flagsets(acl_flagset_t aflags, acl_flagset_t bflags)
346 {
347 int i;
348 /* TBD Implement other match levels as needed */
349 for (i = 0; acl_flags[i].name != NULL; i++) {
350 if (acl_get_flag_np(aflags, acl_flags[i].flag) !=
351 acl_get_flag_np(bflags, acl_flags[i].flag))
352 return MATCH_NONE;
353 }
354 return MATCH_EXACT;
355 }
356
357 /* Compares two ACL entries for equality */
358 int
359 compare_acl_entries(acl_entry_t a, acl_entry_t b)
360 {
361 acl_tag_t atag, btag;
362 acl_permset_t aperms, bperms;
363 acl_flagset_t aflags, bflags;
364 int pcmp = 0, fcmp = 0;
365
366 if (0 != compare_acl_qualifiers(acl_get_qualifier(a),
367 acl_get_qualifier(b)))
368 return MATCH_NONE;
369
370 if (0 != acl_get_tag_type(a, &atag))
371 err(1, "No tag type present in entry");
372 if (0!= acl_get_tag_type(b, &btag))
373 err(1, "No tag type present in entry");
374
375 if (atag != btag)
376 return MATCH_NONE;
377
378 if ((acl_get_permset(a, &aperms) != 0) ||
379 (acl_get_flagset_np(a, &aflags) != 0) ||
380 (acl_get_permset(b, &bperms) != 0) ||
381 (acl_get_flagset_np(b, &bflags) != 0))
382 err(1, "error fetching permissions");
383
384 pcmp = compare_acl_permsets(aperms, bperms);
385 fcmp = compare_acl_flagsets(aflags, bflags);
386
387 if ((pcmp == MATCH_NONE) || (fcmp == MATCH_NONE))
388 return(MATCH_PARTIAL);
389 else
390 return(MATCH_EXACT);
391 }
392
393 /* Verify that an ACL is in canonical order. Currently, the canonical
394 * form is:
395 * local deny
396 * local allow
397 * inherited deny (parent)
398 * inherited allow (parent)
399 * inherited deny (grandparent)
400 * inherited allow (grandparent)
401 * ...
402 */
403 unsigned int
404 is_canonical(acl_t acl) {
405
406 unsigned aindex;
407 acl_entry_t entry;
408 int score = 0, next_score = 0;
409
410 /* XXX - is a zero entry ACL in canonical form? */
411 if (0 != acl_get_entry(acl, ACL_FIRST_ENTRY, &entry))
412 return 1;
413
414 score = score_acl_entry(entry);
415
416 for (aindex = 0; acl_get_entry(acl, ACL_NEXT_ENTRY, &entry) == 0;
417 aindex++) {
418 if (score < (next_score = score_acl_entry(entry)))
419 return 0;
420 score = next_score;
421 }
422 return 1;
423 }
424
425
426 /* Iterate through an ACL, and find the canonical position for the
427 * specified entry
428 */
429 unsigned int
430 find_canonical_position(acl_t acl, acl_entry_t modifier) {
431
432 acl_entry_t entry;
433 int mscore = 0;
434 unsigned mpos = 0;
435
436 /* Check if there's an entry with the same qualifier
437 * and tag type; if not, find the appropriate slot
438 * for the score.
439 */
440
441 if (0 != acl_get_entry(acl, ACL_FIRST_ENTRY, &entry))
442 return 0;
443
444 mscore = score_acl_entry(modifier);
445
446 while (mscore < score_acl_entry(entry)) {
447
448 mpos++;
449
450 if (0 != acl_get_entry(acl, ACL_NEXT_ENTRY, &entry))
451 break;
452
453 }
454 return mpos;
455 }
456
457 int canonicalize_acl_entries(acl_t acl);
458
459 /* For a given acl_entry_t "modifier", find the first exact or
460 * partially matching entry from the specified acl_t acl
461 */
462
463 int
464 find_matching_entry (acl_t acl, acl_entry_t modifier, acl_entry_t *rentryp,
465 unsigned match_inherited) {
466
467 acl_entry_t entry = NULL;
468
469 unsigned aindex;
470 int cmp, fcmp = MATCH_NONE;
471
472 for (aindex = 0;
473 acl_get_entry(acl, entry == NULL ? ACL_FIRST_ENTRY :
474 ACL_NEXT_ENTRY, &entry) == 0;
475 aindex++) {
476 cmp = compare_acl_entries(entry, modifier);
477 if ((cmp == MATCH_EXACT) || (cmp == MATCH_PARTIAL)) {
478 if (match_inherited) {
479 acl_flagset_t eflags, mflags;
480
481 if (0 != acl_get_flagset_np(modifier, &mflags))
482 err(1, "Unable to get flagset");
483
484 if (0 != acl_get_flagset_np(entry, &eflags))
485 err(1, "Unable to get flagset");
486
487 if (compare_acl_flagsets(mflags, eflags) == MATCH_EXACT) {
488 *rentryp = entry;
489 fcmp = cmp;
490 }
491 }
492 else {
493 *rentryp = entry;
494 fcmp = cmp;
495 }
496 }
497 if (fcmp == MATCH_EXACT)
498 break;
499 }
500 return fcmp;
501 }
502
503 /* Remove all perms specified in modifier from rentry*/
504 int
505 subtract_from_entry(acl_entry_t rentry, acl_entry_t modifier, int* valid_perms)
506 {
507 acl_permset_t rperms, mperms;
508 acl_flagset_t rflags, mflags;
509 if (valid_perms)
510 *valid_perms = 0;
511 int i;
512
513 if ((acl_get_permset(rentry, &rperms) != 0) ||
514 (acl_get_flagset_np(rentry, &rflags) != 0) ||
515 (acl_get_permset(modifier, &mperms) != 0) ||
516 (acl_get_flagset_np(modifier, &mflags) != 0))
517 err(1, "error computing ACL modification");
518
519 for (i = 0; acl_perms[i].name != NULL; i++) {
520 if (acl_get_perm_np(mperms, acl_perms[i].perm))
521 acl_delete_perm(rperms, acl_perms[i].perm);
522 else if (valid_perms && acl_get_perm_np(rperms, acl_perms[i].perm))
523 (*valid_perms)++;
524 }
525 for (i = 0; acl_flags[i].name != NULL; i++) {
526 if (acl_get_flag_np(mflags, acl_flags[i].flag))
527 acl_delete_flag_np(rflags, acl_flags[i].flag);
528 }
529 acl_set_permset(rentry, rperms);
530 acl_set_flagset_np(rentry, rflags);
531 return 0;
532 }
533 /* Add the perms specified in modifier to rentry */
534 int
535 merge_entry_perms(acl_entry_t rentry, acl_entry_t modifier)
536 {
537 acl_permset_t rperms, mperms;
538 acl_flagset_t rflags, mflags;
539 int i;
540
541 if ((acl_get_permset(rentry, &rperms) != 0) ||
542 (acl_get_flagset_np(rentry, &rflags) != 0) ||
543 (acl_get_permset(modifier, &mperms) != 0) ||
544 (acl_get_flagset_np(modifier, &mflags) != 0))
545 err(1, "error computing ACL modification");
546
547 for (i = 0; acl_perms[i].name != NULL; i++) {
548 if (acl_get_perm_np(mperms, acl_perms[i].perm))
549 acl_add_perm(rperms, acl_perms[i].perm);
550 }
551 for (i = 0; acl_flags[i].name != NULL; i++) {
552 if (acl_get_flag_np(mflags, acl_flags[i].flag))
553 acl_add_flag_np(rflags, acl_flags[i].flag);
554 }
555 acl_set_permset(rentry, rperms);
556 acl_set_flagset_np(rentry, rflags);
557 return 0;
558 }
559
560 int
561 modify_acl(acl_t *oaclp, acl_entry_t modifier, unsigned int optflags,
562 int position, int inheritance_level,
563 unsigned flag_new_acl, const char* path) {
564
565 unsigned cpos = 0;
566 acl_entry_t newent = NULL;
567 int dmatch = 0;
568 acl_entry_t rentry = NULL;
569 unsigned retval = 0;
570 acl_t oacl = *oaclp;
571
572 /* Add the inherited flag if requested by the user*/
573 if (modifier && (optflags & ACL_INHERIT_FLAG)) {
574 acl_flagset_t mflags;
575
576 acl_get_flagset_np(modifier, &mflags);
577 acl_add_flag_np(mflags, ACL_ENTRY_INHERITED);
578 acl_set_flagset_np(modifier, mflags);
579 }
580
581 if (optflags & ACL_SET_FLAG) {
582 if (position != -1) {
583 if (0 != acl_create_entry_np(&oacl, &newent, position))
584 err(1, "acl_create_entry() failed");
585 acl_copy_entry(newent, modifier);
586 } else {
587 /* If an entry exists, add the new permissions to it, else add an
588 * entry in the canonical position.
589 */
590
591 /* First, check for a matching entry - if one exists, merge flags */
592 dmatch = find_matching_entry(oacl, modifier, &rentry, 1);
593
594 if (dmatch != MATCH_NONE) {
595 if (dmatch == MATCH_EXACT)
596 /* Nothing to be done */
597 goto ma_exit;
598
599 if (dmatch == MATCH_PARTIAL) {
600 merge_entry_perms(rentry, modifier);
601 goto ma_exit;
602 }
603 }
604 /* Insert the entry in canonical order */
605 cpos = find_canonical_position(oacl, modifier);
606 if (0!= acl_create_entry_np(&oacl, &newent, cpos))
607 err(1, "acl_create_entry() failed");
608 acl_copy_entry(newent, modifier);
609 }
610 } else if (optflags & ACL_DELETE_FLAG) {
611 if (flag_new_acl) {
612 warnx("No ACL present '%s'", path);
613 retval = 1;
614 } else if (position != -1 ) {
615 if (0 != acl_get_entry(oacl, position, &rentry)) {
616 warnx("Invalid entry number '%s'", path);
617 retval = 1;
618 } else {
619 acl_delete_entry(oacl, rentry);
620 }
621 } else {
622 unsigned match_found = 0, aindex;
623 for (aindex = 0;
624 acl_get_entry(oacl, rentry == NULL ?
625 ACL_FIRST_ENTRY :
626 ACL_NEXT_ENTRY, &rentry) == 0;
627 aindex++) {
628 unsigned cmp;
629 cmp = compare_acl_entries(rentry, modifier);
630 if ((cmp == MATCH_EXACT) ||
631 (cmp == MATCH_PARTIAL)) {
632 match_found++;
633 if (cmp == MATCH_EXACT)
634 acl_delete_entry(oacl, rentry);
635 else {
636 int valid_perms;
637 /* In the event of a partial match, remove the specified perms from the
638 * entry */
639 subtract_from_entry(rentry, modifier, &valid_perms);
640 /* if no perms survived then delete the entry */
641 if (valid_perms == 0)
642 acl_delete_entry(oacl, rentry);
643 }
644 }
645 }
646 if (0 == match_found) {
647 warnx("Entry not found when attempting delete '%s'",path);
648 retval = 1;
649 }
650 }
651 } else if (optflags & ACL_REWRITE_FLAG) {
652 acl_entry_t rentry;
653
654 if (-1 == position) {
655 usage();
656 }
657 if (0 == flag_new_acl) {
658 if (0 != acl_get_entry(oacl, position,
659 &rentry))
660 err(1, "Invalid entry number '%s'", path);
661
662 if (0 != acl_delete_entry(oacl, rentry))
663 err(1, "Unable to delete entry '%s'", path);
664 }
665 if (0!= acl_create_entry_np(&oacl, &newent, position))
666 err(1, "acl_create_entry() failed");
667 acl_copy_entry(newent, modifier);
668 }
669 ma_exit:
670 *oaclp = oacl;
671 return retval;
672 }
673
674 int
675 modify_file_acl(unsigned int optflags, const char *path, acl_t modifier, int position, int inheritance_level) {
676
677 acl_t oacl = NULL;
678 unsigned aindex = 0, flag_new_acl = 0;
679 acl_entry_t newent = NULL;
680 acl_entry_t entry = NULL;
681 unsigned retval = 0 ;
682
683 extern int fflag;
684
685 /* XXX acl_get_file() returns a zero entry ACL if an ACL was previously
686 * associated with the file, and has had its entries removed.
687 * However, POSIX 1003.1e states that a zero entry ACL should be
688 * returned if the caller asks for ACL_TYPE_DEFAULT, and no ACL is
689 * associated with the path; it
690 * does not specifically state that a request for ACL_TYPE_EXTENDED
691 * should not return a zero entry ACL, however.
692 */
693
694 /* Determine if we've been given a zero entry ACL, or create an ACL if
695 * none exists. There are some issues to consider here: Should we create
696 * a zero-entry ACL for a delete or check canonicity operation?
697 */
698
699 if (path == NULL)
700 usage();
701
702 if (optflags & ACL_CLEAR_FLAG) {
703 filesec_t fsec = filesec_init();
704 if (fsec == NULL)
705 err(1, "filesec_init() failed");
706 if (filesec_set_property(fsec, FILESEC_ACL,
707 _FILESEC_REMOVE_ACL) != 0)
708 err(1, "filesec_set_property() failed");
709 if (chmodx_np(path, fsec) != 0) {
710 if (!fflag)
711 warn("Failed to clear ACL on file %s", path);
712 retval = 1;
713 } else
714 retval = 0;
715 filesec_free(fsec);
716 return (retval);
717 }
718
719 if (optflags & ACL_FROM_STDIN)
720 oacl = acl_dup(modifier);
721 else {
722 oacl = acl_get_file(path, ACL_TYPE_EXTENDED);
723
724 if ((oacl == NULL) ||
725 (acl_get_entry(oacl,ACL_FIRST_ENTRY, &newent) != 0)) {
726 if ((oacl = acl_init(1)) == NULL)
727 err(1, "acl_init() failed");
728 flag_new_acl = 1;
729 position = 0;
730 }
731
732 if ((0 == flag_new_acl) && (optflags & (ACL_REMOVE_INHERIT_FLAG |
733 ACL_REMOVE_INHERITED_ENTRIES))) {
734 acl_t facl = NULL;
735 if ((facl = acl_init(1)) == NULL)
736 err(1, "acl_init() failed");
737 for (aindex = 0;
738 acl_get_entry(oacl,
739 (entry == NULL ? ACL_FIRST_ENTRY :
740 ACL_NEXT_ENTRY), &entry) == 0;
741 aindex++) {
742 acl_flagset_t eflags;
743 acl_entry_t fent = NULL;
744 if (acl_get_flagset_np(entry, &eflags) != 0) {
745 err(1, "Unable to obtain flagset");
746 }
747
748 if (acl_get_flag_np(eflags, ACL_ENTRY_INHERITED)) {
749 if (optflags & ACL_REMOVE_INHERIT_FLAG) {
750 acl_delete_flag_np(eflags, ACL_ENTRY_INHERITED);
751 acl_set_flagset_np(entry, eflags);
752 acl_create_entry(&facl, &fent);
753 acl_copy_entry(fent, entry);
754 }
755 }
756 else {
757 acl_create_entry(&facl, &fent);
758 acl_copy_entry(fent, entry);
759 }
760 }
761 if (oacl)
762 acl_free(oacl);
763 oacl = facl;
764 } else if (optflags & ACL_TO_STDOUT) {
765 ssize_t len; /* need to get printacl() from ls(1) */
766 char *text = acl_to_text(oacl, &len);
767 puts(text);
768 acl_free(text);
769 } else if (optflags & ACL_CHECK_CANONICITY) {
770 if (flag_new_acl) {
771 warnx("No ACL currently associated with file '%s'", path);
772 }
773 retval = is_canonical(oacl);
774 } else if ((optflags & ACL_SET_FLAG) && (position == -1) &&
775 (!is_canonical(oacl))) {
776 warnx("The specified file '%s' does not have an ACL in canonical order, please specify a position with +a# ", path);
777 retval = 1;
778 } else if (((optflags & ACL_DELETE_FLAG) && (position != -1))
779 || (optflags & ACL_CHECK_CANONICITY)) {
780 retval = modify_acl(&oacl, NULL, optflags, position,
781 inheritance_level, flag_new_acl, path);
782 } else if ((optflags & (ACL_REMOVE_INHERIT_FLAG|ACL_REMOVE_INHERITED_ENTRIES)) && flag_new_acl) {
783 warnx("No ACL currently associated with file '%s'", path);
784 retval = 1;
785 } else {
786 if (!modifier) { /* avoid bus error in acl_get_entry */
787 errx(1, "Internal error: modifier should not be NULL");
788 }
789 for (aindex = 0;
790 acl_get_entry(modifier,
791 (entry == NULL ? ACL_FIRST_ENTRY :
792 ACL_NEXT_ENTRY), &entry) == 0;
793 aindex++) {
794
795 retval += modify_acl(&oacl, entry, optflags,
796 position, inheritance_level,
797 flag_new_acl, path);
798 }
799 }
800 }
801
802 /* XXX Potential race here, since someone else could've modified or
803 * read the ACL on this file (with the intention of modifying it) in
804 * the interval from acl_get_file() to acl_set_file(); we can
805 * minimize one aspect of this window by comparing the original acl
806 * to a fresh one from acl_get_file() but we could consider a
807 * "changeset" mechanism, common locking strategy, or kernel
808 * supplied reservation mechanism to prevent this race.
809 */
810 if (!(optflags & (ACL_TO_STDOUT|ACL_CHECK_CANONICITY)) &&
811 (0 != acl_set_file(path, ACL_TYPE_EXTENDED, oacl))){
812 if (!fflag)
813 warn("Failed to set ACL on file '%s'", path);
814 retval = 1;
815 }
816
817 if (oacl)
818 acl_free(oacl);
819
820 return retval;
821 }
822
823 #endif /*__APPLE__*/