r12427: Move SAMR CreateUser2 to transactions, and re-add support for
[samba.git] / source / dsdb / samdb / ldb_modules / samldb.c
index 906a2299f7ebb500f4bd80dcc0cd86b73750217a..a959cc9bb4d8fee2eb44970210500acdfd7f0f65 100644 (file)
 
 #include "includes.h"
 #include "lib/ldb/include/ldb.h"
+#include "lib/ldb/include/ldb_errors.h"
 #include "lib/ldb/include/ldb_private.h"
 #include "system/time.h"
 #include "librpc/gen_ndr/ndr_security.h"
 
 #define SAM_ACCOUNT_NAME_BASE "$000000-000000000000"
 
-static int samldb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
-                               enum ldb_scope scope, struct ldb_parse_tree *tree,
-                               const char * const *attrs, struct ldb_message ***res)
-{
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_search\n");
-       return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
-}
-
 /*
   allocate a new id, attempting to do it atomically
   return 0 on failure, the id on success
@@ -56,7 +49,7 @@ static int samldb_allocate_next_rid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx
                                   const struct ldb_dn *dn, uint32_t *id)
 {
        const char * const attrs[2] = { "nextRid", NULL };
-       struct ldb_message **res = NULL;
+       struct ldb_result *res = NULL;
        struct ldb_message msg;
        int ret;
        const char *str;
@@ -64,11 +57,11 @@ static int samldb_allocate_next_rid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx
        struct ldb_message_element els[2];
 
        ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, "nextRid=*", attrs, &res);
-       if (ret != 1) {
+       if (ret != LDB_SUCCESS || res->count != 1) {
                if (res) talloc_free(res);
                return -1;
        }
-       str = ldb_msg_find_string(res[0], "nextRid", NULL);
+       str = ldb_msg_find_string(res->msgs[0], "nextRid", NULL);
        if (str == NULL) {
                ldb_debug(ldb, LDB_DEBUG_FATAL, "attribute nextRid not found in %s\n", ldb_dn_linearize(res, dn));
                talloc_free(res);
@@ -107,17 +100,17 @@ static int samldb_allocate_next_rid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx
        els[1].flags = LDB_FLAG_MOD_ADD;
        els[1].name = els[0].name;
 
-       vals[0].data = talloc_asprintf(mem_ctx, "%u", *id);
+       vals[0].data = (uint8_t *)talloc_asprintf(mem_ctx, "%u", *id);
        if (!vals[0].data) {
                return -1;
        }
-       vals[0].length = strlen(vals[0].data);
+       vals[0].length = strlen((char *)vals[0].data);
 
-       vals[1].data = talloc_asprintf(mem_ctx, "%u", (*id)+1);
+       vals[1].data = (uint8_t *)talloc_asprintf(mem_ctx, "%u", (*id)+1);
        if (!vals[1].data) {
                return -1;
        }
-       vals[1].length = strlen(vals[1].data);
+       vals[1].length = strlen((char *)vals[1].data);
 
        ret = ldb_modify(ldb, &msg);
        if (ret != 0) {
@@ -133,23 +126,21 @@ static struct ldb_dn *samldb_search_domain(struct ldb_module *module, TALLOC_CTX
 {
        TALLOC_CTX *local_ctx;
        struct ldb_dn *sdn;
-       struct ldb_message **res = NULL;
+       struct ldb_result *res = NULL;
        int ret = 0;
 
-       local_ctx = talloc_named(mem_ctx, 0, "samldb_search_domain memory conext");
+       local_ctx = talloc_new(mem_ctx);
        if (local_ctx == NULL) return NULL;
 
        sdn = ldb_dn_copy(local_ctx, dn);
        do {
                ret = ldb_search(module->ldb, sdn, LDB_SCOPE_BASE, "objectClass=domain", NULL, &res);
-               talloc_free(res);
-
-               if (ret == 1)
+               talloc_steal(local_ctx, res);
+               if (ret == LDB_SUCCESS && res->count == 1)
                        break;
-
        } while ((sdn = ldb_dn_get_parent(local_ctx, sdn)));
 
-       if (ret != 1) {
+       if (ret != LDB_SUCCESS || res->count != 1) {
                talloc_free(local_ctx);
                return NULL;
        }
@@ -168,7 +159,7 @@ static struct dom_sid *samldb_get_new_sid(struct ldb_module *module,
                                          TALLOC_CTX *mem_ctx, const struct ldb_dn *obj_dn)
 {
        const char * const attrs[2] = { "objectSid", NULL };
-       struct ldb_message **res = NULL;
+       struct ldb_result *res = NULL;
        const struct ldb_dn *dom_dn;
        uint32_t rid;
        int ret;
@@ -191,13 +182,13 @@ static struct dom_sid *samldb_get_new_sid(struct ldb_module *module,
        /* find the domain sid */
 
        ret = ldb_search(module->ldb, dom_dn, LDB_SCOPE_BASE, "objectSid=*", attrs, &res);
