acl_valid.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/*
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 192586 2009-05-22 15:56:43Z 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	}
6574191Srwatson	_posix1e_acl_sort(acl);
6674191Srwatson	error = _posix1e_acl_check(acl);
6756055Srwatson	if (error) {
6856055Srwatson		errno = error;
6956055Srwatson		return (-1);
7056055Srwatson	} else {
7156055Srwatson		return (0);
7256055Srwatson	}
7356055Srwatson}
7456055Srwatson
7556055Srwatsonint
7656760Srwatsonacl_valid_file_np(const char *pathp, acl_type_t type, acl_t acl)
7756055Srwatson{
7856055Srwatson	int	error;
7956055Srwatson
8091033Sjedgar	if (pathp == NULL || acl == NULL) {
8191033Sjedgar		errno = EINVAL;
8291033Sjedgar		return (-1);
8391033Sjedgar	}
84192586Strasz	type = _acl_type_unold(type);
8574191Srwatson	if (_posix1e_acl(acl, type)) {
8674191Srwatson		error = _posix1e_acl_sort(acl);
8756055Srwatson		if (error) {
8856055Srwatson			errno = error;
8956055Srwatson			return (-1);
9056055Srwatson		}
9156055Srwatson	}
9256055Srwatson
9375928Sjedgar	return (__acl_aclcheck_file(pathp, type, &acl->ats_acl));
9456055Srwatson}
9556055Srwatson
96108410Srwatsonint
97108410Srwatsonacl_valid_link_np(const char *pathp, acl_type_t type, acl_t acl)
98108410Srwatson{
99108410Srwatson	int	error;
10056055Srwatson
101108410Srwatson	if (pathp == NULL || acl == NULL) {
102108410Srwatson		errno = EINVAL;
103108410Srwatson		return (-1);
104108410Srwatson	}
105192586Strasz	type = _acl_type_unold(type);
106108410Srwatson	if (_posix1e_acl(acl, type)) {
107108410Srwatson		error = _posix1e_acl_sort(acl);
108108410Srwatson		if (error) {
109108410Srwatson			errno = error;
110108410Srwatson			return (-1);
111108410Srwatson		}
112108410Srwatson	}
113108410Srwatson
114108410Srwatson	return (__acl_aclcheck_link(pathp, type, &acl->ats_acl));
115108410Srwatson}
116108410Srwatson
11756055Srwatsonint
11856760Srwatsonacl_valid_fd_np(int fd, acl_type_t type, acl_t acl)
11956055Srwatson{
12056055Srwatson	int	error;
12156055Srwatson
12291033Sjedgar	if (acl == NULL) {
12391033Sjedgar		errno = EINVAL;
12491033Sjedgar		return (-1);
12591033Sjedgar	}
126192586Strasz	type = _acl_type_unold(type);
12774191Srwatson	if (_posix1e_acl(acl, type)) {
12874191Srwatson		error = _posix1e_acl_sort(acl);
12956055Srwatson		if (error) {
13056055Srwatson			errno = error;
13156055Srwatson			return (-1);
13256055Srwatson		}
13356055Srwatson	}
13456055Srwatson
13575928Sjedgar	acl->ats_cur_entry = 0;
13675928Sjedgar
13775928Sjedgar	return (___acl_aclcheck_fd(fd, type, &acl->ats_acl));
13856055Srwatson}
139