From f0b197b2676cf3e73c433c39f39e775a4c64fd39 Mon Sep 17 00:00:00 2001 From: stroeder Date: Fri, 17 Apr 2009 12:19:09 +0000 Subject: [PATCH] Refer to new project home-page --- Modules/LDAPObject.h | 52 ++++++++ Modules/common.c | 19 +++ Modules/common.h | 39 ++++++ Modules/constants.h | 19 +++ Modules/errors.h | 16 +++ Modules/functions.h | 12 ++ Modules/ldapmodule.c | 49 ++++++++ Modules/options.h | 8 ++ Modules/schema.c | 283 +++++++++++++++++++++++++++++++++++++++++++ Modules/schema.h | 14 +++ Modules/version.c | 20 +++ Modules/version.h | 12 ++ 12 files changed, 543 insertions(+) create mode 100644 Modules/LDAPObject.h create mode 100644 Modules/common.c create mode 100644 Modules/common.h create mode 100644 Modules/constants.h create mode 100644 Modules/errors.h create mode 100644 Modules/functions.h create mode 100644 Modules/ldapmodule.c create mode 100644 Modules/options.h create mode 100644 Modules/schema.c create mode 100644 Modules/schema.h create mode 100644 Modules/version.c create mode 100644 Modules/version.h diff --git a/Modules/LDAPObject.h b/Modules/LDAPObject.h new file mode 100644 index 0000000..b0ffbbe --- /dev/null +++ b/Modules/LDAPObject.h @@ -0,0 +1,52 @@ +/* See http://www.python-ldap.org/ for details. + * $Id: LDAPObject.h,v 1.10 2009/04/17 12:19:09 stroeder Exp $ */ + +#ifndef __h_LDAPObject +#define __h_LDAPObject + +#include "common.h" + +#include "lber.h" +#include "ldap.h" +#if LDAP_API_VERSION < 2000 +#error Current python-ldap requires OpenLDAP 2.x +#endif + +#if PYTHON_API_VERSION < 1007 +typedef PyObject* _threadstate; +#else +typedef PyThreadState* _threadstate; +#endif + +typedef struct { + PyObject_HEAD + LDAP* ldap; + _threadstate _save; /* for thread saving on referrals */ + int valid; +} LDAPObject; + +extern PyTypeObject LDAP_Type; +#define LDAPObject_Check(v) ((v)->ob_type == &LDAP_Type) + +extern LDAPObject *newLDAPObject( LDAP* ); + +/* macros to allow thread saving in the context of an LDAP connection */ + +#define LDAP_BEGIN_ALLOW_THREADS( l ) \ + { \ + LDAPObject *lo = (l); \ + if (lo->_save != NULL) \ + Py_FatalError( "saving thread twice?" ); \ + lo->_save = PyEval_SaveThread(); \ + } + +#define LDAP_END_ALLOW_THREADS( l ) \ + { \ + LDAPObject *lo = (l); \ + _threadstate _save = lo->_save; \ + lo->_save = NULL; \ + PyEval_RestoreThread( _save ); \ + } + +#endif /* __h_LDAPObject */ + diff --git a/Modules/common.c b/Modules/common.c new file mode 100644 index 0000000..ff471f1 --- /dev/null +++ b/Modules/common.c @@ -0,0 +1,19 @@ +/* Miscellaneous common routines + * See http://www.python-ldap.org/ for details. + * $Id: common.c,v 1.3 2009/04/17 12:19:09 stroeder Exp $ */ + +#include "common.h" + +/* dynamically add the methods into the module dictionary d */ + +void +LDAPadd_methods( PyObject* d, PyMethodDef* methods ) +{ + PyMethodDef *meth; + + for( meth = methods; meth->ml_meth; meth++ ) { + PyObject *f = PyCFunction_New( meth, NULL ); + PyDict_SetItemString( d, meth->ml_name, f ); + Py_DECREF(f); + } +} diff --git a/Modules/common.h b/Modules/common.h new file mode 100644 index 0000000..a182a97 --- /dev/null +++ b/Modules/common.h @@ -0,0 +1,39 @@ +/* common utility macros + * See http://www.python-ldap.org/ for details. + * $Id: common.h,v 1.8 2009/04/17 12:19:09 stroeder Exp $ */ + +#ifndef __h_common +#define __h_common + +#define PY_SSIZE_T_CLEAN + +#include "Python.h" + +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif + +#if defined(MS_WINDOWS) +#include +#else /* unix */ +#include +#include +#include +#endif + +/* Backwards compability with Python prior 2.5 */ +#if PY_VERSION_HEX < 0x02050000 +typedef int Py_ssize_t; +#define PY_SSIZE_T_MAX INT_MAX +#define PY_SSIZE_T_MIN INT_MIN +#endif + +#include +#define streq( a, b ) \ + ( (*(a)==*(b)) && 0==strcmp(a,b) ) + +void LDAPadd_methods( PyObject*d, PyMethodDef*methods ); +#define PyNone_Check(o) ((o) == Py_None) + +#endif /* __h_common_ */ + diff --git a/Modules/constants.h b/Modules/constants.h new file mode 100644 index 0000000..c77f269 --- /dev/null +++ b/Modules/constants.h @@ -0,0 +1,19 @@ +/* See http://www.python-ldap.org/ for details. + * $Id: constants.h,v 1.6 2009/04/17 12:19:09 stroeder Exp $ */ + +#ifndef __h_constants_ +#define __h_constants_ + +#include "common.h" +extern void LDAPinit_constants( PyObject* d ); +extern PyObject* LDAPconstant( int ); + +#ifndef LDAP_CONTROL_PAGE_OID +#define LDAP_CONTROL_PAGE_OID "1.2.840.113556.1.4.319" +#endif /* !LDAP_CONTROL_PAGE_OID */ + +#ifndef LDAP_CONTROL_VALUESRETURNFILTER +#define LDAP_CONTROL_VALUESRETURNFILTER "1.2.826.0.1.3344810.2.3" /* RFC 3876 */ +#endif /* !LDAP_CONTROL_VALUESRETURNFILTER */ + +#endif /* __h_constants_ */ diff --git a/Modules/errors.h b/Modules/errors.h new file mode 100644 index 0000000..9c672cd --- /dev/null +++ b/Modules/errors.h @@ -0,0 +1,16 @@ +/* See http://www.python-ldap.org/ for details. + * $Id: errors.h,v 1.6 2009/04/17 12:19:09 stroeder Exp $ */ + +#ifndef __h_errors_ +#define __h_errors_ + +#include "common.h" +#include "lber.h" +#include "ldap.h" + +extern PyObject* LDAPexception_class; +extern PyObject* LDAPerror( LDAP*, char*msg ); +extern void LDAPinit_errors( PyObject* ); +PyObject* LDAPerr(int errnum); + +#endif /* __h_errors */ diff --git a/Modules/functions.h b/Modules/functions.h new file mode 100644 index 0000000..515af4d --- /dev/null +++ b/Modules/functions.h @@ -0,0 +1,12 @@ +/* See http://www.python-ldap.org/ for details. + * $Id: functions.h,v 1.4 2009/04/17 12:19:09 stroeder Exp $ */ + +#ifndef __h_functions_ +#define __h_functions_ + +/* $Id: functions.h,v 1.4 2009/04/17 12:19:09 stroeder Exp $ */ + +#include "common.h" +extern void LDAPinit_functions( PyObject* ); + +#endif /* __h_functions_ */ diff --git a/Modules/ldapmodule.c b/Modules/ldapmodule.c new file mode 100644 index 0000000..77cd82b --- /dev/null +++ b/Modules/ldapmodule.c @@ -0,0 +1,49 @@ +/* See http://www.python-ldap.org/ for details. + * $Id: ldapmodule.c,v 1.9 2009/04/17 12:19:09 stroeder Exp $ */ + +#include "common.h" +#include "version.h" +#include "constants.h" +#include "errors.h" +#include "functions.h" +#include "schema.h" +#include "ldapcontrol.h" + +#include "LDAPObject.h" + +DL_EXPORT(void) init_ldap(void); + +/* dummy module methods */ + +static PyMethodDef methods[] = { + { NULL, NULL } +}; + +/* module initialisation */ + +DL_EXPORT(void) +init_ldap() +{ + PyObject *m, *d; + +#if defined(MS_WINDOWS) || defined(__CYGWIN__) + LDAP_Type.ob_type = &PyType_Type; +#endif + + /* Create the module and add the functions */ + m = Py_InitModule("_ldap", methods); + + /* Add some symbolic constants to the module */ + d = PyModule_GetDict(m); + + LDAPinit_version(d); + LDAPinit_constants(d); + LDAPinit_errors(d); + LDAPinit_functions(d); + LDAPinit_schema(d); + LDAPinit_control(d); + + /* Check for errors */ + if (PyErr_Occurred()) + Py_FatalError("can't initialize module _ldap"); +} diff --git a/Modules/options.h b/Modules/options.h new file mode 100644 index 0000000..fb6e0a2 --- /dev/null +++ b/Modules/options.h @@ -0,0 +1,8 @@ +/* See http://www.python-ldap.org/ for details. + * $Id: options.h,v 1.4 2009/04/17 12:19:09 stroeder Exp $ */ + +int LDAP_optionval_by_name(const char *name); +int LDAP_set_option(LDAPObject *self, int option, PyObject *value); +PyObject *LDAP_get_option(LDAPObject *self, int option); + +void set_timeval_from_double( struct timeval *tv, double d ); diff --git a/Modules/schema.c b/Modules/schema.c new file mode 100644 index 0000000..5bfbfa4 --- /dev/null +++ b/Modules/schema.c @@ -0,0 +1,283 @@ +/* See http://www.python-ldap.org/ for details. + * $Id: schema.c,v 1.8 2009/04/17 12:19:09 stroeder Exp $ */ + +#include "common.h" + +#include "schema.h" +#include "ldap_schema.h" + +/* + This utility function takes a null delimited C array of (null + delimited) C strings, creates its python equivalent and returns a + new reference to it. If the array is empty or the pointer to it is + NULL, an empty python array is returned. +*/ +PyObject* c_string_array_to_python(char **string_array) +{ + Py_ssize_t count = 0; + char **s; + PyObject *py_list; + if (string_array) { + for (s=string_array; *s != 0; s++) count++; + py_list = PyList_New(count); + count = 0; + for (s=string_array; *s != 0; s++){ + PyList_SetItem(py_list, count, PyString_FromString(*s)); + count++; + } + } else py_list=PyList_New(0); + return py_list; +} + + +/* + This function returns a list of tuples. The first entry of each + tuple is a string (lsei_name), and the second is a lists built from + lsei_values. + + Probably the C data structure is modeled along the lines of a + mapping "lsei_name -> (list of lsei_values)". However, there seems + to be no guarantee that a lsei_name is unique, so I dare not use a + python mapping for this beast... + */ +PyObject* schema_extension_to_python(LDAPSchemaExtensionItem **extensions) +{ + Py_ssize_t count = 0; + LDAPSchemaExtensionItem **e; + PyObject *py_list, *item_tuple; + if (extensions) { + for (e = extensions; *e !=0; e++) count++; + py_list = PyList_New(count); + count = 0; + for (e = extensions; *e !=0; e++) { + item_tuple = PyTuple_New(2); + PyTuple_SetItem(item_tuple, 0, + PyString_FromString((*e)->lsei_name)); + PyTuple_SetItem(item_tuple, 1, + c_string_array_to_python((*e)->lsei_values)); + PyList_SetItem(py_list, count, item_tuple); + count++; + } + } + else py_list=PyList_New(0); + return py_list; +} + + +/* + The following four functions do the boring job: they take a python + string, feed it into the respective parser functions provided by + openldap, and build a python list from the data structure returned + by the C function. + */ + +static char doc_ldap_str2objectclass[] = +""; + +static PyObject* +l_ldap_str2objectclass(PyObject* self, PyObject *args) +{ + int ret=0, flag = LDAP_SCHEMA_ALLOW_NONE; + char *oc_string; + const char *errp; + LDAPObjectClass *o; + PyObject *oc_names, *oc_sup_oids, *oc_at_oids_must, + *oc_at_oids_may, *py_ret; + + + if (!PyArg_ParseTuple(args, "si", &oc_string, &flag)) + return NULL; + o = ldap_str2objectclass( oc_string, &ret, &errp, flag); + if (ret) { + py_ret = PyInt_FromLong(ret); + return py_ret; + } + + oc_sup_oids = c_string_array_to_python(o->oc_sup_oids); + oc_names = c_string_array_to_python(o->oc_names); + oc_at_oids_must = c_string_array_to_python(o->oc_at_oids_must); + oc_at_oids_may = c_string_array_to_python(o->oc_at_oids_may); + py_ret = PyList_New(9); + PyList_SetItem(py_ret, 0, PyString_FromString(o->oc_oid)); + PyList_SetItem(py_ret, 1, oc_names); + if (o->oc_desc) { + PyList_SetItem(py_ret, 2, PyString_FromString(o->oc_desc)); + } else { + PyList_SetItem(py_ret, 2, PyString_FromString("")); + } + PyList_SetItem(py_ret, 3, PyInt_FromLong(o->oc_obsolete)); + PyList_SetItem(py_ret, 4, oc_sup_oids); + PyList_SetItem(py_ret, 5, PyInt_FromLong(o->oc_kind)); + PyList_SetItem(py_ret, 6, oc_at_oids_must); + PyList_SetItem(py_ret, 7, oc_at_oids_may); + + PyList_SetItem(py_ret, 8, + schema_extension_to_python(o->oc_extensions)); + + ldap_objectclass_free(o); + return py_ret; +} + + +static char doc_ldap_str2attributetype[] = +""; + +static PyObject* +l_ldap_str2attributetype(PyObject* self, PyObject *args) +{ + int ret=0, flag = LDAP_SCHEMA_ALLOW_NONE; + char *at_string; + const char *errp; + LDAPAttributeType *a; + PyObject *py_ret; + PyObject *at_names; + + if (!PyArg_ParseTuple(args, "si", &at_string,&flag)) + return NULL; + a = ldap_str2attributetype( at_string, &ret, &errp, flag); + if (ret) { + py_ret = PyInt_FromLong(ret); + return py_ret; + } + + py_ret = PyList_New(15); + PyList_SetItem(py_ret, 0, PyString_FromString(a->at_oid)); + at_names = c_string_array_to_python(a->at_names); + PyList_SetItem(py_ret, 1, at_names); + if (a->at_desc) { + PyList_SetItem(py_ret, 2, PyString_FromString(a->at_desc)); + } else { + PyList_SetItem(py_ret, 2, PyString_FromString("")); + } + PyList_SetItem(py_ret, 3, PyInt_FromLong(a->at_obsolete)); + if (a->at_sup_oid) { + PyList_SetItem(py_ret, 4, PyString_FromString(a->at_sup_oid)); + } else { + PyList_SetItem(py_ret, 4, PyString_FromString("")); + } + if (a->at_equality_oid) { + PyList_SetItem(py_ret, 5, PyString_FromString(a->at_equality_oid)); + } else { + PyList_SetItem(py_ret, 5, PyString_FromString("")); + } + if (a->at_ordering_oid) { + PyList_SetItem(py_ret, 6, PyString_FromString(a->at_ordering_oid)); + } else { + PyList_SetItem(py_ret, 6, PyString_FromString("")); + } + if (a->at_substr_oid) { + PyList_SetItem(py_ret, 7, PyString_FromString(a->at_substr_oid)); + } else { + PyList_SetItem(py_ret, 7, PyString_FromString("")); + } + if (a->at_syntax_oid) { + PyList_SetItem(py_ret, 8, PyString_FromString(a->at_syntax_oid)); + } else { + PyList_SetItem(py_ret, 8, PyString_FromString("")); + } + PyList_SetItem(py_ret, 9, PyInt_FromLong(a->at_syntax_len)); + PyList_SetItem(py_ret,10, PyInt_FromLong(a->at_single_value)); + PyList_SetItem(py_ret,11, PyInt_FromLong(a->at_collective)); + PyList_SetItem(py_ret,12, PyInt_FromLong(a->at_no_user_mod)); + PyList_SetItem(py_ret,13, PyInt_FromLong(a->at_usage)); + + PyList_SetItem(py_ret, 14, + schema_extension_to_python(a->at_extensions)); + ldap_attributetype_free(a); + return py_ret; +} + +static char doc_ldap_str2syntax[] = +""; + + +static PyObject* +l_ldap_str2syntax(PyObject* self, PyObject *args) +{ + LDAPSyntax *s; + int ret=0, flag = LDAP_SCHEMA_ALLOW_NONE; + const char *errp; + char *syn_string; + PyObject *py_ret, *syn_names; + + if (!PyArg_ParseTuple(args, "si", &syn_string,&flag)) + return NULL; + s = ldap_str2syntax(syn_string, &ret, &errp, flag); + if (ret) { + py_ret = PyInt_FromLong(ret); + return py_ret; + } + py_ret = PyList_New(4); + PyList_SetItem(py_ret, 0, PyString_FromString(s->syn_oid)); + syn_names = c_string_array_to_python(s->syn_names); + PyList_SetItem(py_ret, 1, syn_names); + if (s->syn_desc) { + PyList_SetItem(py_ret, 2, PyString_FromString(s->syn_desc)); + } else { + PyList_SetItem(py_ret, 2, PyString_FromString("")); + } + PyList_SetItem(py_ret, 3, + schema_extension_to_python(s->syn_extensions)); + ldap_syntax_free(s); + return py_ret; +} + +static char doc_ldap_str2matchingrule[] = +""; + +static PyObject* +l_ldap_str2matchingrule(PyObject* self, PyObject *args) +{ + LDAPMatchingRule *m; + int ret=0, flag = LDAP_SCHEMA_ALLOW_NONE; + const char *errp; + char *mr_string; + PyObject *py_ret, *mr_names; + + if (!PyArg_ParseTuple(args, "si", &mr_string,&flag)) + return NULL; + m = ldap_str2matchingrule(mr_string, &ret, &errp, flag); + if (ret) { + py_ret = PyInt_FromLong(ret); + return py_ret; + } + py_ret = PyList_New(6); + PyList_SetItem(py_ret, 0, PyString_FromString(m->mr_oid)); + mr_names = c_string_array_to_python(m->mr_names); + PyList_SetItem(py_ret, 1, mr_names); + if (m->mr_desc) { + PyList_SetItem(py_ret, 2, PyString_FromString(m->mr_desc)); + } else { + PyList_SetItem(py_ret, 2, PyString_FromString("")); + } + PyList_SetItem(py_ret, 3, PyInt_FromLong(m->mr_obsolete)); + if (m->mr_syntax_oid) { + PyList_SetItem(py_ret, 4, PyString_FromString(m->mr_syntax_oid)); + } else { + PyList_SetItem(py_ret, 4, PyString_FromString("")); + } + PyList_SetItem(py_ret, 5, + schema_extension_to_python(m->mr_extensions)); + ldap_matchingrule_free(m); + return py_ret; +} + +/* methods */ + +static PyMethodDef methods[] = { + { "str2objectclass", (PyCFunction)l_ldap_str2objectclass, METH_VARARGS, + doc_ldap_str2objectclass }, + { "str2attributetype", (PyCFunction)l_ldap_str2attributetype, + METH_VARARGS, doc_ldap_str2attributetype }, + { "str2syntax", (PyCFunction)l_ldap_str2syntax, + METH_VARARGS, doc_ldap_str2syntax }, + { "str2matchingrule", (PyCFunction)l_ldap_str2matchingrule, + METH_VARARGS, doc_ldap_str2matchingrule }, + { NULL, NULL } +}; + + +void +LDAPinit_schema( PyObject* d ) { + LDAPadd_methods( d, methods ); +} diff --git a/Modules/schema.h b/Modules/schema.h new file mode 100644 index 0000000..52492b3 --- /dev/null +++ b/Modules/schema.h @@ -0,0 +1,14 @@ +/* See http://www.python-ldap.org/ for details. + * $Id: schema.h,v 1.5 2009/04/17 12:19:09 stroeder Exp $ */ + +#ifndef __h_schema_ +#define __h_schema_ + + + +#include "common.h" +extern void LDAPinit_schema( PyObject* ); + + +#endif /* __h_schema_ */ + diff --git a/Modules/version.c b/Modules/version.c new file mode 100644 index 0000000..264a285 --- /dev/null +++ b/Modules/version.c @@ -0,0 +1,20 @@ +/* Set release version + * See http://www.python-ldap.org/ for details. + * $Id: version.c,v 1.4 2009/04/17 12:19:09 stroeder Exp $ */ + +#include "common.h" + +#define _STR(x) #x +#define STR(x) _STR(x) + +static char version_str[] = STR(LDAPMODULE_VERSION); + +void +LDAPinit_version( PyObject* d ) +{ + PyObject *version; + + version = PyString_FromString(version_str); + PyDict_SetItemString( d, "__version__", version ); + Py_DECREF(version); +} diff --git a/Modules/version.h b/Modules/version.h new file mode 100644 index 0000000..d9a36d9 --- /dev/null +++ b/Modules/version.h @@ -0,0 +1,12 @@ +/* Set release version + * See http://www.python-ldap.org/ for details. + * $Id: version.h,v 1.4 2009/04/17 12:19:09 stroeder Exp $ */ + +#ifndef __h_version_ +#define __h_version_ + + +#include "common.h" +extern void LDAPinit_version( PyObject* d ); + +#endif /* __h_version_ */