2 Unix SMB/CIFS implementation.
4 DRSUAPI utility functions to be used in torture tests
6 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "torture/torture.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "torture/rpc/drsuapi.h"
26 #include "../lib/util/asn1.h"
27 #include "torture/drs/proto.h"
30 * Decode Attribute OID based on MS documentation
31 * See MS-DRSR.pdf - 5.16.4
33 * On success returns decoded OID and
34 * corresponding prefix_map index (if requested)
36 bool drs_util_oid_from_attid(struct torture_context *tctx,
37 const struct drsuapi_DsReplicaOIDMapping_Ctr *prefix_map,
43 uint32_t hi_word, lo_word;
44 DATA_BLOB bin_oid = {NULL, 0};
46 struct drsuapi_DsReplicaOIDMapping *map_entry = NULL;
47 TALLOC_CTX *mem_ctx = talloc_named(tctx, 0, "util_drsuapi_oid_from_attid");
49 /* crack attid value */
50 hi_word = attid >> 16;
51 lo_word = attid & 0xFFFF;
53 /* check last entry in the prefix map is the special one */
54 map_entry = &prefix_map->mappings[prefix_map->num_mappings-1];
56 (map_entry->id_prefix == 0)
57 && (*map_entry->oid.binary_oid == 0xFF),
58 "Last entry in Prefix Map is not the special one!");
60 /* locate corresponding prefixMap entry */
62 for (i = 0; i < prefix_map->num_mappings - 1; i++) {
64 if (hi_word == prefix_map->mappings[i].id_prefix) {
65 map_entry = &prefix_map->mappings[i];
66 if (map_idx) *map_idx = i;
71 torture_assert(tctx, map_entry, "Unable to locate corresponding Prefix Map entry");
73 /* copy partial oid making enough room */
74 bin_oid.length = map_entry->oid.length + 2;
75 bin_oid.data = talloc_array(mem_ctx, uint8_t, bin_oid.length);
76 torture_assert(tctx, bin_oid.data, "Not enough memory");
77 memcpy(bin_oid.data, map_entry->oid.binary_oid, map_entry->oid.length);
80 bin_oid.length = bin_oid.length - 1;
81 bin_oid.data[bin_oid.length-1] = lo_word;
84 if (lo_word >= 32768) {
87 bin_oid.data[bin_oid.length-2] = ((lo_word / 128) % 128) + 128; // (0x80 | ((lo_word>>7) & 0x7f))
88 bin_oid.data[bin_oid.length-1] = lo_word % 128; // lo_word & 0x7f
92 ber_read_OID_String(tctx, bin_oid, &oid),
93 "Failed to decode binary OID");
103 * Loads dsdb_schema from ldb connection using remote prefixMap.
104 * Schema will be loaded only if:
105 * - ldb has no attached schema
106 * - reload_schema is true
108 * This function is to be used in tests that use GetNCChanges() function
110 bool drs_util_dsdb_schema_load_ldb(struct torture_context *tctx,
111 struct ldb_context *ldb,
112 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
118 struct ldb_result *a_res;
119 struct ldb_result *c_res;
120 struct ldb_dn *schema_dn;
121 struct dsdb_schema *ldap_schema;
123 ldap_schema = dsdb_get_schema(ldb, NULL);
124 if (ldap_schema && !reload_schema) {
128 schema_dn = ldb_get_schema_basedn(ldb);
129 torture_assert(tctx, schema_dn != NULL,
130 talloc_asprintf(tctx, "ldb_get_schema_basedn() failed: %s", ldb_errstring(ldb)));
132 ldap_schema = dsdb_new_schema(ldb);
133 torture_assert(tctx, ldap_schema != NULL, "dsdb_new_schema() failed!");
135 werr = dsdb_load_prefixmap_from_drsuapi(ldap_schema, mapping_ctr);
136 torture_assert_werr_ok(tctx, werr,
137 "Failed to construct prefixMap from drsuapi data");
140 * load the attribute definitions
142 ret = ldb_search(ldb, ldap_schema, &a_res,
143 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
144 "(objectClass=attributeSchema)");
145 if (ret != LDB_SUCCESS) {
146 err_msg = talloc_asprintf(tctx,
147 "failed to search attributeSchema objects: %s",
149 torture_fail(tctx, err_msg);
153 * load the objectClass definitions
155 ret = ldb_search(ldb, ldap_schema, &c_res,
156 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
157 "(objectClass=classSchema)");
158 if (ret != LDB_SUCCESS) {
159 err_msg = talloc_asprintf(tctx,
160 "failed to search classSchema objects: %s",
162 torture_fail(tctx, err_msg);
166 for (i=0; i < a_res->count; i++) {
167 werr = dsdb_attribute_from_ldb(ldb, ldap_schema, a_res->msgs[i]);
168 torture_assert_werr_ok(tctx, werr,
169 talloc_asprintf(tctx,
170 "dsdb_attribute_from_ldb() failed for: %s",
171 ldb_dn_get_linearized(a_res->msgs[i]->dn)));
174 for (i=0; i < c_res->count; i++) {
175 werr = dsdb_class_from_ldb(ldap_schema, c_res->msgs[i]);
176 torture_assert_werr_ok(tctx, werr,
177 talloc_asprintf(tctx,
178 "dsdb_class_from_ldb() failed for: %s",
179 ldb_dn_get_linearized(c_res->msgs[i]->dn)));
185 ret = dsdb_set_schema(ldb, ldap_schema);
186 if (ret != LDB_SUCCESS) {
188 talloc_asprintf(tctx, "dsdb_set_schema() failed: %s", ldb_strerror(ret)));