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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
/*
ldb database module
LDAP semantics mapping module
Copyright (C) Jelmer Vernooij 2005
Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
This module relies on ldb_map to do all the real work, but performs
some of the trivial mappings between AD semantics and that provided
by OpenLDAP and similar servers.
*/
#include "includes.h"
#include "ldb/include/ldb_module.h"
#include "ldb/ldb_map/ldb_map.h"
#include "librpc/gen_ndr/ndr_misc.h"
#include "librpc/ndr/libndr.h"
#include "dsdb/samdb/samdb.h"
struct entryuuid_private {
struct ldb_context *ldb;
struct ldb_dn **base_dns;
};
static struct ldb_val encode_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct GUID guid;
NTSTATUS status = GUID_from_data_blob(val, &guid);
enum ndr_err_code ndr_err;
struct ldb_val out = data_blob(NULL, 0);
if (!NT_STATUS_IS_OK(status)) {
return out;
}
ndr_err = ndr_push_struct_blob(&out, ctx, NULL, &guid,
(ndr_push_flags_fn_t)ndr_push_GUID);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
return out;
}
return out;
}
static struct ldb_val guid_always_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_val out = data_blob(NULL, 0);
struct GUID guid;
NTSTATUS status = GUID_from_data_blob(val, &guid);
if (!NT_STATUS_IS_OK(status)) {
return out;
}
return data_blob_string_const(GUID_string(ctx, &guid));
}
static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct GUID guid;
NTSTATUS status = NS_GUID_from_string((char *)val->data, &guid);
enum ndr_err_code ndr_err;
struct ldb_val out = data_blob(NULL, 0);
if (!NT_STATUS_IS_OK(status)) {
return out;
}
ndr_err = ndr_push_struct_blob(&out, ctx, NULL, &guid,
(ndr_push_flags_fn_t)ndr_push_GUID);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
return out;
}
return out;
}
static struct ldb_val guid_ns_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_val out = data_blob(NULL, 0);
struct GUID guid;
NTSTATUS status = GUID_from_data_blob(val, &guid);
if (!NT_STATUS_IS_OK(status)) {
return out;
}
return data_blob_string_const(NS_GUID_string(ctx, &guid));
}
/* The backend holds binary sids, so just copy them back */
static struct ldb_val val_copy(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_val out = data_blob(NULL, 0);
out = ldb_val_dup(ctx, val);
return out;
}
/* Ensure we always convert sids into binary, so the backend doesn't have to know about both forms */
static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_context *ldb = ldb_module_get_ctx(module);
struct ldb_val out = data_blob(NULL, 0);
const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, "objectSid");
if (a->syntax->canonicalise_fn(ldb, ctx, val, &out) != LDB_SUCCESS) {
return data_blob(NULL, 0);
}
return out;
}
/* Ensure we always convert objectCategory into a DN */
static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_context *ldb = ldb_module_get_ctx(module);
struct ldb_dn *dn;
struct ldb_val out = data_blob(NULL, 0);
const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, "objectCategory");
dn = ldb_dn_from_ldb_val(ctx, ldb, val);
if (dn && ldb_dn_validate(dn)) {
talloc_free(dn);
return val_copy(module, ctx, val);
}
talloc_free(dn);
if (a->syntax->canonicalise_fn(ldb, ctx, val, &out) != LDB_SUCCESS) {
return data_blob(NULL, 0);
}
return out;
}
static struct ldb_val normalise_to_signed32(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
long long int signed_ll = strtoll((const char *)val->data, NULL, 10);
if (signed_ll >= 0x80000000LL) {
union {
int32_t signed_int;
uint32_t unsigned_int;
} u = {
.unsigned_int = strtoul((const char *)val->data, NULL, 10)
};
struct ldb_val out = data_blob_string_const(talloc_asprintf(ctx, "%d", u.signed_int));
return out;
}
return val_copy(module, ctx, val);
}
static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_val out;
unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
time_t t = (usn >> 24);
out = data_blob_string_const(talloc_asprintf(ctx, "%s#%06x#00#000000", ldb_timestring(ctx, t), (unsigned int)(usn & 0xFFFFFF)));
return out;
}
static unsigned long long entryCSN_to_usn_int(TALLOC_CTX *ctx, const struct ldb_val *val)
{
char *entryCSN = talloc_strdup(ctx, (const char *)val->data);
char *mod_per_sec;
time_t t;
unsigned long long usn;
char *p;
if (!entryCSN) {
return 0;
}
p = strchr(entryCSN, '#');
if (!p) {
return 0;
}
p[0] = '\0';
p++;
mod_per_sec = p;
p = strchr(p, '#');
if (!p) {
return 0;
}
p[0] = '\0';
p++;
usn = strtol(mod_per_sec, NULL, 16);
t = ldb_string_to_time(entryCSN);
usn = usn | ((unsigned long long)t <<24);
return usn;
}
static struct ldb_val entryCSN_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_val out;
unsigned long long usn = entryCSN_to_usn_int(ctx, val);
out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
return out;
}
static struct ldb_val usn_to_timestamp(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_val out;
unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
time_t t = (usn >> 24);
out = data_blob_string_const(ldb_timestring(ctx, t));
return out;
}
static struct ldb_val timestamp_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
{
struct ldb_val out;
time_t t;
unsigned long long usn;
t = ldb_string_to_time((const char *)val->data);
usn = ((unsigned long long)t <<24);
out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
return out;
}
static const struct ldb_map_attribute entryuuid_attributes[] =
{
/* objectGUID */
{
.local_name = "objectGUID",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "entryUUID",
.convert_local = guid_always_string,
.convert_remote = encode_guid,
},
},
},
/* invocationId */
{
.local_name = "invocationId",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "invocationId",
.convert_local = guid_always_string,
.convert_remote = encode_guid,
},
},
},
/* objectSid */
{
.local_name = "objectSid",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "objectSid",
.convert_local = sid_always_binary,
.convert_remote = val_copy,
},
},
},
{
.local_name = "name",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "samba4RDN"
}
}
},
{
.local_name = "whenCreated",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "createTimestamp"
}
}
},
{
.local_name = "whenChanged",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "modifyTimestamp"
}
}
},
{
.local_name = "objectClasses",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "samba4ObjectClasses"
}
}
},
{
.local_name = "dITContentRules",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "samba4DITContentRules"
}
}
},
{
.local_name = "attributeTypes",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "samba4AttributeTypes"
}
}
},
{
.local_name = "objectCategory",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "objectCategory",
.convert_local = objectCategory_always_dn,
.convert_remote = val_copy,
},
},
},
{
.local_name = "distinguishedName",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "entryDN"
}
}
},
{
.local_name = "groupType",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "groupType",
.convert_local = normalise_to_signed32,
.convert_remote = val_copy,
},
}
},
{
.local_name = "sAMAccountType",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "sAMAccountType",
.convert_local = normalise_to_signed32,
.convert_remote = val_copy,
},
}
},
{
.local_name = "usnChanged",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "entryCSN",
.convert_local = usn_to_entryCSN,
.convert_remote = entryCSN_to_usn
},
},
},
{
.local_name = "usnCreated",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "createTimestamp",
.convert_local = usn_to_timestamp,
.convert_remote = timestamp_to_usn,
},
},
},
{
.local_name = "*",
.type = MAP_KEEP,
},
{
.local_name = NULL,
}
};
/* This objectClass conflicts with builtin classes on OpenLDAP */
const struct ldb_map_objectclass entryuuid_objectclasses[] =
{
{
.local_name = "subSchema",
.remote_name = "samba4SubSchema"
},
{
.local_name = NULL
}
};
/* These things do not show up in wildcard searches in OpenLDAP, but
* we need them to show up in the AD-like view */
static const char * const entryuuid_wildcard_attributes[] = {
"objectGUID",
"whenCreated",
"whenChanged",
"usnCreated",
"usnChanged",
"memberOf",
NULL
};
static const struct ldb_map_attribute nsuniqueid_attributes[] =
{
/* objectGUID */
{
.local_name = "objectGUID",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "nsuniqueid",
.convert_local = guid_ns_string,
.convert_remote = encode_ns_guid,
},
},
},
/* objectSid */
{
.local_name = "objectSid",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "objectSid",
.convert_local = sid_always_binary,
.convert_remote = val_copy,
},
},
},
{
.local_name = "whenCreated",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "createTimestamp"
}
}
},
{
.local_name = "whenChanged",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "modifyTimestamp"
}
}
},
{
.local_name = "objectCategory",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "objectCategory",
.convert_local = objectCategory_always_dn,
.convert_remote = val_copy,
},
},
},
{
.local_name = "distinguishedName",
.type = MAP_RENAME,
.u = {
.rename = {
.remote_name = "entryDN"
}
}
},
{
.local_name = "groupType",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "groupType",
.convert_local = normalise_to_signed32,
.convert_remote = val_copy,
},
}
},
{
.local_name = "sAMAccountType",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "sAMAccountType",
.convert_local = normalise_to_signed32,
.convert_remote = val_copy,
},
}
},
{
.local_name = "usnChanged",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "modifyTimestamp",
.convert_local = usn_to_timestamp,
.convert_remote = timestamp_to_usn,
},
},
},
{
.local_name = "usnCreated",
.type = MAP_CONVERT,
.u = {
.convert = {
.remote_name = "createTimestamp",
.convert_local = usn_to_timestamp,
.convert_remote = timestamp_to_usn,
},
},
},
{
.local_name = "*",
.type = MAP_KEEP,
},
{
.local_name = NULL,
}
};
/* These things do not show up in wildcard searches in OpenLDAP, but
* we need them to show up in the AD-like view */
static const char * const nsuniqueid_wildcard_attributes[] = {
"objectGUID",
"whenCreated",
"whenChanged",
"usnCreated",
"usnChanged",
NULL
};
/* the context init function */
static int entryuuid_init(struct ldb_module *module)
{
int ret;
ret = ldb_map_init(module, entryuuid_attributes, entryuuid_objectclasses, entryuuid_wildcard_attributes, "samba4Top", NULL);
if (ret != LDB_SUCCESS)
return ret;
return ldb_next_init(module);
}
/* the context init function */
static int nsuniqueid_init(struct ldb_module *module)
{
int ret;
ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, "extensibleObject", NULL);
if (ret != LDB_SUCCESS)
return ret;
return ldb_next_init(module);
}
static int get_seq_callback(struct ldb_request *req,
struct ldb_reply *ares)
{
unsigned long long *seq = (unsigned long long *)req->context;
if (!ares) {
return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
}
if (ares->error != LDB_SUCCESS) {
return ldb_request_done(req, ares->error);
}
if (ares->type == LDB_REPLY_ENTRY) {
struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
if (el) {
*seq = entryCSN_to_usn_int(ares, &el->values[0]);
}
}
if (ares->type == LDB_REPLY_DONE) {
return ldb_request_done(req, LDB_SUCCESS);
}
talloc_free(ares);
return LDB_SUCCESS;
}
static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_request *req)
{
struct ldb_context *ldb;
int ret;
struct map_private *map_private;
struct entryuuid_private *entryuuid_private;
unsigned long long seq_num = 0;
struct ldb_request *search_req;
const struct ldb_control *partition_ctrl;
const struct dsdb_control_current_partition *partition;
static const char *contextCSN_attr[] = {
"contextCSN", NULL
};
struct ldb_seqnum_request *seq;
struct ldb_seqnum_result *seqr;
struct ldb_extended *ext;
ldb = ldb_module_get_ctx(module);
seq = talloc_get_type(req->op.extended.data, struct ldb_seqnum_request);
map_private = talloc_get_type(ldb_module_get_private(module), struct map_private);
entryuuid_private = talloc_get_type(map_private->caller_private, struct entryuuid_private);
/* All this to get the DN of the parition, so we can search the right thing */
partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
if (!partition_ctrl) {
ldb_debug_set(ldb, LDB_DEBUG_FATAL,
"entryuuid_sequence_number: no current partition control found");
return LDB_ERR_CONSTRAINT_VIOLATION;
}
partition = talloc_get_type(partition_ctrl->data,
struct dsdb_control_current_partition);
SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
ret = ldb_build_search_req(&search_req, ldb, req,
partition->dn, LDB_SCOPE_BASE,
NULL, contextCSN_attr, NULL,
&seq_num, get_seq_callback,
NULL);
if (ret != LDB_SUCCESS) {
return ret;
}
ret = ldb_next_request(module, search_req);
if (ret == LDB_SUCCESS) {
ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
}
talloc_free(search_req);
if (ret != LDB_SUCCESS) {
return ret;
}
ext = talloc_zero(req, struct ldb_extended);
if (!ext) {
return LDB_ERR_OPERATIONS_ERROR;
}
seqr = talloc_zero(req, struct ldb_seqnum_result);
if (seqr == NULL) {
talloc_free(ext);
return LDB_ERR_OPERATIONS_ERROR;
}
ext->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
ext->data = seqr;
switch (seq->type) {
case LDB_SEQ_HIGHEST_SEQ:
seqr->seq_num = seq_num;
break;
case LDB_SEQ_NEXT:
seqr->seq_num = seq_num;
seqr->seq_num++;
break;
case LDB_SEQ_HIGHEST_TIMESTAMP:
{
seqr->seq_num = (seq_num >> 24);
break;
}
}
seqr->flags = 0;
seqr->flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
seqr->flags |= LDB_SEQ_GLOBAL_SEQUENCE;
/* send request done */
return ldb_module_done(req, NULL, ext, LDB_SUCCESS);
}
static int entryuuid_extended(struct ldb_module *module, struct ldb_request *req)
{
if (strcmp(req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
return entryuuid_sequence_number(module, req);
}
return ldb_next_request(module, req);
}
_PUBLIC_ const struct ldb_module_ops ldb_entryuuid_module_ops = {
.name = "entryuuid",
.init_context = entryuuid_init,
.extended = entryuuid_extended,
LDB_MAP_OPS
};
_PUBLIC_ const struct ldb_module_ops ldb_nsuniqueid_module_ops = {
.name = "nsuniqueid",
.init_context = nsuniqueid_init,
.extended = entryuuid_extended,
LDB_MAP_OPS
};
|