summaryrefslogtreecommitdiff
path: root/source4/torture/ldap/schema.c
blob: e7e50105fb23d34e9b5df72d385ddb54ec012bbd (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
/* 
   Unix SMB/CIFS mplementation.
   LDAP schema tests
   
   Copyright (C) Stefan Metzmacher 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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
*/

#include "includes.h"
#include "libcli/ldap/ldap_client.h"
#include "lib/cmdline/popt_common.h"
#include "db_wrap.h"
#include "lib/ldb/include/ldb.h"
#include "lib/ldb/include/ldb_errors.h"
#include "dsdb/samdb/samdb.h"
#include "dlinklist.h"

#include "torture/torture.h"
#include "torture/ldap/proto.h"

struct dsdb_attribute {
	struct dsdb_attribute *prev, *next;

	const char *lDAPDisplayName;
	const char *attributeID;
	uint32_t attID;
	struct GUID schemaIDGUID;

	uint32_t searchFlags;
	BOOL systemOnly;
	uint32_t systemFlags;
	BOOL isMemberOfPartialAttributeSet;

	const char *attributeSyntax;
	uint32_t oMSyntax;

	BOOL isSingleValued;
	uint32_t rangeLower;
	uint32_t rangeUpper;

	BOOL showInAdvancedViewOnly;
	const char *adminDisplayName;
	const char *adminDescription;
};

struct dsdb_objectClass {
	struct dsdb_objectClass *prev, *next;

	const char *subClassOf;

	const char *governsID;
	const char *rDNAttID;

	BOOL showInAdvancedViewOnly;
	const char *adminDisplayName;
	const char *adminDescription;

	uint32_t objectClassCategory;
	const char *lDAPDisplayName;

	struct GUID schemaIDGUID;

	BOOL systemOnly;

	const char **systemPossSuperiors;
	const char **systemMayContain;

	const char **possSuperiors;
	const char **mayContain;

	const char *defaultSecurityDescriptor;

	uint32_t systemFlags;
	BOOL defaultHidingValue;

	const char *defaultObjectCategory;
};

struct dsdb_schema {
	struct dsdb_attribute *attributes;
	struct dsdb_objectClass *objectClasses;
};

struct test_rootDSE {
	const char *defaultdn;
	const char *rootdn;
	const char *configdn;
	const char *schemadn;
};

struct test_schema_ctx {
	struct ldb_paged_control *ctrl;
	uint32_t count;
	BOOL pending;

	int (*callback)(void *, struct ldb_context *ldb, struct ldb_message *);
	void *private_data;
};

static BOOL test_search_rootDSE(struct ldb_context *ldb, struct test_rootDSE *root)
{
	int ret;
	struct ldb_message *msg;
	struct ldb_result *r;

	d_printf("Testing RootDSE Search\n");

	ret = ldb_search(ldb, ldb_dn_new(ldb), LDB_SCOPE_BASE, 
			 NULL, NULL, &r);
	if (ret != LDB_SUCCESS) {
		return False;
	} else if (r->count != 1) {
		return False;
	}

	msg = r->msgs[0];

	root->defaultdn	= ldb_msg_find_attr_as_string(msg, "defaultNamingContext", NULL);
	talloc_steal(ldb, root->defaultdn);
	root->rootdn	= ldb_msg_find_attr_as_string(msg, "rootDomainNamingContext", NULL);
	talloc_steal(ldb, root->rootdn);
	root->configdn	= ldb_msg_find_attr_as_string(msg, "configurationNamingContext", NULL);
	talloc_steal(ldb, root->configdn);
	root->schemadn	= ldb_msg_find_attr_as_string(msg, "schemaNamingContext", NULL);
	talloc_steal(ldb, root->schemadn);

	talloc_free(r);

	return True;
}

static int test_schema_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
{
	struct test_schema_ctx *actx = talloc_get_type(context, struct test_schema_ctx);
	int ret = LDB_SUCCESS;

	switch (ares->type) {
	case LDB_REPLY_ENTRY:
		actx->count++;
		ret = actx->callback(actx->private_data, ldb, ares->message);
		break;

	case LDB_REPLY_REFERRAL:
		break;

	case LDB_REPLY_DONE:
		if (ares->controls) {
			struct ldb_paged_control *ctrl = NULL;
			int i;

			for (i=0; ares->controls[i]; i++) {
				if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ares->controls[i]->oid) == 0) {
					ctrl = talloc_get_type(ares->controls[i]->data, struct ldb_paged_control);
					break;
				}
			}

			if (!ctrl) break;

			talloc_free(actx->ctrl->cookie);
			actx->ctrl->cookie = talloc_steal(actx->ctrl->cookie, ctrl->cookie);
			actx->ctrl->cookie_len = ctrl->cookie_len;

			if (actx->ctrl->cookie_len > 0) {
				actx->pending = True;
			}
		}
		break;
		
	default:
		d_printf("%s: unknown Reply Type %u\n", __location__, ares->type);
		return LDB_ERR_OTHER;
	}

	if (talloc_free(ares) == -1) {
		d_printf("talloc_free failed\n");
		actx->pending = 0;
		return LDB_ERR_OPERATIONS_ERROR;
	}

	if (ret) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	return LDB_SUCCESS;
}

