source4/torture: Fix prototypes for all functions.
[nivanova/samba-autobuild/.git] / source4 / torture / drs / drs_util.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DRSUAPI utility functions to be used in torture tests
5
6    Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 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         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");
48
49         /* crack attid value */
50         hi_word = attid >> 16;
51         lo_word = attid & 0xFFFF;
52
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!");
59
60         /* locate corresponding prefixMap entry */
61         map_entry = NULL;
62         for (i = 0; i < prefix_map->num_mappings - 1; i++) {
63
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;
68                 }
69         }
70
71         torture_assert(tctx, map_entry, "Unable to locate corresponding Prefix Map entry");
72
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);
78
79         if (lo_word < 128) {
80                 bin_oid.length = bin_oid.length - 1;
81                 bin_oid.data[bin_oid.length-1] = lo_word;
82         }
83         else {
84                 if (lo_word >= 32768) {
85                         lo_word -= 32768;
86                 }
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
89         }
90
91         torture_assert(tctx,
92                         ber_read_OID_String(tctx, bin_oid, &oid),
93                         "Failed to decode binary OID");
94         talloc_free(mem_ctx);
95
96         *_oid = oid;
97
98         return true;
99 }
100
101
102 /**
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
107  *
108  * This function is to be used in tests that use GetNCChanges() function
109  */
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)
114 {
115         int i, ret;
116         WERROR werr;
117         const char *err_msg;
118         struct ldb_result *a_res;
119         struct ldb_result *c_res;
120         struct ldb_dn *schema_dn;
121         struct dsdb_schema *ldap_schema;
122
123         ldap_schema = dsdb_get_schema(ldb, NULL);
124         if (ldap_schema && !reload_schema) {
125                 return true;
126         }
127
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)));
131
132         ldap_schema = dsdb_new_schema(ldb);
133         torture_assert(tctx, ldap_schema != NULL, "dsdb_new_schema() failed!");
134
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");
138
139         /*
140          * load the attribute definitions
141          */
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",
148                                           ldb_errstring(ldb));
149                 torture_fail(tctx, err_msg);
150         }
151
152         /*
153          * load the objectClass definitions
154          */
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",
161                                           ldb_errstring(ldb));
162                 torture_fail(tctx, err_msg);
163         }
164
165         /* Build schema */
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)));
172         }
173
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)));
180         }
181
182         talloc_free(a_res);
183         talloc_free(c_res);
184
185         ret = dsdb_set_schema(ldb, ldap_schema);
186         if (ret != LDB_SUCCESS) {
187                 torture_fail(tctx,
188                              talloc_asprintf(tctx, "dsdb_set_schema() failed: %s", ldb_strerror(ret)));
189         }
190
191         return true;
192 }