summaryrefslogtreecommitdiff
path: root/source4/dsdb/samdb/ldb_modules/resolve_oids.c
blob: 5ff246b1917dd191b6b2316e969e5d6bee73aa2a (plain)
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
/*
   ldb database library

   Copyright (C) Stefan Metzmacher <metze@samba.org> 2009

   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/>.
*/

#include "includes.h"
#include "ldb_module.h"
#include "dsdb/samdb/samdb.h"

static int resolve_oids_need_value(struct ldb_context *ldb,
				   struct dsdb_schema *schema,
				   const struct dsdb_attribute *a,
				   const struct ldb_val *valp)
{
	const struct dsdb_attribute *va = NULL;
	const struct dsdb_class *vo = NULL;
	const void *p2;
	char *str = NULL;

	if (a->syntax->oMSyntax != 6) {
		return LDB_ERR_COMPARE_FALSE;
	}

	if (valp) {
		p2 = memchr(valp->data, '.', valp->length);
	} else {
		p2 = NULL;
	}

	if (!p2) {
		return LDB_ERR_COMPARE_FALSE;
	}

	switch (a->attributeID_id) {
	case DRSUAPI_ATTRIBUTE_objectClass:
	case DRSUAPI_ATTRIBUTE_subClassOf:
	case DRSUAPI_ATTRIBUTE_auxiliaryClass:
	case DRSUAPI_ATTRIBUTE_systemPossSuperiors:
	case DRSUAPI_ATTRIBUTE_possSuperiors:
		str = talloc_strndup(ldb, (char *)valp->data, valp->length);
		if (!str) {
			ldb_oom(ldb);
			return LDB_ERR_OPERATIONS_ERROR;
		}
		vo = dsdb_class_by_governsID_oid(schema, str);
		talloc_free(str);
		if (!vo) {
			return LDB_ERR_COMPARE_FALSE;
		}
		return LDB_ERR_COMPARE_TRUE;
	case DRSUAPI_ATTRIBUTE_systemMustContain:
	case DRSUAPI_ATTRIBUTE_systemMayContain:
	case DRSUAPI_ATTRIBUTE_mustContain:
	case DRSUAPI_ATTRIBUTE_mayContain:
		str = talloc_strndup(ldb, (char *)valp->data, valp->length);
		if (!str) {
			ldb_oom(ldb);
			return LDB_ERR_OPERATIONS_ERROR;
		}
		va = dsdb_attribute_by_attributeID_oid(schema, str);
		talloc_free(str);
		if (!va) {
			return LDB_ERR_COMPARE_FALSE;
		}
		return LDB_ERR_COMPARE_TRUE;
	case DRSUAPI_ATTRIBUTE_governsID:
	case DRSUAPI_ATTRIBUTE_attributeID:
	case DRSUAPI_ATTRIBUTE_attributeSyntax:
		return LDB_ERR_COMPARE_FALSE;
	}

	return LDB_ERR_COMPARE_FALSE;
}

static int resolve_oids_parse_tree_need(struct ldb_context *ldb,
					struct dsdb_schema *schema,
					const struct ldb_parse_tree *tree)
{
	unsigned int i;
	const struct dsdb_attribute *a = NULL;
	const char *attr;
	const char *p1;
	const void *p2;
	const struct ldb_val *valp = NULL;
	int ret;

	switch (tree->operation) {
	case LDB_OP_AND:
	case LDB_OP_OR:
		for (i=0;i<tree->u.list.num_elements;i++) {
			ret = resolve_oids_parse_tree_need(ldb, schema,
						tree->u.list.elements[i]);
			if (ret != LDB_ERR_COMPARE_FALSE) {
				return ret;
			}
		}
		return LDB_ERR_COMPARE_FALSE;
	case LDB_OP_NOT:
		return resolve_oids_parse_tree_need(ldb, schema,
						tree->u.isnot.child);
	case LDB_OP_EQUALITY:
	case LDB_OP_GREATER:
	case LDB_OP_LESS:
	case LDB_OP_APPROX:
		attr = tree->u.equality.attr;
		valp = &tree->u.equality.value;
		break;
	case LDB_OP_SUBSTRING:
		attr = tree->u.substring.attr;
		break;
	case LDB_OP_PRESENT:
		attr = tree->u.present.attr;
		break;
	case LDB_OP_EXTENDED:
		attr = tree->u.extended.attr;
		valp = &tree->u.extended.value;
		break;
	default:
		return LDB_ERR_COMPARE_FALSE;
	}

	p1 = strchr(attr, '.');

	if (valp) {
		p2 = memchr(valp->data, '.', valp->length);
	} else {
		p2 = NULL;
	}

	if (!p1 && !p2) {
		return LDB_ERR_COMPARE_FALSE;
	}

	if (p1) {
		a = dsdb_attribute_by_attributeID_oid(schema, attr);
	} else {
		a = dsdb_attribute_by_lDAPDisplayName(schema, attr);
	}
	if (!a) {
		return LDB_ERR_COMPARE_FALSE;
	}

	if (!p2) {
		return LDB_ERR_COMPARE_FALSE;
	}

	if (a->syntax->oMSyntax != 6) {
		return LDB_ERR_COMPARE_FALSE;
	}

	return resolve_oids_need_value(ldb, schema, a, valp);
}

