2 Unix SMB/CIFS mplementation.
5 Copyright (C) Stefan Metzmacher 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/ldap/ldap_client.h"
25 #include "lib/cmdline/popt_common.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "lib/ldb/include/ldb_errors.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "lib/util/dlinklist.h"
32 #include "torture/torture.h"
33 #include "torture/ldap/proto.h"
35 struct dsdb_attribute {
36 struct dsdb_attribute *prev, *next;
38 const char *lDAPDisplayName;
39 const char *attributeID;
41 struct GUID schemaIDGUID;
46 BOOL isMemberOfPartialAttributeSet;
48 const char *attributeSyntax;
55 BOOL showInAdvancedViewOnly;
56 const char *adminDisplayName;
57 const char *adminDescription;
60 struct dsdb_objectClass {
61 struct dsdb_objectClass *prev, *next;
63 const char *subClassOf;
65 const char *governsID;
68 BOOL showInAdvancedViewOnly;
69 const char *adminDisplayName;
70 const char *adminDescription;
72 uint32_t objectClassCategory;
73 const char *lDAPDisplayName;
75 struct GUID schemaIDGUID;
79 const char **systemPossSuperiors;
80 const char **systemMayContain;
82 const char **possSuperiors;
83 const char **mayContain;
85 const char *defaultSecurityDescriptor;
88 BOOL defaultHidingValue;
90 const char *defaultObjectCategory;
94 struct dsdb_attribute *attributes;
95 struct dsdb_objectClass *objectClasses;
99 const char *defaultdn;
101 const char *configdn;
102 const char *schemadn;
105 struct test_schema_ctx {
106 struct ldb_paged_control *ctrl;
110 int (*callback)(void *, struct ldb_context *ldb, struct ldb_message *);
114 static BOOL test_search_rootDSE(struct ldb_context *ldb, struct test_rootDSE *root)
117 struct ldb_message *msg;
118 struct ldb_result *r;
120 d_printf("Testing RootDSE Search\n");
122 ret = ldb_search(ldb, ldb_dn_new(ldb), LDB_SCOPE_BASE,
124 if (ret != LDB_SUCCESS) {
126 } else if (r->count != 1) {
133 root->defaultdn = ldb_msg_find_attr_as_string(msg, "defaultNamingContext", NULL);
134 talloc_steal(ldb, root->defaultdn);
135 root->rootdn = ldb_msg_find_attr_as_string(msg, "rootDomainNamingContext", NULL);
136 talloc_steal(ldb, root->rootdn);
137 root->configdn = ldb_msg_find_attr_as_string(msg, "configurationNamingContext", NULL);
138 talloc_steal(ldb, root->configdn);
139 root->schemadn = ldb_msg_find_attr_as_string(msg, "schemaNamingContext", NULL);
140 talloc_steal(ldb, root->schemadn);
147 static int test_schema_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
149 struct test_schema_ctx *actx = talloc_get_type(context, struct test_schema_ctx);
150 int ret = LDB_SUCCESS;
152 switch (ares->type) {
153 case LDB_REPLY_ENTRY:
155 ret = actx->callback(actx->private_data, ldb, ares->message);
158 case LDB_REPLY_REFERRAL:
162 if (ares->controls) {
163 struct ldb_paged_control *ctrl = NULL;
166 for (i=0; ares->controls[i]; i++) {
167 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ares->controls[i]->oid) == 0) {
168 ctrl = talloc_get_type(ares->controls[i]->data, struct ldb_paged_control);
175 talloc_free(actx->ctrl->cookie);
176 actx->ctrl->cookie = talloc_steal(actx->ctrl->cookie, ctrl->cookie);
177 actx->ctrl->cookie_len = ctrl->cookie_len;
179 if (actx->ctrl->cookie_len > 0) {
180 actx->pending = True;
186 d_printf("%s: unknown Reply Type %u\n", __location__, ares->type);
187 return LDB_ERR_OTHER;
190 if (talloc_free(ares) == -1) {
191 d_printf("talloc_free failed\n");
193 return LDB_ERR_OPERATIONS_ERROR;
197 return LDB_ERR_OPERATIONS_ERROR;
203 static BOOL test_create_schema_type(struct ldb_context *ldb, struct test_rootDSE *root,
205 int (*callback)(void *, struct ldb_context *ldb, struct ldb_message *),
208 struct ldb_control **ctrl;
209 struct ldb_paged_control *control;
210 struct ldb_request *req;
212 struct test_schema_ctx *actx;
214 req = talloc(ldb, struct ldb_request);
215 actx = talloc(req, struct test_schema_ctx);
217 ctrl = talloc_array(req, struct ldb_control *, 2);
218 ctrl[0] = talloc(ctrl, struct ldb_control);
219 ctrl[0]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
220 ctrl[0]->critical = True;
221 control = talloc(ctrl[0], struct ldb_paged_control);
222 control->size = 1000;
223 control->cookie = NULL;
224 control->cookie_len = 0;
225 ctrl[0]->data = control;
228 req->operation = LDB_SEARCH;
229 req->op.search.base = ldb_dn_explode(req, root->schemadn);
230 req->op.search.scope = LDB_SCOPE_SUBTREE;
231 req->op.search.tree = ldb_parse_tree(req, filter);
232 if (req->op.search.tree == NULL) return -1;
233 req->op.search.attrs = NULL;
234 req->controls = ctrl;
236 req->callback = test_schema_search_callback;
237 ldb_set_timeout(ldb, req, 0);
240 actx->ctrl = control;
241 actx->callback = callback;
242 actx->private_data = private_data;
244 actx->pending = False;
246 ret = ldb_request(ldb, req);
247 if (ret != LDB_SUCCESS) {
248 d_printf("search failed - %s\n", ldb_errstring(ldb));
252 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
253 if (ret != LDB_SUCCESS) {
254 d_printf("search error - %s\n", ldb_errstring(ldb));
261 d_printf("filter[%s] count[%u]\n", filter, actx->count);
265 #define GET_STRING(p, elem, strict) do { \
266 (p)->elem = samdb_result_string(msg, #elem, NULL);\
267 if (strict && (p)->elem == NULL) { \
268 d_printf("%s: %s == NULL\n", __location__, #elem); \
271 (void)talloc_steal(p, (p)->elem); \
274 #define GET_BOOL(p, elem, strict) do { \
276 str = samdb_result_string(msg, #elem, NULL);\
279 d_printf("%s: %s == NULL\n", __location__, #elem); \
284 } else if (strcasecmp("TRUE", str) == 0) { \
286 } else if (strcasecmp("FALSE", str) == 0) { \
289 d_printf("%s: %s == %s\n", __location__, #elem, str); \
294 #define GET_UINT32(p, elem) do { \
295 (p)->elem = samdb_result_uint(msg, #elem, 0);\
298 #define GET_GUID(p, elem) do { \
299 (p)->elem = samdb_result_guid(msg, #elem);\
302 static int test_add_attribute(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
304 struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
305 struct dsdb_attribute *attr;
307 attr = talloc_zero(schema, struct dsdb_attribute);
309 GET_STRING(attr, lDAPDisplayName, True);
310 GET_STRING(attr, attributeID, True);
311 attr->attID = UINT32_MAX;
312 GET_GUID(attr, schemaIDGUID);
314 GET_UINT32(attr, searchFlags);
315 GET_BOOL(attr, systemOnly, False);
316 GET_UINT32(attr, systemFlags);
317 GET_BOOL(attr, isMemberOfPartialAttributeSet, False);
319 GET_STRING(attr, attributeSyntax, True);
320 GET_UINT32(attr, oMSyntax);
322 GET_BOOL(attr, isSingleValued, True);
323 GET_UINT32(attr, rangeLower);
324 GET_UINT32(attr, rangeUpper);
326 GET_BOOL(attr, showInAdvancedViewOnly, False);
327 GET_STRING(attr, adminDisplayName, True);
328 GET_STRING(attr, adminDescription, True);
330 DLIST_ADD_END(schema->attributes, attr, struct dsdb_attribute *);
333 return LDB_ERR_OTHER;
336 static int test_add_class(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
338 struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
339 struct dsdb_objectClass *obj;
341 obj = talloc_zero(schema, struct dsdb_objectClass);
343 GET_STRING(obj, subClassOf, True);
345 GET_STRING(obj, governsID, True);
346 GET_STRING(obj, rDNAttID, True);
348 GET_BOOL(obj, showInAdvancedViewOnly, False);
349 GET_STRING(obj, adminDisplayName, True);
350 GET_STRING(obj, adminDescription, True);
352 GET_UINT32(obj, objectClassCategory);
353 GET_STRING(obj, lDAPDisplayName, True);
355 GET_GUID(obj, schemaIDGUID);
357 GET_BOOL(obj, systemOnly, False);
359 obj->systemPossSuperiors= NULL;
360 obj->systemMayContain = NULL;
362 obj->possSuperiors = NULL;
363 obj->mayContain = NULL;
365 GET_STRING(obj, defaultSecurityDescriptor, False);
367 GET_UINT32(obj, systemFlags);
368 GET_BOOL(obj, defaultHidingValue, True);
370 GET_STRING(obj, defaultObjectCategory, True);
372 DLIST_ADD_END(schema->objectClasses, obj, struct dsdb_objectClass *);
375 return LDB_ERR_OTHER;
378 static BOOL test_create_schema(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema **_schema)
381 struct dsdb_schema *schema;
383 schema = talloc_zero(ldb, struct dsdb_schema);
385 d_printf("Fetching attributes...\n");
386 ret &= test_create_schema_type(ldb, root, "(objectClass=attributeSchema)",
387 test_add_attribute, schema);
388 d_printf("Fetching objectClasses...\n");
389 ret &= test_create_schema_type(ldb, root, "(objectClass=classSchema)",
390 test_add_class, schema);
398 static BOOL test_dump_not_replicated(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
400 struct dsdb_attribute *a;
403 d_printf("Dumping not replicated attributes\n");
405 for (a=schema->attributes; a; a = a->next) {
406 if (!(a->systemFlags & 0x00000001)) continue;
407 d_printf("attr[%4u]: '%s'\n", a_i++,
414 static BOOL test_dump_partial(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
416 struct dsdb_attribute *a;
419 d_printf("Dumping attributes which are provided by the global catalog\n");
421 for (a=schema->attributes; a; a = a->next) {
422 if (!(a->systemFlags & 0x00000002) && !a->isMemberOfPartialAttributeSet) continue;
423 d_printf("attr[%4u]: %u %u '%s'\n", a_i++,
424 a->systemFlags & 0x00000002, a->isMemberOfPartialAttributeSet,
431 static BOOL test_dump_contructed(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
433 struct dsdb_attribute *a;
436 d_printf("Dumping constructed attributes\n");
438 for (a=schema->attributes; a; a = a->next) {
439 if (!(a->systemFlags & 0x00000004)) continue;
440 d_printf("attr[%4u]: '%s'\n", a_i++,
447 static BOOL test_dump_sorted_syntax(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
449 struct dsdb_attribute *a;
452 const char *syntaxes[] = {
473 for (i=0; i < ARRAY_SIZE(syntaxes); i++) {
474 for (a=schema->attributes; a; a = a->next) {
475 if (strcmp(syntaxes[i], a->attributeSyntax) != 0) continue;
476 d_printf("attr[%4u]: %s %u '%s'\n", a_i++,
477 a->attributeSyntax, a->oMSyntax,
485 BOOL torture_ldap_schema(struct torture_context *torture)
487 struct ldb_context *ldb;
490 const char *host = torture_setting_string(torture, "host", NULL);
492 struct test_rootDSE rootDSE;
493 struct dsdb_schema *schema = NULL;
495 mem_ctx = talloc_init("torture_ldap_basic");
496 ZERO_STRUCT(rootDSE);
498 url = talloc_asprintf(mem_ctx, "ldap://%s/", host);
500 ldb = ldb_wrap_connect(mem_ctx, url,
504 if (!ldb) goto failed;
506 ret &= test_search_rootDSE(ldb, &rootDSE);
507 if (!ret) goto failed;
508 ret &= test_create_schema(ldb, &rootDSE, &schema);
509 if (!ret) goto failed;
511 ret &= test_dump_not_replicated(ldb, &rootDSE, schema);
512 ret &= test_dump_partial(ldb, &rootDSE, schema);
513 ret &= test_dump_contructed(ldb, &rootDSE, schema);
514 ret &= test_dump_sorted_syntax(ldb, &rootDSE, schema);
517 talloc_free(mem_ctx);