s4-dsdb: Explicitly mark some internal ldb requests as trusted
[nivanova/samba.git] / source4 / dsdb / samdb / ldb_modules / extended_dn_in.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce 2005-2008
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2008
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb extended dn control module
25  *
26  *  Description: this module interprets DNs of the form <SID=S-1-2-4456> into normal DNs.
27  *
28  *  Authors: Simo Sorce
29  *           Andrew Bartlett
30  */
31
32 #include "includes.h"
33 #include "ldb/include/ldb.h"
34 #include "ldb/include/ldb_errors.h"
35 #include "ldb/include/ldb_module.h"
36
37 /*
38   TODO: if relax is not set then we need to reject the fancy RMD_* and
39   DELETED extended DN codes
40  */
41
42 /* search */
43 struct extended_search_context {
44         struct ldb_module *module;
45         struct ldb_request *req;
46         struct ldb_dn *basedn;
47         char *wellknown_object;
48         int extended_type;
49 };
50
51 /* An extra layer of indirection because LDB does not allow the original request to be altered */
52
53 static int extended_final_callback(struct ldb_request *req, struct ldb_reply *ares)
54 {
55         int ret = LDB_ERR_OPERATIONS_ERROR;
56         struct extended_search_context *ac;
57         ac = talloc_get_type(req->context, struct extended_search_context);
58
59         if (ares->error != LDB_SUCCESS) {
60                 ret = ldb_module_done(ac->req, ares->controls,
61                                       ares->response, ares->error);
62         } else {
63                 switch (ares->type) {
64                 case LDB_REPLY_ENTRY:
65                         
66                         ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
67                         break;
68                 case LDB_REPLY_REFERRAL:
69                         
70                         ret = ldb_module_send_referral(ac->req, ares->referral);
71                         break;
72                 case LDB_REPLY_DONE:
73                         
74                         ret = ldb_module_done(ac->req, ares->controls,
75                                               ares->response, ares->error);
76                         break;
77                 }
78         }
79         return ret;
80 }
81
82 static int extended_base_callback(struct ldb_request *req, struct ldb_reply *ares)
83 {
84         struct extended_search_context *ac;
85         struct ldb_request *down_req;
86         struct ldb_message_element *el;
87         int ret;
88         unsigned int i;
89         size_t wkn_len = 0;
90         char *valstr = NULL;
91         const char *found = NULL;
92
93         ac = talloc_get_type(req->context, struct extended_search_context);
94
95         if (!ares) {
96                 return ldb_module_done(ac->req, NULL, NULL,
97                                         LDB_ERR_OPERATIONS_ERROR);
98         }
99         if (ares->error != LDB_SUCCESS) {
100                 return ldb_module_done(ac->req, ares->controls,
101                                         ares->response, ares->error);
102         }
103
104         switch (ares->type) {
105         case LDB_REPLY_ENTRY:
106                 if (!ac->wellknown_object) {
107                         ac->basedn = talloc_steal(ac, ares->message->dn);
108                         break;
109                 }
110
111                 wkn_len = strlen(ac->wellknown_object);
112
113                 el = ldb_msg_find_element(ares->message, "wellKnownObjects");
114                 if (!el) {
115                         ac->basedn = NULL;
116                         break;
117                 }
118
119                 for (i=0; i < el->num_values; i++) {
120                         valstr = talloc_strndup(ac,
121                                                 (const char *)el->values[i].data,
122                                                 el->values[i].length);
123                         if (!valstr) {
124                                 ldb_oom(ldb_module_get_ctx(ac->module));
125                                 return ldb_module_done(ac->req, NULL, NULL,
126                                                        LDB_ERR_OPERATIONS_ERROR);
127                         }
128
129                         if (strncasecmp(valstr, ac->wellknown_object, wkn_len) != 0) {
130                                 talloc_free(valstr);
131                                 continue;
132                         }
133
134                         found = &valstr[wkn_len];
135                         break;
136                 }
137
138                 if (!found) {
139                         break;
140                 }
141
142                 ac->basedn = ldb_dn_new(ac, ldb_module_get_ctx(ac->module), found);
143                 talloc_free(valstr);
144                 if (!ac->basedn) {
145                         ldb_oom(ldb_module_get_ctx(ac->module));
146                         return ldb_module_done(ac->req, NULL, NULL,
147                                                LDB_ERR_OPERATIONS_ERROR);
148                 }
149
150                 break;
151
152         case LDB_REPLY_REFERRAL:
153                 break;
154
155         case LDB_REPLY_DONE:
156
157                 if (!ac->basedn) {
158                         const char *str = talloc_asprintf(req, "Base-DN '%s' not found",
159                                                           ldb_dn_get_extended_linearized(req, ac->req->op.search.base, 1));
160                         ldb_set_errstring(ldb_module_get_ctx(ac->module), str);
161                         return ldb_module_done(ac->req, NULL, NULL,
162                                                LDB_ERR_NO_SUCH_OBJECT);
163                 }
164
165                 switch (ac->req->operation) {
166                 case LDB_SEARCH:
167                         ret = ldb_build_search_req_ex(&down_req,
168                                                       ldb_module_get_ctx(ac->module), ac->req,
169                                                       ac->basedn,
170                                                       ac->req->op.search.scope,
171                                                       ac->req->op.search.tree,
172                                                       ac->req->op.search.attrs,
173                                                       ac->req->controls,
174                                                       ac, extended_final_callback, 
175                                                       ac->req);
176                         ldb_req_mark_trusted(down_req);
177                         LDB_REQ_SET_LOCATION(down_req);
178                         break;
179                 case LDB_ADD:
180                 {
181                         struct ldb_message *add_msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
182                         if (!add_msg) {
183                                 ldb_oom(ldb_module_get_ctx(ac->module));
184                                 return ldb_module_done(ac->req, NULL, NULL,
185                                                        LDB_ERR_OPERATIONS_ERROR);
186                         }
187                         
188                         add_msg->dn = ac->basedn;
189
190                         ret = ldb_build_add_req(&down_req,
191                                                 ldb_module_get_ctx(ac->module), ac->req,
192                                                 add_msg, 
193                                                 ac->req->controls,
194                                                 ac, extended_final_callback, 
195                                                 ac->req);
196                         LDB_REQ_SET_LOCATION(down_req);
197                         break;
198                 }
199                 case LDB_MODIFY:
200                 {
201                         struct ldb_message *mod_msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
202                         if (!mod_msg) {
203                                 ldb_oom(ldb_module_get_ctx(ac->module));
204                                 return ldb_module_done(ac->req, NULL, NULL,
205                                                        LDB_ERR_OPERATIONS_ERROR);
206                         }
207                         
208                         mod_msg->dn = ac->basedn;
209
210                         ret = ldb_build_mod_req(&down_req,
211                                                 ldb_module_get_ctx(ac->module), ac->req,
212                                                 mod_msg, 
213                                                 ac->req->controls,
214                                                 ac, extended_final_callback, 
215                                                 ac->req);
216                         LDB_REQ_SET_LOCATION(down_req);
217                         break;
218                 }
219                 case LDB_DELETE:
220                         ret = ldb_build_del_req(&down_req,
221                                                 ldb_module_get_ctx(ac->module), ac->req,
222                                                 ac->basedn, 
223                                                 ac->req->controls,
224                                                 ac, extended_final_callback, 
225                                                 ac->req);
226                         LDB_REQ_SET_LOCATION(down_req);
227                         break;
228                 case LDB_RENAME:
229                         ret = ldb_build_rename_req(&down_req,
230                                                    ldb_module_get_ctx(ac->module), ac->req,
231                                                    ac->basedn, 
232                                                    ac->req->op.rename.newdn,
233                                                    ac->req->controls,
234                                                    ac, extended_final_callback, 
235                                                    ac->req);
236                         LDB_REQ_SET_LOCATION(down_req);
237                         break;
238                 default:
239                         return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
240                 }
241                 
242                 if (ret != LDB_SUCCESS) {
243                         return ldb_module_done(ac->req, NULL, NULL, ret);
244                 }
245
246                 return ldb_next_request(ac->module, down_req);
247         }
248         talloc_free(ares);
249         return LDB_SUCCESS;
250 }
251
252 static int extended_dn_in_fix(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn)
253 {
254         struct extended_search_context *ac;
255         struct ldb_request *down_req;
256         int ret;
257         struct ldb_dn *base_dn = NULL;
258         enum ldb_scope base_dn_scope = LDB_SCOPE_BASE;
259         const char *base_dn_filter = NULL;
260         const char * const *base_dn_attrs = NULL;
261         char *wellknown_object = NULL;
262         static const char *no_attr[] = {
263                 NULL
264         };
265         static const char *wkattr[] = {
266                 "wellKnownObjects",
267                 NULL
268         };
269         bool all_partitions = false;
270
271         if (!ldb_dn_has_extended(dn)) {
272                 /* Move along there isn't anything to see here */
273                 return ldb_next_request(module, req);
274         } else {
275                 /* It looks like we need to map the DN */
276                 const struct ldb_val *sid_val, *guid_val, *wkguid_val;
277                 int num_components = ldb_dn_get_comp_num(dn);
278                 int num_ex_components = ldb_dn_get_extended_comp_num(dn);
279
280                 /*
281                   windows ldap searchs don't allow a baseDN with more
282                   than one extended component, or an extended
283                   component and a string DN
284
285                   We only enforce this over ldap, not for internal
286                   use, as there are just too many places where we
287                   internally want to use a DN that has come from a
288                   search with extended DN enabled, or comes from a DRS
289                   naming context.
290
291                   Enforcing this would also make debugging samba much
292                   harder, as we'd need to use ldb_dn_minimise() in a
293                   lot of places, and that would lose the DN string
294                   which is so useful for working out what a request is
295                   for
296                  */
297                 if ((num_components != 0 || num_ex_components != 1) &&
298                     ldb_req_is_untrusted(req)) {
299                         return ldb_error(ldb_module_get_ctx(module),
300                                          LDB_ERR_INVALID_DN_SYNTAX, "invalid number of DN components");
301                 }
302
303                 sid_val = ldb_dn_get_extended_component(dn, "SID");
304                 guid_val = ldb_dn_get_extended_component(dn, "GUID");
305                 wkguid_val = ldb_dn_get_extended_component(dn, "WKGUID");
306
307                 if (sid_val) {
308                         all_partitions = true;
309                         base_dn = ldb_get_default_basedn(ldb_module_get_ctx(module));
310                         base_dn_filter = talloc_asprintf(req, "(objectSid=%s)", 
311                                                          ldb_binary_encode(req, *sid_val));
312                         if (!base_dn_filter) {
313                                 return ldb_oom(ldb_module_get_ctx(module));
314                         }
315                         base_dn_scope = LDB_SCOPE_SUBTREE;
316                         base_dn_attrs = no_attr;
317
318                 } else if (guid_val) {
319
320                         all_partitions = true;
321                         base_dn = ldb_get_default_basedn(ldb_module_get_ctx(module));
322                         base_dn_filter = talloc_asprintf(req, "(objectGUID=%s)", 
323                                                          ldb_binary_encode(req, *guid_val));
324                         if (!base_dn_filter) {
325                                 return ldb_oom(ldb_module_get_ctx(module));
326                         }
327                         base_dn_scope = LDB_SCOPE_SUBTREE;
328                         base_dn_attrs = no_attr;
329
330
331                 } else if (wkguid_val) {
332                         char *wkguid_dup;
333                         char *tail_str;
334                         char *p;
335
336                         wkguid_dup = talloc_strndup(req, (char *)wkguid_val->data, wkguid_val->length);
337
338                         p = strchr(wkguid_dup, ',');
339                         if (!p) {
340                                 return ldb_error(ldb_module_get_ctx(module), LDB_ERR_INVALID_DN_SYNTAX,
341                                                  "Invalid WKGUID format");
342                         }
343
344                         p[0] = '\0';
345                         p++;
346
347                         wellknown_object = talloc_asprintf(req, "B:32:%s:", wkguid_dup);
348                         if (!wellknown_object) {
349                                 return ldb_oom(ldb_module_get_ctx(module));
350                         }
351
352                         tail_str = p;
353
354                         base_dn = ldb_dn_new(req, ldb_module_get_ctx(module), tail_str);
355                         talloc_free(wkguid_dup);
356                         if (!base_dn) {
357                                 return ldb_oom(ldb_module_get_ctx(module));
358                         }
359                         base_dn_filter = talloc_strdup(req, "(objectClass=*)");
360                         if (!base_dn_filter) {
361                                 return ldb_oom(ldb_module_get_ctx(module));
362                         }
363                         base_dn_scope = LDB_SCOPE_BASE;
364                         base_dn_attrs = wkattr;
365                 } else {
366                         return ldb_error(ldb_module_get_ctx(module), LDB_ERR_INVALID_DN_SYNTAX,
367                                          "Invalid extended DN component");
368                 }
369
370                 ac = talloc_zero(req, struct extended_search_context);
371                 if (ac == NULL) {
372                         return ldb_oom(ldb_module_get_ctx(module));
373                 }
374                 
375                 ac->module = module;
376                 ac->req = req;
377                 ac->basedn = NULL;  /* Filled in if the search finds the DN by SID/GUID etc */
378                 ac->wellknown_object = wellknown_object;
379                 
380                 /* If the base DN was an extended DN (perhaps a well known
381                  * GUID) then search for that, so we can proceed with the original operation */
382
383                 ret = ldb_build_search_req(&down_req,
384                                            ldb_module_get_ctx(module), ac,
385                                            base_dn,
386                                            base_dn_scope,
387                                            base_dn_filter,
388                                            base_dn_attrs,
389                                            req->controls,
390                                            ac, extended_base_callback,
391                                            req);
392                 LDB_REQ_SET_LOCATION(down_req);
393                 if (ret != LDB_SUCCESS) {
394                         return ldb_operr(ldb_module_get_ctx(module));
395                 }
396
397                 if (all_partitions) {
398                         struct ldb_search_options_control *control;
399                         control = talloc(down_req, struct ldb_search_options_control);
400                         control->search_options = 2;
401                         ret = ldb_request_replace_control(down_req,
402                                                       LDB_CONTROL_SEARCH_OPTIONS_OID,
403                                                       true, control);
404                         if (ret != LDB_SUCCESS) {
405                                 ldb_oom(ldb_module_get_ctx(module));
406                                 return ret;
407                         }
408                 }
409
410                 /* perform the search */
411                 return ldb_next_request(module, down_req);
412         }
413 }
414
415 static int extended_dn_in_search(struct ldb_module *module, struct ldb_request *req)
416 {
417         return extended_dn_in_fix(module, req, req->op.search.base);
418 }
419
420 static int extended_dn_in_modify(struct ldb_module *module, struct ldb_request *req)
421 {
422         return extended_dn_in_fix(module, req, req->op.mod.message->dn);
423 }
424
425 static int extended_dn_in_del(struct ldb_module *module, struct ldb_request *req)
426 {
427         return extended_dn_in_fix(module, req, req->op.del.dn);
428 }
429
430 static int extended_dn_in_rename(struct ldb_module *module, struct ldb_request *req)
431 {
432         return extended_dn_in_fix(module, req, req->op.rename.olddn);
433 }
434
435 static const struct ldb_module_ops ldb_extended_dn_in_module_ops = {
436         .name              = "extended_dn_in",
437         .search            = extended_dn_in_search,
438         .modify            = extended_dn_in_modify,
439         .del               = extended_dn_in_del,
440         .rename            = extended_dn_in_rename,
441 };
442
443 int ldb_extended_dn_in_module_init(const char *version)
444 {
445         LDB_MODULE_CHECK_VERSION(version);
446         return ldb_register_module(&ldb_extended_dn_in_module_ops);
447 }