2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Gerald Carter 2005
6 Copyright (C) Volker Lendecke 2007
7 Copyright (C) Jeremy Allison 2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /* Map generic access rights to object specific rights. This technique is
26 used to give meaning to assigning read, write, execute and all access to
27 objects. Each type of object has its own mapping of generic to object
28 specific access rights. */
30 void se_map_generic(uint32 *access_mask, const struct generic_mapping *mapping)
32 uint32 old_mask = *access_mask;
34 if (*access_mask & GENERIC_READ_ACCESS) {
35 *access_mask &= ~GENERIC_READ_ACCESS;
36 *access_mask |= mapping->generic_read;
39 if (*access_mask & GENERIC_WRITE_ACCESS) {
40 *access_mask &= ~GENERIC_WRITE_ACCESS;
41 *access_mask |= mapping->generic_write;
44 if (*access_mask & GENERIC_EXECUTE_ACCESS) {
45 *access_mask &= ~GENERIC_EXECUTE_ACCESS;
46 *access_mask |= mapping->generic_execute;
49 if (*access_mask & GENERIC_ALL_ACCESS) {
50 *access_mask &= ~GENERIC_ALL_ACCESS;
51 *access_mask |= mapping->generic_all;
54 if (old_mask != *access_mask) {
55 DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
56 old_mask, *access_mask));
60 /* Map generic access rights to object specific rights for all the ACE's
64 void security_acl_map_generic(struct security_acl *sa,
65 const struct generic_mapping *mapping)
73 for (i = 0; i < sa->num_aces; i++) {
74 se_map_generic(&sa->aces[i].access_mask, mapping);
78 /* Map standard access rights to object specific rights. This technique is
79 used to give meaning to assigning read, write, execute and all access to
80 objects. Each type of object has its own mapping of standard to object
81 specific access rights. */
83 void se_map_standard(uint32 *access_mask, const struct standard_mapping *mapping)
85 uint32 old_mask = *access_mask;
87 if (*access_mask & SEC_STD_READ_CONTROL) {
88 *access_mask &= ~SEC_STD_READ_CONTROL;
89 *access_mask |= mapping->std_read;
92 if (*access_mask & (SEC_STD_DELETE|SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|SEC_STD_SYNCHRONIZE)) {
93 *access_mask &= ~(SEC_STD_DELETE|SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|SEC_STD_SYNCHRONIZE);
94 *access_mask |= mapping->std_all;
97 if (old_mask != *access_mask) {
98 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
99 old_mask, *access_mask));
104 perform a SEC_FLAG_MAXIMUM_ALLOWED access check
106 static uint32_t access_check_max_allowed(const struct security_descriptor *sd,
107 const struct security_token *token)
109 uint32_t denied = 0, granted = 0;
112 if (is_sid_in_token(token, sd->owner_sid)) {
113 granted |= SEC_STD_WRITE_DAC | SEC_STD_READ_CONTROL | SEC_STD_DELETE;
114 } else if (security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
115 granted |= SEC_STD_DELETE;
118 if (sd->dacl == NULL) {
119 return granted & ~denied;
122 for (i = 0;i<sd->dacl->num_aces; i++) {
123 struct security_ace *ace = &sd->dacl->aces[i];
125 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
129 if (!is_sid_in_token(token, &ace->trustee)) {
134 case SEC_ACE_TYPE_ACCESS_ALLOWED:
135 granted |= ace->access_mask;
137 case SEC_ACE_TYPE_ACCESS_DENIED:
138 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
139 denied |= ace->access_mask;
141 default: /* Other ACE types not handled/supported */
146 return granted & ~denied;
150 The main entry point for access checking. If returning ACCESS_DENIED
151 this function returns the denied bits in the uint32_t pointed
152 to by the access_granted pointer.
154 NTSTATUS se_access_check(const struct security_descriptor *sd,
155 const struct security_token *token,
156 uint32_t access_desired,
157 uint32_t *access_granted)
160 uint32_t bits_remaining;
162 *access_granted = access_desired;
163 bits_remaining = access_desired;
165 /* handle the maximum allowed flag */
166 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
167 uint32_t orig_access_desired = access_desired;
169 access_desired |= access_check_max_allowed(sd, token);
170 access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
171 *access_granted = access_desired;
172 bits_remaining = access_desired & ~SEC_STD_DELETE;
174 DEBUG(10,("se_access_check: MAX desired = 0x%x, granted = 0x%x, remaining = 0x%x\n",
181 /* We need to support SeSecurityPrivilege for this. */
183 if (access_desired & SEC_FLAG_SYSTEM_SECURITY) {
184 if (user_has_privileges(token, &sec_security)) {
185 bits_remaining &= ~SEC_FLAG_SYSTEM_SECURITY;
187 return NT_STATUS_PRIVILEGE_NOT_HELD;
192 /* a NULL dacl allows access */
193 if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl == NULL) {
194 *access_granted = access_desired;
198 /* the owner always gets SEC_STD_WRITE_DAC, SEC_STD_READ_CONTROL and SEC_STD_DELETE */
199 if ((bits_remaining & (SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL|SEC_STD_DELETE)) &&
200 is_sid_in_token(token, sd->owner_sid)) {
201 bits_remaining &= ~(SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL|SEC_STD_DELETE);
203 if ((bits_remaining & SEC_STD_DELETE) &&
204 (security_token_has_privilege(token, SEC_PRIV_RESTORE))) {
205 bits_remaining &= ~SEC_STD_DELETE;
208 if (sd->dacl == NULL) {
212 /* check each ace in turn. */
213 for (i=0; bits_remaining && i < sd->dacl->num_aces; i++) {
214 struct security_ace *ace = &sd->dacl->aces[i];
216 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
220 if (!is_sid_in_token(token, &ace->trustee)) {
225 case SEC_ACE_TYPE_ACCESS_ALLOWED:
226 bits_remaining &= ~ace->access_mask;
228 case SEC_ACE_TYPE_ACCESS_DENIED:
229 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
230 if (bits_remaining & ace->access_mask) {
231 return NT_STATUS_ACCESS_DENIED;
234 default: /* Other ACE types not handled/supported */
240 if (bits_remaining != 0) {
241 *access_granted = bits_remaining;
242 return NT_STATUS_ACCESS_DENIED;