xref: /onnv-gate/usr/src/cmd/getfacl/getfacl.c (revision 4321:a8930ec16e52)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4321Scasper  * Common Development and Distribution License (the "License").
6*4321Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #ifndef lint
290Sstevel@tonic-gate static char sccsid[] = "%Z%%M%	%I%	%E% SMI";
300Sstevel@tonic-gate #endif
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * getfacl [-ad] file ...
340Sstevel@tonic-gate  * This command displays discretionary information for a file or files.
350Sstevel@tonic-gate  * display format:
360Sstevel@tonic-gate  *	# file: filename
370Sstevel@tonic-gate  *	# owner: uid
380Sstevel@tonic-gate  *	# group: gid
390Sstevel@tonic-gate  *	user::perm
400Sstevel@tonic-gate  *	user:uid:perm
410Sstevel@tonic-gate  *	group::perm
420Sstevel@tonic-gate  *	group:gid:perm
430Sstevel@tonic-gate  *	mask:perm
440Sstevel@tonic-gate  *	other:perm
450Sstevel@tonic-gate  *	default:user::perm
460Sstevel@tonic-gate  *	default:user:uid:perm
470Sstevel@tonic-gate  *	default:group::perm
480Sstevel@tonic-gate  *	default:group:gid:perm
490Sstevel@tonic-gate  *	default:mask:perm
500Sstevel@tonic-gate  *	default:other:perm
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <stdlib.h>
540Sstevel@tonic-gate #include <stdio.h>
550Sstevel@tonic-gate #include <pwd.h>
560Sstevel@tonic-gate #include <grp.h>
570Sstevel@tonic-gate #include <locale.h>
580Sstevel@tonic-gate #include <sys/acl.h>
590Sstevel@tonic-gate #include <errno.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static char	*pruname(uid_t);
620Sstevel@tonic-gate static char	*prgname(gid_t);
630Sstevel@tonic-gate static char	*display(int);
640Sstevel@tonic-gate static void	usage();
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 
670Sstevel@tonic-gate int
main(int argc,char * argv[])680Sstevel@tonic-gate main(int argc, char *argv[])
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	int		c;
710Sstevel@tonic-gate 	int		aflag = 0;
720Sstevel@tonic-gate 	int		dflag = 0;
730Sstevel@tonic-gate 	int		errflag = 0;
740Sstevel@tonic-gate 	int		savecnt;
750Sstevel@tonic-gate 	int		aclcnt;
760Sstevel@tonic-gate 	int		mask;
770Sstevel@tonic-gate 	aclent_t	*aclp;
780Sstevel@tonic-gate 	aclent_t	*tp;
790Sstevel@tonic-gate 	char		*permp;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
820Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if (argc < 2)
850Sstevel@tonic-gate 		usage();
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ad")) != EOF) {
880Sstevel@tonic-gate 		switch (c) {
890Sstevel@tonic-gate 		case 'a':
900Sstevel@tonic-gate 			aflag++;
910Sstevel@tonic-gate 			break;
920Sstevel@tonic-gate 		case 'd':
930Sstevel@tonic-gate 			dflag++;
940Sstevel@tonic-gate 			break;
950Sstevel@tonic-gate 		case '?':
960Sstevel@tonic-gate 			errflag++;
970Sstevel@tonic-gate 			break;
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 	if (errflag)
1010Sstevel@tonic-gate 		usage();
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (optind >= argc)
1040Sstevel@tonic-gate 		usage();
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	for (; optind < argc; optind++) {
1070Sstevel@tonic-gate 		register char *filep;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		filep = argv[optind];
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		/* Get ACL info of the files */
1120Sstevel@tonic-gate 		errno = 0;
1130Sstevel@tonic-gate 		if ((aclcnt = acl(filep, GETACLCNT, 0, NULL)) < 0) {
114789Sahrens 			if (errno == ENOSYS) {
115789Sahrens 				(void) fprintf(stderr,
116789Sahrens 				    gettext("File system doesn't support "
117789Sahrens 				    "aclent_t style ACL's.\n"
118789Sahrens 				    "See acl(5) for more information on "
119789Sahrens 				    "Solaris ACL support.\n"));
120789Sahrens 				exit(2);
121789Sahrens 			}
1220Sstevel@tonic-gate 			perror(filep);
1230Sstevel@tonic-gate 			exit(2);
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 		if (aclcnt < MIN_ACL_ENTRIES) {
1260Sstevel@tonic-gate 			(void) fprintf(stderr,
1270Sstevel@tonic-gate 			    gettext("%d: acl count too small from %s\n"),
1280Sstevel@tonic-gate 			    aclcnt, filep);
1290Sstevel@tonic-gate 			exit(2);
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		if ((aclp = (aclent_t *)malloc(sizeof (aclent_t) * aclcnt))
1330Sstevel@tonic-gate 		    == NULL) {
1340Sstevel@tonic-gate 			(void) fprintf(stderr,
1350Sstevel@tonic-gate 			    gettext("Insufficient memory\n"));
1360Sstevel@tonic-gate 			exit(1);
1370Sstevel@tonic-gate 		}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 		errno = 0;
1400Sstevel@tonic-gate 		if (acl(filep, GETACL, aclcnt, aclp) < 0) {
1410Sstevel@tonic-gate 			perror(filep);
1420Sstevel@tonic-gate 			exit(2);
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		/* display ACL: assume it is sorted. */
1460Sstevel@tonic-gate 		(void) printf("\n# file: %s\n", filep);
1470Sstevel@tonic-gate 		savecnt = aclcnt;
1480Sstevel@tonic-gate 		for (tp = aclp; aclcnt--; tp++) {
1490Sstevel@tonic-gate 			if (tp->a_type == USER_OBJ)
1500Sstevel@tonic-gate 				(void) printf("# owner: %s\n",
1510Sstevel@tonic-gate 				    pruname(tp->a_id));
1520Sstevel@tonic-gate 			if (tp->a_type == GROUP_OBJ)
1530Sstevel@tonic-gate 				(void) printf("# group: %s\n",
1540Sstevel@tonic-gate 				    prgname(tp->a_id));
1550Sstevel@tonic-gate 			if (tp->a_type == CLASS_OBJ)
1560Sstevel@tonic-gate 				mask = tp->a_perm;
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 		aclcnt = savecnt;
1590Sstevel@tonic-gate 		for (tp = aclp; aclcnt--; tp++) {
1600Sstevel@tonic-gate 			switch (tp->a_type) {
1610Sstevel@tonic-gate 			case USER:
1620Sstevel@tonic-gate 				if (!dflag) {
1630Sstevel@tonic-gate 					permp = display(tp->a_perm);
1640Sstevel@tonic-gate 					(void) printf("user:%s:%s\t\t",
1650Sstevel@tonic-gate 					    pruname(tp->a_id), permp);
1660Sstevel@tonic-gate 					free(permp);
1670Sstevel@tonic-gate 					permp = display(tp->a_perm & mask);
1680Sstevel@tonic-gate 					(void) printf(
1690Sstevel@tonic-gate 					    "#effective:%s\n", permp);
1700Sstevel@tonic-gate 					free(permp);
1710Sstevel@tonic-gate 				}
1720Sstevel@tonic-gate 				break;
1730Sstevel@tonic-gate 			case USER_OBJ:
1740Sstevel@tonic-gate 				if (!dflag) {
1750Sstevel@tonic-gate 					/* no need to display uid */
1760Sstevel@tonic-gate 					permp = display(tp->a_perm);
1770Sstevel@tonic-gate 					(void) printf("user::%s\n", permp);
1780Sstevel@tonic-gate 					free(permp);
1790Sstevel@tonic-gate 				}
1800Sstevel@tonic-gate 				break;
1810Sstevel@tonic-gate 			case GROUP:
1820Sstevel@tonic-gate 				if (!dflag) {
1830Sstevel@tonic-gate 					permp = display(tp->a_perm);
1840Sstevel@tonic-gate 					(void) printf("group:%s:%s\t\t",
1850Sstevel@tonic-gate 					    prgname(tp->a_id), permp);
1860Sstevel@tonic-gate 					free(permp);
1870Sstevel@tonic-gate 					permp = display(tp->a_perm & mask);
1880Sstevel@tonic-gate 					(void) printf(
1890Sstevel@tonic-gate 					    "#effective:%s\n", permp);
1900Sstevel@tonic-gate 					free(permp);
1910Sstevel@tonic-gate 				}
1920Sstevel@tonic-gate 				break;
1930Sstevel@tonic-gate 			case GROUP_OBJ:
1940Sstevel@tonic-gate 				if (!dflag) {
1950Sstevel@tonic-gate 					permp = display(tp->a_perm);
1960Sstevel@tonic-gate 					(void) printf("group::%s\t\t", permp);
1970Sstevel@tonic-gate 					free(permp);
1980Sstevel@tonic-gate 					permp = display(tp->a_perm & mask);
1990Sstevel@tonic-gate 					(void) printf(
2000Sstevel@tonic-gate 					    "#effective:%s\n", permp);
2010Sstevel@tonic-gate 					free(permp);
2020Sstevel@tonic-gate 				}
2030Sstevel@tonic-gate 				break;
2040Sstevel@tonic-gate 			case CLASS_OBJ:
2050Sstevel@tonic-gate 				if (!dflag) {
2060Sstevel@tonic-gate 					permp = display(tp->a_perm);
2070Sstevel@tonic-gate 					(void) printf("mask:%s\n", permp);
2080Sstevel@tonic-gate 					free(permp);
2090Sstevel@tonic-gate 				}
2100Sstevel@tonic-gate 				break;
2110Sstevel@tonic-gate 			case OTHER_OBJ:
2120Sstevel@tonic-gate 				if (!dflag) {
2130Sstevel@tonic-gate 					permp = display(tp->a_perm);
2140Sstevel@tonic-gate 					(void) printf("other:%s\n", permp);
2150Sstevel@tonic-gate 					free(permp);
2160Sstevel@tonic-gate 				}
2170Sstevel@tonic-gate 				break;
2180Sstevel@tonic-gate 			case DEF_USER:
2190Sstevel@tonic-gate 				if (!aflag) {
2200Sstevel@tonic-gate 					permp = display(tp->a_perm);
2210Sstevel@tonic-gate 					(void) printf("default:user:%s:%s\n",
2220Sstevel@tonic-gate 					    pruname(tp->a_id), permp);
2230Sstevel@tonic-gate 					free(permp);
2240Sstevel@tonic-gate 				}
2250Sstevel@tonic-gate 				break;
2260Sstevel@tonic-gate 			case DEF_USER_OBJ:
2270Sstevel@tonic-gate 				if (!aflag) {
2280Sstevel@tonic-gate 					permp = display(tp->a_perm);
2290Sstevel@tonic-gate 					(void) printf("default:user::%s\n",
2300Sstevel@tonic-gate 					    permp);
2310Sstevel@tonic-gate 					free(permp);
2320Sstevel@tonic-gate 				}
2330Sstevel@tonic-gate 				break;
2340Sstevel@tonic-gate 			case DEF_GROUP:
2350Sstevel@tonic-gate 				if (!aflag) {
2360Sstevel@tonic-gate 					permp = display(tp->a_perm);
2370Sstevel@tonic-gate 					(void) printf("default:group:%s:%s\n",
2380Sstevel@tonic-gate 					    prgname(tp->a_id), permp);
2390Sstevel@tonic-gate 					free(permp);
2400Sstevel@tonic-gate 				}
2410Sstevel@tonic-gate 				break;
2420Sstevel@tonic-gate 			case DEF_GROUP_OBJ:
2430Sstevel@tonic-gate 				if (!aflag) {
2440Sstevel@tonic-gate 					permp = display(tp->a_perm);
2450Sstevel@tonic-gate 					(void) printf("default:group::%s\n",
2460Sstevel@tonic-gate 					    permp);
2470Sstevel@tonic-gate 					free(permp);
2480Sstevel@tonic-gate 				}
2490Sstevel@tonic-gate 				break;
2500Sstevel@tonic-gate 			case DEF_CLASS_OBJ:
2510Sstevel@tonic-gate 				if (!aflag) {
2520Sstevel@tonic-gate 					permp = display(tp->a_perm);
2530Sstevel@tonic-gate 					(void) printf("default:mask:%s\n",
2540Sstevel@tonic-gate 					    permp);
2550Sstevel@tonic-gate 					free(permp);
2560Sstevel@tonic-gate 				}
2570Sstevel@tonic-gate 				break;
2580Sstevel@tonic-gate 			case DEF_OTHER_OBJ:
2590Sstevel@tonic-gate 				if (!aflag) {
2600Sstevel@tonic-gate 					permp = display(tp->a_perm);
2610Sstevel@tonic-gate 					(void) printf("default:other:%s\n",
2620Sstevel@tonic-gate 					    permp);
2630Sstevel@tonic-gate 					free(permp);
2640Sstevel@tonic-gate 				}
2650Sstevel@tonic-gate 				break;
2660Sstevel@tonic-gate 			default:
2670Sstevel@tonic-gate 				(void) fprintf(stderr,
2680Sstevel@tonic-gate 				    gettext("unrecognized entry\n"));
2690Sstevel@tonic-gate 				break;
2700Sstevel@tonic-gate 			}
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 		free(aclp);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	return (0);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate static char *
display(int perm)2780Sstevel@tonic-gate display(int perm)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	char	*buf;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	buf = malloc(4);
2830Sstevel@tonic-gate 	if (buf == NULL) {
2840Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Insufficient memory\n"));
2850Sstevel@tonic-gate 		exit(1);
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (perm & 4)
2890Sstevel@tonic-gate 		buf[0] = 'r';
2900Sstevel@tonic-gate 	else
2910Sstevel@tonic-gate 		buf[0] = '-';
2920Sstevel@tonic-gate 	if (perm & 2)
2930Sstevel@tonic-gate 		buf[1] = 'w';
2940Sstevel@tonic-gate 	else
2950Sstevel@tonic-gate 		buf[1] = '-';
2960Sstevel@tonic-gate 	if (perm & 1)
2970Sstevel@tonic-gate 		buf[2] = 'x';
2980Sstevel@tonic-gate 	else
2990Sstevel@tonic-gate 		buf[2] = '-';
3000Sstevel@tonic-gate 	buf[3] = '\0';
3010Sstevel@tonic-gate 	return (buf);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate static char *
pruname(uid_t uid)3050Sstevel@tonic-gate pruname(uid_t uid)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	struct passwd	*passwdp;
3080Sstevel@tonic-gate 	static char	uidp[10];	/* big enough */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	passwdp = getpwuid(uid);
311789Sahrens 	if (passwdp == (struct passwd *)NULL) {
3120Sstevel@tonic-gate 		/* could not get passwd information: display uid instead */
313*4321Scasper 		(void) sprintf(uidp, "%u", uid);
3140Sstevel@tonic-gate 		return (uidp);
3150Sstevel@tonic-gate 	} else
3160Sstevel@tonic-gate 		return (passwdp->pw_name);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate static char *
prgname(gid_t gid)3200Sstevel@tonic-gate prgname(gid_t gid)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	struct group	*groupp;
3230Sstevel@tonic-gate 	static char	gidp[10];	/* big enough */
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	groupp = getgrgid(gid);
326789Sahrens 	if (groupp == (struct group *)NULL) {
3270Sstevel@tonic-gate 		/* could not get group information: display gid instead */
328*4321Scasper 		(void) sprintf(gidp, "%u", gid);
3290Sstevel@tonic-gate 		return (gidp);
3300Sstevel@tonic-gate 	} else
3310Sstevel@tonic-gate 		return (groupp->gr_name);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate static void
usage()3350Sstevel@tonic-gate usage()
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	(void) fprintf(stderr,
3380Sstevel@tonic-gate 	    gettext("usage: getfacl [-ad] file ... \n"));
3390Sstevel@tonic-gate 	exit(1);
3400Sstevel@tonic-gate }
341