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