s4-pydsdb: added dsdb_normalise_attributes() call
authorAndrew Tridgell <tridge@samba.org>
Fri, 17 Jun 2011 01:29:44 +0000 (11:29 +1000)
committerAndrew Tridgell <tridge@samba.org>
Fri, 17 Jun 2011 02:32:55 +0000 (12:32 +1000)
this call converts a set of attributes to DRSUAPI format and back to
ldb format. This has the effect of normalising the attributes using
the schema syntax rules

source4/dsdb/pydsdb.c
source4/scripting/python/samba/samdb.py

index 895bd9a560573dd098a752468e44113b1dbad4f0..62f33bbc3407a9f13a82854de918dac3af63abd1 100644 (file)
@@ -423,6 +423,109 @@ static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args)
        return ret;
 }
 
+
+/*
+  normalise a ldb attribute list
+ */
+static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
+{
+       PyObject *py_ldb, *el_list, *ret;
+       struct ldb_context *ldb;
+       char *ldap_display_name;
+       const struct dsdb_attribute *a;
+       struct dsdb_schema *schema;
+       struct dsdb_syntax_ctx syntax_ctx;
+       struct ldb_message_element *el;
+       struct drsuapi_DsReplicaAttribute *attr;
+       TALLOC_CTX *tmp_ctx;
+       WERROR werr;
+       Py_ssize_t i;
+
+       if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
+               return NULL;
+       }
+
+       PyErr_LDB_OR_RAISE(py_ldb, ldb);
+
+       if (!PyList_Check(el_list)) {
+               PyErr_Format(PyExc_TypeError, "ldif_elements must be a list");
+               return NULL;
+       }
+
+       schema = dsdb_get_schema(ldb, NULL);
+       if (!schema) {
+               PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
+               return NULL;
+       }
+
+       a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
+       if (a == NULL) {
+               PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
+               return NULL;
+       }
+
+       dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
+       syntax_ctx.is_schema_nc = false;
+
+       tmp_ctx = talloc_new(ldb);
+       if (tmp_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       el = talloc_zero(tmp_ctx, struct ldb_message_element);
+       if (el == NULL) {
+               PyErr_NoMemory();
+               talloc_free(tmp_ctx);
+               return NULL;
+       }
+
+       el->name = ldap_display_name;
+       el->num_values = PyList_Size(el_list);
+
+       el->values = talloc_array(el, struct ldb_val, el->num_values);
+       if (el->values == NULL) {
+               PyErr_NoMemory();
+               talloc_free(tmp_ctx);
+               return NULL;
+       }
+
+       for (i = 0; i < el->num_values; i++) {
+               PyObject *item = PyList_GetItem(el_list, i);
+               if (!PyString_Check(item)) {
+                       PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
+                       return NULL;
+               }
+               el->values[i].data = (uint8_t *)PyString_AsString(item);
+               el->values[i].length = PyString_Size(item);
+       }
+
+       /* first run ldb_to_drsuapi, then convert back again. This has
+        * the effect of normalising the attributes
+        */
+
+       attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
+       if (attr == NULL) {
+               PyErr_NoMemory();
+               talloc_free(tmp_ctx);
+               return NULL;
+       }
+
+       werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
+       PyErr_WERROR_IS_ERR_RAISE(werr);
+
+       /* now convert back again */
+       werr = a->syntax->drsuapi_to_ldb(&syntax_ctx, a, attr, el, el);
+       PyErr_WERROR_IS_ERR_RAISE(werr);
+
+       ret = py_return_ndr_struct("ldb", "MessageElement", el, el);
+
+       talloc_free(tmp_ctx);
+
+       return ret;
+}
+
+
 static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args)
 {
        PyObject *py_ldb, *py_guid;
@@ -723,6 +826,7 @@ static PyMethodDef py_dsdb_methods[] = {
                NULL },
        { "_dsdb_get_partitions_dn", (PyCFunction)py_dsdb_get_partitions_dn, METH_VARARGS, NULL },
        { "_dsdb_DsReplicaAttribute", (PyCFunction)py_dsdb_DsReplicaAttribute, METH_VARARGS, NULL },
+       { "_dsdb_normalise_attributes", (PyCFunction)py_dsdb_normalise_attributes, METH_VARARGS, NULL },
        { NULL }
 };
 
index 55f3536c27f7c5b93642fec9a655665ae643c04c..b513ac8fff224bd0bd3ce83a7221631fcd7c2f0a 100644 (file)
@@ -507,8 +507,13 @@ accountExpires: %u
         dsdb._dsdb_set_schema_from_ldb(self, ldb_conn)
 
     def dsdb_DsReplicaAttribute(self, ldb, ldap_display_name, ldif_elements):
+        '''convert a list of attribute values to a DRSUAPI DsReplicaAttribute'''
         return dsdb._dsdb_DsReplicaAttribute(ldb, ldap_display_name, ldif_elements)
 
+    def dsdb_normalise_attributes(self, ldb, ldap_display_name, ldif_elements):
+        '''normalise a list of attribute values'''
+        return dsdb._dsdb_normalise_attributes(ldb, ldap_display_name, ldif_elements)
+
     def get_attribute_from_attid(self, attid):
         """ Get from an attid the associated attribute