dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / getfacl / getfacl.c
blobab1a73d0a02190e73ec5e695159cce86f97d17a6
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 static char sccsid[] = "%Z%%M% %I% %E% SMI";
31 * getfacl [-ad] file ...
32 * This command displays discretionary information for a file or files.
33 * display format:
34 * # file: filename
35 * # owner: uid
36 * # group: gid
37 * user::perm
38 * user:uid:perm
39 * group::perm
40 * group:gid:perm
41 * mask:perm
42 * other:perm
43 * default:user::perm
44 * default:user:uid:perm
45 * default:group::perm
46 * default:group:gid:perm
47 * default:mask:perm
48 * default:other:perm
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <pwd.h>
54 #include <grp.h>
55 #include <locale.h>
56 #include <sys/acl.h>
57 #include <errno.h>
59 static char *pruname(uid_t);
60 static char *prgname(gid_t);
61 static char *display(int);
62 static void usage();
65 int
66 main(int argc, char *argv[])
68 int c;
69 int aflag = 0;
70 int dflag = 0;
71 int errflag = 0;
72 int savecnt;
73 int aclcnt;
74 int mask;
75 aclent_t *aclp;
76 aclent_t *tp;
77 char *permp;
79 (void) setlocale(LC_ALL, "");
80 (void) textdomain(TEXT_DOMAIN);
82 if (argc < 2)
83 usage();
85 while ((c = getopt(argc, argv, "ad")) != EOF) {
86 switch (c) {
87 case 'a':
88 aflag++;
89 break;
90 case 'd':
91 dflag++;
92 break;
93 case '?':
94 errflag++;
95 break;
98 if (errflag)
99 usage();
101 if (optind >= argc)
102 usage();
104 for (; optind < argc; optind++) {
105 register char *filep;
107 filep = argv[optind];
109 /* Get ACL info of the files */
110 errno = 0;
111 if ((aclcnt = acl(filep, GETACLCNT, 0, NULL)) < 0) {
112 if (errno == ENOSYS) {
113 (void) fprintf(stderr,
114 gettext("File system doesn't support "
115 "aclent_t style ACL's.\n"
116 "See acl(5) for more information on "
117 "Solaris ACL support.\n"));
118 exit(2);
120 perror(filep);
121 exit(2);
123 if (aclcnt < MIN_ACL_ENTRIES) {
124 (void) fprintf(stderr,
125 gettext("%d: acl count too small from %s\n"),
126 aclcnt, filep);
127 exit(2);
130 if ((aclp = (aclent_t *)malloc(sizeof (aclent_t) * aclcnt))
131 == NULL) {
132 (void) fprintf(stderr,
133 gettext("Insufficient memory\n"));
134 exit(1);
137 errno = 0;
138 if (acl(filep, GETACL, aclcnt, aclp) < 0) {
139 perror(filep);
140 exit(2);
143 /* display ACL: assume it is sorted. */
144 (void) printf("\n# file: %s\n", filep);
145 savecnt = aclcnt;
146 for (tp = aclp; aclcnt--; tp++) {
147 if (tp->a_type == USER_OBJ)
148 (void) printf("# owner: %s\n",
149 pruname(tp->a_id));
150 if (tp->a_type == GROUP_OBJ)
151 (void) printf("# group: %s\n",
152 prgname(tp->a_id));
153 if (tp->a_type == CLASS_OBJ)
154 mask = tp->a_perm;
156 aclcnt = savecnt;
157 for (tp = aclp; aclcnt--; tp++) {
158 switch (tp->a_type) {
159 case USER:
160 if (!dflag) {
161 permp = display(tp->a_perm);
162 (void) printf("user:%s:%s\t\t",
163 pruname(tp->a_id), permp);
164 free(permp);
165 permp = display(tp->a_perm & mask);
166 (void) printf(
167 "#effective:%s\n", permp);
168 free(permp);
170 break;
171 case USER_OBJ:
172 if (!dflag) {
173 /* no need to display uid */
174 permp = display(tp->a_perm);
175 (void) printf("user::%s\n", permp);
176 free(permp);
178 break;
179 case GROUP:
180 if (!dflag) {
181 permp = display(tp->a_perm);
182 (void) printf("group:%s:%s\t\t",
183 prgname(tp->a_id), permp);
184 free(permp);
185 permp = display(tp->a_perm & mask);
186 (void) printf(
187 "#effective:%s\n", permp);
188 free(permp);
190 break;
191 case GROUP_OBJ:
192 if (!dflag) {
193 permp = display(tp->a_perm);
194 (void) printf("group::%s\t\t", permp);
195 free(permp);
196 permp = display(tp->a_perm & mask);
197 (void) printf(
198 "#effective:%s\n", permp);
199 free(permp);
201 break;
202 case CLASS_OBJ:
203 if (!dflag) {
204 permp = display(tp->a_perm);
205 (void) printf("mask:%s\n", permp);
206 free(permp);
208 break;
209 case OTHER_OBJ:
210 if (!dflag) {
211 permp = display(tp->a_perm);
212 (void) printf("other:%s\n", permp);
213 free(permp);
215 break;
216 case DEF_USER:
217 if (!aflag) {
218 permp = display(tp->a_perm);
219 (void) printf("default:user:%s:%s\n",
220 pruname(tp->a_id), permp);
221 free(permp);
223 break;
224 case DEF_USER_OBJ:
225 if (!aflag) {
226 permp = display(tp->a_perm);
227 (void) printf("default:user::%s\n",
228 permp);
229 free(permp);
231 break;
232 case DEF_GROUP:
233 if (!aflag) {
234 permp = display(tp->a_perm);
235 (void) printf("default:group:%s:%s\n",
236 prgname(tp->a_id), permp);
237 free(permp);
239 break;
240 case DEF_GROUP_OBJ:
241 if (!aflag) {
242 permp = display(tp->a_perm);
243 (void) printf("default:group::%s\n",
244 permp);
245 free(permp);
247 break;
248 case DEF_CLASS_OBJ:
249 if (!aflag) {
250 permp = display(tp->a_perm);
251 (void) printf("default:mask:%s\n",
252 permp);
253 free(permp);
255 break;
256 case DEF_OTHER_OBJ:
257 if (!aflag) {
258 permp = display(tp->a_perm);
259 (void) printf("default:other:%s\n",
260 permp);
261 free(permp);
263 break;
264 default:
265 (void) fprintf(stderr,
266 gettext("unrecognized entry\n"));
267 break;
270 free(aclp);
272 return (0);
275 static char *
276 display(int perm)
278 char *buf;
280 buf = malloc(4);
281 if (buf == NULL) {
282 (void) fprintf(stderr, gettext("Insufficient memory\n"));
283 exit(1);
286 if (perm & 4)
287 buf[0] = 'r';
288 else
289 buf[0] = '-';
290 if (perm & 2)
291 buf[1] = 'w';
292 else
293 buf[1] = '-';
294 if (perm & 1)
295 buf[2] = 'x';
296 else
297 buf[2] = '-';
298 buf[3] = '\0';
299 return (buf);
302 static char *
303 pruname(uid_t uid)
305 struct passwd *passwdp;
306 static char uidp[10]; /* big enough */
308 passwdp = getpwuid(uid);
309 if (passwdp == NULL) {
310 /* could not get passwd information: display uid instead */
311 (void) sprintf(uidp, "%u", uid);
312 return (uidp);
313 } else
314 return (passwdp->pw_name);
317 static char *
318 prgname(gid_t gid)
320 struct group *groupp;
321 static char gidp[10]; /* big enough */
323 groupp = getgrgid(gid);
324 if (groupp == NULL) {
325 /* could not get group information: display gid instead */
326 (void) sprintf(gidp, "%u", gid);
327 return (gidp);
328 } else
329 return (groupp->gr_name);
332 static void
333 usage()
335 (void) fprintf(stderr,
336 gettext("usage: getfacl [-ad] file ... \n"));
337 exit(1);