acl_get.c revision 192586
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: head/lib/libc/posix1e/acl_get.c 192586 2009-05-22 15:56:43Z trasz $");
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>
5056055Srwatson#include <stdlib.h>
5174667Sjedgar#include <string.h>
5256055Srwatson
53192586Strasz#include "acl_support.h"
54192586Strasz
5556055Srwatsonacl_t
5656055Srwatsonacl_get_file(const char *path_p, acl_type_t type)
5756055Srwatson{
5875928Sjedgar	acl_t	aclp;
5956055Srwatson	int	error;
6056055Srwatson
6156274Srwatson	aclp = acl_init(ACL_MAX_ENTRIES);
6291034Sjedgar	if (aclp == NULL)
6371142Srwatson		return (NULL);
6456055Srwatson
65192586Strasz	type = _acl_type_unold(type);
6675928Sjedgar	error = __acl_get_file(path_p, type, &aclp->ats_acl);
6756055Srwatson	if (error) {
6856055Srwatson		acl_free(aclp);
6971142Srwatson		return (NULL);
7056055Srwatson	}
7156055Srwatson
7256055Srwatson	return (aclp);
7356055Srwatson}
7456055Srwatson
7556625Srwatsonacl_t
76108410Srwatsonacl_get_link_np(const char *path_p, acl_type_t type)
77108410Srwatson{
78108410Srwatson	acl_t	aclp;
79108410Srwatson	int	error;
80108410Srwatson
81108410Srwatson	aclp = acl_init(ACL_MAX_ENTRIES);
82108410Srwatson	if (aclp == NULL)
83108410Srwatson		return (NULL);
84108410Srwatson
85192586Strasz	type = _acl_type_unold(type);
86108410Srwatson	error = __acl_get_link(path_p, type, &aclp->ats_acl);
87108410Srwatson	if (error) {
88108410Srwatson		acl_free(aclp);
89108410Srwatson		return (NULL);
90108410Srwatson	}
91108410Srwatson
92108410Srwatson	return (aclp);
93108410Srwatson}
94108410Srwatson
95108410Srwatsonacl_t
9656625Srwatsonacl_get_fd(int fd)
9756625Srwatson{
9875928Sjedgar	acl_t	aclp;
9956625Srwatson	int	error;
10056055Srwatson
10156625Srwatson	aclp = acl_init(ACL_MAX_ENTRIES);
10291034Sjedgar	if (aclp == NULL)
10371142Srwatson		return (NULL);
10456625Srwatson
10575928Sjedgar	error = ___acl_get_fd(fd, ACL_TYPE_ACCESS, &aclp->ats_acl);
10656625Srwatson	if (error) {
10756625Srwatson		acl_free(aclp);
10871142Srwatson		return (NULL);
10956625Srwatson	}
11056625Srwatson
11156625Srwatson	return (aclp);
11256625Srwatson}
11356625Srwatson
11456055Srwatsonacl_t
11556625Srwatsonacl_get_fd_np(int fd, acl_type_t type)
11656055Srwatson{
11775928Sjedgar	acl_t	aclp;
11856055Srwatson	int	error;
11956055Srwatson
12056274Srwatson	aclp = acl_init(ACL_MAX_ENTRIES);
12191034Sjedgar	if (aclp == NULL)
12271142Srwatson		return (NULL);
12356055Srwatson
124192586Strasz	type = _acl_type_unold(type);
12575928Sjedgar	error = ___acl_get_fd(fd, type, &aclp->ats_acl);
12656055Srwatson	if (error) {
12756055Srwatson		acl_free(aclp);
12871142Srwatson		return (NULL);
12956055Srwatson	}
13056055Srwatson
13156055Srwatson	return (aclp);
13256055Srwatson}
13374667Sjedgar
13474667Sjedgarint
13575492Sjedgaracl_get_perm_np(acl_permset_t permset_d, acl_perm_t perm)
13675492Sjedgar{
13775492Sjedgar
13891034Sjedgar	if (permset_d == NULL) {
13975928Sjedgar		errno = EINVAL;
14091034Sjedgar		return (-1);
14175928Sjedgar	}
14275928Sjedgar
14375492Sjedgar	switch(perm) {
14475492Sjedgar	case ACL_READ:
14575492Sjedgar	case ACL_WRITE:
14675492Sjedgar	case ACL_EXECUTE:
14775492Sjedgar		if (*permset_d & perm)
14891034Sjedgar			return (1);
14975492Sjedgar		break;
15075492Sjedgar	default:
15175492Sjedgar		errno = EINVAL;
15291034Sjedgar		return (-1);
15375492Sjedgar	}
15475492Sjedgar
15591034Sjedgar	return (0);
15675492Sjedgar}
15775492Sjedgar
15875928Sjedgar/*
15975928Sjedgar * acl_get_permset() (23.4.17): return via permset_p a descriptor to
16075928Sjedgar * the permission set in the ACL entry entry_d.
16175928Sjedgar */
16275492Sjedgarint
16374667Sjedgaracl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
16474667Sjedgar{
16574667Sjedgar
16691034Sjedgar	if (entry_d == NULL || permset_p == NULL) {
16774667Sjedgar		errno = EINVAL;
16891034Sjedgar		return (-1);
16974667Sjedgar	}
17074667Sjedgar
17174667Sjedgar	*permset_p = &entry_d->ae_perm;
17274667Sjedgar
17391034Sjedgar	return (0);
17474667Sjedgar}
17574667Sjedgar
17675928Sjedgar/*
17775928Sjedgar * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
17875928Sjedgar * for the ACL entry entry_d.
17975928Sjedgar */
18074667Sjedgarvoid *
18174667Sjedgaracl_get_qualifier(acl_entry_t entry_d)
18274667Sjedgar{
18374667Sjedgar	uid_t *retval;
18474667Sjedgar
18591034Sjedgar	if (entry_d == NULL) {
18674667Sjedgar		errno = EINVAL;
18791034Sjedgar		return (NULL);
18874667Sjedgar	}
18974667Sjedgar
19074667Sjedgar	switch(entry_d->ae_tag) {
19174667Sjedgar	case ACL_USER:
19274667Sjedgar	case ACL_GROUP:
19374667Sjedgar		retval = malloc(sizeof(uid_t));
19491034Sjedgar		if (retval == NULL)
19591034Sjedgar			return (NULL);
19675928Sjedgar		*retval = entry_d->ae_id;
19791034Sjedgar		return (retval);
19874667Sjedgar	}
19974667Sjedgar
20074667Sjedgar	errno = EINVAL;
20191034Sjedgar	return (NULL);
20274667Sjedgar}
20374667Sjedgar
20475928Sjedgar/*
20575928Sjedgar * acl_get_tag_type() (23.4.19): return the tag type for the ACL
20675928Sjedgar * entry entry_p.
20775928Sjedgar */
20874667Sjedgarint
20974667Sjedgaracl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
21074667Sjedgar{
21174667Sjedgar
21291034Sjedgar	if (entry_d == NULL || tag_type_p == NULL) {
21374667Sjedgar		errno = EINVAL;
21491034Sjedgar		return (-1);
21574667Sjedgar	}
21674667Sjedgar
21774667Sjedgar	*tag_type_p = entry_d->ae_tag;
21874667Sjedgar
21991034Sjedgar	return (0);
22074667Sjedgar}
221