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