]> gitweb.michael.orlitzky.com - apply-default-acl.git/blob - src/apply-default-acl.c
Eliminate pointless newline in the signature of acl_set_entry().
[apply-default-acl.git] / src / apply-default-acl.c
1 /**
2 * @file apply-default-acl.c
3 *
4 * @brief The entire implementation.
5 *
6 */
7
8 /* On Linux, ftw.h needs this special voodoo to work. */
9 #define _XOPEN_SOURCE 500
10 #define _GNU_SOURCE
11
12 #include <errno.h>
13 #include <fcntl.h> /* AT_FOO constants */
14 #include <ftw.h> /* nftw() et al. */
15 #include <getopt.h>
16 #include <libgen.h> /* basename(), dirname() */
17 #include <stdbool.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23
24 /* ACLs */
25 #include <acl/libacl.h> /* acl_get_perm, not portable */
26 #include <sys/types.h>
27 #include <sys/acl.h>
28
29 /* Most of the libacl functions return 1 for success, 0 for failure,
30 and -1 on error */
31 #define ACL_ERROR -1
32 #define ACL_FAILURE 0
33 #define ACL_SUCCESS 1
34
35
36
37
38 /**
39 * @brief Determine if the given file descriptor might refer to an
40 * (unsafe) hard link.
41 *
42 * @param fd
43 * The file descriptor whose link count we want to investigate.
44 *
45 * @return true if we are certain that @c fd does not describe a hard
46 * link, and false otherwise. In case of error, false is returned,
47 * because we are not sure that @c fd is not a hard link.
48 */
49 bool is_hardlink_safe(int fd) {
50 if (fd <= 0) {
51 return false;
52 }
53 struct stat s;
54 if (fstat(fd, &s) == 0) {
55 return (s.st_nlink == 1 || S_ISDIR(s.st_mode));
56 }
57 else {
58 return false;
59 }
60 }
61
62
63 /**
64 * @brief Determine whether or not the given file descriptor is for
65 * a regular file.
66 *
67 * @param fd
68 * The file descriptor to test for regular-fileness.
69 *
70 * @return true if @c fd describes a regular file, and false otherwise.
71 */
72 bool is_regular_file(int fd) {
73 if (fd <= 0) {
74 return false;
75 }
76
77 struct stat s;
78 if (fstat(fd, &s) == 0) {
79 return S_ISREG(s.st_mode);
80 }
81 else {
82 return false;
83 }
84 }
85
86
87
88 /**
89 * @brief Determine whether or not the given path is accessible.
90 *
91 * @param path
92 * The path to test.
93 *
94 * @return true if @c path is accessible to the current effective
95 * user/group, false otherwise.
96 */
97 bool path_accessible(const char* path) {
98 if (path == NULL) {
99 return false;
100 }
101
102 /* Test for access using the effective user and group rather than
103 the real one. */
104 int flags = AT_EACCESS;
105
106 /* Don't follow symlinks when checking for a path's existence,
107 since we won't follow them to set its ACLs either. */
108 flags |= AT_SYMLINK_NOFOLLOW;
109
110 /* If the path is relative, interpret it relative to the current
111 working directory (just like the access() system call). */
112 if (faccessat(AT_FDCWD, path, F_OK, flags) == 0) {
113 return true;
114 }
115 else {
116 return false;
117 }
118 }
119
120
121
122 /**
123 * @brief Determine whether or not the given path is a directory.
124 *
125 * @param path
126 * The path to test.
127 *
128 * @return true if @c path is a directory, false otherwise.
129 */
130 bool is_path_directory(const char* path) {
131 if (path == NULL) {
132 return false;
133 }
134
135 struct stat s;
136 if (lstat(path, &s) == 0) {
137 return S_ISDIR(s.st_mode);
138 }
139 else {
140 return false;
141 }
142 }
143
144
145 /**
146 * @brief Determine whether or not the given file descriptor is for
147 * a directory.
148 *
149 * @param fd
150 * The file descriptor whose directoryness is in question.
151 *
152 * @return true if @c fd describes a directory, and false otherwise.
153 */
154 bool is_directory(int fd) {
155 if (fd <= 0) {
156 return false;
157 }
158
159 struct stat s;
160 if (fstat(fd, &s) == 0) {
161 return S_ISDIR(s.st_mode);
162 }
163 else {
164 return false;
165 }
166 }
167
168
169
170 /**
171 * @brief Update (or create) an entry in an @b minimal ACL.
172 *
173 * This function will not work if @c aclp contains extended
174 * entries. This is fine for our purposes, since we call @c wipe_acls
175 * on each path before applying the default to it.
176 *
177 * The assumption that there are no extended entries makes things much
178 * simpler. For example, we only have to update the @c ACL_USER_OBJ,
179 * @c ACL_GROUP_OBJ, and @c ACL_OTHER entries -- all others can simply
180 * be created anew. This means we don't have to fool around comparing
181 * named-user/group entries.
182 *
183 * @param aclp
184 * A pointer to the acl_t structure whose entry we want to modify.
185 *
186 * @param entry
187 * The new entry. If @c entry contains a user/group/other entry, we
188 * update the existing one. Otherwise we create a new entry.
189 *
190 * @return If there is an unexpected library error, @c ACL_ERROR is
191 * returned. Otherwise, @c ACL_SUCCESS.
192 *
193 */
194 int acl_set_entry(acl_t* aclp, acl_entry_t entry) {
195
196 acl_tag_t entry_tag;
197 if (acl_get_tag_type(entry, &entry_tag) == ACL_ERROR) {
198 perror("acl_set_entry (acl_get_tag_type)");
199 return ACL_ERROR;
200 }
201
202 acl_permset_t entry_permset;
203 if (acl_get_permset(entry, &entry_permset) == ACL_ERROR) {
204 perror("acl_set_entry (acl_get_permset)");
205 return ACL_ERROR;
206 }
207
208 acl_entry_t existing_entry;
209 /* Loop through the given ACL looking for matching entries. */
210 int result = acl_get_entry(*aclp, ACL_FIRST_ENTRY, &existing_entry);
211
212 while (result == ACL_SUCCESS) {
213 acl_tag_t existing_tag = ACL_UNDEFINED_TAG;
214
215 if (acl_get_tag_type(existing_entry, &existing_tag) == ACL_ERROR) {
216 perror("set_acl_tag_permset (acl_get_tag_type)");
217 return ACL_ERROR;
218 }
219
220 if (existing_tag == entry_tag) {
221 if (entry_tag == ACL_USER_OBJ ||
222 entry_tag == ACL_GROUP_OBJ ||
223 entry_tag == ACL_OTHER) {
224 /* Only update for these three since all other tags will have
225 been wiped. These three are guaranteed to exist, so if we
226 match one of them, we're allowed to return ACL_SUCCESS
227 below and bypass the rest of the function. */
228 acl_permset_t existing_permset;
229 if (acl_get_permset(existing_entry, &existing_permset) == ACL_ERROR) {
230 perror("acl_set_entry (acl_get_permset)");
231 return ACL_ERROR;
232 }
233
234 if (acl_set_permset(existing_entry, entry_permset) == ACL_ERROR) {
235 perror("acl_set_entry (acl_set_permset)");
236 return ACL_ERROR;
237 }
238
239 return ACL_SUCCESS;
240 }
241
242 }
243
244 result = acl_get_entry(*aclp, ACL_NEXT_ENTRY, &existing_entry);
245 }
246
247 /* This catches both the initial acl_get_entry and the ones at the
248 end of the loop. */
249 if (result == ACL_ERROR) {
250 perror("acl_set_entry (acl_get_entry)");
251 return ACL_ERROR;
252 }
253
254 /* If we've made it this far, we need to add a new entry to the
255 ACL. */
256 acl_entry_t new_entry;
257
258 /* The acl_create_entry() function can allocate new memory and/or
259 * change the location of the ACL structure entirely. When that
260 * happens, the value pointed to by aclp is updated, which means
261 * that a new acl_t gets "passed out" to our caller, eventually to
262 * be fed to acl_free(). In other words, we should still be freeing
263 * the right thing, even if the value pointed to by aclp changes.
264 */
265 if (acl_create_entry(aclp, &new_entry) == ACL_ERROR) {
266 perror("acl_set_entry (acl_create_entry)");
267 return ACL_ERROR;
268 }
269
270 if (acl_set_tag_type(new_entry, entry_tag) == ACL_ERROR) {
271 perror("acl_set_entry (acl_set_tag_type)");
272 return ACL_ERROR;
273 }
274
275 if (acl_set_permset(new_entry, entry_permset) == ACL_ERROR) {
276 perror("acl_set_entry (acl_set_permset)");
277 return ACL_ERROR;
278 }
279
280 if (entry_tag == ACL_USER || entry_tag == ACL_GROUP) {
281 /* We need to set the qualifier too. */
282 void* entry_qual = acl_get_qualifier(entry);
283 if (entry_qual == (void*)NULL) {
284 perror("acl_set_entry (acl_get_qualifier)");
285 return ACL_ERROR;
286 }
287
288 if (acl_set_qualifier(new_entry, entry_qual) == ACL_ERROR) {
289 perror("acl_set_entry (acl_set_qualifier)");
290 return ACL_ERROR;
291 }
292 }
293
294 return ACL_SUCCESS;
295 }
296
297
298
299 /**
300 * @brief Determine the number of entries in the given ACL.
301 *
302 * @param acl
303 * The ACL to inspect.
304 *
305 * @return Either the non-negative number of entries in @c acl, or
306 * @c ACL_ERROR on error.
307 */
308 int acl_entry_count(acl_t acl) {
309
310 acl_entry_t entry;
311 int entry_count = 0;
312 int result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
313
314 while (result == ACL_SUCCESS) {
315 entry_count++;
316 result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
317 }
318
319 if (result == ACL_ERROR) {
320 perror("acl_entry_count (acl_get_entry)");
321 return ACL_ERROR;
322 }
323
324 return entry_count;
325 }
326
327
328
329 /**
330 * @brief Determine whether or not the given ACL is minimal.
331 *
332 * An ACL is minimal if it has fewer than four entries.
333 *
334 * @param acl
335 * The ACL whose minimality is in question.
336 *
337 * @return
338 * - @c ACL_SUCCESS - @c acl is minimal
339 * - @c ACL_FAILURE - @c acl is not minimal
340 * - @c ACL_ERROR - Unexpected library error
341 */
342 int acl_is_minimal(acl_t acl) {
343
344 int ec = acl_entry_count(acl);
345
346 if (ec == ACL_ERROR) {
347 perror("acl_is_minimal (acl_entry_count)");
348 return ACL_ERROR;
349 }
350
351 if (ec < 4) {
352 return ACL_SUCCESS;
353 }
354 else {
355 return ACL_FAILURE;
356 }
357 }
358
359
360
361 /**
362 * @brief Determine whether the given ACL's mask denies execute.
363 *
364 * @param acl
365 * The ACL whose mask we want to check.
366 *
367 * @return
368 * - @c ACL_SUCCESS - The @c acl has a mask which denies execute.
369 * - @c ACL_FAILURE - The @c acl has a mask which does not deny execute.
370 * - @c ACL_ERROR - Unexpected library error.
371 */
372 int acl_execute_masked(acl_t acl) {
373
374 acl_entry_t entry;
375 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
376
377 while (ge_result == ACL_SUCCESS) {
378 acl_tag_t tag = ACL_UNDEFINED_TAG;
379
380 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
381 perror("acl_execute_masked (acl_get_tag_type)");
382 return ACL_ERROR;
383 }
384
385 if (tag == ACL_MASK) {
386 /* This is the mask entry, get its permissions, and see if
387 execute is specified. */
388 acl_permset_t permset;
389
390 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
391 perror("acl_execute_masked (acl_get_permset)");
392 return ACL_ERROR;
393 }
394
395 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
396 if (gp_result == ACL_ERROR) {
397 perror("acl_execute_masked (acl_get_perm)");
398 return ACL_ERROR;
399 }
400
401 if (gp_result == ACL_FAILURE) {
402 /* No execute bit set in the mask; execute not allowed. */
403 return ACL_SUCCESS;
404 }
405 }
406
407 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
408 }
409
410 return ACL_FAILURE;
411 }
412
413
414
415 /**
416 * @brief Determine whether @c fd is executable (by anyone) or a
417 * directory.
418 *
419 * This is used as part of the heuristic to determine whether or not
420 * we should mask the execute bit when inheriting an ACL. If @c fd
421 * describes a directory, the answer is a clear-cut yes. This behavior
422 * is modeled after the capital 'X' perms of setfacl.
423 *
424 * If @c fd describes a file, we check the @a effective permissions,
425 * contrary to what setfacl does.
426 *
427 * @param fd
428 * The file descriptor to check.
429 *
430 * @return
431 * - @c ACL_SUCCESS - @c fd describes a directory, or someone has effective
432 execute permissions.
433 * - @c ACL_FAILURE - @c fd describes a regular file and nobody can execute
434 it.
435 * - @c ACL_ERROR - Unexpected library error.
436 */
437 int any_can_execute_or_dir(int fd) {
438
439 if (is_directory(fd)) {
440 /* That was easy... */
441 return ACL_SUCCESS;
442 }
443
444 acl_t acl = acl_get_fd(fd);
445
446 if (acl == (acl_t)NULL) {
447 perror("any_can_execute_or_dir (acl_get_file)");
448 return ACL_ERROR;
449 }
450
451 /* Our return value. */
452 int result = ACL_FAILURE;
453
454 if (acl_is_minimal(acl)) {
455 struct stat s;
456 if (fstat(fd, &s) == -1) {
457 perror("any_can_execute_or_dir (fstat)");
458 result = ACL_ERROR;
459 goto cleanup;
460 }
461 if (s.st_mode & (S_IXUSR | S_IXOTH | S_IXGRP)) {
462 result = ACL_SUCCESS;
463 goto cleanup;
464 }
465 else {
466 result = ACL_FAILURE;
467 goto cleanup;
468 }
469 }
470
471 acl_entry_t entry;
472 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
473
474 while (ge_result == ACL_SUCCESS) {
475 /* The first thing we do is check to see if this is a mask
476 entry. If it is, we skip it entirely. */
477 acl_tag_t tag = ACL_UNDEFINED_TAG;
478
479 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
480 perror("any_can_execute_or_dir (acl_get_tag_type)");
481 result = ACL_ERROR;
482 goto cleanup;
483 }
484
485 if (tag == ACL_MASK) {
486 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
487 continue;
488 }
489
490 /* Ok, so it's not a mask entry. Check the execute perms. */
491 acl_permset_t permset;
492
493 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
494 perror("any_can_execute_or_dir (acl_get_permset)");
495 result = ACL_ERROR;
496 goto cleanup;
497 }
498
499 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
500 if (gp_result == ACL_ERROR) {
501 perror("any_can_execute_or_dir (acl_get_perm)");
502 result = ACL_ERROR;
503 goto cleanup;
504 }
505
506 if (gp_result == ACL_SUCCESS) {
507 /* Only return ACL_SUCCESS if this execute bit is not masked. */
508 if (acl_execute_masked(acl) != ACL_SUCCESS) {
509 result = ACL_SUCCESS;
510 goto cleanup;
511 }
512 }
513
514 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
515 }
516
517 if (ge_result == ACL_ERROR) {
518 perror("any_can_execute_or_dir (acl_get_entry)");
519 result = ACL_ERROR;
520 goto cleanup;
521 }
522
523 cleanup:
524 acl_free(acl);
525 return result;
526 }
527
528
529
530 /**
531 * @brief Set @c acl as the default ACL on @c path if it's a directory.
532 *
533 * This overwrites any existing default ACL on @c path. If no default
534 * ACL exists, then one is created. If @c path is not a directory, we
535 * return ACL_FAILURE but no error is raised.
536 *
537 * @param path
538 * The target directory whose ACL we wish to replace or create.
539 *
540 * @param acl
541 * The ACL to set as default on @c path.
542 *
543 * @return
544 * - @c ACL_SUCCESS - The default ACL was assigned successfully.
545 * - @c ACL_FAILURE - If @c path is not a directory.
546 * - @c ACL_ERROR - Unexpected library error.
547 */
548 int assign_default_acl(const char* path, acl_t acl) {
549
550 if (path == NULL) {
551 errno = ENOENT;
552 return ACL_ERROR;
553 }
554
555 if (!is_path_directory(path)) {
556 return ACL_FAILURE;
557 }
558
559 /* Our return value; success unless something bad happens. */
560 int result = ACL_SUCCESS;
561 acl_t path_acl = acl_dup(acl);
562
563 if (path_acl == (acl_t)NULL) {
564 perror("assign_default_acl (acl_dup)");
565 return ACL_ERROR; /* Nothing to clean up in this case. */
566 }
567
568 if (acl_set_file(path, ACL_TYPE_DEFAULT, path_acl) == ACL_ERROR) {
569 perror("assign_default_acl (acl_set_file)");
570 result = ACL_ERROR;
571 }
572
573 acl_free(path_acl);
574 return result;
575 }
576
577
578
579 /**
580 * @brief Remove all @c ACL_TYPE_ACCESS entries from the given file
581 * descriptor, leaving the UNIX permission bits.
582 *
583 * @param fd
584 * The file descriptor whose ACLs we want to wipe.
585 *
586 * @return
587 * - @c ACL_SUCCESS - The ACLs were wiped successfully, or none
588 * existed in the first place.
589 * - @c ACL_ERROR - Unexpected library error.
590 */
591 int wipe_acls(int fd) {
592 /* Initialize an empty ACL, and then overwrite the one on "fd" with it. */
593 acl_t empty_acl = acl_init(0);
594
595 if (empty_acl == (acl_t)NULL) {
596 perror("wipe_acls (acl_init)");
597 return ACL_ERROR;
598 }
599
600 if (acl_set_fd(fd, empty_acl) == ACL_ERROR) {
601 perror("wipe_acls (acl_set_fd)");
602 acl_free(empty_acl);
603 return ACL_ERROR;
604 }
605
606 acl_free(empty_acl);
607 return ACL_SUCCESS;
608 }
609
610
611
612 /**
613 * @brief Apply parent default ACL to a path.
614 *
615 * This overwrites any existing ACLs on @c path.
616 *
617 * @param path
618 * The path whose ACL we would like to reset to its default.
619 *
620 * @param no_exec_mask
621 * The value (either true or false) of the --no-exec-mask flag.
622 *
623 * @return
624 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
625 * - @c ACL_FAILURE - The target path is not a regular file/directory,
626 * or the parent of @c path is not a directory.
627 * - @c ACL_ERROR - Unexpected library error.
628 */
629 int apply_default_acl(const char* path, bool no_exec_mask) {
630
631 if (path == NULL) {
632 errno = ENOENT;
633 return ACL_ERROR;
634 }
635
636 /* Define these next three variables here because we may have to
637 * jump to the cleanup routine which expects them to exist.
638 */
639
640 /* Our return value. */
641 int result = ACL_SUCCESS;
642
643 /* The default ACL on path's parent directory */
644 acl_t defacl = (acl_t)NULL;
645
646 /* The file descriptor corresponding to "path" */
647 int fd = 0;
648
649 /* Split "path" into base/dirname parts to be used with openat().
650 * We duplicate the strings involved because dirname/basename mangle
651 * their arguments.
652 */
653 char* path_copy = strdup(path);
654 if (path_copy == NULL) {
655 perror("apply_default_acl (strdup)");
656 return ACL_ERROR;
657 }
658 char* parent = dirname(path_copy);
659
660 fd = open(path, O_NOFOLLOW);
661 if (fd == -1) {
662 if (errno == ELOOP) {
663 result = ACL_FAILURE; /* hit a symlink */
664 goto cleanup;
665 }
666 else {
667 perror("apply_default_acl (open fd)");
668 result = ACL_ERROR;
669 goto cleanup;
670 }
671 }
672
673
674 /* Refuse to operate on hard links, which can be abused by an
675 * attacker to trick us into changing the ACL on a file we didn't
676 * intend to; namely the "target" of the hard link. There is TOCTOU
677 * race condition here, but the window is as small as possible
678 * between when we open the file descriptor (look above) and when we
679 * fstat it.
680 */
681 if (!is_hardlink_safe(fd)) {
682 result = ACL_FAILURE;
683 goto cleanup;
684 }
685
686 if (!is_regular_file(fd) && !is_directory(fd)) {
687 result = ACL_FAILURE;
688 goto cleanup;
689 }
690
691 /* Default to not masking the exec bit; i.e. applying the default
692 ACL literally. If --no-exec-mask was not specified, then we try
693 to "guess" whether or not to mask the exec bit. */
694 bool allow_exec = true;
695
696 if (!no_exec_mask) {
697 int ace_result = any_can_execute_or_dir(fd);
698
699 if (ace_result == ACL_ERROR) {
700 perror("apply_default_acl (any_can_execute_or_dir)");
701 result = ACL_ERROR;
702 goto cleanup;
703 }
704
705 allow_exec = (bool)ace_result;
706 }
707
708 defacl = acl_get_file(parent, ACL_TYPE_DEFAULT);
709
710 if (defacl == (acl_t)NULL) {
711 perror("apply_default_acl (acl_get_file)");
712 result = ACL_ERROR;
713 goto cleanup;
714 }
715
716 if (wipe_acls(fd) == ACL_ERROR) {
717 perror("apply_default_acl (wipe_acls)");
718 result = ACL_ERROR;
719 goto cleanup;
720 }
721
722 /* Do this after wipe_acls(), otherwise we'll overwrite the wiped
723 ACL with this one. */
724 acl_t acl = acl_get_fd(fd);
725 if (acl == (acl_t)NULL) {
726 perror("apply_default_acl (acl_get_fd)");
727 result = ACL_ERROR;
728 goto cleanup;
729 }
730
731 /* If it's a directory, inherit the parent's default. */
732 if (assign_default_acl(path, defacl) == ACL_ERROR) {
733 perror("apply_default_acl (assign_default_acl)");
734 result = ACL_ERROR;
735 goto cleanup;
736 }
737
738 acl_entry_t entry;
739 int ge_result = acl_get_entry(defacl, ACL_FIRST_ENTRY, &entry);
740
741 while (ge_result == ACL_SUCCESS) {
742 acl_tag_t tag = ACL_UNDEFINED_TAG;
743
744 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
745 perror("apply_default_acl (acl_get_tag_type)");
746 result = ACL_ERROR;
747 goto cleanup;
748 }
749
750
751 /* We've got an entry/tag from the default ACL. Get its permset. */
752 acl_permset_t permset;
753 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
754 perror("apply_default_acl (acl_get_permset)");
755 result = ACL_ERROR;
756 goto cleanup;
757 }
758
759 /* If this is a default mask, fix it up. */
760 if (tag == ACL_MASK ||
761 tag == ACL_USER_OBJ ||
762 tag == ACL_GROUP_OBJ ||
763 tag == ACL_OTHER) {
764
765 if (!allow_exec) {
766 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
767 minimal ACLs) or acl_other entries, so if execute should be
768 masked, we have to do it manually. */
769 if (acl_delete_perm(permset, ACL_EXECUTE) == ACL_ERROR) {
770 perror("apply_default_acl (acl_delete_perm)");
771 result = ACL_ERROR;
772 goto cleanup;
773 }
774
775 if (acl_set_permset(entry, permset) == ACL_ERROR) {
776 perror("apply_default_acl (acl_set_permset)");
777 result = ACL_ERROR;
778 goto cleanup;
779 }
780 }
781 }
782
783 /* Finally, add the permset to the access ACL. It's actually
784 * important that we pass in the address of "acl" here, and not
785 * "acl" itself. Why? The call to acl_create_entry() within
786 * acl_set_entry() can allocate new memory for the entry.
787 * Sometimes that can be done in-place, in which case everything
788 * is cool and the new memory gets released when we call
789 * acl_free(acl).
790 *
791 * But occasionally, the whole ACL structure will have to be moved
792 * in order to allocate the extra space. When that happens,
793 * acl_create_entry() modifies the pointer it was passed (in this
794 * case, &acl) to point to the new location. We want to call
795 * acl_free() on the new location, and since acl_free() gets
796 * called right here, we need acl_create_entry() to update the
797 * value of "acl". To do that, it needs the address of "acl".
798 */
799 if (acl_set_entry(&acl, entry) == ACL_ERROR) {
800 perror("apply_default_acl (acl_set_entry)");
801 result = ACL_ERROR;
802 goto cleanup;
803 }
804
805 ge_result = acl_get_entry(defacl, ACL_NEXT_ENTRY, &entry);
806 }
807
808 /* Catches the first acl_get_entry as well as the ones at the end of
809 the loop. */
810 if (ge_result == ACL_ERROR) {
811 perror("apply_default_acl (acl_get_entry)");
812 result = ACL_ERROR;
813 goto cleanup;
814 }
815
816 if (acl_set_fd(fd, acl) == ACL_ERROR) {
817 perror("apply_default_acl (acl_set_fd)");
818 result = ACL_ERROR;
819 goto cleanup;
820 }
821
822 cleanup:
823 free(path_copy);
824 if (defacl != (acl_t)NULL) {
825 acl_free(defacl);
826 }
827 if (fd >= 0 && close(fd) == -1) {
828 perror("apply_default_acl (close)");
829 result = ACL_ERROR;
830 }
831 return result;
832 }
833
834
835
836 /**
837 * @brief Display program usage information.
838 *
839 * @param program_name
840 * The program name to use in the output.
841 *
842 */
843 void usage(const char* program_name) {
844 printf("Apply any applicable default ACLs to the given files or "
845 "directories.\n\n");
846 printf("Usage: %s [flags] <target1> [<target2> [ <target3>...]]\n\n",
847 program_name);
848 printf("Flags:\n");
849 printf(" -h, --help Print this help message\n");
850 printf(" -r, --recursive Act on any given directories recursively\n");
851 printf(" -x, --no-exec-mask Apply execute permissions unconditionally\n");
852
853 return;
854 }
855
856
857 /**
858 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
859 *
860 * For parameter information, see the @c nftw man page.
861 *
862 * @return If the ACL was applied to @c target successfully, we return
863 * @c FTW_CONTINUE to signal to @ nftw() that we should proceed onto
864 * the next file or directory. Otherwise, we return @c FTW_STOP to
865 * signal failure.
866 *
867 */
868 int apply_default_acl_nftw(const char *target,
869 const struct stat *s,
870 int info,
871 struct FTW *ftw) {
872
873 if (apply_default_acl(target, false)) {
874 return FTW_CONTINUE;
875 }
876 else {
877 return FTW_STOP;
878 }
879 }
880
881
882
883 /**
884 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
885 *
886 * This is identical to @c apply_default_acl_nftw(), except it passes
887 * @c true to @c apply_default_acl() as its no_exec_mask argument.
888 *
889 */
890 int apply_default_acl_nftw_x(const char *target,
891 const struct stat *s,
892 int info,
893 struct FTW *ftw) {
894
895 if (apply_default_acl(target, true)) {
896 return FTW_CONTINUE;
897 }
898 else {
899 return FTW_STOP;
900 }
901 }
902
903
904
905 /**
906 * @brief Recursive version of @c apply_default_acl().
907 *
908 * If @c target is a directory, we use @c nftw() to call @c
909 * apply_default_acl() recursively on all of its children. Otherwise,
910 * we just delegate to @c apply_default_acl().
911 *
912 * We ignore symlinks for consistency with chmod -r.
913 *
914 * @param target
915 * The root (path) of the recursive application.
916 *
917 * @param no_exec_mask
918 * The value (either true or false) of the --no-exec-mask flag.
919 *
920 * @return
921 * If @c target is not a directory, we return the result of
922 * calling @c apply_default_acl() on @c target. Otherwise, we convert
923 * the return value of @c nftw(). If @c nftw() succeeds (returns 0),
924 * then we return @c true. Otherwise, we return @c false.
925 * \n\n
926 * If there is an error, it will be reported via @c perror, but
927 * we still return @c false.
928 */
929 bool apply_default_acl_recursive(const char *target, bool no_exec_mask) {
930
931 if (!is_path_directory(target)) {
932 return apply_default_acl(target, no_exec_mask);
933 }
934
935 int max_levels = 256;
936 int flags = FTW_PHYS; /* Don't follow links. */
937
938 /* There are two separate functions that could be passed to
939 nftw(). One passes no_exec_mask = true to apply_default_acl(),
940 and the other passes no_exec_mask = false. Since the function we
941 pass to nftw() cannot have parameters, we have to create separate
942 options and make the decision here. */
943 int (*fn)(const char *, const struct stat *, int, struct FTW *) = NULL;
944 fn = no_exec_mask ? apply_default_acl_nftw_x : apply_default_acl_nftw;
945
946 int nftw_result = nftw(target, fn, max_levels, flags);
947
948 if (nftw_result == 0) {
949 /* Success */
950 return true;
951 }
952
953 /* nftw will return -1 on error, or if the supplied function
954 * (apply_default_acl_nftw) returns a non-zero result, nftw will
955 * return that.
956 */
957 if (nftw_result == -1) {
958 perror("apply_default_acl_recursive (nftw)");
959 }
960
961 return false;
962 }
963
964
965
966 /**
967 * @brief Call apply_default_acl (possibly recursively) on each
968 * command-line argument.
969 *
970 * @return Either @c EXIT_FAILURE or @c EXIT_SUCCESS. If everything
971 * goes as expected, we return @c EXIT_SUCCESS. Otherwise, we return
972 * @c EXIT_FAILURE.
973 */
974 int main(int argc, char* argv[]) {
975
976 if (argc < 2) {
977 usage(argv[0]);
978 return EXIT_FAILURE;
979 }
980
981 bool recursive = false;
982 bool no_exec_mask = false;
983
984 struct option long_options[] = {
985 /* These options set a flag. */
986 {"help", no_argument, NULL, 'h'},
987 {"recursive", no_argument, NULL, 'r'},
988 {"no-exec-mask", no_argument, NULL, 'x'},
989 {NULL, 0, NULL, 0}
990 };
991
992 int opt = 0;
993
994 while ((opt = getopt_long(argc, argv, "hrx", long_options, NULL)) != -1) {
995 switch (opt) {
996 case 'h':
997 usage(argv[0]);
998 return EXIT_SUCCESS;
999 case 'r':
1000 recursive = true;
1001 break;
1002 case 'x':
1003 no_exec_mask = true;
1004 break;
1005 default:
1006 usage(argv[0]);
1007 return EXIT_FAILURE;
1008 }
1009 }
1010
1011 int result = EXIT_SUCCESS;
1012
1013 int arg_index = 1;
1014 for (arg_index = optind; arg_index < argc; arg_index++) {
1015 const char* target = argv[arg_index];
1016 bool reapp_result = false;
1017
1018 /* Make sure we can access the given path before we go out of our
1019 * way to please it. Doing this check outside of
1020 * apply_default_acl() lets us spit out a better error message for
1021 * typos, too.
1022 */
1023 if (!path_accessible(target)) {
1024 fprintf(stderr, "%s: %s: No such file or directory\n", argv[0], target);
1025 result = EXIT_FAILURE;
1026 continue;
1027 }
1028
1029 if (recursive) {
1030 reapp_result = apply_default_acl_recursive(target, no_exec_mask);
1031 }
1032 else {
1033 /* It's either a normal file, or we're not operating recursively. */
1034 reapp_result = apply_default_acl(target, no_exec_mask);
1035 }
1036
1037 if (!reapp_result) {
1038 result = EXIT_FAILURE;
1039 }
1040 }
1041
1042 return result;
1043 }