s4-pyldb: Use ldb_msg_diff_ex() in py_ldb_msg_diff()
[kamenim/samba.git] / source4 / lib / ldb / pyldb.c
index ce149ccd95854f064d2a7decf14904343e36768f..925ea5232aaf833be2475266ac2eabe851d73e5e 100644 (file)
@@ -5,8 +5,8 @@
 
    Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
    Copyright (C) 2006 Simo Sorce <idra@samba.org>
-   Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
-   Copyright (C) 2009 Matthias Dieter Wallnöfer
+   Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
+   Copyright (C) 2009-2010 Matthias Dieter Wallnöfer
 
         ** NOTE! The following LGPL license applies to the ldb
         ** library. This does NOT imply that all of Samba is released
@@ -80,34 +80,6 @@ static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
        return ret;
 }
 
-/**
- * Obtain a ldb DN from a Python object.
- *
- * @param mem_ctx Memory context
- * @param object Python object
- * @param ldb_ctx LDB context
- * @return Whether or not the conversion succeeded
- */
-bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 
-                  struct ldb_context *ldb_ctx, struct ldb_dn **dn)
-{
-       struct ldb_dn *odn;
-
-       if (ldb_ctx != NULL && PyString_Check(object)) {
-               odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
-               *dn = odn;
-               return true;
-       }
-
-       if (PyLdbDn_Check(object)) {
-               *dn = PyLdbDn_AsDn(object);
-               return true;
-       }
-
-       PyErr_SetString(PyExc_TypeError, "Expected DN");
-       return false;
-}
-
 /**
  * Create a Python object from a ldb_result.
  *
@@ -655,50 +627,55 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
        struct ldb_control **parsed_controls;
        struct ldb_message *msg;
        int ret;
+       TALLOC_CTX *mem_ctx;
+
        if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls))
                return NULL;
 
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
        ldb_ctx = PyLdb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
        } else {
-               const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
-               parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
+               const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
+               parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
                talloc_free(controls);
        }
 
        if (!PyLdbMessage_Check(py_msg)) {
                PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
+               talloc_free(mem_ctx);
                return NULL;
        }
        msg = PyLdbMessage_AsMessage(py_msg);
 
        ret = ldb_msg_sanity_check(ldb_ctx, msg);
-        if (ret != LDB_SUCCESS) {
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+       if (ret != LDB_SUCCESS) {
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               talloc_free(mem_ctx);
                return NULL;
-        }
-
-        ret = ldb_build_mod_req(&req, ldb_ctx, ldb_ctx,
-                                        msg,
-                                        parsed_controls,
-                                        NULL,
-                                        ldb_op_default_callback,
-                                        NULL);
+       }
 
+       ret = ldb_build_mod_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
+                               NULL, ldb_op_default_callback, NULL);
         if (ret != LDB_SUCCESS) {
                PyErr_SetString(PyExc_TypeError, "failed to build request");
+               talloc_free(mem_ctx);
                return NULL;
        }
 
         /* do request and autostart a transaction */
        /* Then let's LDB handle the message error in case of pb as they are meaningful */
 
-        ret = ldb_transaction_start(ldb_ctx);
-        if (ret != LDB_SUCCESS) {
-               talloc_free(req);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+       ret = ldb_transaction_start(ldb_ctx);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(mem_ctx);
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
         }
 
         ret = ldb_request(ldb_ctx, req);
@@ -715,8 +692,9 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
                        ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
                }
        }
-       talloc_free(req);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+
+       talloc_free(mem_ctx);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
 
        Py_RETURN_NONE;
 }
@@ -738,14 +716,19 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 
        if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls ))
                return NULL;
-       ldb_ctx = PyLdb_AsLdbContext(self);
 
        mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+       ldb_ctx = PyLdb_AsLdbContext(self);
