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/types.h>
31 #include <sys/param.h>
34 #include <sys/queue.h>
49 #define OP_MERGE_ACL 0x00 /* merge acl's (-mM) */
50 #define OP_REMOVE_DEF 0x01 /* remove default acl's (-k) */
51 #define OP_REMOVE_EXT 0x02 /* remove extended acl's (-b) */
52 #define OP_REMOVE_ACL 0x03 /* remove acl's (-xX) */
53 #define OP_REMOVE_BY_NUMBER 0x04 /* remove acl's (-xX) by acl entry number */
54 #define OP_ADD_ACL 0x05 /* add acls entries at a given position */
56 /* TAILQ entry for acl operations */
61 TAILQ_ENTRY(sf_entry) next;
63 static TAILQ_HEAD(, sf_entry) entrylist;
72 static bool need_mask;
73 static acl_type_t acl_type = ACL_TYPE_ACCESS;
75 static int handle_file(FTS *ftsp, FTSENT *file);
76 static char **stdin_files(void);
77 static void usage(void);
83 fprintf(stderr, "usage: setfacl [-R [-H | -L | -P]] [-bdhkn] "
84 "[-a position entries] [-m entries] [-M file] "
85 "[-x entries] [-X file] [file ...]\n");
93 char filename[PATH_MAX];
97 err(1, "cannot have more than one stdin");
101 bzero(&filename, sizeof(filename));
102 /* Start with an array size sufficient for basic cases. */
104 files_list = zmalloc(fl_count * sizeof(char *));
105 while (fgets(filename, (int)sizeof(filename), stdin)) {
107 filename[strlen(filename) - 1] = '\0';
108 files_list[i] = strdup(filename);
109 if (files_list[i] == NULL)
110 err(1, "strdup() failed");
111 /* Grow array if necessary. */
112 if (++i == fl_count) {
114 if (fl_count > SIZE_MAX / sizeof(char *))
115 errx(1, "Too many input files");
116 files_list = zrealloc(files_list,
117 fl_count * sizeof(char *));
121 /* fts_open() requires the last array element to be NULL. */
122 files_list[i] = NULL;
128 handle_file(FTS *ftsp, FTSENT *file)
131 acl_entry_t unused_entry;
132 int local_error, ret;
133 struct sf_entry *entry;
137 switch (file->fts_info) {
139 /* Do not recurse if -R not specified. */
141 fts_set(ftsp, file, FTS_SKIP);
144 /* Skip the second visit to a directory. */
148 warnx("%s: %s", file->fts_path, strerror(file->fts_errno));
154 if (acl_type == ACL_TYPE_DEFAULT && file->fts_info != FTS_D) {
155 warnx("%s: default ACL may only be set on a directory",
160 follow_symlink = (!R_flag && !h_flag) || (R_flag && L_flag) ||
161 (R_flag && H_flag && file->fts_level == FTS_ROOTLEVEL);
164 ret = pathconf(file->fts_accpath, _PC_ACL_NFS4);
166 ret = lpathconf(file->fts_accpath, _PC_ACL_NFS4);
168 if (acl_type == ACL_TYPE_DEFAULT) {
169 warnx("%s: there are no default entries in NFSv4 ACLs",
173 acl_type = ACL_TYPE_NFS4;
174 } else if (ret == 0) {
175 if (acl_type == ACL_TYPE_NFS4)
176 acl_type = ACL_TYPE_ACCESS;
177 } else if (ret < 0 && errno != EINVAL) {
178 warn("%s: pathconf(..., _PC_ACL_NFS4) failed",
183 acl = acl_get_file(file->fts_accpath, acl_type);
185 acl = acl_get_link_np(file->fts_accpath, acl_type);
188 warn("%s: acl_get_file() failed", file->fts_path);
190 warn("%s: acl_get_link_np() failed", file->fts_path);
194 /* Cycle through each option. */
195 TAILQ_FOREACH(entry, &entrylist, next) {
201 local_error += add_acl(entry->acl, entry->entry_number,
202 &acl, file->fts_path);
205 local_error += merge_acl(entry->acl, &acl,
211 * Don't try to call remove_ext() for empty
214 if (acl_type == ACL_TYPE_DEFAULT &&
215 acl_get_entry(acl, ACL_FIRST_ENTRY,
216 &unused_entry) == 0) {
217 local_error += remove_default(&acl,
221 remove_ext(&acl, file->fts_path);
225 if (acl_type == ACL_TYPE_NFS4) {
226 warnx("%s: there are no default entries in "
227 "NFSv4 ACLs; cannot remove",
232 if (acl_delete_def_file(file->fts_accpath) == -1) {
233 warn("%s: acl_delete_def_file() failed",
237 if (acl_type == ACL_TYPE_DEFAULT)
238 local_error += remove_default(&acl,
243 local_error += remove_acl(entry->acl, &acl,
247 case OP_REMOVE_BY_NUMBER:
248 local_error += remove_by_number(entry->entry_number,
249 &acl, file->fts_path);
256 * Don't try to set an empty default ACL; it will always fail.
257 * Use acl_delete_def_file(3) instead.
259 if (acl_type == ACL_TYPE_DEFAULT &&
260 acl_get_entry(acl, ACL_FIRST_ENTRY, &unused_entry) == 0) {
261 if (acl_delete_def_file(file->fts_accpath) == -1) {
262 warn("%s: acl_delete_def_file() failed",
269 /* Don't bother setting the ACL if something is broken. */
274 if (acl_type != ACL_TYPE_NFS4 && need_mask &&
275 set_acl_mask(&acl, file->fts_path) == -1) {
276 warnx("%s: failed to set ACL mask", file->fts_path);
278 } else if (follow_symlink) {
279 if (acl_set_file(file->fts_accpath, acl_type, acl) == -1) {
280 warn("%s: acl_set_file() failed", file->fts_path);
284 if (acl_set_link_np(file->fts_accpath, acl_type, acl) == -1) {
285 warn("%s: acl_set_link_np() failed", file->fts_path);
295 main(int argc, char *argv[])
297 int carried_error, ch, entry_number, fts_options;
301 struct sf_entry *entry;
304 acl_type = ACL_TYPE_ACCESS;
305 carried_error = fts_options = 0;
306 have_mask = have_stdin = n_flag = false;
308 TAILQ_INIT(&entrylist);
310 while ((ch = getopt(argc, argv, "HLM:PRX:a:bdhkm:nx:")) != -1)
321 entry = zmalloc(sizeof(struct sf_entry));
322 entry->acl = get_acl_from_file(optarg);
323 if (entry->acl == NULL)
324 err(1, "%s: get_acl_from_file() failed",
326 entry->op = OP_MERGE_ACL;
327 TAILQ_INSERT_TAIL(&entrylist, entry, next);
330 H_flag = L_flag = false;
336 entry = zmalloc(sizeof(struct sf_entry));
337 entry->acl = get_acl_from_file(optarg);
338 entry->op = OP_REMOVE_ACL;
339 TAILQ_INSERT_TAIL(&entrylist, entry, next);
342 entry = zmalloc(sizeof(struct sf_entry));
344 entry_number = strtol(optarg, &end, 10);
345 if (end - optarg != (int)strlen(optarg))
346 errx(1, "%s: invalid entry number", optarg);
347 if (entry_number < 0)
349 "%s: entry number cannot be less than zero",
351 entry->entry_number = entry_number;
353 if (argv[optind] == NULL)
354 errx(1, "missing ACL");
355 entry->acl = acl_from_text(argv[optind]);
356 if (entry->acl == NULL)
357 err(1, "%s", argv[optind]);
359 entry->op = OP_ADD_ACL;
360 TAILQ_INSERT_TAIL(&entrylist, entry, next);
363 entry = zmalloc(sizeof(struct sf_entry));
364 entry->op = OP_REMOVE_EXT;
365 TAILQ_INSERT_TAIL(&entrylist, entry, next);
368 acl_type = ACL_TYPE_DEFAULT;
374 entry = zmalloc(sizeof(struct sf_entry));
375 entry->op = OP_REMOVE_DEF;
376 TAILQ_INSERT_TAIL(&entrylist, entry, next);
379 entry = zmalloc(sizeof(struct sf_entry));
380 entry->acl = acl_from_text(optarg);
381 if (entry->acl == NULL)
382 err(1, "%s", optarg);
383 entry->op = OP_MERGE_ACL;
384 TAILQ_INSERT_TAIL(&entrylist, entry, next);
390 entry = zmalloc(sizeof(struct sf_entry));
391 entry_number = strtol(optarg, &end, 10);
392 if (end - optarg == (int)strlen(optarg)) {
393 if (entry_number < 0)
395 "%s: entry number cannot be less than zero",
397 entry->entry_number = entry_number;
398 entry->op = OP_REMOVE_BY_NUMBER;
400 entry->acl = acl_from_text(optarg);
401 if (entry->acl == NULL)
402 err(1, "%s", optarg);
403 entry->op = OP_REMOVE_ACL;
405 TAILQ_INSERT_TAIL(&entrylist, entry, next);
414 if (!n_flag && TAILQ_EMPTY(&entrylist))
417 /* Take list of files from stdin. */
418 if (argc == 0 || strcmp(argv[0], "-") == 0) {
419 files_list = stdin_files();
425 errx(1, "the -R and -h options may not be "
426 "specified together.");
428 fts_options = FTS_LOGICAL;
430 fts_options = FTS_PHYSICAL;
433 fts_options |= FTS_COMFOLLOW;
437 fts_options = FTS_PHYSICAL;
439 fts_options = FTS_LOGICAL;
442 /* Open all files. */
443 if ((ftsp = fts_open(files_list, fts_options | FTS_NOSTAT, 0)) == NULL)
445 while ((file = fts_read(ftsp)) != NULL)
446 carried_error += handle_file(ftsp, file);
448 return (carried_error);