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