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