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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "libcli/security/security.h"
27 perform a SEC_FLAG_MAXIMUM_ALLOWED access check
29 static uint32_t access_check_max_allowed(const struct security_descriptor
*sd
,
30 const struct security_token
*token
)
32 uint32_t denied
= 0, granted
= 0;
35 if (security_token_has_sid(token
, sd
->owner_sid
)) {
36 granted
|= SEC_STD_WRITE_DAC
| SEC_STD_READ_CONTROL
| SEC_STD_DELETE
;
37 } else if (security_token_has_privilege(token
, SEC_PRIV_RESTORE
)) {
38 granted
|= SEC_STD_DELETE
;
41 if (sd
->dacl
== NULL
) {
42 return granted
& ~denied
;
45 for (i
= 0;i
<sd
->dacl
->num_aces
; i
++) {
46 struct security_ace
*ace
= &sd
->dacl
->aces
[i
];
48 if (ace
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
) {
52 if (!security_token_has_sid(token
, &ace
->trustee
)) {
57 case SEC_ACE_TYPE_ACCESS_ALLOWED
:
58 granted
|= ace
->access_mask
;
60 case SEC_ACE_TYPE_ACCESS_DENIED
:
61 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT
:
62 denied
|= ace
->access_mask
;
64 default: /* Other ACE types not handled/supported */
69 return granted
& ~denied
;
73 the main entry point for access checking.
75 NTSTATUS
sec_access_check(const struct security_descriptor
*sd
,
76 const struct security_token
*token
,
77 uint32_t access_desired
,
78 uint32_t *access_granted
)
81 uint32_t bits_remaining
;
83 *access_granted
= access_desired
;
84 bits_remaining
= access_desired
;
86 /* handle the maximum allowed flag */
87 if (access_desired
& SEC_FLAG_MAXIMUM_ALLOWED
) {
88 access_desired
|= access_check_max_allowed(sd
, token
);
89 access_desired
&= ~SEC_FLAG_MAXIMUM_ALLOWED
;
90 *access_granted
= access_desired
;
91 bits_remaining
= access_desired
& ~SEC_STD_DELETE
;
94 if (access_desired
& SEC_FLAG_SYSTEM_SECURITY
) {
95 if (security_token_has_privilege(token
, SEC_PRIV_SECURITY
)) {
96 bits_remaining
&= ~SEC_FLAG_SYSTEM_SECURITY
;
98 return NT_STATUS_PRIVILEGE_NOT_HELD
;
102 /* a NULL dacl allows access */
103 if ((sd
->type
& SEC_DESC_DACL_PRESENT
) && sd
->dacl
== NULL
) {
104 *access_granted
= access_desired
;
108 /* the owner always gets SEC_STD_WRITE_DAC, SEC_STD_READ_CONTROL and SEC_STD_DELETE */
109 if ((bits_remaining
& (SEC_STD_WRITE_DAC
|SEC_STD_READ_CONTROL
|SEC_STD_DELETE
)) &&
110 security_token_has_sid(token
, sd
->owner_sid
)) {
111 bits_remaining
&= ~(SEC_STD_WRITE_DAC
|SEC_STD_READ_CONTROL
|SEC_STD_DELETE
);
113 if ((bits_remaining
& SEC_STD_DELETE
) &&
114 security_token_has_privilege(token
, SEC_PRIV_RESTORE
)) {
115 bits_remaining
&= ~SEC_STD_DELETE
;
118 if (sd
->dacl
== NULL
) {
122 /* check each ace in turn. */
123 for (i
=0; bits_remaining
&& i
< sd
->dacl
->num_aces
; i
++) {
124 struct security_ace
*ace
= &sd
->dacl
->aces
[i
];
126 if (ace
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
) {
130 if (!security_token_has_sid(token
, &ace
->trustee
)) {
135 case SEC_ACE_TYPE_ACCESS_ALLOWED
:
136 bits_remaining
&= ~ace
->access_mask
;
138 case SEC_ACE_TYPE_ACCESS_DENIED
:
139 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT
:
140 if (bits_remaining
& ace
->access_mask
) {
141 return NT_STATUS_ACCESS_DENIED
;
144 default: /* Other ACE types not handled/supported */
150 if (bits_remaining
!= 0) {
151 return NT_STATUS_ACCESS_DENIED
;