static int resolve_oids_element_need(struct ldb_context *ldb,
				     struct dsdb_schema *schema,
				     const struct ldb_message_element *el)
{
	unsigned int i;
	const struct dsdb_attribute *a = NULL;
	const char *p1;

	p1 = strchr(el->name, '.');

	if (p1) {
		a = dsdb_attribute_by_attributeID_oid(schema, el->name);
	} else {
		a = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
	}
	if (!a) {
		return LDB_ERR_COMPARE_FALSE;
	}

	for (i=0; i < el->num_values; i++) {
		int ret;
		ret = resolve_oids_need_value(ldb, schema, a,
					      &el->values[i]);
		if (ret != LDB_ERR_COMPARE_FALSE) {
			return ret;
		}
	}

	return LDB_ERR_COMPARE_FALSE;
}

static int resolve_oids_message_need(struct ldb_context *ldb,
				     struct dsdb_schema *schema,
				     const struct ldb_message *msg)
{
	int i;

	for (i=0; i < msg->num_elements; i++) {
		int ret;
		ret = resolve_oids_element_need(ldb, schema,
						&msg->elements[i]);
		if (ret != LDB_ERR_COMPARE_FALSE) {
			return ret;
		}
	}

	return LDB_ERR_COMPARE_FALSE;
}

static int resolve_oids_replace_value(struct ldb_context *ldb,
				      struct dsdb_schema *schema,
				      const struct dsdb_attribute *a,
				      struct ldb_val *valp)
{
	const struct dsdb_attribute *va = NULL;
	const struct dsdb_class *vo = NULL;
	const void *p2;
	char *str = NULL;

	if (a->syntax->oMSyntax != 6) {
		return LDB_SUCCESS;
	}

	if (valp) {
		p2 = memchr(valp->data, '.', valp->length);
	} else {
		p2 = NULL;
	}

	if (!p2) {
		return LDB_SUCCESS;
	}

	switch (a->attributeID_id) {
	case DRSUAPI_ATTRIBUTE_objectClass:
	case DRSUAPI_ATTRIBUTE_subClassOf:
	case DRSUAPI_ATTRIBUTE_auxiliaryClass:
	case DRSUAPI_ATTRIBUTE_systemPossSuperiors:
	case DRSUAPI_ATTRIBUTE_possSuperiors:
		str = talloc_strndup(schema, (char *)valp->data, valp->length);
		if (!str) {
			ldb_oom(ldb);
			return LDB_ERR_OPERATIONS_ERROR;
		}
		vo = dsdb_class_by_governsID_oid(schema, str);
		talloc_free(str);
		if (!vo) {
			return LDB_SUCCESS;
		}
		*valp = data_blob_string_const(vo->lDAPDisplayName);
		return LDB_SUCCESS;
	case DRSUAPI_ATTRIBUTE_systemMustContain:
	case DRSUAPI_ATTRIBUTE_systemMayContain:
	case DRSUAPI_ATTRIBUTE_mustContain:
	case DRSUAPI_ATTRIBUTE_mayContain:
		str = talloc_strndup(schema, (char *)valp->data, valp->length);
		if (!str) {
			ldb_oom(ldb);
			return LDB_ERR_OPERATIONS_ERROR;
		}
		va = dsdb_attribute_by_attributeID_oid(schema, str);
		talloc_free(str);
		if (!va) {
			return LDB_SUCCESS;
		}
		*valp = data_blob_string_const(va->lDAPDisplayName);
		return LDB_SUCCESS;
	case DRSUAPI_ATTRIBUTE_governsID:
	case DRSUAPI_ATTRIBUTE_attributeID:
	case DRSUAPI_ATTRIBUTE_attributeSyntax:
		return LDB_SUCCESS;
	}

	return LDB_SUCCESS;
}

static int resolve_oids_parse_tree_replace(struct ldb_context *ldb,
					   struct dsdb_schema *schema,
					   struct ldb_parse_tree *tree)
{
	unsigned int i;
	const struct dsdb_attribute *a = NULL;
	const char **attrp;
	const char *p1;
	const void *p2;
	struct ldb_val *valp = NULL;
	int ret;

