]> gitweb.michael.orlitzky.com - apply-default-acl.git/blob - src/apply-default-acl.c
Move the "or_dir" out of any_can_execute_or_dir().
[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.
367 *
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 file, we check the @a effective permissions, contrary
372 * to what setfacl does.
373 *
374 * @param fd
375 * The file descriptor to check.
376 *
377 * @return
378 * - @c ACL_SUCCESS - Someone has effective execute permissions on @c fd.
379 * - @c ACL_FAILURE - Nobody can execute @c fd.
380 * - @c ACL_ERROR - Unexpected library error.
381 */
382 int any_can_execute(int fd) {
383 acl_t acl = acl_get_fd(fd);
384
385 if (acl == (acl_t)NULL) {
386 perror("any_can_execute (acl_get_file)");
387 return ACL_ERROR;
388 }
389
390 /* Our return value. */
391 int result = ACL_FAILURE;
392
393 if (acl_is_minimal(acl)) {
394 struct stat s;
395 if (fstat(fd, &s) == -1) {
396 perror("any_can_execute (fstat)");
397 result = ACL_ERROR;
398 goto cleanup;
399 }
400 if (s.st_mode & (S_IXUSR | S_IXOTH | S_IXGRP)) {
401 result = ACL_SUCCESS;
402 goto cleanup;
403 }
404 else {
405 result = ACL_FAILURE;
406 goto cleanup;
407 }
408 }
409
410 acl_entry_t entry;
411 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
412
413 while (ge_result == ACL_SUCCESS) {
414 /* The first thing we do is check to see if this is a mask
415 entry. If it is, we skip it entirely. */
416 acl_tag_t tag = ACL_UNDEFINED_TAG;
417
418 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
419 perror("any_can_execute_or (acl_get_tag_type)");
420 result = ACL_ERROR;
421 goto cleanup;
422 }
423
424 if (tag == ACL_MASK) {
425 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
426 continue;
427 }
428
429 /* Ok, so it's not a mask entry. Check the execute perms. */
430 acl_permset_t permset;
431
432 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
433 perror("any_can_execute_or (acl_get_permset)");
434 result = ACL_ERROR;
435 goto cleanup;
436 }
437
438 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
439 if (gp_result == ACL_ERROR) {
440 perror("any_can_execute (acl_get_perm)");
441 result = ACL_ERROR;
442 goto cleanup;
443 }
444
445 if (gp_result == ACL_SUCCESS) {
446 /* Only return ACL_SUCCESS if this execute bit is not masked. */
447 if (acl_execute_masked(acl) != ACL_SUCCESS) {
448 result = ACL_SUCCESS;
449 goto cleanup;
450 }
451 }
452
453 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
454 }
455
456 if (ge_result == ACL_ERROR) {
457 perror("any_can_execute (acl_get_entry)");
458 result = ACL_ERROR;
459 goto cleanup;
460 }
461
462 cleanup:
463 acl_free(acl);
464 return result;
465 }
466
467
468
469 /**
470 * @brief Set @c acl as the default ACL on @c path if it's a directory.
471 *
472 * This overwrites any existing default ACL on @c path. If no default
473 * ACL exists, then one is created. If @c path is not a directory, we
474 * return ACL_FAILURE but no error is raised.
475 *
476 * @param path
477 * The target directory whose ACL we wish to replace or create.
478 *
479 * @param acl
480 * The ACL to set as default on @c path.
481 *
482 * @return
483 * - @c ACL_SUCCESS - The default ACL was assigned successfully.
484 * - @c ACL_FAILURE - If @c path is not a directory.
485 * - @c ACL_ERROR - Unexpected library error.
486 */
487 int assign_default_acl(const char* path, acl_t acl) {
488
489 if (path == NULL) {
490 errno = ENOENT;
491 return ACL_ERROR;
492 }
493
494 if (!is_path_directory(path)) {
495 return ACL_FAILURE;
496 }
497
498 /* Our return value; success unless something bad happens. */
499 int result = ACL_SUCCESS;
500 acl_t path_acl = acl_dup(acl);
501
502 if (path_acl == (acl_t)NULL) {
503 perror("assign_default_acl (acl_dup)");
504 return ACL_ERROR; /* Nothing to clean up in this case. */
505 }
506
507 if (acl_set_file(path, ACL_TYPE_DEFAULT, path_acl) == ACL_ERROR) {
508 perror("assign_default_acl (acl_set_file)");
509 result = ACL_ERROR;
510 }
511
512 acl_free(path_acl);
513 return result;
514 }
515
516
517
518 /**
519 * @brief Remove all @c ACL_TYPE_ACCESS entries from the given file
520 * descriptor, leaving the UNIX permission bits.
521 *
522 * @param fd
523 * The file descriptor whose ACLs we want to wipe.
524 *
525 * @return
526 * - @c ACL_SUCCESS - The ACLs were wiped successfully, or none
527 * existed in the first place.
528 * - @c ACL_ERROR - Unexpected library error.
529 */
530 int wipe_acls(int fd) {
531 /* Initialize an empty ACL, and then overwrite the one on "fd" with it. */
532 acl_t empty_acl = acl_init(0);
533
534 if (empty_acl == (acl_t)NULL) {
535 perror("wipe_acls (acl_init)");
536 return ACL_ERROR;
537 }
538
539 if (acl_set_fd(fd, empty_acl) == ACL_ERROR) {
540 perror("wipe_acls (acl_set_fd)");
541 acl_free(empty_acl);
542 return ACL_ERROR;
543 }
544
545 acl_free(empty_acl);
546 return ACL_SUCCESS;
547 }
548
549
550
551 /**
552 * @brief Apply parent default ACL to a path.
553 *
554 * This overwrites any existing ACLs on @c path.
555 *
556 * @param path
557 * The path whose ACL we would like to reset to its default.
558 *
559 * @param no_exec_mask
560 * The value (either true or false) of the --no-exec-mask flag.
561 *
562 * @return
563 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
564 * - @c ACL_FAILURE - The target path is not a regular file/directory,
565 * or the parent of @c path is not a directory.
566 * - @c ACL_ERROR - Unexpected library error.
567 */
568 int apply_default_acl(const char* path, bool no_exec_mask) {
569
570 if (path == NULL) {
571 errno = EINVAL;
572 perror("apply_default_acl (args)");
573 return ACL_ERROR;
574 }
575
576 /* Define these next three variables here because we may have to
577 * jump to the cleanup routine which expects them to exist.
578 */
579
580 /* Our return value. */
581 int result = ACL_SUCCESS;
582
583 /* The default ACL on path's parent directory */
584 acl_t defacl = (acl_t)NULL;
585
586 /* The file descriptor corresponding to "path" */
587 int fd = 0;
588
589 /* Split "path" into base/dirname parts to be used with openat().
590 * We duplicate the strings involved because dirname/basename mangle
591 * their arguments.
592 */
593 char* path_copy = strdup(path);
594 if (path_copy == NULL) {
595 perror("apply_default_acl (strdup)");
596 return ACL_ERROR;
597 }
598 char* parent = dirname(path_copy);
599
600 fd = open(path, O_NOFOLLOW);
601 if (fd == -1) {
602 if (errno == ELOOP) {
603 result = ACL_FAILURE; /* hit a symlink */
604 goto cleanup;
605 }
606 else {
607 perror("apply_default_acl (open fd)");
608 result = ACL_ERROR;
609 goto cleanup;
610 }
611 }
612
613
614 /* Refuse to operate on hard links, which can be abused by an
615 * attacker to trick us into changing the ACL on a file we didn't
616 * intend to; namely the "target" of the hard link. There is TOCTOU
617 * race condition here, but the window is as small as possible
618 * between when we open the file descriptor (look above) and when we
619 * fstat it.
620 */
621 struct stat s;
622 if (fstat(fd, &s) == -1) {
623 perror("apply_default_acl (fstat)");
624 goto cleanup;
625 }
626 if (!S_ISDIR(s.st_mode)) {
627 /* If it's not a directory, make sure it's a regular,
628 non-hard-linked file. */
629 if (!S_ISREG(s.st_mode) || s.st_nlink != 1) {
630 result = ACL_FAILURE;
631 goto cleanup;
632 }
633 }
634
635
636 /* Default to not masking the exec bit; i.e. applying the default
637 ACL literally. If --no-exec-mask was not specified, then we try
638 to "guess" whether or not to mask the exec bit. This behavior
639 is modeled after the capital 'X' perms of setfacl. */
640 bool allow_exec = true;
641
642 if (!no_exec_mask) {
643 /* Never mask the execute bit on directories. */
644 int ace_result = any_can_execute(fd) || is_directory(fd);
645
646 if (ace_result == ACL_ERROR) {
647 perror("apply_default_acl (any_can_execute)");
648 result = ACL_ERROR;
649 goto cleanup;
650 }
651
652 allow_exec = (bool)ace_result;
653 }
654
655 defacl = acl_get_file(parent, ACL_TYPE_DEFAULT);
656
657 if (defacl == (acl_t)NULL) {
658 perror("apply_default_acl (acl_get_file)");
659 result = ACL_ERROR;
660 goto cleanup;
661 }
662
663 if (wipe_acls(fd) == ACL_ERROR) {
664 perror("apply_default_acl (wipe_acls)");
665 result = ACL_ERROR;
666 goto cleanup;
667 }
668
669 /* Do this after wipe_acls(), otherwise we'll overwrite the wiped
670 ACL with this one. */
671 acl_t acl = acl_get_fd(fd);
672 if (acl == (acl_t)NULL) {
673 perror("apply_default_acl (acl_get_fd)");
674 result = ACL_ERROR;
675 goto cleanup;
676 }
677
678 /* If it's a directory, inherit the parent's default. */
679 if (assign_default_acl(path, defacl) == ACL_ERROR) {
680 perror("apply_default_acl (assign_default_acl)");
681 result = ACL_ERROR;
682 goto cleanup;
683 }
684
685 acl_entry_t entry;
686 int ge_result = acl_get_entry(defacl, ACL_FIRST_ENTRY, &entry);
687
688 while (ge_result == ACL_SUCCESS) {
689 acl_tag_t tag = ACL_UNDEFINED_TAG;
690
691 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
692 perror("apply_default_acl (acl_get_tag_type)");
693 result = ACL_ERROR;
694 goto cleanup;
695 }
696
697
698 /* We've got an entry/tag from the default ACL. Get its permset. */
699 acl_permset_t permset;
700 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
701 perror("apply_default_acl (acl_get_permset)");
702 result = ACL_ERROR;
703 goto cleanup;
704 }
705
706 /* If this is a default mask, fix it up. */
707 if (tag == ACL_MASK ||
708 tag == ACL_USER_OBJ ||
709 tag == ACL_GROUP_OBJ ||
710 tag == ACL_OTHER) {
711
712 if (!allow_exec) {
713 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
714 minimal ACLs) or acl_other entries, so if execute should be
715 masked, we have to do it manually. */
716 if (acl_delete_perm(permset, ACL_EXECUTE) == ACL_ERROR) {
717 perror("apply_default_acl (acl_delete_perm)");
718 result = ACL_ERROR;
719 goto cleanup;
720 }
721
722 if (acl_set_permset(entry, permset) == ACL_ERROR) {
723 perror("apply_default_acl (acl_set_permset)");
724 result = ACL_ERROR;
725 goto cleanup;
726 }
727 }
728 }
729
730 /* Finally, add the permset to the access ACL. It's actually
731 * important that we pass in the address of "acl" here, and not
732 * "acl" itself. Why? The call to acl_create_entry() within
733 * acl_set_entry() can allocate new memory for the entry.
734 * Sometimes that can be done in-place, in which case everything
735 * is cool and the new memory gets released when we call
736 * acl_free(acl).
737 *
738 * But occasionally, the whole ACL structure will have to be moved
739 * in order to allocate the extra space. When that happens,
740 * acl_create_entry() modifies the pointer it was passed (in this
741 * case, &acl) to point to the new location. We want to call
742 * acl_free() on the new location, and since acl_free() gets
743 * called right here, we need acl_create_entry() to update the
744 * value of "acl". To do that, it needs the address of "acl".
745 */
746 if (acl_set_entry(&acl, entry) == ACL_ERROR) {
747 perror("apply_default_acl (acl_set_entry)");
748 result = ACL_ERROR;
749 goto cleanup;
750 }
751
752 ge_result = acl_get_entry(defacl, ACL_NEXT_ENTRY, &entry);
753 }
754
755 /* Catches the first acl_get_entry as well as the ones at the end of
756 the loop. */
757 if (ge_result == ACL_ERROR) {
758 perror("apply_default_acl (acl_get_entry)");
759 result = ACL_ERROR;
760 goto cleanup;
761 }
762
763 if (acl_set_fd(fd, acl) == ACL_ERROR) {
764 perror("apply_default_acl (acl_set_fd)");
765 result = ACL_ERROR;
766 goto cleanup;
767 }
768
769 cleanup:
770 free(path_copy);
771 if (defacl != (acl_t)NULL) {
772 acl_free(defacl);
773 }
774 if (fd >= 0 && close(fd) == -1) {
775 perror("apply_default_acl (close)");
776 result = ACL_ERROR;
777 }
778 return result;
779 }
780
781
782
783 /**
784 * @brief Display program usage information.
785 *
786 * @param program_name
787 * The program name to use in the output.
788 *
789 */
790 void usage(const char* program_name) {
791 printf("Apply any applicable default ACLs to the given files or "
792 "directories.\n\n");
793 printf("Usage: %s [flags] <target1> [<target2> [ <target3>...]]\n\n",
794 program_name);
795 printf("Flags:\n");
796 printf(" -h, --help Print this help message\n");
797 printf(" -r, --recursive Act on any given directories recursively\n");
798 printf(" -x, --no-exec-mask Apply execute permissions unconditionally\n");
799
800 return;
801 }
802
803
804 /**
805 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
806 *
807 * For parameter information, see the @c nftw man page.
808 *
809 * @return If the ACL was applied to @c target successfully, we return
810 * @c FTW_CONTINUE to signal to @ nftw() that we should proceed onto
811 * the next file or directory. Otherwise, we return @c FTW_STOP to
812 * signal failure.
813 *
814 */
815 int apply_default_acl_nftw(const char *target,
816 const struct stat *s,
817 int info,
818 struct FTW *ftw) {
819
820 if (apply_default_acl(target, false)) {
821 return FTW_CONTINUE;
822 }
823 else {
824 return FTW_STOP;
825 }
826 }
827
828
829
830 /**
831 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
832 *
833 * This is identical to @c apply_default_acl_nftw(), except it passes
834 * @c true to @c apply_default_acl() as its no_exec_mask argument.
835 *
836 */
837 int apply_default_acl_nftw_x(const char *target,
838 const struct stat *s,
839 int info,
840 struct FTW *ftw) {
841
842 if (apply_default_acl(target, true)) {
843 return FTW_CONTINUE;
844 }
845 else {
846 return FTW_STOP;
847 }
848 }
849
850
851
852 /**
853 * @brief Recursive version of @c apply_default_acl().
854 *
855 * If @c target is a directory, we use @c nftw() to call @c
856 * apply_default_acl() recursively on all of its children. Otherwise,
857 * we just delegate to @c apply_default_acl().
858 *
859 * We ignore symlinks for consistency with chmod -r.
860 *
861 * @param target
862 * The root (path) of the recursive application.
863 *
864 * @param no_exec_mask
865 * The value (either true or false) of the --no-exec-mask flag.
866 *
867 * @return
868 * If @c target is not a directory, we return the result of
869 * calling @c apply_default_acl() on @c target. Otherwise, we convert
870 * the return value of @c nftw(). If @c nftw() succeeds (returns 0),
871 * then we return @c true. Otherwise, we return @c false.
872 * \n\n
873 * If there is an error, it will be reported via @c perror, but
874 * we still return @c false.
875 */
876 bool apply_default_acl_recursive(const char *target, bool no_exec_mask) {
877
878 if (!is_path_directory(target)) {
879 return apply_default_acl(target, no_exec_mask);
880 }
881
882 int max_levels = 256;
883 int flags = FTW_PHYS; /* Don't follow links. */
884
885 /* There are two separate functions that could be passed to
886 nftw(). One passes no_exec_mask = true to apply_default_acl(),
887 and the other passes no_exec_mask = false. Since the function we
888 pass to nftw() cannot have parameters, we have to create separate
889 options and make the decision here. */
890 int (*fn)(const char *, const struct stat *, int, struct FTW *) = NULL;
891 fn = no_exec_mask ? apply_default_acl_nftw_x : apply_default_acl_nftw;
892
893 int nftw_result = nftw(target, fn, max_levels, flags);
894
895 if (nftw_result == 0) {
896 /* Success */
897 return true;
898 }
899
900 /* nftw will return -1 on error, or if the supplied function
901 * (apply_default_acl_nftw) returns a non-zero result, nftw will
902 * return that.
903 */
904 if (nftw_result == -1) {
905 perror("apply_default_acl_recursive (nftw)");
906 }
907
908 return false;
909 }
910
911
912
913 /**
914 * @brief Call apply_default_acl (possibly recursively) on each
915 * command-line argument.
916 *
917 * @return Either @c EXIT_FAILURE or @c EXIT_SUCCESS. If everything
918 * goes as expected, we return @c EXIT_SUCCESS. Otherwise, we return
919 * @c EXIT_FAILURE.
920 */
921 int main(int argc, char* argv[]) {
922
923 if (argc < 2) {
924 usage(argv[0]);
925 return EXIT_FAILURE;
926 }
927
928 bool recursive = false;
929 bool no_exec_mask = false;
930
931 struct option long_options[] = {
932 /* These options set a flag. */
933 {"help", no_argument, NULL, 'h'},
934 {"recursive", no_argument, NULL, 'r'},
935 {"no-exec-mask", no_argument, NULL, 'x'},
936 {NULL, 0, NULL, 0}
937 };
938
939 int opt = 0;
940
941 while ((opt = getopt_long(argc, argv, "hrx", long_options, NULL)) != -1) {
942 switch (opt) {
943 case 'h':
944 usage(argv[0]);
945 return EXIT_SUCCESS;
946 case 'r':
947 recursive = true;
948 break;
949 case 'x':
950 no_exec_mask = true;
951 break;
952 default:
953 usage(argv[0]);
954 return EXIT_FAILURE;
955 }
956 }
957
958 int result = EXIT_SUCCESS;
959
960 int arg_index = 1;
961 for (arg_index = optind; arg_index < argc; arg_index++) {
962 const char* target = argv[arg_index];
963 bool reapp_result = false;
964
965 /* Make sure we can access the given path before we go out of our
966 * way to please it. Doing this check outside of
967 * apply_default_acl() lets us spit out a better error message for
968 * typos, too.
969 */
970 if (!path_accessible(target)) {
971 fprintf(stderr, "%s: %s: No such file or directory\n", argv[0], target);
972 result = EXIT_FAILURE;
973 continue;
974 }
975
976 if (recursive) {
977 reapp_result = apply_default_acl_recursive(target, no_exec_mask);
978 }
979 else {
980 /* It's either a normal file, or we're not operating recursively. */
981 reapp_result = apply_default_acl(target, no_exec_mask);
982 }
983
984 if (!reapp_result) {
985 result = EXIT_FAILURE;
986 }
987 }
988
989 return result;
990 }