]>
gitweb.michael.orlitzky.com - apply-default-acl.git/blob - apply-default-acl.c
e5b989ad9a585fe6224f16da93e969b34d07358c
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() */
25 * @brief Determine whether or not the given path is accessible.
30 * @return true if @c path is accessible to the current effective
31 * user/group, false otherwise.
33 bool path_accessible(const char* path
) {
38 /* Test for access using the effective user and group rather than
40 int flags
= AT_EACCESS
;
42 /* Don't follow symlinks when checking for a path's existence,
43 since we won't follow them to set its ACLs either. */
44 flags
|= AT_SYMLINK_NOFOLLOW
;
46 /* If the path is relative, interpret it relative to the current
47 working directory (just like the access() system call). */
48 if (faccessat(AT_FDCWD
, path
, F_OK
, flags
) == 0) {
58 * @brief Display program usage information.
61 * The program name to use in the output.
64 void usage(const char* program_name
) {
65 printf("Apply any applicable default ACLs to the given files or "
67 printf("Usage: %s [flags] <target1> [<target2> [ <target3>...]]\n\n",
70 printf(" -h, --help Print this help message\n");
71 printf(" -r, --recursive Act on any given directories recursively\n");
72 printf(" -x, --no-exec-mask Apply execute permissions unconditionally\n");
79 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
81 * For parameter information, see the @c nftw man page.
83 * @return If the ACL was applied to @c target successfully, we return
84 * @c FTW_CONTINUE to signal to @ nftw() that we should proceed onto
85 * the next file or directory. Otherwise, we return @c FTW_STOP to
89 int apply_default_acl_nftw(const char *target
,
90 const struct stat
*sp
,
94 if (apply_default_acl_ex(target
, sp
, false)) {
105 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
107 * This is identical to @c apply_default_acl_nftw(), except it passes
108 * @c true to @c apply_default_acl() as its no_exec_mask argument.
111 int apply_default_acl_nftw_x(const char *target
,
112 const struct stat
*sp
,
116 if (apply_default_acl_ex(target
, sp
, true)) {
127 * @brief Recursive version of @c apply_default_acl().
129 * If @c target is a directory, we use @c nftw() to call @c
130 * apply_default_acl() recursively on all of its children. Otherwise,
131 * we just delegate to @c apply_default_acl().
133 * We ignore symlinks for consistency with chmod -r.
136 * The root (path) of the recursive application.
138 * @param no_exec_mask
139 * The value (either true or false) of the --no-exec-mask flag.
142 * If @c target is not a directory, we return the result of
143 * calling @c apply_default_acl() on @c target. Otherwise, we convert
144 * the return value of @c nftw(). If @c nftw() succeeds (returns 0),
145 * then we return @c true. Otherwise, we return @c false.
147 * If there is an error, it will be reported via @c perror, but
148 * we still return @c false.
150 bool apply_default_acl_recursive(const char *target
, bool no_exec_mask
) {
151 int max_levels
= 256;
152 int flags
= FTW_PHYS
; /* Don't follow links. */
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 if (nftw_result
== 0) {
169 /* nftw will return NFTW_ERROR on error, or if the supplied function
170 * (apply_default_acl_nftw) returns a non-zero result, nftw will
173 if (nftw_result
== NFTW_ERROR
) {
174 perror("apply_default_acl_recursive (nftw)");
183 * @brief Call apply_default_acl (possibly recursively) on each
184 * command-line argument.
186 * @return Either @c EXIT_FAILURE or @c EXIT_SUCCESS. If everything
187 * goes as expected, we return @c EXIT_SUCCESS. Otherwise, we return
190 int main(int argc
, char* argv
[]) {
197 bool recursive
= false;
198 bool no_exec_mask
= false;
200 struct option long_options
[] = {
201 /* These options set a flag. */
202 {"help", no_argument
, NULL
, 'h'},
203 {"recursive", no_argument
, NULL
, 'r'},
204 {"no-exec-mask", no_argument
, NULL
, 'x'},
210 while ((opt
= getopt_long(argc
, argv
, "hrx", long_options
, NULL
)) != -1) {
227 int result
= EXIT_SUCCESS
;
230 for (arg_index
= optind
; arg_index
< argc
; arg_index
++) {
231 const char* target
= argv
[arg_index
];
232 bool reapp_result
= false;
234 /* Make sure we can access the given path before we go out of our
235 * way to please it. Doing this check outside of
236 * apply_default_acl() lets us spit out a better error message for
239 if (!path_accessible(target
)) {
240 fprintf(stderr
, "%s: %s: No such file or directory\n", argv
[0], target
);
241 result
= EXIT_FAILURE
;
246 reapp_result
= apply_default_acl_recursive(target
, no_exec_mask
);
249 /* It's either a normal file, or we're not operating recursively. */
250 reapp_result
= apply_default_acl(target
, no_exec_mask
);
254 result
= EXIT_FAILURE
;