xref: /freebsd/lib/libc/posix1e/acl_delete_entry.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*
2  * Copyright (c) 2001-2002 Chris D. Faulhaber
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include "namespace.h"
32 #include <sys/acl.h>
33 #include "un-namespace.h"
34 #include <errno.h>
35 #include <string.h>
36 #include <stdio.h>
37 
38 #include "acl_support.h"
39 
40 static int
41 _entry_matches(const acl_entry_t a, const acl_entry_t b)
42 {
43 	/*
44 	 * There is a semantical difference here between NFSv4 and POSIX
45 	 * draft ACLs.  In POSIX, there may be only one entry for the particular
46 	 * user or group.  In NFSv4 ACL, there may be any number of them.  We're
47 	 * trying to be more specific here in that case.
48 	 */
49 	switch (_entry_brand(a)) {
50 	case ACL_BRAND_NFS4:
51 		if (a->ae_tag != b->ae_tag || a->ae_entry_type != b->ae_entry_type)
52 			return (0);
53 
54 		/* If ae_ids matter, compare them as well. */
55 		if (a->ae_tag == ACL_USER || a->ae_tag == ACL_GROUP) {
56 			if (a->ae_id != b->ae_id)
57 				return (0);
58 		}
59 
60 		return (1);
61 
62 	default:
63 		if ((a->ae_tag == b->ae_tag) && (a->ae_id == b->ae_id))
64 			return (1);
65 	}
66 
67 	return (0);
68 }
69 
70 /*
71  * acl_delete_entry() (23.4.9): remove the ACL entry indicated by entry_d
72  * from acl.
73  */
74 int
75 acl_delete_entry(acl_t acl, acl_entry_t entry_d)
76 {
77 	struct acl *acl_int;
78 	int i, j, found = 0;
79 
80 	if (acl == NULL || entry_d == NULL) {
81 		errno = EINVAL;
82 		return (-1);
83 	}
84 
85 	acl_int = &acl->ats_acl;
86 
87 	if (_entry_brand(entry_d) != _acl_brand(acl)) {
88 		errno = EINVAL;
89 		return (-1);
90 	}
91 
92 	if ((acl->ats_acl.acl_cnt < 1) ||
93 	    (acl->ats_acl.acl_cnt > ACL_MAX_ENTRIES)) {
94 		errno = EINVAL;
95 		return (-1);
96 	}
97 	for (i = 0; i < acl->ats_acl.acl_cnt;) {
98 		if (_entry_matches(&(acl->ats_acl.acl_entry[i]), entry_d)) {
99 			/* ...shift the remaining entries... */
100 			for (j = i; j < acl->ats_acl.acl_cnt - 1; ++j)
101 				acl->ats_acl.acl_entry[j] =
102 				    acl->ats_acl.acl_entry[j+1];
103 			/* ...drop the count and zero the unused entry... */
104 			acl->ats_acl.acl_cnt--;
105 			bzero(&acl->ats_acl.acl_entry[j],
106 			    sizeof(struct acl_entry));
107 			acl->ats_cur_entry = 0;
108 
109 			/* Continue with the loop to remove all maching entries. */
110 			found = 1;
111 		} else
112 			i++;
113 	}
114 
115 	if (found)
116 		return (0);
117 
118 	errno = EINVAL;
119 	return (-1);
120 }
121 
122 int
123 acl_delete_entry_np(acl_t acl, int offset)
124 {
125 	struct acl *acl_int;
126 	int i;
127 
128 	if (acl == NULL) {
129 		errno = EINVAL;
130 		return (-1);
131 	}
132 
133 	acl_int = &acl->ats_acl;
134 
135 	if (offset < 0 || offset >= acl_int->acl_cnt) {
136 		errno = EINVAL;
137 		return (-1);
138 	}
139 
140 	if ((acl->ats_acl.acl_cnt < 1) ||
141 	    (acl->ats_acl.acl_cnt > ACL_MAX_ENTRIES)) {
142 		errno = EINVAL;
143 		return (-1);
144 	}
145 
146 	/* ...shift the remaining entries... */
147 	for (i = offset; i < acl->ats_acl.acl_cnt - 1; ++i)
148 		acl->ats_acl.acl_entry[i] =
149 		    acl->ats_acl.acl_entry[i+1];
150 	/* ...drop the count and zero the unused entry... */
151 	acl->ats_acl.acl_cnt--;
152 	bzero(&acl->ats_acl.acl_entry[i],
153 	    sizeof(struct acl_entry));
154 	acl->ats_cur_entry = 0;
155 
156 	return (0);
157 }
158