s4:torture:smb2: fix segfault on error condition in durable-open.reopen2 test
[Samba.git] / source4 / torture / drs / drs_util.c
blob7809e67103840293ce7e2b848287fc67f79b9d88
1 /*
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/>.
22 #include "includes.h"
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"
29 /**
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,
38 uint32_t attid,
39 const char **_oid,
40 int *map_idx)
42 int i;
43 uint32_t hi_word, lo_word;
44 DATA_BLOB bin_oid = {NULL, 0};
45 char *oid;
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];
55 torture_assert(tctx,
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 */
61 map_entry = NULL;
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;
67 break;
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);
79 if (lo_word < 128) {
80 bin_oid.length = bin_oid.length - 1;
81 bin_oid.data[bin_oid.length-1] = lo_word;
83 else {
84 if (lo_word >= 32768) {
85 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 */
91 torture_assert(tctx,
92 ber_read_OID_String(tctx, bin_oid, &oid),
93 "Failed to decode binary OID");
94 talloc_free(mem_ctx);
96 *_oid = oid;
98 return true;
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,
113 bool reload_schema)
115 int ret;
116 WERROR werr;
117 char *err_msg;
118 struct ldb_result *res;
119 struct ldb_dn *schema_dn;
120 struct dsdb_schema *ldap_schema;
122 ldap_schema = dsdb_get_schema(ldb, NULL);
123 if (ldap_schema && !reload_schema) {
124 return true;
127 schema_dn = ldb_get_schema_basedn(ldb);
128 torture_assert(tctx, schema_dn != NULL,
129 talloc_asprintf(tctx, "ldb_get_schema_basedn() failed: %s", ldb_errstring(ldb)));
131 ldap_schema = dsdb_new_schema(ldb);
132 torture_assert(tctx, ldap_schema != NULL, "dsdb_new_schema() failed!");
134 werr = dsdb_load_prefixmap_from_drsuapi(ldap_schema, mapping_ctr);
135 torture_assert_werr_ok(tctx, werr,
136 "Failed to construct prefixMap from drsuapi data");
139 * load the attribute and objectClass definitions
141 ret = ldb_search(ldb, ldap_schema, &res,
142 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
143 "(|(objectClass=attributeSchema)(objectClass=classSchema))");
144 if (ret != LDB_SUCCESS) {
145 err_msg = talloc_asprintf(tctx,
146 "failed to search attributeSchema or classSchema objects: %s",
147 ldb_errstring(ldb));
148 torture_fail(tctx, err_msg);
151 ret = dsdb_load_ldb_results_into_schema(tctx, ldb, ldap_schema, res, &err_msg);
152 if (ret != LDB_SUCCESS) {
153 err_msg = talloc_asprintf(tctx,
154 "dsdb_load_ldb_results_into_schema failed: %s",
155 err_msg);
156 torture_fail(tctx, err_msg);
159 talloc_free(res);
161 ret = dsdb_set_schema(ldb, ldap_schema);
162 if (ret != LDB_SUCCESS) {
163 torture_fail(tctx,
164 talloc_asprintf(tctx, "dsdb_set_schema() failed: %s", ldb_strerror(ret)));
167 return true;