156055Srwatson/*- 2108410Srwatson * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 356055Srwatson * All rights reserved. 456055Srwatson * 5108410Srwatson * This software was developed by Robert Watson for the TrustedBSD Project. 6108410Srwatson * 756055Srwatson * Redistribution and use in source and binary forms, with or without 856055Srwatson * modification, are permitted provided that the following conditions 956055Srwatson * are met: 1056055Srwatson * 1. Redistributions of source code must retain the above copyright 1156055Srwatson * notice, this list of conditions and the following disclaimer. 1256055Srwatson * 2. Redistributions in binary form must reproduce the above copyright 1356055Srwatson * notice, this list of conditions and the following disclaimer in the 1456055Srwatson * documentation and/or other materials provided with the distribution. 1556055Srwatson * 1656055Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1756055Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1856055Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1956055Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2056055Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2156055Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2256055Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2356055Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2456055Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2556055Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2656055Srwatson * SUCH DAMAGE. 2756055Srwatson */ 2856055Srwatson/* 2956055Srwatson * acl_set_file -- set a file/directory ACL by name 3056055Srwatson */ 3156055Srwatson 3292986Sobrien#include <sys/cdefs.h> 3392986Sobrien__FBSDID("$FreeBSD$"); 3492986Sobrien 3556055Srwatson#include <sys/types.h> 3675185Stmm#include "namespace.h" 3756055Srwatson#include <sys/acl.h> 3875185Stmm#include "un-namespace.h" 3974667Sjedgar 4056055Srwatson#include <errno.h> 4174667Sjedgar#include <stdlib.h> 4274667Sjedgar#include <string.h> 43194955Strasz#include <unistd.h> 4456055Srwatson 4556055Srwatson#include "acl_support.h" 4656055Srwatson 4756055Srwatson/* 4856055Srwatson * For POSIX.1e-semantic ACLs, do a presort so the kernel doesn't have to 4956055Srwatson * (the POSIX.1e semantic code will reject unsorted ACL submission). If it's 5056055Srwatson * not a semantic that the library knows about, just submit it flat and 5156055Srwatson * assume the caller knows what they're up to. 5256055Srwatson */ 5356055Srwatsonint 5456055Srwatsonacl_set_file(const char *path_p, acl_type_t type, acl_t acl) 5556055Srwatson{ 5656055Srwatson 5791033Sjedgar if (acl == NULL || path_p == NULL) { 5891033Sjedgar errno = EINVAL; 5991033Sjedgar return (-1); 6091033Sjedgar } 61192586Strasz type = _acl_type_unold(type); 62194955Strasz if (_acl_type_not_valid_for_acl(acl, type)) { 63194955Strasz errno = EINVAL; 64194955Strasz return (-1); 65194955Strasz } 66208785Strasz if (_posix1e_acl(acl, type)) 67208785Strasz _posix1e_acl_sort(acl); 6856055Srwatson 6975928Sjedgar acl->ats_cur_entry = 0; 7075928Sjedgar 7175928Sjedgar return (__acl_set_file(path_p, type, &acl->ats_acl)); 7256055Srwatson} 7356055Srwatson 7456625Srwatsonint 75108410Srwatsonacl_set_link_np(const char *path_p, acl_type_t type, acl_t acl) 76108410Srwatson{ 77108410Srwatson 78108410Srwatson if (acl == NULL || path_p == NULL) { 79108410Srwatson errno = EINVAL; 80108410Srwatson return (-1); 81108410Srwatson } 82192586Strasz type = _acl_type_unold(type); 83194955Strasz if (_acl_type_not_valid_for_acl(acl, type)) { 84194955Strasz errno = EINVAL; 85194955Strasz return (-1); 86194955Strasz } 87208785Strasz if (_posix1e_acl(acl, type)) 88208785Strasz _posix1e_acl_sort(acl); 89108410Srwatson 90108410Srwatson acl->ats_cur_entry = 0; 91108410Srwatson 92108410Srwatson return (__acl_set_link(path_p, type, &acl->ats_acl)); 93108410Srwatson} 94108410Srwatson 95108410Srwatsonint 9656625Srwatsonacl_set_fd(int fd, acl_t acl) 9756625Srwatson{ 9856055Srwatson 99195004Strasz if (fpathconf(fd, _PC_ACL_NFS4) == 1) 100194955Strasz return (acl_set_fd_np(fd, acl, ACL_TYPE_NFS4)); 10156625Srwatson 102194955Strasz return (acl_set_fd_np(fd, acl, ACL_TYPE_ACCESS)); 10356625Srwatson} 10456625Srwatson 10556055Srwatsonint 10656625Srwatsonacl_set_fd_np(int fd, acl_t acl, acl_type_t type) 10756055Srwatson{ 10856055Srwatson 109194955Strasz if (acl == NULL) { 110194955Strasz errno = EINVAL; 111194955Strasz return (-1); 112194955Strasz } 113192586Strasz type = _acl_type_unold(type); 114194955Strasz if (_acl_type_not_valid_for_acl(acl, type)) { 115194955Strasz errno = EINVAL; 116194955Strasz return (-1); 117194955Strasz } 118208785Strasz if (_posix1e_acl(acl, type)) 119208785Strasz _posix1e_acl_sort(acl); 12056055Srwatson 12175928Sjedgar acl->ats_cur_entry = 0; 12275928Sjedgar 12375928Sjedgar return (___acl_set_fd(fd, type, &acl->ats_acl)); 12456055Srwatson} 12574667Sjedgar 12674667Sjedgar/* 12775928Sjedgar * acl_set_permset() (23.4.23): sets the permissions of ACL entry entry_d 12874667Sjedgar * with the permissions in permset_d 12974667Sjedgar */ 13074667Sjedgarint 13174667Sjedgaracl_set_permset(acl_entry_t entry_d, acl_permset_t permset_d) 13274667Sjedgar{ 13374667Sjedgar 13474667Sjedgar if (!entry_d) { 13574667Sjedgar errno = EINVAL; 13691034Sjedgar return (-1); 13774667Sjedgar } 13874667Sjedgar 139194955Strasz if ((*permset_d & ACL_POSIX1E_BITS) != *permset_d) { 140194955Strasz if ((*permset_d & ACL_NFS4_PERM_BITS) != *permset_d) { 141194955Strasz errno = EINVAL; 142194955Strasz return (-1); 143194955Strasz } 144194955Strasz if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 145194955Strasz errno = EINVAL; 146194955Strasz return (-1); 147194955Strasz } 148194955Strasz _entry_brand_as(entry_d, ACL_BRAND_NFS4); 149194955Strasz } 150194955Strasz 15174667Sjedgar entry_d->ae_perm = *permset_d; 15274667Sjedgar 15391034Sjedgar return (0); 15474667Sjedgar} 15574667Sjedgar 15674667Sjedgar/* 15774667Sjedgar * acl_set_qualifier() sets the qualifier (ae_id) of the tag for 15874667Sjedgar * ACL entry entry_d to the value referred to by tag_qualifier_p 15974667Sjedgar */ 16074667Sjedgarint 16174667Sjedgaracl_set_qualifier(acl_entry_t entry_d, const void *tag_qualifier_p) 16274667Sjedgar{ 163194955Strasz 16474667Sjedgar if (!entry_d || !tag_qualifier_p) { 16574667Sjedgar errno = EINVAL; 16691034Sjedgar return (-1); 16774667Sjedgar } 16874667Sjedgar switch(entry_d->ae_tag) { 16974667Sjedgar case ACL_USER: 17074667Sjedgar case ACL_GROUP: 17175526Sjedgar entry_d->ae_id = *(uid_t *)tag_qualifier_p; 17274667Sjedgar break; 17374667Sjedgar default: 17474667Sjedgar errno = EINVAL; 17591034Sjedgar return (-1); 17674667Sjedgar } 17774667Sjedgar 17891034Sjedgar return (0); 17974667Sjedgar} 18074667Sjedgar 18174667Sjedgar/* 18274667Sjedgar * acl_set_tag_type() sets the tag type for ACL entry entry_d to the 18374667Sjedgar * value of tag_type 18474667Sjedgar */ 18574667Sjedgarint 18674667Sjedgaracl_set_tag_type(acl_entry_t entry_d, acl_tag_t tag_type) 18774667Sjedgar{ 18874667Sjedgar 18991034Sjedgar if (entry_d == NULL) { 19074667Sjedgar errno = EINVAL; 19191034Sjedgar return (-1); 19274667Sjedgar } 19374667Sjedgar 19474667Sjedgar switch(tag_type) { 195194955Strasz case ACL_OTHER: 196194955Strasz case ACL_MASK: 197194955Strasz if (!_entry_brand_may_be(entry_d, ACL_BRAND_POSIX)) { 198194955Strasz errno = EINVAL; 199194955Strasz return (-1); 200194955Strasz } 201194955Strasz _entry_brand_as(entry_d, ACL_BRAND_POSIX); 202194955Strasz break; 203194955Strasz case ACL_EVERYONE: 204194955Strasz if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 205194955Strasz errno = EINVAL; 206194955Strasz return (-1); 207194955Strasz } 208194955Strasz _entry_brand_as(entry_d, ACL_BRAND_NFS4); 209194955Strasz break; 210194955Strasz } 211194955Strasz 212194955Strasz switch(tag_type) { 21374667Sjedgar case ACL_USER_OBJ: 21474667Sjedgar case ACL_USER: 21574667Sjedgar case ACL_GROUP_OBJ: 21674667Sjedgar case ACL_GROUP: 21774667Sjedgar case ACL_MASK: 21874667Sjedgar case ACL_OTHER: 219194955Strasz case ACL_EVERYONE: 22074667Sjedgar entry_d->ae_tag = tag_type; 22191034Sjedgar return (0); 22274667Sjedgar } 22374667Sjedgar 22474667Sjedgar errno = EINVAL; 22591034Sjedgar return (-1); 22674667Sjedgar} 227194955Strasz 228194955Straszint 229194955Straszacl_set_entry_type_np(acl_entry_t entry_d, acl_entry_type_t entry_type) 230194955Strasz{ 231194955Strasz 232194955Strasz if (entry_d == NULL) { 233194955Strasz errno = EINVAL; 234194955Strasz return (-1); 235194955Strasz } 236194955Strasz if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) { 237194955Strasz errno = EINVAL; 238194955Strasz return (-1); 239194955Strasz } 240194955Strasz _entry_brand_as(entry_d, ACL_BRAND_NFS4); 241194955Strasz 242194955Strasz switch (entry_type) { 243194955Strasz case ACL_ENTRY_TYPE_ALLOW: 244194955Strasz case ACL_ENTRY_TYPE_DENY: 245194955Strasz case ACL_ENTRY_TYPE_AUDIT: 246194955Strasz case ACL_ENTRY_TYPE_ALARM: 247194955Strasz entry_d->ae_entry_type = entry_type; 248194955Strasz return (0); 249194955Strasz } 250194955Strasz 251194955Strasz errno = EINVAL; 252194955Strasz return (-1); 253194955Strasz} 254