	switch (tree->operation) {
	case LDB_OP_AND:
	case LDB_OP_OR:
		for (i=0;i<tree->u.list.num_elements;i++) {
			ret = resolve_oids_parse_tree_replace(ldb, schema,
							tree->u.list.elements[i]);
			if (ret != LDB_SUCCESS) {
				return ret;
			}
		}
		return LDB_SUCCESS;
	case LDB_OP_NOT:
		return resolve_oids_parse_tree_replace(ldb, schema,
						tree->u.isnot.child);
	case LDB_OP_EQUALITY:
	case LDB_OP_GREATER:
	case LDB_OP_LESS:
	case LDB_OP_APPROX:
		attrp = &tree->u.equality.attr;
		valp = &tree->u.equality.value;
		break;
	case LDB_OP_SUBSTRING:
		attrp = &tree->u.substring.attr;
		break;
	case LDB_OP_PRESENT:
		attrp = &tree->u.present.attr;
		break;
	case LDB_OP_EXTENDED:
		attrp = &tree->u.extended.attr;
		valp = &tree->u.extended.value;
		break;
	default:
		return LDB_SUCCESS;
	}

	p1 = strchr(*attrp, '.');

	if (valp) {
		p2 = memchr(valp->data, '.', valp->length);
	} else {
		p2 = NULL;
	}

	if (!p1 && !p2) {
		return LDB_SUCCESS;
	}

	if (p1) {
		a = dsdb_attribute_by_attributeID_oid(schema, *attrp);
	} else {
		a = dsdb_attribute_by_lDAPDisplayName(schema, *attrp);
	}
	if (!a) {
		return LDB_SUCCESS;
	}

	*attrp = a->lDAPDisplayName;

	if (!p2) {
		return LDB_SUCCESS;
	}

	if (a->syntax->oMSyntax != 6) {
		return LDB_SUCCESS;
	}

	return resolve_oids_replace_value(ldb, schema, a, valp);
}

static int resolve_oids_element_replace(struct ldb_context *ldb,
					struct dsdb_schema *schema,
					struct ldb_message_element *el)
{
	unsigned int i;
	const struct dsdb_attribute *a = NULL;
	const char *p1;

	p1 = strchr(el->name, '.');

	if (p1) {
		a = dsdb_attribute_by_attributeID_oid(schema, el->name);
	} else {
		a = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
	}
	if (!a) {
		return LDB_SUCCESS;
	}

	el->name = a->lDAPDisplayName;

	for (i=0; i < el->num_values; i++) {
		int ret;
		ret = resolve_oids_replace_value(ldb, schema, a,
						 &el->values[i]);
		if (ret != LDB_SUCCESS) {
			return ret;
		}
	}

	return LDB_SUCCESS;
}

static int resolve_oids_message_replace(struct ldb_context *ldb,
					struct dsdb_schema *schema,
					struct ldb_message *msg)
{
	unsigned int i;

	for (i=0; i < msg->num_elements; i++) {
		int ret;
		ret = resolve_oids_element_replace(ldb, schema,
						   &msg->elements[i]);
		if (ret != LDB_SUCCESS) {
			return ret;
		}
	}

	return LDB_SUCCESS;
}

struct resolve_oids_context {
	struct ldb_module *module;
	struct ldb_request *req;
};

