1515d7c92SRobert Watson /*- 2*d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*d915a14eSPedro F. Giffuni * 4d5af31a2SChris D. Faulhaber * Copyright (c) 1999-2002 Robert N. M. Watson 5515d7c92SRobert Watson * All rights reserved. 6515d7c92SRobert 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_to_text - return a text string with a text representation of the acl 30515d7c92SRobert Watson * in it. 31515d7c92SRobert Watson */ 32515d7c92SRobert Watson 33333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 34333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 35333fc21eSDavid E. O'Brien 36515d7c92SRobert Watson #include <sys/types.h> 377bd44e92SThomas Moestl #include "namespace.h" 38515d7c92SRobert Watson #include <sys/acl.h> 397bd44e92SThomas Moestl #include "un-namespace.h" 40515d7c92SRobert Watson #include <sys/errno.h> 41515d7c92SRobert Watson #include <stdio.h> 42515d7c92SRobert Watson #include <stdlib.h> 43515d7c92SRobert Watson #include <string.h> 44515d7c92SRobert Watson 45515d7c92SRobert Watson #include "acl_support.h" 46515d7c92SRobert Watson 47515d7c92SRobert Watson /* 48515d7c92SRobert Watson * acl_to_text - generate a text form of an acl 49515d7c92SRobert Watson * spec says nothing about output ordering, so leave in acl order 50515d7c92SRobert Watson * 518f45e8c0SRobert Watson * This function will not produce nice results if it is called with 528f45e8c0SRobert Watson * a non-POSIX.1e semantics ACL. 53515d7c92SRobert Watson */ 54aa015c8eSEdward Tomasz Napierala 55aa015c8eSEdward Tomasz Napierala char *_nfs4_acl_to_text_np(const acl_t acl, ssize_t *len_p, int flags); 56aa015c8eSEdward Tomasz Napierala 57aa015c8eSEdward Tomasz Napierala static char * 58aa015c8eSEdward Tomasz Napierala _posix1e_acl_to_text(acl_t acl, ssize_t *len_p, int flags) 59515d7c92SRobert Watson { 600f626307SChris D. Faulhaber struct acl *acl_int; 61515d7c92SRobert Watson char *buf, *tmpbuf; 62dc29acd1SEd Schouten char name_buf[MAXLOGNAME]; 632de14c39SRobert Watson char perm_buf[_POSIX1E_ACL_STRING_PERM_MAXSIZE+1], 642de14c39SRobert Watson effective_perm_buf[_POSIX1E_ACL_STRING_PERM_MAXSIZE+1]; 65515d7c92SRobert Watson int i, error, len; 66515d7c92SRobert Watson uid_t ae_id; 67515d7c92SRobert Watson acl_tag_t ae_tag; 68515d7c92SRobert Watson acl_perm_t ae_perm, effective_perm, mask_perm; 69515d7c92SRobert Watson 70515d7c92SRobert Watson buf = strdup(""); 719fd46b02SChris D. Faulhaber if (buf == NULL) 72f0078215SRobert Watson return(NULL); 73515d7c92SRobert Watson 740f626307SChris D. Faulhaber acl_int = &acl->ats_acl; 75515d7c92SRobert Watson 760f626307SChris D. Faulhaber mask_perm = ACL_PERM_BITS; /* effective is regular if no mask */ 770f626307SChris D. Faulhaber for (i = 0; i < acl_int->acl_cnt; i++) 780f626307SChris D. Faulhaber if (acl_int->acl_entry[i].ae_tag == ACL_MASK) 790f626307SChris D. Faulhaber mask_perm = acl_int->acl_entry[i].ae_perm; 800f626307SChris D. Faulhaber 810f626307SChris D. Faulhaber for (i = 0; i < acl_int->acl_cnt; i++) { 820f626307SChris D. Faulhaber ae_tag = acl_int->acl_entry[i].ae_tag; 830f626307SChris D. Faulhaber ae_id = acl_int->acl_entry[i].ae_id; 840f626307SChris D. Faulhaber ae_perm = acl_int->acl_entry[i].ae_perm; 85515d7c92SRobert Watson 86515d7c92SRobert Watson switch(ae_tag) { 87515d7c92SRobert Watson case ACL_USER_OBJ: 882de14c39SRobert Watson error = _posix1e_acl_perm_to_string(ae_perm, 892de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf); 90515d7c92SRobert Watson if (error) 91515d7c92SRobert Watson goto error_label; 92515d7c92SRobert Watson len = asprintf(&tmpbuf, "%suser::%s\n", buf, 93515d7c92SRobert Watson perm_buf); 942137646aSRobert Watson if (len == -1) 95515d7c92SRobert Watson goto error_label; 96515d7c92SRobert Watson free(buf); 97515d7c92SRobert Watson buf = tmpbuf; 98515d7c92SRobert Watson break; 99515d7c92SRobert Watson 100515d7c92SRobert Watson case ACL_USER: 1012de14c39SRobert Watson error = _posix1e_acl_perm_to_string(ae_perm, 1022de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf); 103515d7c92SRobert Watson if (error) 104515d7c92SRobert Watson goto error_label; 105515d7c92SRobert Watson 1062de14c39SRobert Watson error = _posix1e_acl_id_to_name(ae_tag, ae_id, 107dc29acd1SEd Schouten MAXLOGNAME, name_buf, flags); 108515d7c92SRobert Watson if (error) 109515d7c92SRobert Watson goto error_label; 110515d7c92SRobert Watson 111515d7c92SRobert Watson effective_perm = ae_perm & mask_perm; 112515d7c92SRobert Watson if (effective_perm != ae_perm) { 1132de14c39SRobert Watson error = _posix1e_acl_perm_to_string( 1142de14c39SRobert Watson effective_perm, 1152de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, 116515d7c92SRobert Watson effective_perm_buf); 117515d7c92SRobert Watson if (error) 118515d7c92SRobert Watson goto error_label; 119515d7c92SRobert Watson len = asprintf(&tmpbuf, "%suser:%s:%s\t\t# " 120515d7c92SRobert Watson "effective: %s\n", 121515d7c92SRobert Watson buf, name_buf, perm_buf, 122515d7c92SRobert Watson effective_perm_buf); 123515d7c92SRobert Watson } else { 124515d7c92SRobert Watson len = asprintf(&tmpbuf, "%suser:%s:%s\n", buf, 125515d7c92SRobert Watson name_buf, perm_buf); 126515d7c92SRobert Watson } 1272137646aSRobert Watson if (len == -1) 128515d7c92SRobert Watson goto error_label; 129515d7c92SRobert Watson free(buf); 130515d7c92SRobert Watson buf = tmpbuf; 131515d7c92SRobert Watson break; 132515d7c92SRobert Watson 133515d7c92SRobert Watson case ACL_GROUP_OBJ: 1342de14c39SRobert Watson error = _posix1e_acl_perm_to_string(ae_perm, 1352de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf); 136515d7c92SRobert Watson if (error) 137515d7c92SRobert Watson goto error_label; 138515d7c92SRobert Watson 139515d7c92SRobert Watson effective_perm = ae_perm & mask_perm; 140515d7c92SRobert Watson if (effective_perm != ae_perm) { 1412de14c39SRobert Watson error = _posix1e_acl_perm_to_string( 1422de14c39SRobert Watson effective_perm, 1432de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, 144515d7c92SRobert Watson effective_perm_buf); 145515d7c92SRobert Watson if (error) 146515d7c92SRobert Watson goto error_label; 147515d7c92SRobert Watson len = asprintf(&tmpbuf, "%sgroup::%s\t\t# " 148515d7c92SRobert Watson "effective: %s\n", 149515d7c92SRobert Watson buf, perm_buf, effective_perm_buf); 150515d7c92SRobert Watson } else { 151515d7c92SRobert Watson len = asprintf(&tmpbuf, "%sgroup::%s\n", buf, 152515d7c92SRobert Watson perm_buf); 153515d7c92SRobert Watson } 1542137646aSRobert Watson if (len == -1) 155515d7c92SRobert Watson goto error_label; 156515d7c92SRobert Watson free(buf); 157515d7c92SRobert Watson buf = tmpbuf; 158515d7c92SRobert Watson break; 159515d7c92SRobert Watson 160515d7c92SRobert Watson case ACL_GROUP: 1612de14c39SRobert Watson error = _posix1e_acl_perm_to_string(ae_perm, 1622de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf); 163515d7c92SRobert Watson if (error) 164515d7c92SRobert Watson goto error_label; 165515d7c92SRobert Watson 1662de14c39SRobert Watson error = _posix1e_acl_id_to_name(ae_tag, ae_id, 167dc29acd1SEd Schouten MAXLOGNAME, name_buf, flags); 168515d7c92SRobert Watson if (error) 169515d7c92SRobert Watson goto error_label; 170515d7c92SRobert Watson 171515d7c92SRobert Watson effective_perm = ae_perm & mask_perm; 172515d7c92SRobert Watson if (effective_perm != ae_perm) { 1732de14c39SRobert Watson error = _posix1e_acl_perm_to_string( 1742de14c39SRobert Watson effective_perm, 1752de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, 176515d7c92SRobert Watson effective_perm_buf); 177515d7c92SRobert Watson if (error) 178515d7c92SRobert Watson goto error_label; 17982fefadaSRobert Watson len = asprintf(&tmpbuf, "%sgroup:%s:%s\t\t# " 180515d7c92SRobert Watson "effective: %s\n", 18182fefadaSRobert Watson buf, name_buf, perm_buf, 18282fefadaSRobert Watson effective_perm_buf); 183515d7c92SRobert Watson } else { 184515d7c92SRobert Watson len = asprintf(&tmpbuf, "%sgroup:%s:%s\n", buf, 185515d7c92SRobert Watson name_buf, perm_buf); 186515d7c92SRobert Watson } 1872137646aSRobert Watson if (len == -1) 188515d7c92SRobert Watson goto error_label; 189515d7c92SRobert Watson free(buf); 190515d7c92SRobert Watson buf = tmpbuf; 191515d7c92SRobert Watson break; 192515d7c92SRobert Watson 193515d7c92SRobert Watson case ACL_MASK: 1942de14c39SRobert Watson error = _posix1e_acl_perm_to_string(ae_perm, 1952de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf); 196515d7c92SRobert Watson if (error) 197515d7c92SRobert Watson goto error_label; 198515d7c92SRobert Watson 199515d7c92SRobert Watson len = asprintf(&tmpbuf, "%smask::%s\n", buf, 200515d7c92SRobert Watson perm_buf); 2012137646aSRobert Watson if (len == -1) 202515d7c92SRobert Watson goto error_label; 203515d7c92SRobert Watson free(buf); 204515d7c92SRobert Watson buf = tmpbuf; 205515d7c92SRobert Watson break; 206515d7c92SRobert Watson 207515d7c92SRobert Watson case ACL_OTHER: 2082de14c39SRobert Watson error = _posix1e_acl_perm_to_string(ae_perm, 2092de14c39SRobert Watson _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf); 210515d7c92SRobert Watson if (error) 211515d7c92SRobert Watson goto error_label; 212515d7c92SRobert Watson 213515d7c92SRobert Watson len = asprintf(&tmpbuf, "%sother::%s\n", buf, 214515d7c92SRobert Watson perm_buf); 2152137646aSRobert Watson if (len == -1) 216515d7c92SRobert Watson goto error_label; 217515d7c92SRobert Watson free(buf); 218515d7c92SRobert Watson buf = tmpbuf; 219515d7c92SRobert Watson break; 220515d7c92SRobert Watson 221515d7c92SRobert Watson default: 222515d7c92SRobert Watson errno = EINVAL; 2232137646aSRobert Watson goto error_label; 224515d7c92SRobert Watson } 225515d7c92SRobert Watson } 226515d7c92SRobert Watson 227515d7c92SRobert Watson if (len_p) { 228515d7c92SRobert Watson *len_p = strlen(buf); 229515d7c92SRobert Watson } 230515d7c92SRobert Watson return (buf); 231515d7c92SRobert Watson 232515d7c92SRobert Watson error_label: 233515d7c92SRobert Watson /* jump to here sets errno already, we just clean up */ 234515d7c92SRobert Watson if (buf) free(buf); 235f0078215SRobert Watson return (NULL); 236515d7c92SRobert Watson } 237aa015c8eSEdward Tomasz Napierala 238aa015c8eSEdward Tomasz Napierala char * 239aa015c8eSEdward Tomasz Napierala acl_to_text_np(acl_t acl, ssize_t *len_p, int flags) 240aa015c8eSEdward Tomasz Napierala { 241aa015c8eSEdward Tomasz Napierala 24259831d75SEdward Tomasz Napierala if (acl == NULL) { 24359831d75SEdward Tomasz Napierala errno = EINVAL; 24459831d75SEdward Tomasz Napierala return(NULL); 24559831d75SEdward Tomasz Napierala } 24659831d75SEdward Tomasz Napierala 247aa015c8eSEdward Tomasz Napierala switch (_acl_brand(acl)) { 248aa015c8eSEdward Tomasz Napierala case ACL_BRAND_POSIX: 249aa015c8eSEdward Tomasz Napierala return (_posix1e_acl_to_text(acl, len_p, flags)); 250aa015c8eSEdward Tomasz Napierala case ACL_BRAND_NFS4: 251aa015c8eSEdward Tomasz Napierala return (_nfs4_acl_to_text_np(acl, len_p, flags)); 252aa015c8eSEdward Tomasz Napierala default: 253aa015c8eSEdward Tomasz Napierala errno = EINVAL; 254aa015c8eSEdward Tomasz Napierala return (NULL); 255aa015c8eSEdward Tomasz Napierala } 256aa015c8eSEdward Tomasz Napierala } 257aa015c8eSEdward Tomasz Napierala 258aa015c8eSEdward Tomasz Napierala char * 259aa015c8eSEdward Tomasz Napierala acl_to_text(acl_t acl, ssize_t *len_p) 260aa015c8eSEdward Tomasz Napierala { 261aa015c8eSEdward Tomasz Napierala 262aa015c8eSEdward Tomasz Napierala return (acl_to_text_np(acl, len_p, 0)); 263aa015c8eSEdward Tomasz Napierala } 264