1 /*- 2 * Copyright (c) 1999-2001 Robert N M Watson 3 * All rights reserved. 4 * 5 * This software was developed by Robert Watson for the TrustedBSD Project. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 /* 31 * getfacl -- POSIX.1e utility to extract ACLs from files and directories 32 * and send the results to stdout 33 */ 34 35 #include <sys/types.h> 36 #include <sys/param.h> 37 #include <sys/acl.h> 38 #include <sys/stat.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 int more_than_one = 0; 47 48 static void 49 usage(void) 50 { 51 52 fprintf(stderr, "getfacl [-d] [files ...]\n"); 53 } 54 55 /* 56 * return an ACL corresponding to the permissions 57 * contained in struct stat 58 */ 59 static acl_t 60 acl_from_stat(struct stat sb) 61 { 62 acl_t acl; 63 acl_entry_t entry; 64 acl_permset_t perms; 65 66 /* create the ACL */ 67 acl = acl_init(3); 68 if (!acl) 69 return NULL; 70 71 /* First entry: ACL_USER_OBJ */ 72 if (acl_create_entry(&acl, &entry) == -1) 73 return NULL; 74 if (acl_set_tag_type(entry, ACL_USER_OBJ) == -1) 75 return NULL; 76 77 if (acl_get_permset(entry, &perms) == -1) 78 return NULL; 79 if (acl_clear_perms(perms) == -1) 80 return NULL; 81 82 /* calculate user mode */ 83 if (sb.st_mode & S_IRUSR) 84 if (acl_add_perm(perms, ACL_READ) == -1) 85 return NULL; 86 if (sb.st_mode & S_IWUSR) 87 if (acl_add_perm(perms, ACL_WRITE) == -1) 88 return NULL; 89 if (sb.st_mode & S_IXUSR) 90 if (acl_add_perm(perms, ACL_EXECUTE) == -1) 91 return NULL; 92 if (acl_set_permset(entry, perms) == -1) 93 return NULL; 94 95 /* Second entry: ACL_GROUP_OBJ */ 96 if (acl_create_entry(&acl, &entry) == -1) 97 return NULL; 98 if (acl_set_tag_type(entry, ACL_GROUP_OBJ) == -1) 99 return NULL; 100 101 if (acl_get_permset(entry, &perms) == -1) 102 return NULL; 103 if (acl_clear_perms(perms) == -1) 104 return NULL; 105 106 /* calculate group mode */ 107 if (sb.st_mode & S_IRGRP) 108 if (acl_add_perm(perms, ACL_READ) == -1) 109 return NULL; 110 if (sb.st_mode & S_IWGRP) 111 if (acl_add_perm(perms, ACL_WRITE) == -1) 112 return NULL; 113 if (sb.st_mode & S_IXGRP) 114 if (acl_add_perm(perms, ACL_EXECUTE) == -1) 115 return NULL; 116 if (acl_set_permset(entry, perms) == -1) 117 return NULL; 118 119 /* Third entry: ACL_OTHER */ 120 if (acl_create_entry(&acl, &entry) == -1) 121 return NULL; 122 if (acl_set_tag_type(entry, ACL_OTHER) == -1) 123 return NULL; 124 125 if (acl_get_permset(entry, &perms) == -1) 126 return NULL; 127 if (acl_clear_perms(perms) == -1) 128 return NULL; 129 130 /* calculate other mode */ 131 if (sb.st_mode & S_IROTH) 132 if (acl_add_perm(perms, ACL_READ) == -1) 133 return NULL; 134 if (sb.st_mode & S_IWOTH) 135 if (acl_add_perm(perms, ACL_WRITE) == -1) 136 return NULL; 137 if (sb.st_mode & S_IXOTH) 138 if (acl_add_perm(perms, ACL_EXECUTE) == -1) 139 return NULL; 140 if (acl_set_permset(entry, perms) == -1) 141 return NULL; 142 143 return(acl); 144 } 145 146 static int 147 print_acl(char *path, acl_type_t type) 148 { 149 struct stat sb; 150 acl_t acl; 151 char *acl_text; 152 int error; 153 154 error = stat(path, &sb); 155 if (error == -1) { 156 perror(path); 157 return(-1); 158 } 159 160 if (more_than_one) 161 printf("\n"); 162 else 163 more_than_one++; 164 165 printf("#file:%s\n#owner:%d\n#group:%d\n", path, sb.st_uid, sb.st_gid); 166 167 acl = acl_get_file(path, type); 168 if (!acl) { 169 if (errno != EOPNOTSUPP) { 170 warn("%s", path); 171 return(-1); 172 } 173 errno = 0; 174 if (type != ACL_TYPE_ACCESS) 175 return(0); 176 acl = acl_from_stat(sb); 177 if (!acl) { 178 perror("acl_from_stat()"); 179 return(-1); 180 } 181 } 182 183 acl_text = acl_to_text(acl, 0); 184 if (!acl_text) { 185 perror(path); 186 return(-1); 187 } 188 189 printf("%s", acl_text); 190 191 (void)acl_free(acl); 192 (void)acl_free(acl_text); 193 194 return(0); 195 } 196 197 static int 198 print_acl_from_stdin(acl_type_t type) 199 { 200 char pathname[PATH_MAX]; 201 int carried_error = 0; 202 203 pathname[sizeof(pathname) - 1] = '\0'; 204 while (fgets(pathname, (int)sizeof(pathname), stdin)) { 205 /* remove the \n */ 206 pathname[strlen(pathname) - 1] = '\0'; 207 if (print_acl(pathname, type) == -1) { 208 carried_error = -1; 209 } 210 } 211 212 return(carried_error); 213 } 214 215 int 216 main(int argc, char *argv[]) 217 { 218 acl_type_t type = ACL_TYPE_ACCESS; 219 int carried_error = 0; 220 int ch, error, i; 221 222 while ((ch = getopt(argc, argv, "d")) != -1) 223 switch(ch) { 224 case 'd': 225 type = ACL_TYPE_DEFAULT; 226 break; 227 default: 228 usage(); 229 return(-1); 230 } 231 argc -= optind; 232 argv += optind; 233 234 if (argc == 0) { 235 error = print_acl_from_stdin(type); 236 return(error); 237 } 238 239 for (i = 0; i < argc; i++) { 240 if (!strcmp(argv[i], "-")) { 241 error = print_acl_from_stdin(type); 242 if (error == -1) 243 carried_error = -1; 244 } else { 245 error = print_acl(argv[i], type); 246 if (error == -1) 247 carried_error = -1; 248 } 249 } 250 251 return(carried_error); 252 } 253