static BOOL test_create_schema_type(struct ldb_context *ldb, struct test_rootDSE *root,
				    const char *filter,
				    int (*callback)(void *, struct ldb_context *ldb, struct ldb_message *),
				    void *private_data)
{
	struct ldb_control **ctrl;
	struct ldb_paged_control *control;
	struct ldb_request *req;
	int ret;
	struct test_schema_ctx *actx;

	req = talloc(ldb, struct ldb_request);
	actx = talloc(req, struct test_schema_ctx);

	ctrl = talloc_array(req, struct ldb_control *, 2);
	ctrl[0] = talloc(ctrl, struct ldb_control);
	ctrl[0]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
	ctrl[0]->critical = True;
	control = talloc(ctrl[0], struct ldb_paged_control);
	control->size = 1000;
	control->cookie = NULL;
	control->cookie_len = 0;
	ctrl[0]->data = control;
	ctrl[1] = NULL;

	req->operation = LDB_SEARCH;
	req->op.search.base = ldb_dn_explode(ldb, root->schemadn);
	req->op.search.scope = LDB_SCOPE_SUBTREE;
	req->op.search.tree = ldb_parse_tree(ldb, filter);
	if (req->op.search.tree == NULL) return -1;
	req->op.search.attrs = NULL;
	req->controls = ctrl;
	req->context = actx;
	req->callback = test_schema_search_callback;
	ldb_set_timeout(ldb, req, 0);

	actx->count		= 0;
	actx->ctrl		= control;
	actx->callback		= callback;
	actx->private_data	= private_data;
again:
	actx->pending		= False;

	ret = ldb_request(ldb, req);
	if (ret != LDB_SUCCESS) {
		d_printf("search failed - %s\n", ldb_errstring(ldb));
		return False;
	}

	ret = ldb_wait(req->handle, LDB_WAIT_ALL);
       	if (ret != LDB_SUCCESS) {
		d_printf("search error - %s\n", ldb_errstring(ldb));
		return False;
	}

	if (actx->pending)
		goto again;

	d_printf("filter[%s] count[%u]\n", filter, actx->count);
	return True;
}

