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