pydsdb: added get_backlink_from_lDAPDisplayName()
authorAndrew Tridgell <tridge@samba.org>
Mon, 11 Jul 2011 06:55:11 +0000 (16:55 +1000)
committerAndrew Tridgell <tridge@samba.org>
Wed, 13 Jul 2011 10:51:05 +0000 (12:51 +0200)
Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>
Pair-Programmed-With: Amitay Isaacs <amitay@gmail.com>

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

index 463a2f99d9649fdee9da4ce4fd1f2cd53fe213e5..e74f2bb584bab27efd2a8f3be596e296c0623acf 100644 (file)
@@ -394,6 +394,49 @@ static PyObject *py_dsdb_get_linkId_from_lDAPDisplayName(PyObject *self, PyObjec
        return PyInt_FromLong(attribute->linkID);
 }
 
+/*
+  return the backlink attribute name (if any) for an attribute
+ */
+static PyObject *py_dsdb_get_backlink_from_lDAPDisplayName(PyObject *self, PyObject *args)
+{
+       PyObject *py_ldb;
+       struct ldb_context *ldb;
+       struct dsdb_schema *schema;
+       const char *ldap_display_name;
+       const struct dsdb_attribute *attribute, *target_attr;
+
+       if (!PyArg_ParseTuple(args, "Os", &py_ldb, &ldap_display_name))
+               return NULL;
+
+       PyErr_LDB_OR_RAISE(py_ldb, ldb);
+
+       schema = dsdb_get_schema(ldb, NULL);
+
+       if (!schema) {
+               PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
+               return NULL;
+       }
+
+       attribute = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
+       if (attribute == NULL) {
+               PyErr_Format(PyExc_RuntimeError, "Failed to find attribute '%s'", ldap_display_name);
+               return NULL;
+       }
+
+       if (attribute->linkID == 0) {
+               Py_RETURN_NONE;
+       }
+
+       target_attr = dsdb_attribute_by_linkID(schema, attribute->linkID ^ 1);
+       if (target_attr == NULL) {
+               /* when we add pseudo-backlinks we'll need to handle
+                  them here */
+               Py_RETURN_NONE;
+       }
+
+       return PyString_FromString(target_attr->lDAPDisplayName);
+}
+
 
 static PyObject *py_dsdb_get_lDAPDisplayName_by_attid(PyObject *self, PyObject *args)
 {
@@ -937,6 +980,8 @@ static PyMethodDef py_dsdb_methods[] = {
                METH_VARARGS, NULL },
        { "_dsdb_get_lDAPDisplayName_by_attid", (PyCFunction)py_dsdb_get_lDAPDisplayName_by_attid,
                METH_VARARGS, NULL },
+       { "_dsdb_get_backlink_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_backlink_from_lDAPDisplayName,
+               METH_VARARGS, NULL },
        { "_dsdb_set_ntds_invocation_id",
                (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
                NULL },
index f77ce1737db607df00d48c6e665991ed12c1586b..a7ed1d2c39e96b3d7c016cb795a0ea540b022941 100644 (file)
@@ -521,6 +521,11 @@ accountExpires: %u
         '''return the lDAPDisplayName from an integer DRS attribute ID'''
         return dsdb._dsdb_get_lDAPDisplayName_by_attid(self, attid)
 
+    def get_backlink_from_lDAPDisplayName(self, ldap_display_name):
+        '''return the attribute name of the corresponding backlink from the name
+        of a forward link attribute. If there is no backlink return None'''
+        return dsdb._dsdb_get_backlink_from_lDAPDisplayName(self, ldap_display_name)
+
     def set_ntds_settings_dn(self, ntds_settings_dn):
         """Set the NTDS Settings DN, as would be returned on the dsServiceName
         rootDSE attribute.