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