From: Jennifer Sutton Date: Wed, 5 Feb 2025 03:07:49 +0000 (+1300) Subject: python:samdb: Add get_must_contain_from_lDAPDisplayName() method X-Git-Tag: tevent-0.17.0~131 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4443abc74b7623dabc113b7f34ee7d4e2db3bedb;p=thirdparty%2Fsamba.git python:samdb: Add get_must_contain_from_lDAPDisplayName() method BUG: https://bugzilla.samba.org/show_bug.cgi?id=15852 Signed-off-by: Jennifer Sutton Reviewed-by: Douglas Bagnall --- diff --git a/python/samba/samdb.py b/python/samba/samdb.py index bc818b8b83c..58144386b23 100644 --- a/python/samba/samdb.py +++ b/python/samba/samdb.py @@ -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. diff --git a/source4/dsdb/pydsdb.c b/source4/dsdb/pydsdb.c index 2529fc3229f..33b27bfaa87 100644 --- a/source4/dsdb/pydsdb.c +++ b/source4/dsdb/pydsdb.c @@ -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 },