1515d7c92SRobert Watson /*- 26394f703SRobert Watson * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3515d7c92SRobert Watson * All rights reserved. 4515d7c92SRobert Watson * 56394f703SRobert Watson * This software was developed by Robert Watson for the TrustedBSD Project. 66394f703SRobert Watson * 7515d7c92SRobert Watson * Redistribution and use in source and binary forms, with or without 8515d7c92SRobert Watson * modification, are permitted provided that the following conditions 9515d7c92SRobert Watson * are met: 10515d7c92SRobert Watson * 1. Redistributions of source code must retain the above copyright 11515d7c92SRobert Watson * notice, this list of conditions and the following disclaimer. 12515d7c92SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 13515d7c92SRobert Watson * notice, this list of conditions and the following disclaimer in the 14515d7c92SRobert Watson * documentation and/or other materials provided with the distribution. 15515d7c92SRobert Watson * 16515d7c92SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17515d7c92SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18515d7c92SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19515d7c92SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20515d7c92SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21515d7c92SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22515d7c92SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23515d7c92SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24515d7c92SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25515d7c92SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26515d7c92SRobert Watson * SUCH DAMAGE. 27515d7c92SRobert Watson */ 28515d7c92SRobert Watson /* 29515d7c92SRobert Watson * acl_set_file -- set a file/directory ACL by name 30515d7c92SRobert Watson */ 31515d7c92SRobert Watson 32333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 33333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34333fc21eSDavid E. O'Brien 35515d7c92SRobert Watson #include <sys/types.h> 367bd44e92SThomas Moestl #include "namespace.h" 37515d7c92SRobert Watson #include <sys/acl.h> 387bd44e92SThomas Moestl #include "un-namespace.h" 394bf60dfaSChris D. Faulhaber 40515d7c92SRobert Watson #include <errno.h> 414bf60dfaSChris D. Faulhaber #include <stdlib.h> 424bf60dfaSChris D. Faulhaber #include <string.h> 43aa015c8eSEdward Tomasz Napierala #include <unistd.h> 44515d7c92SRobert Watson 45515d7c92SRobert Watson #include "acl_support.h" 46515d7c92SRobert Watson 47515d7c92SRobert Watson /* 48515d7c92SRobert Watson * For POSIX.1e-semantic ACLs, do a presort so the kernel doesn't have to 49515d7c92SRobert Watson * (the POSIX.1e semantic code will reject unsorted ACL submission). If it's 50515d7c92SRobert Watson * not a semantic that the library knows about, just submit it flat and 51515d7c92SRobert Watson * assume the caller knows what they're up to. 52515d7c92SRobert Watson */ 53515d7c92SRobert Watson int 54515d7c92SRobert Watson acl_set_file(const char *path_p, acl_type_t type, acl_t acl) 55515d7c92SRobert Watson { 56515d7c92SRobert Watson 57e146d0bcSChris D. Faulhaber if (acl == NULL || path_p == NULL) { 58e146d0bcSChris D. Faulhaber errno = EINVAL; 59e146d0bcSChris D. Faulhaber return (-1); 60e146d0bcSChris D. Faulhaber } 61ae1add4eSEdward Tomasz Napierala type = _acl_type_unold(type); 62aa015c8eSEdward Tomasz Napierala if (_acl_type_not_valid_for_acl(acl, type)) { 63aa015c8eSEdward Tomasz Napierala errno = EINVAL; 64aa015c8eSEdward Tomasz Napierala return (-1); 65aa015c8eSEdward Tomasz Napierala } 66d72fb30aSEdward Tomasz Napierala if (_posix1e_acl(acl, type)) 67d72fb30aSEdward Tomasz Napierala _posix1e_acl_sort(acl); 68515d7c92SRobert Watson 690f626307SChris D. Faulhaber acl->ats_cur_entry = 0; 700f626307SChris D. Faulhaber 710f626307SChris D. Faulhaber return (__acl_set_file(path_p, type, &acl->ats_acl)); 72515d7c92SRobert Watson } 73515d7c92SRobert Watson 74515d7c92SRobert Watson int 756394f703SRobert Watson acl_set_link_np(const char *path_p, acl_type_t type, acl_t acl) 766394f703SRobert Watson { 776394f703SRobert Watson 786394f703SRobert Watson if (acl == NULL || path_p == NULL) { 796394f703SRobert Watson errno = EINVAL; 806394f703SRobert Watson return (-1); 816394f703SRobert Watson } 82ae1add4eSEdward Tomasz Napierala type = _acl_type_unold(type); 83aa015c8eSEdward Tomasz Napierala if (_acl_type_not_valid_for_acl(acl, type)) { 84aa015c8eSEdward Tomasz Napierala errno = EINVAL; 85aa015c8eSEdward Tomasz Napierala return (-1); 86aa015c8eSEdward Tomasz Napierala } 87d72fb30aSEdward Tomasz Napierala if (_posix1e_acl(acl, type)) 88d72fb30aSEdward Tomasz Napierala _posix1e_acl_sort(acl); 896394f703SRobert Watson 906394f703SRobert Watson acl->ats_cur_entry = 0; 916394f703SRobert Watson 926394f703SRobert Watson return (__acl_set_link(path_p, type, &acl->ats_acl)); 936394f703SRobert Watson } 946394f703SRobert Watson 956394f703SRobert Watson int 968f45e8c0SRobert Watson acl_set_fd(int fd, acl_t acl) 97515d7c92SRobert Watson { 98515d7c92SRobert Watson 99c3380d40SEdward Tomasz Napierala if (fpathconf(fd, _PC_ACL_NFS4) == 1) 100aa015c8eSEdward Tomasz Napierala return (acl_set_fd_np(fd, acl, ACL_TYPE_NFS4)); 1018f45e8c0SRobert Watson 102aa015c8eSEdward Tomasz Napierala return (acl_set_fd_np(fd, acl, ACL_TYPE_ACCESS)); 1038f45e8c0SRobert Watson } 1048f45e8c0SRobert Watson 1058f45e8c0SRobert Watson int 1068f45e8c0SRobert Watson acl_set_fd_np(int fd, acl_t acl, acl_type_t type) 1078f45e8c0SRobert Watson { 1088f45e8c0SRobert Watson 109aa015c8eSEdward Tomasz Napierala if (acl == NULL) { 110aa015c8eSEdward Tomasz Napierala errno = EINVAL; 111aa015c8eSEdward Tomasz Napierala return (-1); 112aa015c8eSEdward Tomasz Napierala } 113ae1add4eSEdward Tomasz Napierala type = _acl_type_unold(type); 114aa015c8eSEdward Tomasz Napierala if (_acl_type_not_valid_for_acl(acl, type)) { 115aa015c8eSEdward Tomasz Napierala errno = EINVAL; 116aa015c8eSEdward Tomasz Napierala return (-1); 117aa015c8eSEdward Tomasz Napierala } 118d72fb30aSEdward Tomasz Napierala if (_posix1e_acl(acl, type)) 119d72fb30aSEdward Tomasz Napierala _posix1e_acl_sort(acl); 120515d7c92SRobert Watson 1210f626307SChris D. Faulhaber acl->ats_cur_entry = 0; 1220f626307SChris D. Faulhaber 1230f626307SChris D. Faulhaber return (___acl_set_fd(fd, type, &acl->ats_acl)); 124515d7c92SRobert Watson } 1254bf60dfaSChris D. Faulhaber 1264bf60dfaSChris D. Faulhaber /* 1270f626307SChris D. Faulhaber * acl_set_permset() (23.4.23): sets the permissions of ACL entry entry_d 1284bf60dfaSChris D. Faulhaber * with the permissions in permset_d 1294bf60dfaSChris D. Faulhaber */ 1304bf60dfaSChris D. Faulhaber int 1314bf60dfaSChris D. Faulhaber acl_set_permset(acl_entry_t entry_d, acl_permset_t permset_d) 1324bf60dfaSChris D. Faulhaber { 1334bf60dfaSChris D. Faulhaber 1344bf60dfaSChris D. Faulhaber if (!entry_d) { 1354bf60dfaSChris D. Faulhaber errno = EINVAL; 1369fd46b02SChris D. Faulhaber return (-1); 1374bf60dfaSChris D. Faulhaber } 1384bf60dfaSChris D. Faulhaber 139aa015c8eSEdward Tomasz Napierala if ((*permset_d & ACL_POSIX1E_BITS) != *permset_d) { 140aa015c8eSEdward Tomasz Napierala if ((*permset_d & ACL_NFS4_PERM_BITS) != *permset_d) { 141aa015c8eSEdward Tomasz Napierala errno = EINVAL; 142aa015c8eSEdward Tomasz Napierala return (-1); 143aa015c8eSEdward Tomasz Napierala } 144aa015c8eSEdward Tomasz Napierala if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 145aa015c8eSEdward Tomasz Napierala errno = EINVAL; 146aa015c8eSEdward Tomasz Napierala return (-1); 147aa015c8eSEdward Tomasz Napierala } 148aa015c8eSEdward Tomasz Napierala _entry_brand_as(entry_d, ACL_BRAND_NFS4); 149aa015c8eSEdward Tomasz Napierala } 150aa015c8eSEdward Tomasz Napierala 1514bf60dfaSChris D. Faulhaber entry_d->ae_perm = *permset_d; 1524bf60dfaSChris D. Faulhaber 1539fd46b02SChris D. Faulhaber return (0); 1544bf60dfaSChris D. Faulhaber } 1554bf60dfaSChris D. Faulhaber 1564bf60dfaSChris D. Faulhaber /* 1574bf60dfaSChris D. Faulhaber * acl_set_qualifier() sets the qualifier (ae_id) of the tag for 1584bf60dfaSChris D. Faulhaber * ACL entry entry_d to the value referred to by tag_qualifier_p 1594bf60dfaSChris D. Faulhaber */ 1604bf60dfaSChris D. Faulhaber int 1614bf60dfaSChris D. Faulhaber acl_set_qualifier(acl_entry_t entry_d, const void *tag_qualifier_p) 1624bf60dfaSChris D. Faulhaber { 163aa015c8eSEdward Tomasz Napierala 1644bf60dfaSChris D. Faulhaber if (!entry_d || !tag_qualifier_p) { 1654bf60dfaSChris D. Faulhaber errno = EINVAL; 1669fd46b02SChris D. Faulhaber return (-1); 1674bf60dfaSChris D. Faulhaber } 1684bf60dfaSChris D. Faulhaber switch(entry_d->ae_tag) { 1694bf60dfaSChris D. Faulhaber case ACL_USER: 1704bf60dfaSChris D. Faulhaber case ACL_GROUP: 171d5675fffSChris D. Faulhaber entry_d->ae_id = *(uid_t *)tag_qualifier_p; 1724bf60dfaSChris D. Faulhaber break; 1734bf60dfaSChris D. Faulhaber default: 1744bf60dfaSChris D. Faulhaber errno = EINVAL; 1759fd46b02SChris D. Faulhaber return (-1); 1764bf60dfaSChris D. Faulhaber } 1774bf60dfaSChris D. Faulhaber 1789fd46b02SChris D. Faulhaber return (0); 1794bf60dfaSChris D. Faulhaber } 1804bf60dfaSChris D. Faulhaber 1814bf60dfaSChris D. Faulhaber /* 1824bf60dfaSChris D. Faulhaber * acl_set_tag_type() sets the tag type for ACL entry entry_d to the 1834bf60dfaSChris D. Faulhaber * value of tag_type 1844bf60dfaSChris D. Faulhaber */ 1854bf60dfaSChris D. Faulhaber int 1864bf60dfaSChris D. Faulhaber acl_set_tag_type(acl_entry_t entry_d, acl_tag_t tag_type) 1874bf60dfaSChris D. Faulhaber { 1884bf60dfaSChris D. Faulhaber 1899fd46b02SChris D. Faulhaber if (entry_d == NULL) { 1904bf60dfaSChris D. Faulhaber errno = EINVAL; 1919fd46b02SChris D. Faulhaber return (-1); 1924bf60dfaSChris D. Faulhaber } 1934bf60dfaSChris D. Faulhaber 1944bf60dfaSChris D. Faulhaber switch(tag_type) { 195aa015c8eSEdward Tomasz Napierala case ACL_OTHER: 196aa015c8eSEdward Tomasz Napierala case ACL_MASK: 197aa015c8eSEdward Tomasz Napierala if (!_entry_brand_may_be(entry_d, ACL_BRAND_POSIX)) { 198aa015c8eSEdward Tomasz Napierala errno = EINVAL; 199aa015c8eSEdward Tomasz Napierala return (-1); 200aa015c8eSEdward Tomasz Napierala } 201aa015c8eSEdward Tomasz Napierala _entry_brand_as(entry_d, ACL_BRAND_POSIX); 202aa015c8eSEdward Tomasz Napierala break; 203aa015c8eSEdward Tomasz Napierala case ACL_EVERYONE: 204aa015c8eSEdward Tomasz Napierala if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 205aa015c8eSEdward Tomasz Napierala errno = EINVAL; 206aa015c8eSEdward Tomasz Napierala return (-1); 207aa015c8eSEdward Tomasz Napierala } 208aa015c8eSEdward Tomasz Napierala _entry_brand_as(entry_d, ACL_BRAND_NFS4); 209aa015c8eSEdward Tomasz Napierala break; 210aa015c8eSEdward Tomasz Napierala } 211aa015c8eSEdward Tomasz Napierala 212aa015c8eSEdward Tomasz Napierala switch(tag_type) { 2134bf60dfaSChris D. Faulhaber case ACL_USER_OBJ: 2144bf60dfaSChris D. Faulhaber case ACL_USER: 2154bf60dfaSChris D. Faulhaber case ACL_GROUP_OBJ: 2164bf60dfaSChris D. Faulhaber case ACL_GROUP: 2174bf60dfaSChris D. Faulhaber case ACL_MASK: 2184bf60dfaSChris D. Faulhaber case ACL_OTHER: 219aa015c8eSEdward Tomasz Napierala case ACL_EVERYONE: 2204bf60dfaSChris D. Faulhaber entry_d->ae_tag = tag_type; 2219fd46b02SChris D. Faulhaber return (0); 2224bf60dfaSChris D. Faulhaber } 2234bf60dfaSChris D. Faulhaber 2244bf60dfaSChris D. Faulhaber errno = EINVAL; 2259fd46b02SChris D. Faulhaber return (-1); 2264bf60dfaSChris D. Faulhaber } 227aa015c8eSEdward Tomasz Napierala 228aa015c8eSEdward Tomasz Napierala int 229aa015c8eSEdward Tomasz Napierala acl_set_entry_type_np(acl_entry_t entry_d, acl_entry_type_t entry_type) 230aa015c8eSEdward Tomasz Napierala { 231aa015c8eSEdward Tomasz Napierala 232aa015c8eSEdward Tomasz Napierala if (entry_d == NULL) { 233aa015c8eSEdward Tomasz Napierala errno = EINVAL; 234aa015c8eSEdward Tomasz Napierala return (-1); 235aa015c8eSEdward Tomasz Napierala } 236aa015c8eSEdward Tomasz Napierala if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 237aa015c8eSEdward Tomasz Napierala errno = EINVAL; 238aa015c8eSEdward Tomasz Napierala return (-1); 239aa015c8eSEdward Tomasz Napierala } 240aa015c8eSEdward Tomasz Napierala _entry_brand_as(entry_d, ACL_BRAND_NFS4); 241aa015c8eSEdward Tomasz Napierala 242aa015c8eSEdward Tomasz Napierala switch (entry_type) { 243aa015c8eSEdward Tomasz Napierala case ACL_ENTRY_TYPE_ALLOW: 244aa015c8eSEdward Tomasz Napierala case ACL_ENTRY_TYPE_DENY: 245aa015c8eSEdward Tomasz Napierala case ACL_ENTRY_TYPE_AUDIT: 246aa015c8eSEdward Tomasz Napierala case ACL_ENTRY_TYPE_ALARM: 247aa015c8eSEdward Tomasz Napierala entry_d->ae_entry_type = entry_type; 248aa015c8eSEdward Tomasz Napierala return (0); 249aa015c8eSEdward Tomasz Napierala } 250aa015c8eSEdward Tomasz Napierala 251aa015c8eSEdward Tomasz Napierala errno = EINVAL; 252aa015c8eSEdward Tomasz Napierala return (-1); 253aa015c8eSEdward Tomasz Napierala } 254