static int resolve_oids_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	struct ldb_context *ldb;
	struct resolve_oids_context *ac;

	ac = talloc_get_type_abort(req->context, struct resolve_oids_context);
	ldb = ldb_module_get_ctx(ac->module);

	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:
		return ldb_module_send_entry(ac->req, ares->message, ares->controls);

	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 resolve_oids_search(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_context *ldb;
	struct dsdb_schema *schema;
	struct ldb_parse_tree *tree;
	struct ldb_request *down_req;
	struct resolve_oids_context *ac;
	int ret;
	bool needed = false;
	const char * const *attrs1;
	const char **attrs2;
	uint32_t i;

	ldb = ldb_module_get_ctx(module);
	schema = dsdb_get_schema(ldb);

	if (!schema) {
		return ldb_next_request(module, req);
	}

	/* do not manipulate our control entries */
	if (ldb_dn_is_special(req->op.search.base)) {
		return ldb_next_request(module, req);
	}

	ret = resolve_oids_parse_tree_need(ldb, schema,
					   req->op.search.tree);
	if (ret == LDB_ERR_COMPARE_TRUE) {
		needed = true;
	} else if (ret != LDB_ERR_COMPARE_FALSE) {
		return ret;
	}

	attrs1 = req->op.search.attrs;

	for (i=0; attrs1 && attrs1[i]; i++) {
		const char *p;
		struct dsdb_attribute *a;

		p = strchr(attrs1[i], '.');
		if (p == NULL) {
			continue;
		}

		a = dsdb_attribute_by_attributeID_oid(schema, attrs1[i]);
		if (a == NULL) {
			continue;
		}

		needed = true;
		break;
	}

	if (!needed) {
		return ldb_next_request(module, req);
	}

	ac = talloc(req, struct resolve_oids_context);
	if (ac == NULL) {
		ldb_oom(ldb);
		return LDB_ERR_OPERATIONS_ERROR;
	}
	ac->module = module;
	ac->req = req;

	tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
	if (!tree) {
		ldb_oom(ldb);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	ret = resolve_oids_parse_tree_replace(ldb, schema,
					      tree);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	attrs2 = str_list_copy_const(ac, req->op.search.attrs);
	if (req->op.search.attrs && !attrs2) {
		ldb_oom(ldb);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	for (i=0; attrs2 && attrs2[i]; i++) {
		const char *p;
		struct dsdb_attribute *a;

		p = strchr(attrs2[i], '.');
		if (p == NULL) {
			continue;
		}

		a = dsdb_attribute_by_attributeID_oid(schema, attrs2[i]);
		if (a == NULL) {
			continue;
		}

		attrs2[i] = a->lDAPDisplayName;
	}

	ret = ldb_build_search_req_ex(&down_req, ldb, ac,
				      req->op.search.base,
				      req->op.search.scope,
				      tree,
				      attrs2,
				      req->controls,
				      ac, resolve_oids_callback,
				      req);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	/* go on with the call chain */
	return ldb_next_request(module, down_req);
}

static int resolve_oids_add(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_context *ldb;
	struct dsdb_schema *schema;
	int ret;
	struct ldb_message *msg;
	struct ldb_request *down_req;
	struct resolve_oids_context *ac;

	ldb = ldb_module_get_ctx(module);
	schema = dsdb_get_schema(ldb);

	if (!schema) {
		return ldb_next_request(module, req);
	}

	/* do not manipulate our control entries */
	if (ldb_dn_is_special(req->op.add.message->dn)) {
		return ldb_next_request(module, req);
	}

	ret = resolve_oids_message_need(ldb, schema,
					req->op.add.message);
	if (ret == LDB_ERR_COMPARE_FALSE) {
		return ldb_next_request(module, req);
	} else if (ret != LDB_ERR_COMPARE_TRUE) {
		return ret;
	}

	ac = talloc(req, struct resolve_oids_context);
	if (ac == NULL) {
		ldb_oom(ldb);
		return LDB_ERR_OPERATIONS_ERROR;
	}
	ac->module = module;
	ac->req = req;

	msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
	if (!msg) {
		ldb_oom(ldb);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	ret = resolve_oids_message_replace(ldb, schema, msg);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	ret = ldb_build_add_req(&down_req, ldb, ac,
				msg,
				req->controls,
				ac, resolve_oids_callback,
				req);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	/* go on with the call chain */
	return ldb_next_request(module, down_req);
}

static int resolve_oids_modify(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_context *ldb;
	struct dsdb_schema *schema;
	int ret;
	struct ldb_message *msg;
	struct ldb_request *down_req;
	struct resolve_oids_context *ac;

	ldb = ldb_module_get_ctx(module);
	schema = dsdb_get_schema(ldb);

	if (!schema) {
		return ldb_next_request(module, req);
	}

	/* do not manipulate our control entries */
	if (ldb_dn_is_special(req->op.mod.message->dn)) {
		return ldb_next_request(module, req);
	}

	ret = resolve_oids_message_need(ldb, schema,
					req->op.mod.message);
	if (ret == LDB_ERR_COMPARE_FALSE) {
		return ldb_next_request(module, req);
	} else if (ret != LDB_ERR_COMPARE_TRUE) {
		return ret;
	}

	ac = talloc(req, struct resolve_oids_context);
	if (ac == NULL) {
		ldb_oom(ldb);
		return LDB_ERR_OPERATIONS_ERROR;
	}
	ac->module = module;
	ac->req = req;

	/* we have to copy the message as the caller might have it as a const */
	msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
	if (msg == NULL) {
		ldb_oom(ldb);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	ret = resolve_oids_message_replace(ldb, schema, msg);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	ret = ldb_build_mod_req(&down_req, ldb, ac,
				msg,
				req->controls,
				ac, resolve_oids_callback,
				req);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	/* go on with the call chain */
	return ldb_next_request(module, down_req);
}

_PUBLIC_ const struct ldb_module_ops ldb_resolve_oids_module_ops = {
	.name		= "resolve_oids",
	.search		= resolve_oids_search,
	.add		= resolve_oids_add,
	.modify		= resolve_oids_modify,
};