LCOV - code coverage report
Current view: top level - source4/torture/drs - drs_util.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 18 53 34.0 %
Date: 2021-09-23 10:06:22 Functions: 1 2 50.0 %

          Line data    Source code
       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           0 : 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           0 :         DATA_BLOB bin_oid = {NULL, 0};
      44             :         char *oid;
      45           0 :         struct drsuapi_DsReplicaOIDMapping *map_entry = NULL;
      46           0 :         TALLOC_CTX *mem_ctx = talloc_named(tctx, 0, "util_drsuapi_oid_from_attid");
      47             : 
      48             :         /* crack attid value */
      49           0 :         hi_word = attid >> 16;
      50           0 :         lo_word = attid & 0xFFFF;
      51             : 
      52             :         /* check last entry in the prefix map is the special one */
      53           0 :         map_entry = &prefix_map->mappings[prefix_map->num_mappings-1];
      54           0 :         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           0 :         map_entry = NULL;
      61           0 :         for (i = 0; i < prefix_map->num_mappings - 1; i++) {
      62             : 
      63           0 :                 if (hi_word == prefix_map->mappings[i].id_prefix) {
      64           0 :                         map_entry = &prefix_map->mappings[i];
      65           0 :                         if (map_idx)    *map_idx = i;
      66           0 :                         break;
      67             :                 }
      68             :         }
      69             : 
      70           0 :         torture_assert(tctx, map_entry, "Unable to locate corresponding Prefix Map entry");
      71             : 
      72             :         /* copy partial oid making enough room */
      73           0 :         bin_oid.length = map_entry->oid.length + 2;
      74           0 :         bin_oid.data = talloc_array(mem_ctx, uint8_t, bin_oid.length);
      75           0 :         torture_assert(tctx, bin_oid.data, "Not enough memory");
      76           0 :         memcpy(bin_oid.data, map_entry->oid.binary_oid, map_entry->oid.length);
      77             : 
      78           0 :         if (lo_word < 128) {
      79           0 :                 bin_oid.length = bin_oid.length - 1;
      80           0 :                 bin_oid.data[bin_oid.length-1] = lo_word;
      81             :         }
      82             :         else {
      83           0 :                 if (lo_word >= 32768) {
      84           0 :                         lo_word -= 32768;
      85             :                 }
      86           0 :                 bin_oid.data[bin_oid.length-2] = ((lo_word / 128) % 128) + 128; /* (0x80 | ((lo_word>>7) & 0x7f)) */
      87           0 :                 bin_oid.data[bin_oid.length-1] = lo_word % 128; /* lo_word & 0x7f */
      88             :         }
      89             : 
      90           0 :         torture_assert(tctx,
      91             :                         ber_read_OID_String(tctx, bin_oid, &oid),
      92             :                         "Failed to decode binary OID");
      93           0 :         talloc_free(mem_ctx);
      94             : 
      95           0 :         *_oid = oid;
      96             : 
      97           0 :         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          42 : 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          42 :         ldap_schema = dsdb_get_schema(ldb, NULL);
     122          42 :         if (ldap_schema && !reload_schema) {
     123          36 :                 return true;
     124             :         }
     125             : 
     126           6 :         schema_dn = ldb_get_schema_basedn(ldb);
     127           6 :         torture_assert(tctx, schema_dn != NULL,
     128             :                        talloc_asprintf(tctx, "ldb_get_schema_basedn() failed: %s", ldb_errstring(ldb)));
     129             : 
     130           6 :         ldap_schema = dsdb_new_schema(ldb);
     131           6 :         torture_assert(tctx, ldap_schema != NULL, "dsdb_new_schema() failed!");
     132             : 
     133           6 :         werr = dsdb_load_prefixmap_from_drsuapi(ldap_schema, mapping_ctr);
     134           6 :         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           6 :         ret = ldb_search(ldb, ldap_schema, &res,
     141             :                          schema_dn, LDB_SCOPE_ONELEVEL, NULL,
     142             :                          "(|(objectClass=attributeSchema)(objectClass=classSchema))");
     143           6 :         if (ret != LDB_SUCCESS) {
     144           0 :                 err_msg = talloc_asprintf(tctx,
     145             :                                           "failed to search attributeSchema or classSchema objects: %s",
     146             :                                           ldb_errstring(ldb));
     147           0 :                 torture_fail(tctx, err_msg);
     148             :         }
     149             : 
     150           6 :         ret = dsdb_load_ldb_results_into_schema(tctx, ldb, ldap_schema, res, &err_msg);
     151           6 :         if (ret != LDB_SUCCESS) {
     152           0 :                 err_msg = talloc_asprintf(tctx,
     153             :                                           "dsdb_load_ldb_results_into_schema failed: %s",
     154             :                                           err_msg);
     155           0 :                 torture_fail(tctx, err_msg);
     156             :         }
     157             : 
     158           6 :         talloc_free(res);
     159             : 
     160           6 :         ret = dsdb_set_schema(ldb, ldap_schema, SCHEMA_WRITE);
     161           6 :         if (ret != LDB_SUCCESS) {
     162           0 :                 torture_fail(tctx,
     163             :                              talloc_asprintf(tctx, "dsdb_set_schema() failed: %s", ldb_strerror(ret)));
     164             :         }
     165             : 
     166           6 :         return true;
     167             : }

Generated by: LCOV version 1.13