17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*f48205beScasper * Common Development and Distribution License (the "License").
6*f48205beScasper * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*f48205beScasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #ifndef lint
297c478bd9Sstevel@tonic-gate static char sccsid[] = "%Z%%M% %I% %E% SMI";
307c478bd9Sstevel@tonic-gate #endif
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * getfacl [-ad] file ...
347c478bd9Sstevel@tonic-gate * This command displays discretionary information for a file or files.
357c478bd9Sstevel@tonic-gate * display format:
367c478bd9Sstevel@tonic-gate * # file: filename
377c478bd9Sstevel@tonic-gate * # owner: uid
387c478bd9Sstevel@tonic-gate * # group: gid
397c478bd9Sstevel@tonic-gate * user::perm
407c478bd9Sstevel@tonic-gate * user:uid:perm
417c478bd9Sstevel@tonic-gate * group::perm
427c478bd9Sstevel@tonic-gate * group:gid:perm
437c478bd9Sstevel@tonic-gate * mask:perm
447c478bd9Sstevel@tonic-gate * other:perm
457c478bd9Sstevel@tonic-gate * default:user::perm
467c478bd9Sstevel@tonic-gate * default:user:uid:perm
477c478bd9Sstevel@tonic-gate * default:group::perm
487c478bd9Sstevel@tonic-gate * default:group:gid:perm
497c478bd9Sstevel@tonic-gate * default:mask:perm
507c478bd9Sstevel@tonic-gate * default:other:perm
517c478bd9Sstevel@tonic-gate */
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #include <stdlib.h>
547c478bd9Sstevel@tonic-gate #include <stdio.h>
557c478bd9Sstevel@tonic-gate #include <pwd.h>
567c478bd9Sstevel@tonic-gate #include <grp.h>
577c478bd9Sstevel@tonic-gate #include <locale.h>
587c478bd9Sstevel@tonic-gate #include <sys/acl.h>
597c478bd9Sstevel@tonic-gate #include <errno.h>
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate static char *pruname(uid_t);
627c478bd9Sstevel@tonic-gate static char *prgname(gid_t);
637c478bd9Sstevel@tonic-gate static char *display(int);
647c478bd9Sstevel@tonic-gate static void usage();
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])687c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate int c;
717c478bd9Sstevel@tonic-gate int aflag = 0;
727c478bd9Sstevel@tonic-gate int dflag = 0;
737c478bd9Sstevel@tonic-gate int errflag = 0;
747c478bd9Sstevel@tonic-gate int savecnt;
757c478bd9Sstevel@tonic-gate int aclcnt;
767c478bd9Sstevel@tonic-gate int mask;
777c478bd9Sstevel@tonic-gate aclent_t *aclp;
787c478bd9Sstevel@tonic-gate aclent_t *tp;
797c478bd9Sstevel@tonic-gate char *permp;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
827c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate if (argc < 2)
857c478bd9Sstevel@tonic-gate usage();
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "ad")) != EOF) {
887c478bd9Sstevel@tonic-gate switch (c) {
897c478bd9Sstevel@tonic-gate case 'a':
907c478bd9Sstevel@tonic-gate aflag++;
917c478bd9Sstevel@tonic-gate break;
927c478bd9Sstevel@tonic-gate case 'd':
937c478bd9Sstevel@tonic-gate dflag++;
947c478bd9Sstevel@tonic-gate break;
957c478bd9Sstevel@tonic-gate case '?':
967c478bd9Sstevel@tonic-gate errflag++;
977c478bd9Sstevel@tonic-gate break;
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate if (errflag)
1017c478bd9Sstevel@tonic-gate usage();
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate if (optind >= argc)
1047c478bd9Sstevel@tonic-gate usage();
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate for (; optind < argc; optind++) {
1077c478bd9Sstevel@tonic-gate register char *filep;
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate filep = argv[optind];
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /* Get ACL info of the files */
1127c478bd9Sstevel@tonic-gate errno = 0;
1137c478bd9Sstevel@tonic-gate if ((aclcnt = acl(filep, GETACLCNT, 0, NULL)) < 0) {
114fa9e4066Sahrens if (errno == ENOSYS) {
115fa9e4066Sahrens (void) fprintf(stderr,
116fa9e4066Sahrens gettext("File system doesn't support "
117fa9e4066Sahrens "aclent_t style ACL's.\n"
118fa9e4066Sahrens "See acl(5) for more information on "
119fa9e4066Sahrens "Solaris ACL support.\n"));
120fa9e4066Sahrens exit(2);
121fa9e4066Sahrens }
1227c478bd9Sstevel@tonic-gate perror(filep);
1237c478bd9Sstevel@tonic-gate exit(2);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate if (aclcnt < MIN_ACL_ENTRIES) {
1267c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1277c478bd9Sstevel@tonic-gate gettext("%d: acl count too small from %s\n"),
1287c478bd9Sstevel@tonic-gate aclcnt, filep);
1297c478bd9Sstevel@tonic-gate exit(2);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate if ((aclp = (aclent_t *)malloc(sizeof (aclent_t) * aclcnt))
1337c478bd9Sstevel@tonic-gate == NULL) {
1347c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1357c478bd9Sstevel@tonic-gate gettext("Insufficient memory\n"));
1367c478bd9Sstevel@tonic-gate exit(1);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate errno = 0;
1407c478bd9Sstevel@tonic-gate if (acl(filep, GETACL, aclcnt, aclp) < 0) {
1417c478bd9Sstevel@tonic-gate perror(filep);
1427c478bd9Sstevel@tonic-gate exit(2);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* display ACL: assume it is sorted. */
1467c478bd9Sstevel@tonic-gate (void) printf("\n# file: %s\n", filep);
1477c478bd9Sstevel@tonic-gate savecnt = aclcnt;
1487c478bd9Sstevel@tonic-gate for (tp = aclp; aclcnt--; tp++) {
1497c478bd9Sstevel@tonic-gate if (tp->a_type == USER_OBJ)
1507c478bd9Sstevel@tonic-gate (void) printf("# owner: %s\n",
1517c478bd9Sstevel@tonic-gate pruname(tp->a_id));
1527c478bd9Sstevel@tonic-gate if (tp->a_type == GROUP_OBJ)
1537c478bd9Sstevel@tonic-gate (void) printf("# group: %s\n",
1547c478bd9Sstevel@tonic-gate prgname(tp->a_id));
1557c478bd9Sstevel@tonic-gate if (tp->a_type == CLASS_OBJ)
1567c478bd9Sstevel@tonic-gate mask = tp->a_perm;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate aclcnt = savecnt;
1597c478bd9Sstevel@tonic-gate for (tp = aclp; aclcnt--; tp++) {
1607c478bd9Sstevel@tonic-gate switch (tp->a_type) {
1617c478bd9Sstevel@tonic-gate case USER:
1627c478bd9Sstevel@tonic-gate if (!dflag) {
1637c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
1647c478bd9Sstevel@tonic-gate (void) printf("user:%s:%s\t\t",
1657c478bd9Sstevel@tonic-gate pruname(tp->a_id), permp);
1667c478bd9Sstevel@tonic-gate free(permp);
1677c478bd9Sstevel@tonic-gate permp = display(tp->a_perm & mask);
1687c478bd9Sstevel@tonic-gate (void) printf(
1697c478bd9Sstevel@tonic-gate "#effective:%s\n", permp);
1707c478bd9Sstevel@tonic-gate free(permp);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate break;
1737c478bd9Sstevel@tonic-gate case USER_OBJ:
1747c478bd9Sstevel@tonic-gate if (!dflag) {
1757c478bd9Sstevel@tonic-gate /* no need to display uid */
1767c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
1777c478bd9Sstevel@tonic-gate (void) printf("user::%s\n", permp);
1787c478bd9Sstevel@tonic-gate free(permp);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate break;
1817c478bd9Sstevel@tonic-gate case GROUP:
1827c478bd9Sstevel@tonic-gate if (!dflag) {
1837c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
1847c478bd9Sstevel@tonic-gate (void) printf("group:%s:%s\t\t",
1857c478bd9Sstevel@tonic-gate prgname(tp->a_id), permp);
1867c478bd9Sstevel@tonic-gate free(permp);
1877c478bd9Sstevel@tonic-gate permp = display(tp->a_perm & mask);
1887c478bd9Sstevel@tonic-gate (void) printf(
1897c478bd9Sstevel@tonic-gate "#effective:%s\n", permp);
1907c478bd9Sstevel@tonic-gate free(permp);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate break;
1937c478bd9Sstevel@tonic-gate case GROUP_OBJ:
1947c478bd9Sstevel@tonic-gate if (!dflag) {
1957c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
1967c478bd9Sstevel@tonic-gate (void) printf("group::%s\t\t", permp);
1977c478bd9Sstevel@tonic-gate free(permp);
1987c478bd9Sstevel@tonic-gate permp = display(tp->a_perm & mask);
1997c478bd9Sstevel@tonic-gate (void) printf(
2007c478bd9Sstevel@tonic-gate "#effective:%s\n", permp);
2017c478bd9Sstevel@tonic-gate free(permp);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate case CLASS_OBJ:
2057c478bd9Sstevel@tonic-gate if (!dflag) {
2067c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
2077c478bd9Sstevel@tonic-gate (void) printf("mask:%s\n", permp);
2087c478bd9Sstevel@tonic-gate free(permp);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate break;
2117c478bd9Sstevel@tonic-gate case OTHER_OBJ:
2127c478bd9Sstevel@tonic-gate if (!dflag) {
2137c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
2147c478bd9Sstevel@tonic-gate (void) printf("other:%s\n", permp);
2157c478bd9Sstevel@tonic-gate free(permp);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate break;
2187c478bd9Sstevel@tonic-gate case DEF_USER:
2197c478bd9Sstevel@tonic-gate if (!aflag) {
2207c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
2217c478bd9Sstevel@tonic-gate (void) printf("default:user:%s:%s\n",
2227c478bd9Sstevel@tonic-gate pruname(tp->a_id), permp);
2237c478bd9Sstevel@tonic-gate free(permp);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate break;
2267c478bd9Sstevel@tonic-gate case DEF_USER_OBJ:
2277c478bd9Sstevel@tonic-gate if (!aflag) {
2287c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
2297c478bd9Sstevel@tonic-gate (void) printf("default:user::%s\n",
2307c478bd9Sstevel@tonic-gate permp);
2317c478bd9Sstevel@tonic-gate free(permp);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate break;
2347c478bd9Sstevel@tonic-gate case DEF_GROUP:
2357c478bd9Sstevel@tonic-gate if (!aflag) {
2367c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
2377c478bd9Sstevel@tonic-gate (void) printf("default:group:%s:%s\n",
2387c478bd9Sstevel@tonic-gate prgname(tp->a_id), permp);
2397c478bd9Sstevel@tonic-gate free(permp);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate break;
2427c478bd9Sstevel@tonic-gate case DEF_GROUP_OBJ:
2437c478bd9Sstevel@tonic-gate if (!aflag) {
2447c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
2457c478bd9Sstevel@tonic-gate (void) printf("default:group::%s\n",
2467c478bd9Sstevel@tonic-gate permp);
2477c478bd9Sstevel@tonic-gate free(permp);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate break;
2507c478bd9Sstevel@tonic-gate case DEF_CLASS_OBJ:
2517c478bd9Sstevel@tonic-gate if (!aflag) {
2527c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
2537c478bd9Sstevel@tonic-gate (void) printf("default:mask:%s\n",
2547c478bd9Sstevel@tonic-gate permp);
2557c478bd9Sstevel@tonic-gate free(permp);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate break;
2587c478bd9Sstevel@tonic-gate case DEF_OTHER_OBJ:
2597c478bd9Sstevel@tonic-gate if (!aflag) {
2607c478bd9Sstevel@tonic-gate permp = display(tp->a_perm);
2617c478bd9Sstevel@tonic-gate (void) printf("default:other:%s\n",
2627c478bd9Sstevel@tonic-gate permp);
2637c478bd9Sstevel@tonic-gate free(permp);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate break;
2667c478bd9Sstevel@tonic-gate default:
2677c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2687c478bd9Sstevel@tonic-gate gettext("unrecognized entry\n"));
2697c478bd9Sstevel@tonic-gate break;
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate free(aclp);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate return (0);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate static char *
display(int perm)2787c478bd9Sstevel@tonic-gate display(int perm)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate char *buf;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate buf = malloc(4);
2837c478bd9Sstevel@tonic-gate if (buf == NULL) {
2847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Insufficient memory\n"));
2857c478bd9Sstevel@tonic-gate exit(1);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate if (perm & 4)
2897c478bd9Sstevel@tonic-gate buf[0] = 'r';
2907c478bd9Sstevel@tonic-gate else
2917c478bd9Sstevel@tonic-gate buf[0] = '-';
2927c478bd9Sstevel@tonic-gate if (perm & 2)
2937c478bd9Sstevel@tonic-gate buf[1] = 'w';
2947c478bd9Sstevel@tonic-gate else
2957c478bd9Sstevel@tonic-gate buf[1] = '-';
2967c478bd9Sstevel@tonic-gate if (perm & 1)
2977c478bd9Sstevel@tonic-gate buf[2] = 'x';
2987c478bd9Sstevel@tonic-gate else
2997c478bd9Sstevel@tonic-gate buf[2] = '-';
3007c478bd9Sstevel@tonic-gate buf[3] = '\0';
3017c478bd9Sstevel@tonic-gate return (buf);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate static char *
pruname(uid_t uid)3057c478bd9Sstevel@tonic-gate pruname(uid_t uid)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate struct passwd *passwdp;
3087c478bd9Sstevel@tonic-gate static char uidp[10]; /* big enough */
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate passwdp = getpwuid(uid);
3117c478bd9Sstevel@tonic-gate if (passwdp == (struct passwd *)NULL) {
3127c478bd9Sstevel@tonic-gate /* could not get passwd information: display uid instead */
313*f48205beScasper (void) sprintf(uidp, "%u", uid);
3147c478bd9Sstevel@tonic-gate return (uidp);
3157c478bd9Sstevel@tonic-gate } else
3167c478bd9Sstevel@tonic-gate return (passwdp->pw_name);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate static char *
prgname(gid_t gid)3207c478bd9Sstevel@tonic-gate prgname(gid_t gid)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate struct group *groupp;
3237c478bd9Sstevel@tonic-gate static char gidp[10]; /* big enough */
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate groupp = getgrgid(gid);
3267c478bd9Sstevel@tonic-gate if (groupp == (struct group *)NULL) {
3277c478bd9Sstevel@tonic-gate /* could not get group information: display gid instead */
328*f48205beScasper (void) sprintf(gidp, "%u", gid);
3297c478bd9Sstevel@tonic-gate return (gidp);
3307c478bd9Sstevel@tonic-gate } else
3317c478bd9Sstevel@tonic-gate return (groupp->gr_name);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate static void
usage()3357c478bd9Sstevel@tonic-gate usage()
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3387c478bd9Sstevel@tonic-gate gettext("usage: getfacl [-ad] file ... \n"));
3397c478bd9Sstevel@tonic-gate exit(1);
3407c478bd9Sstevel@tonic-gate }
341