summaryrefslogtreecommitdiff
path: root/source4/dsdb/samdb/ldb_modules/resolve_oids.c
blob: f4d9eba17a4b1449878a7cf7354ada1c6fba9418 (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
/*
   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_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)
{
	int i;
	const struct dsdb_attribute *a = NULL;
	const char **attrp;
	const char *p1;
	const void *p2;
	struct ldb_val *valp = NULL;

	switch (tree->operation) {
	case LDB_OP_AND:
	case LDB_OP_OR:
		for (i=0;i<tree->u.list.num_elements;i++) {
			resolve_oids_parse_tree_replace(ldb, schema,
							tree->u.list.elements[i]);
		}
		return LDB_SUCCESS;
	case LDB_OP_NOT:
		resolve_oids_parse_tree_replace(ldb, schema,
						tree->u.isnot.child);
		return LDB_SUCCESS;
	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)
{
	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)
{
	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;

	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);
	}

	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;
	}

	ret = ldb_build_search_req_ex(&down_req, ldb, ac,
				      req->op.search.base,
				      req->op.search.scope,
				      tree,
				      req->op.search.attrs,
				      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);
	}

	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);
	}

	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,
};