acl_valid.c revision 194955
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: head/lib/libc/posix1e/acl_valid.c 194955 2009-06-25 12:46:59Z trasz $");
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	int	error;
8356055Srwatson
8491033Sjedgar	if (pathp == NULL || acl == NULL) {
8591033Sjedgar		errno = EINVAL;
8691033Sjedgar		return (-1);
8791033Sjedgar	}
88192586Strasz	type = _acl_type_unold(type);
8974191Srwatson	if (_posix1e_acl(acl, type)) {
9074191Srwatson		error = _posix1e_acl_sort(acl);
9156055Srwatson		if (error) {
9256055Srwatson			errno = error;
9356055Srwatson			return (-1);
9456055Srwatson		}
9556055Srwatson	}
9656055Srwatson
9775928Sjedgar	return (__acl_aclcheck_file(pathp, type, &acl->ats_acl));
9856055Srwatson}
9956055Srwatson
100108410Srwatsonint
101108410Srwatsonacl_valid_link_np(const char *pathp, acl_type_t type, acl_t acl)
102108410Srwatson{
103108410Srwatson	int	error;
10456055Srwatson
105108410Srwatson	if (pathp == NULL || acl == NULL) {
106108410Srwatson		errno = EINVAL;
107108410Srwatson		return (-1);
108108410Srwatson	}
109192586Strasz	type = _acl_type_unold(type);
110108410Srwatson	if (_posix1e_acl(acl, type)) {
111108410Srwatson		error = _posix1e_acl_sort(acl);
112108410Srwatson		if (error) {
113108410Srwatson			errno = error;
114108410Srwatson			return (-1);
115108410Srwatson		}
116108410Srwatson	}
117108410Srwatson
118108410Srwatson	return (__acl_aclcheck_link(pathp, type, &acl->ats_acl));
119108410Srwatson}
120108410Srwatson
12156055Srwatsonint
12256760Srwatsonacl_valid_fd_np(int fd, acl_type_t type, acl_t acl)
12356055Srwatson{
12456055Srwatson	int	error;
12556055Srwatson
12691033Sjedgar	if (acl == NULL) {
12791033Sjedgar		errno = EINVAL;
12891033Sjedgar		return (-1);
12991033Sjedgar	}
130192586Strasz	type = _acl_type_unold(type);
13174191Srwatson	if (_posix1e_acl(acl, type)) {
13274191Srwatson		error = _posix1e_acl_sort(acl);
13356055Srwatson		if (error) {
13456055Srwatson			errno = error;
13556055Srwatson			return (-1);
13656055Srwatson		}
13756055Srwatson	}
13856055Srwatson
13975928Sjedgar	acl->ats_cur_entry = 0;
14075928Sjedgar
14175928Sjedgar	return (___acl_aclcheck_fd(fd, type, &acl->ats_acl));
14256055Srwatson}
143