s4-dsdb: Explicitly mark some internal ldb requests as trusted
[nivanova/samba.git] / source4 / dsdb / samdb / ldb_modules / acl_util.c
1 /*
2   ACL utility functions
3
4   Copyright (C) Nadezhda Ivanova 2010
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 /*
21  *  Name: acl_util
22  *
23  *  Component: ldb ACL modules
24  *
25  *  Description: Some auxiliary functions used for access checking
26  *
27  *  Author: Nadezhda Ivanova
28  */
29 #include "includes.h"
30 #include "ldb_module.h"
31 #include "auth/auth.h"
32 #include "libcli/security/security.h"
33 #include "dsdb/samdb/samdb.h"
34 #include "librpc/gen_ndr/ndr_security.h"
35 #include "param/param.h"
36 #include "dsdb/samdb/ldb_modules/util.h"
37
38 struct security_token *acl_user_token(struct ldb_module *module)
39 {
40         struct ldb_context *ldb = ldb_module_get_ctx(module);
41         struct auth_session_info *session_info
42                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
43         if(!session_info) {
44                 return NULL;
45         }
46         return session_info->security_token;
47 }
48
49 /* performs an access check from inside the module stack
50  * given the dn of the object to be checked, the required access
51  * guid is either the guid of the extended right, or NULL
52  */
53
54 int dsdb_module_check_access_on_dn(struct ldb_module *module,
55                                    TALLOC_CTX *mem_ctx,
56                                    struct ldb_dn *dn,
57                                    uint32_t access_mask,
58                                    const struct GUID *guid,
59                                    struct ldb_request *parent)
60 {
61         int ret;
62         struct ldb_result *acl_res;
63         static const char *acl_attrs[] = {
64                 "nTSecurityDescriptor",
65                 "objectSid",
66                 NULL
67         };
68         struct ldb_context *ldb = ldb_module_get_ctx(module);
69         struct auth_session_info *session_info
70                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
71         if(!session_info) {
72                 return ldb_operr(ldb);
73         }
74         ret = dsdb_module_search_dn(module, mem_ctx, &acl_res, dn,
75                                     acl_attrs,
76                                     DSDB_FLAG_NEXT_MODULE |
77                                     DSDB_SEARCH_SHOW_RECYCLED |
78                                     DSDB_FLAG_TRUSTED,
79                                     parent);
80         if (ret != LDB_SUCCESS) {
81                 DEBUG(0,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
82                 return ret;
83         }
84         return dsdb_check_access_on_dn_internal(ldb, acl_res,
85                                                 mem_ctx,
86                                                 session_info->security_token,
87                                                 dn,
88                                                 access_mask,
89                                                 guid);
90 }
91
92 int dsdb_module_check_access_on_guid(struct ldb_module *module,
93                                      TALLOC_CTX *mem_ctx,
94                                      struct GUID *guid,
95                                      uint32_t access_mask,
96                                      const struct GUID *oc_guid,
97                                      struct ldb_request *parent)
98 {
99         int ret;
100         struct ldb_result *acl_res;
101         static const char *acl_attrs[] = {
102                 "nTSecurityDescriptor",
103                 "objectSid",
104                 NULL
105         };
106         struct ldb_context *ldb = ldb_module_get_ctx(module);
107         struct auth_session_info *session_info
108                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
109         if(!session_info) {
110                 return ldb_operr(ldb);
111         }
112         ret = dsdb_module_search(module, mem_ctx, &acl_res, NULL, LDB_SCOPE_SUBTREE,
113                                  acl_attrs,
114                                  DSDB_FLAG_NEXT_MODULE |
115                                  DSDB_SEARCH_SHOW_RECYCLED |
116                                  DSDB_FLAG_TRUSTED,
117                                  parent,
118                                  "objectGUID=%s", GUID_string(mem_ctx, guid));
119
120         if (ret != LDB_SUCCESS || acl_res->count == 0) {
121                 DEBUG(0,("access_check: failed to find object %s\n", GUID_string(mem_ctx, guid)));
122                 return ret;
123         }
124         return dsdb_check_access_on_dn_internal(ldb, acl_res,
125                                                 mem_ctx,
126                                                 session_info->security_token,
127                                                 acl_res->msgs[0]->dn,
128                                                 access_mask,
129                                                 oc_guid);
130 }
131
132 int acl_check_access_on_attribute(struct ldb_module *module,
133                                   TALLOC_CTX *mem_ctx,
134                                   struct security_descriptor *sd,
135                                   struct dom_sid *rp_sid,
136                                   uint32_t access_mask,
137                                   const struct dsdb_attribute *attr)
138 {
139         int ret;
140         NTSTATUS status;
141         uint32_t access_granted;
142         struct object_tree *root = NULL;
143         struct object_tree *new_node = NULL;
144         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
145         struct security_token *token = acl_user_token(module);
146         if (attr) {
147                 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
148                         if (!insert_in_object_tree(tmp_ctx,
149                                                    &attr->attributeSecurityGUID,
150                                                    access_mask, &root,
151                                                    &new_node)) {
152                                 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
153                                 goto fail;
154                         }
155
156                         if (!insert_in_object_tree(tmp_ctx,
157                                                    &attr->schemaIDGUID,
158                                                    access_mask, &new_node,
159                                                    &new_node)) {
160                                 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
161                                 goto fail;
162                         }
163                 }
164                 else {
165                         if (!insert_in_object_tree(tmp_ctx,
166                                                    &attr->schemaIDGUID,
167                                                    access_mask, &root,
168                                                    &new_node)) {
169                                 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
170                                 goto fail;
171                         }
172                 }
173         }
174         status = sec_access_check_ds(sd, token,
175                                      access_mask,
176                                      &access_granted,
177                                      root,
178                                      rp_sid);
179         if (!NT_STATUS_IS_OK(status)) {
180                 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
181         }
182         else {
183                 ret = LDB_SUCCESS;
184         }
185         talloc_free(tmp_ctx);
186         return ret;
187 fail:
188         talloc_free(tmp_ctx);
189         return ldb_operr(ldb_module_get_ctx(module));
190 }
191
192
193 /* checks for validated writes */
194 int acl_check_extended_right(TALLOC_CTX *mem_ctx,
195                              struct security_descriptor *sd,
196                              struct security_token *token,
197                              const char *ext_right,
198                              uint32_t right_type,
199                              struct dom_sid *sid)
200 {
201         struct GUID right;
202         NTSTATUS status;
203         uint32_t access_granted;
204         struct object_tree *root = NULL;
205         struct object_tree *new_node = NULL;
206         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
207
208         GUID_from_string(ext_right, &right);
209
210         if (!insert_in_object_tree(tmp_ctx, &right, right_type,
211                                    &root, &new_node)) {
212                 DEBUG(10, ("acl_ext_right: cannot add to object tree\n"));
213                 talloc_free(tmp_ctx);
214                 return LDB_ERR_OPERATIONS_ERROR;
215         }
216         status = sec_access_check_ds(sd, token,
217                                      right_type,
218                                      &access_granted,
219                                      root,
220                                      sid);
221
222         if (!NT_STATUS_IS_OK(status)) {
223                 talloc_free(tmp_ctx);
224                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
225         }
226         talloc_free(tmp_ctx);
227         return LDB_SUCCESS;
228 }
229
230 const char *acl_user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module)
231 {
232         struct ldb_context *ldb = ldb_module_get_ctx(module);
233         struct auth_session_info *session_info
234                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
235         if (!session_info) {
236                 return "UNKNOWN (NULL)";
237         }
238
239         return talloc_asprintf(mem_ctx, "%s\\%s",
240                                session_info->server_info->domain_name,
241                                session_info->server_info->account_name);
242 }