xref: /freebsd/bin/getfacl/getfacl.c (revision 9768746b)
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 		return(-1);
161 	}
162 
163 	printf("%s", acl_text);
164 
165 	(void)acl_free(acl);
166 	(void)acl_free(acl_text);
167 
168 	return(0);
169 }
170 
171 static int
172 print_acl_from_stdin(acl_type_t type, int hflag, int iflag, int nflag,
173     int qflag, int vflag)
174 {
175 	char	*p, pathname[PATH_MAX];
176 	int	carried_error = 0;
177 
178 	while (fgets(pathname, (int)sizeof(pathname), stdin)) {
179 		if ((p = strchr(pathname, '\n')) != NULL)
180 			*p = '\0';
181 		if (print_acl(pathname, type, hflag, iflag, nflag,
182 		    qflag, vflag) == -1) {
183 			carried_error = -1;
184 		}
185 	}
186 
187 	return(carried_error);
188 }
189 
190 int
191 main(int argc, char *argv[])
192 {
193 	acl_type_t	type = ACL_TYPE_ACCESS;
194 	int	carried_error = 0;
195 	int	ch, error, i;
196 	int	hflag, iflag, qflag, nflag, vflag;
197 
198 	hflag = 0;
199 	iflag = 0;
200 	qflag = 0;
201 	nflag = 0;
202 	vflag = 0;
203 	while ((ch = getopt(argc, argv, "dhinqv")) != -1)
204 		switch(ch) {
205 		case 'd':
206 			type = ACL_TYPE_DEFAULT;
207 			break;
208 		case 'h':
209 			hflag = 1;
210 			break;
211 		case 'i':
212 			iflag = 1;
213 			break;
214 		case 'n':
215 			nflag = 1;
216 			break;
217 		case 'q':
218 			qflag = 1;
219 			break;
220 		case 'v':
221 			vflag = 1;
222 			break;
223 		default:
224 			usage();
225 			return(-1);
226 		}
227 	argc -= optind;
228 	argv += optind;
229 
230 	if (argc == 0) {
231 		error = print_acl_from_stdin(type, hflag, iflag, nflag,
232 		    qflag, vflag);
233 		return(error ? 1 : 0);
234 	}
235 
236 	for (i = 0; i < argc; i++) {
237 		if (!strcmp(argv[i], "-")) {
238 			error = print_acl_from_stdin(type, hflag, iflag, nflag,
239 			    qflag, vflag);
240 			if (error == -1)
241 				carried_error = -1;
242 		} else {
243 			error = print_acl(argv[i], type, hflag, iflag, nflag,
244 			    qflag, vflag);
245 			if (error == -1)
246 				carried_error = -1;
247 		}
248 	}
249 
250 	return(carried_error ? 1 : 0);
251 }
252