s4-dsdb: use ldb_operr() in the dsdb code
[samba.git] / source4 / dsdb / common / dsdb_access.c
1 /*
2   ldb database library
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: dsdb_access
22  *
23  *  Description: utility functions for access checking on objects
24  *
25  *  Authors: Nadezhda Ivanova
26  */
27
28 #include "includes.h"
29 #include "ldb.h"
30 #include "ldb_module.h"
31 #include "ldb_errors.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_security.h"
34 #include "libcli/ldap/ldap_ndr.h"
35 #include "param/param.h"
36 #include "auth/auth.h"
37 #include "dsdb/samdb/samdb.h"
38
39 void dsdb_acl_debug(struct security_descriptor *sd,
40                       struct security_token *token,
41                       struct ldb_dn *dn,
42                       bool denied,
43                       int level)
44 {
45         if (denied) {
46                 DEBUG(level, ("Access on %s denied", ldb_dn_get_linearized(dn)));
47         } else {
48                 DEBUG(level, ("Access on %s granted", ldb_dn_get_linearized(dn)));
49         }
50
51         DEBUG(level,("Security context: %s\n",
52                      ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_token,"", token)));
53         DEBUG(level,("Security descriptor: %s\n",
54                      ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,"", sd)));
55 }
56
57 int dsdb_get_sd_from_ldb_message(struct ldb_context *ldb,
58                                  TALLOC_CTX *mem_ctx,
59                                  struct ldb_message *acl_res,
60                                  struct security_descriptor **sd)
61 {
62         struct ldb_message_element *sd_element;
63         enum ndr_err_code ndr_err;
64
65         sd_element = ldb_msg_find_element(acl_res, "nTSecurityDescriptor");
66         if (!sd_element) {
67                 *sd = NULL;
68                 return LDB_SUCCESS;
69         }
70         *sd = talloc(mem_ctx, struct security_descriptor);
71         if(!*sd) {
72                 return ldb_oom(ldb);
73         }
74         ndr_err = ndr_pull_struct_blob(&sd_element->values[0], *sd, *sd,
75                                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
76
77         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
78                 return ldb_operr(ldb);
79         }
80
81         return LDB_SUCCESS;
82 }
83
84 int dsdb_check_access_on_dn_internal(struct ldb_context *ldb,
85                                      struct ldb_result *acl_res,
86                                      TALLOC_CTX *mem_ctx,
87                                      struct security_token *token,
88                                      struct ldb_dn *dn,
89                                      uint32_t access,
90                                      const struct GUID *guid)
91 {
92         struct security_descriptor *sd = NULL;
93         struct dom_sid *sid = NULL;
94         struct object_tree *root = NULL;
95         struct object_tree *new_node = NULL;
96         NTSTATUS status;
97         uint32_t access_granted;
98         int ret;
99
100         ret = dsdb_get_sd_from_ldb_message(ldb, mem_ctx, acl_res->msgs[0], &sd);
101         if (ret != LDB_SUCCESS) {
102                 return ldb_operr(ldb);
103         }
104         /* Theoretically we pass the check if the object has no sd */
105         if (!sd) {
106                 return LDB_SUCCESS;
107         }
108         sid = samdb_result_dom_sid(mem_ctx, acl_res->msgs[0], "objectSid");
109         if (guid) {
110                 if (!insert_in_object_tree(mem_ctx, guid, access, &root, &new_node)) {
111                         return ldb_operr(ldb);
112                 }
113         }
114         status = sec_access_check_ds(sd, token,
115                                      access,
116                                      &access_granted,
117                                      root,
118                                      sid);
119         if (!NT_STATUS_IS_OK(status)) {
120                 dsdb_acl_debug(sd,
121                                token,
122                                dn,
123                                true,
124                                10);
125                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
126         }
127         return LDB_SUCCESS;
128 }
129
130 /* performs an access check from outside the module stack
131  * given the dn of the object to be checked, the required access
132  * guid is either the guid of the extended right, or NULL
133  */
134
135 int dsdb_check_access_on_dn(struct ldb_context *ldb,
136                             TALLOC_CTX *mem_ctx,
137                             struct ldb_dn *dn,
138                             uint32_t access,
139                             const struct GUID *guid)
140 {
141         int ret;
142         struct ldb_result *acl_res;
143         static const char *acl_attrs[] = {
144                 "nTSecurityDescriptor",
145                 "objectSid",
146                 NULL
147         };
148
149         struct auth_session_info *session_info
150                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
151         if(!session_info) {
152                 return ldb_operr(ldb);
153         }
154
155         ret = ldb_search(ldb, mem_ctx, &acl_res, dn, LDB_SCOPE_BASE, acl_attrs, NULL);
156         if (ret != LDB_SUCCESS) {
157                 DEBUG(10,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
158                 return ret;
159         }
160
161         return dsdb_check_access_on_dn_internal(ldb, acl_res,
162                                                 mem_ctx,
163                                                 session_info->security_token,
164                                                 dn,
165                                                 access,
166                                                 guid);
167 }
168