patch to compile with OpenLDAP 2.0.7

Konstantin Chuguev Konstantin.Chuguev at dante.org.uk
Fri Nov 17 11:47:20 CET 2000


Hello Jeffrey and all.

Attached are recent version of my patches, if you are interested. They
provide the following:

   * automatic recognition of OpenLDAP-1.2.x or OpenLDAP-2.0.x at compilation
     time, building different sets of Python LDAP constants and error
     exception objects for different OpenLDAP versions;
   * special tuples for continuation references: (None, [list_of_URLs]).
     Compare to normal entry tuples: (DN, {dict_of_attributes}). Both types
     of tuples can be returned from search methods;
   * new type of result type in the result method: SEARCH_REFERENCE.
   * a few new ldap object attributes, among them: referrals - disables (0)
     or enables (1) following referrals; is set to 1 after ldap object
     initialisation; version (2 or 3) - sets LDAP protocol version (default
     is 2).

Any comments/suggestions/fixes are welcome.


Regards,
    Konstantin.

"Jeffrey C. Ollie" wrote:

> On Thu, Nov 16, 2000 at 11:55:57PM +0100, Michael Ströder wrote:
> > "Jeffrey C. Ollie" wrote:
> > >
> > > I've gotten today's CVS version to compile against OpenLDAP 2.0.7.
> >
> > Ooops. Double efforts. I'm currently testing patches by Konstantin
> > (Cc:-ed) which also allow to retrieve search continuation references
> > (LDAPv3 referrals).
> >
> > He made some changes to attributes of LDAPObject. Especially setting
> > the options is different.
>
> I didn't know that anyone else was working on it...  The list has been
> quiet for some time now.  It looks like Konstantin's work goes beyond
> what I intended to accomplish.  I wasn't looking to add any
> functionality, just to get python-ldap to compile and work.  But I'd
> be happy to test out other patches and perhaps fix a bug or two.
>
> Jeff
>
>   ------------------------------------------------------------------------
>    Part 1.2Type: application/pgp-signature

--
          * *        Konstantin Chuguev - Application Engineer
       *      *              Francis House, 112 Hills Road
     *                       Cambridge CB2 1PQ, United Kingdom
 D  A  N  T  E       WWW:    http://www.dante.net


-------------- next part --------------
--- constants.c.orig	Wed Nov 15 10:41:35 2000
+++ constants.c	Wed Nov 15 10:48:23 2000
@@ -66,7 +66,6 @@
 	add_int(d,VERSION1);
 	add_int(d,VERSION2);
 	add_int(d,VERSION);
-	add_int(d,MAX_ATTR_LEN);
 	add_int(d,TAG_MESSAGE);
 	add_int(d,TAG_MSGID);
 
@@ -79,9 +78,27 @@
 	add_int(d,REQ_MODRDN);
 	add_int(d,REQ_COMPARE);
 	add_int(d,REQ_ABANDON);
+
+#if defined(LDAP_API_VERSION)
+	/* OpenLDAPv2 */
+	add_int(d,VERSION3);
+	add_int(d,VERSION_MIN);
+	add_int(d,VERSION_MAX);
+	add_int(d,TAG_LDAPDN);
+	add_int(d,TAG_LDAPCRED);
+	add_int(d,TAG_CONTROLS);
+	add_int(d,TAG_REFERRAL);
+
+	add_int(d,REQ_MODDN);
+	add_int(d,REQ_RENAME);
+#else
+	/* OpenLDAPv1 */
+	add_int(d,MAX_ATTR_LEN);
+
 	add_int(d,REQ_UNBIND_30);
 	add_int(d,REQ_DELETE_30);
 	add_int(d,REQ_ABANDON_30);
+#endif
 
 	/* reversibles */
 
@@ -89,6 +106,14 @@
 	PyDict_SetItem( reverse, zero, Py_None );
 	Py_DECREF( zero );
 