+
        if (py_controls == Py_None) {
                parsed_controls = NULL;
        } else {
-               const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
-               parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
+               const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
+               parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
                talloc_free(controls);
        }
        if (PyDict_Check(py_msg)) {
@@ -793,18 +776,13 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
         
        ret = ldb_msg_sanity_check(ldb_ctx, msg);
         if (ret != LDB_SUCCESS) {
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
                talloc_free(mem_ctx);
                return NULL;
         }
 
-        ret = ldb_build_add_req(&req, ldb_ctx, ldb_ctx,
-                                        msg,
-                                        parsed_controls,
-                                        NULL,
-                                        ldb_op_default_callback,
-                                        NULL);
-
+        ret = ldb_build_add_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
+                               NULL, ldb_op_default_callback, NULL);
         if (ret != LDB_SUCCESS) {
                PyErr_SetString(PyExc_TypeError, "failed to build request");
                talloc_free(mem_ctx);
@@ -816,9 +794,8 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 
         ret = ldb_transaction_start(ldb_ctx);
         if (ret != LDB_SUCCESS) {
-               talloc_free(req);
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
         }
 
         ret = ldb_request(ldb_ctx, req);
@@ -835,9 +812,9 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
                        ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
                }
        }
-       talloc_free(req);
+
        talloc_free(mem_ctx);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
 
        Py_RETURN_NONE;
 }
@@ -847,9 +824,13 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
        PyObject *py_dn;
        struct ldb_dn *dn;
        int ret;
-       struct ldb_context *ldb;
+       struct ldb_context *ldb_ctx;
+       struct ldb_request *req;
+       PyObject *py_controls = Py_None;
        TALLOC_CTX *mem_ctx;
-       if (!PyArg_ParseTuple(args, "O", &py_dn))
+       struct ldb_control **parsed_controls;
+
+       if (!PyArg_ParseTuple(args, "O|O", &py_dn, &py_controls))
                return NULL;
 
        mem_ctx = talloc_new(NULL);
@@ -857,15 +838,55 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
                PyErr_NoMemory();
                return NULL;
        }
-       ldb = PyLdb_AsLdbContext(self);
-       if (!PyObject_AsDn(mem_ctx, py_dn, ldb, &dn)) {
+       ldb_ctx = PyLdb_AsLdbContext(self);
+
+       if (py_controls == Py_None) {
+               parsed_controls = NULL;
+       } else {
+               const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
+               parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
+               talloc_free(controls);
+       }
+
+       if (!PyObject_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
+               talloc_free(mem_ctx);
+               return NULL;
+       }
+
+       ret = ldb_build_del_req(&req, ldb_ctx, mem_ctx, dn, parsed_controls,
+                               NULL, ldb_op_default_callback, NULL);
+       if (ret != LDB_SUCCESS) {
+               PyErr_SetString(PyExc_TypeError, "failed to build request");
                talloc_free(mem_ctx);
                return NULL;
        }
 
-       ret = ldb_delete(ldb, dn);
+       /* do request and autostart a transaction */
+       /* Then let's LDB handle the message error in case of pb as they are meaningful */
+
+       ret = ldb_transaction_start(ldb_ctx);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(mem_ctx);
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+       }
+
+       ret = ldb_request(ldb_ctx, req);
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+       }
+
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_transaction_commit(ldb_ctx);
+       } else {
+               ldb_transaction_cancel(ldb_ctx);
+               if (ldb_ctx->err_string == NULL) {
+                       /* no error string was setup by the backend */
+                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
+               }
+       }
+
        talloc_free(mem_ctx);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
 
        Py_RETURN_NONE;
 }
@@ -877,6 +898,7 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
        int ret;
        struct ldb_context *ldb;
        TALLOC_CTX *mem_ctx;
+
        if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
                return NULL;
 
@@ -885,7 +907,9 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
                PyErr_NoMemory();
                return NULL;
        }
+
        ldb = PyLdb_AsLdbContext(self);
+
        if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
                talloc_free(mem_ctx);
                return NULL;
@@ -1011,9 +1035,11 @@ static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
 
 static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
 {
+       int ldb_ret;
        PyObject *py_msg_old;
        PyObject *py_msg_new;
        struct ldb_message *diff;
+       struct ldb_context *ldb;
        PyObject *py_ret;
 
        if (!PyArg_ParseTuple(args, "OO", &py_msg_old, &py_msg_new))
@@ -1029,14 +1055,20 @@ static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
                return NULL;
        }
 
-       diff = ldb_msg_diff(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg_old), PyLdbMessage_AsMessage(py_msg_new));
-       if (!diff) {
+       ldb = PyLdb_AsLdbContext(self);
+       ldb_ret = ldb_msg_diff_ex(ldb,
+                                 PyLdbMessage_AsMessage(py_msg_old),
+                                 PyLdbMessage_AsMessage(py_msg_new),
+                                 (TALLOC_CTX*)ldb, &diff);
+       if (ldb_ret != LDB_SUCCESS) {
                PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff");
                return NULL;
        }
 
        py_ret = PyLdbMessage_FromMessage(diff);
 
+       talloc_unlink(ldb, diff);
+
        return py_ret;
 }
 