-       if (ret != 1) {
+       if (ret != LDB_SUCCESS || res->count != 1) {
                ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_get_new_sid: error retrieving domain sid!\n");
                talloc_free(res);
                return NULL;
        }
 
-       dom_sid = samdb_result_dom_sid(res, res[0], "objectSid");
+       dom_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
        if (dom_sid == NULL) {
                ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_get_new_sid: error retrieving domain sid!\n");
                talloc_free(res);
@@ -240,7 +231,8 @@ static struct ldb_message_element *samldb_find_attribute(const struct ldb_messag
                                return &msg->elements[i];
                        }
                        for (j = 0; j < msg->elements[i].num_values; j++) {
-                               if (strcasecmp(value, msg->elements[i].values[j].data) == 0) {
+                               if (strcasecmp(value, 
+                                              (char *)msg->elements[i].values[j].data) == 0) {
                                        return &msg->elements[i];
                                }
                        }
@@ -260,7 +252,7 @@ static BOOL samldb_msg_add_string(struct ldb_module *module, struct ldb_message
                return False;
        }
 
-       if (ldb_msg_add_string(module->ldb, msg, aname, aval) != 0) {
+       if (ldb_msg_add_string(msg, aname, aval) != 0) {
                return False;
        }
 
@@ -276,7 +268,7 @@ static BOOL samldb_msg_add_sid(struct ldb_module *module, struct ldb_message *ms
        if (!NT_STATUS_IS_OK(status)) {
                return -1;
        }
-       return (ldb_msg_add_value(module->ldb, msg, name, &v) == 0);
+       return (ldb_msg_add_value(msg, name, &v) == 0);
 }
 
 static BOOL samldb_find_or_add_attribute(struct ldb_module *module, struct ldb_message *msg, const char *name, const char *value, const char *set_value)
@@ -289,17 +281,18 @@ static BOOL samldb_find_or_add_attribute(struct ldb_module *module, struct ldb_m
 
 static int samldb_copy_template(struct ldb_module *module, struct ldb_message *msg, const char *filter)
 {
-       struct ldb_message **res, *t;
+       struct ldb_result *res;
+       struct ldb_message *t;
        int ret, i, j;
        
 
        /* pull the template record */
        ret = ldb_search(module->ldb, NULL, LDB_SCOPE_SUBTREE, filter, NULL, &res);
-       if (ret != 1) {
-               ldb_debug(module->ldb, LDB_DEBUG_WARNING, "samldb: ERROR: template '%s' matched %d records\n", filter, ret);
+       if (ret != LDB_SUCCESS || res->count != 1) {
+               ldb_debug(module->ldb, LDB_DEBUG_WARNING, "samldb: ERROR: template '%s' matched too many records\n", filter);
                return -1;
        }
-       t = res[0];
+       t = res->msgs[0];
 
        for (i = 0; i < t->num_elements; i++) {
                struct ldb_message_element *el = &t->elements[i];
@@ -413,7 +406,7 @@ static struct ldb_message *samldb_fill_user_or_computer_object(struct ldb_module
        }
 
        if (samldb_find_attribute(msg, "objectclass", "computer") != NULL) {
-               if (samldb_copy_template(module, msg2, "(&(CN=TemplateMemberServer)(objectclass=userTemplate))") != 0) {
+               if (samldb_copy_template(module, msg2, "(&(CN=TemplateServer)(objectclass=userTemplate))") != 0) {
                        ldb_debug(module->ldb, LDB_DEBUG_WARNING, "samldb_fill_user_or_computer_object: Error copying computer template!\n");
                        return NULL;
                }
@@ -456,6 +449,10 @@ static struct ldb_message *samldb_fill_user_or_computer_object(struct ldb_module
                return NULL;
        }
 
+       /*
+         useraccountcontrol: setting value 0 gives 0x200 for users
+       */
+
        /* TODO: objectCategory, userAccountControl, badPwdCount, codePage, countryCode, badPasswordTime, lastLogoff, lastLogon, pwdLastSet, primaryGroupID, accountExpires, logonCount */
 
        return msg2;
@@ -497,7 +494,7 @@ static struct ldb_message *samldb_fill_foreignSecurityPrincipal_object(struct ld
        }
 
        if ((attribute = samldb_find_attribute(msg2, "objectSid", NULL)) == NULL ) {
-               struct dom_sid *sid = dom_sid_parse_talloc(msg2, rdn->value.data);
+               struct dom_sid *sid = dom_sid_parse_talloc(msg2, (char *)rdn->value.data);
                if (sid == NULL) {
                        ldb_debug(module->ldb, LDB_DEBUG_FATAL, "samldb_fill_foreignSecurityPrincipal_object: internal error! Can't parse sid in CN\n");
                        return NULL;
@@ -514,8 +511,9 @@ static struct ldb_message *samldb_fill_foreignSecurityPrincipal_object(struct ld
 }
 
 /* add_record */
-static int samldb_add_record(struct ldb_module *module, const struct ldb_message *msg)
+static int samldb_add(struct ldb_module *module, struct ldb_request *req)
 {
+       const struct ldb_message *msg = req->op.add.message;
        struct ldb_message *msg2 = NULL;
        int ret;
 
@@ -523,7 +521,7 @@ static int samldb_add_record(struct ldb_module *module, const struct ldb_message
 
        
        if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
-               return ldb_next_add_record(module, msg);
+               return ldb_next_request(module, req);
        }
 
        /* is user or computer?  add all relevant missing objects */
@@ -540,47 +538,39 @@ static int samldb_add_record(struct ldb_module *module, const struct ldb_message
        }
 
        if (msg2) {
-               ret = ldb_next_add_record(module, msg2);
+               req->op.add.message = msg2;
+               ret = ldb_next_request(module, req);
+               req->op.add.message = msg;
        } else {
-               ret = ldb_next_add_record(module, msg);
+               ret = ldb_next_request(module, req);
        }
 
        return ret;
 }
 
-/* modify_record: change modifyTimestamp as well */
-static int samldb_modify_record(struct ldb_module *module, const struct ldb_message *msg)
+static int samldb_destructor(void *module_ctx)
 {
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_modify_record\n");
-       return ldb_next_modify_record(module, msg);
+       /* struct ldb_module *ctx = module_ctx; */
+       /* put your clean-up functions here */
+       return 0;
 }
 
-static int samldb_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
+static int samldb_request(struct ldb_module *module, struct ldb_request *req)
 {
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_delete_record\n");
-       return ldb_next_delete_record(module, dn);
-}
+       switch (req->operation) {
 
-static int samldb_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
-{
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_rename_record\n");
-       return ldb_next_rename_record(module, olddn, newdn);
-}
+       case LDB_REQ_ADD:
+               return samldb_add(module, req);
 
-static int samldb_destructor(void *module_ctx)
-{
-       /* struct ldb_module *ctx = module_ctx; */
-       /* put your clean-up functions here */
-       return 0;
+       default:
+               return ldb_next_request(module, req);
+
+       }
 }
 
 static const struct ldb_module_ops samldb_ops = {
        .name          = "samldb",
-       .search_bytree = samldb_search_bytree,
-       .add_record    = samldb_add_record,
-       .modify_record = samldb_modify_record,
-       .delete_record = samldb_delete_record,
-       .rename_record = samldb_rename_record
+       .request       = samldb_request
 };