s4/drs(tort): oid_from_attid() reference implementation
[Samba/gebeck_regimport.git] / source4 / torture / drs / drs_util.c
blobd4bfd8139056adc19d021a9a6f6e9d66f7d10436
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 "torture/rpc/drsuapi.h"
25 #include "../lib/util/asn1.h"
27 /**
28 * Decode Attribute OID based on MS documentation
29 * See MS-DRSR.pdf - 5.16.4
31 * On success returns decoded OID and
32 * corresponding prefix_map index (if requested)
34 bool drs_util_oid_from_attid(struct torture_context *tctx,
35 struct drsuapi_DsReplicaOIDMapping_Ctr *prefix_map,
36 uint32_t attid,
37 const char **_oid,
38 int *map_idx)
40 int i;
41 uint32_t hi_word, lo_word;
42 DATA_BLOB bin_oid = {NULL, 0};
43 struct drsuapi_DsReplicaOIDMapping *map_entry = NULL;
44 TALLOC_CTX *mem_ctx = talloc_named(tctx, 0, "util_drsuapi_oid_from_attid");
46 /* crack attid value */
47 hi_word = attid >> 16;
48 lo_word = attid & 0xFFFF;
50 /* check last entry in the prefix map is the special one */
51 map_entry = &prefix_map->mappings[prefix_map->num_mappings-1];
52 torture_assert(tctx,
53 (map_entry->id_prefix == 0)
54 && (*map_entry->oid.binary_oid == 0xFF),
55 "Last entry in Prefix Map is not the special one!");
57 /* locate correspoding prefixMap entry */
58 map_entry = NULL;
59 for (i = 0; i < prefix_map->num_mappings - 1; i++) {
61 if (hi_word == prefix_map->mappings[i].id_prefix) {
62 map_entry = &prefix_map->mappings[i];
63 if (map_idx) *map_idx = i;
64 break;
68 torture_assert(tctx, map_entry, "Unable to locate corresponding Prefix Map entry");
70 /* copy partial oid making enough room */
71 bin_oid.length = map_entry->oid.length + 2;
72 bin_oid.data = talloc_array(mem_ctx, uint8_t, bin_oid.length);
73 torture_assert(tctx, bin_oid.data, "Not enough memory");
74 memcpy(bin_oid.data, map_entry->oid.binary_oid, map_entry->oid.length);
76 if (lo_word < 128) {
77 bin_oid.length = bin_oid.length - 1;
78 bin_oid.data[bin_oid.length-1] = lo_word;
80 else {
81 if (lo_word == 32768) {
82 lo_word -= 32768;
84 bin_oid.data[bin_oid.length-2] = ((lo_word / 128) % 128) + 128; // (0x80 | ((lo_word>>7)&0x7f))
85 bin_oid.data[bin_oid.length-1] = lo_word % 128; // lo_word & 0x7f
88 torture_assert(tctx,
89 ber_read_OID_String(tctx, bin_oid, _oid),
90 "Failed to decode binary OID");
91 talloc_free(mem_ctx);
93 return true;