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