174667Sjedgar/*
290781Sjedgar * Copyright (c) 2001-2002 Chris D. Faulhaber
374667Sjedgar * All rights reserved.
474667Sjedgar *
574667Sjedgar * Redistribution and use in source and binary forms, with or without
674667Sjedgar * modification, are permitted provided that the following conditions
774667Sjedgar * are met:
874667Sjedgar * 1. Redistributions of source code must retain the above copyright
974667Sjedgar *    notice, this list of conditions and the following disclaimer.
1074667Sjedgar * 2. Redistributions in binary form must reproduce the above copyright
1174667Sjedgar *    notice, this list of conditions and the following disclaimer in the
1274667Sjedgar *    documentation and/or other materials provided with the distribution.
1374667Sjedgar *
1474667Sjedgar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1574667Sjedgar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1674667Sjedgar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184607Simp * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18184607Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184607Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184607Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184607Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184607Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184607Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184607Simp * SUCH DAMAGE.
2574667Sjedgar */
2674667Sjedgar
2792986Sobrien#include <sys/cdefs.h>
2892986Sobrien__FBSDID("$FreeBSD$");
2992986Sobrien
3074667Sjedgar#include <sys/types.h>
3175185Stmm#include "namespace.h"
3274667Sjedgar#include <sys/acl.h>
3375185Stmm#include "un-namespace.h"
3474667Sjedgar
3574667Sjedgar#include <errno.h>
3674667Sjedgar#include <string.h>
3774667Sjedgar
38208033Straszstatic int
39208033Strasz_perm_is_invalid(acl_perm_t perm)
40208033Strasz{
41208033Strasz
42208033Strasz	/* Check if more than a single bit is set. */
43208033Strasz	if ((perm & -perm) == perm &&
44208033Strasz	    (perm & (ACL_POSIX1E_BITS | ACL_NFS4_PERM_BITS)) == perm)
45208033Strasz		return (0);
46208033Strasz
47208033Strasz	errno = EINVAL;
48208033Strasz
49208033Strasz	return (1);
50208033Strasz}
51208033Strasz
5274667Sjedgar/*
5375928Sjedgar * acl_add_perm() (23.4.1): add the permission contained in perm to the
5474667Sjedgar * permission set permset_d
5574667Sjedgar */
5674667Sjedgarint
5774667Sjedgaracl_add_perm(acl_permset_t permset_d, acl_perm_t perm)
5874667Sjedgar{
5974667Sjedgar
60208033Strasz	if (permset_d == NULL) {
61208033Strasz		errno = EINVAL;
62208033Strasz		return (-1);
6374667Sjedgar	}
6474667Sjedgar
65208033Strasz	if (_perm_is_invalid(perm))
66208033Strasz		return (-1);
67208033Strasz
68208033Strasz	*permset_d |= perm;
69208033Strasz
70208033Strasz	return (0);
7174667Sjedgar}
7274667Sjedgar
7374667Sjedgar/*
7475928Sjedgar * acl_clear_perms() (23.4.3): clear all permisions from the permission
7574667Sjedgar * set permset_d
7674667Sjedgar */
7774667Sjedgarint
7874973Sjedgaracl_clear_perms(acl_permset_t permset_d)
7974667Sjedgar{
8074667Sjedgar
8190781Sjedgar	if (permset_d == NULL) {
8274667Sjedgar		errno = EINVAL;
8390781Sjedgar		return (-1);
8474667Sjedgar	}
8574667Sjedgar
8675928Sjedgar	*permset_d = ACL_PERM_NONE;
8774667Sjedgar
8890781Sjedgar	return (0);
8974667Sjedgar}
9074667Sjedgar
9174667Sjedgar/*
9275928Sjedgar * acl_delete_perm() (23.4.10): remove the permission in perm from the
9374667Sjedgar * permission set permset_d
9474667Sjedgar */
9574667Sjedgarint
9674667Sjedgaracl_delete_perm(acl_permset_t permset_d, acl_perm_t perm)
9774667Sjedgar{
9874667Sjedgar
99208033Strasz	if (permset_d == NULL) {
100208033Strasz		errno = EINVAL;
101208033Strasz		return (-1);
10274667Sjedgar	}
10374667Sjedgar
104208033Strasz	if (_perm_is_invalid(perm))
105208033Strasz		return (-1);
106208033Strasz
107208033Strasz	*permset_d &= ~perm;
108208033Strasz
109208033Strasz	return (0);
11074667Sjedgar}
111208437Strasz
112208437Straszint
113208437Straszacl_get_perm_np(acl_permset_t permset_d, acl_perm_t perm)
114208437Strasz{
115208437Strasz
116208437Strasz	if (permset_d == NULL) {
117208437Strasz		errno = EINVAL;
118208437Strasz		return (-1);
119208437Strasz	}
120208437Strasz
121208437Strasz	if (_perm_is_invalid(perm))
122208437Strasz		return (-1);
123208437Strasz
124208437Strasz	if (*permset_d & perm)
125208437Strasz		return (1);
126208437Strasz
127208437Strasz	return (0);
128208437Strasz}
129