]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python:samdb: Add get_must_contain_from_lDAPDisplayName() method
authorJennifer Sutton <jennifersutton@catalyst.net.nz>
Wed, 5 Feb 2025 03:07:49 +0000 (16:07 +1300)
committerJo Sutton <jsutton@samba.org>
Mon, 26 May 2025 02:41:36 +0000 (02:41 +0000)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15852

Signed-off-by: Jennifer Sutton <jennifersutton@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/samba/samdb.py
source4/dsdb/pydsdb.c

index bc818b8b83c542db10548eb6a8502c2521077582..58144386b239d2d40b20d51e7981bb21aba8f562 100644 (file)
@@ -1120,6 +1120,10 @@ accountExpires: %u
         """return the lDAPDisplayName from an integer DRS governsID"""
         return dsdb._dsdb_get_lDAPDisplayName_by_governsID_id(self, governs_idns_id)
 
+    def get_must_contain_from_lDAPDisplayName(self, ldap_display_name):
+        """return the mandatory attributes for a LDAP class as a set of strings"""
+        return dsdb._dsdb_get_must_contain_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.
index 2529fc3229f1608d69cc362217e2636d88c6a9b5..33b27bfaa87113b07ca7e0a45fcef7daa64a988b 100644 (file)
@@ -485,6 +485,93 @@ static PyObject *py_dsdb_get_lDAPDisplayName_by_governsID_id(PyObject *self,
 }
 
 
+/*
+  return the set of mandatory attributes from the class name
+ */
+static PyObject *py_dsdb_get_must_contain_from_lDAPDisplayName(PyObject *self, PyObject *args)
+{
+       PyObject *py_ldb = NULL;
+       struct ldb_context *ldb = NULL;
+       struct dsdb_schema *schema = NULL;
+       const char *ldap_display_name = NULL;
+       const struct dsdb_class *cls = NULL;
+       const char **must_contain = NULL;
+       int ret;
+
+       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 == NULL) {
+               PyErr_SetString(PyExc_RuntimeError,
+                               "Failed to find a schema from ldb");
+               return NULL;
+       }
+
+       cls = dsdb_class_by_lDAPDisplayName(schema, ldap_display_name);
+       if (cls == NULL) {
+               PyErr_Format(PyExc_KeyError,
+                            "Failed to find class '%s'",
+                            ldap_display_name);
+               return NULL;
+       }
+
+       {
+               PyObject *set = NULL;
+
+               set = PySet_New(NULL);
+               if (set == NULL) {
+                       return PyErr_NoMemory();
+               }
+
+               must_contain = cls->systemMustContain;
+               if (must_contain != NULL) {
+                       for (; *must_contain != NULL; ++must_contain) {
+                               PyObject *attr = NULL;
+
+                               attr = PyUnicode_FromString(*must_contain);
+                               if (attr == NULL) {
+                                       Py_DECREF(set);
+                                       return NULL;
+                               }
+
+                               ret = PySet_Add(set, attr);
+                               if (ret) {
+                                       Py_DECREF(attr);
+                                       Py_DECREF(set);
+                                       return NULL;
+                               }
+                       }
+               }
+
+               must_contain = cls->mustContain;
+               if (must_contain != NULL) {
+                       for (; *must_contain != NULL; ++must_contain) {
+                               PyObject *attr = NULL;
+
+                               attr = PyUnicode_FromString(*must_contain);
+                               if (attr == NULL) {
+                                       Py_DECREF(set);
+                                       return NULL;
+                               }
+
+                               ret = PySet_Add(set, attr);
+                               if (ret) {
+                                       Py_DECREF(attr);
+                                       Py_DECREF(set);
+                                       return NULL;
+                               }
+                       }
+               }
+
+               return set;
+       }
+}
+
+
 /*
   return the attribute syntax oid as a string from the attribute name
  */
@@ -1625,6 +1712,8 @@ static PyMethodDef py_dsdb_methods[] = {
                METH_VARARGS, NULL },
        { "_dsdb_get_lDAPDisplayName_by_governsID_id", (PyCFunction)py_dsdb_get_lDAPDisplayName_by_governsID_id,
                METH_VARARGS, NULL },
+       { "_dsdb_get_must_contain_from_lDAPDisplayName", (PyCFunction)py_dsdb_get_must_contain_from_lDAPDisplayName,
+               METH_VARARGS, NULL },
        { "_dsdb_set_ntds_invocation_id",
                (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS,
                NULL },