2 * Copyright (c) 1999, 2001, 2002 Robert N M Watson
5 * This software was developed by Robert Watson for the TrustedBSD Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * getfacl -- POSIX.1e utility to extract ACLs from files and directories
30 * and send the results to stdout
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
38 #include <sys/param.h>
51 static int more_than_one = 0;
57 fprintf(stderr, "getfacl [-dhnqv] [file ...]\n");
66 if ((pw = getpwuid(uid)) == NULL) {
67 (void)snprintf(uids, sizeof(uids), "%u", uid);
79 if ((gr = getgrgid(gid)) == NULL) {
80 (void)snprintf(gids, sizeof(gids), "%u", gid);
87 print_acl(char *path, acl_type_t type, int hflag, int iflag, int nflag,
93 int error, flags = 0, ret;
96 error = lstat(path, &sb);
98 error = stat(path, &sb);
100 warn("%s: stat() failed", path);
105 ret = lpathconf(path, _PC_ACL_NFS4);
107 ret = pathconf(path, _PC_ACL_NFS4);
109 if (type == ACL_TYPE_DEFAULT) {
110 warnx("%s: there are no default entries in NFSv4 ACLs",
114 type = ACL_TYPE_NFS4;
115 } else if (ret < 0 && errno != EINVAL) {
116 warn("%s: pathconf(..., _PC_ACL_NFS4) failed", path);
126 printf("# file: %s\n# owner: %s\n# group: %s\n", path,
127 getuname(sb.st_uid), getgname(sb.st_gid));
130 acl = acl_get_link_np(path, type);
132 acl = acl_get_file(path, type);
134 if (errno != EOPNOTSUPP) {
139 if (type == ACL_TYPE_DEFAULT)
141 acl = acl_from_mode_np(sb.st_mode);
143 warn("%s: acl_from_mode() failed", path);
149 flags |= ACL_TEXT_APPEND_ID;
152 flags |= ACL_TEXT_NUMERIC_IDS;
155 flags |= ACL_TEXT_VERBOSE;
157 acl_text = acl_to_text_np(acl, 0, flags);
159 warn("%s: acl_to_text_np() failed", path);
163 printf("%s", acl_text);
166 (void)acl_free(acl_text);
172 print_acl_from_stdin(acl_type_t type, int hflag, int iflag, int nflag,
173 int qflag, int vflag)
175 char *p, pathname[PATH_MAX];
176 int carried_error = 0;
178 while (fgets(pathname, (int)sizeof(pathname), stdin)) {
179 if ((p = strchr(pathname, '\n')) != NULL)
181 if (print_acl(pathname, type, hflag, iflag, nflag,
182 qflag, vflag) == -1) {
187 return(carried_error);
191 main(int argc, char *argv[])
193 acl_type_t type = ACL_TYPE_ACCESS;
194 int carried_error = 0;
196 int hflag, iflag, qflag, nflag, vflag;
203 while ((ch = getopt(argc, argv, "dhinqv")) != -1)
206 type = ACL_TYPE_DEFAULT;
231 error = print_acl_from_stdin(type, hflag, iflag, nflag,
233 return(error ? 1 : 0);
236 for (i = 0; i < argc; i++) {
237 if (!strcmp(argv[i], "-")) {
238 error = print_acl_from_stdin(type, hflag, iflag, nflag,
243 error = print_acl(argv[i], type, hflag, iflag, nflag,
250 return(carried_error ? 1 : 0);