0a3227a912007577d9f0300cbff0c5924fdc1a56
[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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldb extended dn control module
28  *
29  *  Description: this module builds a special dn
30  *
31  *  Author: Simo Sorce
32  */
33
34 #include "includes.h"
35 #include "ldb/include/ldb.h"
36 #include "ldb/include/ldb_errors.h"
37 #include "ldb/include/ldb_private.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "libcli/security/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                                 struct ldb_context *ldb,
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;
110
111         /* retrieve object_guid */
112         guid = samdb_result_guid(msg, "objectGUID");
113         object_guid = GUID_string(msg, &guid);
114         if (!object_guid)
115                 return False;
116
117         if (remove_guid)
118                 ldb_msg_remove_attr(msg, "objectGUID");
119
120         /* retrieve object_sid */
121         object_sid = NULL;
122         sid = samdb_result_dom_sid(msg, msg, "objectSID");
123         if (sid) {
124                 object_sid = dom_sid_string(msg, sid);
125                 if (!object_sid)
126                         return False;
127
128                 if (remove_sid)
129                         ldb_msg_remove_attr(msg, "objectSID");
130         }
131
132         /* TODO: handle type */
133         switch (type) {
134                 case 0:
135                 case 1:
136                         if (object_sid) {
137                                 new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
138                                                          object_guid, object_sid,
139                                                          ldb_dn_get_linearized(msg->dn));
140                         } else {
141                                 new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
142                                                          object_guid,
143                                                          ldb_dn_get_linearized(msg->dn));
144                         }
145                         break;
146                 default:
147                         return False;
148         }
149
150         if (!new_dn)
151                 return False;
152
153         msg->dn = ldb_dn_new(msg, ldb, new_dn);
154         if (! ldb_dn_validate(msg->dn))
155                 return False;
156
157         val = ldb_msg_find_ldb_val(msg, "distinguishedName");
158         if (val) {
159                 ldb_msg_remove_attr(msg, "distinguishedName");
160                 if (ldb_msg_add_steal_string(msg, "distinguishedName", new_dn))
161                         return False;
162         }
163
164         return True;
165 }
166
167 /* search */
168 struct extended_context {
169
170         struct ldb_module *module;
171         void *up_context;
172         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
173
174         const char * const *attrs;
175         BOOL remove_guid;
176         BOOL remove_sid;
177         int extended_type;
178 };
179
180 static int extended_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
181 {
182         struct extended_context *ac;
183
184         if (!context || !ares) {
185                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
186                 goto error;
187         }
188
189         ac = talloc_get_type(context, struct extended_context);
190
191         if (ares->type == LDB_REPLY_ENTRY) {
192                 /* for each record returned post-process to add any derived
193                    attributes that have been asked for */
194                 if (!inject_extended_dn(ares->message, ldb, ac->extended_type, ac->remove_guid, ac->remove_sid)) {
195                         goto error;
196                 }
197         }
198
199         return ac->up_callback(ldb, ac->up_context, ares);
200
201 error:
202         talloc_free(ares);
203         return LDB_ERR_OPERATIONS_ERROR;
204 }
205
206 static int extended_search(struct ldb_module *module, struct ldb_request *req)
207 {
208         struct ldb_control *control;
209         struct ldb_extended_dn_control *extended_ctrl;
210         struct ldb_control **saved_controls;
211         struct extended_context *ac;
212         struct ldb_request *down_req;
213         char **new_attrs;
214         int ret;
215
216         /* check if there's an extended dn control */
217         control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
218         if (control == NULL) {
219                 /* not found go on */
220                 return ldb_next_request(module, req);
221         }
222
223         extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
224         if (!extended_ctrl) {
225                 return LDB_ERR_PROTOCOL_ERROR;
226         }
227
228         ac = talloc(req, struct extended_context);
229         if (ac == NULL) {
230                 return LDB_ERR_OPERATIONS_ERROR;
231         }
232
233         ac->module = module;
234         ac->up_context = req->context;
235         ac->up_callback = req->callback;
236         ac->attrs = req->op.search.attrs;
237         ac->remove_guid = False;
238         ac->remove_sid = False;
239         ac->extended_type = extended_ctrl->type;
240
241         down_req = talloc_zero(req, struct ldb_request);
242         if (down_req == NULL) {
243                 return LDB_ERR_OPERATIONS_ERROR;
244         }
245
246         down_req->operation = req->operation;
247         down_req->op.search.base = req->op.search.base;
248         down_req->op.search.scope = req->op.search.scope;
249         down_req->op.search.tree = req->op.search.tree;
250
251         /* check if attrs only is specified, in that case check wether we need to modify them */
252         if (req->op.search.attrs) {
253                 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
254                         ac->remove_guid = True;
255                 }
256                 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
257                         ac->remove_sid = True;
258                 }
259                 if (ac->remove_guid || ac->remove_sid) {
260                         new_attrs = copy_attrs(down_req, req->op.search.attrs);
261                         if (new_attrs == NULL)
262                                 return LDB_ERR_OPERATIONS_ERROR;
263                         
264                         if (ac->remove_guid) {
265                                 if (!add_attrs(down_req, &new_attrs, "objectGUID"))
266                                         return LDB_ERR_OPERATIONS_ERROR;
267                         }
268                         if (ac->remove_sid) {
269                                 if (!add_attrs(down_req, &new_attrs, "objectSID"))
270                                         return LDB_ERR_OPERATIONS_ERROR;
271                         }
272
273                         down_req->op.search.attrs = (const char * const *)new_attrs;
274                 }
275         }
276
277         down_req->controls = req->controls;
278
279         /* save it locally and remove it from the list */
280         /* we do not need to replace them later as we
281          * are keeping the original req intact */
282         if (!save_controls(control, down_req, &saved_controls)) {
283                 return LDB_ERR_OPERATIONS_ERROR;
284         }
285
286         down_req->context = ac;
287         down_req->callback = extended_callback;
288         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
289
290         /* perform the search */
291         ret = ldb_next_request(module, down_req);
292
293         /* do not free down_req as the call results may be linked to it,
294          * it will be freed when the upper level request get freed */
295         if (ret == LDB_SUCCESS) {
296                 req->handle = down_req->handle;
297         }
298
299         return ret;
300 }
301
302 static int extended_init(struct ldb_module *module)
303 {
304         struct ldb_request *req;
305         int ret;
306
307         req = talloc(module, struct ldb_request);
308         if (req == NULL) {
309                 return LDB_ERR_OPERATIONS_ERROR;
310         }
311
312         req->operation = LDB_REQ_REGISTER_CONTROL;
313         req->op.reg_control.oid = LDB_CONTROL_EXTENDED_DN_OID;
314         req->controls = NULL;
315
316         ret = ldb_request(module->ldb, req);
317         if (ret != LDB_SUCCESS) {
318                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "extended_dn: Unable to register control with rootdse!\n");
319                 talloc_free(req);
320                 return LDB_ERR_OPERATIONS_ERROR;
321         }
322
323         talloc_free(req);
324         return ldb_next_init(module);
325 }
326
327 static const struct ldb_module_ops extended_dn_ops = {
328         .name              = "extended_dn",
329         .search            = extended_search,
330         .init_context      = extended_init
331 };
332
333 int ldb_extended_dn_init(void)
334 {
335         return ldb_register_module(&extended_dn_ops);
336 }