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