s4:dsdb - substitute the "show_deleted" with the "show_recycled" control
[nivanova/samba-autobuild/.git] / source4 / rpc_server / drsuapi / drsutil.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    useful utilities for the DRS server
5
6    Copyright (C) Andrew Tridgell 2009
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "libcli/security/security.h"
26 #include "param/param.h"
27 #include "auth/session.h"
28
29 int drsuapi_search_with_extended_dn(struct ldb_context *ldb,
30                                     TALLOC_CTX *mem_ctx,
31                                     struct ldb_result **_res,
32                                     struct ldb_dn *basedn,
33                                     enum ldb_scope scope,
34                                     const char * const *attrs,
35                                     const char *filter)
36 {
37         int ret;
38         struct ldb_request *req;
39         TALLOC_CTX *tmp_ctx;
40         struct ldb_result *res;
41
42         tmp_ctx = talloc_new(mem_ctx);
43
44         res = talloc_zero(tmp_ctx, struct ldb_result);
45         if (!res) {
46                 return LDB_ERR_OPERATIONS_ERROR;
47         }
48
49         ret = ldb_build_search_req(&req, ldb, tmp_ctx,
50                                    basedn,
51                                    scope,
52                                    filter,
53                                    attrs,
54                                    NULL,
55                                    res,
56                                    ldb_search_default_callback,
57                                    NULL);
58         if (ret != LDB_SUCCESS) {
59                 talloc_free(tmp_ctx);
60                 return ret;
61         }
62
63         ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, NULL);
64         if (ret != LDB_SUCCESS) {
65                 return ret;
66         }
67
68         ret = ldb_request_add_control(req, LDB_CONTROL_SHOW_RECYCLED_OID, true, NULL);
69         if (ret != LDB_SUCCESS) {
70                 return ret;
71         }
72
73         ret = ldb_request_add_control(req, LDB_CONTROL_REVEAL_INTERNALS, false, NULL);
74         if (ret != LDB_SUCCESS) {
75                 return ret;
76         }
77
78         ret = ldb_request(ldb, req);
79         if (ret == LDB_SUCCESS) {
80                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
81         }
82
83         talloc_free(req);
84         *_res = talloc_steal(mem_ctx, res);
85         return ret;
86 }
87
88 WERROR drs_security_level_check(struct dcesrv_call_state *dce_call,
89                                 const char* call,
90                                 enum security_user_level minimum_level,
91                                 const struct dom_sid *domain_sid)
92 {
93         enum security_user_level level;
94
95         if (lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL,
96                          "drs", "disable_sec_check", false)) {
97                 return WERR_OK;
98         }
99
100         level = security_session_user_level(dce_call->conn->auth_state.session_info, domain_sid);
101         if (level < minimum_level) {
102                 if (call) {
103                         DEBUG(0,("%s refused for security token (level=%u)\n",
104                                  call, (unsigned)level));
105                         security_token_debug(2, dce_call->conn->auth_state.session_info->security_token);
106                 }
107                 return WERR_DS_DRA_ACCESS_DENIED;
108         }
109
110         return WERR_OK;
111 }
112
113 void drsuapi_process_secret_attribute(struct drsuapi_DsReplicaAttribute *attr,
114                                       struct drsuapi_DsReplicaMetaData *meta_data)
115 {
116         if (attr->value_ctr.num_values == 0) {
117                 return;
118         }
119
120         switch (attr->attid) {
121         case DRSUAPI_ATTRIBUTE_dBCSPwd:
122         case DRSUAPI_ATTRIBUTE_unicodePwd:
123         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
124         case DRSUAPI_ATTRIBUTE_lmPwdHistory:
125         case DRSUAPI_ATTRIBUTE_supplementalCredentials:
126         case DRSUAPI_ATTRIBUTE_priorValue:
127         case DRSUAPI_ATTRIBUTE_currentValue:
128         case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
129         case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
130         case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
131         case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
132                 /*set value to null*/
133                 attr->value_ctr.num_values = 0;
134                 talloc_free(attr->value_ctr.values);
135                 attr->value_ctr.values = NULL;
136                 meta_data->originating_change_time = 0;
137                 return;
138         default:
139                 return;
140         }
141 }
142
143
144 /*
145   check security on a DN, with logging of errors
146  */
147 static WERROR drs_security_access_check_log(struct ldb_context *sam_ctx,
148                                             TALLOC_CTX *mem_ctx,
149                                             struct security_token *token,
150                                             struct ldb_dn *dn,
151                                             const char *ext_right)
152 {
153         int ret;
154         if (!dn) {
155                 DEBUG(3,("drs_security_access_check: Null dn provided, access is denied for %s\n",
156                               ext_right));
157                 return WERR_DS_DRA_ACCESS_DENIED;
158         }
159         ret = dsdb_check_access_on_dn(sam_ctx,
160                                       mem_ctx,
161                                       dn,
162                                       token,
163                                       SEC_ADS_CONTROL_ACCESS,
164                                       ext_right);
165         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
166                 DEBUG(3,("%s refused for security token on %s\n",
167                          ext_right, ldb_dn_get_linearized(dn)));
168                 security_token_debug(2, token);
169                 return WERR_DS_DRA_ACCESS_DENIED;
170         } else if (ret != LDB_SUCCESS) {
171                 DEBUG(1,("Failed to perform access check on %s\n", ldb_dn_get_linearized(dn)));
172                 return WERR_DS_DRA_INTERNAL_ERROR;
173         }
174         return WERR_OK;
175 }
176
177
178 /*
179   check security on a object identifier
180  */
181 WERROR drs_security_access_check(struct ldb_context *sam_ctx,
182                                  TALLOC_CTX *mem_ctx,
183                                  struct security_token *token,
184                                  struct drsuapi_DsReplicaObjectIdentifier *nc,
185                                  const char *ext_right)
186 {
187         struct ldb_dn *dn = drs_ObjectIdentifier_to_dn(mem_ctx, sam_ctx, nc);
188         WERROR werr;
189         werr = drs_security_access_check_log(sam_ctx, mem_ctx, token, dn, ext_right);
190         talloc_free(dn);
191         return werr;
192 }
193
194 /*
195   check security on the NC root of a object identifier
196  */
197 WERROR drs_security_access_check_nc_root(struct ldb_context *sam_ctx,
198                                          TALLOC_CTX *mem_ctx,
199                                          struct security_token *token,
200                                          struct drsuapi_DsReplicaObjectIdentifier *nc,
201                                          const char *ext_right)
202 {
203         struct ldb_dn *dn, *nc_root;
204         WERROR werr;
205         int ret;
206
207         dn = drs_ObjectIdentifier_to_dn(mem_ctx, sam_ctx, nc);
208         W_ERROR_HAVE_NO_MEMORY(dn);
209         ret = dsdb_find_nc_root(sam_ctx, dn, dn, &nc_root);
210         if (ret != LDB_SUCCESS) {
211                 return WERR_DS_CANT_FIND_EXPECTED_NC;
212         }
213         werr = drs_security_access_check_log(sam_ctx, mem_ctx, token, nc_root, ext_right);
214         talloc_free(dn);
215         return werr;
216 }