Debian Codegraph @

source4/torture/drs/drs_util.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 DRSUAPI utility functions to be used in torture tests
5
6 Copyright (C) Kamen Mazdrashki <[email protected]> 2009
7
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.
12
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.
17
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/>.
20 */
21
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"
28
29 /**
30 * Decode Attribute OID based on MS documentation
31 * See MS-DRSR.pdf - 5.16.4
32 *
33 * On success returns decoded OID and
34 * corresponding prefix_map index (if requested)
35 */
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)
41 {
42 uint32_t i, hi_word, lo_word;
43 DATA_BLOB bin_oid = {NULL, 0};
44 char *oid;
45 struct drsuapi_DsReplicaOIDMapping *map_entry = NULL;
46 TALLOC_CTX *mem_ctx = talloc_named(tctx, 0, "util_drsuapi_oid_from_attid");
47
48 /* crack attid value */
49 hi_word = attid >> 16;
50 lo_word = attid & 0xFFFF;
51
52 /* check last entry in the prefix map is the special one */
53 map_entry = &prefix_map->mappings[prefix_map->num_mappings-1];
54 torture_assert(tctx,
55 (map_entry->id_prefix == 0)
56 && (*map_entry->oid.binary_oid == 0xFF),
57 "Last entry in Prefix Map is not the special one!");
58
59 /* locate corresponding prefixMap entry */
60 map_entry = NULL;
61 for (i = 0; i < prefix_map->num_mappings - 1; i++) {
62
63 if (hi_word == prefix_map->mappings[i].id_prefix) {
64 map_entry = &prefix_map->mappings[i];
65 if (map_idx) *map_idx = i;
66 break;
67 }
68 }
69
70 torture_assert(tctx, map_entry, "Unable to locate corresponding Prefix Map entry");
71
72 /* copy partial oid making enough room */
73 bin_oid.length = map_entry->oid.length + 2;
74 bin_oid.data = talloc_array(mem_ctx, uint8_t, bin_oid.length);
75 torture_assert(tctx, bin_oid.data, "Not enough memory");
76 memcpy(bin_oid.data, map_entry->oid.binary_oid, map_entry->oid.length);
77
78 if (lo_word < 128) {
79 bin_oid.length = bin_oid.length - 1;
80 bin_oid.data[bin_oid.length-1] = lo_word;
81 }
82 else {
83 if (lo_word >= 32768) {
84 lo_word -= 32768;
85 }
86 bin_oid.data[bin_oid.length-2] = ((lo_word / 128) % 128) + 128; /* (0x80 | ((lo_word>>7) & 0x7f)) */
87 bin_oid.data[bin_oid.length-1] = lo_word % 128; /* lo_word & 0x7f */
88 }
89
90 torture_assert(tctx,
91 ber_read_OID_String(tctx, bin_oid, &oid),
92 "Failed to decode binary OID");
93 talloc_free(mem_ctx);
94
95 *_oid = oid;
96
97 return true;
98 }
99
100
101 /**
102 * Loads dsdb_schema from ldb connection using remote prefixMap.
103 * Schema will be loaded only if:
104 * - ldb has no attached schema
105 * - reload_schema is true
106 *
107 * This function is to be used in tests that use GetNCChanges() function
108 */
109 bool drs_util_dsdb_schema_load_ldb(struct torture_context *tctx,
110 struct ldb_context *ldb,
111 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
112 bool reload_schema)
113 {
114 int ret;
115 WERROR werr;
116 char *err_msg;
117 struct ldb_result *res;
118 struct ldb_dn *schema_dn;
119 struct dsdb_schema *ldap_schema;
120
121 ldap_schema = dsdb_get_schema(ldb, NULL);
122 if (ldap_schema && !reload_schema) {
123 return true;
124 }
125
126 schema_dn = ldb_get_schema_basedn(ldb);
127 torture_assert(tctx, schema_dn != NULL,
128 talloc_asprintf(tctx, "ldb_get_schema_basedn() failed: %s", ldb_errstring(ldb)));
129
130 ldap_schema = dsdb_new_schema(ldb);
131 torture_assert(tctx, ldap_schema != NULL, "dsdb_new_schema() failed!");
132
133 werr = dsdb_load_prefixmap_from_drsuapi(ldap_schema, mapping_ctr);
134 torture_assert_werr_ok(tctx, werr,
135 "Failed to construct prefixMap from drsuapi data");
136
137 /*
138 * load the attribute and objectClass definitions
139 */
140 ret = ldb_search(ldb, ldap_schema, &res,
141 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
142 "(|(objectClass=attributeSchema)(objectClass=classSchema))");
143 if (ret != LDB_SUCCESS) {
144 err_msg = talloc_asprintf(tctx,
145 "failed to search attributeSchema or classSchema objects: %s",
146 ldb_errstring(ldb));
147 torture_fail(tctx, err_msg);
148 }
149
150 ret = dsdb_load_ldb_results_into_schema(tctx, ldb, ldap_schema, res, &err_msg);
151 if (ret != LDB_SUCCESS) {
152 err_msg = talloc_asprintf(tctx,
153 "dsdb_load_ldb_results_into_schema failed: %s",
154 err_msg);
155 torture_fail(tctx, err_msg);
156 }
157
158 talloc_free(res);
159
160 ret = dsdb_set_schema(ldb, ldap_schema, SCHEMA_WRITE);
161 if (ret != LDB_SUCCESS) {
162 torture_fail(tctx,
163 talloc_asprintf(tctx, "dsdb_set_schema() failed: %s", ldb_strerror(ret)));
164 }
165
166 return true;
167 }