+#if defined(LDAP_API_VERSION)
+	/* OpenLDAPv2 */
+	add_int_r(d,RES_SEARCH_REFERENCE);
+	add_int_r(d,RES_MODDN);		/* Aliases for RES_MODRDN; */
+	add_int_r(d,RES_RENAME);	/* init them before the main name */
+	add_int(d,RES_UNSOLICITED);
+#endif
+
 	add_int_r(d,RES_BIND);
 	add_int_r(d,RES_SEARCH_ENTRY);
 	add_int_r(d,RES_SEARCH_RESULT);
@@ -106,9 +131,6 @@
 	add_int(d,AUTH_KRBV4);
 	add_int(d,AUTH_KRBV41);
 	add_int(d,AUTH_KRBV42);
-	add_int(d,AUTH_SIMPLE_30);
-	add_int(d,AUTH_KRBV41_30);
-	add_int(d,AUTH_KRBV42_30);
 	add_int(d,FILTER_AND);
 	add_int(d,FILTER_OR);
 	add_int(d,FILTER_NOT);
@@ -118,13 +140,9 @@
 	add_int(d,FILTER_LE);
 	add_int(d,FILTER_PRESENT);
 	add_int(d,FILTER_APPROX);
-	add_int(d,FILTER_PRESENT_30);
 	add_int(d,SUBSTRING_INITIAL);
 	add_int(d,SUBSTRING_ANY);
 	add_int(d,SUBSTRING_FINAL);
-	add_int(d,SUBSTRING_INITIAL_30);
-	add_int(d,SUBSTRING_ANY_30);
-	add_int(d,SUBSTRING_FINAL_30);
 	add_int(d,SCOPE_BASE);
 	add_int(d,SCOPE_ONELEVEL);
 	add_int(d,SCOPE_SUBTREE);
@@ -133,6 +151,16 @@
 	add_int(d,MOD_REPLACE);
 	add_int(d,MOD_BVALUES);
 
+#if !defined(LDAP_API_VERSION)
+	/* OpenLDAPv1 */
+	add_int(d,AUTH_SIMPLE_30);
+	add_int(d,AUTH_KRBV41_30);
+	add_int(d,AUTH_KRBV42_30);
+	add_int(d,FILTER_PRESENT_30);
+	add_int(d,SUBSTRING_INITIAL_30);
+	add_int(d,SUBSTRING_ANY_30);
+	add_int(d,SUBSTRING_FINAL_30);
+
 	/* (errors.c contains the error constants) */
 
 	add_int(d,DEFAULT_REFHOPLIMIT);
@@ -145,15 +173,36 @@
 #ifdef LDAP_CACHE_OPT_CACHEALLERRS
 	add_int(d,CACHE_OPT_CACHEALLERRS);
 #endif
+
+#endif	/* !defined(LDAP_API_VERSION) */
 	add_int(d,FILT_MAXSIZ);
 	add_int(d,DEREF_NEVER);
 	add_int(d,DEREF_SEARCHING);
 	add_int(d,DEREF_FINDING);
 	add_int(d,DEREF_ALWAYS);
 	add_int(d,NO_LIMIT);
+#if defined(LDAP_API_VERSION)
+	/* OpenLDAPv2 */
+	add_int(d,OPT_API_INFO);
+	add_int(d,OPT_DESC);
+	add_int(d,OPT_DEREF);
+	add_int(d,OPT_SIZELIMIT);
+	add_int(d,OPT_TIMELIMIT);
+	add_int(d,OPT_PROTOCOL_VERSION);
+	add_int(d,OPT_SERVER_CONTROLS);
+	add_int(d,OPT_CLIENT_CONTROLS);
+	add_int(d,OPT_API_FEATURE_INFO);
+	add_int(d,OPT_HOST_NAME);
+	add_int(d,OPT_ERROR_NUMBER);
+	add_int(d,OPT_ERROR_STRING);
+	add_int(d,OPT_MATCHED_DN);
+	add_int(d,OPT_PRIVATE_EXTENSION_BASE);
+#else
+	/* OpenLDAPv1 */
 #ifdef LDAP_OPT_DNS
 	add_int(d,OPT_DNS);
 #endif
+#endif /* defined(LDAP_API_VERSION) */
 #ifdef LDAP_OPT_REFERRALS
 	add_int(d,OPT_REFERRALS);
 #endif
@@ -161,9 +210,24 @@
 
 	/* XXX - these belong in errors.c */
 
