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 */ 22*bdcaf822Sbasabi /* 23*bdcaf822Sbasabi * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*bdcaf822Sbasabi * Use is subject to license terms. 25*bdcaf822Sbasabi */ 26*bdcaf822Sbasabi 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #ifndef lint 30*bdcaf822Sbasabi static char sccsid[] = "@(#)setfacl.c 1.10 05/06/16 SMI"; 317c478bd9Sstevel@tonic-gate #endif 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * setfacl [-r] -f aclfile file ... 357c478bd9Sstevel@tonic-gate * setfacl [-r] -d acl_entries file ... 367c478bd9Sstevel@tonic-gate * setfacl [-r] -m acl_entries file ... 377c478bd9Sstevel@tonic-gate * setfacl [-r] -s acl_entries file ... 387c478bd9Sstevel@tonic-gate * This command deletes/adds/modifies/sets discretionary information for a file 397c478bd9Sstevel@tonic-gate * or files. 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <stdlib.h> 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <pwd.h> 457c478bd9Sstevel@tonic-gate #include <grp.h> 467c478bd9Sstevel@tonic-gate #include <string.h> 477c478bd9Sstevel@tonic-gate #include <locale.h> 487c478bd9Sstevel@tonic-gate #include <sys/acl.h> 497c478bd9Sstevel@tonic-gate #include <sys/types.h> 507c478bd9Sstevel@tonic-gate #include <unistd.h> 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #define ADD 1 547c478bd9Sstevel@tonic-gate #define MODIFY 2 557c478bd9Sstevel@tonic-gate #define DELETE 3 567c478bd9Sstevel@tonic-gate #define SET 4 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate static int get_acl_info(char *filep, aclent_t **aclpp); 597c478bd9Sstevel@tonic-gate static int mod_entries(aclent_t *, int, char *, char *, char *, int); 607c478bd9Sstevel@tonic-gate static int set_file_entries(char *, char *, int); 617c478bd9Sstevel@tonic-gate static int set_online_entries(char *, char *, int); 627c478bd9Sstevel@tonic-gate static void usage(); 637c478bd9Sstevel@tonic-gate static int parse_entry_list(aclent_t **, int *, char *, int); 647c478bd9Sstevel@tonic-gate static int convert_to_aclent_t(char *, int *, aclent_t **, int); 657c478bd9Sstevel@tonic-gate static int parse_entry(char *, aclent_t *, int); 667c478bd9Sstevel@tonic-gate static void err_handle(int, aclent_t *); 677c478bd9Sstevel@tonic-gate static int conv_id(char *); 687c478bd9Sstevel@tonic-gate 69*bdcaf822Sbasabi int 707c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate int c; 737c478bd9Sstevel@tonic-gate int dflag = 0; 747c478bd9Sstevel@tonic-gate int mflag = 0; 757c478bd9Sstevel@tonic-gate int rflag = 0; 767c478bd9Sstevel@tonic-gate int sflag = 0; 777c478bd9Sstevel@tonic-gate int fflag = 0; 787c478bd9Sstevel@tonic-gate int errflag = 0; 797c478bd9Sstevel@tonic-gate int aclcnt; /* used by -m -d */ 807c478bd9Sstevel@tonic-gate aclent_t *aclp; /* used by -m -d */ 817c478bd9Sstevel@tonic-gate char *aclfilep; /* acl file argument */ 827c478bd9Sstevel@tonic-gate char *d_entryp = NULL; /* ptr to del entry list */ 837c478bd9Sstevel@tonic-gate char *m_entryp = NULL; /* ptr to mod entry list */ 847c478bd9Sstevel@tonic-gate char *s_entryp = NULL; /* ptr to set entry list */ 857c478bd9Sstevel@tonic-gate char *work_dp = NULL; /* working ptrs for the above */ 867c478bd9Sstevel@tonic-gate char *work_mp = NULL; 877c478bd9Sstevel@tonic-gate char *work_sp = NULL; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 907c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate if (argc < 3) 937c478bd9Sstevel@tonic-gate usage(); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "rm:d:s:f:")) != EOF) { 967c478bd9Sstevel@tonic-gate switch (c) { 977c478bd9Sstevel@tonic-gate case 'r': 987c478bd9Sstevel@tonic-gate rflag++; 997c478bd9Sstevel@tonic-gate break; 1007c478bd9Sstevel@tonic-gate case 'd': 1017c478bd9Sstevel@tonic-gate if (dflag || fflag || sflag) 1027c478bd9Sstevel@tonic-gate usage(); 1037c478bd9Sstevel@tonic-gate dflag++; 1047c478bd9Sstevel@tonic-gate d_entryp = optarg; 1057c478bd9Sstevel@tonic-gate break; 1067c478bd9Sstevel@tonic-gate case 'm': 1077c478bd9Sstevel@tonic-gate if (mflag || fflag || sflag) 1087c478bd9Sstevel@tonic-gate usage(); 1097c478bd9Sstevel@tonic-gate mflag++; 1107c478bd9Sstevel@tonic-gate m_entryp = optarg; 1117c478bd9Sstevel@tonic-gate break; 1127c478bd9Sstevel@tonic-gate case 's': 1137c478bd9Sstevel@tonic-gate if (fflag || sflag || mflag || dflag) 1147c478bd9Sstevel@tonic-gate usage(); 1157c478bd9Sstevel@tonic-gate sflag++; 1167c478bd9Sstevel@tonic-gate s_entryp = optarg; 1177c478bd9Sstevel@tonic-gate break; 1187c478bd9Sstevel@tonic-gate case 'f': 1197c478bd9Sstevel@tonic-gate if (fflag || sflag || mflag || dflag) 1207c478bd9Sstevel@tonic-gate usage(); 1217c478bd9Sstevel@tonic-gate fflag++; 1227c478bd9Sstevel@tonic-gate aclfilep = optarg; 1237c478bd9Sstevel@tonic-gate break; 1247c478bd9Sstevel@tonic-gate case '?': 1257c478bd9Sstevel@tonic-gate errflag++; 1267c478bd9Sstevel@tonic-gate break; 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate if (errflag) 1307c478bd9Sstevel@tonic-gate usage(); 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /* one of these flags should be set */ 1337c478bd9Sstevel@tonic-gate if (!fflag && !sflag && !mflag && !dflag) 1347c478bd9Sstevel@tonic-gate usage(); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /* no file arguments */ 1377c478bd9Sstevel@tonic-gate if (optind >= argc) 1387c478bd9Sstevel@tonic-gate usage(); 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate for (; optind < argc; optind++) { 1417c478bd9Sstevel@tonic-gate register char *filep; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate filep = argv[optind]; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* modify and delete: we need to get the ACL first */ 1467c478bd9Sstevel@tonic-gate if (mflag || dflag) { 1477c478bd9Sstevel@tonic-gate if (m_entryp != NULL) { 1487c478bd9Sstevel@tonic-gate free(work_mp); 1497c478bd9Sstevel@tonic-gate work_mp = strdup(m_entryp); 1507c478bd9Sstevel@tonic-gate if (work_mp == NULL) { 1517c478bd9Sstevel@tonic-gate fprintf(stderr, 1527c478bd9Sstevel@tonic-gate gettext("out of memory %s\n"), 1537c478bd9Sstevel@tonic-gate m_entryp); 1547c478bd9Sstevel@tonic-gate exit(1); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (d_entryp != NULL) { 1597c478bd9Sstevel@tonic-gate free(work_dp); 1607c478bd9Sstevel@tonic-gate work_dp = strdup(d_entryp); 1617c478bd9Sstevel@tonic-gate if (work_dp == NULL) { 1627c478bd9Sstevel@tonic-gate fprintf(stderr, 1637c478bd9Sstevel@tonic-gate gettext("out of memory %s\n"), 1647c478bd9Sstevel@tonic-gate d_entryp); 1657c478bd9Sstevel@tonic-gate exit(1); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate aclcnt = get_acl_info(filep, &aclp); 1707c478bd9Sstevel@tonic-gate if (aclcnt == -1) 1717c478bd9Sstevel@tonic-gate exit(2); 1727c478bd9Sstevel@tonic-gate if (mod_entries(aclp, aclcnt, work_mp, 1737c478bd9Sstevel@tonic-gate work_dp, filep, rflag) == -1) 1747c478bd9Sstevel@tonic-gate exit(2); 1757c478bd9Sstevel@tonic-gate } else if (fflag) { 1767c478bd9Sstevel@tonic-gate if (set_file_entries(aclfilep, filep, rflag) == -1) 1777c478bd9Sstevel@tonic-gate exit(2); 1787c478bd9Sstevel@tonic-gate } else if (sflag) { 1797c478bd9Sstevel@tonic-gate if (s_entryp != NULL) { 1807c478bd9Sstevel@tonic-gate free(work_sp); 1817c478bd9Sstevel@tonic-gate work_sp = strdup(s_entryp); 1827c478bd9Sstevel@tonic-gate if (work_sp == NULL) { 1837c478bd9Sstevel@tonic-gate fprintf(stderr, 1847c478bd9Sstevel@tonic-gate gettext("out of memory %s\n"), 1857c478bd9Sstevel@tonic-gate s_entryp); 1867c478bd9Sstevel@tonic-gate exit(1); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate if (set_online_entries(work_sp, filep, rflag) == -1) 1907c478bd9Sstevel@tonic-gate exit(2); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate } 193*bdcaf822Sbasabi return (0); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * For add, modify, and delete, we need to get the ACL of the file first. 1987c478bd9Sstevel@tonic-gate */ 1997c478bd9Sstevel@tonic-gate static int 2007c478bd9Sstevel@tonic-gate get_acl_info(char *filep, aclent_t **aclpp) 2017c478bd9Sstevel@tonic-gate { 2027c478bd9Sstevel@tonic-gate int aclcnt; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate if ((aclcnt = acl(filep, GETACLCNT, 0, NULL)) < 0) { 2057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2067c478bd9Sstevel@tonic-gate gettext("%s: failed to get acl count\n"), filep); 2077c478bd9Sstevel@tonic-gate perror("get acl count error"); 2087c478bd9Sstevel@tonic-gate return (-1); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate if (aclcnt < MIN_ACL_ENTRIES) { 2117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2127c478bd9Sstevel@tonic-gate gettext("%d: acl count is too small from %s\n"), 2137c478bd9Sstevel@tonic-gate aclcnt, filep); 2147c478bd9Sstevel@tonic-gate return (-1); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if ((*aclpp = (aclent_t *)malloc(sizeof (aclent_t) * aclcnt)) == NULL) { 2187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("out of memory\n")); 2197c478bd9Sstevel@tonic-gate return (-1); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate if (acl(filep, GETACL, aclcnt, *aclpp) < 0) { 2227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2237c478bd9Sstevel@tonic-gate gettext("%s: failed to get acl entries\n"), filep); 2247c478bd9Sstevel@tonic-gate perror("getacl error"); 2257c478bd9Sstevel@tonic-gate return (-1); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate return (aclcnt); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* 2317c478bd9Sstevel@tonic-gate * mod_entries() handles add, delete, and modify ACL entries of a file. 2327c478bd9Sstevel@tonic-gate * The real action is in convert_to_aclent_t() called by parse_entry_list(). 2337c478bd9Sstevel@tonic-gate * aclp: points ACL of a file and may be changed by lower level routine. 2347c478bd9Sstevel@tonic-gate * modp: modify entry list in ascii format 2357c478bd9Sstevel@tonic-gate * delp: delete entry list in ascii format 2367c478bd9Sstevel@tonic-gate * fnamep: file of interest 2377c478bd9Sstevel@tonic-gate */ 2387c478bd9Sstevel@tonic-gate static int 2397c478bd9Sstevel@tonic-gate mod_entries(aclent_t *aclp, int cnt, char *modp, char *delp, 2407c478bd9Sstevel@tonic-gate char *fnamep, int rfg) 2417c478bd9Sstevel@tonic-gate { 2427c478bd9Sstevel@tonic-gate int rc; /* return code */ 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* modify and add: from -m option */ 2457c478bd9Sstevel@tonic-gate if (parse_entry_list(&aclp, &cnt, modp, MODIFY) == -1) 2467c478bd9Sstevel@tonic-gate return (-1); 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* deletion: from -d option */ 2497c478bd9Sstevel@tonic-gate if (parse_entry_list(&aclp, &cnt, delp, DELETE) == -1) 2507c478bd9Sstevel@tonic-gate return (-1); 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate if (aclsort(cnt, rfg, aclp) == -1) { 2537c478bd9Sstevel@tonic-gate (void) err_handle(cnt, aclp); 2547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2557c478bd9Sstevel@tonic-gate gettext("aclcnt %d, file %s\n"), cnt, fnamep); 2567c478bd9Sstevel@tonic-gate return (-1); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate if (acl(fnamep, SETACL, cnt, aclp) < 0) { 2607c478bd9Sstevel@tonic-gate fprintf(stderr, 2617c478bd9Sstevel@tonic-gate gettext("%s: failed to set acl entries\n"), fnamep); 2627c478bd9Sstevel@tonic-gate perror("setacl error"); 2637c478bd9Sstevel@tonic-gate return (-1); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate return (0); 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /* 2697c478bd9Sstevel@tonic-gate * set_file_entries() creates ACL entries from ACL file (acl_fnamep). 2707c478bd9Sstevel@tonic-gate * It opens the file and converts every line (one line per acl entry) 2717c478bd9Sstevel@tonic-gate * into aclent_t format. It then recalculates the mask according to rflag. 2727c478bd9Sstevel@tonic-gate * Finally it sets ACL to the file (fnamep). 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate static int 2757c478bd9Sstevel@tonic-gate set_file_entries(char *acl_fnamep, char *fnamep, int rflag) 2767c478bd9Sstevel@tonic-gate { 2777c478bd9Sstevel@tonic-gate int aclcnt = 0; 2787c478bd9Sstevel@tonic-gate FILE *acl_fp; 2797c478bd9Sstevel@tonic-gate aclent_t *aclp; 2807c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 2817c478bd9Sstevel@tonic-gate char *tp; 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate if (strcmp(acl_fnamep, "-") == 0) 2847c478bd9Sstevel@tonic-gate acl_fp = stdin; 2857c478bd9Sstevel@tonic-gate else { 2867c478bd9Sstevel@tonic-gate if ((acl_fp = fopen(acl_fnamep, "r")) == NULL) { 2877c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Can't open acl file %s\n"), 2887c478bd9Sstevel@tonic-gate acl_fnamep); 2897c478bd9Sstevel@tonic-gate return (-1); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate while (fgets(buf, BUFSIZ, acl_fp) != NULL) { 2937c478bd9Sstevel@tonic-gate if (buf[0] == '#' || buf[0] == '\n') 2947c478bd9Sstevel@tonic-gate continue; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate /* check effective permission: add a null after real perm */ 2977c478bd9Sstevel@tonic-gate if ((tp = (char *)strchr(buf, '#')) != NULL) { 2987c478bd9Sstevel@tonic-gate tp--; 2997c478bd9Sstevel@tonic-gate while (*tp == ' ' || *tp == '\t') { 3007c478bd9Sstevel@tonic-gate if (tp != buf) 3017c478bd9Sstevel@tonic-gate tp--; 3027c478bd9Sstevel@tonic-gate else { 3037c478bd9Sstevel@tonic-gate fprintf(stderr, 3047c478bd9Sstevel@tonic-gate gettext("entry format error %s\n"), 3057c478bd9Sstevel@tonic-gate buf); 3067c478bd9Sstevel@tonic-gate exit(1); 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate *(tp+1) = '\0'; 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate /* remove <nl> at the end if there is one */ 3137c478bd9Sstevel@tonic-gate if ((tp = (char *)strchr(buf, '\n')) != NULL) 3147c478bd9Sstevel@tonic-gate *tp = '\0'; 3157c478bd9Sstevel@tonic-gate aclcnt++; 3167c478bd9Sstevel@tonic-gate if (convert_to_aclent_t(buf, &aclcnt, &aclp, SET) == -1) 3177c478bd9Sstevel@tonic-gate return (-1); 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate if (aclsort(aclcnt, rflag, aclp) == -1) { 3217c478bd9Sstevel@tonic-gate (void) err_handle(aclcnt, aclp); 3227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("aclcnt %d, aclfile %s\n"), 3237c478bd9Sstevel@tonic-gate aclcnt, acl_fnamep); 3247c478bd9Sstevel@tonic-gate return (-1); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate if (acl(fnamep, SETACL, aclcnt, aclp) < 0) { 3287c478bd9Sstevel@tonic-gate fprintf(stderr, 3297c478bd9Sstevel@tonic-gate gettext("%s: failed to set acl entries\n"), fnamep); 3307c478bd9Sstevel@tonic-gate perror("setacl error"); 3317c478bd9Sstevel@tonic-gate return (-1); 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate return (0); 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* 3377c478bd9Sstevel@tonic-gate * set_online_entries() parses the acl entries from command line (setp). 3387c478bd9Sstevel@tonic-gate * It converts the comma separated acl entries into aclent_t format. 3397c478bd9Sstevel@tonic-gate * It then recalculates the mask according to rflag. 3407c478bd9Sstevel@tonic-gate * Finally it sets ACL to the file (fnamep). 3417c478bd9Sstevel@tonic-gate */ 3427c478bd9Sstevel@tonic-gate static int 3437c478bd9Sstevel@tonic-gate set_online_entries(char *setp, char *fnamep, int rflag) 3447c478bd9Sstevel@tonic-gate { 3457c478bd9Sstevel@tonic-gate char *commap; 3467c478bd9Sstevel@tonic-gate aclent_t *aclp; 3477c478bd9Sstevel@tonic-gate int aclcnt = 0; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate if (parse_entry_list(&aclp, &aclcnt, setp, SET) == -1) 3507c478bd9Sstevel@tonic-gate return (-1); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate if (aclsort(aclcnt, rflag, aclp) == -1) { 3537c478bd9Sstevel@tonic-gate (void) err_handle(aclcnt, aclp); 3547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3557c478bd9Sstevel@tonic-gate gettext("aclcnt %d, file %s\n"), aclcnt, fnamep); 3567c478bd9Sstevel@tonic-gate return (-1); 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate if (acl(fnamep, SETACL, aclcnt, aclp) < 0) { 3607c478bd9Sstevel@tonic-gate fprintf(stderr, 3617c478bd9Sstevel@tonic-gate gettext("%s: failed to set acl entries\n"), fnamep); 3627c478bd9Sstevel@tonic-gate perror("setacl error"); 3637c478bd9Sstevel@tonic-gate return (-1); 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate return (0); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * parse_entry_list() parses entry list (listp) separated by commas. 3707c478bd9Sstevel@tonic-gate * Once it gets an ACL entry, it calls convert_to_aclent_t() to convert 3717c478bd9Sstevel@tonic-gate * to internal format. 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate static int 3747c478bd9Sstevel@tonic-gate parse_entry_list(aclent_t **aclpp, int *aclcntp, char *listp, int mode) 3757c478bd9Sstevel@tonic-gate { 3767c478bd9Sstevel@tonic-gate char *commap; 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate if (listp == NULL) 3797c478bd9Sstevel@tonic-gate return (0); 3807c478bd9Sstevel@tonic-gate while ((commap = (char *)strchr(listp, ',')) != NULL) { 3817c478bd9Sstevel@tonic-gate *commap = '\0'; 3827c478bd9Sstevel@tonic-gate *aclcntp += 1; 3837c478bd9Sstevel@tonic-gate /* aclcnt may be updated after the call: add or modify */ 3847c478bd9Sstevel@tonic-gate if (convert_to_aclent_t(listp, aclcntp, aclpp, mode) == -1) 3857c478bd9Sstevel@tonic-gate return (-1); 3867c478bd9Sstevel@tonic-gate listp = ++commap; 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate /* this is for only one entry or last entry */ 3897c478bd9Sstevel@tonic-gate if (*listp != '\0') { 3907c478bd9Sstevel@tonic-gate *aclcntp += 1; 3917c478bd9Sstevel@tonic-gate if (convert_to_aclent_t(listp, aclcntp, aclpp, mode) == -1) 3927c478bd9Sstevel@tonic-gate return (-1); 3937c478bd9Sstevel@tonic-gate } 394*bdcaf822Sbasabi return (0); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate /* 3987c478bd9Sstevel@tonic-gate * convert_to_aclent_t() converts an acl entry in ascii format (fields separated 3997c478bd9Sstevel@tonic-gate * by colon) into aclent_t and appends it to the current ACL. It also handles 4007c478bd9Sstevel@tonic-gate * memory allocation/deallocation for acl entries in aclent_t format. 4017c478bd9Sstevel@tonic-gate * aclpp that contains acl entries in acl format will be returned. 4027c478bd9Sstevel@tonic-gate * We don't check duplicates. 4037c478bd9Sstevel@tonic-gate */ 4047c478bd9Sstevel@tonic-gate static int 4057c478bd9Sstevel@tonic-gate convert_to_aclent_t(char *entryp, int *cntp, aclent_t **aclpp, int mode) 4067c478bd9Sstevel@tonic-gate { 4077c478bd9Sstevel@tonic-gate aclent_t *new_aclp; 4087c478bd9Sstevel@tonic-gate aclent_t tmpacl; 4097c478bd9Sstevel@tonic-gate aclent_t *taclp; 4107c478bd9Sstevel@tonic-gate int cur_cnt; 4117c478bd9Sstevel@tonic-gate int found = 0; 4127c478bd9Sstevel@tonic-gate int is_obj; 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate if (entryp == NULL) 4157c478bd9Sstevel@tonic-gate return (0); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if (*cntp > 1) 4187c478bd9Sstevel@tonic-gate new_aclp = (aclent_t *)realloc(*aclpp, 4197c478bd9Sstevel@tonic-gate sizeof (aclent_t) * (*cntp)); 4207c478bd9Sstevel@tonic-gate else 4217c478bd9Sstevel@tonic-gate new_aclp = (aclent_t *) malloc(sizeof (aclent_t) * (*cntp)); 4227c478bd9Sstevel@tonic-gate if (new_aclp == NULL) { 4237c478bd9Sstevel@tonic-gate fprintf(stderr, 4247c478bd9Sstevel@tonic-gate gettext("Insufficient memory for acl %d\n"), *cntp); 4257c478bd9Sstevel@tonic-gate return (-1); 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate tmpacl.a_id = 0; /* id field needs to be initialized */ 4297c478bd9Sstevel@tonic-gate if (entryp[0] == 'u') 4307c478bd9Sstevel@tonic-gate tmpacl.a_id = getuid(); /* id field for user */ 4317c478bd9Sstevel@tonic-gate if (entryp[0] == 'g') 4327c478bd9Sstevel@tonic-gate tmpacl.a_id = getgid(); /* id field for group */ 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate tmpacl.a_type = 0; 4357c478bd9Sstevel@tonic-gate if (parse_entry(entryp, &tmpacl, mode) == -1) 4367c478bd9Sstevel@tonic-gate return (-1); 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate is_obj = ((tmpacl.a_type == USER_OBJ) || 4397c478bd9Sstevel@tonic-gate (tmpacl.a_type == GROUP_OBJ) || 4407c478bd9Sstevel@tonic-gate (tmpacl.a_type == DEF_USER_OBJ) || 4417c478bd9Sstevel@tonic-gate (tmpacl.a_type == DEF_GROUP_OBJ)); 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate cur_cnt = *cntp - 1; 4447c478bd9Sstevel@tonic-gate switch (mode) { 4457c478bd9Sstevel@tonic-gate case MODIFY: /* and add */ 4467c478bd9Sstevel@tonic-gate for (taclp = new_aclp; cur_cnt-- > 0; taclp++) { 4477c478bd9Sstevel@tonic-gate if (taclp->a_type == tmpacl.a_type && 4487c478bd9Sstevel@tonic-gate ((taclp->a_id == tmpacl.a_id) || is_obj)) { 4497c478bd9Sstevel@tonic-gate found++; 4507c478bd9Sstevel@tonic-gate /* cnt is added before it's called */ 4517c478bd9Sstevel@tonic-gate *cntp -= 1; 4527c478bd9Sstevel@tonic-gate taclp->a_perm = tmpacl.a_perm; 4537c478bd9Sstevel@tonic-gate break; 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate if (!found) /* Add it to the end: no need to change cntp */ 4577c478bd9Sstevel@tonic-gate memcpy(new_aclp + *cntp -1, &tmpacl, sizeof (aclent_t)); 4587c478bd9Sstevel@tonic-gate break; 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate case DELETE: 4617c478bd9Sstevel@tonic-gate for (taclp = new_aclp; cur_cnt-- > 0; taclp++) { 4627c478bd9Sstevel@tonic-gate if (taclp->a_type == tmpacl.a_type && 4637c478bd9Sstevel@tonic-gate ((taclp->a_id == tmpacl.a_id) || is_obj)) { 4647c478bd9Sstevel@tonic-gate found++; 4657c478bd9Sstevel@tonic-gate /* move up the rest */ 4667c478bd9Sstevel@tonic-gate while (cur_cnt-- > 0) { 4677c478bd9Sstevel@tonic-gate memcpy(taclp, taclp+1, 4687c478bd9Sstevel@tonic-gate sizeof (aclent_t)); 4697c478bd9Sstevel@tonic-gate taclp++; 4707c478bd9Sstevel@tonic-gate } 4717c478bd9Sstevel@tonic-gate *cntp = *cntp - 2; 4727c478bd9Sstevel@tonic-gate break; 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate if (!found) 4767c478bd9Sstevel@tonic-gate *cntp -= 1; 4777c478bd9Sstevel@tonic-gate break; 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate case SET: 4807c478bd9Sstevel@tonic-gate /* we may check duplicate before copying over?? */ 4817c478bd9Sstevel@tonic-gate memcpy(new_aclp + *cntp -1, &tmpacl, sizeof (aclent_t)); 4827c478bd9Sstevel@tonic-gate break; 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate default: 4857c478bd9Sstevel@tonic-gate fprintf(stderr, 4867c478bd9Sstevel@tonic-gate gettext("Unrecognized mode: internal error\n")); 4877c478bd9Sstevel@tonic-gate break; 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate *aclpp = new_aclp; /* return new acl entries */ 4917c478bd9Sstevel@tonic-gate return (0); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate static void 4957c478bd9Sstevel@tonic-gate usage() 4967c478bd9Sstevel@tonic-gate { 4977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("usage:\n")); 4987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4997c478bd9Sstevel@tonic-gate gettext("\tsetfacl [-r] -f aclfile file ...\n")); 5007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5017c478bd9Sstevel@tonic-gate gettext("\tsetfacl [-r] -d acl_entries file ...\n")); 5027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5037c478bd9Sstevel@tonic-gate gettext("\tsetfacl [-r] -m acl_entries file ...\n")); 5047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5057c478bd9Sstevel@tonic-gate gettext("\tsetfacl [-r] -s acl_entries file ...\n")); 5067c478bd9Sstevel@tonic-gate exit(1); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate static void 5107c478bd9Sstevel@tonic-gate err_handle(int cnt, aclent_t *aclentp) 5117c478bd9Sstevel@tonic-gate { 5127c478bd9Sstevel@tonic-gate int rc; 5137c478bd9Sstevel@tonic-gate int which; 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate rc = aclcheck(aclentp, cnt, &which); 5167c478bd9Sstevel@tonic-gate switch (rc) { 5177c478bd9Sstevel@tonic-gate case USER_ERROR: 5187c478bd9Sstevel@tonic-gate fprintf(stderr, 5197c478bd9Sstevel@tonic-gate gettext("There is more than one user owner entry")); 5207c478bd9Sstevel@tonic-gate fprintf(stderr, 5217c478bd9Sstevel@tonic-gate gettext(" -- error found at entry index %d\n"), which); 5227c478bd9Sstevel@tonic-gate break; 5237c478bd9Sstevel@tonic-gate case GRP_ERROR: 5247c478bd9Sstevel@tonic-gate fprintf(stderr, 5257c478bd9Sstevel@tonic-gate gettext("There is more than one group owner entry")); 5267c478bd9Sstevel@tonic-gate fprintf(stderr, 5277c478bd9Sstevel@tonic-gate gettext(" -- error found at entry index %d\n"), which); 5287c478bd9Sstevel@tonic-gate break; 5297c478bd9Sstevel@tonic-gate case CLASS_ERROR: 5307c478bd9Sstevel@tonic-gate fprintf(stderr, 5317c478bd9Sstevel@tonic-gate gettext("There is more than one mask entry")); 5327c478bd9Sstevel@tonic-gate fprintf(stderr, 5337c478bd9Sstevel@tonic-gate gettext(" -- error found at entry index %d\n"), which); 5347c478bd9Sstevel@tonic-gate break; 5357c478bd9Sstevel@tonic-gate case OTHER_ERROR: 5367c478bd9Sstevel@tonic-gate fprintf(stderr, 5377c478bd9Sstevel@tonic-gate gettext("There is more than one other entry")); 5387c478bd9Sstevel@tonic-gate fprintf(stderr, 5397c478bd9Sstevel@tonic-gate gettext(" -- error found at entry index %d\n"), which); 5407c478bd9Sstevel@tonic-gate break; 5417c478bd9Sstevel@tonic-gate case DUPLICATE_ERROR: 5427c478bd9Sstevel@tonic-gate fprintf(stderr, 5437c478bd9Sstevel@tonic-gate gettext("Duplicate user or group entries")); 5447c478bd9Sstevel@tonic-gate fprintf(stderr, 5457c478bd9Sstevel@tonic-gate gettext(" -- error found at entry index %d\n"), which); 5467c478bd9Sstevel@tonic-gate break; 5477c478bd9Sstevel@tonic-gate case MISS_ERROR: 5487c478bd9Sstevel@tonic-gate fprintf(stderr, 5497c478bd9Sstevel@tonic-gate gettext("Missing user/group owner, other, mask entry\n")); 5507c478bd9Sstevel@tonic-gate break; 5517c478bd9Sstevel@tonic-gate case MEM_ERROR: 5527c478bd9Sstevel@tonic-gate fprintf(stderr, 5537c478bd9Sstevel@tonic-gate gettext("Insufficient memory\n")); 5547c478bd9Sstevel@tonic-gate break; 5557c478bd9Sstevel@tonic-gate case ENTRY_ERROR: 5567c478bd9Sstevel@tonic-gate fprintf(stderr, 5577c478bd9Sstevel@tonic-gate gettext("Unrecognized entry type")); 5587c478bd9Sstevel@tonic-gate fprintf(stderr, 5597c478bd9Sstevel@tonic-gate gettext(" -- error found at entry index %d\n"), which); 5607c478bd9Sstevel@tonic-gate break; 5617c478bd9Sstevel@tonic-gate default: 5627c478bd9Sstevel@tonic-gate /* error is not from aclcheck */ 5637c478bd9Sstevel@tonic-gate fprintf(stderr, 5647c478bd9Sstevel@tonic-gate gettext("aclsort error\n")); 5657c478bd9Sstevel@tonic-gate break; 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate } 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate static int 5707c478bd9Sstevel@tonic-gate parse_entry(char *fieldp, aclent_t *aclentp, int mode) 5717c478bd9Sstevel@tonic-gate { 5727c478bd9Sstevel@tonic-gate char *colonp; 5737c478bd9Sstevel@tonic-gate int def_flag = 0, mo_flag = 0; 5747c478bd9Sstevel@tonic-gate int id; 5757c478bd9Sstevel@tonic-gate struct passwd *pwp; 5767c478bd9Sstevel@tonic-gate struct group *grp; 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate colonp = (char *)strchr(fieldp, ':'); 5797c478bd9Sstevel@tonic-gate if (colonp == NULL) { 5807c478bd9Sstevel@tonic-gate fprintf(stderr, 5817c478bd9Sstevel@tonic-gate gettext("Can't find colon delimiter %s\n"), fieldp); 5827c478bd9Sstevel@tonic-gate return (-1); 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate *colonp = '\0'; 5857c478bd9Sstevel@tonic-gate if ((strcmp(fieldp, "default") == 0) || (strcmp(fieldp, "d") == 0)) { 5867c478bd9Sstevel@tonic-gate def_flag++; 5877c478bd9Sstevel@tonic-gate fieldp = ++colonp; 5887c478bd9Sstevel@tonic-gate colonp = (char *)strchr(fieldp, ':'); 5897c478bd9Sstevel@tonic-gate if (colonp == NULL) { 5907c478bd9Sstevel@tonic-gate fprintf(stderr, 5917c478bd9Sstevel@tonic-gate gettext("Can't find colon delimiter %s\n"), fieldp); 5927c478bd9Sstevel@tonic-gate return (-1); 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate *colonp = '\0'; 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate /* process entry type */ 5987c478bd9Sstevel@tonic-gate if ((strcmp(fieldp, "user") == 0) || (strcmp(fieldp, "u") == 0)) { 5997c478bd9Sstevel@tonic-gate if (def_flag) 6007c478bd9Sstevel@tonic-gate aclentp->a_type = DEF_USER; 6017c478bd9Sstevel@tonic-gate else 6027c478bd9Sstevel@tonic-gate aclentp->a_type = USER; 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate if ((strcmp(fieldp, "group") == 0) || (strcmp(fieldp, "g") == 0)) { 6057c478bd9Sstevel@tonic-gate if (def_flag) 6067c478bd9Sstevel@tonic-gate aclentp->a_type = DEF_GROUP; 6077c478bd9Sstevel@tonic-gate else 6087c478bd9Sstevel@tonic-gate aclentp->a_type = GROUP; 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate if ((strcmp(fieldp, "mask") == 0) || (strcmp(fieldp, "m") == 0)) { 6117c478bd9Sstevel@tonic-gate if (def_flag) 6127c478bd9Sstevel@tonic-gate aclentp->a_type = DEF_CLASS_OBJ; 6137c478bd9Sstevel@tonic-gate else 6147c478bd9Sstevel@tonic-gate aclentp->a_type = CLASS_OBJ; 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate if ((strcmp(fieldp, "other") == 0) || (strcmp(fieldp, "o") == 0)) { 6177c478bd9Sstevel@tonic-gate if (def_flag) 6187c478bd9Sstevel@tonic-gate aclentp->a_type = DEF_OTHER_OBJ; 6197c478bd9Sstevel@tonic-gate else 6207c478bd9Sstevel@tonic-gate aclentp->a_type = OTHER_OBJ; 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate /* still can't determine entry type */ 6247c478bd9Sstevel@tonic-gate if (aclentp->a_type == 0) { 6257c478bd9Sstevel@tonic-gate fprintf(stderr, 6267c478bd9Sstevel@tonic-gate gettext("Unrecognized entry type %s \n"), fieldp); 6277c478bd9Sstevel@tonic-gate return (-1); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate /* mask and other entries dont have id field */ 6317c478bd9Sstevel@tonic-gate if (aclentp->a_type != CLASS_OBJ && aclentp->a_type != OTHER_OBJ && 6327c478bd9Sstevel@tonic-gate aclentp->a_type != DEF_CLASS_OBJ && 6337c478bd9Sstevel@tonic-gate aclentp->a_type != DEF_OTHER_OBJ) { 6347c478bd9Sstevel@tonic-gate /* process id: */ 6357c478bd9Sstevel@tonic-gate fieldp = ++colonp; 6367c478bd9Sstevel@tonic-gate colonp = (char *)strchr(fieldp, ':'); 6377c478bd9Sstevel@tonic-gate if (colonp == NULL) { 6387c478bd9Sstevel@tonic-gate if (mode != DELETE) { 6397c478bd9Sstevel@tonic-gate fprintf(stderr, 6407c478bd9Sstevel@tonic-gate gettext("Can't find colon delimiter %s\n"), 6417c478bd9Sstevel@tonic-gate fieldp); 6427c478bd9Sstevel@tonic-gate return (-1); 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate } else 6457c478bd9Sstevel@tonic-gate *colonp = '\0'; 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate if (*fieldp == '\0') { 6487c478bd9Sstevel@tonic-gate /* empty uid */ 6497c478bd9Sstevel@tonic-gate if (aclentp->a_type == USER) 6507c478bd9Sstevel@tonic-gate aclentp->a_type = USER_OBJ; 6517c478bd9Sstevel@tonic-gate if (aclentp->a_type == DEF_USER) 6527c478bd9Sstevel@tonic-gate aclentp->a_type = DEF_USER_OBJ; 6537c478bd9Sstevel@tonic-gate if (aclentp->a_type == GROUP) 6547c478bd9Sstevel@tonic-gate aclentp->a_type = GROUP_OBJ; 6557c478bd9Sstevel@tonic-gate if (aclentp->a_type == DEF_GROUP) 6567c478bd9Sstevel@tonic-gate aclentp->a_type = DEF_GROUP_OBJ; 6577c478bd9Sstevel@tonic-gate } else { 6587c478bd9Sstevel@tonic-gate /* see if it's a user/group name */ 6597c478bd9Sstevel@tonic-gate if (aclentp->a_type == USER || 6607c478bd9Sstevel@tonic-gate aclentp->a_type == USER_OBJ || 6617c478bd9Sstevel@tonic-gate aclentp->a_type == DEF_USER || 6627c478bd9Sstevel@tonic-gate aclentp->a_type == DEF_USER_OBJ) { 6637c478bd9Sstevel@tonic-gate if ((pwp = getpwnam(fieldp)) != NULL) 6647c478bd9Sstevel@tonic-gate aclentp->a_id = pwp->pw_uid; 6657c478bd9Sstevel@tonic-gate else { 6667c478bd9Sstevel@tonic-gate /* treat it as numeric id */ 6677c478bd9Sstevel@tonic-gate id = conv_id(fieldp); 6687c478bd9Sstevel@tonic-gate if (id == -1) 6697c478bd9Sstevel@tonic-gate return (-1); 6707c478bd9Sstevel@tonic-gate aclentp->a_id = id; 6717c478bd9Sstevel@tonic-gate } 6727c478bd9Sstevel@tonic-gate } else { 6737c478bd9Sstevel@tonic-gate /* group name */ 6747c478bd9Sstevel@tonic-gate if ((grp = getgrnam(fieldp)) != NULL) 6757c478bd9Sstevel@tonic-gate aclentp->a_id = grp->gr_gid; 6767c478bd9Sstevel@tonic-gate else { 6777c478bd9Sstevel@tonic-gate id = conv_id(fieldp); 6787c478bd9Sstevel@tonic-gate if (id == -1) 6797c478bd9Sstevel@tonic-gate return (-1); 6807c478bd9Sstevel@tonic-gate aclentp->a_id = id; 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate } else { 6857c478bd9Sstevel@tonic-gate /* it is mask/other entry */ 6867c478bd9Sstevel@tonic-gate mo_flag = 1; 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate /* process permission: rwx and [0]n format */ 6907c478bd9Sstevel@tonic-gate if (mode == DELETE) 6917c478bd9Sstevel@tonic-gate /* delete format: no permission field */ 6927c478bd9Sstevel@tonic-gate return (0); 6937c478bd9Sstevel@tonic-gate fieldp = ++colonp; 6947c478bd9Sstevel@tonic-gate colonp = (char *)strchr(fieldp, ':'); 6957c478bd9Sstevel@tonic-gate if (colonp != NULL) { 6967c478bd9Sstevel@tonic-gate if (mo_flag == 1) { 6977c478bd9Sstevel@tonic-gate /* Use only single : on mask/other entry */ 6987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("use only 1 colon for " 6997c478bd9Sstevel@tonic-gate "mask and other entries.\n")); 7007c478bd9Sstevel@tonic-gate return (-1); 7017c478bd9Sstevel@tonic-gate } else { 7027c478bd9Sstevel@tonic-gate /* it's ok to have extra colon */ 7037c478bd9Sstevel@tonic-gate *colonp = '\0'; 7047c478bd9Sstevel@tonic-gate } 7057c478bd9Sstevel@tonic-gate } 7067c478bd9Sstevel@tonic-gate 7077c478bd9Sstevel@tonic-gate if ((int)strlen(fieldp) > 3) { 7087c478bd9Sstevel@tonic-gate fprintf(stderr, 7097c478bd9Sstevel@tonic-gate gettext("only rwx or [0]n format is allowed\n")); 7107c478bd9Sstevel@tonic-gate return (-1); 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate if (strlen(fieldp) == 3) { 7137c478bd9Sstevel@tonic-gate aclentp->a_perm = 0; 7147c478bd9Sstevel@tonic-gate /* treat it as rwx */ 7157c478bd9Sstevel@tonic-gate if (*fieldp == 'r') 7167c478bd9Sstevel@tonic-gate aclentp->a_perm += 4; 7177c478bd9Sstevel@tonic-gate else 7187c478bd9Sstevel@tonic-gate if (*fieldp != '-') { 7197c478bd9Sstevel@tonic-gate fprintf(stderr, 7207c478bd9Sstevel@tonic-gate gettext("Unrecognized character ")); 7217c478bd9Sstevel@tonic-gate fprintf(stderr, 7227c478bd9Sstevel@tonic-gate gettext("found in mode field\n")); 7237c478bd9Sstevel@tonic-gate return (-1); 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate fieldp++; 7267c478bd9Sstevel@tonic-gate if (*fieldp == 'w') 7277c478bd9Sstevel@tonic-gate aclentp->a_perm += 2; 7287c478bd9Sstevel@tonic-gate else 7297c478bd9Sstevel@tonic-gate if (*fieldp != '-') { 7307c478bd9Sstevel@tonic-gate fprintf(stderr, 7317c478bd9Sstevel@tonic-gate gettext("Unrecognized character ")); 7327c478bd9Sstevel@tonic-gate fprintf(stderr, 7337c478bd9Sstevel@tonic-gate gettext("found in mode field\n")); 7347c478bd9Sstevel@tonic-gate return (-1); 7357c478bd9Sstevel@tonic-gate } 7367c478bd9Sstevel@tonic-gate fieldp++; 7377c478bd9Sstevel@tonic-gate if (*fieldp == 'x') 7387c478bd9Sstevel@tonic-gate aclentp->a_perm += 1; 7397c478bd9Sstevel@tonic-gate else 7407c478bd9Sstevel@tonic-gate if (*fieldp != '-') { 7417c478bd9Sstevel@tonic-gate fprintf(stderr, 7427c478bd9Sstevel@tonic-gate gettext("Unrecognized character ")); 7437c478bd9Sstevel@tonic-gate fprintf(stderr, 7447c478bd9Sstevel@tonic-gate gettext("found in mode field\n")); 7457c478bd9Sstevel@tonic-gate return (-1); 7467c478bd9Sstevel@tonic-gate } 7477c478bd9Sstevel@tonic-gate return (0); 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate if (*fieldp == '\0') 7517c478bd9Sstevel@tonic-gate return (0); 7527c478bd9Sstevel@tonic-gate 7537c478bd9Sstevel@tonic-gate if (*fieldp >= '0' && *fieldp <= '7') 7547c478bd9Sstevel@tonic-gate aclentp->a_perm = *fieldp - '0'; 7557c478bd9Sstevel@tonic-gate else { 7567c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Unrecognized character ")); 7577c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("found in mode field\n")); 7587c478bd9Sstevel@tonic-gate return (-1); 7597c478bd9Sstevel@tonic-gate } 7607c478bd9Sstevel@tonic-gate if (aclentp->a_perm == 0 && *++fieldp != '\0') { 7617c478bd9Sstevel@tonic-gate /* look at next char */ 7627c478bd9Sstevel@tonic-gate if (*fieldp >= '0' && *fieldp <= '7') 7637c478bd9Sstevel@tonic-gate aclentp->a_perm = *fieldp - '0'; 7647c478bd9Sstevel@tonic-gate else { 7657c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Unrecognized character ")); 7667c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("found in mode field\n")); 7677c478bd9Sstevel@tonic-gate fprintf(stderr, 7687c478bd9Sstevel@tonic-gate gettext("Check also the number of fields ")); 7697c478bd9Sstevel@tonic-gate fprintf(stderr, 7707c478bd9Sstevel@tonic-gate gettext("(default) mask and other entries\n")); 7717c478bd9Sstevel@tonic-gate return (-1); 7727c478bd9Sstevel@tonic-gate } 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate /* check for junk at the end ??? */ 7757c478bd9Sstevel@tonic-gate return (0); 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate /* 7797c478bd9Sstevel@tonic-gate * This function is different from atoi() in that it checks for 7807c478bd9Sstevel@tonic-gate * valid digit in the id field whereas atoi() won't report any 7817c478bd9Sstevel@tonic-gate * error. 7827c478bd9Sstevel@tonic-gate */ 7837c478bd9Sstevel@tonic-gate static int 7847c478bd9Sstevel@tonic-gate conv_id(char *fieldp) 7857c478bd9Sstevel@tonic-gate { 7867c478bd9Sstevel@tonic-gate int a_id = 0; 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate for (; *fieldp != '\0'; fieldp++) { 7897c478bd9Sstevel@tonic-gate if (!isdigit(*fieldp)) { 7907c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("non-digit in id field\n")); 7917c478bd9Sstevel@tonic-gate return (-1); 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate a_id = a_id * 10 + (*fieldp - '0'); 7947c478bd9Sstevel@tonic-gate } 7957c478bd9Sstevel@tonic-gate return (a_id); 7967c478bd9Sstevel@tonic-gate } 797