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/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/queue.h>
47 #define OP_MERGE_ACL 0x00 /* merge acl's (-mM) */
48 #define OP_REMOVE_DEF 0x01 /* remove default acl's (-k) */
49 #define OP_REMOVE_EXT 0x02 /* remove extended acl's (-b) */
50 #define OP_REMOVE_ACL 0x03 /* remove acl's (-xX) */
51 #define OP_REMOVE_BY_NUMBER 0x04 /* remove acl's (-xX) by acl entry number */
52 #define OP_ADD_ACL 0x05 /* add acls entries at a given position */
54 /* TAILQ entry for acl operations */
59 TAILQ_ENTRY(sf_entry) next;
61 static TAILQ_HEAD(, sf_entry) entrylist;
70 static bool need_mask;
71 static acl_type_t acl_type = ACL_TYPE_ACCESS;
73 static int handle_file(FTS *ftsp, FTSENT *file);
74 static acl_t clear_inheritance_flags(acl_t acl);
75 static char **stdin_files(void);
76 static void usage(void);
82 fprintf(stderr, "usage: setfacl [-R [-H | -L | -P]] [-bdhkn] "
83 "[-a position entries] [-m entries] [-M file] "
84 "[-x entries] [-X file] [file ...]\n");
92 char filename[PATH_MAX];
96 err(1, "cannot have more than one stdin");
100 bzero(&filename, sizeof(filename));
101 /* Start with an array size sufficient for basic cases. */
103 files_list = zmalloc(fl_count * sizeof(char *));
104 while (fgets(filename, (int)sizeof(filename), stdin)) {
106 filename[strlen(filename) - 1] = '\0';
107 files_list[i] = strdup(filename);
108 if (files_list[i] == NULL)
109 err(1, "strdup() failed");
110 /* Grow array if necessary. */
111 if (++i == fl_count) {
113 if (fl_count > SIZE_MAX / sizeof(char *))
114 errx(1, "Too many input files");
115 files_list = zrealloc(files_list,
116 fl_count * sizeof(char *));
120 /* fts_open() requires the last array element to be NULL. */
121 files_list[i] = NULL;
127 * Remove any inheritance flags from NFSv4 ACLs when running in recursive
128 * mode. This is to avoid files being assigned identical ACLs to their
129 * parent directory while also being set to inherit them.
131 * The acl argument is assumed to be valid.
134 clear_inheritance_flags(acl_t acl)
137 acl_entry_t acl_entry;
138 acl_flagset_t acl_flagset;
139 int acl_brand, entry_id;
141 (void)acl_get_brand_np(acl, &acl_brand);
142 if (acl_brand != ACL_BRAND_NFS4)
147 warn("acl_dup() failed");
151 entry_id = ACL_FIRST_ENTRY;
152 while (acl_get_entry(nacl, entry_id, &acl_entry) == 1) {
153 entry_id = ACL_NEXT_ENTRY;
154 if (acl_get_flagset_np(acl_entry, &acl_flagset) != 0) {
155 warn("acl_get_flagset_np() failed");
158 if (acl_get_flag_np(acl_flagset, ACL_ENTRY_INHERIT_ONLY) == 1) {
159 if (acl_delete_entry(nacl, acl_entry) != 0)
160 warn("acl_delete_entry() failed");
163 if (acl_delete_flag_np(acl_flagset,
164 ACL_ENTRY_FILE_INHERIT |
165 ACL_ENTRY_DIRECTORY_INHERIT |
166 ACL_ENTRY_NO_PROPAGATE_INHERIT) != 0)
167 warn("acl_delete_flag_np() failed");
174 handle_file(FTS *ftsp, FTSENT *file)
177 acl_entry_t unused_entry;
178 int local_error, ret;
179 struct sf_entry *entry;
183 switch (file->fts_info) {
185 /* Do not recurse if -R not specified. */
187 fts_set(ftsp, file, FTS_SKIP);
190 /* Skip the second visit to a directory. */
194 warnx("%s: %s", file->fts_path, strerror(file->fts_errno));
200 if (acl_type == ACL_TYPE_DEFAULT && file->fts_info != FTS_D) {
201 warnx("%s: default ACL may only be set on a directory",
206 follow_symlink = (!R_flag && !h_flag) || (R_flag && L_flag) ||
207 (R_flag && H_flag && file->fts_level == FTS_ROOTLEVEL);
210 ret = pathconf(file->fts_accpath, _PC_ACL_NFS4);
212 ret = lpathconf(file->fts_accpath, _PC_ACL_NFS4);
214 if (acl_type == ACL_TYPE_DEFAULT) {
215 warnx("%s: there are no default entries in NFSv4 ACLs",
219 acl_type = ACL_TYPE_NFS4;
220 } else if (ret == 0) {
221 if (acl_type == ACL_TYPE_NFS4)
222 acl_type = ACL_TYPE_ACCESS;
223 } else if (ret < 0 && errno != EINVAL && errno != ENOENT) {
224 warn("%s: pathconf(_PC_ACL_NFS4) failed",
229 acl = acl_get_file(file->fts_accpath, acl_type);
231 acl = acl_get_link_np(file->fts_accpath, acl_type);
234 warn("%s: acl_get_file() failed", file->fts_path);
236 warn("%s: acl_get_link_np() failed", file->fts_path);
240 /* Cycle through each option. */
241 TAILQ_FOREACH(entry, &entrylist, next) {
245 if (R_flag && file->fts_info != FTS_D &&
246 acl_type == ACL_TYPE_NFS4)
247 nacl = clear_inheritance_flags(nacl);
248 local_error += add_acl(nacl, entry->entry_number, &acl,
252 if (R_flag && file->fts_info != FTS_D &&
253 acl_type == ACL_TYPE_NFS4)
254 nacl = clear_inheritance_flags(nacl);
255 local_error += merge_acl(nacl, &acl, file->fts_path);
260 * Don't try to call remove_ext() for empty
263 if (acl_type == ACL_TYPE_DEFAULT &&
264 acl_get_entry(acl, ACL_FIRST_ENTRY,
265 &unused_entry) == 0) {
266 local_error += remove_default(&acl,
270 remove_ext(&acl, file->fts_path);
274 if (acl_type == ACL_TYPE_NFS4) {
275 warnx("%s: there are no default entries in "
276 "NFSv4 ACLs; cannot remove",
281 if (acl_delete_def_file(file->fts_accpath) == -1) {
282 warn("%s: acl_delete_def_file() failed",
286 if (acl_type == ACL_TYPE_DEFAULT)
287 local_error += remove_default(&acl,
292 local_error += remove_acl(nacl, &acl, file->fts_path);
295 case OP_REMOVE_BY_NUMBER:
296 local_error += remove_by_number(entry->entry_number,
297 &acl, file->fts_path);
302 if (nacl != entry->acl) {
313 * Don't try to set an empty default ACL; it will always fail.
314 * Use acl_delete_def_file(3) instead.
316 if (acl_type == ACL_TYPE_DEFAULT &&
317 acl_get_entry(acl, ACL_FIRST_ENTRY, &unused_entry) == 0) {
318 if (acl_delete_def_file(file->fts_accpath) == -1) {
319 warn("%s: acl_delete_def_file() failed",
326 /* Don't bother setting the ACL if something is broken. */
329 } else if (acl_type != ACL_TYPE_NFS4 && need_mask &&
330 set_acl_mask(&acl, file->fts_path) == -1) {
331 warnx("%s: failed to set ACL mask", file->fts_path);
333 } else if (follow_symlink) {
334 if (acl_set_file(file->fts_accpath, acl_type, acl) == -1) {
335 warn("%s: acl_set_file() failed", file->fts_path);
339 if (acl_set_link_np(file->fts_accpath, acl_type, acl) == -1) {
340 warn("%s: acl_set_link_np() failed", file->fts_path);
351 main(int argc, char *argv[])
353 int carried_error, ch, entry_number, fts_options;
357 struct sf_entry *entry;
360 acl_type = ACL_TYPE_ACCESS;
361 carried_error = fts_options = 0;
362 have_mask = have_stdin = n_flag = false;
364 TAILQ_INIT(&entrylist);
366 while ((ch = getopt(argc, argv, "HLM:PRX:a:bdhkm:nx:")) != -1)
377 entry = zmalloc(sizeof(struct sf_entry));
378 entry->acl = get_acl_from_file(optarg);
379 if (entry->acl == NULL)
380 err(1, "%s: get_acl_from_file() failed",
382 entry->op = OP_MERGE_ACL;
383 TAILQ_INSERT_TAIL(&entrylist, entry, next);
386 H_flag = L_flag = false;
392 entry = zmalloc(sizeof(struct sf_entry));
393 entry->acl = get_acl_from_file(optarg);
394 entry->op = OP_REMOVE_ACL;
395 TAILQ_INSERT_TAIL(&entrylist, entry, next);
398 entry = zmalloc(sizeof(struct sf_entry));
400 entry_number = strtol(optarg, &end, 10);
401 if (end - optarg != (int)strlen(optarg))
402 errx(1, "%s: invalid entry number", optarg);
403 if (entry_number < 0)
405 "%s: entry number cannot be less than zero",
407 entry->entry_number = entry_number;
409 if (argv[optind] == NULL)
410 errx(1, "missing ACL");
411 entry->acl = acl_from_text(argv[optind]);
412 if (entry->acl == NULL)
413 err(1, "%s", argv[optind]);
415 entry->op = OP_ADD_ACL;
416 TAILQ_INSERT_TAIL(&entrylist, entry, next);
419 entry = zmalloc(sizeof(struct sf_entry));
420 entry->op = OP_REMOVE_EXT;
421 TAILQ_INSERT_TAIL(&entrylist, entry, next);
424 acl_type = ACL_TYPE_DEFAULT;
430 entry = zmalloc(sizeof(struct sf_entry));
431 entry->op = OP_REMOVE_DEF;
432 TAILQ_INSERT_TAIL(&entrylist, entry, next);
435 entry = zmalloc(sizeof(struct sf_entry));
436 entry->acl = acl_from_text(optarg);
437 if (entry->acl == NULL)
438 err(1, "%s", optarg);
439 entry->op = OP_MERGE_ACL;
440 TAILQ_INSERT_TAIL(&entrylist, entry, next);
446 entry = zmalloc(sizeof(struct sf_entry));
447 entry_number = strtol(optarg, &end, 10);
448 if (end - optarg == (int)strlen(optarg)) {
449 if (entry_number < 0)
451 "%s: entry number cannot be less than zero",
453 entry->entry_number = entry_number;
454 entry->op = OP_REMOVE_BY_NUMBER;
456 entry->acl = acl_from_text(optarg);
457 if (entry->acl == NULL)
458 err(1, "%s", optarg);
459 entry->op = OP_REMOVE_ACL;
461 TAILQ_INSERT_TAIL(&entrylist, entry, next);
470 if (!n_flag && TAILQ_EMPTY(&entrylist))
473 /* Take list of files from stdin. */
474 if (argc == 0 || strcmp(argv[0], "-") == 0) {
475 files_list = stdin_files();
481 errx(1, "the -R and -h options may not be "
482 "specified together.");
484 fts_options = FTS_LOGICAL;
486 fts_options = FTS_PHYSICAL;
489 fts_options |= FTS_COMFOLLOW;
493 fts_options = FTS_PHYSICAL;
495 fts_options = FTS_LOGICAL;
498 /* Open all files. */
499 if ((ftsp = fts_open(files_list, fts_options | FTS_NOSTAT, 0)) == NULL)
501 while ((file = fts_read(ftsp)) != NULL)
502 carried_error += handle_file(ftsp, file);
504 return (carried_error);