s4-dsdb: added tagging of requests in dsdb modules
[mat/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_SET_LOCATION(down_req);
177                         break;
178                 case LDB_ADD:
179                 {
180                         struct ldb_message *add_msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
181                         if (!add_msg) {
182                                 ldb_oom(ldb_module_get_ctx(ac->module));
183                                 return ldb_module_done(ac->req, NULL, NULL,
184                                                        LDB_ERR_OPERATIONS_ERROR);
185                         }
186                         
187                         add_msg->dn = ac->basedn;
188
189                         ret = ldb_build_add_req(&down_req,
190                                                 ldb_module_get_ctx(ac->module), ac->req,
191                                                 add_msg, 
192                                                 ac->req->controls,
193                                                 ac, extended_final_callback, 
194                                                 ac->req);
195                         LDB_REQ_SET_LOCATION(down_req);
196                         break;
197                 }
198                 case LDB_MODIFY:
199                 {
200                         struct ldb_message *mod_msg = ldb_msg_copy_shallow(ac, ac->req->op.mod.message);
201                         if (!mod_msg) {
202                                 ldb_oom(ldb_module_get_ctx(ac->module));
203                                 return ldb_module_done(ac->req, NULL, NULL,
204                                                        LDB_ERR_OPERATIONS_ERROR);
205                         }
206                         
207                         mod_msg->dn = ac->basedn;
208
209                         ret = ldb_build_mod_req(&down_req,
210                                                 ldb_module_get_ctx(ac->module), ac->req,
211                                                 mod_msg, 
212                                                 ac->req->controls,
213                                                 ac, extended_final_callback, 
214                                                 ac->req);
215                         break;
216                 }
217                 case LDB_DELETE:
218                         ret = ldb_build_del_req(&down_req,
219                                                 ldb_module_get_ctx(ac->module), ac->req,
220                                                 ac->basedn, 
221                                                 ac->req->controls,
222                                                 ac, extended_final_callback, 
223                                                 ac->req);
224                         break;
225                 case LDB_RENAME:
226                         ret = ldb_build_rename_req(&down_req,
227                                                    ldb_module_get_ctx(ac->module), ac->req,
228                                                    ac->basedn, 
229                                                    ac->req->op.rename.newdn,
230                                                    ac->req->controls,
231                                                    ac, extended_final_callback, 
232                                                    ac->req);
233                         break;
234                 default:
235                         return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
236                 }
237                 
238                 if (ret != LDB_SUCCESS) {
239                         return ldb_module_done(ac->req, NULL, NULL, ret);
240                 }
241
242                 return ldb_next_request(ac->module, down_req);
243         }
244         talloc_free(ares);
245         return LDB_SUCCESS;
246 }
247
248 static int extended_dn_in_fix(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn)
249 {
250         struct extended_search_context *ac;
251         struct ldb_request *down_req;
252         int ret;
253         struct ldb_dn *base_dn = NULL;
254         enum ldb_scope base_dn_scope = LDB_SCOPE_BASE;
255         const char *base_dn_filter = NULL;
256         const char * const *base_dn_attrs = NULL;
257         char *wellknown_object = NULL;
258         static const char *no_attr[] = {
259                 NULL
260         };
261         static const char *wkattr[] = {
262                 "wellKnownObjects",
263                 NULL
264         };
265         bool all_partitions = false;
266
267         if (!ldb_dn_has_extended(dn)) {
268                 /* Move along there isn't anything to see here */
269                 return ldb_next_request(module, req);
270         } else {
271                 /* It looks like we need to map the DN */
272                 const struct ldb_val *sid_val, *guid_val, *wkguid_val;
273
274                 sid_val = ldb_dn_get_extended_component(dn, "SID");
275                 guid_val = ldb_dn_get_extended_component(dn, "GUID");
276                 wkguid_val = ldb_dn_get_extended_component(dn, "WKGUID");
277
278                 if (sid_val) {
279                         all_partitions = true;
280                         base_dn = ldb_get_default_basedn(ldb_module_get_ctx(module));
281                         base_dn_filter = talloc_asprintf(req, "(objectSid=%s)", 
282                                                          ldb_binary_encode(req, *sid_val));
283                         if (!base_dn_filter) {
284                                 return ldb_oom(ldb_module_get_ctx(module));
285                         }
286                         base_dn_scope = LDB_SCOPE_SUBTREE;
287                         base_dn_attrs = no_attr;
288
289                 } else if (guid_val) {
290
291                         all_partitions = true;
292                         base_dn = ldb_get_default_basedn(ldb_module_get_ctx(module));
293                         base_dn_filter = talloc_asprintf(req, "(objectGUID=%s)", 
294                                                          ldb_binary_encode(req, *guid_val));
295                         if (!base_dn_filter) {
296                                 return ldb_oom(ldb_module_get_ctx(module));
297                         }
298                         base_dn_scope = LDB_SCOPE_SUBTREE;
299                         base_dn_attrs = no_attr;
300
301
302                 } else if (wkguid_val) {
303                         char *wkguid_dup;
304                         char *tail_str;
305                         char *p;
306
307                         wkguid_dup = talloc_strndup(req, (char *)wkguid_val->data, wkguid_val->length);
308
309                         p = strchr(wkguid_dup, ',');
310                         if (!p) {
311                                 return LDB_ERR_INVALID_DN_SYNTAX;
312                         }
313
314                         p[0] = '\0';
315                         p++;
316
317                         wellknown_object = talloc_asprintf(req, "B:32:%s:", wkguid_dup);
318                         if (!wellknown_object) {
319                                 return ldb_oom(ldb_module_get_ctx(module));
320                         }
321
322                         tail_str = p;
323
324                         base_dn = ldb_dn_new(req, ldb_module_get_ctx(module), tail_str);
325                         talloc_free(wkguid_dup);
326                         if (!base_dn) {
327                                 return ldb_oom(ldb_module_get_ctx(module));
328                         }
329                         base_dn_filter = talloc_strdup(req, "(objectClass=*)");
330                         if (!base_dn_filter) {
331                                 return ldb_oom(ldb_module_get_ctx(module));
332                         }
333                         base_dn_scope = LDB_SCOPE_BASE;
334                         base_dn_attrs = wkattr;
335                 } else {
336                         return LDB_ERR_INVALID_DN_SYNTAX;
337                 }
338
339                 ac = talloc_zero(req, struct extended_search_context);
340                 if (ac == NULL) {
341                         return ldb_oom(ldb_module_get_ctx(module));
342                 }
343                 
344                 ac->module = module;
345                 ac->req = req;
346                 ac->basedn = NULL;  /* Filled in if the search finds the DN by SID/GUID etc */
347                 ac->wellknown_object = wellknown_object;
348                 
349                 /* If the base DN was an extended DN (perhaps a well known
350                  * GUID) then search for that, so we can proceed with the original operation */
351
352                 ret = ldb_build_search_req(&down_req,
353                                            ldb_module_get_ctx(module), ac,
354                                            base_dn,
355                                            base_dn_scope,
356                                            base_dn_filter,
357                                            base_dn_attrs,
358                                            NULL,
359                                            ac, extended_base_callback,
360                                            req);
361                 if (ret != LDB_SUCCESS) {
362                         return ldb_operr(ldb_module_get_ctx(module));
363                 }
364
365                 if (all_partitions) {
366                         struct ldb_search_options_control *control;
367                         control = talloc(down_req, struct ldb_search_options_control);
368                         control->search_options = 2;
369                         ret = ldb_request_add_control(down_req,
370                                                       LDB_CONTROL_SEARCH_OPTIONS_OID,
371                                                       true, control);
372                         if (ret != LDB_SUCCESS) {
373                                 ldb_oom(ldb_module_get_ctx(module));
374                                 return ret;
375                         }
376                 }
377
378                 /* perform the search */
379                 return ldb_next_request(module, down_req);
380         }
381 }
382
383 static int extended_dn_in_search(struct ldb_module *module, struct ldb_request *req)
384 {
385         return extended_dn_in_fix(module, req, req->op.search.base);
386 }
387
388 static int extended_dn_in_modify(struct ldb_module *module, struct ldb_request *req)
389 {
390         return extended_dn_in_fix(module, req, req->op.mod.message->dn);
391 }
392
393 static int extended_dn_in_del(struct ldb_module *module, struct ldb_request *req)
394 {
395         return extended_dn_in_fix(module, req, req->op.del.dn);
396 }
397
398 static int extended_dn_in_rename(struct ldb_module *module, struct ldb_request *req)
399 {
400         return extended_dn_in_fix(module, req, req->op.rename.olddn);
401 }
402
403 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_in_module_ops = {
404         .name              = "extended_dn_in",
405         .search            = extended_dn_in_search,
406         .modify            = extended_dn_in_modify,
407         .del               = extended_dn_in_del,
408         .rename            = extended_dn_in_rename,
409 };