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