r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[kai/samba.git] / source4 / dsdb / samdb / ldb_modules / extended_dn.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb extended dn control module
29  *
30  *  Description: this module builds a special dn
31  *
32  *  Author: Simo Sorce
33  */
34
35 #include "includes.h"
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41
42 #include <time.h>
43
44 static BOOL is_attr_in_list(const char * const * attrs, const char *attr)
45 {
46         int i;
47
48         for (i = 0; attrs[i]; i++) {
49                 if (strcasecmp(attrs[i], attr) == 0)
50                         return True;
51         }
52
53         return False;
54 }
55
56 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
57 {
58         char **new;
59         int i, num;
60
61         for (num = 0; attrs[num]; num++);
62
63         new = talloc_array(mem_ctx, char *, num + 1);
64         if (!new) return NULL;
65
66         for(i = 0; i < num; i++) {
67                 new[i] = talloc_strdup(new, attrs[i]);
68                 if (!new[i]) {
69                         talloc_free(new);
70                         return NULL;
71                 }
72         }
73         new[i] = NULL;
74
75         return new;
76 }
77
78 static BOOL add_attrs(void *mem_ctx, char ***attrs, const char *attr)
79 {
80         char **new;
81         int num;
82
83         for (num = 0; (*attrs)[num]; num++);
84
85         new = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
86         if (!new) return False;
87
88         *attrs = new;
89
90         new[num] = talloc_strdup(new, attr);
91         if (!new[num]) return False;
92
93         new[num + 1] = NULL;
94
95         return True;
96 }
97
98 static BOOL inject_extended_dn(struct ldb_message *msg,
99                                 int type,
100                                 BOOL remove_guid,
101                                 BOOL remove_sid)
102 {
103         const struct ldb_val *val;
104         struct GUID guid;
105         struct dom_sid *sid;
106         char *object_guid;
107         char *object_sid;
108         char *new_dn, *dn;
109
110         dn = ldb_dn_linearize(msg, msg->dn);
111         if (!dn)
112                 return False;
113
114         /* retrieve object_guid */
115         guid = samdb_result_guid(msg, "objectGUID");
116         object_guid = GUID_string(msg, &guid);
117         if (!object_guid)
118                 return False;
119
120         if (remove_guid)
121                 ldb_msg_remove_attr(msg, "objectGUID");
122
123         /* retrieve object_sid */
124         object_sid = NULL;
125         sid = samdb_result_dom_sid(msg, msg, "objectSID");
126         if (sid) {
127                 object_sid = dom_sid_string(msg, sid);
128                 if (!object_sid)
129                         return False;
130
131                 if (remove_sid)
132                         ldb_msg_remove_attr(msg, "objectSID");
133         }
134
135         /* TODO: handle type */
136         switch (type) {
137                 case 0:
138                 case 1:
139                         if (object_sid) {
140                                 new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
141                                                          object_guid, object_sid, dn);
142                         } else {
143                                 new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
144                                                          object_guid, dn);
145                         }
146                         break;
147                 default:
148                         return False;
149         }
150
151         if (!new_dn)
152                 return False;
153
154         msg->dn = ldb_dn_explode_or_special(msg, new_dn);
155         if (!msg->dn)
156                 return False;
157
158         val = ldb_msg_find_ldb_val(msg, "distinguishedName");
159         if (val) {
160                 ldb_msg_remove_attr(msg, "distinguishedName");
161                 if (ldb_msg_add_steal_string(msg, "distinguishedName", new_dn))
162                         return False;
163         }
164
165         return True;
166 }
167
168 /* search */
169 static int extended_search(struct ldb_module *module, struct ldb_control *control, struct ldb_request *req)
170 {
171         struct ldb_result *extended_result;
172         struct ldb_control **saved_controls;
173         struct ldb_extended_dn_control *extended_ctrl;
174         int i, ret;
175         const char * const *saved_attrs = NULL;
176         char **new_attrs;
177         BOOL remove_guid = False;
178         BOOL remove_sid = False;
179
180         extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
181         if (!extended_ctrl) {
182                 return LDB_ERR_PROTOCOL_ERROR;
183         }
184
185         /* save it locally and remove it from the list */
186         if (!save_controls(control, req, &saved_controls)) {
187                 return LDB_ERR_OPERATIONS_ERROR;
188         }
189                 
190         /* check if attrs only is specified, in that case check wether we need to modify them */
191         if (req->op.search.attrs) {
192                 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
193                         remove_guid = True;
194                 }
195                 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
196                         remove_sid = True;
197                 }
198                 if (remove_guid || remove_sid) {
199                         new_attrs = copy_attrs(req, req->op.search.attrs);
200                         if (!new_attrs)
201                                 return LDB_ERR_OPERATIONS_ERROR;
202                         
203                         saved_attrs = req->op.search.attrs;
204
205                         if (remove_guid) {
206                                 if (!add_attrs(req, &new_attrs, "objectGUID"))
207                                         return LDB_ERR_OPERATIONS_ERROR;
208                         }
209                         if (remove_sid) {
210                                 if (!add_attrs(req, &new_attrs, "objectSID"))
211                                         return LDB_ERR_OPERATIONS_ERROR;
212                         }
213
214                         req->op.search.attrs = (const char * const *)new_attrs;
215                 }
216         }
217
218         ret = ldb_next_request(module, req);
219
220         /* put request back into original shape */
221         /* TODO: build a new req and don't touch the original one */
222
223         if (req->controls) talloc_free(req->controls);
224         req->controls = saved_controls;
225
226         if (saved_attrs) {
227                 talloc_free(new_attrs);
228                 req->op.search.attrs = saved_attrs;
229         }
230
231         if (ret != LDB_SUCCESS) {
232                 return ret;
233         }
234
235         extended_result = req->op.search.res;
236         
237         for (i = 0; i < extended_result->count; i++) {
238                 /* TODO: the following funtion updates only dn and
239                  * distinguishedName. We still need to address other
240                  * DN entries like objectCategory
241                  */
242                 if (!inject_extended_dn(extended_result->msgs[i], 
243                                         extended_ctrl->type,
244                                         remove_guid, remove_sid)) {
245                         return LDB_ERR_OPERATIONS_ERROR;
246                 }
247         }
248         
249         return LDB_SUCCESS;     
250 }
251
252 /* search */
253 struct extended_async_context {
254
255         struct ldb_module *module;
256         void *up_context;
257         int (*up_callback)(struct ldb_context *, void *, struct ldb_async_result *);
258         int timeout;
259
260         const char * const *attrs;
261         BOOL remove_guid;
262         BOOL remove_sid;
263         int extended_type;
264 };
265
266 static int extended_async_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
267 {
268         struct extended_async_context *ac;
269
270         if (!context || !ares) {
271                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
272                 goto error;
273         }
274
275         ac = talloc_get_type(context, struct extended_async_context);
276
277         if (ares->type == LDB_REPLY_ENTRY) {
278                 /* for each record returned post-process to add any derived
279                    attributes that have been asked for */
280                 if (!inject_extended_dn(ares->message, ac->extended_type, ac->remove_guid, ac->remove_sid)) {
281                         goto error;
282                 }
283         }
284
285         return ac->up_callback(ldb, ac->up_context, ares);
286
287 error:
288         talloc_free(ares);
289         return LDB_ERR_OPERATIONS_ERROR;
290 }
291
292 static int extended_search_async(struct ldb_module *module, struct ldb_control *control, struct ldb_request *req)
293 {
294         struct ldb_extended_dn_control *extended_ctrl;
295         struct ldb_control **saved_controls;
296         struct extended_async_context *ac;
297         struct ldb_request *down_req;
298         char **new_attrs;
299         int ret;
300
301         extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
302         if (!extended_ctrl) {
303                 return LDB_ERR_PROTOCOL_ERROR;
304         }
305
306         ac = talloc(req, struct extended_async_context);
307         if (ac == NULL) {
308                 return LDB_ERR_OPERATIONS_ERROR;
309         }
310
311         ac->module = module;
312         ac->up_context = req->async.context;
313         ac->up_callback = req->async.callback;
314         ac->timeout = req->async.timeout;
315         ac->attrs = req->op.search.attrs;
316         ac->remove_guid = False;
317         ac->remove_sid = False;
318         ac->extended_type = extended_ctrl->type;
319
320         down_req = talloc_zero(req, struct ldb_request);
321         if (down_req == NULL) {
322                 return LDB_ERR_OPERATIONS_ERROR;
323         }
324
325         down_req->operation = req->operation;
326         down_req->op.search.base = req->op.search.base;
327         down_req->op.search.scope = req->op.search.scope;
328         down_req->op.search.tree = req->op.search.tree;
329
330         /* check if attrs only is specified, in that case check wether we need to modify them */
331         if (req->op.search.attrs) {
332                 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
333                         ac->remove_guid = True;
334                 }
335                 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
336                         ac->remove_sid = True;
337                 }
338                 if (ac->remove_guid || ac->remove_sid) {
339                         new_attrs = copy_attrs(down_req, req->op.search.attrs);
340                         if (new_attrs == NULL)
341                                 return LDB_ERR_OPERATIONS_ERROR;
342                         
343                         if (ac->remove_guid) {
344                                 if (!add_attrs(down_req, &new_attrs, "objectGUID"))
345                                         return LDB_ERR_OPERATIONS_ERROR;
346                         }
347                         if (ac->remove_sid) {
348                                 if (!add_attrs(down_req, &new_attrs, "objectSID"))
349                                         return LDB_ERR_OPERATIONS_ERROR;
350                         }
351
352                         down_req->op.search.attrs = (const char * const *)new_attrs;
353                 }
354         }
355
356         down_req->controls = req->controls;
357
358         /* save it locally and remove it from the list */
359         /* we do not need to replace them later as we
360          * are keeping the original req intact */
361         if (!save_controls(control, down_req, &saved_controls)) {
362                 return LDB_ERR_OPERATIONS_ERROR;
363         }
364
365         down_req->creds = req->creds;
366
367         down_req->async.context = ac;
368         down_req->async.callback = extended_async_callback;
369         down_req->async.timeout = req->async.timeout;
370
371         /* perform the search */
372         ret = ldb_next_request(module, down_req);
373
374         /* do not free down_req as the call results may be linked to it,
375          * it will be freed when the upper level request get freed */
376         if (ret == LDB_SUCCESS) {
377                 req->async.handle = down_req->async.handle;
378         }
379
380         return ret;
381 }
382
383 static int extended_request(struct ldb_module *module, struct ldb_request *req)
384 {
385         struct ldb_control *control;
386
387         /* check if there's an extended dn control */
388         control = get_control_from_list(req->controls, LDB_CONTROL_EXTENDED_DN_OID);
389         if (control == NULL) {
390                 /* not found go on */
391                 return ldb_next_request(module, req);
392         }
393
394         switch (req->operation) {
395
396         case LDB_REQ_SEARCH:
397                 return extended_search(module, control, req);
398
399         case LDB_ASYNC_SEARCH:
400                 return extended_search_async(module, control, req);
401
402         default:
403                 return LDB_ERR_OPERATIONS_ERROR;
404
405         }
406 }
407
408 static int extended_init(struct ldb_module *module)
409 {
410         struct ldb_request *req;
411         int ret;
412
413         req = talloc(module, struct ldb_request);
414         if (req == NULL) {
415                 return LDB_ERR_OPERATIONS_ERROR;
416         }
417
418         req->operation = LDB_REQ_REGISTER;
419         req->op.reg.oid = LDB_CONTROL_EXTENDED_DN_OID;
420         req->controls = NULL;
421
422         ret = ldb_request(module->ldb, req);
423         if (ret != LDB_SUCCESS) {
424                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "extended_dn: Unable to register control with rootdse!\n");
425                 talloc_free(req);
426                 return LDB_ERR_OPERATIONS_ERROR;
427         }
428
429         talloc_free(req);
430         return ldb_next_init(module);
431 }
432
433 static const struct ldb_module_ops extended_dn_ops = {
434         .name              = "extended_dn",
435         .request           = extended_request,
436         .init_context      = extended_init
437 };
438
439 int ldb_extended_dn_init(void)
440 {
441         return ldb_register_module(&extended_dn_ops);
442 }