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_valid -- POSIX.1e ACL check routine
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"
3956055Srwatson#include <sys/errno.h>
4091033Sjedgar#include <stdlib.h>
4156055Srwatson
4256055Srwatson#include "acl_support.h"
4356055Srwatson
4456055Srwatson/*
4556055Srwatson * acl_valid: accepts an ACL, returns 0 on valid ACL, -1 for invalid,
4656055Srwatson * and errno set to EINVAL.
4756055Srwatson *
4856055Srwatson * Implemented by calling the acl_check routine in acl_support, which
4974191Srwatson * requires ordering.  We call acl_support's _posix1e_acl_sort to make this
5056760Srwatson * true.  POSIX.1e allows acl_valid() to reorder the ACL as it sees fit.
5156055Srwatson *
5256055Srwatson * This call is deprecated, as it doesn't ask whether the ACL is valid
5356760Srwatson * for a particular target.  However, this call is standardized, unlike
5456760Srwatson * the other two forms.
5556055Srwatson */
5656055Srwatsonint
5756055Srwatsonacl_valid(acl_t acl)
5856055Srwatson{
5956055Srwatson	int	error;
6056055Srwatson
6191033Sjedgar	if (acl == NULL) {
6291033Sjedgar		errno = EINVAL;
6391033Sjedgar		return (-1);
6491033Sjedgar	}
65194955Strasz	if (!_acl_brand_may_be(acl, ACL_BRAND_POSIX)) {
66194955Strasz		errno = EINVAL;
67194955Strasz		return (-1);
68194955Strasz	}
6974191Srwatson	_posix1e_acl_sort(acl);
7074191Srwatson	error = _posix1e_acl_check(acl);
7156055Srwatson	if (error) {
7256055Srwatson		errno = error;
7356055Srwatson		return (-1);
7456055Srwatson	} else {
7556055Srwatson		return (0);
7656055Srwatson	}
7756055Srwatson}
7856055Srwatson
7956055Srwatsonint
8056760Srwatsonacl_valid_file_np(const char *pathp, acl_type_t type, acl_t acl)
8156055Srwatson{
8256055Srwatson
8391033Sjedgar	if (pathp == NULL || acl == NULL) {
8491033Sjedgar		errno = EINVAL;
8591033Sjedgar		return (-1);
8691033Sjedgar	}
87192586Strasz	type = _acl_type_unold(type);
88208785Strasz	if (_posix1e_acl(acl, type))
89208785Strasz		_posix1e_acl_sort(acl);
9056055Srwatson
9175928Sjedgar	return (__acl_aclcheck_file(pathp, type, &acl->ats_acl));
9256055Srwatson}
9356055Srwatson
94108410Srwatsonint
95108410Srwatsonacl_valid_link_np(const char *pathp, acl_type_t type, acl_t acl)
96108410Srwatson{
9756055Srwatson
98108410Srwatson	if (pathp == NULL || acl == NULL) {
99108410Srwatson		errno = EINVAL;
100108410Srwatson		return (-1);
101108410Srwatson	}
102192586Strasz	type = _acl_type_unold(type);
103208785Strasz	if (_posix1e_acl(acl, type))
104208785Strasz		_posix1e_acl_sort(acl);
105108410Srwatson
106108410Srwatson	return (__acl_aclcheck_link(pathp, type, &acl->ats_acl));
107108410Srwatson}
108108410Srwatson
10956055Srwatsonint
11056760Srwatsonacl_valid_fd_np(int fd, acl_type_t type, acl_t acl)
11156055Srwatson{
11256055Srwatson
11391033Sjedgar	if (acl == NULL) {
11491033Sjedgar		errno = EINVAL;
11591033Sjedgar		return (-1);
11691033Sjedgar	}
117192586Strasz	type = _acl_type_unold(type);
118208785Strasz	if (_posix1e_acl(acl, type))
119208785Strasz		_posix1e_acl_sort(acl);
12056055Srwatson
12175928Sjedgar	acl->ats_cur_entry = 0;
12275928Sjedgar
12375928Sjedgar	return (___acl_aclcheck_fd(fd, type, &acl->ats_acl));
12456055Srwatson}
125