s4:scheam quiet a 'const' warning
[ira/wip.git] / source4 / dsdb / schema / schema_init.c
index 3a65c474fb7813615cae59e9ae428d4cccd2941a..e741bc0ee5710dacf74be45f4e2612b56ae64369 100644 (file)
@@ -340,8 +340,12 @@ WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *s
                return status;
        }
 
+       talloc_free(schema->prefixes);
+       schema->prefixes = talloc_steal(schema, prefixes);
+       schema->num_prefixes = num_prefixes;
+
        /* Update prefixMap in ldb*/
-       status = dsdb_write_prefixes_to_ldb(mem_ctx, ldb, num_prefixes, prefixes);
+       status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
        if (!W_ERROR_IS_OK(status)) {
                DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
                        win_errstr(status)));
@@ -349,6 +353,9 @@ WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *s
                return status;
        }
 
+       DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
+                full_oid, num_prefixes));
+
        talloc_free(mem_ctx);
        return status;
 }
@@ -443,12 +450,13 @@ WERROR dsdb_find_prefix_for_oid(uint32_t num_prefixes, const struct dsdb_schema_
                return WERR_OK;
        }
 
+       DEBUG(5,(__location__ " Failed to find oid %s - have %u prefixes\n", in, num_prefixes));
+
        return WERR_DS_NO_MSDS_INTID;
 }
 
-WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
-                                 uint32_t num_prefixes,
-                                 const struct dsdb_schema_oid_prefix *prefixes)
+WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
+                                                    const struct dsdb_schema *schema)
 {
        struct ldb_message msg;
        struct ldb_dn *schema_dn;
@@ -461,12 +469,12 @@ WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
        
        schema_dn = samdb_schema_dn(ldb);
        if (!schema_dn) {
-               DEBUG(0,("dsdb_write_prefixes_to_ldb: no schema dn present\n"));        
+               DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));    
                return WERR_FOOBAR;
        }
 
        pm.version                      = PREFIX_MAP_VERSION_DSDB;
-       pm.ctr.dsdb.num_mappings        = num_prefixes;
+       pm.ctr.dsdb.num_mappings        = schema->num_prefixes;
        pm.ctr.dsdb.mappings            = talloc_array(mem_ctx,
                                                struct drsuapi_DsReplicaOIDMapping,
                                                pm.ctr.dsdb.num_mappings);
@@ -474,9 +482,9 @@ WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
                return WERR_NOMEM;
        }
 
-       for (i=0; i < num_prefixes; i++) {
-               pm.ctr.dsdb.mappings[i].id_prefix = prefixes[i].id>>16;
-               pm.ctr.dsdb.mappings[i].oid.oid = talloc_strdup(pm.ctr.dsdb.mappings, prefixes[i].oid);
+       for (i=0; i < schema->num_prefixes; i++) {
+               pm.ctr.dsdb.mappings[i].id_prefix = schema->prefixes[i].id>>16;
+               pm.ctr.dsdb.mappings[i].oid.oid = talloc_strdup(pm.ctr.dsdb.mappings, schema->prefixes[i].oid);
        }
 
        ndr_err = ndr_push_struct_blob(&ndr_blob, ldb,
@@ -498,7 +506,7 @@ WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
  
        ret = ldb_modify( ldb, &msg );
        if (ret != 0) {
-               DEBUG(0,("dsdb_write_prefixes_to_ldb: ldb_modify failed\n"));   
+               DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_modify failed\n"));       
                return WERR_FOOBAR;
        }
  
@@ -576,13 +584,29 @@ WERROR dsdb_read_prefixes_from_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
                (*prefixes)[i].id = blob->ctr.dsdb.mappings[i].id_prefix<<16;
                oid = talloc_strdup(mem_ctx, blob->ctr.dsdb.mappings[i].oid.oid);
                (*prefixes)[i].oid = talloc_asprintf_append(oid, "."); 
-               (*prefixes)[i].oid_len = strlen(blob->ctr.dsdb.mappings[i].oid.oid);
+               (*prefixes)[i].oid_len = strlen((*prefixes)[i].oid);
        }
 
        talloc_free(blob);
        return WERR_OK;
 }
 