#define GET_STRING(p, elem, strict) do { \
	(p)->elem = samdb_result_string(msg, #elem, NULL);\
	if (strict && (p)->elem == NULL) { \
		d_printf("%s: %s == NULL\n", __location__, #elem); \
		goto failed; \
	} \
	(void)talloc_steal(p, (p)->elem); \
} while (0)

#define GET_BOOL(p, elem, strict) do { \
	const char *str; \
	str = samdb_result_string(msg, #elem, NULL);\
	if (str == NULL) { \
		if (strict) { \
			d_printf("%s: %s == NULL\n", __location__, #elem); \
			goto failed; \
		} else { \
			(p)->elem = False; \
		} \
	} else if (strcasecmp("TRUE", str) == 0) { \
		(p)->elem = True; \
	} else if (strcasecmp("FALSE", str) == 0) { \
		(p)->elem = False; \
	} else { \
		d_printf("%s: %s == %s\n", __location__, #elem, str); \
		goto failed; \
	} \
} while (0)

#define GET_UINT32(p, elem) do { \
	(p)->elem = samdb_result_uint(msg, #elem, 0);\
} while (0)

#define GET_GUID(p, elem) do { \
	(p)->elem = samdb_result_guid(msg, #elem);\
} while (0)

static int test_add_attribute(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
{
	struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
	struct dsdb_attribute *attr;

	attr = talloc_zero(schema, struct dsdb_attribute);

	GET_STRING(attr, lDAPDisplayName, True);
	GET_STRING(attr, attributeID, True);
	attr->attID = UINT32_MAX;
	GET_GUID(attr, schemaIDGUID);

	GET_UINT32(attr, searchFlags);
	GET_BOOL(attr, systemOnly, False);
	GET_UINT32(attr, systemFlags);
	GET_BOOL(attr, isMemberOfPartialAttributeSet, False);

	GET_STRING(attr, attributeSyntax, True);
	GET_UINT32(attr, oMSyntax);

	GET_BOOL(attr, isSingleValued, True);
	GET_UINT32(attr, rangeLower);
	GET_UINT32(attr, rangeUpper);

	GET_BOOL(attr, showInAdvancedViewOnly, False);
	GET_STRING(attr, adminDisplayName, True);
	GET_STRING(attr, adminDescription, True);

	DLIST_ADD_END(schema->attributes, attr, struct dsdb_attribute *);
	return LDB_SUCCESS;
failed:
	return LDB_ERR_OTHER;
}

static int test_add_class(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
{
	struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
	struct dsdb_objectClass *obj;

	obj = talloc_zero(schema, struct dsdb_objectClass);

	GET_STRING(obj, subClassOf, True);

	GET_STRING(obj, governsID, True);
	GET_STRING(obj, rDNAttID, True);

	GET_BOOL(obj, showInAdvancedViewOnly, False);
	GET_STRING(obj, adminDisplayName, True);
	GET_STRING(obj, adminDescription, True);

	GET_UINT32(obj, objectClassCategory);
	GET_STRING(obj, lDAPDisplayName, True);

	GET_GUID(obj, schemaIDGUID);

	GET_BOOL(obj, systemOnly, False);

	obj->systemPossSuperiors= NULL;
	obj->systemMayContain	= NULL;

	obj->possSuperiors	= NULL;
	obj->mayContain		= NULL;

	GET_STRING(obj, defaultSecurityDescriptor, False);

	GET_UINT32(obj, systemFlags);
	GET_BOOL(obj, defaultHidingValue, True);

	GET_STRING(obj, defaultObjectCategory, True);

	DLIST_ADD_END(schema->objectClasses, obj, struct dsdb_objectClass *);
	return LDB_SUCCESS;
failed:
	return LDB_ERR_OTHER;
}

static BOOL test_create_schema(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema **_schema)
{
	BOOL ret = True;
	struct dsdb_schema *schema;

	schema = talloc_zero(ldb, struct dsdb_schema);

	d_printf("Fetching attributes...\n");
	ret &= test_create_schema_type(ldb, root, "(objectClass=attributeSchema)",
				       test_add_attribute, schema);
	d_printf("Fetching objectClasses...\n");
	ret &= test_create_schema_type(ldb, root, "(objectClass=classSchema)",
				       test_add_class, schema);

	if (ret == True) {
		*_schema = schema;
	}
	return ret;
}

static BOOL test_dump_not_replicated(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
{
	struct dsdb_attribute *a;
	uint32_t a_i = 1;

	d_printf("Dumping not replicated attributes\n");

	for (a=schema->attributes; a; a = a->next) {
		if (!(a->systemFlags & 0x00000001)) continue;
		d_printf("attr[%4u]: '%s'\n", a_i++,
			 a->lDAPDisplayName);
	}

	return True;
}

static BOOL test_dump_partial(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
{
	struct dsdb_attribute *a;
	uint32_t a_i = 1;

	d_printf("Dumping attributes which are provided by the global catalog\n");

	for (a=schema->attributes; a; a = a->next) {
		if (!(a->systemFlags & 0x00000002) && !a->isMemberOfPartialAttributeSet) continue;
		d_printf("attr[%4u]:  %u %u '%s'\n", a_i++,
			 a->systemFlags & 0x00000002, a->isMemberOfPartialAttributeSet,
			 a->lDAPDisplayName);
	}

	return True;
}

static BOOL test_dump_contructed(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
{
	struct dsdb_attribute *a;
	uint32_t a_i = 1;

	d_printf("Dumping constructed attributes\n");

	for (a=schema->attributes; a; a = a->next) {
		if (!(a->systemFlags & 0x00000004)) continue;
		d_printf("attr[%4u]: '%s'\n", a_i++,
			 a->lDAPDisplayName);
	}

	return True;
}

static BOOL test_dump_sorted_syntax(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
{
	struct dsdb_attribute *a;
	uint32_t a_i = 1;
	uint32_t i;
	const char *syntaxes[] = {
		"2.5.5.0",
		"2.5.5.1",
		"2.5.5.2",
		"2.5.5.3",
		"2.5.5.4",
		"2.5.5.5",
		"2.5.5.6",
		"2.5.5.7",
		"2.5.5.8",
		"2.5.5.9",
		"2.5.5.10",
		"2.5.5.11",
		"2.5.5.12",
		"2.5.5.13",
		"2.5.5.14",
		"2.5.5.15",
		"2.5.5.16",
		"2.5.5.17"
	};

	for (i=0; i < ARRAY_SIZE(syntaxes); i++) {
		for (a=schema->attributes; a; a = a->next) {
			if (strcmp(syntaxes[i], a->attributeSyntax) != 0) continue;
			d_printf("attr[%4u]: %s %u '%s'\n", a_i++,
				 a->attributeSyntax, a->oMSyntax,
				 a->lDAPDisplayName);
		}
	}

	return True;
}

BOOL torture_ldap_schema(struct torture_context *torture)
{
	struct ldb_context *ldb;
	TALLOC_CTX *mem_ctx;
	BOOL ret = True;
	const char *host = lp_parm_string(-1, "torture", "host");
	char *url;
	struct test_rootDSE rootDSE;
	struct dsdb_schema *schema = NULL;

	mem_ctx = talloc_init("torture_ldap_basic");
	ZERO_STRUCT(rootDSE);

	url = talloc_asprintf(mem_ctx, "ldap://%s/", host);

	ldb = ldb_wrap_connect(mem_ctx, url,
			       NULL,
			       cmdline_credentials,
			       0, NULL);
	if (!ldb) goto failed;

	ret &= test_search_rootDSE(ldb, &rootDSE);
	if (!ret) goto failed;
	ret &= test_create_schema(ldb, &rootDSE, &schema);
	if (!ret) goto failed;

	ret &= test_dump_not_replicated(ldb, &rootDSE, schema);
	ret &= test_dump_partial(ldb, &rootDSE, schema);
	ret &= test_dump_contructed(ldb, &rootDSE, schema);
	ret &= test_dump_sorted_syntax(ldb, &rootDSE, schema);

failed:
	talloc_free(mem_ctx);
	return ret;
}