acl_strip.c revision 194955
1/*- 2 * Copyright (c) 2001 Chris D. Faulhaber 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE VOICES IN HIS HEAD BE 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_strip.c 194955 2009-06-25 12:46:59Z trasz $"); 29 30#include <errno.h> 31#include <stdio.h> 32#include <assert.h> 33#include <sys/acl.h> 34 35#include "acl_support.h" 36 37/* 38 * These two routines from sys/kern/subr_acl_nfs4.c are used by both kernel 39 * and libc. 40 */ 41void acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode, 42 int file_owner_id); 43void acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp); 44 45static acl_t 46_nfs4_acl_strip_np(const acl_t aclp, int recalculate_mask) 47{ 48 acl_t newacl; 49 mode_t mode; 50 51 newacl = acl_init(ACL_MAX_ENTRIES); 52 if (newacl == NULL) { 53 errno = ENOMEM; 54 return (NULL); 55 } 56 57 _acl_brand_as(newacl, ACL_BRAND_NFS4); 58 59 acl_nfs4_sync_mode_from_acl(&mode, &(aclp->ats_acl)); 60 acl_nfs4_sync_acl_from_mode(&(newacl->ats_acl), mode, -1); 61 62 return (newacl); 63} 64 65static acl_t 66_posix1e_acl_strip_np(const acl_t aclp, int recalculate_mask) 67{ 68 acl_t acl_new, acl_old; 69 acl_entry_t entry, entry_new; 70 acl_permset_t perm; 71 acl_tag_t tag; 72 int entry_id, have_mask_entry; 73 74 assert(_acl_brand(aclp) == ACL_BRAND_POSIX); 75 76 acl_old = acl_dup(aclp); 77 if (acl_old == NULL) 78 return (NULL); 79 80 assert(_acl_brand(acl_old) == ACL_BRAND_POSIX); 81 82 have_mask_entry = 0; 83 acl_new = acl_init(ACL_MAX_ENTRIES); 84 if (acl_new == NULL) 85 return (NULL); 86 tag = ACL_UNDEFINED_TAG; 87 88 /* only save the default user/group/other entries */ 89 entry_id = ACL_FIRST_ENTRY; 90 while (acl_get_entry(acl_old, entry_id, &entry) == 1) { 91 entry_id = ACL_NEXT_ENTRY; 92 93 assert(_entry_brand(entry) == ACL_BRAND_POSIX); 94 95 if (acl_get_tag_type(entry, &tag) == -1) 96 return (NULL); 97 98 switch(tag) { 99 case ACL_USER_OBJ: 100 case ACL_GROUP_OBJ: 101 case ACL_OTHER: 102 if (acl_get_tag_type(entry, &tag) == -1) 103 return (NULL); 104 if (acl_get_permset(entry, &perm) == -1) 105 return (NULL); 106 if (acl_create_entry(&acl_new, &entry_new) == -1) 107 return (NULL); 108 if (acl_set_tag_type(entry_new, tag) == -1) 109 return (NULL); 110 if (acl_set_permset(entry_new, perm) == -1) 111 return (NULL); 112 if (acl_copy_entry(entry_new, entry) == -1) 113 return (NULL); 114 assert(_entry_brand(entry_new) == ACL_BRAND_POSIX); 115 break; 116 case ACL_MASK: 117 have_mask_entry = 1; 118 break; 119 default: 120 break; 121 } 122 } 123 124 assert(_acl_brand(acl_new) == ACL_BRAND_POSIX); 125 126 if (have_mask_entry && recalculate_mask) { 127 if (acl_calc_mask(&acl_new) == -1) 128 return (NULL); 129 } 130 131 return (acl_new); 132} 133 134acl_t 135acl_strip_np(const acl_t aclp, int recalculate_mask) 136{ 137 switch (_acl_brand(aclp)) { 138 case ACL_BRAND_NFS4: 139 return (_nfs4_acl_strip_np(aclp, recalculate_mask)); 140 141 case ACL_BRAND_POSIX: 142 return (_posix1e_acl_strip_np(aclp, recalculate_mask)); 143 144 default: 145 errno = EINVAL; 146 return (NULL); 147 } 148} 149 150/* 151 * Return 1, if ACL is trivial, 0 otherwise. 152 * 153 * ACL is trivial, iff its meaning could be fully expressed using just file 154 * mode. In other words, ACL is trivial iff it doesn't have "+" to the right 155 * of the mode bits in "ls -l" output ;-) 156 */ 157int 158acl_is_trivial_np(const acl_t aclp, int *trivialp) 159{ 160 acl_t tmpacl; 161 int differs; 162 163 if (aclp == NULL || trivialp == NULL) { 164 errno = EINVAL; 165 return (-1); 166 } 167 168 switch (_acl_brand(aclp)) { 169 case ACL_BRAND_POSIX: 170 if (aclp->ats_acl.acl_cnt == 3) 171 *trivialp = 1; 172 else 173 *trivialp = 0; 174 175 return (0); 176 177 case ACL_BRAND_NFS4: 178 /* 179 * Calculate trivial ACL - using acl_strip_np - and compare 180 * with the original. 181 */ 182 tmpacl = acl_strip_np(aclp, 0); 183 if (tmpacl == NULL) 184 return (-1); 185 186 differs = _acl_differs(aclp, tmpacl); 187 acl_free(tmpacl); 188 189 if (differs) 190 *trivialp = 0; 191 else 192 *trivialp = 1; 193 194 return (0); 195 196 default: 197 errno = EINVAL; 198 return (-1); 199 } 200} 201/*- 202 * Copyright (c) 2001 Chris D. Faulhaber 203 * All rights reserved. 204 * 205 * Redistribution and use in source and binary forms, with or without 206 * modification, are permitted provided that the following conditions 207 * are met: 208 * 1. Redistributions of source code must retain the above copyright 209 * notice, this list of conditions and the following disclaimer. 210 * 2. Redistributions in binary form must reproduce the above copyright 211 * notice, this list of conditions and the following disclaimer in the 212 * documentation and/or other materials provided with the distribution. 213 * 214 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 215 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 216 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 217 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE VOICES IN HIS HEAD BE 218 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 219 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 220 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 221 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 222 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 223 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 224 * POSSIBILITY OF SUCH DAMAGE. 225 */ 226 227#include <sys/cdefs.h> 228__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_strip.c 194955 2009-06-25 12:46:59Z trasz $"); 229 230#include <errno.h> 231#include <stdio.h> 232#include <assert.h> 233#include <sys/acl.h> 234 235#include "acl_support.h" 236 237/* 238 * These two routines from sys/kern/subr_acl_nfs4.c are used by both kernel 239 * and libc. 240 */ 241void acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode, 242 int file_owner_id); 243void acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp); 244 245static acl_t 246_nfs4_acl_strip_np(const acl_t aclp, int recalculate_mask) 247{ 248 acl_t newacl; 249 mode_t mode; 250 251 newacl = acl_init(ACL_MAX_ENTRIES); 252 if (newacl == NULL) { 253 errno = ENOMEM; 254 return (NULL); 255 } 256 257 _acl_brand_as(newacl, ACL_BRAND_NFS4); 258 259 acl_nfs4_sync_mode_from_acl(&mode, &(aclp->ats_acl)); 260 acl_nfs4_sync_acl_from_mode(&(newacl->ats_acl), mode, -1); 261 262 return (newacl); 263} 264 265static acl_t 266_posix1e_acl_strip_np(const acl_t aclp, int recalculate_mask) 267{ 268 acl_t acl_new, acl_old; 269 acl_entry_t entry, entry_new; 270 acl_permset_t perm; 271 acl_tag_t tag; 272 int entry_id, have_mask_entry; 273 274 assert(_acl_brand(aclp) == ACL_BRAND_POSIX); 275 276 acl_old = acl_dup(aclp); 277 if (acl_old == NULL) 278 return (NULL); 279 280 assert(_acl_brand(acl_old) == ACL_BRAND_POSIX); 281 282 have_mask_entry = 0; 283 acl_new = acl_init(ACL_MAX_ENTRIES); 284 if (acl_new == NULL) 285 return (NULL); 286 tag = ACL_UNDEFINED_TAG; 287 288 /* only save the default user/group/other entries */ 289 entry_id = ACL_FIRST_ENTRY; 290 while (acl_get_entry(acl_old, entry_id, &entry) == 1) { 291 entry_id = ACL_NEXT_ENTRY; 292 293 assert(_entry_brand(entry) == ACL_BRAND_POSIX); 294 295 if (acl_get_tag_type(entry, &tag) == -1) 296 return (NULL); 297 298 switch(tag) { 299 case ACL_USER_OBJ: 300 case ACL_GROUP_OBJ: 301 case ACL_OTHER: 302 if (acl_get_tag_type(entry, &tag) == -1) 303 return (NULL); 304 if (acl_get_permset(entry, &perm) == -1) 305 return (NULL); 306 if (acl_create_entry(&acl_new, &entry_new) == -1) 307 return (NULL); 308 if (acl_set_tag_type(entry_new, tag) == -1) 309 return (NULL); 310 if (acl_set_permset(entry_new, perm) == -1) 311 return (NULL); 312 if (acl_copy_entry(entry_new, entry) == -1) 313 return (NULL); 314 assert(_entry_brand(entry_new) == ACL_BRAND_POSIX); 315 break; 316 case ACL_MASK: 317 have_mask_entry = 1; 318 break; 319 default: 320 break; 321 } 322 } 323 324 assert(_acl_brand(acl_new) == ACL_BRAND_POSIX); 325 326 if (have_mask_entry && recalculate_mask) { 327 if (acl_calc_mask(&acl_new) == -1) 328 return (NULL); 329 } 330 331 return (acl_new); 332} 333 334acl_t 335acl_strip_np(const acl_t aclp, int recalculate_mask) 336{ 337 switch (_acl_brand(aclp)) { 338 case ACL_BRAND_NFS4: 339 return (_nfs4_acl_strip_np(aclp, recalculate_mask)); 340 341 case ACL_BRAND_POSIX: 342 return (_posix1e_acl_strip_np(aclp, recalculate_mask)); 343 344 default: 345 errno = EINVAL; 346 return (NULL); 347 } 348} 349 350/* 351 * Return 1, if ACL is trivial, 0 otherwise. 352 * 353 * ACL is trivial, iff its meaning could be fully expressed using just file 354 * mode. In other words, ACL is trivial iff it doesn't have "+" to the right 355 * of the mode bits in "ls -l" output ;-) 356 */ 357int 358acl_is_trivial_np(const acl_t aclp, int *trivialp) 359{ 360 acl_t tmpacl; 361 int differs; 362 363 if (aclp == NULL || trivialp == NULL) { 364 errno = EINVAL; 365 return (-1); 366 } 367 368 switch (_acl_brand(aclp)) { 369 case ACL_BRAND_POSIX: 370 if (aclp->ats_acl.acl_cnt == 3) 371 *trivialp = 1; 372 else 373 *trivialp = 0; 374 375 return (0); 376 377 case ACL_BRAND_NFS4: 378 /* 379 * Calculate trivial ACL - using acl_strip_np - and compare 380 * with the original. 381 */ 382 tmpacl = acl_strip_np(aclp, 0); 383 if (tmpacl == NULL) 384 return (-1); 385 386 differs = _acl_differs(aclp, tmpacl); 387 acl_free(tmpacl); 388 389 if (differs) 390 *trivialp = 0; 391 else 392 *trivialp = 1; 393 394 return (0); 395 396 default: 397 errno = EINVAL; 398 return (-1); 399 } 400} 401/*- 402 * Copyright (c) 2001 Chris D. Faulhaber 403 * All rights reserved. 404 * 405 * Redistribution and use in source and binary forms, with or without 406 * modification, are permitted provided that the following conditions 407 * are met: 408 * 1. Redistributions of source code must retain the above copyright 409 * notice, this list of conditions and the following disclaimer. 410 * 2. Redistributions in binary form must reproduce the above copyright 411 * notice, this list of conditions and the following disclaimer in the 412 * documentation and/or other materials provided with the distribution. 413 * 414 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 415 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 416 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 417 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE VOICES IN HIS HEAD BE 418 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 419 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 420 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 421 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 422 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 423 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 424 * POSSIBILITY OF SUCH DAMAGE. 425 */ 426 427#include <sys/cdefs.h> 428__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_strip.c 194955 2009-06-25 12:46:59Z trasz $"); 429 430#include <errno.h> 431#include <stdio.h> 432#include <assert.h> 433#include <sys/acl.h> 434 435#include "acl_support.h" 436 437/* 438 * These two routines from sys/kern/subr_acl_nfs4.c are used by both kernel 439 * and libc. 440 */ 441void acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode, 442 int file_owner_id); 443void acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp); 444 445static acl_t 446_nfs4_acl_strip_np(const acl_t aclp, int recalculate_mask) 447{ 448 acl_t newacl; 449 mode_t mode; 450 451 newacl = acl_init(ACL_MAX_ENTRIES); 452 if (newacl == NULL) { 453 errno = ENOMEM; 454 return (NULL); 455 } 456 457 _acl_brand_as(newacl, ACL_BRAND_NFS4); 458 459 acl_nfs4_sync_mode_from_acl(&mode, &(aclp->ats_acl)); 460 acl_nfs4_sync_acl_from_mode(&(newacl->ats_acl), mode, -1); 461 462 return (newacl); 463} 464 465static acl_t 466_posix1e_acl_strip_np(const acl_t aclp, int recalculate_mask) 467{ 468 acl_t acl_new, acl_old; 469 acl_entry_t entry, entry_new; 470 acl_permset_t perm; 471 acl_tag_t tag; 472 int entry_id, have_mask_entry; 473 474 assert(_acl_brand(aclp) == ACL_BRAND_POSIX); 475 476 acl_old = acl_dup(aclp); 477 if (acl_old == NULL) 478 return (NULL); 479 480 assert(_acl_brand(acl_old) == ACL_BRAND_POSIX); 481 482 have_mask_entry = 0; 483 acl_new = acl_init(ACL_MAX_ENTRIES); 484 if (acl_new == NULL) 485 return (NULL); 486 tag = ACL_UNDEFINED_TAG; 487 488 /* only save the default user/group/other entries */ 489 entry_id = ACL_FIRST_ENTRY; 490 while (acl_get_entry(acl_old, entry_id, &entry) == 1) { 491 entry_id = ACL_NEXT_ENTRY; 492 493 assert(_entry_brand(entry) == ACL_BRAND_POSIX); 494 495 if (acl_get_tag_type(entry, &tag) == -1) 496 return (NULL); 497 498 switch(tag) { 499 case ACL_USER_OBJ: 500 case ACL_GROUP_OBJ: 501 case ACL_OTHER: 502 if (acl_get_tag_type(entry, &tag) == -1) 503 return (NULL); 504 if (acl_get_permset(entry, &perm) == -1) 505 return (NULL); 506 if (acl_create_entry(&acl_new, &entry_new) == -1) 507 return (NULL); 508 if (acl_set_tag_type(entry_new, tag) == -1) 509 return (NULL); 510 if (acl_set_permset(entry_new, perm) == -1) 511 return (NULL); 512 if (acl_copy_entry(entry_new, entry) == -1) 513 return (NULL); 514 assert(_entry_brand(entry_new) == ACL_BRAND_POSIX); 515 break; 516 case ACL_MASK: 517 have_mask_entry = 1; 518 break; 519 default: 520 break; 521 } 522 } 523 524 assert(_acl_brand(acl_new) == ACL_BRAND_POSIX); 525 526 if (have_mask_entry && recalculate_mask) { 527 if (acl_calc_mask(&acl_new) == -1) 528 return (NULL); 529 } 530 531 return (acl_new); 532} 533 534acl_t 535acl_strip_np(const acl_t aclp, int recalculate_mask) 536{ 537 switch (_acl_brand(aclp)) { 538 case ACL_BRAND_NFS4: 539 return (_nfs4_acl_strip_np(aclp, recalculate_mask)); 540 541 case ACL_BRAND_POSIX: 542 return (_posix1e_acl_strip_np(aclp, recalculate_mask)); 543 544 default: 545 errno = EINVAL; 546 return (NULL); 547 } 548} 549 550/* 551 * Return 1, if ACL is trivial, 0 otherwise. 552 * 553 * ACL is trivial, iff its meaning could be fully expressed using just file 554 * mode. In other words, ACL is trivial iff it doesn't have "+" to the right 555 * of the mode bits in "ls -l" output ;-) 556 */ 557int 558acl_is_trivial_np(const acl_t aclp, int *trivialp) 559{ 560 acl_t tmpacl; 561 int differs; 562 563 if (aclp == NULL || trivialp == NULL) { 564 errno = EINVAL; 565 return (-1); 566 } 567 568 switch (_acl_brand(aclp)) { 569 case ACL_BRAND_POSIX: 570 if (aclp->ats_acl.acl_cnt == 3) 571 *trivialp = 1; 572 else 573 *trivialp = 0; 574 575 return (0); 576 577 case ACL_BRAND_NFS4: 578 /* 579 * Calculate trivial ACL - using acl_strip_np - and compare 580 * with the original. 581 */ 582 tmpacl = acl_strip_np(aclp, 0); 583 if (tmpacl == NULL) 584 return (-1); 585 586 differs = _acl_differs(aclp, tmpacl); 587 acl_free(tmpacl); 588 589 if (differs) 590 *trivialp = 0; 591 else 592 *trivialp = 1; 593 594 return (0); 595 596 default: 597 errno = EINVAL; 598 return (-1); 599 } 600} 601