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/*
2956625Srwatson * acl_get_fd - syscall wrapper for retrieving access ACL by fd
3056625Srwatson * acl_get_fd_np - syscall wrapper for retrieving ACL by fd (non-POSIX)
31108410Srwatson * acl_get_file - syscall wrapper for retrieving ACL by filename
32108410Srwatson * acl_get_link_np - syscall wrapper for retrieving ACL by filename (NOFOLLOW)
33108410Srwatson *                   (non-POSIX)
3475492Sjedgar * acl_get_perm_np() checks if a permission is in the specified
3575492Sjedgar *                   permset (non-POSIX)
3674667Sjedgar * acl_get_permset() returns the permission set in the ACL entry
3774667Sjedgar * acl_get_qualifier() retrieves the qualifier of the tag from the ACL entry
3874667Sjedgar * acl_get_tag_type() returns the tag type for the ACL entry entry_d
3956055Srwatson */
4056055Srwatson
4192986Sobrien#include <sys/cdefs.h>
4292986Sobrien__FBSDID("$FreeBSD$");
4392986Sobrien
4456055Srwatson#include <sys/types.h>
4575185Stmm#include "namespace.h"
4656055Srwatson#include <sys/acl.h>
4775185Stmm#include "un-namespace.h"
4874667Sjedgar
4974667Sjedgar#include <errno.h>
50195004Strasz#include <stdio.h>
5156055Srwatson#include <stdlib.h>
5274667Sjedgar#include <string.h>
53194955Strasz#include <unistd.h>
5456055Srwatson
55192586Strasz#include "acl_support.h"
56192586Strasz
5756055Srwatsonacl_t
5856055Srwatsonacl_get_file(const char *path_p, acl_type_t type)
5956055Srwatson{
6075928Sjedgar	acl_t	aclp;
6156055Srwatson	int	error;
6256055Srwatson
6356274Srwatson	aclp = acl_init(ACL_MAX_ENTRIES);
6491034Sjedgar	if (aclp == NULL)
6571142Srwatson		return (NULL);
6656055Srwatson
67192586Strasz	type = _acl_type_unold(type);
6875928Sjedgar	error = __acl_get_file(path_p, type, &aclp->ats_acl);
6956055Srwatson	if (error) {
7056055Srwatson		acl_free(aclp);
7171142Srwatson		return (NULL);
7256055Srwatson	}
7356055Srwatson
74194955Strasz	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
75194955Strasz	_acl_brand_from_type(aclp, type);
76194955Strasz
7756055Srwatson	return (aclp);
7856055Srwatson}
7956055Srwatson
8056625Srwatsonacl_t
81108410Srwatsonacl_get_link_np(const char *path_p, acl_type_t type)
82108410Srwatson{
83108410Srwatson	acl_t	aclp;
84108410Srwatson	int	error;
85108410Srwatson
86108410Srwatson	aclp = acl_init(ACL_MAX_ENTRIES);
87108410Srwatson	if (aclp == NULL)
88108410Srwatson		return (NULL);
89108410Srwatson
90192586Strasz	type = _acl_type_unold(type);
91108410Srwatson	error = __acl_get_link(path_p, type, &aclp->ats_acl);
92108410Srwatson	if (error) {
93108410Srwatson		acl_free(aclp);
94108410Srwatson		return (NULL);
95108410Srwatson	}
96108410Srwatson
97194955Strasz	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
98194955Strasz	_acl_brand_from_type(aclp, type);
99194955Strasz
100108410Srwatson	return (aclp);
101108410Srwatson}
102108410Srwatson
103108410Srwatsonacl_t
10456625Srwatsonacl_get_fd(int fd)
10556625Srwatson{
106195004Strasz	if (fpathconf(fd, _PC_ACL_NFS4) == 1)
107194955Strasz		return (acl_get_fd_np(fd, ACL_TYPE_NFS4));
10856055Srwatson
109194955Strasz	return (acl_get_fd_np(fd, ACL_TYPE_ACCESS));
11056625Srwatson}
11156625Srwatson
11256055Srwatsonacl_t
11356625Srwatsonacl_get_fd_np(int fd, acl_type_t type)
11456055Srwatson{
11575928Sjedgar	acl_t	aclp;
11656055Srwatson	int	error;
11756055Srwatson
11856274Srwatson	aclp = acl_init(ACL_MAX_ENTRIES);
11991034Sjedgar	if (aclp == NULL)
12071142Srwatson		return (NULL);
12156055Srwatson
122192586Strasz	type = _acl_type_unold(type);
12375928Sjedgar	error = ___acl_get_fd(fd, type, &aclp->ats_acl);
12456055Srwatson	if (error) {
12556055Srwatson		acl_free(aclp);
12671142Srwatson		return (NULL);
12756055Srwatson	}
12856055Srwatson
129194955Strasz	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
130194955Strasz	_acl_brand_from_type(aclp, type);
131194955Strasz
13256055Srwatson	return (aclp);
13356055Srwatson}
13474667Sjedgar
13575928Sjedgar/*
13675928Sjedgar * acl_get_permset() (23.4.17): return via permset_p a descriptor to
13775928Sjedgar * the permission set in the ACL entry entry_d.
13875928Sjedgar */
13975492Sjedgarint
14074667Sjedgaracl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
14174667Sjedgar{
14274667Sjedgar
14391034Sjedgar	if (entry_d == NULL || permset_p == NULL) {
14474667Sjedgar		errno = EINVAL;
14591034Sjedgar		return (-1);
14674667Sjedgar	}
14774667Sjedgar
14874667Sjedgar	*permset_p = &entry_d->ae_perm;
14974667Sjedgar
15091034Sjedgar	return (0);
15174667Sjedgar}
15274667Sjedgar
15375928Sjedgar/*
15475928Sjedgar * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
15575928Sjedgar * for the ACL entry entry_d.
15675928Sjedgar */
15774667Sjedgarvoid *
15874667Sjedgaracl_get_qualifier(acl_entry_t entry_d)
15974667Sjedgar{
16074667Sjedgar	uid_t *retval;
16174667Sjedgar
16291034Sjedgar	if (entry_d == NULL) {
16374667Sjedgar		errno = EINVAL;
16491034Sjedgar		return (NULL);
16574667Sjedgar	}
16674667Sjedgar
16774667Sjedgar	switch(entry_d->ae_tag) {
16874667Sjedgar	case ACL_USER:
16974667Sjedgar	case ACL_GROUP:
17074667Sjedgar		retval = malloc(sizeof(uid_t));
17191034Sjedgar		if (retval == NULL)
17291034Sjedgar			return (NULL);
17375928Sjedgar		*retval = entry_d->ae_id;
17491034Sjedgar		return (retval);
17574667Sjedgar	}
17674667Sjedgar
17774667Sjedgar	errno = EINVAL;
17891034Sjedgar	return (NULL);
17974667Sjedgar}
18074667Sjedgar
18175928Sjedgar/*
18275928Sjedgar * acl_get_tag_type() (23.4.19): return the tag type for the ACL
18375928Sjedgar * entry entry_p.
18475928Sjedgar */
18574667Sjedgarint
18674667Sjedgaracl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
18774667Sjedgar{
18874667Sjedgar
18991034Sjedgar	if (entry_d == NULL || tag_type_p == NULL) {
19074667Sjedgar		errno = EINVAL;
19191034Sjedgar		return (-1);
19274667Sjedgar	}
19374667Sjedgar
19474667Sjedgar	*tag_type_p = entry_d->ae_tag;
19574667Sjedgar
19691034Sjedgar	return (0);
19774667Sjedgar}
198194955Strasz
199194955Straszint
200194955Straszacl_get_entry_type_np(acl_entry_t entry_d, acl_entry_type_t *entry_type_p)
201194955Strasz{
202194955Strasz
203194955Strasz	if (entry_d == NULL || entry_type_p == NULL) {
204194955Strasz		errno = EINVAL;
205194955Strasz		return (-1);
206194955Strasz	}
207194955Strasz
208194955Strasz	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
209194955Strasz		errno = EINVAL;
210194955Strasz		return (-1);
211194955Strasz	}
212194955Strasz
213194955Strasz	*entry_type_p = entry_d->ae_entry_type;
214194955Strasz
215194955Strasz	return (0);
216194955Strasz}
217