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>
49 int more_than_one = 0;
55 fprintf(stderr, "getfacl [-dhq] [file ...]\n");
59 * return an ACL corresponding to the permissions
60 * contained in struct stat
63 acl_from_stat(struct stat sb)
74 /* First entry: ACL_USER_OBJ */
75 if (acl_create_entry(&acl, &entry) == -1)
77 if (acl_set_tag_type(entry, ACL_USER_OBJ) == -1)
80 if (acl_get_permset(entry, &perms) == -1)
82 if (acl_clear_perms(perms) == -1)
85 /* calculate user mode */
86 if (sb.st_mode & S_IRUSR)
87 if (acl_add_perm(perms, ACL_READ) == -1)
89 if (sb.st_mode & S_IWUSR)
90 if (acl_add_perm(perms, ACL_WRITE) == -1)
92 if (sb.st_mode & S_IXUSR)
93 if (acl_add_perm(perms, ACL_EXECUTE) == -1)
95 if (acl_set_permset(entry, perms) == -1)
98 /* Second entry: ACL_GROUP_OBJ */
99 if (acl_create_entry(&acl, &entry) == -1)
101 if (acl_set_tag_type(entry, ACL_GROUP_OBJ) == -1)
104 if (acl_get_permset(entry, &perms) == -1)
106 if (acl_clear_perms(perms) == -1)
109 /* calculate group mode */
110 if (sb.st_mode & S_IRGRP)
111 if (acl_add_perm(perms, ACL_READ) == -1)
113 if (sb.st_mode & S_IWGRP)
114 if (acl_add_perm(perms, ACL_WRITE) == -1)
116 if (sb.st_mode & S_IXGRP)
117 if (acl_add_perm(perms, ACL_EXECUTE) == -1)
119 if (acl_set_permset(entry, perms) == -1)
122 /* Third entry: ACL_OTHER */
123 if (acl_create_entry(&acl, &entry) == -1)
125 if (acl_set_tag_type(entry, ACL_OTHER) == -1)
128 if (acl_get_permset(entry, &perms) == -1)
130 if (acl_clear_perms(perms) == -1)
133 /* calculate other mode */
134 if (sb.st_mode & S_IROTH)
135 if (acl_add_perm(perms, ACL_READ) == -1)
137 if (sb.st_mode & S_IWOTH)
138 if (acl_add_perm(perms, ACL_WRITE) == -1)
140 if (sb.st_mode & S_IXOTH)
141 if (acl_add_perm(perms, ACL_EXECUTE) == -1)
143 if (acl_set_permset(entry, perms) == -1)
150 print_acl(char *path, acl_type_t type, int hflag, int qflag)
158 error = lstat(path, &sb);
160 error = stat(path, &sb);
172 printf("#file:%s\n#owner:%d\n#group:%d\n", path, sb.st_uid,
176 acl = acl_get_link_np(path, type);
178 acl = acl_get_file(path, type);
180 if (errno != EOPNOTSUPP) {
185 if (type != ACL_TYPE_ACCESS)
187 acl = acl_from_stat(sb);
189 warn("acl_from_stat()");
194 acl_text = acl_to_text(acl, 0);
200 printf("%s", acl_text);
203 (void)acl_free(acl_text);
209 print_acl_from_stdin(acl_type_t type, int hflag, int qflag)
211 char *p, pathname[PATH_MAX];
212 int carried_error = 0;
214 while (fgets(pathname, (int)sizeof(pathname), stdin)) {
215 if ((p = strchr(pathname, '\n')) != NULL)
217 if (print_acl(pathname, type, hflag, qflag) == -1) {
222 return(carried_error);
226 main(int argc, char *argv[])
228 acl_type_t type = ACL_TYPE_ACCESS;
229 int carried_error = 0;
235 while ((ch = getopt(argc, argv, "dhq")) != -1)
238 type = ACL_TYPE_DEFAULT;
254 error = print_acl_from_stdin(type, hflag, qflag);
255 return(error ? 1 : 0);
258 for (i = 0; i < argc; i++) {
259 if (!strcmp(argv[i], "-")) {
260 error = print_acl_from_stdin(type, hflag, qflag);
264 error = print_acl(argv[i], type, hflag, qflag);
270 return(carried_error ? 1 : 0);