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 5f48205beScasper * Common Development and Distribution License (the "License"). 6f48205beScasper * 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 /* 22f48205beScasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*f5f3cbecSPeter Tribble * Copyright 2020 Peter Tribble. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * getfacl [-ad] file ... 297c478bd9Sstevel@tonic-gate * This command displays discretionary information for a file or files. 307c478bd9Sstevel@tonic-gate * display format: 317c478bd9Sstevel@tonic-gate * # file: filename 327c478bd9Sstevel@tonic-gate * # owner: uid 337c478bd9Sstevel@tonic-gate * # group: gid 347c478bd9Sstevel@tonic-gate * user::perm 357c478bd9Sstevel@tonic-gate * user:uid:perm 367c478bd9Sstevel@tonic-gate * group::perm 377c478bd9Sstevel@tonic-gate * group:gid:perm 387c478bd9Sstevel@tonic-gate * mask:perm 397c478bd9Sstevel@tonic-gate * other:perm 407c478bd9Sstevel@tonic-gate * default:user::perm 417c478bd9Sstevel@tonic-gate * default:user:uid:perm 427c478bd9Sstevel@tonic-gate * default:group::perm 437c478bd9Sstevel@tonic-gate * default:group:gid:perm 447c478bd9Sstevel@tonic-gate * default:mask:perm 457c478bd9Sstevel@tonic-gate * default:other:perm 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #include <stdlib.h> 497c478bd9Sstevel@tonic-gate #include <stdio.h> 507c478bd9Sstevel@tonic-gate #include <pwd.h> 517c478bd9Sstevel@tonic-gate #include <grp.h> 527c478bd9Sstevel@tonic-gate #include <locale.h> 537c478bd9Sstevel@tonic-gate #include <sys/acl.h> 547c478bd9Sstevel@tonic-gate #include <errno.h> 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate static char *pruname(uid_t); 577c478bd9Sstevel@tonic-gate static char *prgname(gid_t); 587c478bd9Sstevel@tonic-gate static char *display(int); 597c478bd9Sstevel@tonic-gate static void usage(); 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate int 637c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 647c478bd9Sstevel@tonic-gate { 657c478bd9Sstevel@tonic-gate int c; 667c478bd9Sstevel@tonic-gate int aflag = 0; 677c478bd9Sstevel@tonic-gate int dflag = 0; 687c478bd9Sstevel@tonic-gate int errflag = 0; 697c478bd9Sstevel@tonic-gate int savecnt; 707c478bd9Sstevel@tonic-gate int aclcnt; 71*f5f3cbecSPeter Tribble int mask = 0; 727c478bd9Sstevel@tonic-gate aclent_t *aclp; 737c478bd9Sstevel@tonic-gate aclent_t *tp; 747c478bd9Sstevel@tonic-gate char *permp; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 777c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate if (argc < 2) 807c478bd9Sstevel@tonic-gate usage(); 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "ad")) != EOF) { 837c478bd9Sstevel@tonic-gate switch (c) { 847c478bd9Sstevel@tonic-gate case 'a': 857c478bd9Sstevel@tonic-gate aflag++; 867c478bd9Sstevel@tonic-gate break; 877c478bd9Sstevel@tonic-gate case 'd': 887c478bd9Sstevel@tonic-gate dflag++; 897c478bd9Sstevel@tonic-gate break; 907c478bd9Sstevel@tonic-gate case '?': 917c478bd9Sstevel@tonic-gate errflag++; 927c478bd9Sstevel@tonic-gate break; 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate if (errflag) 967c478bd9Sstevel@tonic-gate usage(); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate if (optind >= argc) 997c478bd9Sstevel@tonic-gate usage(); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate for (; optind < argc; optind++) { 1027c478bd9Sstevel@tonic-gate register char *filep; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate filep = argv[optind]; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* Get ACL info of the files */ 1077c478bd9Sstevel@tonic-gate errno = 0; 1087c478bd9Sstevel@tonic-gate if ((aclcnt = acl(filep, GETACLCNT, 0, NULL)) < 0) { 109fa9e4066Sahrens if (errno == ENOSYS) { 110fa9e4066Sahrens (void) fprintf(stderr, 111fa9e4066Sahrens gettext("File system doesn't support " 112fa9e4066Sahrens "aclent_t style ACL's.\n" 113fa9e4066Sahrens "See acl(5) for more information on " 114*f5f3cbecSPeter Tribble "POSIX-draft ACL support.\n")); 115fa9e4066Sahrens exit(2); 116fa9e4066Sahrens } 1177c478bd9Sstevel@tonic-gate perror(filep); 1187c478bd9Sstevel@tonic-gate exit(2); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate if (aclcnt < MIN_ACL_ENTRIES) { 1217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1227c478bd9Sstevel@tonic-gate gettext("%d: acl count too small from %s\n"), 1237c478bd9Sstevel@tonic-gate aclcnt, filep); 1247c478bd9Sstevel@tonic-gate exit(2); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if ((aclp = (aclent_t *)malloc(sizeof (aclent_t) * aclcnt)) 1287c478bd9Sstevel@tonic-gate == NULL) { 1297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1307c478bd9Sstevel@tonic-gate gettext("Insufficient memory\n")); 1317c478bd9Sstevel@tonic-gate exit(1); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate errno = 0; 1357c478bd9Sstevel@tonic-gate if (acl(filep, GETACL, aclcnt, aclp) < 0) { 1367c478bd9Sstevel@tonic-gate perror(filep); 1377c478bd9Sstevel@tonic-gate exit(2); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* display ACL: assume it is sorted. */ 1417c478bd9Sstevel@tonic-gate (void) printf("\n# file: %s\n", filep); 1427c478bd9Sstevel@tonic-gate savecnt = aclcnt; 1437c478bd9Sstevel@tonic-gate for (tp = aclp; aclcnt--; tp++) { 1447c478bd9Sstevel@tonic-gate if (tp->a_type == USER_OBJ) 1457c478bd9Sstevel@tonic-gate (void) printf("# owner: %s\n", 1467c478bd9Sstevel@tonic-gate pruname(tp->a_id)); 1477c478bd9Sstevel@tonic-gate if (tp->a_type == GROUP_OBJ) 1487c478bd9Sstevel@tonic-gate (void) printf("# group: %s\n", 1497c478bd9Sstevel@tonic-gate prgname(tp->a_id)); 1507c478bd9Sstevel@tonic-gate if (tp->a_type == CLASS_OBJ) 1517c478bd9Sstevel@tonic-gate mask = tp->a_perm; 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate aclcnt = savecnt; 1547c478bd9Sstevel@tonic-gate for (tp = aclp; aclcnt--; tp++) { 1557c478bd9Sstevel@tonic-gate switch (tp->a_type) { 1567c478bd9Sstevel@tonic-gate case USER: 1577c478bd9Sstevel@tonic-gate if (!dflag) { 1587c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 1597c478bd9Sstevel@tonic-gate (void) printf("user:%s:%s\t\t", 1607c478bd9Sstevel@tonic-gate pruname(tp->a_id), permp); 1617c478bd9Sstevel@tonic-gate free(permp); 1627c478bd9Sstevel@tonic-gate permp = display(tp->a_perm & mask); 1637c478bd9Sstevel@tonic-gate (void) printf( 1647c478bd9Sstevel@tonic-gate "#effective:%s\n", permp); 1657c478bd9Sstevel@tonic-gate free(permp); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate break; 1687c478bd9Sstevel@tonic-gate case USER_OBJ: 1697c478bd9Sstevel@tonic-gate if (!dflag) { 1707c478bd9Sstevel@tonic-gate /* no need to display uid */ 1717c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 1727c478bd9Sstevel@tonic-gate (void) printf("user::%s\n", permp); 1737c478bd9Sstevel@tonic-gate free(permp); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate break; 1767c478bd9Sstevel@tonic-gate case GROUP: 1777c478bd9Sstevel@tonic-gate if (!dflag) { 1787c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 1797c478bd9Sstevel@tonic-gate (void) printf("group:%s:%s\t\t", 1807c478bd9Sstevel@tonic-gate prgname(tp->a_id), permp); 1817c478bd9Sstevel@tonic-gate free(permp); 1827c478bd9Sstevel@tonic-gate permp = display(tp->a_perm & mask); 1837c478bd9Sstevel@tonic-gate (void) printf( 1847c478bd9Sstevel@tonic-gate "#effective:%s\n", permp); 1857c478bd9Sstevel@tonic-gate free(permp); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate break; 1887c478bd9Sstevel@tonic-gate case GROUP_OBJ: 1897c478bd9Sstevel@tonic-gate if (!dflag) { 1907c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 1917c478bd9Sstevel@tonic-gate (void) printf("group::%s\t\t", permp); 1927c478bd9Sstevel@tonic-gate free(permp); 1937c478bd9Sstevel@tonic-gate permp = display(tp->a_perm & mask); 1947c478bd9Sstevel@tonic-gate (void) printf( 1957c478bd9Sstevel@tonic-gate "#effective:%s\n", permp); 1967c478bd9Sstevel@tonic-gate free(permp); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate break; 1997c478bd9Sstevel@tonic-gate case CLASS_OBJ: 2007c478bd9Sstevel@tonic-gate if (!dflag) { 2017c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 2027c478bd9Sstevel@tonic-gate (void) printf("mask:%s\n", permp); 2037c478bd9Sstevel@tonic-gate free(permp); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate break; 2067c478bd9Sstevel@tonic-gate case OTHER_OBJ: 2077c478bd9Sstevel@tonic-gate if (!dflag) { 2087c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 2097c478bd9Sstevel@tonic-gate (void) printf("other:%s\n", permp); 2107c478bd9Sstevel@tonic-gate free(permp); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate break; 2137c478bd9Sstevel@tonic-gate case DEF_USER: 2147c478bd9Sstevel@tonic-gate if (!aflag) { 2157c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 2167c478bd9Sstevel@tonic-gate (void) printf("default:user:%s:%s\n", 2177c478bd9Sstevel@tonic-gate pruname(tp->a_id), permp); 2187c478bd9Sstevel@tonic-gate free(permp); 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate break; 2217c478bd9Sstevel@tonic-gate case DEF_USER_OBJ: 2227c478bd9Sstevel@tonic-gate if (!aflag) { 2237c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 2247c478bd9Sstevel@tonic-gate (void) printf("default:user::%s\n", 2257c478bd9Sstevel@tonic-gate permp); 2267c478bd9Sstevel@tonic-gate free(permp); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate break; 2297c478bd9Sstevel@tonic-gate case DEF_GROUP: 2307c478bd9Sstevel@tonic-gate if (!aflag) { 2317c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 2327c478bd9Sstevel@tonic-gate (void) printf("default:group:%s:%s\n", 2337c478bd9Sstevel@tonic-gate prgname(tp->a_id), permp); 2347c478bd9Sstevel@tonic-gate free(permp); 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate break; 2377c478bd9Sstevel@tonic-gate case DEF_GROUP_OBJ: 2387c478bd9Sstevel@tonic-gate if (!aflag) { 2397c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 2407c478bd9Sstevel@tonic-gate (void) printf("default:group::%s\n", 2417c478bd9Sstevel@tonic-gate permp); 2427c478bd9Sstevel@tonic-gate free(permp); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate break; 2457c478bd9Sstevel@tonic-gate case DEF_CLASS_OBJ: 2467c478bd9Sstevel@tonic-gate if (!aflag) { 2477c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 2487c478bd9Sstevel@tonic-gate (void) printf("default:mask:%s\n", 2497c478bd9Sstevel@tonic-gate permp); 2507c478bd9Sstevel@tonic-gate free(permp); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate break; 2537c478bd9Sstevel@tonic-gate case DEF_OTHER_OBJ: 2547c478bd9Sstevel@tonic-gate if (!aflag) { 2557c478bd9Sstevel@tonic-gate permp = display(tp->a_perm); 2567c478bd9Sstevel@tonic-gate (void) printf("default:other:%s\n", 2577c478bd9Sstevel@tonic-gate permp); 2587c478bd9Sstevel@tonic-gate free(permp); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate break; 2617c478bd9Sstevel@tonic-gate default: 2627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2637c478bd9Sstevel@tonic-gate gettext("unrecognized entry\n")); 2647c478bd9Sstevel@tonic-gate break; 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate free(aclp); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate return (0); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate static char * 2737c478bd9Sstevel@tonic-gate display(int perm) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate char *buf; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate buf = malloc(4); 2787c478bd9Sstevel@tonic-gate if (buf == NULL) { 2797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Insufficient memory\n")); 2807c478bd9Sstevel@tonic-gate exit(1); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate if (perm & 4) 2847c478bd9Sstevel@tonic-gate buf[0] = 'r'; 2857c478bd9Sstevel@tonic-gate else 2867c478bd9Sstevel@tonic-gate buf[0] = '-'; 2877c478bd9Sstevel@tonic-gate if (perm & 2) 2887c478bd9Sstevel@tonic-gate buf[1] = 'w'; 2897c478bd9Sstevel@tonic-gate else 2907c478bd9Sstevel@tonic-gate buf[1] = '-'; 2917c478bd9Sstevel@tonic-gate if (perm & 1) 2927c478bd9Sstevel@tonic-gate buf[2] = 'x'; 2937c478bd9Sstevel@tonic-gate else 2947c478bd9Sstevel@tonic-gate buf[2] = '-'; 2957c478bd9Sstevel@tonic-gate buf[3] = '\0'; 2967c478bd9Sstevel@tonic-gate return (buf); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate static char * 3007c478bd9Sstevel@tonic-gate pruname(uid_t uid) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate struct passwd *passwdp; 3037c478bd9Sstevel@tonic-gate static char uidp[10]; /* big enough */ 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate passwdp = getpwuid(uid); 3067c478bd9Sstevel@tonic-gate if (passwdp == (struct passwd *)NULL) { 3077c478bd9Sstevel@tonic-gate /* could not get passwd information: display uid instead */ 308f48205beScasper (void) sprintf(uidp, "%u", uid); 3097c478bd9Sstevel@tonic-gate return (uidp); 3107c478bd9Sstevel@tonic-gate } else 3117c478bd9Sstevel@tonic-gate return (passwdp->pw_name); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate static char * 3157c478bd9Sstevel@tonic-gate prgname(gid_t gid) 3167c478bd9Sstevel@tonic-gate { 3177c478bd9Sstevel@tonic-gate struct group *groupp; 3187c478bd9Sstevel@tonic-gate static char gidp[10]; /* big enough */ 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate groupp = getgrgid(gid); 3217c478bd9Sstevel@tonic-gate if (groupp == (struct group *)NULL) { 3227c478bd9Sstevel@tonic-gate /* could not get group information: display gid instead */ 323f48205beScasper (void) sprintf(gidp, "%u", gid); 3247c478bd9Sstevel@tonic-gate return (gidp); 3257c478bd9Sstevel@tonic-gate } else 3267c478bd9Sstevel@tonic-gate return (groupp->gr_name); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate static void 3307c478bd9Sstevel@tonic-gate usage() 3317c478bd9Sstevel@tonic-gate { 3327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3337c478bd9Sstevel@tonic-gate gettext("usage: getfacl [-ad] file ... \n")); 3347c478bd9Sstevel@tonic-gate exit(1); 3357c478bd9Sstevel@tonic-gate } 336