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
8 Copyright (C) Andrew Bartlett 2010
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libcli/security/security.h"
27 /* Map generic access rights to object specific rights. This technique is
28 used to give meaning to assigning read, write, execute and all access to
29 objects. Each type of object has its own mapping of generic to object
30 specific access rights. */
32 void se_map_generic(uint32_t *access_mask, const struct generic_mapping *mapping)
34 uint32_t old_mask = *access_mask;
36 if (*access_mask & GENERIC_READ_ACCESS) {
37 *access_mask &= ~GENERIC_READ_ACCESS;
38 *access_mask |= mapping->generic_read;
41 if (*access_mask & GENERIC_WRITE_ACCESS) {
42 *access_mask &= ~GENERIC_WRITE_ACCESS;
43 *access_mask |= mapping->generic_write;
46 if (*access_mask & GENERIC_EXECUTE_ACCESS) {
47 *access_mask &= ~GENERIC_EXECUTE_ACCESS;
48 *access_mask |= mapping->generic_execute;
51 if (*access_mask & GENERIC_ALL_ACCESS) {
52 *access_mask &= ~GENERIC_ALL_ACCESS;
53 *access_mask |= mapping->generic_all;
56 if (old_mask != *access_mask) {
57 DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
58 old_mask, *access_mask));
62 /* Map generic access rights to object specific rights for all the ACE's
66 void security_acl_map_generic(struct security_acl *sa,
67 const struct generic_mapping *mapping)
75 for (i = 0; i < sa->num_aces; i++) {
76 se_map_generic(&sa->aces[i].access_mask, mapping);
80 /* Map standard access rights to object specific rights. This technique is
81 used to give meaning to assigning read, write, execute and all access to
82 objects. Each type of object has its own mapping of standard to object
83 specific access rights. */
85 void se_map_standard(uint32_t *access_mask, const struct standard_mapping *mapping)
87 uint32_t old_mask = *access_mask;
89 if (*access_mask & SEC_STD_READ_CONTROL) {
90 *access_mask &= ~SEC_STD_READ_CONTROL;
91 *access_mask |= mapping->std_read;
94 if (*access_mask & (SEC_STD_DELETE|SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|SEC_STD_SYNCHRONIZE)) {
95 *access_mask &= ~(SEC_STD_DELETE|SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|SEC_STD_SYNCHRONIZE);
96 *access_mask |= mapping->std_all;
99 if (old_mask != *access_mask) {
100 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
101 old_mask, *access_mask));
106 perform a SEC_FLAG_MAXIMUM_ALLOWED access check
108 static uint32_t access_check_max_allowed(const struct security_descriptor *sd,
109 const struct security_token *token)
111 uint32_t denied = 0, granted = 0;
114 if (security_token_has_sid(token, sd->owner_sid)) {
115 granted |= SEC_STD_WRITE_DAC | SEC_STD_READ_CONTROL | SEC_STD_DELETE;
116 } else if (security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
117 granted |= SEC_STD_DELETE;
120 if (sd->dacl == NULL) {
121 return granted & ~denied;
124 for (i = 0;i<sd->dacl->num_aces; i++) {
125 struct security_ace *ace = &sd->dacl->aces[i];
127 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
131 if (!security_token_has_sid(token, &ace->trustee)) {
136 case SEC_ACE_TYPE_ACCESS_ALLOWED:
137 granted |= ace->access_mask;
139 case SEC_ACE_TYPE_ACCESS_DENIED:
140 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
141 denied |= ace->access_mask;
143 default: /* Other ACE types not handled/supported */
148 return granted & ~denied;
152 The main entry point for access checking. If returning ACCESS_DENIED
153 this function returns the denied bits in the uint32_t pointed
154 to by the access_granted pointer.
156 NTSTATUS se_access_check(const struct security_descriptor *sd,
157 const struct security_token *token,
158 uint32_t access_desired,
159 uint32_t *access_granted)
162 uint32_t bits_remaining;
164 *access_granted = access_desired;
165 bits_remaining = access_desired;
167 /* handle the maximum allowed flag */
168 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
169 uint32_t orig_access_desired = access_desired;
171 access_desired |= access_check_max_allowed(sd, token);
172 access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
173 *access_granted = access_desired;
174 bits_remaining = access_desired & ~SEC_STD_DELETE;
176 DEBUG(10,("se_access_check: MAX desired = 0x%x, granted = 0x%x, remaining = 0x%x\n",
182 if (access_desired & SEC_FLAG_SYSTEM_SECURITY) {
183 if (security_token_has_privilege(token, SEC_PRIV_SECURITY)) {
184 bits_remaining &= ~SEC_FLAG_SYSTEM_SECURITY;
186 return NT_STATUS_PRIVILEGE_NOT_HELD;
190 /* a NULL dacl allows access */
191 if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl == NULL) {
192 *access_granted = access_desired;
196 /* the owner always gets SEC_STD_WRITE_DAC, SEC_STD_READ_CONTROL and SEC_STD_DELETE */
197 if ((bits_remaining & (SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL|SEC_STD_DELETE)) &&
198 security_token_has_sid(token, sd->owner_sid)) {
199 bits_remaining &= ~(SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL|SEC_STD_DELETE);
201 if ((bits_remaining & SEC_STD_DELETE) &&
202 (security_token_has_privilege(token, SEC_PRIV_RESTORE))) {
203 bits_remaining &= ~SEC_STD_DELETE;
205 if ((bits_remaining & SEC_RIGHTS_PRIV_RESTORE) &&
206 security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
207 bits_remaining &= ~(SEC_RIGHTS_PRIV_RESTORE);
209 if ((bits_remaining & SEC_RIGHTS_PRIV_BACKUP) &&
210 security_token_has_privilege(token, SEC_PRIV_BACKUP)) {
211 bits_remaining &= ~(SEC_RIGHTS_PRIV_BACKUP);
214 if (sd->dacl == NULL) {
218 /* check each ace in turn. */
219 for (i=0; bits_remaining && i < sd->dacl->num_aces; i++) {
220 struct security_ace *ace = &sd->dacl->aces[i];
222 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
226 if (!security_token_has_sid(token, &ace->trustee)) {
231 case SEC_ACE_TYPE_ACCESS_ALLOWED:
232 bits_remaining &= ~ace->access_mask;
234 case SEC_ACE_TYPE_ACCESS_DENIED:
235 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
236 if (bits_remaining & ace->access_mask) {
237 return NT_STATUS_ACCESS_DENIED;
240 default: /* Other ACE types not handled/supported */
246 if (bits_remaining != 0) {
247 *access_granted = bits_remaining;
248 return NT_STATUS_ACCESS_DENIED;