| 1 |
#define PY_SSIZE_T_CLEAN |
| 2 |
|
| 3 |
#include <Python.h> |
| 4 |
|
| 5 |
#include "ldapconnection.h" |
| 6 |
#include "ldapentry.h" |
| 7 |
#include "ldapsearchiter.h" |
| 8 |
#include "ldapmodlist.h" |
| 9 |
#include "ldapconnectiter.h" |
| 10 |
#include "utils.h" |
| 11 |
|
| 12 |
PyObject *LDAPDNObj = NULL; |
| 13 |
PyObject *LDAPEntryObj = NULL; |
| 14 |
PyObject *LDAPValueListObj = NULL; |
| 15 |
char _g_debugmod = 0; |
| 16 |
|
| 17 |
|
| 18 |
char _g_asyncmod = 0; |
| 19 |
|
| 20 |
|
| 21 |
static PyObject * |
| 22 |
bonsai_set_connect_async(PyObject *self, PyObject *args) { |
| 23 |
PyObject *flag; |
| 24 |
|
| 25 |
if (!PyArg_ParseTuple(args,"O!", &PyBool_Type, &flag)) { |
| 26 |
return NULL; |
| 27 |
} |
| 28 |
|
| 29 |
_g_asyncmod = (char)PyObject_IsTrue(flag); |
| 30 |
|
| 31 |
Py_RETURN_NONE; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
static PyObject * |
| 36 |
bonsai_set_debug(PyObject *self, PyObject *args, PyObject *kwds) { |
| 37 |
int deb_level = 0; |
| 38 |
PyObject *flag = NULL; |
| 39 |
static char *kwlist[] = {"debug", "level", NULL}; |
| 40 |
|
| 41 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|i", kwlist, |
| 42 |
&PyBool_Type, &flag, &deb_level)) { |
| 43 |
return NULL; |
| 44 |
} |
| 45 |
|
| 46 |
_g_debugmod = (char)PyObject_IsTrue(flag); |
| 47 |
#ifndef WIN32 |
| 48 |
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &deb_level); |
| 49 |
#endif |
| 50 |
Py_RETURN_NONE; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
static PyObject * |
| 55 |
bonsai_get_vendor_info(PyObject *self) { |
| 56 |
int rc = 0; |
| 57 |
LDAPAPIInfo info; |
| 58 |
|
| 59 |
info.ldapai_info_version = LDAP_API_INFO_VERSION; |
| 60 |
rc = ldap_get_option(NULL, LDAP_OPT_API_INFO, &info); |
| 61 |
if (rc != LDAP_SUCCESS) { |
| 62 |
PyErr_SetString(PyExc_Exception, "Failed to receive API info."); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
return Py_BuildValue("(s,i)", info.ldapai_vendor_name, |
| 67 |
info.ldapai_vendor_version); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
static PyObject * |
| 73 |
bonsai_get_tls_impl_name(PyObject *self) { |
| 74 |
int rc = 0; |
| 75 |
char *package = NULL; |
| 76 |
PyObject *str = NULL; |
| 77 |
|
| 78 |
rc = ldap_get_option(NULL, LDAP_OPT_X_TLS_PACKAGE, &package); |
| 79 |
if (rc != LDAP_SUCCESS || package == NULL) { |
| 80 |
PyErr_SetString(PyExc_Exception, "Failed to receive name of the" |
| 81 |
" TLS implementation."); |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
str = PyUnicode_FromString(package); |
| 86 |
ldap_memfree(package); |
| 87 |
return str; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
static PyObject * |
| 92 |
bonsai_has_krb5_support(PyObject *self) { |
| 93 |
#if defined(HAVE_KRB5) || defined(WIN32) |
| 94 |
Py_RETURN_TRUE; |
| 95 |
#else |
| 96 |
Py_RETURN_FALSE; |
| 97 |
#endif |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static PyObject * |
| 104 |
bonsai_unique_contains(PyObject *self, PyObject *args) { |
| 105 |
PyObject *list = NULL; |
| 106 |
PyObject *value = NULL; |
| 107 |
|
| 108 |
if (!PyArg_ParseTuple(args, "OO", &list, &value)) return NULL; |
| 109 |
|
| 110 |
return unique_contains(list, value); |
| 111 |
} |
| 112 |
|
| 113 |
static void |
| 114 |
bonsai_free(PyObject *self) { |
| 115 |
Py_DECREF(LDAPDNObj); |
| 116 |
Py_DECREF(LDAPValueListObj); |
| 117 |
Py_XDECREF(LDAPEntryObj); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
static PyMethodDef bonsai_methods[] = { |
| 122 |
{"set_connect_async", (PyCFunction)bonsai_set_connect_async, METH_VARARGS, |
| 123 |
"Sets if bonsai will attempt async connections."}, |
| 124 |
{"get_vendor_info", (PyCFunction)bonsai_get_vendor_info, METH_NOARGS, |
| 125 |
"Returns the vendor information of LDAP library."}, |
| 126 |
{"get_tls_impl_name", (PyCFunction)bonsai_get_tls_impl_name, METH_NOARGS, |
| 127 |
"Returns the name of the underlying TLS implementation."}, |
| 128 |
{"has_krb5_support", (PyCFunction)bonsai_has_krb5_support, METH_NOARGS, |
| 129 |
"Check that the module is build with additional Kerberos support."}, |
| 130 |
{"set_debug", (PyCFunction)bonsai_set_debug, METH_VARARGS | METH_KEYWORDS, |
| 131 |
"Turn on and off debug mode."}, |
| 132 |
{"_unique_contains", (PyCFunction)bonsai_unique_contains, METH_VARARGS, |
| 133 |
"Check that the item is in the LDAPValueList. Returns with a tuple of" |
| 134 |
"status of the search and the matched element."}, |
| 135 |
{NULL, NULL, 0, NULL} |
| 136 |
}; |
| 137 |
|
| 138 |
static PyModuleDef bonsai2module = { |
| 139 |
PyModuleDef_HEAD_INIT, |
| 140 |
"_bonsai", |
| 141 |
"Python C extension for accessing directory servers using LDAP.", |
| 142 |
-1, |
| 143 |
bonsai_methods, NULL, NULL, NULL, (freefunc)bonsai_free |
| 144 |
}; |
| 145 |
|
| 146 |
PyMODINIT_FUNC |
| 147 |
PyInit__bonsai(void) { |
| 148 |
PyObject* module = NULL; |
| 149 |
|
| 150 |
|
| 151 |
LDAPDNObj = load_python_object("bonsai.ldapdn", "LDAPDN"); |
| 152 |
if (LDAPDNObj == NULL) return NULL; |
| 153 |
|
| 154 |
|
| 155 |
LDAPValueListObj = load_python_object("bonsai.ldapvaluelist", "LDAPValueList"); |
| 156 |
if (LDAPValueListObj == NULL) return NULL; |
| 157 |
|
| 158 |
module = PyModule_Create(&bonsai2module); |
| 159 |
if (module == NULL) return NULL; |
| 160 |
|
| 161 |
LDAPEntryType.tp_base = &PyDict_Type; |
| 162 |
|
| 163 |
if (PyType_Ready(&LDAPConnectionType) < 0) return NULL; |
| 164 |
if (PyType_Ready(&LDAPSearchIterType) < 0) return NULL; |
| 165 |
if (PyType_Ready(&LDAPConnectIterType) < 0) return NULL; |
| 166 |
if (PyType_Ready(&LDAPEntryType) < 0) return NULL; |
| 167 |
if (PyType_Ready(&LDAPModListType) < 0) return NULL; |
| 168 |
|
| 169 |
Py_INCREF(&LDAPEntryType); |
| 170 |
PyModule_AddObject(module, "ldapentry", (PyObject *)&LDAPEntryType); |
| 171 |
|
| 172 |
Py_INCREF(&LDAPConnectionType); |
| 173 |
PyModule_AddObject(module, "ldapconnection", (PyObject *)&LDAPConnectionType); |
| 174 |
|
| 175 |
Py_INCREF(&LDAPSearchIterType); |
| 176 |
PyModule_AddObject(module, "ldapsearchiter", (PyObject *)&LDAPSearchIterType); |
| 177 |
|
| 178 |
return module; |
| 179 |
} |