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