s3: fix build without ads support
[nivanova/samba-autobuild/.git] / libgpo / gpo_sec.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Object Support
4  *  Copyright (C) Guenther Deschner 2007
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "libcli/security/security.h"
22 #include "../libgpo/gpo.h"
23 #include "auth.h"
24 #include "../librpc/ndr/libndr.h"
25
26 /****************************************************************
27 ****************************************************************/
28
29 static bool gpo_sd_check_agp_object_guid(const struct security_ace_object *object)
30 {
31         struct GUID ext_right_apg_guid;
32         NTSTATUS status;
33
34         if (!object) {
35                 return false;
36         }
37
38         status = GUID_from_string(ADS_EXTENDED_RIGHT_APPLY_GROUP_POLICY,
39                                   &ext_right_apg_guid);
40         if (!NT_STATUS_IS_OK(status)) {
41                 return false;
42         }
43
44         switch (object->flags) {
45                 case SEC_ACE_OBJECT_TYPE_PRESENT:
46                         if (GUID_equal(&object->type.type,
47                                        &ext_right_apg_guid)) {
48                                 return true;
49                         }
50                 case SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT:
51                         if (GUID_equal(&object->inherited_type.inherited_type,
52                                        &ext_right_apg_guid)) {
53                                 return true;
54                         }
55                 default:
56                         break;
57         }
58
59         return false;
60 }
61
62 /****************************************************************
63 ****************************************************************/
64
65 static bool gpo_sd_check_agp_object(const struct security_ace *ace)
66 {
67         if (!sec_ace_object(ace->type)) {
68                 return false;
69         }
70
71         return gpo_sd_check_agp_object_guid(&ace->object.object);
72 }
73
74 /****************************************************************
75 ****************************************************************/
76
77 static bool gpo_sd_check_agp_access_bits(uint32_t access_mask)
78 {
79         return (access_mask & SEC_ADS_CONTROL_ACCESS);
80 }
81
82 #if 0
83 /****************************************************************
84 ****************************************************************/
85
86 static bool gpo_sd_check_read_access_bits(uint32_t access_mask)
87 {
88         uint32_t read_bits = SEC_RIGHTS_LIST_CONTENTS |
89                            SEC_RIGHTS_READ_ALL_PROP |
90                            SEC_RIGHTS_READ_PERMS;
91
92         return (read_bits == (access_mask & read_bits));
93 }
94 #endif
95
96 /****************************************************************
97 ****************************************************************/
98
99 static NTSTATUS gpo_sd_check_ace_denied_object(const struct security_ace *ace,
100                                                const struct security_token *token)
101 {
102         char *sid_str;
103
104         if (gpo_sd_check_agp_object(ace) &&
105             gpo_sd_check_agp_access_bits(ace->access_mask) &&
106             security_token_has_sid(token, &ace->trustee)) {
107                 sid_str = dom_sid_string(NULL, &ace->trustee);
108                 DEBUG(10,("gpo_sd_check_ace_denied_object: "
109                         "Access denied as of ace for %s\n",
110                         sid_str));
111                 talloc_free(sid_str);
112                 return NT_STATUS_ACCESS_DENIED;
113         }
114
115         return STATUS_MORE_ENTRIES;
116 }
117
118 /****************************************************************
119 ****************************************************************/
120
121 static NTSTATUS gpo_sd_check_ace_allowed_object(const struct security_ace *ace,
122                                                 const struct security_token *token)
123 {
124         char *sid_str;
125
126         if (gpo_sd_check_agp_object(ace) &&
127             gpo_sd_check_agp_access_bits(ace->access_mask) &&
128             security_token_has_sid(token, &ace->trustee)) {
129                 sid_str = dom_sid_string(NULL, &ace->trustee);
130                 DEBUG(10,("gpo_sd_check_ace_allowed_object: "
131                         "Access granted as of ace for %s\n",
132                         sid_str));
133                 talloc_free(sid_str);
134
135                 return NT_STATUS_OK;
136         }
137
138         return STATUS_MORE_ENTRIES;
139 }
140
141 /****************************************************************
142 ****************************************************************/
143
144 static NTSTATUS gpo_sd_check_ace(const struct security_ace *ace,
145                                  const struct security_token *token)
146 {
147         switch (ace->type) {
148                 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
149                         return gpo_sd_check_ace_denied_object(ace, token);
150                 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
151                         return gpo_sd_check_ace_allowed_object(ace, token);
152                 default:
153                         return STATUS_MORE_ENTRIES;
154         }
155 }
156
157 /****************************************************************
158 ****************************************************************/
159
160 NTSTATUS gpo_apply_security_filtering(const struct GROUP_POLICY_OBJECT *gpo,
161                                       const struct security_token *token)
162 {
163         struct security_descriptor *sd = gpo->security_descriptor;
164         struct security_acl *dacl = NULL;
165         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
166         int i;
167
168         if (!token) {
169                 return NT_STATUS_INVALID_USER_BUFFER;
170         }
171
172         if (!sd) {
173                 return NT_STATUS_INVALID_SECURITY_DESCR;
174         }
175
176         dacl = sd->dacl;
177         if (!dacl) {
178                 return NT_STATUS_INVALID_SECURITY_DESCR;
179         }
180
181         /* check all aces and only return NT_STATUS_OK (== Access granted) or
182          * NT_STATUS_ACCESS_DENIED ( == Access denied) - the default is to
183          * deny access */
184
185         for (i = 0; i < dacl->num_aces; i ++) {
186
187                 status = gpo_sd_check_ace(&dacl->aces[i], token);
188
189                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
190                         return status;
191                 } else if (NT_STATUS_IS_OK(status)) {
192                         return status;
193                 }
194
195                 continue;
196         }
197
198         return NT_STATUS_ACCESS_DENIED;
199 }