2 * @file apply-default-acl.c
4 * @brief The command-line interface.
8 /* On Linux, ftw.h needs this special voodoo to work. */
9 #define _XOPEN_SOURCE 500
12 #include <fcntl.h> /* AT_FOO constants */
13 #include <ftw.h> /* nftw() et al. */
14 #include <getopt.h> /* getopt_long() */
15 #include <stdbool.h> /* the "bool" type */
16 #include <stdio.h> /* perror() */
17 #include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
18 #include <unistd.h> /* faccessat() */
22 /* We exit with EXIT_FAILURE for small errors, but we need something
23 * else for big ones. */
30 * @brief Determine whether or not the given path is accessible.
35 * @return true if @c path is accessible to the current effective
36 * user/group, false otherwise.
38 bool path_accessible(const char* path
) {
43 /* Test for access using the effective user and group rather than
45 int flags
= AT_EACCESS
;
47 /* Don't follow symlinks when checking for a path's existence,
48 since we won't follow them to set its ACLs either. */
49 flags
|= AT_SYMLINK_NOFOLLOW
;
51 /* If the path is relative, interpret it relative to the current
52 working directory (just like the access() system call). */
53 if (faccessat(AT_FDCWD
, path
, F_OK
, flags
) == 0) {
63 * @brief Display program usage information.
66 * The program name to use in the output.
69 void usage(const char* program_name
) {
70 printf("Apply any applicable default ACLs to the given files or "
72 printf("Usage: %s [flags] <target1> [<target2> [ <target3>...]]\n\n",
75 printf(" -h, --help Print this help message\n");
76 printf(" -r, --recursive Act on any given directories recursively\n");
77 printf(" -x, --no-exec-mask Apply execute permissions unconditionally\n");
84 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
86 * For parameter information, see the @c nftw man page.
88 * @return If the ACL was applied to @c target successfully, we return
89 * @c FTW_CONTINUE to signal to @ nftw() that we should proceed onto
90 * the next file or directory. Otherwise, we return @c FTW_STOP to
94 int apply_default_acl_nftw(const char *target
,
95 const struct stat
*sp
,
99 if (apply_default_acl_ex(target
, sp
, false) == ACL_ERROR
) {
100 /* I guess we do want to bail out for serious/unexpected errors? */
104 /* We don't want to kill the tree walk because we it a symlink. */
111 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
113 * This is identical to @c apply_default_acl_nftw(), except it passes
114 * @c true to @c apply_default_acl() as its no_exec_mask argument.
117 int apply_default_acl_nftw_x(const char *target
,
118 const struct stat
*sp
,
122 if (apply_default_acl_ex(target
, sp
, true) == ACL_ERROR
) {
123 /* I guess we do want to bail out for serious/unexpected errors? */
127 /* We don't want to kill the tree walk because we it a symlink. */
134 * @brief Recursive version of @c apply_default_acl().
136 * If @c target is a directory, we use @c nftw() to call @c
137 * apply_default_acl() recursively on all of its children. Otherwise,
138 * we just delegate to @c apply_default_acl().
141 * The root (path) of the recursive application.
143 * @param no_exec_mask
144 * The value (either true or false) of the --no-exec-mask flag.
147 * If @c nftw() fails with a serious error (returns NFTW_ERROR),
148 * then we return @c ACL_ERROR. Otherwise, we return @c ACL_SUCCESS.
150 int apply_default_acl_recursive(const char *target
, bool no_exec_mask
) {
151 int max_levels
= 256;
152 int flags
= FTW_MOUNT
| FTW_PHYS
;
154 /* There are two separate functions that could be passed to
155 nftw(). One passes no_exec_mask = true to apply_default_acl(),
156 and the other passes no_exec_mask = false. Since the function we
157 pass to nftw() cannot have parameters, we have to create separate
158 options and make the decision here. */
159 int (*fn
)(const char *, const struct stat
*, int, struct FTW
*) = NULL
;
160 fn
= no_exec_mask
? apply_default_acl_nftw_x
: apply_default_acl_nftw
;
162 int nftw_result
= nftw(target
, fn
, max_levels
, flags
);
164 /* nftw will itself return NFTW_ERROR on errors like malloc failure,
165 and since the only non-success value that "fn" can return us
166 ACL_ERROR == NFTW_ERROR, this covers all error cases. */
167 if (nftw_result
== NFTW_ERROR
) {
168 perror("apply_default_acl_recursive (nftw)");
172 /* Beware: nftw indicates success with 0, but ACL_SUCCESS != 0. */
179 * @brief Call apply_default_acl (possibly recursively) on each
180 * command-line argument.
182 * @return Either @c EXIT_FAILURE or @c EXIT_SUCCESS. If everything
183 * goes as expected, we return @c EXIT_SUCCESS. Otherwise, we return
186 int main(int argc
, char* argv
[]) {
193 bool recursive
= false;
194 bool no_exec_mask
= false;
196 struct option long_options
[] = {
197 /* These options set a flag. */
198 {"help", no_argument
, NULL
, 'h'},
199 {"recursive", no_argument
, NULL
, 'r'},
200 {"no-exec-mask", no_argument
, NULL
, 'x'},
206 while ((opt
= getopt_long(argc
, argv
, "hrx", long_options
, NULL
)) != -1) {
223 int result
= EXIT_SUCCESS
;
226 for (arg_index
= optind
; arg_index
< argc
; arg_index
++) {
227 const char* target
= argv
[arg_index
];
229 /* Make sure we can access the given path before we go out of our
230 * way to please it. Doing this check outside of
231 * apply_default_acl() lets us spit out a better error message for
234 if (!path_accessible(target
)) {
235 fprintf(stderr
, "%s: %s: No such file or directory\n", argv
[0], target
);
236 result
= EXIT_FAILURE
;
240 int (*f
)(const char *, bool) = recursive
? apply_default_acl_recursive
242 int reapp_result
= f(target
, no_exec_mask
);
244 if (result
== EXIT_SUCCESS
&& reapp_result
== ACL_FAILURE
) {
245 /* We don't want to turn an error into a (less-severe) failure. */
246 result
= EXIT_FAILURE
;
248 if (reapp_result
== ACL_ERROR
) {
249 /* Turn both success and failure into an error, if we encounter one. */