2 Unix SMB/CIFS implementation.
4 security access checking routines
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/security/security.h"
28 perform a SEC_FLAG_MAXIMUM_ALLOWED access check
30 static uint32_t access_check_max_allowed(const struct security_descriptor *sd,
31 const struct security_token *token)
33 uint32_t denied = 0, granted = 0;
36 if (security_token_has_sid(token, sd->owner_sid)) {
37 granted |= SEC_STD_WRITE_DAC | SEC_STD_READ_CONTROL | SEC_STD_DELETE;
38 } else if (security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
39 granted |= SEC_STD_DELETE;
42 for (i = 0;i<sd->dacl->num_aces; i++) {
43 struct security_ace *ace = &sd->dacl->aces[i];
45 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
49 if (!security_token_has_sid(token, &ace->trustee)) {
54 case SEC_ACE_TYPE_ACCESS_ALLOWED:
55 granted |= ace->access_mask;
57 case SEC_ACE_TYPE_ACCESS_DENIED:
58 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
59 denied |= ace->access_mask;
61 default: /* Other ACE types not handled/supported */
66 return granted & ~denied;
70 the main entry point for access checking.
72 NTSTATUS sec_access_check(const struct security_descriptor *sd,
73 const struct security_token *token,
74 uint32_t access_desired,
75 uint32_t *access_granted)
78 uint32_t bits_remaining;
80 *access_granted = access_desired;
81 bits_remaining = access_desired;
83 /* handle the maximum allowed flag */
84 if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
85 access_desired |= access_check_max_allowed(sd, token);
86 access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
87 *access_granted = access_desired;
88 bits_remaining = access_desired & ~SEC_STD_DELETE;
91 if (access_desired & SEC_FLAG_SYSTEM_SECURITY) {
92 if (security_token_has_privilege(token, SEC_PRIV_SECURITY)) {
93 bits_remaining &= ~SEC_FLAG_SYSTEM_SECURITY;
95 return NT_STATUS_PRIVILEGE_NOT_HELD;
99 /* dacl not present allows access */
100 if (!(sd->type & SEC_DESC_DACL_PRESENT)) {
101 *access_granted = access_desired;
105 /* empty dacl denies access */
106 if (sd->dacl == NULL || sd->dacl->num_aces == 0) {
107 return NT_STATUS_ACCESS_DENIED;
110 /* the owner always gets SEC_STD_WRITE_DAC, SEC_STD_READ_CONTROL and SEC_STD_DELETE */
111 if ((bits_remaining & (SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL|SEC_STD_DELETE)) &&
112 security_token_has_sid(token, sd->owner_sid)) {
113 bits_remaining &= ~(SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL|SEC_STD_DELETE);
115 if ((bits_remaining & SEC_STD_DELETE) &&
116 security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
117 bits_remaining &= ~SEC_STD_DELETE;
120 /* check each ace in turn. */
121 for (i=0; bits_remaining && i < sd->dacl->num_aces; i++) {
122 struct security_ace *ace = &sd->dacl->aces[i];
124 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
128 if (!security_token_has_sid(token, &ace->trustee)) {
133 case SEC_ACE_TYPE_ACCESS_ALLOWED:
134 bits_remaining &= ~ace->access_mask;
136 case SEC_ACE_TYPE_ACCESS_DENIED:
137 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
138 if (bits_remaining & ace->access_mask) {
139 return NT_STATUS_ACCESS_DENIED;
142 default: /* Other ACE types not handled/supported */
147 if (bits_remaining != 0) {
148 return NT_STATUS_ACCESS_DENIED;