+#if defined(LDAP_API_VERSION)
+	/* OpenLDAPv2 */
+	add_int(d,URL_SUCCESS);
+	add_int(d,URL_ERR_PARAM);
+	add_int(d,URL_ERR_BADSCHEME);
+	add_int(d,URL_ERR_BADENCLOSURE);
+	add_int(d,URL_ERR_BADURL);
+	add_int(d,URL_ERR_BADHOST);
+	add_int(d,URL_ERR_BADATTRS);
+	add_int(d,URL_ERR_BADSCOPE);
+	add_int(d,URL_ERR_BADFILTER);
+	add_int(d,URL_ERR_BADEXTS);
+#else
+	/* OpenLDAPv1 */
 	add_int(d,URL_ERR_NOTLDAP);
 	add_int(d,URL_ERR_NODN);
 	add_int(d,URL_ERR_BADSCOPE);
+#endif /* defined(LDAP_API_VERSION) */
 	add_int(d,URL_ERR_MEM);
 
 	/* author */
-------------- next part --------------
--- errors.c.orig	Wed Nov 15 10:41:35 2000
+++ errors.c	Wed Nov 15 14:17:15 2000
@@ -17,7 +17,13 @@
 
 /* list of error objects */
 
+#if defined(LDAP_API_VERSION)
+/* OpenLDAPv2 */
+#define NUM_LDAP_ERRORS		LDAP_REFERRAL_LIMIT_EXCEEDED+1
+#else
+/* OpenLDAPv1 */
 #define NUM_LDAP_ERRORS		LDAP_NO_MEMORY+1
+#endif
 static PyObject* 
 errobjects[ NUM_LDAP_ERRORS ];
 
@@ -30,21 +36,26 @@
 		PyErr_SetFromErrno( LDAPexception_class );
 		return NULL;
 	}
-#ifdef LDAP_TYPE_IS_OPAQUE
+#if defined(LDAP_TYPE_IS_OPAQUE) && !defined(LDAP_API_VERSION)
 	else {
 		PyErr_SetString(LDAPexception_class,
 			"unknown error (C API does not expose error)");
 		return NULL;
 	}
-#else
+#else /* defined(LDAP_TYPE_IS_OPAQUE) && !defined(LDAP_API_VERSION) */
 	else {
 		int errnum;
 		PyObject *errobj;
 		PyObject *info;
 		PyObject *str;
 
+#if defined(LDAP_API_VERSION)
+		char *matched, *error;
+		if (ldap_get_option(l, LDAP_OPT_ERROR_NUMBER, &errnum) < 0)
+#else
 		errnum = l->ld_errno;
 		if (errnum<0 || errnum>=NUM_LDAP_ERRORS)
+#endif /* defined(LDAP_API_VERSION) */
 			errobj = LDAPexception_class;	/* unknown error XXX */
 		else
 			errobj = errobjects[errnum];
@@ -61,6 +72,35 @@
 			PyDict_SetItemString( info, "desc", str );
 		Py_XDECREF(str);
 
+#if defined(LDAP_API_VERSION)
+		if (ldap_get_option(l, LDAP_OPT_MATCHED_DN, &matched) >= 0
+			&& matched != NULL) {
+		    if (*matched != '\0') {
+			str = PyString_FromString(matched);
+			if (str)
+			    PyDict_SetItemString( info, "matched", str );
+			Py_XDECREF(str);
+		    }
+		    ldap_memfree(matched);
+		}
+
+		if (errnum == LDAP_REFERRAL) {
+		    str = PyString_FromString(msg);
+		    if (str)
+			PyDict_SetItemString( info, "info", str );
+		    Py_XDECREF(str);
+		} else if (ldap_get_option(l, LDAP_OPT_ERROR_STRING, &error) >= 0
+			&& error != NULL) {
+		    if (error != '\0') {
+			str = PyString_FromString(error);
+			if (str)
+			    PyDict_SetItemString( info, "info", str );
+			Py_XDECREF(str);
+		    }
+		    ldap_memfree(error);
+		}
+
+#else /* defined(LDAP_API_VERSION) */
 		if (l->ld_matched != NULL && *l->ld_matched != '\0') 
 		{
 		   str = PyString_FromString(l->ld_matched);
@@ -76,11 +116,12 @@
 			   PyDict_SetItemString( info, "info", str );
 		   Py_XDECREF(str);
 		}
+#endif /* defined(LDAP_API_VERSION) */
 		PyErr_SetObject( errobj, info );
 		Py_DECREF(info);
 		return NULL;
 	}
