acl_valid.c revision 92986
156055Srwatson/*-
274191Srwatson * Copyright (c) 1999, 2000, 20001 Robert N. M. Watson
356055Srwatson * All rights reserved.
456055Srwatson *
556055Srwatson * Redistribution and use in source and binary forms, with or without
656055Srwatson * modification, are permitted provided that the following conditions
756055Srwatson * are met:
856055Srwatson * 1. Redistributions of source code must retain the above copyright
956055Srwatson *    notice, this list of conditions and the following disclaimer.
1056055Srwatson * 2. Redistributions in binary form must reproduce the above copyright
1156055Srwatson *    notice, this list of conditions and the following disclaimer in the
1256055Srwatson *    documentation and/or other materials provided with the distribution.
1356055Srwatson *
1456055Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1556055Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1656055Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1756055Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1856055Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1956055Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2056055Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2156055Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2256055Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2356055Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2456055Srwatson * SUCH DAMAGE.
2556055Srwatson */
2656055Srwatson/*
2756055Srwatson * acl_valid -- POSIX.1e ACL check routine
2856055Srwatson */
2956055Srwatson
3092986Sobrien#include <sys/cdefs.h>
3192986Sobrien__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_valid.c 92986 2002-03-22 21:53:29Z obrien $");
3292986Sobrien
3356055Srwatson#include <sys/types.h>
3475185Stmm#include "namespace.h"
3556055Srwatson#include <sys/acl.h>
3675185Stmm#include "un-namespace.h"
3756055Srwatson#include <sys/errno.h>
3891033Sjedgar#include <stdlib.h>
3956055Srwatson
4056055Srwatson#include "acl_support.h"
4156055Srwatson
4256055Srwatson/*
4356055Srwatson * acl_valid: accepts an ACL, returns 0 on valid ACL, -1 for invalid,
4456055Srwatson * and errno set to EINVAL.
4556055Srwatson *
4656055Srwatson * Implemented by calling the acl_check routine in acl_support, which
4774191Srwatson * requires ordering.  We call acl_support's _posix1e_acl_sort to make this
4856760Srwatson * true.  POSIX.1e allows acl_valid() to reorder the ACL as it sees fit.
4956055Srwatson *
5056055Srwatson * This call is deprecated, as it doesn't ask whether the ACL is valid
5156760Srwatson * for a particular target.  However, this call is standardized, unlike
5256760Srwatson * the other two forms.
5356055Srwatson */
5456055Srwatsonint
5556055Srwatsonacl_valid(acl_t acl)
5656055Srwatson{
5756055Srwatson	int	error;
5856055Srwatson
5991033Sjedgar	if (acl == NULL) {
6091033Sjedgar		errno = EINVAL;
6191033Sjedgar		return (-1);
6291033Sjedgar	}
6374191Srwatson	_posix1e_acl_sort(acl);
6474191Srwatson	error = _posix1e_acl_check(acl);
6556055Srwatson	if (error) {
6656055Srwatson		errno = error;
6756055Srwatson		return (-1);
6856055Srwatson	} else {
6956055Srwatson		return (0);
7056055Srwatson	}
7156055Srwatson}
7256055Srwatson
7356055Srwatson
7456055Srwatsonint
7556760Srwatsonacl_valid_file_np(const char *pathp, acl_type_t type, acl_t acl)
7656055Srwatson{
7756055Srwatson	int	error;
7856055Srwatson
7991033Sjedgar	if (pathp == NULL || acl == NULL) {
8091033Sjedgar		errno = EINVAL;
8191033Sjedgar		return (-1);
8291033Sjedgar	}
8374191Srwatson	if (_posix1e_acl(acl, type)) {
8474191Srwatson		error = _posix1e_acl_sort(acl);
8556055Srwatson		if (error) {
8656055Srwatson			errno = error;
8756055Srwatson			return (-1);
8856055Srwatson		}
8956055Srwatson	}
9056055Srwatson
9175928Sjedgar	return (__acl_aclcheck_file(pathp, type, &acl->ats_acl));
9256055Srwatson}
9356055Srwatson
9456055Srwatson
9556055Srwatsonint
9656760Srwatsonacl_valid_fd_np(int fd, acl_type_t type, acl_t acl)
9756055Srwatson{
9856055Srwatson	int	error;
9956055Srwatson
10091033Sjedgar	if (acl == NULL) {
10191033Sjedgar		errno = EINVAL;
10291033Sjedgar		return (-1);
10391033Sjedgar	}
10474191Srwatson	if (_posix1e_acl(acl, type)) {
10574191Srwatson		error = _posix1e_acl_sort(acl);
10656055Srwatson		if (error) {
10756055Srwatson			errno = error;
10856055Srwatson			return (-1);
10956055Srwatson		}
11056055Srwatson	}
11156055Srwatson
11275928Sjedgar	acl->ats_cur_entry = 0;
11375928Sjedgar
11475928Sjedgar
11575928Sjedgar	return (___acl_aclcheck_fd(fd, type, &acl->ats_acl));
11656055Srwatson}
117