getfacl.c revision 1.2 1 1.2 joerg /* $NetBSD: getfacl.c,v 1.2 2020/05/22 01:28:00 joerg Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1999, 2001, 2002 Robert N M Watson
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This software was developed by Robert Watson for the TrustedBSD Project.
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos *
18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 christos * SUCH DAMAGE.
29 1.1 christos */
30 1.1 christos /*
31 1.1 christos * getfacl -- POSIX.1e utility to extract ACLs from files and directories
32 1.1 christos * and send the results to stdout
33 1.1 christos */
34 1.1 christos
35 1.1 christos
36 1.1 christos #include <sys/cdefs.h>
37 1.1 christos #if 0
38 1.1 christos __FBSDID("$FreeBSD: head/bin/getfacl/getfacl.c 340014 2018-11-01 17:45:29Z markj $");
39 1.1 christos #else
40 1.2 joerg __RCSID("$NetBSD: getfacl.c,v 1.2 2020/05/22 01:28:00 joerg Exp $");
41 1.1 christos #endif
42 1.1 christos
43 1.1 christos #include <sys/types.h>
44 1.1 christos #include <sys/param.h>
45 1.1 christos #include <sys/acl.h>
46 1.1 christos #include <sys/stat.h>
47 1.1 christos
48 1.1 christos #include <err.h>
49 1.1 christos #include <errno.h>
50 1.1 christos #include <grp.h>
51 1.1 christos #include <pwd.h>
52 1.1 christos #include <stdio.h>
53 1.1 christos #include <stdlib.h>
54 1.1 christos #include <string.h>
55 1.1 christos #include <unistd.h>
56 1.1 christos
57 1.1 christos static int more_than_one = 0;
58 1.1 christos
59 1.1 christos static __dead void
60 1.1 christos usage(void)
61 1.1 christos {
62 1.1 christos
63 1.1 christos fprintf(stderr, "Usage: %s [-dhnqv] [file ...]\n", getprogname());
64 1.2 joerg exit(1);
65 1.1 christos }
66 1.1 christos
67 1.1 christos static char *
68 1.1 christos getuname(uid_t uid)
69 1.1 christos {
70 1.1 christos struct passwd *pw;
71 1.1 christos static char uids[10];
72 1.1 christos
73 1.1 christos if ((pw = getpwuid(uid)) == NULL) {
74 1.1 christos (void)snprintf(uids, sizeof(uids), "%u", uid);
75 1.1 christos return (uids);
76 1.1 christos } else
77 1.1 christos return (pw->pw_name);
78 1.1 christos }
79 1.1 christos
80 1.1 christos static char *
81 1.1 christos getgname(gid_t gid)
82 1.1 christos {
83 1.1 christos struct group *gr;
84 1.1 christos static char gids[10];
85 1.1 christos
86 1.1 christos if ((gr = getgrgid(gid)) == NULL) {
87 1.1 christos (void)snprintf(gids, sizeof(gids), "%u", gid);
88 1.1 christos return (gids);
89 1.1 christos } else
90 1.1 christos return (gr->gr_name);
91 1.1 christos }
92 1.1 christos
93 1.1 christos /*
94 1.1 christos * return an ACL corresponding to the permissions
95 1.1 christos * contained in struct stat
96 1.1 christos */
97 1.1 christos static acl_t
98 1.1 christos acl_from_stat(const struct stat *sb)
99 1.1 christos {
100 1.1 christos acl_t acl;
101 1.1 christos acl_entry_t entry;
102 1.1 christos acl_permset_t perms;
103 1.1 christos
104 1.1 christos /* create the ACL */
105 1.1 christos acl = acl_init(3);
106 1.1 christos if (!acl)
107 1.1 christos return NULL;
108 1.1 christos
109 1.1 christos /* First entry: ACL_USER_OBJ */
110 1.1 christos if (acl_create_entry(&acl, &entry) == -1)
111 1.1 christos return NULL;
112 1.1 christos if (acl_set_tag_type(entry, ACL_USER_OBJ) == -1)
113 1.1 christos return NULL;
114 1.1 christos
115 1.1 christos if (acl_get_permset(entry, &perms) == -1)
116 1.1 christos return NULL;
117 1.1 christos if (acl_clear_perms(perms) == -1)
118 1.1 christos return NULL;
119 1.1 christos
120 1.1 christos /* calculate user mode */
121 1.1 christos if (sb->st_mode & S_IRUSR)
122 1.1 christos if (acl_add_perm(perms, ACL_READ) == -1)
123 1.1 christos return NULL;
124 1.1 christos if (sb->st_mode & S_IWUSR)
125 1.1 christos if (acl_add_perm(perms, ACL_WRITE) == -1)
126 1.1 christos return NULL;
127 1.1 christos if (sb->st_mode & S_IXUSR)
128 1.1 christos if (acl_add_perm(perms, ACL_EXECUTE) == -1)
129 1.1 christos return NULL;
130 1.1 christos if (acl_set_permset(entry, perms) == -1)
131 1.1 christos return NULL;
132 1.1 christos
133 1.1 christos /* Second entry: ACL_GROUP_OBJ */
134 1.1 christos if (acl_create_entry(&acl, &entry) == -1)
135 1.1 christos return NULL;
136 1.1 christos if (acl_set_tag_type(entry, ACL_GROUP_OBJ) == -1)
137 1.1 christos return NULL;
138 1.1 christos
139 1.1 christos if (acl_get_permset(entry, &perms) == -1)
140 1.1 christos return NULL;
141 1.1 christos if (acl_clear_perms(perms) == -1)
142 1.1 christos return NULL;
143 1.1 christos
144 1.1 christos /* calculate group mode */
145 1.1 christos if (sb->st_mode & S_IRGRP)
146 1.1 christos if (acl_add_perm(perms, ACL_READ) == -1)
147 1.1 christos return NULL;
148 1.1 christos if (sb->st_mode & S_IWGRP)
149 1.1 christos if (acl_add_perm(perms, ACL_WRITE) == -1)
150 1.1 christos return NULL;
151 1.1 christos if (sb->st_mode & S_IXGRP)
152 1.1 christos if (acl_add_perm(perms, ACL_EXECUTE) == -1)
153 1.1 christos return NULL;
154 1.1 christos if (acl_set_permset(entry, perms) == -1)
155 1.1 christos return NULL;
156 1.1 christos
157 1.1 christos /* Third entry: ACL_OTHER */
158 1.1 christos if (acl_create_entry(&acl, &entry) == -1)
159 1.1 christos return NULL;
160 1.1 christos if (acl_set_tag_type(entry, ACL_OTHER) == -1)
161 1.1 christos return NULL;
162 1.1 christos
163 1.1 christos if (acl_get_permset(entry, &perms) == -1)
164 1.1 christos return NULL;
165 1.1 christos if (acl_clear_perms(perms) == -1)
166 1.1 christos return NULL;
167 1.1 christos
168 1.1 christos /* calculate other mode */
169 1.1 christos if (sb->st_mode & S_IROTH)
170 1.1 christos if (acl_add_perm(perms, ACL_READ) == -1)
171 1.1 christos return NULL;
172 1.1 christos if (sb->st_mode & S_IWOTH)
173 1.1 christos if (acl_add_perm(perms, ACL_WRITE) == -1)
174 1.1 christos return NULL;
175 1.1 christos if (sb->st_mode & S_IXOTH)
176 1.1 christos if (acl_add_perm(perms, ACL_EXECUTE) == -1)
177 1.1 christos return NULL;
178 1.1 christos if (acl_set_permset(entry, perms) == -1)
179 1.1 christos return NULL;
180 1.1 christos
181 1.1 christos return(acl);
182 1.1 christos }
183 1.1 christos
184 1.1 christos static int
185 1.1 christos print_acl(char *path, acl_type_t type, int hflag, int iflag, int nflag,
186 1.1 christos int qflag, int vflag)
187 1.1 christos {
188 1.1 christos struct stat sb;
189 1.1 christos acl_t acl;
190 1.1 christos char *acl_text;
191 1.1 christos int error, flags = 0, ret;
192 1.1 christos
193 1.1 christos if (hflag)
194 1.1 christos error = lstat(path, &sb);
195 1.1 christos else
196 1.1 christos error = stat(path, &sb);
197 1.1 christos if (error == -1) {
198 1.1 christos warn("%s: stat() failed", path);
199 1.1 christos return(-1);
200 1.1 christos }
201 1.1 christos
202 1.1 christos if (hflag)
203 1.1 christos ret = lpathconf(path, _PC_ACL_NFS4);
204 1.1 christos else
205 1.1 christos ret = pathconf(path, _PC_ACL_NFS4);
206 1.1 christos if (ret > 0) {
207 1.1 christos if (type == ACL_TYPE_DEFAULT) {
208 1.1 christos warnx("%s: there are no default entries in NFSv4 ACLs",
209 1.1 christos path);
210 1.1 christos return (-1);
211 1.1 christos }
212 1.1 christos type = ACL_TYPE_NFS4;
213 1.1 christos } else if (ret < 0 && errno != EINVAL) {
214 1.1 christos warn("%s: pathconf(..., _PC_ACL_NFS4) failed", path);
215 1.1 christos return (-1);
216 1.1 christos }
217 1.1 christos
218 1.1 christos if (more_than_one)
219 1.1 christos printf("\n");
220 1.1 christos else
221 1.1 christos more_than_one++;
222 1.1 christos
223 1.1 christos if (!qflag)
224 1.1 christos printf("# file: %s\n# owner: %s\n# group: %s\n", path,
225 1.1 christos getuname(sb.st_uid), getgname(sb.st_gid));
226 1.1 christos
227 1.1 christos if (hflag)
228 1.1 christos acl = acl_get_link_np(path, type);
229 1.1 christos else
230 1.1 christos acl = acl_get_file(path, type);
231 1.1 christos if (!acl) {
232 1.1 christos if (errno != EOPNOTSUPP) {
233 1.1 christos warn("%s", path);
234 1.1 christos return(-1);
235 1.1 christos }
236 1.1 christos errno = 0;
237 1.1 christos if (type == ACL_TYPE_DEFAULT)
238 1.1 christos return(0);
239 1.1 christos acl = acl_from_stat(&sb);
240 1.1 christos if (!acl) {
241 1.1 christos warn("%s: acl_from_stat() failed", path);
242 1.1 christos return(-1);
243 1.1 christos }
244 1.1 christos }
245 1.1 christos
246 1.1 christos if (iflag)
247 1.1 christos flags |= ACL_TEXT_APPEND_ID;
248 1.1 christos
249 1.1 christos if (nflag)
250 1.1 christos flags |= ACL_TEXT_NUMERIC_IDS;
251 1.1 christos
252 1.1 christos if (vflag)
253 1.1 christos flags |= ACL_TEXT_VERBOSE;
254 1.1 christos
255 1.1 christos acl_text = acl_to_text_np(acl, 0, flags);
256 1.1 christos if (!acl_text) {
257 1.1 christos warn("%s: acl_to_text_np() failed", path);
258 1.1 christos return(-1);
259 1.1 christos }
260 1.1 christos
261 1.1 christos printf("%s", acl_text);
262 1.1 christos
263 1.1 christos (void)acl_free(acl);
264 1.1 christos (void)acl_free(acl_text);
265 1.1 christos
266 1.1 christos return(0);
267 1.1 christos }
268 1.1 christos
269 1.1 christos static int
270 1.1 christos print_acl_from_stdin(acl_type_t type, int hflag, int iflag, int nflag,
271 1.1 christos int qflag, int vflag)
272 1.1 christos {
273 1.1 christos char *p, pathname[PATH_MAX];
274 1.1 christos int carried_error = 0;
275 1.1 christos
276 1.1 christos while (fgets(pathname, (int)sizeof(pathname), stdin)) {
277 1.1 christos if ((p = strchr(pathname, '\n')) != NULL)
278 1.1 christos *p = '\0';
279 1.1 christos if (print_acl(pathname, type, hflag, iflag, nflag,
280 1.1 christos qflag, vflag) == -1) {
281 1.1 christos carried_error = -1;
282 1.1 christos }
283 1.1 christos }
284 1.1 christos
285 1.1 christos return(carried_error);
286 1.1 christos }
287 1.1 christos
288 1.1 christos int
289 1.1 christos main(int argc, char *argv[])
290 1.1 christos {
291 1.1 christos acl_type_t type = ACL_TYPE_ACCESS;
292 1.1 christos int carried_error = 0;
293 1.1 christos int ch, error, i;
294 1.1 christos int hflag, iflag, qflag, nflag, vflag;
295 1.1 christos
296 1.1 christos hflag = 0;
297 1.1 christos iflag = 0;
298 1.1 christos qflag = 0;
299 1.1 christos nflag = 0;
300 1.1 christos vflag = 0;
301 1.1 christos while ((ch = getopt(argc, argv, "dhinqv")) != -1)
302 1.1 christos switch(ch) {
303 1.1 christos case 'd':
304 1.1 christos type = ACL_TYPE_DEFAULT;
305 1.1 christos break;
306 1.1 christos case 'h':
307 1.1 christos hflag = 1;
308 1.1 christos break;
309 1.1 christos case 'i':
310 1.1 christos iflag = 1;
311 1.1 christos break;
312 1.1 christos case 'n':
313 1.1 christos nflag = 1;
314 1.1 christos break;
315 1.1 christos case 'q':
316 1.1 christos qflag = 1;
317 1.1 christos break;
318 1.1 christos case 'v':
319 1.1 christos vflag = 1;
320 1.1 christos break;
321 1.1 christos default:
322 1.1 christos usage();
323 1.1 christos }
324 1.1 christos argc -= optind;
325 1.1 christos argv += optind;
326 1.1 christos
327 1.1 christos if (argc == 0) {
328 1.1 christos error = print_acl_from_stdin(type, hflag, iflag, nflag,
329 1.1 christos qflag, vflag);
330 1.1 christos return(error ? 1 : 0);
331 1.1 christos }
332 1.1 christos
333 1.1 christos for (i = 0; i < argc; i++) {
334 1.1 christos if (!strcmp(argv[i], "-")) {
335 1.1 christos error = print_acl_from_stdin(type, hflag, iflag, nflag,
336 1.1 christos qflag, vflag);
337 1.1 christos if (error == -1)
338 1.1 christos carried_error = -1;
339 1.1 christos } else {
340 1.1 christos error = print_acl(argv[i], type, hflag, iflag, nflag,
341 1.1 christos qflag, vflag);
342 1.1 christos if (error == -1)
343 1.1 christos carried_error = -1;
344 1.1 christos }
345 1.1 christos }
346 1.1 christos
347 1.1 christos return(carried_error ? 1 : 0);
348 1.1 christos }
349