+/*
+  this will be replaced with something that looks at the right part of
+  the schema once we know where unique indexing information is hidden
+ */
+static bool dsdb_schema_unique_attribute(const char *attr)
+{
+       const char *attrs[] = { "objectGUID", "objectSID" , NULL };
+       int i;
+       for (i=0;attrs[i];i++) {
+               if (strcasecmp(attr, attrs[i]) == 0) {
+                       return true;
+               }
+       }
+       return false;
+}
+
 
 /*
   setup the ldb_schema_attribute field for a dsdb_attribute
@@ -619,19 +643,33 @@ static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
        a->name = attr->lDAPDisplayName;
        a->flags = 0;
        a->syntax = s;
+
+       if (dsdb_schema_unique_attribute(a->name)) {
+               a->flags |= LDB_ATTR_FLAG_UNIQUE_INDEX;
+       }
        
        return LDB_SUCCESS;
 }
 
 
-
 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
-       (p)->elem = samdb_result_string(msg, attr, NULL);\
-       if (strict && (p)->elem == NULL) { \
-               d_printf("%s: %s == NULL\n", __location__, attr); \
-               return WERR_INVALID_PARAM; \
-       } \
-       talloc_steal(mem_ctx, (p)->elem); \
+       const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
+       if (get_string_val == NULL) { \
+               if (strict) {                                     \
+                       d_printf("%s: %s == NULL\n", __location__, attr); \
+                       return WERR_INVALID_PARAM;                      \
+               } else {                                                \
+                       (p)->elem = NULL;                               \
+               }                                                       \
+       } else {                                                        \
+               (p)->elem = talloc_strndup(mem_ctx,                     \
+                                          (const char *)get_string_val->data, \
+                                          get_string_val->length); \
+               if (!(p)->elem) {                                       \
+                       d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
+                       return WERR_NOMEM;                              \
+               }                                                       \
+       }                                                               \
 } while (0)
 
 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do {  \
@@ -1171,32 +1209,20 @@ static struct drsuapi_DsReplicaAttribute *dsdb_find_object_attr_name(struct dsdb
        } \
 } while (0)
 
-#define GET_STRING_LIST_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
-       int get_string_list_counter;                                    \
+#define GET_UINT32_LIST_DS(s, r, attr, mem_ctx, p, elem) do { \
+       int list_counter;                                       \
        struct drsuapi_DsReplicaAttribute *_a; \
        _a = dsdb_find_object_attr_name(s, r, attr, NULL); \
-       if (strict && !_a) { \
-               d_printf("%s: %s == NULL\n", __location__, attr); \
-               return WERR_INVALID_PARAM; \
-       } \
-       (p)->elem = _a ? talloc_array(mem_ctx, const char *, _a->value_ctr.num_values + 1) : NULL; \
-        for (get_string_list_counter=0;                                        \
-            _a && get_string_list_counter < _a->value_ctr.num_values;  \
-            get_string_list_counter++) {                               \
-               size_t _ret;                                            \
-               if (!convert_string_talloc_convenience(mem_ctx, s->iconv_convenience, CH_UTF16, CH_UNIX, \
-                                            _a->value_ctr.values[get_string_list_counter].blob->data, \
-                                            _a->value_ctr.values[get_string_list_counter].blob->length, \
-                                                      (void **)discard_const(&(p)->elem[get_string_list_counter]), &_ret, false)) { \
-                       DEBUG(0,("%s: invalid data!\n", attr)); \
-                       dump_data(0, \
-                                    _a->value_ctr.values[get_string_list_counter].blob->data, \
-                                    _a->value_ctr.values[get_string_list_counter].blob->length); \
-                       return WERR_FOOBAR; \
-               } \
-               (p)->elem[get_string_list_counter+1] = NULL;            \
+       (p)->elem = _a ? talloc_array(mem_ctx, uint32_t, _a->value_ctr.num_values + 1) : NULL; \
+        for (list_counter=0;                                   \
+            _a && list_counter < _a->value_ctr.num_values;     \
+            list_counter++) {                          \
+               if (_a->value_ctr.values[list_counter].blob->length != 4) { \
+                       return WERR_INVALID_PARAM;                      \
+               }                                                       \
+               (p)->elem[list_counter] = IVAL(_a->value_ctr.values[list_counter].blob->data, 0); \
        }                                                               \
-       talloc_steal(mem_ctx, (p)->elem);                               \
+       if (_a) (p)->elem[list_counter] = 0;                            \
 } while (0)
 
 #define GET_DN_DS(s, r, attr, mem_ctx, p, elem, strict) do { \
@@ -1412,19 +1438,18 @@ WERROR dsdb_class_from_drsuapi(struct dsdb_schema *schema,
        GET_STRING_DS(schema, r, "rDNAttID", mem_ctx, obj, rDNAttID, false);
        GET_DN_DS(schema, r, "defaultObjectCategory", mem_ctx, obj, defaultObjectCategory, true);
 
-       GET_STRING_DS(schema, r, "subClassOf", mem_ctx, obj, subClassOf, true);
-
+       GET_UINT32_DS(schema, r, "subClassOf", obj, subClassOf_id);
 
-       GET_STRING_LIST_DS(schema, r, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass, false);
-       GET_STRING_LIST_DS(schema, r, "auxiliaryClass", mem_ctx, obj, auxiliaryClass, false);
+       GET_UINT32_LIST_DS(schema, r, "systemAuxiliaryClass", mem_ctx, obj, systemAuxiliaryClass_ids);
+       GET_UINT32_LIST_DS(schema, r, "auxiliaryClass", mem_ctx, obj, auxiliaryClass_ids);
 
-       GET_STRING_LIST_DS(schema, r, "systemMustContain", mem_ctx, obj, systemMustContain, false);
-       GET_STRING_LIST_DS(schema, r, "systemMayContain", mem_ctx, obj, systemMayContain, false);
-       GET_STRING_LIST_DS(schema, r, "mustContain", mem_ctx, obj, mustContain, false);
-       GET_STRING_LIST_DS(schema, r, "mayContain", mem_ctx, obj, mayContain, false);
+       GET_UINT32_LIST_DS(schema, r, "systemMustContain", mem_ctx, obj, systemMustContain_ids);
+       GET_UINT32_LIST_DS(schema, r, "systemMayContain", mem_ctx, obj, systemMayContain_ids);
+       GET_UINT32_LIST_DS(schema, r, "mustContain", mem_ctx, obj, mustContain_ids);
+       GET_UINT32_LIST_DS(schema, r, "mayContain", mem_ctx, obj, mayContain_ids);
 
-       GET_STRING_LIST_DS(schema, r, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors, false);
-       GET_STRING_LIST_DS(schema, r, "possSuperiors", mem_ctx, obj, possSuperiors, false);
+       GET_UINT32_LIST_DS(schema, r, "systemPossSuperiors", mem_ctx, obj, systemPossSuperiors_ids);
+       GET_UINT32_LIST_DS(schema, r, "possSuperiors", mem_ctx, obj, possSuperiors_ids);
 
        GET_STRING_DS(schema, r, "defaultSecurityDescriptor", mem_ctx, obj, defaultSecurityDescriptor, false);