s4-dsdb: use ldb_msg_canonicalize_ex() in source4/lib/ldb/common/ldb.c
[kamenim/samba.git] / source4 / lib / ldb / common / ldb.c
index 20e32064ec8c22b58c741e13b32099050636d857..0d73d100d131a2a084edb18f8dde5b4ab999ebf3 100644 (file)
@@ -285,6 +285,22 @@ void ldb_reset_err_string(struct ldb_context *ldb)
        }
 }
 
+
+
+/*
+  set an ldb error based on file:line
+*/
+int ldb_error_at(struct ldb_context *ldb, int ecode,
+                const char *reason, const char *file, int line)
+{
+       if (reason == NULL) {
+               reason = ldb_strerror(ecode);
+       }
+       ldb_asprintf_errstring(ldb, "%s at %s:%d", reason, file, line);
+       return ecode;
+}
+
+
 #define FIRST_OP_NOERR(ldb, op) do { \
        module = ldb->modules;                                  \
        while (module && module->ops->op == NULL) module = module->next; \
@@ -382,8 +398,8 @@ int ldb_transaction_prepare_commit(struct ldb_context *ldb)
        if (status != LDB_SUCCESS) {
                /* if a module fails the prepare then we need
                   to call the end transaction for everyone */
-               FIRST_OP(ldb, end_transaction);
-               module->ops->end_transaction(module);
+               FIRST_OP(ldb, del_transaction);
+               module->ops->del_transaction(module);
                if (ldb->err_string == NULL) {
                        /* no error string was setup by the backend */
                        ldb_asprintf_errstring(ldb,
@@ -665,7 +681,7 @@ int ldb_request_get_status(struct ldb_request *req)
 static void ldb_trace_request(struct ldb_context *ldb, struct ldb_request *req)
 {
        TALLOC_CTX *tmp_ctx = talloc_new(req);
-       int i;
+       unsigned int i;
 
        switch (req->operation) {
        case LDB_SEARCH:
@@ -779,6 +795,16 @@ int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
                ret = module->ops->search(module, req);
                break;
        case LDB_ADD:
+               /* we have to canonicalise here, as so many places
+                * in modules and backends assume we don't have two
+                * elements with the same name */
+               ret = ldb_msg_canonicalize_ex(ldb, req->op.add.message,
+                                             (TALLOC_CTX*)req,
+                                             discard_const(&req->op.add.message));
+               if (ret != LDB_SUCCESS) {
+                       ldb_oom(ldb);
+                       return LDB_ERR_OPERATIONS_ERROR;
+               }
                FIRST_OP(ldb, add);
                ret = module->ops->add(module, req);
                break;
@@ -791,6 +817,16 @@ int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
                ret = module->ops->del(module, req);
                break;
        case LDB_RENAME:
+               if (!ldb_dn_validate(req->op.rename.olddn)) {
+                       ldb_asprintf_errstring(ldb, "ldb_rename: invalid olddn '%s'",
+                                              ldb_dn_get_linearized(req->op.rename.olddn));
+                       return LDB_ERR_INVALID_DN_SYNTAX;
+               }
+               if (!ldb_dn_validate(req->op.rename.newdn)) {
+                       ldb_asprintf_errstring(ldb, "ldb_rename: invalid newdn '%s'",
+                                              ldb_dn_get_linearized(req->op.rename.newdn));
+                       return LDB_ERR_INVALID_DN_SYNTAX;
+               }
                FIRST_OP(ldb, rename);
                ret = module->ops->rename(module, req);
                break;
@@ -826,7 +862,7 @@ int ldb_search_default_callback(struct ldb_request *req,
                                struct ldb_reply *ares)
 {
        struct ldb_result *res;
-       int n;
+       unsigned int n;
 
        res = talloc_get_type(req->context, struct ldb_result);
 
@@ -884,6 +920,54 @@ int ldb_search_default_callback(struct ldb_request *req,
        return LDB_SUCCESS;
 }
 
+int ldb_modify_default_callback(struct ldb_request *req, struct ldb_reply *ares)
+{
+       struct ldb_result *res;
+       unsigned int n;
+       int ret;
+
+       res = talloc_get_type(req->context, struct ldb_result);
+
+       if (!ares) {
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
+       }
+
+       if (ares->error != LDB_SUCCESS) {
+               ret = ares->error;
+               talloc_free(ares);
+               return ldb_request_done(req, ret);
+       }
+
+       switch (ares->type) {
+       case LDB_REPLY_REFERRAL:
+               if (res->refs) {
+                       for (n = 0; res->refs[n]; n++) /*noop*/ ;
+               } else {
+                       n = 0;
+               }
+
+               res->refs = talloc_realloc(res, res->refs, char *, n + 2);
+               if (! res->refs) {
+                       return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
+               }
+
+               res->refs[n] = talloc_move(res->refs, &ares->referral);
+               res->refs[n + 1] = NULL;
+               break;
+
+       case LDB_REPLY_DONE:
+               talloc_free(ares);
+               return ldb_request_done(req, LDB_SUCCESS);
+       default:
+               talloc_free(ares);
+               ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
+       }
+
+       talloc_free(ares);
+       return ldb_request_done(req, LDB_SUCCESS);
+}
+
 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
        int ret;