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