xref: /freebsd/bin/getfacl/getfacl.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * Copyright (c) 1999, 2001, 2002 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 <grp.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 static int more_than_one = 0;
52 
53 static void
54 usage(void)
55 {
56 
57 	fprintf(stderr, "getfacl [-dhnqv] [file ...]\n");
58 }
59 
60 static char *
61 getuname(uid_t uid)
62 {
63 	struct passwd *pw;
64 	static char uids[10];
65 
66 	if ((pw = getpwuid(uid)) == NULL) {
67 		(void)snprintf(uids, sizeof(uids), "%u", uid);
68 		return (uids);
69 	} else
70 		return (pw->pw_name);
71 }
72 
73 static char *
74 getgname(gid_t gid)
75 {
76 	struct group *gr;
77 	static char gids[10];
78 
79 	if ((gr = getgrgid(gid)) == NULL) {
80 		(void)snprintf(gids, sizeof(gids), "%u", gid);
81 		return (gids);
82 	} else
83 		return (gr->gr_name);
84 }
85 
86 static int
87 print_acl(char *path, acl_type_t type, int hflag, int iflag, int nflag,
88     int qflag, int vflag)
89 {
90 	struct stat	sb;
91 	acl_t	acl;
92 	char	*acl_text;
93 	int	error, flags = 0, ret;
94 
95 	if (hflag)
96 		error = lstat(path, &sb);
97 	else
98 		error = stat(path, &sb);
99 	if (error == -1) {
100 		warn("%s: stat() failed", path);
101 		return(-1);
102 	}
103 
104 	if (hflag)
105 		ret = lpathconf(path, _PC_ACL_NFS4);
106 	else
107 		ret = pathconf(path, _PC_ACL_NFS4);
108 	if (ret > 0) {
109 		if (type == ACL_TYPE_DEFAULT) {
110 			warnx("%s: there are no default entries in NFSv4 ACLs",
111 			    path);
112 			return (-1);
113 		}
114 		type = ACL_TYPE_NFS4;
115 	} else if (ret < 0 && errno != EINVAL) {
116 		warn("%s: pathconf(..., _PC_ACL_NFS4) failed", path);
117 		return (-1);
118 	}
119 
120 	if (more_than_one)
121 		printf("\n");
122 	else
123 		more_than_one++;
124 
125 	if (!qflag)
126 		printf("# file: %s\n# owner: %s\n# group: %s\n", path,
127 		    getuname(sb.st_uid), getgname(sb.st_gid));
128 
129 	if (hflag)
130 		acl = acl_get_link_np(path, type);
131 	else
132 		acl = acl_get_file(path, type);
133 	if (!acl) {
134 		if (errno != EOPNOTSUPP) {
135 			warn("%s", path);
136 			return(-1);
137 		}
138 		errno = 0;
139 		if (type == ACL_TYPE_DEFAULT)
140 			return(0);
141 		acl = acl_from_mode_np(sb.st_mode);
142 		if (!acl) {
143 			warn("%s: acl_from_mode() failed", path);
144 			return(-1);
145 		}
146 	}
147 
148 	if (iflag)
149 		flags |= ACL_TEXT_APPEND_ID;
150 
151 	if (nflag)
152 		flags |= ACL_TEXT_NUMERIC_IDS;
153 
154 	if (vflag)
155 		flags |= ACL_TEXT_VERBOSE;
156 
157 	acl_text = acl_to_text_np(acl, 0, flags);
158 	if (!acl_text) {
159 		warn("%s: acl_to_text_np() failed", path);
160 		(void)acl_free(acl);
161 		return(-1);
162 	}
163 
164 	printf("%s", acl_text);
165 
166 	(void)acl_free(acl);
167 	(void)acl_free(acl_text);
168 
169 	return(0);
170 }
171 
172 static int
173 print_acl_from_stdin(acl_type_t type, int hflag, int iflag, int nflag,
174     int qflag, int vflag)
175 {
176 	char	*p, pathname[PATH_MAX];
177 	int	carried_error = 0;
178 
179 	while (fgets(pathname, (int)sizeof(pathname), stdin)) {
180 		if ((p = strchr(pathname, '\n')) != NULL)
181 			*p = '\0';
182 		if (print_acl(pathname, type, hflag, iflag, nflag,
183 		    qflag, vflag) == -1) {
184 			carried_error = -1;
185 		}
186 	}
187 
188 	return(carried_error);
189 }
190 
191 int
192 main(int argc, char *argv[])
193 {
194 	acl_type_t	type = ACL_TYPE_ACCESS;
195 	int	carried_error = 0;
196 	int	ch, error, i;
197 	int	hflag, iflag, qflag, nflag, vflag;
198 
199 	hflag = 0;
200 	iflag = 0;
201 	qflag = 0;
202 	nflag = 0;
203 	vflag = 0;
204 	while ((ch = getopt(argc, argv, "dhinqv")) != -1)
205 		switch(ch) {
206 		case 'd':
207 			type = ACL_TYPE_DEFAULT;
208 			break;
209 		case 'h':
210 			hflag = 1;
211 			break;
212 		case 'i':
213 			iflag = 1;
214 			break;
215 		case 'n':
216 			nflag = 1;
217 			break;
218 		case 'q':
219 			qflag = 1;
220 			break;
221 		case 'v':
222 			vflag = 1;
223 			break;
224 		default:
225 			usage();
226 			return(-1);
227 		}
228 	argc -= optind;
229 	argv += optind;
230 
231 	if (argc == 0) {
232 		error = print_acl_from_stdin(type, hflag, iflag, nflag,
233 		    qflag, vflag);
234 		return(error ? 1 : 0);
235 	}
236 
237 	for (i = 0; i < argc; i++) {
238 		if (!strcmp(argv[i], "-")) {
239 			error = print_acl_from_stdin(type, hflag, iflag, nflag,
240 			    qflag, vflag);
241 			if (error == -1)
242 				carried_error = -1;
243 		} else {
244 			error = print_acl(argv[i], type, hflag, iflag, nflag,
245 			    qflag, vflag);
246 			if (error == -1)
247 				carried_error = -1;
248 		}
249 	}
250 
251 	return(carried_error ? 1 : 0);
252 }
253