2 * Copyright (c) 2001 Chris D. Faulhaber
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/param.h>
29 #include <sys/queue.h>
44 #define OP_MERGE_ACL 0x00 /* merge acl's (-mM) */
45 #define OP_REMOVE_DEF 0x01 /* remove default acl's (-k) */
46 #define OP_REMOVE_EXT 0x02 /* remove extended acl's (-b) */
47 #define OP_REMOVE_ACL 0x03 /* remove acl's (-xX) */
48 #define OP_REMOVE_BY_NUMBER 0x04 /* remove acl's (-xX) by acl entry number */
49 #define OP_ADD_ACL 0x05 /* add acls entries at a given position */
51 /* TAILQ entry for acl operations */
56 TAILQ_ENTRY(sf_entry) next;
58 static TAILQ_HEAD(, sf_entry) entrylist;
67 static bool need_mask;
68 static acl_type_t acl_type = ACL_TYPE_ACCESS;
70 static int handle_file(FTS *ftsp, FTSENT *file);
71 static acl_t clear_inheritance_flags(acl_t acl);
72 static char **stdin_files(void);
73 static void usage(void);
79 fprintf(stderr, "usage: setfacl [-R [-H | -L | -P]] [-bdhkn] "
80 "[-a position entries] [-m entries] [-M file] "
81 "[-x entries] [-X file] [file ...]\n");
89 char filename[PATH_MAX];
93 err(1, "cannot have more than one stdin");
97 bzero(&filename, sizeof(filename));
98 /* Start with an array size sufficient for basic cases. */
100 files_list = zmalloc(fl_count * sizeof(char *));
101 while (fgets(filename, (int)sizeof(filename), stdin)) {
103 filename[strlen(filename) - 1] = '\0';
104 files_list[i] = strdup(filename);
105 if (files_list[i] == NULL)
106 err(1, "strdup() failed");
107 /* Grow array if necessary. */
108 if (++i == fl_count) {
110 if (fl_count > SIZE_MAX / sizeof(char *))
111 errx(1, "Too many input files");
112 files_list = zrealloc(files_list,
113 fl_count * sizeof(char *));
117 /* fts_open() requires the last array element to be NULL. */
118 files_list[i] = NULL;
124 * Remove any inheritance flags from NFSv4 ACLs when running in recursive
125 * mode. This is to avoid files being assigned identical ACLs to their
126 * parent directory while also being set to inherit them.
128 * The acl argument is assumed to be valid.
131 clear_inheritance_flags(acl_t acl)
134 acl_entry_t acl_entry;
135 acl_flagset_t acl_flagset;
136 int acl_brand, entry_id;
138 (void)acl_get_brand_np(acl, &acl_brand);
139 if (acl_brand != ACL_BRAND_NFS4)
144 warn("acl_dup() failed");
148 entry_id = ACL_FIRST_ENTRY;
149 while (acl_get_entry(nacl, entry_id, &acl_entry) == 1) {
150 entry_id = ACL_NEXT_ENTRY;
151 if (acl_get_flagset_np(acl_entry, &acl_flagset) != 0) {
152 warn("acl_get_flagset_np() failed");
155 if (acl_get_flag_np(acl_flagset, ACL_ENTRY_INHERIT_ONLY) == 1) {
156 if (acl_delete_entry(nacl, acl_entry) != 0)
157 warn("acl_delete_entry() failed");
160 if (acl_delete_flag_np(acl_flagset,
161 ACL_ENTRY_FILE_INHERIT |
162 ACL_ENTRY_DIRECTORY_INHERIT |
163 ACL_ENTRY_NO_PROPAGATE_INHERIT) != 0)
164 warn("acl_delete_flag_np() failed");
171 handle_file(FTS *ftsp, FTSENT *file)
174 acl_entry_t unused_entry;
175 int local_error, ret;
176 struct sf_entry *entry;
180 switch (file->fts_info) {
182 /* Do not recurse if -R not specified. */
184 fts_set(ftsp, file, FTS_SKIP);
187 /* Skip the second visit to a directory. */
191 warnx("%s: %s", file->fts_path, strerror(file->fts_errno));
197 if (acl_type == ACL_TYPE_DEFAULT && file->fts_info != FTS_D) {
198 warnx("%s: default ACL may only be set on a directory",
203 follow_symlink = (!R_flag && !h_flag) || (R_flag && L_flag) ||
204 (R_flag && H_flag && file->fts_level == FTS_ROOTLEVEL);
207 ret = pathconf(file->fts_accpath, _PC_ACL_NFS4);
209 ret = lpathconf(file->fts_accpath, _PC_ACL_NFS4);
211 if (acl_type == ACL_TYPE_DEFAULT) {
212 warnx("%s: there are no default entries in NFSv4 ACLs",
216 acl_type = ACL_TYPE_NFS4;
217 } else if (ret == 0) {
218 if (acl_type == ACL_TYPE_NFS4)
219 acl_type = ACL_TYPE_ACCESS;
220 } else if (ret < 0 && errno != EINVAL && errno != ENOENT) {
221 warn("%s: pathconf(_PC_ACL_NFS4) failed",
226 acl = acl_get_file(file->fts_accpath, acl_type);
228 acl = acl_get_link_np(file->fts_accpath, acl_type);
231 warn("%s: acl_get_file() failed", file->fts_path);
233 warn("%s: acl_get_link_np() failed", file->fts_path);
237 /* Cycle through each option. */
238 TAILQ_FOREACH(entry, &entrylist, next) {
242 if (R_flag && file->fts_info != FTS_D &&
243 acl_type == ACL_TYPE_NFS4)
244 nacl = clear_inheritance_flags(nacl);
245 local_error += add_acl(nacl, entry->entry_number, &acl,
249 if (R_flag && file->fts_info != FTS_D &&
250 acl_type == ACL_TYPE_NFS4)
251 nacl = clear_inheritance_flags(nacl);
252 local_error += merge_acl(nacl, &acl, file->fts_path);
257 * Don't try to call remove_ext() for empty
260 if (acl_type == ACL_TYPE_DEFAULT &&
261 acl_get_entry(acl, ACL_FIRST_ENTRY,
262 &unused_entry) == 0) {
263 local_error += remove_default(&acl,
267 remove_ext(&acl, file->fts_path);
271 if (acl_type == ACL_TYPE_NFS4) {
272 warnx("%s: there are no default entries in "
273 "NFSv4 ACLs; cannot remove",
278 if (acl_delete_def_file(file->fts_accpath) == -1) {
279 warn("%s: acl_delete_def_file() failed",
283 if (acl_type == ACL_TYPE_DEFAULT)
284 local_error += remove_default(&acl,
289 local_error += remove_acl(nacl, &acl, file->fts_path);
292 case OP_REMOVE_BY_NUMBER:
293 local_error += remove_by_number(entry->entry_number,
294 &acl, file->fts_path);
299 if (nacl != entry->acl) {
310 * Don't try to set an empty default ACL; it will always fail.
311 * Use acl_delete_def_file(3) instead.
313 if (acl_type == ACL_TYPE_DEFAULT &&
314 acl_get_entry(acl, ACL_FIRST_ENTRY, &unused_entry) == 0) {
315 if (acl_delete_def_file(file->fts_accpath) == -1) {
316 warn("%s: acl_delete_def_file() failed",
323 /* Don't bother setting the ACL if something is broken. */
326 } else if (acl_type != ACL_TYPE_NFS4 && need_mask &&
327 set_acl_mask(&acl, file->fts_path) == -1) {
328 warnx("%s: failed to set ACL mask", file->fts_path);
330 } else if (follow_symlink) {
331 if (acl_set_file(file->fts_accpath, acl_type, acl) == -1) {
332 warn("%s: acl_set_file() failed", file->fts_path);
336 if (acl_set_link_np(file->fts_accpath, acl_type, acl) == -1) {
337 warn("%s: acl_set_link_np() failed", file->fts_path);
348 main(int argc, char *argv[])
350 int carried_error, ch, entry_number, fts_options;
354 struct sf_entry *entry;
357 acl_type = ACL_TYPE_ACCESS;
358 carried_error = fts_options = 0;
359 have_mask = have_stdin = n_flag = false;
361 TAILQ_INIT(&entrylist);
363 while ((ch = getopt(argc, argv, "HLM:PRX:a:bdhkm:nx:")) != -1)
374 entry = zmalloc(sizeof(struct sf_entry));
375 entry->acl = get_acl_from_file(optarg);
376 if (entry->acl == NULL)
377 err(1, "%s: get_acl_from_file() failed",
379 entry->op = OP_MERGE_ACL;
380 TAILQ_INSERT_TAIL(&entrylist, entry, next);
383 H_flag = L_flag = false;
389 entry = zmalloc(sizeof(struct sf_entry));
390 entry->acl = get_acl_from_file(optarg);
391 entry->op = OP_REMOVE_ACL;
392 TAILQ_INSERT_TAIL(&entrylist, entry, next);
395 entry = zmalloc(sizeof(struct sf_entry));
397 entry_number = strtol(optarg, &end, 10);
398 if (end - optarg != (int)strlen(optarg))
399 errx(1, "%s: invalid entry number", optarg);
400 if (entry_number < 0)
402 "%s: entry number cannot be less than zero",
404 entry->entry_number = entry_number;
406 if (argv[optind] == NULL)
407 errx(1, "missing ACL");
408 entry->acl = acl_from_text(argv[optind]);
409 if (entry->acl == NULL)
410 err(1, "%s", argv[optind]);
412 entry->op = OP_ADD_ACL;
413 TAILQ_INSERT_TAIL(&entrylist, entry, next);
416 entry = zmalloc(sizeof(struct sf_entry));
417 entry->op = OP_REMOVE_EXT;
418 TAILQ_INSERT_TAIL(&entrylist, entry, next);
421 acl_type = ACL_TYPE_DEFAULT;
427 entry = zmalloc(sizeof(struct sf_entry));
428 entry->op = OP_REMOVE_DEF;
429 TAILQ_INSERT_TAIL(&entrylist, entry, next);
432 entry = zmalloc(sizeof(struct sf_entry));
433 entry->acl = acl_from_text(optarg);
434 if (entry->acl == NULL)
435 err(1, "%s", optarg);
436 entry->op = OP_MERGE_ACL;
437 TAILQ_INSERT_TAIL(&entrylist, entry, next);
443 entry = zmalloc(sizeof(struct sf_entry));
444 entry_number = strtol(optarg, &end, 10);
445 if (end - optarg == (int)strlen(optarg)) {
446 if (entry_number < 0)
448 "%s: entry number cannot be less than zero",
450 entry->entry_number = entry_number;
451 entry->op = OP_REMOVE_BY_NUMBER;
453 entry->acl = acl_from_text(optarg);
454 if (entry->acl == NULL)
455 err(1, "%s", optarg);
456 entry->op = OP_REMOVE_ACL;
458 TAILQ_INSERT_TAIL(&entrylist, entry, next);
467 if (!n_flag && TAILQ_EMPTY(&entrylist))
470 /* Take list of files from stdin. */
471 if (argc == 0 || strcmp(argv[0], "-") == 0) {
472 files_list = stdin_files();
478 errx(1, "the -R and -h options may not be "
479 "specified together.");
481 fts_options = FTS_LOGICAL;
483 fts_options = FTS_PHYSICAL;
486 fts_options |= FTS_COMFOLLOW;
490 fts_options = FTS_PHYSICAL;
492 fts_options = FTS_LOGICAL;
495 /* Open all files. */
496 if ((ftsp = fts_open(files_list, fts_options | FTS_NOSTAT, 0)) == NULL)
498 while (errno = 0, (file = fts_read(ftsp)) != NULL)
499 carried_error += handle_file(ftsp, file);
503 return (carried_error);