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