@@ -1092,6 +1124,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        struct ldb_control **parsed_controls;
        struct ldb_dn *base;
        PyObject *py_ret;
+       TALLOC_CTX *mem_ctx;
 
        /* type "int" rather than "enum" for "scope" is intentional */
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
@@ -1099,14 +1132,22 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
                                         &py_base, &scope, &expr, &py_attrs, &py_controls))
                return NULL;
 
+
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
        ldb_ctx = PyLdb_AsLdbContext(self);
 
        if (py_attrs == Py_None) {
                attrs = NULL;
        } else {
-               attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
-               if (attrs == NULL)
+               attrs = PyList_AsStringList(mem_ctx, py_attrs, "attrs");
+               if (attrs == NULL) {
+                       talloc_free(mem_ctx);
                        return NULL;
+               }
        }
 
        if (py_base == Py_None) {
@@ -1121,19 +1162,19 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        if (py_controls == Py_None) {
                parsed_controls = NULL;
        } else {
-               const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
-               parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
+               const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
+               parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
                talloc_free(controls);
        }
 
-       res = talloc_zero(ldb_ctx, struct ldb_result);
+       res = talloc_zero(mem_ctx, struct ldb_result);
        if (res == NULL) {
                PyErr_NoMemory();
-               talloc_free(attrs);
+               talloc_free(mem_ctx);
                return NULL;
        }
 
-       ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
+       ret = ldb_build_search_req(&req, ldb_ctx, mem_ctx,
                                   base,
                                   scope,
                                   expr,
@@ -1146,7 +1187,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        talloc_steal(req, attrs);
 
        if (ret != LDB_SUCCESS) {
-               talloc_free(res);
+               talloc_free(mem_ctx);
                PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
@@ -1157,17 +1198,15 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
                ret = ldb_wait(req->handle, LDB_WAIT_ALL);
        }
 
-       talloc_free(req);
-
        if (ret != LDB_SUCCESS) {
-               talloc_free(res);
+               talloc_free(mem_ctx);
                PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
        py_ret = PyLdbResult_FromResult(res);
 
-       talloc_free(res);
+       talloc_free(mem_ctx);
 
        return py_ret;
 }
@@ -2107,6 +2146,43 @@ static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
        self->ob_type->tp_free(self);
 }
 
+static int py_ldb_msg_compare(PyLdbMessageObject *py_msg1,
+                             PyLdbMessageObject *py_msg2)
+{
+       struct ldb_message *msg1 = PyLdbMessage_AsMessage(py_msg1),
+                          *msg2 = PyLdbMessage_AsMessage(py_msg2);
+       unsigned int i;
+       int ret;
+
+       if ((msg1->dn != NULL) || (msg2->dn != NULL)) {
+               ret = ldb_dn_compare(msg1->dn, msg2->dn);
+               if (ret != 0) {
+                       return ret;
+               }
+       }
+
+       ret = msg1->num_elements - msg2->num_elements;
+       if (ret != 0) {
+               return ret;
+       }
+
+       for (i = 0; i < msg1->num_elements; i++) {
+               ret = ldb_msg_element_compare_name(&msg1->elements[i],
+                                                  &msg2->elements[i]);
+               if (ret != 0) {
+                       return ret;
+               }
+
+               ret = ldb_msg_element_compare(&msg1->elements[i],
+                                             &msg2->elements[i]);
+               if (ret != 0) {
+                       return ret;
+               }
+       }
+
+       return 0;
+}
+
 PyTypeObject PyLdbMessage = {
        .tp_name = "ldb.Message",
        .tp_methods = py_ldb_msg_methods,
@@ -2118,6 +2194,7 @@ PyTypeObject PyLdbMessage = {
        .tp_repr = (reprfunc)py_ldb_msg_repr,
        .tp_flags = Py_TPFLAGS_DEFAULT,
        .tp_iter = (getiterfunc)py_ldb_msg_iter,
+       .tp_compare = (cmpfunc)py_ldb_msg_compare,
 };
 
 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)