1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/*
ldb database library
Copyright (C) Simo Sorce 2005-2008
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Name: ldb
*
* Component: ldb extended dn control module
*
* Description: this module builds a special dn
*
* Author: Simo Sorce
*/
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_errors.h"
#include "ldb/include/ldb_private.h"
#include "librpc/gen_ndr/ndr_misc.h"
#include "dsdb/samdb/samdb.h"
#include "libcli/security/security.h"
#include <time.h>
static bool is_attr_in_list(const char * const * attrs, const char *attr)
{
int i;
for (i = 0; attrs[i]; i++) {
if (strcasecmp(attrs[i], attr) == 0)
return true;
}
return false;
}
static char **copy_attrs(void *mem_ctx, const char * const * attrs)
{
char **new;
int i, num;
for (num = 0; attrs[num]; num++);
new = talloc_array(mem_ctx, char *, num + 1);
if (!new) return NULL;
for(i = 0; i < num; i++) {
new[i] = talloc_strdup(new, attrs[i]);
if (!new[i]) {
talloc_free(new);
return NULL;
}
}
new[i] = NULL;
return new;
}
static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
{
char **new;
int num;
for (num = 0; (*attrs)[num]; num++);
new = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
if (!new) return false;
*attrs = new;
new[num] = talloc_strdup(new, attr);
if (!new[num]) return false;
new[num + 1] = NULL;
return true;
}
static int inject_extended_dn(struct ldb_message *msg,
struct ldb_context *ldb,
int type,
bool remove_guid,
bool remove_sid)
{
const struct ldb_val *val;
struct GUID guid;
struct dom_sid *sid;
const DATA_BLOB *guid_blob;
const DATA_BLOB *sid_blob;
char *object_guid;
char *object_sid;
char *new_dn;
guid_blob = ldb_msg_find_ldb_val(msg, "objectGUID");
sid_blob = ldb_msg_find_ldb_val(msg, "objectSID");
if (!guid_blob) {
return LDB_ERR_OPERATIONS_ERROR;
}
switch (type) {
case 0:
/* return things in hexadecimal format */
if (sid_blob) {
const char *lower_guid_hex = strlower_talloc(msg, data_blob_hex_string(msg, guid_blob));
const char *lower_sid_hex = strlower_talloc(msg, data_blob_hex_string(msg, sid_blob));
if (!lower_guid_hex || !lower_sid_hex) {
return LDB_ERR_OPERATIONS_ERROR;
}
new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
lower_guid_hex,
lower_sid_hex,
ldb_dn_get_linearized(msg->dn));
} else {
const char *lower_guid_hex = strlower_talloc(msg, data_blob_hex_string(msg, guid_blob));
if (!lower_guid_hex) {
return LDB_ERR_OPERATIONS_ERROR;
}
new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
lower_guid_hex,
ldb_dn_get_linearized(msg->dn));
}
break;
case 1:
/* retrieve object_guid */
guid = samdb_result_guid(msg, "objectGUID");
object_guid = GUID_string(msg, &guid);
/* retrieve object_sid */
object_sid = NULL;
sid = samdb_result_dom_sid(msg, msg, "objectSID");
if (sid) {
object_sid = dom_sid_string(msg, sid);
if (!object_sid)
return LDB_ERR_OPERATIONS_ERROR;
}
/* Normal, sane format */
if (object_sid) {
new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
object_guid, object_sid,
ldb_dn_get_linearized(msg->dn));
} else {
new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
object_guid,
ldb_dn_get_linearized(msg->dn));
}
break;
default:
return LDB_ERR_OPERATIONS_ERROR;
}
if (!new_dn) {
return LDB_ERR_OPERATIONS_ERROR;
}
if (remove_guid) {
ldb_msg_remove_attr(msg, "objectGUID");
}
if (sid_blob && remove_sid) {
ldb_msg_remove_attr(msg, "objectSID");
}
msg->dn = ldb_dn_new(msg, ldb, new_dn);
if (! ldb_dn_validate(msg->dn))
return LDB_ERR_OPERATIONS_ERROR;
val = ldb_msg_find_ldb_val(msg, "distinguishedName");
if (val) {
ldb_msg_remove_attr(msg, "distinguishedName");
if (ldb_msg_add_steal_string(msg, "distinguishedName", new_dn))
return LDB_ERR_OPERATIONS_ERROR;
}
return LDB_SUCCESS;
}
/* search */
struct extended_context {
struct ldb_module *module;
struct ldb_request *req;
struct ldb_control *control;
struct ldb_dn *basedn;
char *wellknown_object;
bool inject;
bool remove_guid;
bool remove_sid;
int extended_type;
const char * const *cast_attrs;
};
static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
{
struct extended_context *ac;
int ret;
ac = talloc_get_type(req->context, struct extended_context);
if (!ares) {
return ldb_module_done(ac->req, NULL, NULL,
LDB_ERR_OPERATIONS_ERROR);
}
if (ares->error != LDB_SUCCESS) {
return ldb_module_done(ac->req, ares->controls,
ares->response, ares->error);
}
switch (ares->type) {
case LDB_REPLY_ENTRY:
if (ac->inject) {
/* for each record returned post-process to add any derived
attributes that have been asked for */
ret = inject_extended_dn(ares->message, ac->module->ldb,
ac->extended_type, ac->remove_guid,
ac->remove_sid);
if (ret != LDB_SUCCESS) {
return ldb_module_done(ac->req, NULL, NULL, ret);
}
}
return ldb_module_send_entry(ac->req, ares->message);
case LDB_REPLY_REFERRAL:
return ldb_module_send_referral(ac->req, ares->referral);
case LDB_REPLY_DONE:
return ldb_module_done(ac->req, ares->controls,
ares->response, LDB_SUCCESS);
}
return LDB_SUCCESS;
}
static int extended_base_callback(struct ldb_request *req, struct ldb_reply *ares)
{
struct extended_context *ac;
struct ldb_request *down_req;
struct ldb_control **saved_controls;
struct ldb_message_element *el;
int ret;
size_t i;
size_t wkn_len = 0;
char *valstr = NULL;
const char *found = NULL;
ac = talloc_get_type(req->context, struct extended_context);
if (!ares) {
return ldb_module_done(ac->req, NULL, NULL,
LDB_ERR_OPERATIONS_ERROR);
}
if (ares->error != LDB_SUCCESS) {
return ldb_module_done(ac->req, ares->controls,
ares->response, ares->error);
}
switch (ares->type) {
case LDB_REPLY_ENTRY:
if (!ac->wellknown_object) {
ac->basedn = ares->message->dn;
break;
}
wkn_len = strlen(ac->wellknown_object);
el = ldb_msg_find_element(ares->message, "wellKnownObjects");
if (!el) {
ac->basedn = NULL;
break;
}
for (i=0; i < el->num_values; i++) {
valstr = talloc_strndup(ac,
(const char *)el->values[i].data,
el->values[i].length);
if (!valstr) {
ldb_oom(ac->module->ldb);
return ldb_module_done(ac->req, NULL, NULL,
LDB_ERR_OPERATIONS_ERROR);
}
if (strncasecmp(valstr, ac->wellknown_object, wkn_len) != 0) {
talloc_free(valstr);
continue;
}
found = &valstr[wkn_len];
break;
}
if (!found) {
break;
}
ac->basedn = ldb_dn_new(ac, ac->module->ldb, found);
talloc_free(valstr);
if (!ac->basedn) {
ldb_oom(ac->module->ldb);
return ldb_module_done(ac->req, NULL, NULL,
LDB_ERR_OPERATIONS_ERROR);
}
break;
case LDB_REPLY_REFERRAL:
break;
case LDB_REPLY_DONE:
if (!ac->basedn) {
const char *str = talloc_asprintf(req, "Base-DN '%s' not found",
ldb_dn_get_linearized(ac->req->op.search.base));
ldb_set_errstring(ac->module->ldb, str);
return ldb_module_done(ac->req, NULL, NULL,
LDB_ERR_NO_SUCH_OBJECT);
}
ret = ldb_build_search_req_ex(&down_req,
ac->module->ldb, ac,
ac->basedn,
ac->req->op.search.scope,
ac->req->op.search.tree,
ac->cast_attrs,
ac->req->controls,
ac, extended_callback,
ac->req);
if (ret != LDB_SUCCESS) {
return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
}
if (ac->control) {
/* save it locally and remove it from the list */
/* we do not need to replace them later as we
* are keeping the original req intact */
if (!save_controls(ac->control, down_req, &saved_controls)) {
return ldb_module_done(ac->req, NULL, NULL,
LDB_ERR_OPERATIONS_ERROR);
}
}
/* perform the search */
return ldb_next_request(ac->module, down_req);
}
return LDB_SUCCESS;
}
static int extended_search(struct ldb_module *module, struct ldb_request *req)
{
struct ldb_control *control;
struct ldb_extended_dn_control *extended_ctrl = NULL;
struct ldb_control **saved_controls;
struct extended_context *ac;
struct ldb_request *down_req;
char **new_attrs;
int ret;
struct ldb_dn *base_dn = NULL;
enum ldb_scope base_dn_scope = LDB_SCOPE_BASE;
const char *base_dn_filter = NULL;
const char * const *base_dn_attrs = NULL;
char *wellknown_object = NULL;
static const char *dnattr[] = {
"distinguishedName",
NULL
};
static const char *wkattr[] = {
"wellKnownObjects",
NULL
};
if (ldb_dn_is_special(req->op.search.base)) {
char *dn;
dn = ldb_dn_alloc_linearized(req, req->op.search.base);
if (!dn) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
if (strncasecmp(dn, "<SID=", 5) == 0) {
char *str;
char *valstr;
char *p;
p = strchr(dn, '=');
if (!p) {
return LDB_ERR_INVALID_DN_SYNTAX;
}
p[0] = '\0';
p++;
str = p;
p = strchr(str, '>');
if (!p) {
return LDB_ERR_INVALID_DN_SYNTAX;
}
p[0] = '\0';
if (strncasecmp(str, "S-", 2) == 0) {
valstr = str;
} else {
DATA_BLOB binary;
binary = strhex_to_data_blob(NULL, str);
if (!binary.data) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
valstr = ldb_binary_encode(req, binary);
data_blob_free(&binary);
if (!valstr) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
}
/* TODO: do a search over all partitions */
base_dn = ldb_get_default_basedn(module->ldb);
base_dn_filter = talloc_asprintf(req, "(objectSid=%s)", valstr);
if (!base_dn_filter) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
base_dn_scope = LDB_SCOPE_SUBTREE;
base_dn_attrs = dnattr;
} else if (strncasecmp(dn, "<GUID=", 6) == 0) {
char *str;
char *valstr;
char *p;
p = strchr(dn, '=');
if (!p) {
return LDB_ERR_INVALID_DN_SYNTAX;
}
p[0] = '\0';
p++;
str = p;
p = strchr(str, '>');
if (!p) {
return LDB_ERR_INVALID_DN_SYNTAX;
}
p[0] = '\0';
if (strchr(str, '-')) {
valstr = str;
} else {
DATA_BLOB binary;
binary = strhex_to_data_blob(NULL, str);
if (!binary.data) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
valstr = ldb_binary_encode(req, binary);
data_blob_free(&binary);
if (!valstr) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
}
/* TODO: do a search over all partitions */
base_dn = ldb_get_default_basedn(module->ldb);
base_dn_filter = talloc_asprintf(req, "(objectGUID=%s)", valstr);
if (!base_dn_filter) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
base_dn_scope = LDB_SCOPE_SUBTREE;
base_dn_attrs = dnattr;
} else if (strncasecmp(dn, "<WKGUID=", 8) == 0) {
char *tail_str;
char *p;
p = strchr(dn, ',');
if (!p) {
return LDB_ERR_INVALID_DN_SYNTAX;
}
p[0] = '\0';
p++;
wellknown_object = talloc_asprintf(req, "B:32:%s:", &dn[8]);
if (!wellknown_object) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
tail_str = p;
p = strchr(tail_str, '>');
if (!p) {
return LDB_ERR_INVALID_DN_SYNTAX;
}
p[0] = '\0';
base_dn = ldb_dn_new(req, module->ldb, tail_str);
if (!base_dn) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
base_dn_filter = talloc_strdup(req, "(objectClass=*)");
if (!base_dn_filter) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
base_dn_scope = LDB_SCOPE_BASE;
base_dn_attrs = wkattr;
}
talloc_free(dn);
}
/* check if there's an extended dn control */
control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
if (control == NULL && base_dn_filter == NULL) {
/* not found go on */
return ldb_next_request(module, req);
}
if (control && control->data) {
extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
if (!extended_ctrl) {
return LDB_ERR_PROTOCOL_ERROR;
}
}
ac = talloc_zero(req, struct extended_context);
if (ac == NULL) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
ac->module = module;
ac->req = req;
ac->control = control;
ac->basedn = NULL;
ac->wellknown_object = wellknown_object;
ac->inject = false;
ac->remove_guid = false;
ac->remove_sid = false;
if (control) {
ac->inject = true;
if (extended_ctrl) {
ac->extended_type = extended_ctrl->type;
} else {
ac->extended_type = 0;
}
/* check if attrs only is specified, in that case check wether we need to modify them */
if (req->op.search.attrs) {
if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
ac->remove_guid = true;
}
if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
ac->remove_sid = true;
}
if (ac->remove_guid || ac->remove_sid) {
new_attrs = copy_attrs(ac, req->op.search.attrs);
if (new_attrs == NULL) {
ldb_oom(module->ldb);
return LDB_ERR_OPERATIONS_ERROR;
}
if (ac->remove_guid) {
if (!add_attrs(ac, &new_attrs, "objectGUID"))
return LDB_ERR_OPERATIONS_ERROR;
}
if (ac->remove_sid) {
if (!add_attrs(ac, &new_attrs, "objectSID"))
return LDB_ERR_OPERATIONS_ERROR;
}
ac->cast_attrs = (const char * const *)new_attrs;
} else {
ac->cast_attrs = req->op.search.attrs;
}
}
}
if (base_dn) {
ret = ldb_build_search_req(&down_req,
module->ldb, ac,
base_dn,
base_dn_scope,
base_dn_filter,
base_dn_attrs,
NULL,
ac, extended_base_callback,
req);
if (ret != LDB_SUCCESS) {
return LDB_ERR_OPERATIONS_ERROR;
}
/* perform the search */
return ldb_next_request(module, down_req);
}
ret = ldb_build_search_req_ex(&down_req,
module->ldb, ac,
req->op.search.base,
req->op.search.scope,
req->op.search.tree,
ac->cast_attrs,
req->controls,
ac, extended_callback,
req);
if (ret != LDB_SUCCESS) {
return LDB_ERR_OPERATIONS_ERROR;
}
if (ac->control) {
/* save it locally and remove it from the list */
/* we do not need to replace them later as we
* are keeping the original req intact */
if (!save_controls(control, down_req, &saved_controls)) {
return LDB_ERR_OPERATIONS_ERROR;
}
}
/* perform the search */
return ldb_next_request(module, down_req);
}
static int extended_init(struct ldb_module *module)
{
int ret;
ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
if (ret != LDB_SUCCESS) {
ldb_debug(module->ldb, LDB_DEBUG_ERROR,
"extended_dn: Unable to register control with rootdse!\n");
return LDB_ERR_OPERATIONS_ERROR;
}
return ldb_next_init(module);
}
_PUBLIC_ const struct ldb_module_ops ldb_extended_dn_module_ops = {
.name = "extended_dn",
.search = extended_search,
.init_context = extended_init
};
|