-#endif
+#endif  /* defined(LDAP_TYPE_IS_OPAQUE) && !defined(LDAP_API_VERSION) */
 }
 
 
@@ -163,4 +204,19 @@
 	seterrobj(USER_CANCELLED);
 	seterrobj(PARAM_ERROR);
 	seterrobj(NO_MEMORY);
+#if defined(LDAP_API_VERSION)
+	seterrobj(REFERRAL);
+	seterrobj(ADMINLIMIT_EXCEEDED);
+	seterrobj(UNAVAILABLE_CRITICAL_EXTENSION);
+	seterrobj(CONFIDENTIALITY_REQUIRED);
+	seterrobj(SASL_BIND_IN_PROGRESS);
+	seterrobj(AFFECTS_MULTIPLE_DSAS);
+	seterrobj(CONNECT_ERROR);
+	seterrobj(NOT_SUPPORTED);
+	seterrobj(CONTROL_NOT_FOUND);
+	seterrobj(NO_RESULTS_RETURNED);
+	seterrobj(MORE_RESULTS_TO_RETURN);
+	seterrobj(CLIENT_LOOP);
+	seterrobj(REFERRAL_LIMIT_EXCEEDED);
+#endif
 }
-------------- next part --------------
--- LDAPObject.c.orig	Wed Nov 15 10:46:27 2000
+++ LDAPObject.c	Wed Nov 15 16:24:34 2000
@@ -1204,9 +1204,9 @@
     double timeout = -1.0;
     struct timeval tv;
     struct timeval* tvp;
-    int res_type, result;
+    int res_type;
     LDAPMessage *msg = NULL;
-    PyObject *result_str, *retval;
+    PyObject *result_str, *retval, *pmsg;
 
     if (!PyArg_ParseTuple( args, "|iid", &msgid, &all, &timeout ))
     	return NULL;
@@ -1232,28 +1232,46 @@
     	return Py_None;
     }
 
