xref: /freebsd/lib/libc/posix1e/acl_get.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1515d7c92SRobert Watson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
46394f703SRobert Watson  * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
5515d7c92SRobert Watson  * All rights reserved.
6515d7c92SRobert Watson  *
76394f703SRobert Watson  * This software was developed by Robert Watson for the TrustedBSD Project.
86394f703SRobert Watson  *
9515d7c92SRobert Watson  * Redistribution and use in source and binary forms, with or without
10515d7c92SRobert Watson  * modification, are permitted provided that the following conditions
11515d7c92SRobert Watson  * are met:
12515d7c92SRobert Watson  * 1. Redistributions of source code must retain the above copyright
13515d7c92SRobert Watson  *    notice, this list of conditions and the following disclaimer.
14515d7c92SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
15515d7c92SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
16515d7c92SRobert Watson  *    documentation and/or other materials provided with the distribution.
17515d7c92SRobert Watson  *
18515d7c92SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19515d7c92SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20515d7c92SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21515d7c92SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22515d7c92SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23515d7c92SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24515d7c92SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25515d7c92SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26515d7c92SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27515d7c92SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28515d7c92SRobert Watson  * SUCH DAMAGE.
29515d7c92SRobert Watson  */
30515d7c92SRobert Watson /*
318f45e8c0SRobert Watson  * acl_get_fd - syscall wrapper for retrieving access ACL by fd
328f45e8c0SRobert Watson  * acl_get_fd_np - syscall wrapper for retrieving ACL by fd (non-POSIX)
336394f703SRobert Watson  * acl_get_file - syscall wrapper for retrieving ACL by filename
346394f703SRobert Watson  * acl_get_link_np - syscall wrapper for retrieving ACL by filename (NOFOLLOW)
356394f703SRobert Watson  *                   (non-POSIX)
369a227c57SChris D. Faulhaber  * acl_get_perm_np() checks if a permission is in the specified
379a227c57SChris D. Faulhaber  *                   permset (non-POSIX)
384bf60dfaSChris D. Faulhaber  * acl_get_permset() returns the permission set in the ACL entry
394bf60dfaSChris D. Faulhaber  * acl_get_qualifier() retrieves the qualifier of the tag from the ACL entry
404bf60dfaSChris D. Faulhaber  * acl_get_tag_type() returns the tag type for the ACL entry entry_d
41515d7c92SRobert Watson  */
42515d7c92SRobert Watson 
43515d7c92SRobert Watson #include <sys/types.h>
447bd44e92SThomas Moestl #include "namespace.h"
45515d7c92SRobert Watson #include <sys/acl.h>
467bd44e92SThomas Moestl #include "un-namespace.h"
474bf60dfaSChris D. Faulhaber 
484bf60dfaSChris D. Faulhaber #include <errno.h>
49c3380d40SEdward Tomasz Napierala #include <stdio.h>
50515d7c92SRobert Watson #include <stdlib.h>
514bf60dfaSChris D. Faulhaber #include <string.h>
52aa015c8eSEdward Tomasz Napierala #include <unistd.h>
53515d7c92SRobert Watson 
54ae1add4eSEdward Tomasz Napierala #include "acl_support.h"
55ae1add4eSEdward Tomasz Napierala 
56515d7c92SRobert Watson acl_t
acl_get_file(const char * path_p,acl_type_t type)57515d7c92SRobert Watson acl_get_file(const char *path_p, acl_type_t type)
58515d7c92SRobert Watson {
590f626307SChris D. Faulhaber 	acl_t	aclp;
60515d7c92SRobert Watson 	int	error;
61515d7c92SRobert Watson 
62d3352316SRobert Watson 	aclp = acl_init(ACL_MAX_ENTRIES);
639fd46b02SChris D. Faulhaber 	if (aclp == NULL)
64f0078215SRobert Watson 		return (NULL);
65515d7c92SRobert Watson 
66ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
670f626307SChris D. Faulhaber 	error = __acl_get_file(path_p, type, &aclp->ats_acl);
68515d7c92SRobert Watson 	if (error) {
69515d7c92SRobert Watson 		acl_free(aclp);
70f0078215SRobert Watson 		return (NULL);
71515d7c92SRobert Watson 	}
72515d7c92SRobert Watson 
73aa015c8eSEdward Tomasz Napierala 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
74aa015c8eSEdward Tomasz Napierala 	_acl_brand_from_type(aclp, type);
75aa015c8eSEdward Tomasz Napierala 
76515d7c92SRobert Watson 	return (aclp);
77515d7c92SRobert Watson }
78515d7c92SRobert Watson 
798f45e8c0SRobert Watson acl_t
acl_get_link_np(const char * path_p,acl_type_t type)806394f703SRobert Watson acl_get_link_np(const char *path_p, acl_type_t type)
816394f703SRobert Watson {
826394f703SRobert Watson 	acl_t	aclp;
836394f703SRobert Watson 	int	error;
846394f703SRobert Watson 
856394f703SRobert Watson 	aclp = acl_init(ACL_MAX_ENTRIES);
866394f703SRobert Watson 	if (aclp == NULL)
876394f703SRobert Watson 		return (NULL);
886394f703SRobert Watson 
89ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
906394f703SRobert Watson 	error = __acl_get_link(path_p, type, &aclp->ats_acl);
916394f703SRobert Watson 	if (error) {
926394f703SRobert Watson 		acl_free(aclp);
936394f703SRobert Watson 		return (NULL);
946394f703SRobert Watson 	}
956394f703SRobert Watson 
96aa015c8eSEdward Tomasz Napierala 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
97aa015c8eSEdward Tomasz Napierala 	_acl_brand_from_type(aclp, type);
98aa015c8eSEdward Tomasz Napierala 
996394f703SRobert Watson 	return (aclp);
1006394f703SRobert Watson }
1016394f703SRobert Watson 
1026394f703SRobert Watson acl_t
acl_get_fd(int fd)1038f45e8c0SRobert Watson acl_get_fd(int fd)
1048f45e8c0SRobert Watson {
105c3380d40SEdward Tomasz Napierala 	if (fpathconf(fd, _PC_ACL_NFS4) == 1)
106aa015c8eSEdward Tomasz Napierala 		return (acl_get_fd_np(fd, ACL_TYPE_NFS4));
1078f45e8c0SRobert Watson 
108aa015c8eSEdward Tomasz Napierala 	return (acl_get_fd_np(fd, ACL_TYPE_ACCESS));
1098f45e8c0SRobert Watson }
110515d7c92SRobert Watson 
111515d7c92SRobert Watson acl_t
acl_get_fd_np(int fd,acl_type_t type)1128f45e8c0SRobert Watson acl_get_fd_np(int fd, acl_type_t type)
113515d7c92SRobert Watson {
1140f626307SChris D. Faulhaber 	acl_t	aclp;
115515d7c92SRobert Watson 	int	error;
116515d7c92SRobert Watson 
117d3352316SRobert Watson 	aclp = acl_init(ACL_MAX_ENTRIES);
1189fd46b02SChris D. Faulhaber 	if (aclp == NULL)
119f0078215SRobert Watson 		return (NULL);
120515d7c92SRobert Watson 
121ae1add4eSEdward Tomasz Napierala 	type = _acl_type_unold(type);
1220f626307SChris D. Faulhaber 	error = ___acl_get_fd(fd, type, &aclp->ats_acl);
123515d7c92SRobert Watson 	if (error) {
124515d7c92SRobert Watson 		acl_free(aclp);
125f0078215SRobert Watson 		return (NULL);
126515d7c92SRobert Watson 	}
127515d7c92SRobert Watson 
128aa015c8eSEdward Tomasz Napierala 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
129aa015c8eSEdward Tomasz Napierala 	_acl_brand_from_type(aclp, type);
130aa015c8eSEdward Tomasz Napierala 
131515d7c92SRobert Watson 	return (aclp);
132515d7c92SRobert Watson }
1334bf60dfaSChris D. Faulhaber 
1340f626307SChris D. Faulhaber /*
1350f626307SChris D. Faulhaber  * acl_get_permset() (23.4.17): return via permset_p a descriptor to
1360f626307SChris D. Faulhaber  * the permission set in the ACL entry entry_d.
1370f626307SChris D. Faulhaber  */
1389a227c57SChris D. Faulhaber int
acl_get_permset(acl_entry_t entry_d,acl_permset_t * permset_p)1394bf60dfaSChris D. Faulhaber acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
1404bf60dfaSChris D. Faulhaber {
1414bf60dfaSChris D. Faulhaber 
1429fd46b02SChris D. Faulhaber 	if (entry_d == NULL || permset_p == NULL) {
1434bf60dfaSChris D. Faulhaber 		errno = EINVAL;
1449fd46b02SChris D. Faulhaber 		return (-1);
1454bf60dfaSChris D. Faulhaber 	}
1464bf60dfaSChris D. Faulhaber 
1474bf60dfaSChris D. Faulhaber 	*permset_p = &entry_d->ae_perm;
1484bf60dfaSChris D. Faulhaber 
1499fd46b02SChris D. Faulhaber 	return (0);
1504bf60dfaSChris D. Faulhaber }
1514bf60dfaSChris D. Faulhaber 
1520f626307SChris D. Faulhaber /*
1530f626307SChris D. Faulhaber  * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
1540f626307SChris D. Faulhaber  * for the ACL entry entry_d.
1550f626307SChris D. Faulhaber  */
1564bf60dfaSChris D. Faulhaber void *
acl_get_qualifier(acl_entry_t entry_d)1574bf60dfaSChris D. Faulhaber acl_get_qualifier(acl_entry_t entry_d)
1584bf60dfaSChris D. Faulhaber {
1594bf60dfaSChris D. Faulhaber 	uid_t *retval;
1604bf60dfaSChris D. Faulhaber 
1619fd46b02SChris D. Faulhaber 	if (entry_d == NULL) {
1624bf60dfaSChris D. Faulhaber 		errno = EINVAL;
1639fd46b02SChris D. Faulhaber 		return (NULL);
1644bf60dfaSChris D. Faulhaber 	}
1654bf60dfaSChris D. Faulhaber 
1664bf60dfaSChris D. Faulhaber 	switch(entry_d->ae_tag) {
1674bf60dfaSChris D. Faulhaber 	case ACL_USER:
1684bf60dfaSChris D. Faulhaber 	case ACL_GROUP:
1694bf60dfaSChris D. Faulhaber 		retval = malloc(sizeof(uid_t));
1709fd46b02SChris D. Faulhaber 		if (retval == NULL)
1719fd46b02SChris D. Faulhaber 			return (NULL);
1724bf60dfaSChris D. Faulhaber 		*retval = entry_d->ae_id;
1739fd46b02SChris D. Faulhaber 		return (retval);
1744bf60dfaSChris D. Faulhaber 	}
1754bf60dfaSChris D. Faulhaber 
1764bf60dfaSChris D. Faulhaber 	errno = EINVAL;
1779fd46b02SChris D. Faulhaber 	return (NULL);
1784bf60dfaSChris D. Faulhaber }
1794bf60dfaSChris D. Faulhaber 
1800f626307SChris D. Faulhaber /*
1810f626307SChris D. Faulhaber  * acl_get_tag_type() (23.4.19): return the tag type for the ACL
1820f626307SChris D. Faulhaber  * entry entry_p.
1830f626307SChris D. Faulhaber  */
1844bf60dfaSChris D. Faulhaber int
acl_get_tag_type(acl_entry_t entry_d,acl_tag_t * tag_type_p)1854bf60dfaSChris D. Faulhaber acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
1864bf60dfaSChris D. Faulhaber {
1874bf60dfaSChris D. Faulhaber 
1889fd46b02SChris D. Faulhaber 	if (entry_d == NULL || tag_type_p == NULL) {
1894bf60dfaSChris D. Faulhaber 		errno = EINVAL;
1909fd46b02SChris D. Faulhaber 		return (-1);
1914bf60dfaSChris D. Faulhaber 	}
1924bf60dfaSChris D. Faulhaber 
1934bf60dfaSChris D. Faulhaber 	*tag_type_p = entry_d->ae_tag;
1944bf60dfaSChris D. Faulhaber 
1959fd46b02SChris D. Faulhaber 	return (0);
1964bf60dfaSChris D. Faulhaber }
197aa015c8eSEdward Tomasz Napierala 
198aa015c8eSEdward Tomasz Napierala int
acl_get_entry_type_np(acl_entry_t entry_d,acl_entry_type_t * entry_type_p)199aa015c8eSEdward Tomasz Napierala acl_get_entry_type_np(acl_entry_t entry_d, acl_entry_type_t *entry_type_p)
200aa015c8eSEdward Tomasz Napierala {
201aa015c8eSEdward Tomasz Napierala 
202aa015c8eSEdward Tomasz Napierala 	if (entry_d == NULL || entry_type_p == NULL) {
203aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
204aa015c8eSEdward Tomasz Napierala 		return (-1);
205aa015c8eSEdward Tomasz Napierala 	}
206aa015c8eSEdward Tomasz Napierala 
207aa015c8eSEdward Tomasz Napierala 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
208aa015c8eSEdward Tomasz Napierala 		errno = EINVAL;
209aa015c8eSEdward Tomasz Napierala 		return (-1);
210aa015c8eSEdward Tomasz Napierala 	}
211aa015c8eSEdward Tomasz Napierala 
212aa015c8eSEdward Tomasz Napierala 	*entry_type_p = entry_d->ae_entry_type;
213aa015c8eSEdward Tomasz Napierala 
214aa015c8eSEdward Tomasz Napierala 	return (0);
215aa015c8eSEdward Tomasz Napierala }
216