1fb8b9a475f9cde9e8118def36f914324115b24c
[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 {
60         int ret;
61         struct ldb_result *acl_res;
62         static const char *acl_attrs[] = {
63                 "nTSecurityDescriptor",
64                 "objectSid",
65                 NULL
66         };
67         struct ldb_context *ldb = ldb_module_get_ctx(module);
68         struct auth_session_info *session_info
69                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
70         if(!session_info) {
71                 return ldb_operr(ldb);
72         }
73         ret = dsdb_module_search_dn(module, mem_ctx, &acl_res, dn,
74                                     acl_attrs,
75                                     DSDB_FLAG_NEXT_MODULE |
76                                     DSDB_SEARCH_SHOW_RECYCLED);
77         if (ret != LDB_SUCCESS) {
78                 DEBUG(0,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
79                 return ret;
80         }
81         return dsdb_check_access_on_dn_internal(ldb, acl_res,
82                                                 mem_ctx,
83                                                 session_info->security_token,
84                                                 dn,
85                                                 access_mask,
86                                                 guid);
87 }
88
89 int dsdb_module_check_access_on_guid(struct ldb_module *module,
90                                      TALLOC_CTX *mem_ctx,
91                                      struct GUID *guid,
92                                      uint32_t access_mask,
93                                      const struct GUID *oc_guid)
94 {
95         int ret;
96         struct ldb_result *acl_res;
97         static const char *acl_attrs[] = {
98                 "nTSecurityDescriptor",
99                 "objectSid",
100                 NULL
101         };
102         struct ldb_context *ldb = ldb_module_get_ctx(module);
103         struct auth_session_info *session_info
104                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
105         if(!session_info) {
106                 return ldb_operr(ldb);
107         }
108         ret = dsdb_module_search(module, mem_ctx, &acl_res, NULL, LDB_SCOPE_SUBTREE,
109                                  acl_attrs,
110                                  DSDB_FLAG_NEXT_MODULE |
111                                  DSDB_SEARCH_SHOW_RECYCLED,
112                                  "objectGUID=%s", GUID_string(mem_ctx, guid));
113
114         if (ret != LDB_SUCCESS || acl_res->count == 0) {
115                 DEBUG(0,("access_check: failed to find object %s\n", GUID_string(mem_ctx, guid)));
116                 return ret;
117         }
118         return dsdb_check_access_on_dn_internal(ldb, acl_res,
119                                                 mem_ctx,
120                                                 session_info->security_token,
121                                                 acl_res->msgs[0]->dn,
122                                                 access_mask,
123                                                 oc_guid);
124 }
125
126 int acl_check_access_on_attribute(struct ldb_module *module,
127                                   TALLOC_CTX *mem_ctx,
128                                   struct security_descriptor *sd,
129                                   struct dom_sid *rp_sid,
130                                   uint32_t access_mask,
131                                   const struct dsdb_attribute *attr)
132 {
133         int ret;
134         NTSTATUS status;
135         uint32_t access_granted;
136         struct object_tree *root = NULL;
137         struct object_tree *new_node = NULL;
138         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
139         struct security_token *token = acl_user_token(module);
140         if (attr) {
141                 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
142                         if (!insert_in_object_tree(tmp_ctx,
143                                                    &attr->attributeSecurityGUID,
144                                                    access_mask, &root,
145                                                    &new_node)) {
146                                 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
147                                 goto fail;
148                         }
149
150                         if (!insert_in_object_tree(tmp_ctx,
151                                                    &attr->schemaIDGUID,
152                                                    access_mask, &new_node,
153                                                    &new_node)) {
154                                 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
155                                 goto fail;
156                         }
157                 }
158                 else {
159                         if (!insert_in_object_tree(tmp_ctx,
160                                                    &attr->schemaIDGUID,
161                                                    access_mask, &root,
162                                                    &new_node)) {
163                                 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
164                                 goto fail;
165                         }
166                 }
167         }
168         status = sec_access_check_ds(sd, token,
169                                      access_mask,
170                                      &access_granted,
171                                      root,
172                                      rp_sid);
173         if (!NT_STATUS_IS_OK(status)) {
174                 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
175         }
176         else {
177                 ret = LDB_SUCCESS;
178         }
179         talloc_free(tmp_ctx);
180         return ret;
181 fail:
182         talloc_free(tmp_ctx);
183         return ldb_operr(ldb_module_get_ctx(module));
184 }
185
186
187 /* checks for validated writes */
188 int acl_check_extended_right(TALLOC_CTX *mem_ctx,
189                              struct security_descriptor *sd,
190                              struct security_token *token,
191                              const char *ext_right,
192                              uint32_t right_type,
193                              struct dom_sid *sid)
194 {
195         struct GUID right;
196         NTSTATUS status;
197         uint32_t access_granted;
198         struct object_tree *root = NULL;
199         struct object_tree *new_node = NULL;
200         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
201
202         GUID_from_string(ext_right, &right);
203
204         if (!insert_in_object_tree(tmp_ctx, &right, right_type,
205                                    &root, &new_node)) {
206                 DEBUG(10, ("acl_ext_right: cannot add to object tree\n"));
207                 talloc_free(tmp_ctx);
208                 return LDB_ERR_OPERATIONS_ERROR;
209         }
210         status = sec_access_check_ds(sd, token,
211                                      right_type,
212                                      &access_granted,
213                                      root,
214                                      sid);
215
216         if (!NT_STATUS_IS_OK(status)) {
217                 talloc_free(tmp_ctx);
218                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
219         }
220         talloc_free(tmp_ctx);
221         return LDB_SUCCESS;
222 }
223
224 const char *acl_user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module)
225 {
226         struct ldb_context *ldb = ldb_module_get_ctx(module);
227         struct auth_session_info *session_info
228                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
229         if (!session_info) {
230                 return "UNKNOWN (NULL)";
231         }
232
233         return talloc_asprintf(mem_ctx, "%s\\%s",
234                                session_info->server_info->domain_name,
235                                session_info->server_info->account_name);
236 }