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