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