xref: /freebsd/bin/getfacl/getfacl.c (revision 5eb43ac2f70729e688f03999a2f66d1272e14a6d)
1 /*-
2  * Copyright (c) 1999-2001 Robert N M Watson
3  * All rights reserved.
4  *
5  * This software was developed by Robert Watson for the TrustedBSD Project.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 /*
29  * getfacl -- POSIX.1e utility to extract ACLs from files and directories
30  * and send the results to stdout
31  */
32 
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/acl.h>
40 #include <sys/stat.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 int	more_than_one = 0;
50 
51 static void
52 usage(void)
53 {
54 
55 	fprintf(stderr, "getfacl [-d] [files ...]\n");
56 }
57 
58 /*
59  * return an ACL corresponding to the permissions
60  * contained in struct stat
61  */
62 static acl_t
63 acl_from_stat(struct stat sb)
64 {
65 	acl_t acl;
66 	acl_entry_t entry;
67 	acl_permset_t perms;
68 
69 	/* create the ACL */
70 	acl = acl_init(3);
71 	if (!acl)
72 		return NULL;
73 
74 	/* First entry: ACL_USER_OBJ */
75 	if (acl_create_entry(&acl, &entry) == -1)
76 		return NULL;
77 	if (acl_set_tag_type(entry, ACL_USER_OBJ) == -1)
78 		return NULL;
79 
80 	if (acl_get_permset(entry, &perms) == -1)
81 		return NULL;
82 	if (acl_clear_perms(perms) == -1)
83 		return NULL;
84 
85 	/* calculate user mode */
86 	if (sb.st_mode & S_IRUSR)
87 		if (acl_add_perm(perms, ACL_READ) == -1)
88 			return NULL;
89 	if (sb.st_mode & S_IWUSR)
90 		if (acl_add_perm(perms, ACL_WRITE) == -1)
91 			return NULL;
92 	if (sb.st_mode & S_IXUSR)
93 		if (acl_add_perm(perms, ACL_EXECUTE) == -1)
94 			return NULL;
95 	if (acl_set_permset(entry, perms) == -1)
96 		return NULL;
97 
98 	/* Second entry: ACL_GROUP_OBJ */
99 	if (acl_create_entry(&acl, &entry) == -1)
100 		return NULL;
101 	if (acl_set_tag_type(entry, ACL_GROUP_OBJ) == -1)
102 		return NULL;
103 
104 	if (acl_get_permset(entry, &perms) == -1)
105 		return NULL;
106 	if (acl_clear_perms(perms) == -1)
107 		return NULL;
108 
109 	/* calculate group mode */
110 	if (sb.st_mode & S_IRGRP)
111 		if (acl_add_perm(perms, ACL_READ) == -1)
112 			return NULL;
113 	if (sb.st_mode & S_IWGRP)
114 		if (acl_add_perm(perms, ACL_WRITE) == -1)
115 			return NULL;
116 	if (sb.st_mode & S_IXGRP)
117 		if (acl_add_perm(perms, ACL_EXECUTE) == -1)
118 			return NULL;
119 	if (acl_set_permset(entry, perms) == -1)
120 		return NULL;
121 
122 	/* Third entry: ACL_OTHER */
123 	if (acl_create_entry(&acl, &entry) == -1)
124 		return NULL;
125 	if (acl_set_tag_type(entry, ACL_OTHER) == -1)
126 		return NULL;
127 
128 	if (acl_get_permset(entry, &perms) == -1)
129 		return NULL;
130 	if (acl_clear_perms(perms) == -1)
131 		return NULL;
132 
133 	/* calculate other mode */
134 	if (sb.st_mode & S_IROTH)
135 		if (acl_add_perm(perms, ACL_READ) == -1)
136 			return NULL;
137 	if (sb.st_mode & S_IWOTH)
138 		if (acl_add_perm(perms, ACL_WRITE) == -1)
139 			return NULL;
140 	if (sb.st_mode & S_IXOTH)
141 		if (acl_add_perm(perms, ACL_EXECUTE) == -1)
142 			return NULL;
143 	if (acl_set_permset(entry, perms) == -1)
144 		return NULL;
145 
146 	return(acl);
147 }
148 
149 static int
150 print_acl(char *path, acl_type_t type)
151 {
152 	struct stat	sb;
153 	acl_t	acl;
154 	char	*acl_text;
155 	int	error;
156 
157 	error = stat(path, &sb);
158 	if (error == -1) {
159 		perror(path);
160 		return(-1);
161 	}
162 
163 	if (more_than_one)
164 		printf("\n");
165 	else
166 		more_than_one++;
167 
168 	printf("#file:%s\n#owner:%d\n#group:%d\n", path, sb.st_uid, sb.st_gid);
169 
170 	acl = acl_get_file(path, type);
171 	if (!acl) {
172 		if (errno != EOPNOTSUPP) {
173 			warn("%s", path);
174 			return(-1);
175 		}
176 		errno = 0;
177 		if (type != ACL_TYPE_ACCESS)
178 			return(0);
179 		acl = acl_from_stat(sb);
180 		if (!acl) {
181 			perror("acl_from_stat()");
182 			return(-1);
183 		}
184 	}
185 
186 	acl_text = acl_to_text(acl, 0);
187 	if (!acl_text) {
188 		perror(path);
189 		return(-1);
190 	}
191 
192 	printf("%s", acl_text);
193 
194 	(void)acl_free(acl);
195 	(void)acl_free(acl_text);
196 
197 	return(0);
198 }
199 
200 static int
201 print_acl_from_stdin(acl_type_t type)
202 {
203 	char	pathname[PATH_MAX];
204 	int	carried_error = 0;
205 
206 	pathname[sizeof(pathname) - 1] = '\0';
207 	while (fgets(pathname, (int)sizeof(pathname), stdin)) {
208 		/* remove the \n */
209 		pathname[strlen(pathname) - 1] = '\0';
210 		if (print_acl(pathname, type) == -1) {
211 			carried_error = -1;
212 		}
213 	}
214 
215 	return(carried_error);
216 }
217 
218 int
219 main(int argc, char *argv[])
220 {
221 	acl_type_t	type = ACL_TYPE_ACCESS;
222 	int	carried_error = 0;
223 	int	ch, error, i;
224 
225 	while ((ch = getopt(argc, argv, "d")) != -1)
226 		switch(ch) {
227 		case 'd':
228 			type = ACL_TYPE_DEFAULT;
229 			break;
230 		default:
231 			usage();
232 			return(-1);
233 		}
234 	argc -= optind;
235 	argv += optind;
236 
237 	if (argc == 0) {
238 		error = print_acl_from_stdin(type);
239 		return(error);
240 	}
241 
242 	for (i = 0; i < argc; i++) {
243 		if (!strcmp(argv[i], "-")) {
244 			error = print_acl_from_stdin(type);
245 			if (error == -1)
246 				carried_error = -1;
247 		} else {
248 			error = print_acl(argv[i], type);
249 			if (error == -1)
250 				carried_error = -1;
251 		}
252 	}
253 
254 	return(carried_error);
255 }
256