-    /* thanks to Konstantin Chuguev for this */
-    if (res_type != LDAP_RES_SEARCH_ENTRY) {
-	LDAP_BEGIN_ALLOW_THREADS( self );
+    if (res_type == LDAP_RES_SEARCH_ENTRY
+#if defined(LDAP_API_VERSION)
+	    || res_type == LDAP_RES_SEARCH_REFERENCE
+#endif
+       )
+	pmsg = LDAPmessage_to_python( self->ldap, msg );
+    else {
+	int result;
+#if defined(LDAP_API_VERSION)
+	char **refs = NULL;
+	ldap_parse_result( self->ldap, msg, &result, NULL, NULL, &refs, NULL, 0 );
+#else
 	result = ldap_result2error( self->ldap, msg, 0 );
-	LDAP_END_ALLOW_THREADS( self );
-
-	if (result != LDAP_SUCCESS) {		/* result error */
-	    return LDAPerror( self->ldap, "ldap_result2error" );
+#endif
+	if (result != LDAP_SUCCESS) {
+	    char *e;
+#if defined(LDAP_API_VERSION)
+	    char err[1024];
+	    if (result == LDAP_REFERRAL && refs && refs[0]) {
+		snprintf(err, sizeof(err), "Referral:\n%s", refs[0]);
+		e = err;
+	    } else
+		e = "ldap_parse_result";
+#else
+	    e = "ldap_result2error";
+#endif
+	    return LDAPerror( self->ldap, e );
 	}
+	pmsg = Py_None;
     }
 
     result_str = LDAPconstant( res_type );
 
-    if (msg == NULL) {
-    	retval = Py_BuildValue("(OO)", result_str, Py_None);
+    if (pmsg == NULL) {
+	retval = NULL;
     } else {
-	PyObject *pmsg = LDAPmessage_to_python( self->ldap, msg );
-	if (pmsg == NULL) 
-	    retval = NULL;
-	else
-	    retval = Py_BuildValue("(OO)", result_str, pmsg);
-        Py_DECREF(pmsg);
+	retval = Py_BuildValue("(OO)", result_str, pmsg);
+	if (pmsg != Py_None) {
+	    Py_DECREF(pmsg);
+	}
     }
     Py_DECREF(result_str);
     return retval;
@@ -1444,6 +1462,9 @@
 
 /* ldap_search_s == ldap_search_st */
 
+#if !defined(LDAP_API_VERSION)
+/* OpenLDAPv1 */
+
 /* ldap_ufn_search_c */
 
 /* ldap_ufn_search_ct */
@@ -1527,6 +1548,8 @@
 "\tSee the LDAP library manual pages for more information on these\n"
 "\t`user-friendly name' functions.";
 
+#endif /* !defined(LDAP_API_VERSION) */
+
 /* ldap_sort_entries */
 
 /* ldap_url_search */
@@ -1681,9 +1704,12 @@
     {"search",		(PyCFunction)l_ldap_search,		METH_VARARGS,	doc_search},	
     {"search_s",	(PyCFunction)l_ldap_search_st,		METH_VARARGS,	doc_search},	
     {"search_st",	(PyCFunction)l_ldap_search_st,		METH_VARARGS,	doc_search},	
+#if !defined(LDAP_API_VERSION)
+/* OpenLDAPv1 */
     {"ufn_search_s",	(PyCFunction)l_ldap_ufn_search_s,	METH_VARARGS,	doc_ufn},
     {"ufn_setfilter",	(PyCFunction)l_ldap_ufn_setfilter,	METH_VARARGS,	doc_ufn},
     {"ufn_setprefix",	(PyCFunction)l_ldap_ufn_setprefix,	METH_VARARGS,	doc_ufn},
+#endif
     {"url_search_s",	(PyCFunction)l_ldap_url_search_st,	METH_VARARGS,	doc_url_search},	
     {"url_search_st",	(PyCFunction)l_ldap_url_search_st,	METH_VARARGS,	doc_url_search},	
 #if defined(FILENO_SUPPORTED)
@@ -1758,7 +1784,47 @@
 static PyObject*
 getattr( LDAPObject* self, char* name ) 
 {
-
+#if defined(LDAP_API_VERSION)
+/* OpenLDAPv2 */
+	int res, option, intval, is_string = 0;
+	char *strval;
+
+	if (streq(name,"version"))
+		option = LDAP_OPT_PROTOCOL_VERSION;
+	else if (streq(name,"deref")) 
+		option = LDAP_OPT_DEREF;
+	else if (streq(name,"referrals"))
+		option = LDAP_OPT_REFERRALS;
+	else if (streq(name,"restart"))
+		option = LDAP_OPT_REFERRALS;
+	else if (streq(name,"timelimit")) 
+		option = LDAP_OPT_TIMELIMIT;
+	else if (streq(name,"sizelimit")) 
+		option = LDAP_OPT_SIZELIMIT;
+	else if (streq(name,"errno")) 
+		option = LDAP_OPT_ERROR_NUMBER;
+	else if (streq(name,"error")) {
+		option = LDAP_OPT_ERROR_STRING;
+		is_string = 1;
+	} else if (streq(name,"matched")) {
+		option = LDAP_OPT_MATCHED_DN;
+		is_string = 1;
+	} else
+		return Py_FindMethod( methods, (PyObject*)self, name );
+	LDAP_BEGIN_ALLOW_THREADS( self );
+	res = ldap_get_option(self->ldap, option, is_string ? (void *)&strval
+	                                                    : (void *)&intval);
+	LDAP_END_ALLOW_THREADS( self );
+	if (res < 0)
+		return LDAPerror( self->ldap, "ldap_get_option" );
+	if (!is_string)
+		return PyInt_FromLong(intval);
+	if (strval != NULL)
+		return PyString_FromString(strval);
+	Py_INCREF(Py_None);
+	return Py_None;
+#else
+/* OpenLDAPv1 */
 #ifndef LDAP_TYPE_IS_OPAQUE
 	if (streq(name,"lberoptions")) 
 		return PyInt_FromLong(self->ldap->ld_lberoptions);
@@ -1791,6 +1857,7 @@
 		return PyInt_FromLong(self->valid);
 
 	return Py_FindMethod( methods, (PyObject*)self, name );
+#endif /* defined(LDAP_API_VERSION) */
 }
 
 /* set attribute */
@@ -1798,7 +1865,12 @@
 static int
 setattr( LDAPObject* self, char* name, PyObject* value ) 
 {
+#if defined(LDAP_API_VERSION)
+	int res, intval, option;
+	int *intptr = &intval;
+#else
 	long intval;
+#endif
 
 	if (streq(name,"errno") ||
 	    streq(name,"error") ||
@@ -1814,6 +1886,34 @@
 	    return -1;
 	}
 
+#if defined(LDAP_API_VERSION)
+/* OpenLDAPv2 */
+	if (streq(name,"deref")) 
+		option = LDAP_OPT_DEREF;
+	else if(streq(name,"version"))
+		option = LDAP_OPT_PROTOCOL_VERSION;
+	else if(streq(name,"referrals")) {
+		option = LDAP_OPT_REFERRALS;
+		intptr = (void *)intval;
+	} else if(streq(name,"restart")) {
+		option = LDAP_OPT_RESTART;
+		intptr = (void *)intval;
+	} else if (streq(name,"timelimit")) 
+		option = LDAP_OPT_TIMELIMIT;
+	else if (streq(name,"sizelimit")) 
+		option = LDAP_OPT_SIZELIMIT;
+	else {
+		PyErr_SetString( PyExc_NameError, "cannot set that field" );
+		return -1;
+	}
+	LDAP_BEGIN_ALLOW_THREADS( self );
+	res = ldap_set_option(self->ldap, option, intptr);
+	LDAP_END_ALLOW_THREADS( self );
+	if (res < 0)
+		return LDAPerror( self->ldap, "ldap_get_option" ), -1;
+	return 0;
+#else
+/* OpenLDAPv1 */
 #       define set(a,max)                                          \
 	if (streq(name,#a)) {                                       \
 	    if (intval < 0 || intval > max )                        \
@@ -1837,6 +1937,7 @@
 	/* it fell through to here */
 	PyErr_SetString( PyExc_NameError, "cannot set that field" );
 	return -1;
+#endif /* defined(LDAP_API_VERSION) */
 }
 
 /* type entry */
-------------- next part --------------
--- message.c.orig	Wed Nov 15 10:41:35 2000
+++ message.c	Wed Nov 15 16:51:18 2000
@@ -114,6 +114,40 @@
 	 PyList_Append(result, entrytuple);
 	 Py_DECREF(entrytuple);
      }
+#if defined(LDAP_API_VERSION)
+     for(entry = ldap_first_reference(ld,m);
+         entry != NULL;
+	 entry = ldap_next_reference(ld,entry))
+     {
+         char **refs = NULL;
+	 PyObject* entrytuple;
+	 PyObject* reflist = PyList_New(0);
+
+	 if (reflist == NULL)  {
+	     Py_DECREF(result);
+             ldap_msgfree( m );
+	     return NULL;
+	 }
+	 if (ldap_parse_reference(ld, entry, &refs, NULL, 0) != LDAP_SUCCESS) {
+	     Py_DECREF(result);
+             ldap_msgfree( m );
+	     return LDAPerror( ld, "ldap_parse_reference" );
+	 }
+	 if (refs) {
+	     int i;
+	     for (i=0; refs[i] != NULL; i++) {
+		 PyObject *refstr = PyString_FromString(refs[i]);
+		 PyList_Append(reflist, refstr);
+		 Py_DECREF(refstr);
+	     }
+	     ber_memvfree( (void **) refs );
+         }
+	 entrytuple = Py_BuildValue("(sO)", NULL, reflist);
+	 Py_DECREF(reflist);
+	 PyList_Append(result, entrytuple);
+	 Py_DECREF(entrytuple);
+     }
+#endif
      ldap_msgfree( m );
      return result;
 }


More information about the python-ldap mailing list