diff --git a/Modules/LDAPObject.c b/Modules/LDAPObject.c index d7265a42..2bd6209c 100644 --- a/Modules/LDAPObject.c +++ b/Modules/LDAPObject.c @@ -16,15 +16,16 @@ #include #endif -static void free_attrs(char***); +static void free_attrs(char ***); /* constructor */ -LDAPObject* -newLDAPObject( LDAP* l ) +LDAPObject * +newLDAPObject(LDAP *l) { - LDAPObject* self = (LDAPObject*) PyObject_NEW(LDAPObject, &LDAP_Type); - if (self == NULL) + LDAPObject *self = (LDAPObject *)PyObject_NEW(LDAPObject, &LDAP_Type); + + if (self == NULL) return NULL; self->ldap = l; self->_save = NULL; @@ -35,13 +36,13 @@ newLDAPObject( LDAP* l ) /* destructor */ static void -dealloc( LDAPObject* self ) +dealloc(LDAPObject *self) { if (self->ldap) { if (self->valid) { - LDAP_BEGIN_ALLOW_THREADS( self ); - ldap_unbind_ext( self->ldap, NULL, NULL ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldap_unbind_ext(self->ldap, NULL, NULL); + LDAP_END_ALLOW_THREADS(self); self->valid = 0; } self->ldap = NULL; @@ -59,11 +60,13 @@ dealloc( LDAPObject* self ) */ static int -not_valid( LDAPObject* l ) { +not_valid(LDAPObject *l) +{ if (l->valid) { return 0; - } else { - PyErr_SetString( LDAPexception_class, "LDAP connection invalid" ); + } + else { + PyErr_SetString(LDAPexception_class, "LDAP connection invalid"); return 1; } } @@ -71,7 +74,7 @@ not_valid( LDAPObject* l ) { /* free a LDAPMod (complete or partially) allocated in Tuple_to_LDAPMod() */ static void -LDAPMod_DEL( LDAPMod* lm ) +LDAPMod_DEL(LDAPMod *lm) { Py_ssize_t i; @@ -98,8 +101,8 @@ LDAPMod_DEL( LDAPMod* lm ) /* XXX - there is no way to pass complex-structured BER objects in here! */ -static LDAPMod* -Tuple_to_LDAPMod( PyObject* tup, int no_op ) +static LDAPMod * +Tuple_to_LDAPMod(PyObject *tup, int no_op) { int op; char *type; @@ -113,12 +116,13 @@ Tuple_to_LDAPMod( PyObject* tup, int no_op ) } if (no_op) { - if (!PyArg_ParseTuple( tup, "sO:Tuple_to_LDAPMod", &type, &list )) - return NULL; + if (!PyArg_ParseTuple(tup, "sO:Tuple_to_LDAPMod", &type, &list)) + return NULL; op = 0; - } else { - if (!PyArg_ParseTuple( tup, "isO:Tuple_to_LDAPMod", &op, &type, &list )) - return NULL; + } + else { + if (!PyArg_ParseTuple(tup, "isO:Tuple_to_LDAPMod", &op, &type, &list)) + return NULL; } lm = PyMem_NEW(LDAPMod, 1); @@ -130,43 +134,52 @@ Tuple_to_LDAPMod( PyObject* tup, int no_op ) len = strlen(type); lm->mod_type = PyMem_NEW(char, len + 1); + if (lm->mod_type == NULL) goto nomem; memcpy(lm->mod_type, type, len + 1); if (list == Py_None) { /* None indicates a NULL mod_bvals */ - } else if (PyBytes_Check(list)) { + } + else if (PyBytes_Check(list)) { /* Single string is a singleton list */ lm->mod_bvalues = PyMem_NEW(struct berval *, 2); + if (lm->mod_bvalues == NULL) goto nomem; lm->mod_bvalues[0] = PyMem_NEW(struct berval, 1); + if (lm->mod_bvalues[0] == NULL) goto nomem; lm->mod_bvalues[1] = NULL; lm->mod_bvalues[0]->bv_len = PyBytes_Size(list); lm->mod_bvalues[0]->bv_val = PyBytes_AsString(list); - } else if (PySequence_Check(list)) { + } + else if (PySequence_Check(list)) { nstrs = PySequence_Length(list); lm->mod_bvalues = PyMem_NEW(struct berval *, nstrs + 1); + if (lm->mod_bvalues == NULL) goto nomem; for (i = 0; i < nstrs; i++) { - lm->mod_bvalues[i] = PyMem_NEW(struct berval, 1); - if (lm->mod_bvalues[i] == NULL) - goto nomem; - lm->mod_bvalues[i+1] = NULL; - item = PySequence_GetItem(list, i); - if (item == NULL) - goto error; - if (!PyBytes_Check(item)) { - LDAPerror_TypeError("Tuple_to_LDAPMod(): expected a byte string in the list", item); - goto error; - } - lm->mod_bvalues[i]->bv_len = PyBytes_Size(item); - lm->mod_bvalues[i]->bv_val = PyBytes_AsString(item); - Py_DECREF(item); + lm->mod_bvalues[i] = PyMem_NEW(struct berval, 1); + + if (lm->mod_bvalues[i] == NULL) + goto nomem; + lm->mod_bvalues[i + 1] = NULL; + item = PySequence_GetItem(list, i); + if (item == NULL) + goto error; + if (!PyBytes_Check(item)) { + LDAPerror_TypeError + ("Tuple_to_LDAPMod(): expected a byte string in the list", + item); + goto error; + } + lm->mod_bvalues[i]->bv_len = PyBytes_Size(item); + lm->mod_bvalues[i]->bv_val = PyBytes_AsString(item); + Py_DECREF(item); } if (nstrs == 0) lm->mod_bvalues[0] = NULL; @@ -174,10 +187,10 @@ Tuple_to_LDAPMod( PyObject* tup, int no_op ) return lm; -nomem: + nomem: PyErr_NoMemory(); -error: - if (lm) + error: + if (lm) LDAPMod_DEL(lm); return NULL; @@ -186,10 +199,12 @@ Tuple_to_LDAPMod( PyObject* tup, int no_op ) /* free the structure allocated in List_to_LDAPMods() */ static void -LDAPMods_DEL( LDAPMod** lms ) { - LDAPMod** lmp; - for ( lmp = lms; *lmp; lmp++ ) - LDAPMod_DEL( *lmp ); +LDAPMods_DEL(LDAPMod **lms) +{ + LDAPMod **lmp; + + for (lmp = lms; *lmp; lmp++) + LDAPMod_DEL(*lmp); PyMem_DEL(lms); } @@ -198,33 +213,36 @@ LDAPMods_DEL( LDAPMod** lms ) { * NOTE: list of tuples must live longer than the LDAPMods */ -static LDAPMod** -List_to_LDAPMods( PyObject *list, int no_op ) { +static LDAPMod ** +List_to_LDAPMods(PyObject *list, int no_op) +{ Py_ssize_t i, len; - LDAPMod** lms; + LDAPMod **lms; PyObject *item; if (!PySequence_Check(list)) { - LDAPerror_TypeError("List_to_LDAPMods(): expected list of tuples", list); + LDAPerror_TypeError("List_to_LDAPMods(): expected list of tuples", + list); return NULL; } len = PySequence_Length(list); if (len < 0) { - LDAPerror_TypeError("List_to_LDAPMods(): expected list of tuples", list); + LDAPerror_TypeError("List_to_LDAPMods(): expected list of tuples", + list); return NULL; } lms = PyMem_NEW(LDAPMod *, len + 1); - if (lms == NULL) + if (lms == NULL) goto nomem; for (i = 0; i < len; i++) { lms[i] = NULL; item = PySequence_GetItem(list, i); - if (item == NULL) + if (item == NULL) goto error; lms[i] = Tuple_to_LDAPMod(item, no_op); Py_DECREF(item); @@ -234,9 +252,9 @@ List_to_LDAPMods( PyObject *list, int no_op ) { lms[len] = NULL; return lms; -nomem: + nomem: PyErr_NoMemory(); -error: + error: if (lms) LDAPMods_DEL(lms); return NULL; @@ -248,7 +266,8 @@ List_to_LDAPMods( PyObject *list, int no_op ) { */ int -attrs_from_List( PyObject *attrlist, char***attrsp) { +attrs_from_List(PyObject *attrlist, char ***attrsp) +{ char **attrs = NULL; PyObject *seq = NULL; @@ -256,17 +275,22 @@ attrs_from_List( PyObject *attrlist, char***attrsp) { if (attrlist == Py_None) { /* None means a NULL attrlist */ #if PY_MAJOR_VERSION == 2 - } else if (PyBytes_Check(attrlist)) { + } + else if (PyBytes_Check(attrlist)) { #else - } else if (PyUnicode_Check(attrlist)) { + } + else if (PyUnicode_Check(attrlist)) { #endif /* caught by John Benninghoff */ - LDAPerror_TypeError( - "attrs_from_List(): expected *list* of strings, not a string", attrlist); + LDAPerror_TypeError + ("attrs_from_List(): expected *list* of strings, not a string", + attrlist); goto error; - } else { + } + else { PyObject *item = NULL; Py_ssize_t i, len, strlen; + #if PY_MAJOR_VERSION >= 3 const char *str; #else @@ -280,8 +304,9 @@ attrs_from_List( PyObject *attrlist, char***attrsp) { len = PySequence_Length(attrlist); attrs = PyMem_NEW(char *, len + 1); + if (attrs == NULL) - goto nomem; + goto nomem; for (i = 0; i < len; i++) { attrs[i] = NULL; @@ -291,7 +316,8 @@ attrs_from_List( PyObject *attrlist, char***attrsp) { #if PY_MAJOR_VERSION == 2 /* Encoded in Python to UTF-8 */ if (!PyBytes_Check(item)) { - LDAPerror_TypeError("attrs_from_List(): expected bytes in list", item); + LDAPerror_TypeError + ("attrs_from_List(): expected bytes in list", item); goto error; } if (PyBytes_AsStringAndSize(item, &str, &strlen) == -1) { @@ -299,7 +325,8 @@ attrs_from_List( PyObject *attrlist, char***attrsp) { } #else if (!PyUnicode_Check(item)) { - LDAPerror_TypeError("attrs_from_List(): expected string in list", item); + LDAPerror_TypeError + ("attrs_from_List(): expected string in list", item); goto error; } str = PyUnicode_AsUTF8AndSize(item, &strlen); @@ -309,6 +336,7 @@ attrs_from_List( PyObject *attrlist, char***attrsp) { * 3.7 actually returns a const char. */ attrs[i] = (char *)PyMem_NEW(char *, strlen + 1); + if (attrs[i] == NULL) goto nomem; memcpy(attrs[i], str, strlen + 1); @@ -320,9 +348,9 @@ attrs_from_List( PyObject *attrlist, char***attrsp) { *attrsp = attrs; return 1; -nomem: + nomem: PyErr_NoMemory(); -error: + error: Py_XDECREF(seq); free_attrs(&attrs); return 0; @@ -331,7 +359,8 @@ attrs_from_List( PyObject *attrlist, char***attrsp) { /* free memory allocated from above routine */ static void -free_attrs( char*** attrsp) { +free_attrs(char ***attrsp) +{ char **attrs = *attrsp; char **p; @@ -351,18 +380,20 @@ free_attrs( char*** attrsp) { /* ldap_unbind_ext */ -static PyObject* -l_ldap_unbind_ext( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_unbind_ext(LDAPObject *self, PyObject *args) { PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int ldaperror; - if (!PyArg_ParseTuple( args, "|OO:unbind_ext", &serverctrls, &clientctrls)) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple(args, "|OO:unbind_ext", &serverctrls, &clientctrls)) + return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -371,20 +402,20 @@ l_ldap_unbind_ext( LDAPObject* self, PyObject* args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_unbind_ext( self->ldap, server_ldcs, client_ldcs ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = ldap_unbind_ext(self->ldap, server_ldcs, client_ldcs); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_unbind_ext" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_unbind_ext"); self->valid = 0; Py_INCREF(Py_None); @@ -393,19 +424,22 @@ l_ldap_unbind_ext( LDAPObject* self, PyObject* args ) /* ldap_abandon_ext */ -static PyObject* -l_ldap_abandon_ext( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_abandon_ext(LDAPObject *self, PyObject *args) { int msgid; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int ldaperror; - if (!PyArg_ParseTuple( args, "i|OO:abandon_ext", &msgid, &serverctrls, &clientctrls)) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple + (args, "i|OO:abandon_ext", &msgid, &serverctrls, &clientctrls)) + return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -414,20 +448,20 @@ l_ldap_abandon_ext( LDAPObject* self, PyObject* args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_abandon_ext( self->ldap, msgid, server_ldcs, client_ldcs ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = ldap_abandon_ext(self->ldap, msgid, server_ldcs, client_ldcs); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_abandon_ext" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_abandon_ext"); Py_INCREF(Py_None); return Py_None; @@ -436,58 +470,62 @@ l_ldap_abandon_ext( LDAPObject* self, PyObject* args ) /* ldap_add_ext */ static PyObject * -l_ldap_add_ext( LDAPObject* self, PyObject *args ) +l_ldap_add_ext(LDAPObject *self, PyObject *args) { char *dn; PyObject *modlist; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int msgid; int ldaperror; LDAPMod **mods; - if (!PyArg_ParseTuple( args, "sO|OO:add_ext", &dn, &modlist, &serverctrls, &clientctrls )) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple + (args, "sO|OO:add_ext", &dn, &modlist, &serverctrls, &clientctrls)) + return NULL; + if (not_valid(self)) + return NULL; - mods = List_to_LDAPMods( modlist, 1 ); + mods = List_to_LDAPMods(modlist, 1); if (mods == NULL) return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) { - LDAPMods_DEL( mods ); + LDAPMods_DEL(mods); return NULL; } } if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPMods_DEL( mods ); - LDAPControl_List_DEL( server_ldcs ); + LDAPMods_DEL(mods); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_add_ext( self->ldap, dn, mods, server_ldcs, client_ldcs, &msgid); - LDAP_END_ALLOW_THREADS( self ); - LDAPMods_DEL( mods ); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = + ldap_add_ext(self->ldap, dn, mods, server_ldcs, client_ldcs, &msgid); + LDAP_END_ALLOW_THREADS(self); + LDAPMods_DEL(mods); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_add_ext" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_add_ext"); return PyInt_FromLong(msgid); } /* ldap_simple_bind */ -static PyObject* -l_ldap_simple_bind( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_simple_bind(LDAPObject *self, PyObject *args) { char *who; int msgid; @@ -495,14 +533,18 @@ l_ldap_simple_bind( LDAPObject* self, PyObject* args ) Py_ssize_t cred_len; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; struct berval cred; - if (!PyArg_ParseTuple( args, "zz#|OO:simple_bind", &who, &cred.bv_val, &cred_len, &serverctrls, &clientctrls )) return NULL; + if (!PyArg_ParseTuple + (args, "zz#|OO:simple_bind", &who, &cred.bv_val, &cred_len, + &serverctrls, &clientctrls)) + return NULL; cred.bv_len = (ber_len_t) cred_len; - if (not_valid(self)) return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -511,25 +553,26 @@ l_ldap_simple_bind( LDAPObject* self, PyObject* args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_sasl_bind( self->ldap, who, LDAP_SASL_SIMPLE, &cred, server_ldcs, client_ldcs, &msgid); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = + ldap_sasl_bind(self->ldap, who, LDAP_SASL_SIMPLE, &cred, server_ldcs, + client_ldcs, &msgid); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_simple_bind" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_simple_bind"); - return PyInt_FromLong( msgid ); + return PyInt_FromLong(msgid); } - #ifdef HAVE_SASL /* The following functions implement SASL binds. A new method sasl_interactive_bind_s(bind_dn, sasl_mechanism) has been introduced. @@ -566,46 +609,41 @@ l_ldap_simple_bind( LDAPObject* self, PyObject* args ) argument specifies, which information should be passed back to the SASL lib (see SASL_CB_xxx in sasl.h) */ -static int interaction ( unsigned flags, - sasl_interact_t *interact, - PyObject* SASLObject ) +static int +interaction(unsigned flags, sasl_interact_t *interact, PyObject *SASLObject) { /* const char *dflt = interact->defresult; */ - PyObject *result; - char *c_result; - result = PyObject_CallMethod(SASLObject, - "callback", - "isss", - interact->id, /* see sasl.h */ - interact->challenge, - interact->prompt, - interact->defresult); - - if (result == NULL) - /*searching for a better error code */ - return LDAP_OPERATIONS_ERROR; - c_result = PyBytes_AsString(result); /*xxx Error checking?? */ - - /* according to the sasl docs, we should malloc() the returned - string only for calls where interact->id == SASL_CB_PASS, so we - probably leak a few bytes per ldap bind. However, if I restrict - the strdup() to this case, I get segfaults. Should probably be - fixed sometimes. - */ - interact->result = strdup( c_result ); - if (interact->result == NULL) - return LDAP_OPERATIONS_ERROR; - interact->len = strlen(c_result); - /* We _should_ overwrite the python string buffer for security - reasons, however we may not (api/stringObjects.html). Any ideas? - */ - - Py_DECREF(result); /*not needed any longer */ - result = NULL; - - return LDAP_SUCCESS; -} + PyObject *result; + char *c_result; + + result = PyObject_CallMethod(SASLObject, "callback", "isss", interact->id, /* see sasl.h */ + interact->challenge, + interact->prompt, interact->defresult); + + if (result == NULL) + /*searching for a better error code */ + return LDAP_OPERATIONS_ERROR; + c_result = PyBytes_AsString(result); /*xxx Error checking?? */ + + /* according to the sasl docs, we should malloc() the returned + string only for calls where interact->id == SASL_CB_PASS, so we + probably leak a few bytes per ldap bind. However, if I restrict + the strdup() to this case, I get segfaults. Should probably be + fixed sometimes. + */ + interact->result = strdup(c_result); + if (interact->result == NULL) + return LDAP_OPERATIONS_ERROR; + interact->len = strlen(c_result); + /* We _should_ overwrite the python string buffer for security + reasons, however we may not (api/stringObjects.html). Any ideas? + */ + Py_DECREF(result); /*not needed any longer */ + result = NULL; + + return LDAP_SUCCESS; +} /* This function will be called by ldap_sasl_interactive_bind(). The @@ -615,26 +653,27 @@ static int interaction ( unsigned flags, */ -int py_ldap_sasl_interaction( LDAP *ld, - unsigned flags, - void *defaults, - void *in ) +int +py_ldap_sasl_interaction(LDAP *ld, unsigned flags, void *defaults, void *in) { - /* These are just typecasts */ - sasl_interact_t *interact = (sasl_interact_t *) in; - PyObject *SASLObject = (PyObject *) defaults; - /* Loop over the array of sasl_interact_t structs */ - while( interact->id != SASL_CB_LIST_END ) { - int rc = 0; - rc = interaction( flags, interact, SASLObject ); - if( rc ) return rc; - interact++; - } - return LDAP_SUCCESS; + /* These are just typecasts */ + sasl_interact_t *interact = (sasl_interact_t *)in; + PyObject *SASLObject = (PyObject *)defaults; + + /* Loop over the array of sasl_interact_t structs */ + while (interact->id != SASL_CB_LIST_END) { + int rc = 0; + + rc = interaction(flags, interact, SASLObject); + if (rc) + return rc; + interact++; + } + return LDAP_SUCCESS; } -static PyObject* -l_ldap_sasl_bind_s( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_sasl_bind_s(LDAPObject *self, PyObject *args) { const char *dn; const char *mechanism; @@ -643,16 +682,19 @@ l_ldap_sasl_bind_s( LDAPObject* self, PyObject* args ) PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; struct berval *servercred; int ldaperror; - if (!PyArg_ParseTuple(args, "zzz#OO:sasl_bind_s", &dn, &mechanism, &cred.bv_val, &cred_len, &serverctrls, &clientctrls )) + if (!PyArg_ParseTuple + (args, "zzz#OO:sasl_bind_s", &dn, &mechanism, &cred.bv_val, &cred_len, + &serverctrls, &clientctrls)) return NULL; - if (not_valid(self)) return NULL; + if (not_valid(self)) + return NULL; cred.bv_len = cred_len; @@ -662,44 +704,45 @@ l_ldap_sasl_bind_s( LDAPObject* self, PyObject* args ) } if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); ldaperror = ldap_sasl_bind_s(self->ldap, dn, mechanism, cred.bv_val ? &cred : NULL, - (LDAPControl**) server_ldcs, - (LDAPControl**) client_ldcs, - &servercred); - LDAP_END_ALLOW_THREADS( self ); + (LDAPControl **)server_ldcs, + (LDAPControl **)client_ldcs, &servercred); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); if (ldaperror == LDAP_SASL_BIND_IN_PROGRESS) { if (servercred && servercred->bv_val && *servercred->bv_val) - return PyBytes_FromStringAndSize( servercred->bv_val, servercred->bv_len ); - } else if (ldaperror != LDAP_SUCCESS) - return LDAPerror( self->ldap, "l_ldap_sasl_bind_s" ); - return PyInt_FromLong( ldaperror ); + return PyBytes_FromStringAndSize(servercred->bv_val, + servercred->bv_len); + } + else if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "l_ldap_sasl_bind_s"); + return PyInt_FromLong(ldaperror); } -static PyObject* -l_ldap_sasl_interactive_bind_s( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_sasl_interactive_bind_s(LDAPObject *self, PyObject *args) { char *c_mechanism; char *who; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; - PyObject *SASLObject = NULL; + PyObject *SASLObject = NULL; PyObject *mechanism = NULL; int msgid; @@ -713,13 +756,18 @@ l_ldap_sasl_interactive_bind_s( LDAPObject* self, PyObject* args ) * "i" otherwise. */ #if (PY_MAJOR_VERSION == 2) && (PY_MINOR_VERSION < 3) - if (!PyArg_ParseTuple(args, "sOOOi:sasl_interactive_bind_s", &who, &SASLObject, &serverctrls, &clientctrls, &sasl_flags )) + if (!PyArg_ParseTuple + (args, "sOOOi:sasl_interactive_bind_s", &who, &SASLObject, + &serverctrls, &clientctrls, &sasl_flags)) #else - if (!PyArg_ParseTuple(args, "sOOOI:sasl_interactive_bind_s", &who, &SASLObject, &serverctrls, &clientctrls, &sasl_flags )) + if (!PyArg_ParseTuple + (args, "sOOOI:sasl_interactive_bind_s", &who, &SASLObject, + &serverctrls, &clientctrls, &sasl_flags)) #endif - return NULL; + return NULL; - if (not_valid(self)) return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -728,14 +776,15 @@ l_ldap_sasl_interactive_bind_s( LDAPObject* self, PyObject* args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } /* now we extract the sasl mechanism from the SASL Object */ mechanism = PyObject_GetAttrString(SASLObject, "mech"); - if (mechanism == NULL) return NULL; + if (mechanism == NULL) + return NULL; c_mechanism = PyBytes_AsString(mechanism); Py_DECREF(mechanism); mechanism = NULL; @@ -745,43 +794,44 @@ l_ldap_sasl_interactive_bind_s( LDAPObject* self, PyObject* args ) Python object SASLObject, but passing it through some static variable would destroy thread safety, IMHO. */ - msgid = ldap_sasl_interactive_bind_s(self->ldap, - who, - c_mechanism, - (LDAPControl**) server_ldcs, - (LDAPControl**) client_ldcs, - sasl_flags, - py_ldap_sasl_interaction, - SASLObject); - - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + msgid = ldap_sasl_interactive_bind_s(self->ldap, + who, + c_mechanism, + (LDAPControl **)server_ldcs, + (LDAPControl **)client_ldcs, + sasl_flags, + py_ldap_sasl_interaction, SASLObject); + + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); if (msgid != LDAP_SUCCESS) - return LDAPerror( self->ldap, "ldap_sasl_interactive_bind_s" ); - return PyInt_FromLong( msgid ); + return LDAPerror(self->ldap, "ldap_sasl_interactive_bind_s"); + return PyInt_FromLong(msgid); } #endif - #ifdef LDAP_API_FEATURE_CANCEL /* ldap_cancel */ -static PyObject* -l_ldap_cancel( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_cancel(LDAPObject *self, PyObject *args) { int msgid; int cancelid; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int ldaperror; - if (!PyArg_ParseTuple( args, "i|OO:cancel", &cancelid, &serverctrls, &clientctrls)) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple + (args, "i|OO:cancel", &cancelid, &serverctrls, &clientctrls)) + return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -790,22 +840,23 @@ l_ldap_cancel( LDAPObject* self, PyObject* args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_cancel( self->ldap, cancelid, server_ldcs, client_ldcs, &msgid ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = + ldap_cancel(self->ldap, cancelid, server_ldcs, client_ldcs, &msgid); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_cancel" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_cancel"); - return PyInt_FromLong( msgid ); + return PyInt_FromLong(msgid); } #endif @@ -813,23 +864,27 @@ l_ldap_cancel( LDAPObject* self, PyObject* args ) /* ldap_compare_ext */ static PyObject * -l_ldap_compare_ext( LDAPObject* self, PyObject *args ) +l_ldap_compare_ext(LDAPObject *self, PyObject *args) { char *dn, *attr; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int msgid; int ldaperror; Py_ssize_t value_len; struct berval value; - if (!PyArg_ParseTuple( args, "sss#|OO:compare_ext", &dn, &attr, &value.bv_val, &value_len, &serverctrls, &clientctrls )) return NULL; + if (!PyArg_ParseTuple + (args, "sss#|OO:compare_ext", &dn, &attr, &value.bv_val, &value_len, + &serverctrls, &clientctrls)) + return NULL; value.bv_len = (ber_len_t) value_len; - if (not_valid(self)) return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -838,41 +893,45 @@ l_ldap_compare_ext( LDAPObject* self, PyObject *args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_compare_ext( self->ldap, dn, attr, &value, server_ldcs, client_ldcs, &msgid ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = + ldap_compare_ext(self->ldap, dn, attr, &value, server_ldcs, + client_ldcs, &msgid); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_compare_ext" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_compare_ext"); - return PyInt_FromLong( msgid ); + return PyInt_FromLong(msgid); } - /* ldap_delete_ext */ static PyObject * -l_ldap_delete_ext( LDAPObject* self, PyObject *args ) +l_ldap_delete_ext(LDAPObject *self, PyObject *args) { char *dn; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int msgid; int ldaperror; - if (!PyArg_ParseTuple( args, "s|OO:delete_ext", &dn, &serverctrls, &clientctrls )) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple + (args, "s|OO:delete_ext", &dn, &serverctrls, &clientctrls)) + return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -881,97 +940,104 @@ l_ldap_delete_ext( LDAPObject* self, PyObject *args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_delete_ext( self->ldap, dn, server_ldcs, client_ldcs, &msgid ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = + ldap_delete_ext(self->ldap, dn, server_ldcs, client_ldcs, &msgid); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_delete_ext" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_delete_ext"); return PyInt_FromLong(msgid); } - /* ldap_modify_ext */ static PyObject * -l_ldap_modify_ext( LDAPObject* self, PyObject *args ) +l_ldap_modify_ext(LDAPObject *self, PyObject *args) { char *dn; PyObject *modlist; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int msgid; int ldaperror; LDAPMod **mods; - if (!PyArg_ParseTuple( args, "sO|OO:modify_ext", &dn, &modlist, &serverctrls, &clientctrls )) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple + (args, "sO|OO:modify_ext", &dn, &modlist, &serverctrls, &clientctrls)) + return NULL; + if (not_valid(self)) + return NULL; - mods = List_to_LDAPMods( modlist, 0 ); + mods = List_to_LDAPMods(modlist, 0); if (mods == NULL) return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) { - LDAPMods_DEL( mods ); + LDAPMods_DEL(mods); return NULL; } } if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPMods_DEL( mods ); - LDAPControl_List_DEL( server_ldcs ); + LDAPMods_DEL(mods); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_modify_ext( self->ldap, dn, mods, server_ldcs, client_ldcs, &msgid ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = + ldap_modify_ext(self->ldap, dn, mods, server_ldcs, client_ldcs, + &msgid); + LDAP_END_ALLOW_THREADS(self); - LDAPMods_DEL( mods ); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPMods_DEL(mods); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_modify_ext" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_modify_ext"); - return PyInt_FromLong( msgid ); + return PyInt_FromLong(msgid); } - /* ldap_rename */ static PyObject * -l_ldap_rename( LDAPObject* self, PyObject *args ) +l_ldap_rename(LDAPObject *self, PyObject *args) { char *dn, *newrdn; char *newSuperior = NULL; int delold = 1; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int msgid; int ldaperror; - if (!PyArg_ParseTuple( args, "ss|ziOO:rename", &dn, &newrdn, &newSuperior, &delold, &serverctrls, &clientctrls )) + if (!PyArg_ParseTuple + (args, "ss|ziOO:rename", &dn, &newrdn, &newSuperior, &delold, + &serverctrls, &clientctrls)) + return NULL; + if (not_valid(self)) return NULL; - if (not_valid(self)) return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -980,29 +1046,30 @@ l_ldap_rename( LDAPObject* self, PyObject *args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_rename( self->ldap, dn, newrdn, newSuperior, delold, server_ldcs, client_ldcs, &msgid ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = + ldap_rename(self->ldap, dn, newrdn, newSuperior, delold, server_ldcs, + client_ldcs, &msgid); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_rename" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_rename"); - return PyInt_FromLong( msgid ); + return PyInt_FromLong(msgid); } - /* ldap_result4 */ static PyObject * -l_ldap_result4( LDAPObject* self, PyObject *args ) +l_ldap_result4(LDAPObject *self, PyObject *args) { int msgid = LDAP_RES_ANY; int all = 1; @@ -1011,7 +1078,7 @@ l_ldap_result4( LDAPObject* self, PyObject *args ) int add_intermediates = 0; int add_extop = 0; struct timeval tv; - struct timeval* tvp; + struct timeval *tvp; int res_type; LDAPMessage *msg = NULL; PyObject *result_str, *retval, *pmsg, *pyctrls = 0; @@ -1022,31 +1089,38 @@ l_ldap_result4( LDAPObject* self, PyObject *args ) char **refs = NULL; LDAPControl **serverctrls = 0; - if (!PyArg_ParseTuple( args, "|iidiii:result4", &msgid, &all, &timeout, &add_ctrls, &add_intermediates, &add_extop )) + if (!PyArg_ParseTuple + (args, "|iidiii:result4", &msgid, &all, &timeout, &add_ctrls, + &add_intermediates, &add_extop)) return NULL; - if (not_valid(self)) return NULL; - + if (not_valid(self)) + return NULL; + if (timeout >= 0) { tvp = &tv; - set_timeval_from_double( tvp, timeout ); - } else { + set_timeval_from_double(tvp, timeout); + } + else { tvp = NULL; } - LDAP_BEGIN_ALLOW_THREADS( self ); - res_type = ldap_result( self->ldap, msgid, all, tvp, &msg ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + res_type = ldap_result(self->ldap, msgid, all, tvp, &msg); + LDAP_END_ALLOW_THREADS(self); if (res_type < 0) /* LDAP or system error */ - return LDAPerror( self->ldap, "ldap_result4" ); + return LDAPerror(self->ldap, "ldap_result4"); if (res_type == 0) { /* Polls return (None, None, None, None); timeouts raise an exception */ if (timeout == 0) { if (add_extop) { - return Py_BuildValue("(OOOOOO)", Py_None, Py_None, Py_None, Py_None, Py_None, Py_None); - } else { - return Py_BuildValue("(OOOO)", Py_None, Py_None, Py_None, Py_None); + return Py_BuildValue("(OOOOOO)", Py_None, Py_None, Py_None, + Py_None, Py_None, Py_None); + } + else { + return Py_BuildValue("(OOOO)", Py_None, Py_None, Py_None, + Py_None); } } else @@ -1058,75 +1132,90 @@ l_ldap_result4( LDAPObject* self, PyObject *args ) if (res_type == LDAP_RES_SEARCH_ENTRY) { /* LDAPmessage_to_python will parse entries and read the controls for each entry */ - } else if (res_type == LDAP_RES_SEARCH_REFERENCE) { + } + else if (res_type == LDAP_RES_SEARCH_REFERENCE) { /* LDAPmessage_to_python will parse refs and read the controls for each res */ - } else if (res_type == LDAP_RES_INTERMEDIATE) { + } + else if (res_type == LDAP_RES_INTERMEDIATE) { /* LDAPmessage_to_python will parse intermediates and controls */ - } else { + } + else { int rc; + if (res_type == LDAP_RES_EXTENDED) { struct berval *retdata = 0; - LDAP_BEGIN_ALLOW_THREADS( self ); - rc = ldap_parse_extended_result( self->ldap, msg, &retoid, &retdata, 0 ); - LDAP_END_ALLOW_THREADS( self ); + + LDAP_BEGIN_ALLOW_THREADS(self); + rc = ldap_parse_extended_result(self->ldap, msg, &retoid, &retdata, + 0); + LDAP_END_ALLOW_THREADS(self); /* handle error rc!=0 here? */ if (rc == LDAP_SUCCESS) { valuestr = LDAPberval_to_object(retdata); } - ber_bvfree( retdata ); + ber_bvfree(retdata); } - - LDAP_BEGIN_ALLOW_THREADS( self ); - rc = ldap_parse_result( self->ldap, msg, &result, NULL, NULL, &refs, - &serverctrls, 0 ); - LDAP_END_ALLOW_THREADS( self ); + + LDAP_BEGIN_ALLOW_THREADS(self); + rc = ldap_parse_result(self->ldap, msg, &result, NULL, NULL, &refs, + &serverctrls, 0); + LDAP_END_ALLOW_THREADS(self); } - if (result != LDAP_SUCCESS) { /* result error */ + if (result != LDAP_SUCCESS) { /* result error */ char *e, err[1024]; + if (result == LDAP_REFERRAL && refs && refs[0]) { snprintf(err, sizeof(err), "Referral:\n%s", refs[0]); e = err; - } else + } + else e = "ldap_parse_result"; ldap_msgfree(msg); Py_XDECREF(valuestr); - return LDAPerror( self->ldap, e ); + return LDAPerror(self->ldap, e); } if (!(pyctrls = LDAPControls_to_List(serverctrls))) { int err = LDAP_NO_MEMORY; - LDAP_BEGIN_ALLOW_THREADS( self ); + + LDAP_BEGIN_ALLOW_THREADS(self); ldap_set_option(self->ldap, LDAP_OPT_ERROR_NUMBER, &err); - LDAP_END_ALLOW_THREADS( self ); + LDAP_END_ALLOW_THREADS(self); ldap_msgfree(msg); Py_XDECREF(valuestr); return LDAPerror(self->ldap, "LDAPControls_to_List"); } ldap_controls_free(serverctrls); - pmsg = LDAPmessage_to_python( self->ldap, msg, add_ctrls, add_intermediates ); + pmsg = + LDAPmessage_to_python(self->ldap, msg, add_ctrls, add_intermediates); if (res_type == 0) { result_str = Py_None; Py_INCREF(Py_None); - } else { - result_str = PyInt_FromLong( res_type ); + } + else { + result_str = PyInt_FromLong(res_type); } if (pmsg == NULL) { - retval = NULL; - } else { + retval = NULL; + } + else { /* s handles NULL, but O does not */ if (add_extop) { retval = Py_BuildValue("(OOiOsO)", result_str, pmsg, res_msgid, - pyctrls, retoid, valuestr ? valuestr : Py_None); - } else { - retval = Py_BuildValue("(OOiO)", result_str, pmsg, res_msgid, pyctrls); + pyctrls, retoid, + valuestr ? valuestr : Py_None); + } + else { + retval = + Py_BuildValue("(OOiO)", result_str, pmsg, res_msgid, pyctrls); } if (pmsg != Py_None) { - Py_DECREF(pmsg); + Py_DECREF(pmsg); } } Py_XDECREF(valuestr); @@ -1135,11 +1224,10 @@ l_ldap_result4( LDAPObject* self, PyObject *args ) return retval; } - /* ldap_search_ext */ -static PyObject* -l_ldap_search_ext( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_search_ext(LDAPObject *self, PyObject *args) { char *base; int scope; @@ -1150,73 +1238,76 @@ l_ldap_search_ext( LDAPObject* self, PyObject* args ) PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; double timeout = -1.0; struct timeval tv; - struct timeval* tvp; + struct timeval *tvp; int sizelimit = 0; int msgid; int ldaperror; - if (!PyArg_ParseTuple( args, "sis|OiOOdi:search_ext", - &base, &scope, &filter, &attrlist, &attrsonly, - &serverctrls, &clientctrls, &timeout, &sizelimit )) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple(args, "sis|OiOOdi:search_ext", + &base, &scope, &filter, &attrlist, &attrsonly, + &serverctrls, &clientctrls, &timeout, &sizelimit)) + return NULL; + if (not_valid(self)) + return NULL; - if (!attrs_from_List( attrlist, &attrs )) - return NULL; + if (!attrs_from_List(attrlist, &attrs)) + return NULL; if (timeout >= 0) { tvp = &tv; - set_timeval_from_double( tvp, timeout ); - } else { + set_timeval_from_double(tvp, timeout); + } + else { tvp = NULL; } if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) { - free_attrs( &attrs ); + free_attrs(&attrs); return NULL; } } if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - free_attrs( &attrs ); - LDAPControl_List_DEL( server_ldcs ); + free_attrs(&attrs); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_search_ext( self->ldap, base, scope, filter, attrs, attrsonly, - server_ldcs, client_ldcs, tvp, sizelimit, &msgid ); - LDAP_END_ALLOW_THREADS( self ); - - free_attrs( &attrs ); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = + ldap_search_ext(self->ldap, base, scope, filter, attrs, attrsonly, + server_ldcs, client_ldcs, tvp, sizelimit, &msgid); + LDAP_END_ALLOW_THREADS(self); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_search_ext" ); + free_attrs(&attrs); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - return PyInt_FromLong( msgid ); -} + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_search_ext"); + return PyInt_FromLong(msgid); +} /* ldap_whoami_s (available since OpenLDAP 2.1.13) */ -static PyObject* -l_ldap_whoami_s( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_whoami_s(LDAPObject *self, PyObject *args) { PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; struct berval *bvalue = NULL; @@ -1224,8 +1315,10 @@ l_ldap_whoami_s( LDAPObject* self, PyObject* args ) int ldaperror; - if (!PyArg_ParseTuple( args, "|OO:whoami_s", &serverctrls, &clientctrls)) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple(args, "|OO:whoami_s", &serverctrls, &clientctrls)) + return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -1234,21 +1327,21 @@ l_ldap_whoami_s( LDAPObject* self, PyObject* args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_whoami_s( self->ldap, &bvalue, server_ldcs, client_ldcs ); - LDAP_END_ALLOW_THREADS( self ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = ldap_whoami_s(self->ldap, &bvalue, server_ldcs, client_ldcs); + LDAP_END_ALLOW_THREADS(self); - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) { + if (ldaperror != LDAP_SUCCESS) { ber_bvfree(bvalue); - return LDAPerror( self->ldap, "ldap_whoami_s" ); + return LDAPerror(self->ldap, "ldap_whoami_s"); } result = LDAPberval_to_unicode_object(bvalue); @@ -1260,20 +1353,22 @@ l_ldap_whoami_s( LDAPObject* self, PyObject* args ) #ifdef HAVE_TLS /* ldap_start_tls_s */ -static PyObject* -l_ldap_start_tls_s( LDAPObject* self, PyObject* args ) +static PyObject * +l_ldap_start_tls_s(LDAPObject *self, PyObject *args) { int ldaperror; - if (!PyArg_ParseTuple( args, ":start_tls_s" )) return NULL; - if (not_valid(self)) return NULL; + if (!PyArg_ParseTuple(args, ":start_tls_s")) + return NULL; + if (not_valid(self)) + return NULL; - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_start_tls_s( self->ldap, NULL, NULL ); - LDAP_END_ALLOW_THREADS( self ); - if ( ldaperror != LDAP_SUCCESS ){ + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = ldap_start_tls_s(self->ldap, NULL, NULL); + LDAP_END_ALLOW_THREADS(self); + if (ldaperror != LDAP_SUCCESS) { ldap_set_option(self->ldap, LDAP_OPT_ERROR_NUMBER, &ldaperror); - return LDAPerror( self->ldap, "ldap_start_tls_s" ); + return LDAPerror(self->ldap, "ldap_start_tls_s"); } Py_INCREF(Py_None); @@ -1284,8 +1379,8 @@ l_ldap_start_tls_s( LDAPObject* self, PyObject* args ) /* ldap_set_option */ -static PyObject* -l_ldap_set_option(PyObject* self, PyObject *args) +static PyObject * +l_ldap_set_option(PyObject *self, PyObject *args) { PyObject *value; int option; @@ -1298,11 +1393,10 @@ l_ldap_set_option(PyObject* self, PyObject *args) return Py_None; } - /* ldap_get_option */ -static PyObject* -l_ldap_get_option(PyObject* self, PyObject *args) +static PyObject * +l_ldap_get_option(PyObject *self, PyObject *args) { int option; @@ -1311,11 +1405,10 @@ l_ldap_get_option(PyObject* self, PyObject *args) return LDAP_get_option((LDAPObject *)self, option); } - /* ldap_passwd */ static PyObject * -l_ldap_passwd( LDAPObject* self, PyObject *args ) +l_ldap_passwd(LDAPObject *self, PyObject *args) { struct berval user; Py_ssize_t user_len; @@ -1325,20 +1418,23 @@ l_ldap_passwd( LDAPObject* self, PyObject *args ) Py_ssize_t newpw_len; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int msgid; int ldaperror; - if (!PyArg_ParseTuple( args, "z#z#z#|OO:passwd", &user.bv_val, &user_len, &oldpw.bv_val, &oldpw_len, &newpw.bv_val, &newpw_len, &serverctrls, &clientctrls )) + if (!PyArg_ParseTuple + (args, "z#z#z#|OO:passwd", &user.bv_val, &user_len, &oldpw.bv_val, + &oldpw_len, &newpw.bv_val, &newpw_len, &serverctrls, &clientctrls)) return NULL; user.bv_len = (ber_len_t) user_len; oldpw.bv_len = (ber_len_t) oldpw_len; newpw.bv_len = (ber_len_t) newpw_len; - - if (not_valid(self)) return NULL; + + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -1347,50 +1443,50 @@ l_ldap_passwd( LDAPObject* self, PyObject *args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_passwd( self->ldap, - user.bv_val != NULL ? &user : NULL, - oldpw.bv_val != NULL ? &oldpw : NULL, - newpw.bv_val != NULL ? &newpw : NULL, - server_ldcs, - client_ldcs, - &msgid ); - LDAP_END_ALLOW_THREADS( self ); - - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = ldap_passwd(self->ldap, + user.bv_val != NULL ? &user : NULL, + oldpw.bv_val != NULL ? &oldpw : NULL, + newpw.bv_val != NULL ? &newpw : NULL, + server_ldcs, client_ldcs, &msgid); + LDAP_END_ALLOW_THREADS(self); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_passwd" ); + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - return PyInt_FromLong( msgid ); -} + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_passwd"); + return PyInt_FromLong(msgid); +} /* ldap_extended_operation */ static PyObject * -l_ldap_extended_operation( LDAPObject* self, PyObject *args ) +l_ldap_extended_operation(LDAPObject *self, PyObject *args) { char *reqoid = NULL; - struct berval reqvalue = {0, NULL}; + struct berval reqvalue = { 0, NULL }; PyObject *serverctrls = Py_None; PyObject *clientctrls = Py_None; - LDAPControl** server_ldcs = NULL; - LDAPControl** client_ldcs = NULL; + LDAPControl **server_ldcs = NULL; + LDAPControl **client_ldcs = NULL; int msgid; int ldaperror; - if (!PyArg_ParseTuple( args, "sz#|OO:extended_operation", &reqoid, &reqvalue.bv_val, &reqvalue.bv_len, &serverctrls, &clientctrls )) + if (!PyArg_ParseTuple + (args, "sz#|OO:extended_operation", &reqoid, &reqvalue.bv_val, + &reqvalue.bv_len, &serverctrls, &clientctrls)) return NULL; - if (not_valid(self)) return NULL; + if (not_valid(self)) + return NULL; if (!PyNone_Check(serverctrls)) { if (!LDAPControls_from_object(serverctrls, &server_ldcs)) @@ -1399,91 +1495,91 @@ l_ldap_extended_operation( LDAPObject* self, PyObject *args ) if (!PyNone_Check(clientctrls)) { if (!LDAPControls_from_object(clientctrls, &client_ldcs)) { - LDAPControl_List_DEL( server_ldcs ); + LDAPControl_List_DEL(server_ldcs); return NULL; } } - LDAP_BEGIN_ALLOW_THREADS( self ); - ldaperror = ldap_extended_operation( self->ldap, reqoid, - reqvalue.bv_val != NULL ? &reqvalue : NULL, - server_ldcs, - client_ldcs, - &msgid ); - LDAP_END_ALLOW_THREADS( self ); - - LDAPControl_List_DEL( server_ldcs ); - LDAPControl_List_DEL( client_ldcs ); + LDAP_BEGIN_ALLOW_THREADS(self); + ldaperror = ldap_extended_operation(self->ldap, reqoid, + reqvalue.bv_val != + NULL ? &reqvalue : NULL, server_ldcs, + client_ldcs, &msgid); + LDAP_END_ALLOW_THREADS(self); + + LDAPControl_List_DEL(server_ldcs); + LDAPControl_List_DEL(client_ldcs); - if ( ldaperror!=LDAP_SUCCESS ) - return LDAPerror( self->ldap, "ldap_extended_operation" ); + if (ldaperror != LDAP_SUCCESS) + return LDAPerror(self->ldap, "ldap_extended_operation"); - return PyInt_FromLong( msgid ); + return PyInt_FromLong(msgid); } /* methods */ static PyMethodDef methods[] = { - {"unbind_ext", (PyCFunction)l_ldap_unbind_ext, METH_VARARGS }, - {"abandon_ext", (PyCFunction)l_ldap_abandon_ext, METH_VARARGS }, - {"add_ext", (PyCFunction)l_ldap_add_ext, METH_VARARGS }, - {"simple_bind", (PyCFunction)l_ldap_simple_bind, METH_VARARGS }, + {"unbind_ext", (PyCFunction)l_ldap_unbind_ext, METH_VARARGS}, + {"abandon_ext", (PyCFunction)l_ldap_abandon_ext, METH_VARARGS}, + {"add_ext", (PyCFunction)l_ldap_add_ext, METH_VARARGS}, + {"simple_bind", (PyCFunction)l_ldap_simple_bind, METH_VARARGS}, #ifdef HAVE_SASL - {"sasl_interactive_bind_s", (PyCFunction)l_ldap_sasl_interactive_bind_s, METH_VARARGS }, - {"sasl_bind_s", (PyCFunction)l_ldap_sasl_bind_s, METH_VARARGS }, + {"sasl_interactive_bind_s", (PyCFunction)l_ldap_sasl_interactive_bind_s, + METH_VARARGS}, + {"sasl_bind_s", (PyCFunction)l_ldap_sasl_bind_s, METH_VARARGS}, #endif - {"compare_ext", (PyCFunction)l_ldap_compare_ext, METH_VARARGS }, - {"delete_ext", (PyCFunction)l_ldap_delete_ext, METH_VARARGS }, - {"modify_ext", (PyCFunction)l_ldap_modify_ext, METH_VARARGS }, - {"rename", (PyCFunction)l_ldap_rename, METH_VARARGS }, - {"result4", (PyCFunction)l_ldap_result4, METH_VARARGS }, - {"search_ext", (PyCFunction)l_ldap_search_ext, METH_VARARGS }, + {"compare_ext", (PyCFunction)l_ldap_compare_ext, METH_VARARGS}, + {"delete_ext", (PyCFunction)l_ldap_delete_ext, METH_VARARGS}, + {"modify_ext", (PyCFunction)l_ldap_modify_ext, METH_VARARGS}, + {"rename", (PyCFunction)l_ldap_rename, METH_VARARGS}, + {"result4", (PyCFunction)l_ldap_result4, METH_VARARGS}, + {"search_ext", (PyCFunction)l_ldap_search_ext, METH_VARARGS}, #ifdef HAVE_TLS - {"start_tls_s", (PyCFunction)l_ldap_start_tls_s, METH_VARARGS }, + {"start_tls_s", (PyCFunction)l_ldap_start_tls_s, METH_VARARGS}, #endif - {"whoami_s", (PyCFunction)l_ldap_whoami_s, METH_VARARGS }, - {"passwd", (PyCFunction)l_ldap_passwd, METH_VARARGS }, - {"set_option", (PyCFunction)l_ldap_set_option, METH_VARARGS }, - {"get_option", (PyCFunction)l_ldap_get_option, METH_VARARGS }, + {"whoami_s", (PyCFunction)l_ldap_whoami_s, METH_VARARGS}, + {"passwd", (PyCFunction)l_ldap_passwd, METH_VARARGS}, + {"set_option", (PyCFunction)l_ldap_set_option, METH_VARARGS}, + {"get_option", (PyCFunction)l_ldap_get_option, METH_VARARGS}, #ifdef LDAP_API_FEATURE_CANCEL - {"cancel", (PyCFunction)l_ldap_cancel, METH_VARARGS }, + {"cancel", (PyCFunction)l_ldap_cancel, METH_VARARGS}, #endif - {"extop", (PyCFunction)l_ldap_extended_operation, METH_VARARGS }, - { NULL, NULL } + {"extop", (PyCFunction)l_ldap_extended_operation, METH_VARARGS}, + {NULL, NULL} }; /* type entry */ PyTypeObject LDAP_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "LDAP", /*tp_name*/ - sizeof(LDAPObject), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - /* methods */ - (destructor)dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - 0, /*tp_flags*/ - 0, /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - methods, /*tp_methods*/ - 0, /*tp_members*/ - 0, /*tp_getset*/ + PyVarObject_HEAD_INIT(NULL, 0) + "LDAP", /*tp_name */ + sizeof(LDAPObject), /*tp_basicsize */ + 0, /*tp_itemsize */ + /* methods */ + (destructor) dealloc, /*tp_dealloc */ + 0, /*tp_print */ + 0, /*tp_getattr */ + 0, /*tp_setattr */ + 0, /*tp_compare */ + 0, /*tp_repr */ + 0, /*tp_as_number */ + 0, /*tp_as_sequence */ + 0, /*tp_as_mapping */ + 0, /*tp_hash */ + 0, /*tp_call */ + 0, /*tp_str */ + 0, /*tp_getattro */ + 0, /*tp_setattro */ + 0, /*tp_as_buffer */ + 0, /*tp_flags */ + 0, /*tp_doc */ + 0, /*tp_traverse */ + 0, /*tp_clear */ + 0, /*tp_richcompare */ + 0, /*tp_weaklistoffset */ + 0, /*tp_iter */ + 0, /*tp_iternext */ + methods, /*tp_methods */ + 0, /*tp_members */ + 0, /*tp_getset */ }; diff --git a/Modules/LDAPObject.h b/Modules/LDAPObject.h index 8cd6fc3e..a456bce0 100644 --- a/Modules/LDAPObject.h +++ b/Modules/LDAPObject.h @@ -1,7 +1,7 @@ /* See https://www.python-ldap.org/ for details. */ -#ifndef __h_LDAPObject -#define __h_LDAPObject +#ifndef __h_LDAPObject +#define __h_LDAPObject #include "common.h" @@ -12,22 +12,22 @@ #endif #if PYTHON_API_VERSION < 1007 -typedef PyObject* _threadstate; +typedef PyObject *_threadstate; #else -typedef PyThreadState* _threadstate; +typedef PyThreadState *_threadstate; #endif typedef struct { - PyObject_HEAD - LDAP* ldap; - _threadstate _save; /* for thread saving on referrals */ - int valid; + PyObject_HEAD LDAP *ldap; + _threadstate _save; /* for thread saving on referrals */ + int valid; } LDAPObject; extern PyTypeObject LDAP_Type; + #define LDAPObject_Check(v) (Py_TYPE(v) == &LDAP_Type) -extern LDAPObject *newLDAPObject( LDAP* ); +extern LDAPObject *newLDAPObject(LDAP *); /* macros to allow thread saving in the context of an LDAP connection */ @@ -48,4 +48,3 @@ extern LDAPObject *newLDAPObject( LDAP* ); } #endif /* __h_LDAPObject */ - diff --git a/Modules/berval.h b/Modules/berval.h index 9702b8ce..2aa9c977 100644 --- a/Modules/berval.h +++ b/Modules/berval.h @@ -1,7 +1,7 @@ /* See https://www.python-ldap.org/ for details. */ -#ifndef __h_berval -#define __h_berval +#ifndef __h_berval +#define __h_berval #include "common.h" #include "lber.h" diff --git a/Modules/common.c b/Modules/common.c index 0f0cd36a..9d7001c0 100644 --- a/Modules/common.c +++ b/Modules/common.c @@ -6,21 +6,24 @@ /* dynamically add the methods into the module dictionary d */ void -LDAPadd_methods( PyObject* d, PyMethodDef* methods ) +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 ); + for (meth = methods; meth->ml_meth; meth++) { + PyObject *f = PyCFunction_New(meth, NULL); + + PyDict_SetItemString(d, meth->ml_name, f); Py_DECREF(f); } } /* Raise TypeError with custom message and object */ -PyObject* -LDAPerror_TypeError(const char *msg, PyObject *obj) { +PyObject * +LDAPerror_TypeError(const char *msg, PyObject *obj) +{ PyObject *args = Py_BuildValue("sO", msg, obj); + if (args == NULL) { return NULL; } diff --git a/Modules/common.h b/Modules/common.h index 029f234f..affa5f93 100644 --- a/Modules/common.h +++ b/Modules/common.h @@ -1,8 +1,8 @@ /* common utility macros * See https://www.python-ldap.org/ for details. */ -#ifndef __h_common -#define __h_common +#ifndef __h_common +#define __h_common #define PY_SSIZE_T_CLEAN @@ -24,9 +24,10 @@ #define streq( a, b ) \ ( (*(a)==*(b)) && 0==strcmp(a,b) ) -extern PyObject* LDAPerror_TypeError(const char *, PyObject *); +extern PyObject *LDAPerror_TypeError(const char *, PyObject *); + +void LDAPadd_methods(PyObject *d, PyMethodDef *methods); -void LDAPadd_methods( PyObject*d, PyMethodDef*methods ); #define PyNone_Check(o) ((o) == Py_None) /* Py2/3 compatibility */ @@ -36,4 +37,3 @@ void LDAPadd_methods( PyObject*d, PyMethodDef*methods ); #endif #endif /* __h_common_ */ - diff --git a/Modules/constants.c b/Modules/constants.c index c2b595c1..f8da3736 100644 --- a/Modules/constants.c +++ b/Modules/constants.c @@ -8,133 +8,136 @@ /* the base exception class */ -PyObject* -LDAPexception_class; +PyObject *LDAPexception_class; /* list of exception classes */ #define LDAP_ERROR_MIN LDAP_REFERRAL_LIMIT_EXCEEDED #ifdef LDAP_PROXIED_AUTHORIZATION_DENIED - #define LDAP_ERROR_MAX LDAP_PROXIED_AUTHORIZATION_DENIED +#define LDAP_ERROR_MAX LDAP_PROXIED_AUTHORIZATION_DENIED #else - #ifdef LDAP_ASSERTION_FAILED - #define LDAP_ERROR_MAX LDAP_ASSERTION_FAILED - #else - #define LDAP_ERROR_MAX LDAP_OTHER - #endif +#ifdef LDAP_ASSERTION_FAILED +#define LDAP_ERROR_MAX LDAP_ASSERTION_FAILED +#else +#define LDAP_ERROR_MAX LDAP_OTHER +#endif #endif #define LDAP_ERROR_OFFSET -LDAP_ERROR_MIN -static PyObject* errobjects[ LDAP_ERROR_MAX-LDAP_ERROR_MIN+1 ]; - +static PyObject *errobjects[LDAP_ERROR_MAX - LDAP_ERROR_MIN + 1]; /* Convert a bare LDAP error number into an exception */ -PyObject* +PyObject * LDAPerr(int errnum) { - if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX) { - PyErr_SetNone(errobjects[errnum+LDAP_ERROR_OFFSET]); - } else { - PyObject *args = Py_BuildValue("{s:i}", "errnum", errnum); - if (args == NULL) - return NULL; - PyErr_SetObject(LDAPexception_class, args); - Py_DECREF(args); - } - return NULL; + if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX) { + PyErr_SetNone(errobjects[errnum + LDAP_ERROR_OFFSET]); + } + else { + PyObject *args = Py_BuildValue("{s:i}", "errnum", errnum); + + if (args == NULL) + return NULL; + PyErr_SetObject(LDAPexception_class, args); + Py_DECREF(args); + } + return NULL; } /* Convert an LDAP error into an informative python exception */ -PyObject* -LDAPerror( LDAP *l, char *msg ) +PyObject * +LDAPerror(LDAP *l, char *msg) { - if (l == NULL) { - PyErr_SetFromErrno( LDAPexception_class ); - return NULL; - } - else { - int myerrno, errnum, opt_errnum; - PyObject *errobj; - PyObject *info; - PyObject *str; - PyObject *pyerrno; - char *matched, *error; - - /* at first save errno for later use before it gets overwritten by another call */ - myerrno = errno; - - opt_errnum = ldap_get_option(l, LDAP_OPT_ERROR_NUMBER, &errnum); - if (opt_errnum != LDAP_OPT_SUCCESS) - errnum = opt_errnum; - - if (errnum == LDAP_NO_MEMORY) - return PyErr_NoMemory(); - - if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX) - errobj = errobjects[errnum+LDAP_ERROR_OFFSET]; - else - errobj = LDAPexception_class; - - info = PyDict_New(); - if (info == NULL) - return NULL; - - str = PyUnicode_FromString(ldap_err2string(errnum)); - if (str) - PyDict_SetItemString( info, "desc", str ); - Py_XDECREF(str); - - if (myerrno != 0) { - pyerrno = PyInt_FromLong(myerrno); - if (pyerrno) - PyDict_SetItemString( info, "errno", pyerrno ); - Py_XDECREF(pyerrno); + if (l == NULL) { + PyErr_SetFromErrno(LDAPexception_class); + return NULL; } + else { + int myerrno, errnum, opt_errnum; + PyObject *errobj; + PyObject *info; + PyObject *str; + PyObject *pyerrno; + char *matched, *error; + + /* at first save errno for later use before it gets overwritten by another call */ + myerrno = errno; + + opt_errnum = ldap_get_option(l, LDAP_OPT_ERROR_NUMBER, &errnum); + if (opt_errnum != LDAP_OPT_SUCCESS) + errnum = opt_errnum; + + if (errnum == LDAP_NO_MEMORY) + return PyErr_NoMemory(); + + if (errnum >= LDAP_ERROR_MIN && errnum <= LDAP_ERROR_MAX) + errobj = errobjects[errnum + LDAP_ERROR_OFFSET]; + else + errobj = LDAPexception_class; + + info = PyDict_New(); + if (info == NULL) + return NULL; + + str = PyUnicode_FromString(ldap_err2string(errnum)); + if (str) + PyDict_SetItemString(info, "desc", str); + Py_XDECREF(str); - if (ldap_get_option(l, LDAP_OPT_MATCHED_DN, &matched) >= 0 - && matched != NULL) { - if (*matched != '\0') { - str = PyUnicode_FromString(matched); - if (str) - PyDict_SetItemString( info, "matched", str ); - Py_XDECREF(str); + if (myerrno != 0) { + pyerrno = PyInt_FromLong(myerrno); + if (pyerrno) + PyDict_SetItemString(info, "errno", pyerrno); + Py_XDECREF(pyerrno); } - ldap_memfree(matched); - } - if (errnum == LDAP_REFERRAL) { - str = PyUnicode_FromString(msg); - if (str) - PyDict_SetItemString( info, "info", str ); - Py_XDECREF(str); - } else if (ldap_get_option(l, LDAP_OPT_ERROR_STRING, &error) >= 0) { - if (error != NULL && *error != '\0') { - str = PyUnicode_FromString(error); + if (ldap_get_option(l, LDAP_OPT_MATCHED_DN, &matched) >= 0 + && matched != NULL) { + if (*matched != '\0') { + str = PyUnicode_FromString(matched); + if (str) + PyDict_SetItemString(info, "matched", str); + Py_XDECREF(str); + } + ldap_memfree(matched); + } + + if (errnum == LDAP_REFERRAL) { + str = PyUnicode_FromString(msg); if (str) - PyDict_SetItemString( info, "info", str ); + PyDict_SetItemString(info, "info", str); Py_XDECREF(str); } - ldap_memfree(error); + else if (ldap_get_option(l, LDAP_OPT_ERROR_STRING, &error) >= 0) { + if (error != NULL && *error != '\0') { + str = PyUnicode_FromString(error); + if (str) + PyDict_SetItemString(info, "info", str); + Py_XDECREF(str); + } + ldap_memfree(error); + } + PyErr_SetObject(errobj, info); + Py_DECREF(info); + return NULL; } - PyErr_SetObject( errobj, info ); - Py_DECREF(info); - return NULL; - } } /* initialise the module constants */ int -LDAPinit_constants( PyObject* m ) +LDAPinit_constants(PyObject *m) { PyObject *exc; /* simple constants */ - if (PyModule_AddIntConstant(m, "OPT_ON", 1) != 0) return -1; - if (PyModule_AddIntConstant(m, "OPT_OFF", 0) != 0) return -1; + if (PyModule_AddIntConstant(m, "OPT_ON", 1) != 0) + return -1; + if (PyModule_AddIntConstant(m, "OPT_OFF", 0) != 0) + return -1; /* exceptions */ @@ -143,11 +146,13 @@ LDAPinit_constants( PyObject* m ) return -1; } - if (PyModule_AddObject(m, "LDAPError", LDAPexception_class) != 0) return -1; + if (PyModule_AddObject(m, "LDAPError", LDAPexception_class) != 0) + return -1; Py_INCREF(LDAPexception_class); /* XXX - backward compatibility with pre-1.8 */ - if (PyModule_AddObject(m, "error", LDAPexception_class) != 0) return -1; + if (PyModule_AddObject(m, "error", LDAPexception_class) != 0) + return -1; Py_INCREF(LDAPexception_class); /* Generated constants -- see Lib/ldap/constants.py */ diff --git a/Modules/constants.h b/Modules/constants.h index 4056f907..8a390b5b 100644 --- a/Modules/constants.h +++ b/Modules/constants.h @@ -7,19 +7,19 @@ #include "lber.h" #include "ldap.h" -extern int LDAPinit_constants( PyObject* m ); -extern PyObject* LDAPconstant( int ); +extern int LDAPinit_constants(PyObject *m); +extern PyObject *LDAPconstant(int); -extern PyObject* LDAPexception_class; -extern PyObject* LDAPerror( LDAP*, char*msg ); -PyObject* LDAPerr(int errnum); +extern PyObject *LDAPexception_class; +extern PyObject *LDAPerror(LDAP *, char *msg); +PyObject *LDAPerr(int errnum); #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 */ +#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/constants_generated.h b/Modules/constants_generated.h index 083ba161..455852ed 100644 --- a/Modules/constants_generated.h +++ b/Modules/constants_generated.h @@ -76,12 +76,10 @@ add_err(TOO_LATE); add_err(CANNOT_CANCEL); #endif - #if defined(LDAP_ASSERTION_FAILED) add_err(ASSERTION_FAILED); #endif - #if defined(LDAP_PROXIED_AUTHORIZATION_DENIED) add_err(PROXIED_AUTHORIZATION_DENIED); #endif @@ -194,7 +192,6 @@ add_int(OPT_URI); add_int(OPT_DEFBASE); #endif - #if HAVE_TLS #if defined(LDAP_OPT_X_TLS) @@ -220,27 +217,22 @@ add_int(OPT_X_TLS_TRY); add_int(OPT_X_TLS_PEERCERT); #endif - #if defined(LDAP_OPT_X_TLS_VERSION) add_int(OPT_X_TLS_VERSION); #endif - #if defined(LDAP_OPT_X_TLS_CIPHER) add_int(OPT_X_TLS_CIPHER); #endif - #if defined(LDAP_OPT_X_TLS_PEERCERT) add_int(OPT_X_TLS_PEERCERT); #endif - #if defined(LDAP_OPT_X_TLS_CRLCHECK) add_int(OPT_X_TLS_CRLCHECK); #endif - #if defined(LDAP_OPT_X_TLS_CRLFILE) add_int(OPT_X_TLS_CRLFILE); #endif @@ -253,12 +245,10 @@ add_int(OPT_X_TLS_CRL_ALL); add_int(OPT_X_TLS_NEWCTX); #endif - #if defined(LDAP_OPT_X_TLS_PROTOCOL_MIN) add_int(OPT_X_TLS_PROTOCOL_MIN); #endif - #if defined(LDAP_OPT_X_TLS_PACKAGE) add_int(OPT_X_TLS_PACKAGE); #endif @@ -279,27 +269,22 @@ add_int(OPT_X_SASL_SSF_MAX); add_int(OPT_X_SASL_NOCANON); #endif - #if defined(LDAP_OPT_X_SASL_USERNAME) add_int(OPT_X_SASL_USERNAME); #endif - #if defined(LDAP_OPT_CONNECT_ASYNC) add_int(OPT_CONNECT_ASYNC); #endif - #if defined(LDAP_OPT_X_KEEPALIVE_IDLE) add_int(OPT_X_KEEPALIVE_IDLE); #endif - #if defined(LDAP_OPT_X_KEEPALIVE_PROBES) add_int(OPT_X_KEEPALIVE_PROBES); #endif - #if defined(LDAP_OPT_X_KEEPALIVE_INTERVAL) add_int(OPT_X_KEEPALIVE_INTERVAL); #endif @@ -325,23 +310,27 @@ add_int(URL_ERR_BADSCOPE); add_int(URL_ERR_MEM); #ifdef HAVE_LIBLDAP_R -if (PyModule_AddIntConstant(m, "LIBLDAP_R", 1) != 0) return -1; +if (PyModule_AddIntConstant(m, "LIBLDAP_R", 1) != 0) + return -1; #else -if (PyModule_AddIntConstant(m, "LIBLDAP_R", 0) != 0) return -1; +if (PyModule_AddIntConstant(m, "LIBLDAP_R", 0) != 0) + return -1; #endif - #ifdef HAVE_SASL -if (PyModule_AddIntConstant(m, "SASL_AVAIL", 1) != 0) return -1; +if (PyModule_AddIntConstant(m, "SASL_AVAIL", 1) != 0) + return -1; #else -if (PyModule_AddIntConstant(m, "SASL_AVAIL", 0) != 0) return -1; +if (PyModule_AddIntConstant(m, "SASL_AVAIL", 0) != 0) + return -1; #endif - #ifdef HAVE_TLS -if (PyModule_AddIntConstant(m, "TLS_AVAIL", 1) != 0) return -1; +if (PyModule_AddIntConstant(m, "TLS_AVAIL", 1) != 0) + return -1; #else -if (PyModule_AddIntConstant(m, "TLS_AVAIL", 0) != 0) return -1; +if (PyModule_AddIntConstant(m, "TLS_AVAIL", 0) != 0) + return -1; #endif add_string(CONTROL_MANAGEDSAIT); diff --git a/Modules/functions.c b/Modules/functions.c index 6bbf487b..4731efb8 100644 --- a/Modules/functions.c +++ b/Modules/functions.c @@ -9,29 +9,26 @@ /* ldap_initialize */ -static PyObject* -l_ldap_initialize(PyObject* unused, PyObject *args) +static PyObject * +l_ldap_initialize(PyObject *unused, PyObject *args) { char *uri; LDAP *ld = NULL; int ret; if (!PyArg_ParseTuple(args, "s:initialize", &uri)) - return NULL; - - Py_BEGIN_ALLOW_THREADS - ret = ldap_initialize(&ld, uri); - Py_END_ALLOW_THREADS - if (ret != LDAP_SUCCESS) - return LDAPerror(ld, "ldap_initialize"); - return (PyObject*)newLDAPObject(ld); -} + return NULL; + Py_BEGIN_ALLOW_THREADS ret = ldap_initialize(&ld, uri); + Py_END_ALLOW_THREADS if (ret != LDAP_SUCCESS) + return LDAPerror(ld, "ldap_initialize"); + return (PyObject *)newLDAPObject(ld); +} /* ldap_str2dn */ -static PyObject* -l_ldap_str2dn( PyObject* unused, PyObject *args ) +static PyObject * +l_ldap_str2dn(PyObject *unused, PyObject *args) { struct berval str; LDAPDN dn; @@ -46,58 +43,59 @@ l_ldap_str2dn( PyObject* unused, PyObject *args ) * ((('a','b',1),('c','d',1)),(('e','f',1),)) * The integers are a bit combination of the AVA_* flags */ - if (!PyArg_ParseTuple( args, "z#|i:str2dn", - &str.bv_val, &str_len, &flags )) - return NULL; + if (!PyArg_ParseTuple(args, "z#|i:str2dn", &str.bv_val, &str_len, &flags)) + return NULL; str.bv_len = (ber_len_t) str_len; res = ldap_bv2dn(&str, &dn, flags); if (res != LDAP_SUCCESS) - return LDAPerr(res); + return LDAPerr(res); tmp = PyList_New(0); if (!tmp) - goto failed; + goto failed; for (i = 0; dn[i]; i++) { - LDAPRDN rdn; - PyObject *rdnlist; - - rdn = dn[i]; - rdnlist = PyList_New(0); - if (!rdnlist) - goto failed; - if (PyList_Append(tmp, rdnlist) == -1) { - Py_DECREF(rdnlist); - goto failed; - } - - for (j = 0; rdn[j]; j++) { - LDAPAVA *ava = rdn[j]; - PyObject *tuple; - - tuple = Py_BuildValue("(O&O&i)", - LDAPberval_to_unicode_object, &ava->la_attr, - LDAPberval_to_unicode_object, &ava->la_value, - ava->la_flags & ~(LDAP_AVA_FREE_ATTR|LDAP_AVA_FREE_VALUE)); - if (!tuple) { - Py_DECREF(rdnlist); - goto failed; - } - - if (PyList_Append(rdnlist, tuple) == -1) { - Py_DECREF(tuple); - goto failed; - } - Py_DECREF(tuple); - } - Py_DECREF(rdnlist); + LDAPRDN rdn; + PyObject *rdnlist; + + rdn = dn[i]; + rdnlist = PyList_New(0); + if (!rdnlist) + goto failed; + if (PyList_Append(tmp, rdnlist) == -1) { + Py_DECREF(rdnlist); + goto failed; + } + + for (j = 0; rdn[j]; j++) { + LDAPAVA *ava = rdn[j]; + PyObject *tuple; + + tuple = Py_BuildValue("(O&O&i)", + LDAPberval_to_unicode_object, &ava->la_attr, + LDAPberval_to_unicode_object, &ava->la_value, + ava-> + la_flags & ~(LDAP_AVA_FREE_ATTR | + LDAP_AVA_FREE_VALUE)); + if (!tuple) { + Py_DECREF(rdnlist); + goto failed; + } + + if (PyList_Append(rdnlist, tuple) == -1) { + Py_DECREF(tuple); + goto failed; + } + Py_DECREF(tuple); + } + Py_DECREF(rdnlist); } result = tmp; tmp = NULL; -failed: + failed: Py_XDECREF(tmp); ldap_dnfree(dn); return result; @@ -105,46 +103,46 @@ l_ldap_str2dn( PyObject* unused, PyObject *args ) /* ldap_set_option (global options) */ -static PyObject* -l_ldap_set_option(PyObject* self, PyObject *args) +static PyObject * +l_ldap_set_option(PyObject *self, PyObject *args) { PyObject *value; int option; if (!PyArg_ParseTuple(args, "iO:set_option", &option, &value)) - return NULL; + return NULL; if (!LDAP_set_option(NULL, option, value)) - return NULL; + return NULL; Py_INCREF(Py_None); return Py_None; } /* ldap_get_option (global options) */ -static PyObject* -l_ldap_get_option(PyObject* self, PyObject *args) +static PyObject * +l_ldap_get_option(PyObject *self, PyObject *args) { int option; if (!PyArg_ParseTuple(args, "i:get_option", &option)) - return NULL; + return NULL; return LDAP_get_option(NULL, option); } - /* methods */ static PyMethodDef methods[] = { - { "initialize", (PyCFunction)l_ldap_initialize, METH_VARARGS }, - { "str2dn", (PyCFunction)l_ldap_str2dn, METH_VARARGS }, - { "set_option", (PyCFunction)l_ldap_set_option, METH_VARARGS }, - { "get_option", (PyCFunction)l_ldap_get_option, METH_VARARGS }, - { NULL, NULL } + {"initialize", (PyCFunction)l_ldap_initialize, METH_VARARGS}, + {"str2dn", (PyCFunction)l_ldap_str2dn, METH_VARARGS}, + {"set_option", (PyCFunction)l_ldap_set_option, METH_VARARGS}, + {"get_option", (PyCFunction)l_ldap_get_option, METH_VARARGS}, + {NULL, NULL} }; /* initialisation */ void -LDAPinit_functions( PyObject* d ) { - LDAPadd_methods( d, methods ); +LDAPinit_functions(PyObject *d) +{ + LDAPadd_methods(d, methods); } diff --git a/Modules/functions.h b/Modules/functions.h index 854a9403..2aef9740 100644 --- a/Modules/functions.h +++ b/Modules/functions.h @@ -4,6 +4,6 @@ #define __h_functions_ #include "common.h" -extern void LDAPinit_functions( PyObject* ); +extern void LDAPinit_functions(PyObject *); #endif /* __h_functions_ */ diff --git a/Modules/ldapcontrol.c b/Modules/ldapcontrol.c index c8f9dfce..f53e681a 100644 --- a/Modules/ldapcontrol.c +++ b/Modules/ldapcontrol.c @@ -27,13 +27,13 @@ LDAPControl_DumpList( LDAPControl** lcs ) { } */ /* Free a single LDAPControl object created by Tuple_to_LDAPControl */ - + static void -LDAPControl_DEL( LDAPControl* lc ) +LDAPControl_DEL(LDAPControl *lc) { if (lc == NULL) return; - + if (lc->ldctl_oid) PyMem_DEL(lc->ldctl_oid); PyMem_DEL(lc); @@ -42,16 +42,17 @@ LDAPControl_DEL( LDAPControl* lc ) /* Free an array of LDAPControl objects created by LDAPControls_from_object */ void -LDAPControl_List_DEL( LDAPControl** lcs ) +LDAPControl_List_DEL(LDAPControl **lcs) { - LDAPControl** lcp; + LDAPControl **lcp; + if (lcs == NULL) return; - for ( lcp = lcs; *lcp; lcp++ ) - LDAPControl_DEL( *lcp ); + for (lcp = lcs; *lcp; lcp++) + LDAPControl_DEL(*lcp); - PyMem_DEL( lcs ); + PyMem_DEL(lcs); } /* Takes a tuple of the form: @@ -61,8 +62,8 @@ LDAPControl_List_DEL( LDAPControl** lcs ) * The Value string should represent an ASN.1 encoded structure. */ -static LDAPControl* -Tuple_to_LDAPControl( PyObject* tup ) +static LDAPControl * +Tuple_to_LDAPControl(PyObject *tup) { char *oid; char iscritical; @@ -72,13 +73,14 @@ Tuple_to_LDAPControl( PyObject* tup ) Py_ssize_t len; if (!PyTuple_Check(tup)) { - LDAPerror_TypeError("Tuple_to_LDAPControl(): expected a tuple", tup); - return NULL; + LDAPerror_TypeError("Tuple_to_LDAPControl(): expected a tuple", tup); + return NULL; } - if (!PyArg_ParseTuple( tup, "sbO:Tuple_to_LDAPControl", &oid, &iscritical, &bytes )) + if (!PyArg_ParseTuple + (tup, "sbO:Tuple_to_LDAPControl", &oid, &iscritical, &bytes)) return NULL; - + lc = PyMem_NEW(LDAPControl, 1); if (lc == NULL) { PyErr_NoMemory(); @@ -110,7 +112,7 @@ Tuple_to_LDAPControl( PyObject* tup ) LDAPControl_DEL(lc); return NULL; } - + lc->ldctl_value = berbytes; return lc; @@ -120,41 +122,42 @@ Tuple_to_LDAPControl( PyObject* tup ) * function) into an array of LDAPControl objects. */ int -LDAPControls_from_object(PyObject* list, LDAPControl ***controls_ret) +LDAPControls_from_object(PyObject *list, LDAPControl ***controls_ret) { Py_ssize_t len, i; - LDAPControl** ldcs; - LDAPControl* ldc; - PyObject* item; - + LDAPControl **ldcs; + LDAPControl *ldc; + PyObject *item; + if (!PySequence_Check(list)) { - LDAPerror_TypeError("LDAPControls_from_object(): expected a list", list); + LDAPerror_TypeError("LDAPControls_from_object(): expected a list", + list); return 0; } len = PySequence_Length(list); - ldcs = PyMem_NEW(LDAPControl*, len + 1); + ldcs = PyMem_NEW(LDAPControl *, len + 1); if (ldcs == NULL) { PyErr_NoMemory(); return 0; } for (i = 0; i < len; i++) { - item = PySequence_GetItem(list, i); - if (item == NULL) { - PyMem_DEL(ldcs); - return 0; - } - - ldc = Tuple_to_LDAPControl(item); - if (ldc == NULL) { - Py_DECREF(item); - PyMem_DEL(ldcs); - return 0; - } - - ldcs[i] = ldc; - Py_DECREF(item); + item = PySequence_GetItem(list, i); + if (item == NULL) { + PyMem_DEL(ldcs); + return 0; + } + + ldc = Tuple_to_LDAPControl(item); + if (ldc == NULL) { + Py_DECREF(item); + PyMem_DEL(ldcs); + return 0; + } + + ldcs[i] = ldc; + Py_DECREF(item); } ldcs[len] = NULL; @@ -162,7 +165,7 @@ LDAPControls_from_object(PyObject* list, LDAPControl ***controls_ret) return 1; } -PyObject* +PyObject * LDAPControls_to_List(LDAPControl **ldcs) { PyObject *res = 0, *pyctrl; @@ -170,7 +173,8 @@ LDAPControls_to_List(LDAPControl **ldcs) Py_ssize_t num_ctrls = 0, i; if (tmp) - while (*tmp++) num_ctrls++; + while (*tmp++) + num_ctrls++; if ((res = PyList_New(num_ctrls)) == NULL) { return NULL; @@ -190,59 +194,58 @@ LDAPControls_to_List(LDAPControl **ldcs) return res; } - - /* --------------- en-/decoders ------------- */ /* Matched Values, aka, Values Return Filter */ -static PyObject* +static PyObject * encode_rfc3876(PyObject *self, PyObject *args) { - PyObject *res = 0; - int err; - BerElement *vrber = 0; - char *vrFilter; - struct berval *ctrl_val; - - if (!PyArg_ParseTuple(args, "s:encode_valuesreturnfilter_control", &vrFilter)) { - goto endlbl; - } - - if (!(vrber = ber_alloc_t(LBER_USE_DER))) { - LDAPerr(LDAP_NO_MEMORY); - goto endlbl; - } - - err = ldap_put_vrFilter(vrber, vrFilter); - if (err == -1) { - LDAPerr(LDAP_FILTER_ERROR); - goto endlbl; - } - - err = ber_flatten(vrber, &ctrl_val); - if (err == -1) { - LDAPerr(LDAP_NO_MEMORY); - goto endlbl; - } - - res = LDAPberval_to_object(ctrl_val); - ber_bvfree(ctrl_val); - -endlbl: - if (vrber) - ber_free(vrber, 1); - - return res; + PyObject *res = 0; + int err; + BerElement *vrber = 0; + char *vrFilter; + struct berval *ctrl_val; + + if (!PyArg_ParseTuple + (args, "s:encode_valuesreturnfilter_control", &vrFilter)) { + goto endlbl; + } + + if (!(vrber = ber_alloc_t(LBER_USE_DER))) { + LDAPerr(LDAP_NO_MEMORY); + goto endlbl; + } + + err = ldap_put_vrFilter(vrber, vrFilter); + if (err == -1) { + LDAPerr(LDAP_FILTER_ERROR); + goto endlbl; + } + + err = ber_flatten(vrber, &ctrl_val); + if (err == -1) { + LDAPerr(LDAP_NO_MEMORY); + goto endlbl; + } + + res = LDAPberval_to_object(ctrl_val); + ber_bvfree(ctrl_val); + + endlbl: + if (vrber) + ber_free(vrber, 1); + + return res; } -static PyObject* +static PyObject * encode_rfc2696(PyObject *self, PyObject *args) { PyObject *res = 0; BerElement *ber = 0; struct berval cookie, *ctrl_val; Py_ssize_t cookie_len; - int size = 0; /* ber_int_t is int */ + int size = 0; /* ber_int_t is int */ ber_tag_t tag; if (!PyArg_ParseTuple(args, "is#:encode_page_control", &size, @@ -285,14 +288,13 @@ encode_rfc2696(PyObject *self, PyObject *args) res = LDAPberval_to_object(ctrl_val); ber_bvfree(ctrl_val); - endlbl: + endlbl: if (ber) ber_free(ber, 1); return res; } - -static PyObject* +static PyObject * decode_rfc2696(PyObject *self, PyObject *args) { PyObject *res = 0; @@ -300,7 +302,7 @@ decode_rfc2696(PyObject *self, PyObject *args) struct berval ldctl_value; ber_tag_t tag; struct berval *cookiep; - int count = 0; /* ber_int_t is int */ + int count = 0; /* ber_int_t is int */ Py_ssize_t ldctl_value_len; if (!PyArg_ParseTuple(args, "s#:decode_page_control", @@ -323,13 +325,13 @@ decode_rfc2696(PyObject *self, PyObject *args) res = Py_BuildValue("(iO&)", count, LDAPberval_to_object, cookiep); ber_bvfree(cookiep); - endlbl: + endlbl: if (ber) ber_free(ber, 1); return res; } -static PyObject* +static PyObject * encode_assertion_control(PyObject *self, PyObject *args) { int err; @@ -346,41 +348,35 @@ encode_assertion_control(PyObject *self, PyObject *args) /* XXX: ldap_create() is a nasty and slow hack. It's creating a full blown * LDAP object just to encode assertion controls. */ - Py_BEGIN_ALLOW_THREADS - err = ldap_create(&ld); - Py_END_ALLOW_THREADS - - if (err != LDAP_SUCCESS) - return LDAPerror(ld, "ldap_create"); + Py_BEGIN_ALLOW_THREADS err = ldap_create(&ld); + Py_END_ALLOW_THREADS if (err != LDAP_SUCCESS) + return LDAPerror(ld, "ldap_create"); - err = ldap_create_assertion_control_value(ld,assertion_filterstr,&ctrl_val); + err = + ldap_create_assertion_control_value(ld, assertion_filterstr, + &ctrl_val); if (err != LDAP_SUCCESS) { LDAPerror(ld, "ldap_create_assertion_control_value"); - Py_BEGIN_ALLOW_THREADS - ldap_unbind_ext(ld, NULL, NULL); - Py_END_ALLOW_THREADS - return NULL; + Py_BEGIN_ALLOW_THREADS ldap_unbind_ext(ld, NULL, NULL); + Py_END_ALLOW_THREADS return NULL; } - Py_BEGIN_ALLOW_THREADS - ldap_unbind_ext(ld, NULL, NULL); - Py_END_ALLOW_THREADS - - res = LDAPberval_to_object(&ctrl_val); + Py_BEGIN_ALLOW_THREADS ldap_unbind_ext(ld, NULL, NULL); + Py_END_ALLOW_THREADS res = LDAPberval_to_object(&ctrl_val); if (ctrl_val.bv_val != NULL) { ber_memfree(ctrl_val.bv_val); } - endlbl: + endlbl: return res; } static PyMethodDef methods[] = { - {"encode_page_control", encode_rfc2696, METH_VARARGS }, - {"decode_page_control", decode_rfc2696, METH_VARARGS }, - {"encode_valuesreturnfilter_control", encode_rfc3876, METH_VARARGS }, - {"encode_assertion_control", encode_assertion_control, METH_VARARGS }, - { NULL, NULL } + {"encode_page_control", encode_rfc2696, METH_VARARGS}, + {"decode_page_control", decode_rfc2696, METH_VARARGS}, + {"encode_valuesreturnfilter_control", encode_rfc3876, METH_VARARGS}, + {"encode_assertion_control", encode_assertion_control, METH_VARARGS}, + {NULL, NULL} }; void diff --git a/Modules/ldapcontrol.h b/Modules/ldapcontrol.h index 1c09d954..de694c07 100644 --- a/Modules/ldapcontrol.h +++ b/Modules/ldapcontrol.h @@ -7,8 +7,8 @@ #include "ldap.h" void LDAPinit_control(PyObject *d); -void LDAPControl_List_DEL( LDAPControl** ); -int LDAPControls_from_object(PyObject *, LDAPControl ***); -PyObject* LDAPControls_to_List(LDAPControl **ldcs); +void LDAPControl_List_DEL(LDAPControl **); +int LDAPControls_from_object(PyObject *, LDAPControl ***); +PyObject *LDAPControls_to_List(LDAPControl **ldcs); #endif /* __h_ldapcontrol */ diff --git a/Modules/ldapmodule.c b/Modules/ldapmodule.c index f37c1b8d..8bd55ab4 100644 --- a/Modules/ldapmodule.c +++ b/Modules/ldapmodule.c @@ -21,56 +21,56 @@ static char author_str[] = STR(LDAPMODULE_AUTHOR); static char license_str[] = STR(LDAPMODULE_LICENSE); static void -init_pkginfo( PyObject* m ) +init_pkginfo(PyObject *m) { - PyModule_AddStringConstant(m, "__version__", version_str); - PyModule_AddStringConstant(m, "__author__", author_str); - PyModule_AddStringConstant(m, "__license__", license_str); + PyModule_AddStringConstant(m, "__version__", version_str); + PyModule_AddStringConstant(m, "__author__", author_str); + PyModule_AddStringConstant(m, "__license__", license_str); } /* dummy module methods */ -static PyMethodDef methods[] = { - { NULL, NULL } +static PyMethodDef methods[] = { + {NULL, NULL} }; /* module initialisation */ - /* Common initialization code */ -PyObject* init_ldap_module(void) +PyObject * +init_ldap_module(void) { - PyObject *m, *d; + PyObject *m, *d; - /* Create the module and add the functions */ + /* Create the module and add the functions */ #if PY_MAJOR_VERSION >= 3 - static struct PyModuleDef ldap_moduledef = { - PyModuleDef_HEAD_INIT, - "_ldap", /* m_name */ - "", /* m_doc */ - -1, /* m_size */ - methods, /* m_methods */ - }; - m = PyModule_Create(&ldap_moduledef); + static struct PyModuleDef ldap_moduledef = { + PyModuleDef_HEAD_INIT, + "_ldap", /* m_name */ + "", /* m_doc */ + -1, /* m_size */ + methods, /* m_methods */ + }; + m = PyModule_Create(&ldap_moduledef); #else - m = Py_InitModule("_ldap", methods); + m = Py_InitModule("_ldap", methods); #endif - /* Initialize LDAP class */ - if (PyType_Ready(&LDAP_Type) < 0) { - Py_DECREF(m); - return NULL; - } + /* Initialize LDAP class */ + if (PyType_Ready(&LDAP_Type) < 0) { + Py_DECREF(m); + return NULL; + } - /* Add some symbolic constants to the module */ - d = PyModule_GetDict(m); + /* Add some symbolic constants to the module */ + d = PyModule_GetDict(m); - init_pkginfo(m); + init_pkginfo(m); - if (LDAPinit_constants(m) == -1) { - return NULL; - } + if (LDAPinit_constants(m) == -1) { + return NULL; + } - LDAPinit_functions(d); - LDAPinit_control(d); + LDAPinit_functions(d); + LDAPinit_control(d); /* Marker for LDAPBytesWarning stack walking * See _raise_byteswarning in ldapobject.py @@ -79,20 +79,23 @@ PyObject* init_ldap_module(void) return NULL; } - /* Check for errors */ - if (PyErr_Occurred()) - Py_FatalError("can't initialize module _ldap"); + /* Check for errors */ + if (PyErr_Occurred()) + Py_FatalError("can't initialize module _ldap"); - return m; + return m; } - #if PY_MAJOR_VERSION < 3 -PyMODINIT_FUNC init_ldap() { +PyMODINIT_FUNC +init_ldap() +{ init_ldap_module(); } #else -PyMODINIT_FUNC PyInit__ldap() { +PyMODINIT_FUNC +PyInit__ldap() +{ return init_ldap_module(); } #endif diff --git a/Modules/message.c b/Modules/message.c index b7f3ae79..cf3ea3b4 100644 --- a/Modules/message.c +++ b/Modules/message.c @@ -21,261 +21,268 @@ * be returned */ PyObject * -LDAPmessage_to_python(LDAP *ld, LDAPMessage *m, int add_ctrls, int add_intermediates) +LDAPmessage_to_python(LDAP *ld, LDAPMessage *m, int add_ctrls, + int add_intermediates) { /* we convert an LDAP message into a python structure. * It is always a list of dictionaries. * We always free m. */ - PyObject *result, *pyctrls = 0; - LDAPMessage* entry; - LDAPControl **serverctrls = 0; - int rc; + PyObject *result, *pyctrls = 0; + LDAPMessage *entry; + LDAPControl **serverctrls = 0; + int rc; - result = PyList_New(0); - if (result == NULL) { - ldap_msgfree( m ); - return NULL; - } + result = PyList_New(0); + if (result == NULL) { + ldap_msgfree(m); + return NULL; + } - for(entry = ldap_first_entry(ld,m); - entry != NULL; - entry = ldap_next_entry(ld,entry)) - { - char *dn; - char *attr; - BerElement *ber = NULL; - PyObject* entrytuple; - PyObject* attrdict; - PyObject* pydn; + for (entry = ldap_first_entry(ld, m); + entry != NULL; entry = ldap_next_entry(ld, entry)) { + char *dn; + char *attr; + BerElement *ber = NULL; + PyObject *entrytuple; + PyObject *attrdict; + PyObject *pydn; - dn = ldap_get_dn( ld, entry ); - if (dn == NULL) { - Py_DECREF(result); - ldap_msgfree( m ); - return LDAPerror( ld, "ldap_get_dn" ); - } + dn = ldap_get_dn(ld, entry); + if (dn == NULL) { + Py_DECREF(result); + ldap_msgfree(m); + return LDAPerror(ld, "ldap_get_dn"); + } - attrdict = PyDict_New(); - if (attrdict == NULL) { - Py_DECREF(result); - ldap_msgfree( m ); - ldap_memfree(dn); - return NULL; - } + attrdict = PyDict_New(); + if (attrdict == NULL) { + Py_DECREF(result); + ldap_msgfree(m); + ldap_memfree(dn); + return NULL; + } - rc = ldap_get_entry_controls( ld, entry, &serverctrls ); - if (rc) { - Py_DECREF(result); - ldap_msgfree( m ); - ldap_memfree(dn); - return LDAPerror( ld, "ldap_get_entry_controls" ); - } + rc = ldap_get_entry_controls(ld, entry, &serverctrls); + if (rc) { + Py_DECREF(result); + ldap_msgfree(m); + ldap_memfree(dn); + return LDAPerror(ld, "ldap_get_entry_controls"); + } - /* convert serverctrls to list of tuples */ - if ( ! ( pyctrls = LDAPControls_to_List( serverctrls ) ) ) { - int err = LDAP_NO_MEMORY; - ldap_set_option( ld, LDAP_OPT_ERROR_NUMBER, &err ); - Py_DECREF(result); - ldap_msgfree( m ); - ldap_memfree(dn); - ldap_controls_free(serverctrls); - return LDAPerror( ld, "LDAPControls_to_List" ); - } - ldap_controls_free(serverctrls); + /* convert serverctrls to list of tuples */ + if (!(pyctrls = LDAPControls_to_List(serverctrls))) { + int err = LDAP_NO_MEMORY; - /* Fill attrdict with lists */ - for( attr = ldap_first_attribute( ld, entry, &ber ); - attr != NULL; - attr = ldap_next_attribute( ld, entry, ber ) - ) { - PyObject* valuelist; - PyObject* pyattr; - struct berval **bvals; + ldap_set_option(ld, LDAP_OPT_ERROR_NUMBER, &err); + Py_DECREF(result); + ldap_msgfree(m); + ldap_memfree(dn); + ldap_controls_free(serverctrls); + return LDAPerror(ld, "LDAPControls_to_List"); + } + ldap_controls_free(serverctrls); - pyattr = PyUnicode_FromString(attr); + /* Fill attrdict with lists */ + for (attr = ldap_first_attribute(ld, entry, &ber); + attr != NULL; attr = ldap_next_attribute(ld, entry, ber) + ) { + PyObject *valuelist; + PyObject *pyattr; + struct berval **bvals; - bvals = ldap_get_values_len( ld, entry, attr ); + pyattr = PyUnicode_FromString(attr); - /* Find which list to append to */ - if ( PyDict_Contains( attrdict, pyattr ) ) { - valuelist = PyDict_GetItem( attrdict, pyattr ); - } else { - valuelist = PyList_New(0); - if (valuelist != NULL && PyDict_SetItem(attrdict, - pyattr, valuelist) == -1) { - Py_DECREF(valuelist); - valuelist = NULL; /* catch error later */ - } - } + bvals = ldap_get_values_len(ld, entry, attr); - if (valuelist == NULL) { - Py_DECREF(pyattr); - Py_DECREF(attrdict); - Py_DECREF(result); - if (ber != NULL) - ber_free(ber, 0); - ldap_msgfree( m ); - ldap_memfree(attr); - ldap_memfree(dn); - Py_XDECREF(pyctrls); - return NULL; - } + /* Find which list to append to */ + if (PyDict_Contains(attrdict, pyattr)) { + valuelist = PyDict_GetItem(attrdict, pyattr); + } + else { + valuelist = PyList_New(0); + if (valuelist != NULL && PyDict_SetItem(attrdict, + pyattr, + valuelist) == -1) { + Py_DECREF(valuelist); + valuelist = NULL; /* catch error later */ + } + } - if (bvals != NULL) { - Py_ssize_t i; - for (i=0; bvals[i]; i++) { - PyObject *valuestr; + if (valuelist == NULL) { + Py_DECREF(pyattr); + Py_DECREF(attrdict); + Py_DECREF(result); + if (ber != NULL) + ber_free(ber, 0); + ldap_msgfree(m); + ldap_memfree(attr); + ldap_memfree(dn); + Py_XDECREF(pyctrls); + return NULL; + } - valuestr = LDAPberval_to_object(bvals[i]); - if (PyList_Append( valuelist, valuestr ) == -1) { - Py_DECREF(pyattr); - Py_DECREF(attrdict); - Py_DECREF(result); - Py_DECREF(valuestr); - Py_DECREF(valuelist); - if (ber != NULL) - ber_free(ber, 0); - ldap_msgfree( m ); - ldap_memfree(attr); - ldap_memfree(dn); - Py_XDECREF(pyctrls); - return NULL; - } - Py_DECREF(valuestr); - } - ldap_value_free_len(bvals); - } - Py_DECREF(pyattr); - Py_DECREF( valuelist ); - ldap_memfree(attr); - } + if (bvals != NULL) { + Py_ssize_t i; - pydn = PyUnicode_FromString(dn); - if (pydn == NULL) { - Py_DECREF(result); - ldap_msgfree( m ); - ldap_memfree(dn); - return NULL; - } + for (i = 0; bvals[i]; i++) { + PyObject *valuestr; - if (add_ctrls) { - entrytuple = Py_BuildValue("(OOO)", pydn, attrdict, pyctrls); - } else { - entrytuple = Py_BuildValue("(OO)", pydn, attrdict); - } - Py_DECREF(pydn); - ldap_memfree(dn); - Py_DECREF(attrdict); - Py_XDECREF(pyctrls); - PyList_Append(result, entrytuple); - Py_DECREF(entrytuple); - if (ber != NULL) - ber_free(ber, 0); - } - 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); + valuestr = LDAPberval_to_object(bvals[i]); + if (PyList_Append(valuelist, valuestr) == -1) { + Py_DECREF(pyattr); + Py_DECREF(attrdict); + Py_DECREF(result); + Py_DECREF(valuestr); + Py_DECREF(valuelist); + if (ber != NULL) + ber_free(ber, 0); + ldap_msgfree(m); + ldap_memfree(attr); + ldap_memfree(dn); + Py_XDECREF(pyctrls); + return NULL; + } + Py_DECREF(valuestr); + } + ldap_value_free_len(bvals); + } + Py_DECREF(pyattr); + Py_DECREF(valuelist); + ldap_memfree(attr); + } - if (reflist == NULL) { - Py_DECREF(result); - ldap_msgfree( m ); - return NULL; - } - if (ldap_parse_reference(ld, entry, &refs, &serverctrls, 0) != LDAP_SUCCESS) { - Py_DECREF(reflist); - Py_DECREF(result); - ldap_msgfree( m ); - return LDAPerror( ld, "ldap_parse_reference" ); - } - /* convert serverctrls to list of tuples */ - if ( ! ( pyctrls = LDAPControls_to_List( serverctrls ) ) ) { - int err = LDAP_NO_MEMORY; - ldap_set_option( ld, LDAP_OPT_ERROR_NUMBER, &err ); - Py_DECREF(reflist); - Py_DECREF(result); - ldap_msgfree( m ); - ldap_controls_free(serverctrls); - return LDAPerror( ld, "LDAPControls_to_List" ); - } - ldap_controls_free(serverctrls); - if (refs) { - Py_ssize_t i; - for (i=0; refs[i] != NULL; i++) { - /* A referal is a distinguishedName => unicode */ - PyObject *refstr = PyUnicode_FromString(refs[i]); - PyList_Append(reflist, refstr); - Py_DECREF(refstr); - } - ber_memvfree( (void **) refs ); - } - if (add_ctrls) { - entrytuple = Py_BuildValue("(sOO)", NULL, reflist, pyctrls); - } else { - entrytuple = Py_BuildValue("(sO)", NULL, reflist); - } - Py_DECREF(reflist); - Py_XDECREF(pyctrls); - PyList_Append(result, entrytuple); - Py_DECREF(entrytuple); - } - if (add_intermediates) { - for(entry = ldap_first_message(ld,m); - entry != NULL; - entry = ldap_next_message(ld,entry)) - { - /* list of tuples */ - /* each tuple is OID, Berval, controllist */ - if ( LDAP_RES_INTERMEDIATE == ldap_msgtype( entry ) ) { - PyObject* valtuple; - PyObject *valuestr; - char *retoid = 0; - PyObject *pyoid; - struct berval *retdata = 0; + pydn = PyUnicode_FromString(dn); + if (pydn == NULL) { + Py_DECREF(result); + ldap_msgfree(m); + ldap_memfree(dn); + return NULL; + } - if (ldap_parse_intermediate( ld, entry, &retoid, &retdata, &serverctrls, 0 ) != LDAP_SUCCESS) { - Py_DECREF(result); - ldap_msgfree( m ); - return LDAPerror( ld, "ldap_parse_intermediate" ); - } - /* convert serverctrls to list of tuples */ - if ( ! ( pyctrls = LDAPControls_to_List( serverctrls ) ) ) { - int err = LDAP_NO_MEMORY; - ldap_set_option( ld, LDAP_OPT_ERROR_NUMBER, &err ); - Py_DECREF(result); - ldap_msgfree( m ); - ldap_controls_free(serverctrls); - ldap_memfree( retoid ); - ber_bvfree( retdata ); - return LDAPerror( ld, "LDAPControls_to_List" ); - } - ldap_controls_free(serverctrls); + if (add_ctrls) { + entrytuple = Py_BuildValue("(OOO)", pydn, attrdict, pyctrls); + } + else { + entrytuple = Py_BuildValue("(OO)", pydn, attrdict); + } + Py_DECREF(pydn); + ldap_memfree(dn); + Py_DECREF(attrdict); + Py_XDECREF(pyctrls); + PyList_Append(result, entrytuple); + Py_DECREF(entrytuple); + if (ber != NULL) + ber_free(ber, 0); + } + 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); - valuestr = LDAPberval_to_object(retdata); - ber_bvfree( retdata ); - pyoid = PyUnicode_FromString(retoid); - ldap_memfree( retoid ); - if (pyoid == NULL) { - Py_DECREF(result); - ldap_msgfree( m ); - return NULL; - } - valtuple = Py_BuildValue("(OOO)", pyoid, - valuestr ? valuestr : Py_None, - pyctrls); - Py_DECREF(pyoid); - Py_DECREF(valuestr); - Py_XDECREF(pyctrls); - PyList_Append(result, valtuple); - Py_DECREF(valtuple); - } - } - } - ldap_msgfree( m ); - return result; + if (reflist == NULL) { + Py_DECREF(result); + ldap_msgfree(m); + return NULL; + } + if (ldap_parse_reference(ld, entry, &refs, &serverctrls, 0) != + LDAP_SUCCESS) { + Py_DECREF(reflist); + Py_DECREF(result); + ldap_msgfree(m); + return LDAPerror(ld, "ldap_parse_reference"); + } + /* convert serverctrls to list of tuples */ + if (!(pyctrls = LDAPControls_to_List(serverctrls))) { + int err = LDAP_NO_MEMORY; + + ldap_set_option(ld, LDAP_OPT_ERROR_NUMBER, &err); + Py_DECREF(reflist); + Py_DECREF(result); + ldap_msgfree(m); + ldap_controls_free(serverctrls); + return LDAPerror(ld, "LDAPControls_to_List"); + } + ldap_controls_free(serverctrls); + if (refs) { + Py_ssize_t i; + + for (i = 0; refs[i] != NULL; i++) { + /* A referal is a distinguishedName => unicode */ + PyObject *refstr = PyUnicode_FromString(refs[i]); + + PyList_Append(reflist, refstr); + Py_DECREF(refstr); + } + ber_memvfree((void **)refs); + } + if (add_ctrls) { + entrytuple = Py_BuildValue("(sOO)", NULL, reflist, pyctrls); + } + else { + entrytuple = Py_BuildValue("(sO)", NULL, reflist); + } + Py_DECREF(reflist); + Py_XDECREF(pyctrls); + PyList_Append(result, entrytuple); + Py_DECREF(entrytuple); + } + if (add_intermediates) { + for (entry = ldap_first_message(ld, m); + entry != NULL; entry = ldap_next_message(ld, entry)) { + /* list of tuples */ + /* each tuple is OID, Berval, controllist */ + if (LDAP_RES_INTERMEDIATE == ldap_msgtype(entry)) { + PyObject *valtuple; + PyObject *valuestr; + char *retoid = 0; + PyObject *pyoid; + struct berval *retdata = 0; + + if (ldap_parse_intermediate + (ld, entry, &retoid, &retdata, &serverctrls, + 0) != LDAP_SUCCESS) { + Py_DECREF(result); + ldap_msgfree(m); + return LDAPerror(ld, "ldap_parse_intermediate"); + } + /* convert serverctrls to list of tuples */ + if (!(pyctrls = LDAPControls_to_List(serverctrls))) { + int err = LDAP_NO_MEMORY; + + ldap_set_option(ld, LDAP_OPT_ERROR_NUMBER, &err); + Py_DECREF(result); + ldap_msgfree(m); + ldap_controls_free(serverctrls); + ldap_memfree(retoid); + ber_bvfree(retdata); + return LDAPerror(ld, "LDAPControls_to_List"); + } + ldap_controls_free(serverctrls); + + valuestr = LDAPberval_to_object(retdata); + ber_bvfree(retdata); + pyoid = PyUnicode_FromString(retoid); + ldap_memfree(retoid); + if (pyoid == NULL) { + Py_DECREF(result); + ldap_msgfree(m); + return NULL; + } + valtuple = Py_BuildValue("(OOO)", pyoid, + valuestr ? valuestr : Py_None, + pyctrls); + Py_DECREF(pyoid); + Py_DECREF(valuestr); + Py_XDECREF(pyctrls); + PyList_Append(result, valtuple); + Py_DECREF(valtuple); + } + } + } + ldap_msgfree(m); + return result; } diff --git a/Modules/message.h b/Modules/message.h index c3522ac3..2978ea56 100644 --- a/Modules/message.h +++ b/Modules/message.h @@ -1,12 +1,13 @@ /* See https://www.python-ldap.org/ for details. */ -#ifndef __h_message -#define __h_message +#ifndef __h_message +#define __h_message #include "common.h" #include "lber.h" #include "ldap.h" -extern PyObject* LDAPmessage_to_python( LDAP*ld, LDAPMessage*m, int add_ctrls, int add_intermediates ); +extern PyObject *LDAPmessage_to_python(LDAP *ld, LDAPMessage *m, int add_ctrls, + int add_intermediates); #endif /* __h_message_ */ diff --git a/Modules/options.c b/Modules/options.c index ee606d46..85560e62 100644 --- a/Modules/options.c +++ b/Modules/options.c @@ -7,9 +7,10 @@ #include "options.h" void -set_timeval_from_double( struct timeval *tv, double d ) { - tv->tv_usec = (long) ( fmod(d, 1.0) * 1000000.0 ); - tv->tv_sec = (long) floor(d); +set_timeval_from_double(struct timeval *tv, double d) +{ + tv->tv_usec = (long)(fmod(d, 1.0) * 1000000.0); + tv->tv_sec = (long)floor(d); } /** @@ -23,7 +24,7 @@ option_error(int res, const char *fn) PyErr_SetString(PyExc_ValueError, "option error"); else if (res == LDAP_PARAM_ERROR) PyErr_SetString(PyExc_ValueError, "parameter error"); - else if (res == LDAP_NO_MEMORY) + else if (res == LDAP_NO_MEMORY) PyErr_NoMemory(); else PyErr_Format(PyExc_SystemError, "error %d from %s", res, fn); @@ -48,15 +49,15 @@ LDAP_set_option(LDAPObject *self, int option, PyObject *value) ld = self ? self->ldap : NULL; - switch(option) { + switch (option) { case LDAP_OPT_API_INFO: case LDAP_OPT_API_FEATURE_INFO: #ifdef HAVE_SASL case LDAP_OPT_X_SASL_SSF: #endif - /* Read-only options */ - PyErr_SetString(PyExc_ValueError, "read-only option"); - return 0; + /* Read-only options */ + PyErr_SetString(PyExc_ValueError, "read-only option"); + return 0; case LDAP_OPT_REFERRALS: case LDAP_OPT_RESTART: #ifdef LDAP_OPT_X_SASL_NOCANON @@ -65,9 +66,9 @@ LDAP_set_option(LDAPObject *self, int option, PyObject *value) #ifdef LDAP_OPT_CONNECT_ASYNC case LDAP_OPT_CONNECT_ASYNC: #endif - /* Truth-value options */ - ptr = PyObject_IsTrue(value) ? LDAP_OPT_ON : LDAP_OPT_OFF; - break; + /* Truth-value options */ + ptr = PyObject_IsTrue(value) ? LDAP_OPT_ON : LDAP_OPT_OFF; + break; case LDAP_OPT_DEREF: case LDAP_OPT_SIZELIMIT: @@ -102,11 +103,11 @@ LDAP_set_option(LDAPObject *self, int option, PyObject *value) case LDAP_OPT_X_KEEPALIVE_INTERVAL: #endif - /* integer value options */ - if (!PyArg_Parse(value, "i:set_option", &intval)) - return 0; - ptr = &intval; - break; + /* integer value options */ + if (!PyArg_Parse(value, "i:set_option", &intval)) + return 0; + ptr = &intval; + break; case LDAP_OPT_HOST_NAME: case LDAP_OPT_URI: #ifdef LDAP_OPT_DEFBASE @@ -129,69 +130,71 @@ LDAP_set_option(LDAPObject *self, int option, PyObject *value) #ifdef HAVE_SASL case LDAP_OPT_X_SASL_SECPROPS: #endif - /* String valued options */ - if (!PyArg_Parse(value, "s:set_option", &strval)) - return 0; - ptr = strval; - break; + /* String valued options */ + if (!PyArg_Parse(value, "s:set_option", &strval)) + return 0; + ptr = strval; + break; case LDAP_OPT_TIMEOUT: case LDAP_OPT_NETWORK_TIMEOUT: /* Float valued timeval options */ if (value == Py_None) { /* None is mapped to infinity timeout */ doubleval = -1; - } else { + } + else { /* 'd' handles int/long */ if (!PyArg_Parse(value, "d:set_option", &doubleval)) { if (PyErr_ExceptionMatches(PyExc_TypeError)) { /* TypeError: mention either float or None is expected */ PyErr_Clear(); - PyErr_Format( - PyExc_TypeError, - "A float or None is expected for timeout, got %.100s", - Py_TYPE(value)->tp_name - ); + PyErr_Format(PyExc_TypeError, + "A float or None is expected for timeout, got %.100s", + Py_TYPE(value)->tp_name); } return 0; } } if (doubleval >= 0) { - set_timeval_from_double( &tv, doubleval ); + set_timeval_from_double(&tv, doubleval); ptr = &tv; - } else if (doubleval == -1) { + } + else if (doubleval == -1) { /* -1 is infinity timeout */ tv.tv_sec = -1; tv.tv_usec = 0; ptr = &tv; - } else { - PyErr_Format( - PyExc_ValueError, - "timeout must be >= 0 or -1/None for infinity, got %d", - option - ); + } + else { + PyErr_Format(PyExc_ValueError, + "timeout must be >= 0 or -1/None for infinity, got %d", + option); return 0; } break; case LDAP_OPT_SERVER_CONTROLS: case LDAP_OPT_CLIENT_CONTROLS: - if (!LDAPControls_from_object(value, &controls)) - return 0; - ptr = controls; - break; + if (!LDAPControls_from_object(value, &controls)) + return 0; + ptr = controls; + break; default: - PyErr_Format(PyExc_ValueError, "unknown option %d", option); - return 0; + PyErr_Format(PyExc_ValueError, "unknown option %d", option); + return 0; } - - if (self) LDAP_BEGIN_ALLOW_THREADS(self); + + if (self) + LDAP_BEGIN_ALLOW_THREADS(self); res = ldap_set_option(ld, option, ptr); - if (self) LDAP_END_ALLOW_THREADS(self); + if (self) + LDAP_END_ALLOW_THREADS(self); - if ((option == LDAP_OPT_SERVER_CONTROLS) || (option == LDAP_OPT_CLIENT_CONTROLS)) + if ((option == LDAP_OPT_SERVER_CONTROLS) || + (option == LDAP_OPT_CLIENT_CONTROLS)) LDAPControl_List_DEL(controls); - + if (res != LDAP_OPT_SUCCESS) { option_error(res, "ldap_set_option"); return 0; @@ -215,41 +218,44 @@ LDAP_get_option(LDAPObject *self, int option) ld = self ? self->ldap : NULL; - switch(option) { + switch (option) { case LDAP_OPT_API_INFO: - apiinfo.ldapai_info_version = LDAP_API_INFO_VERSION; - if (self) LDAP_BEGIN_ALLOW_THREADS(self); - res = ldap_get_option( ld, option, &apiinfo ); - if (self) LDAP_END_ALLOW_THREADS(self); - if (res != LDAP_OPT_SUCCESS) - return option_error(res, "ldap_get_option"); - - /* put the extensions into tuple form */ - num_extensions = 0; - while (apiinfo.ldapai_extensions[num_extensions]) - num_extensions++; - extensions = PyTuple_New(num_extensions); - for (i = 0; i < num_extensions; i++) - PyTuple_SET_ITEM(extensions, i, - PyUnicode_FromString(apiinfo.ldapai_extensions[i])); + apiinfo.ldapai_info_version = LDAP_API_INFO_VERSION; + if (self) + LDAP_BEGIN_ALLOW_THREADS(self); + res = ldap_get_option(ld, option, &apiinfo); + if (self) + LDAP_END_ALLOW_THREADS(self); + if (res != LDAP_OPT_SUCCESS) + return option_error(res, "ldap_get_option"); + + /* put the extensions into tuple form */ + num_extensions = 0; + while (apiinfo.ldapai_extensions[num_extensions]) + num_extensions++; + extensions = PyTuple_New(num_extensions); + for (i = 0; i < num_extensions; i++) + PyTuple_SET_ITEM(extensions, i, + PyUnicode_FromString(apiinfo. + ldapai_extensions[i])); - /* return api info as a dictionary */ - v = Py_BuildValue("{s:i, s:i, s:i, s:s, s:i, s:O}", - "info_version", apiinfo.ldapai_info_version, - "api_version", apiinfo.ldapai_api_version, - "protocol_version", apiinfo.ldapai_protocol_version, - "vendor_name", apiinfo.ldapai_vendor_name, - "vendor_version", apiinfo.ldapai_vendor_version, - "extensions", extensions); + /* return api info as a dictionary */ + v = Py_BuildValue("{s:i, s:i, s:i, s:s, s:i, s:O}", + "info_version", apiinfo.ldapai_info_version, + "api_version", apiinfo.ldapai_api_version, + "protocol_version", apiinfo.ldapai_protocol_version, + "vendor_name", apiinfo.ldapai_vendor_name, + "vendor_version", apiinfo.ldapai_vendor_version, + "extensions", extensions); - if (apiinfo.ldapai_vendor_name) - ldap_memfree(apiinfo.ldapai_vendor_name); - for (i = 0; i < num_extensions; i++) - ldap_memfree(apiinfo.ldapai_extensions[i]); - ldap_memfree(apiinfo.ldapai_extensions); - Py_DECREF(extensions); + if (apiinfo.ldapai_vendor_name) + ldap_memfree(apiinfo.ldapai_vendor_name); + for (i = 0; i < num_extensions; i++) + ldap_memfree(apiinfo.ldapai_extensions[i]); + ldap_memfree(apiinfo.ldapai_extensions); + Py_DECREF(extensions); - return v; + return v; #ifdef HAVE_SASL case LDAP_OPT_X_SASL_SSF: @@ -292,13 +298,15 @@ LDAP_get_option(LDAPObject *self, int option) #ifdef LDAP_OPT_X_KEEPALIVE_INTERVAL case LDAP_OPT_X_KEEPALIVE_INTERVAL: #endif - /* Integer-valued options */ - if (self) LDAP_BEGIN_ALLOW_THREADS(self); - res = ldap_get_option(ld, option, &intval); - if (self) LDAP_END_ALLOW_THREADS(self); - if (res != LDAP_OPT_SUCCESS) - return option_error(res, "ldap_get_option"); - return PyInt_FromLong(intval); + /* Integer-valued options */ + if (self) + LDAP_BEGIN_ALLOW_THREADS(self); + res = ldap_get_option(ld, option, &intval); + if (self) + LDAP_END_ALLOW_THREADS(self); + if (res != LDAP_OPT_SUCCESS) + return option_error(res, "ldap_get_option"); + return PyInt_FromLong(intval); case LDAP_OPT_HOST_NAME: case LDAP_OPT_URI: @@ -338,53 +346,59 @@ LDAP_get_option(LDAPObject *self, int option) case LDAP_OPT_X_SASL_USERNAME: #endif #endif - /* String-valued options */ - if (self) LDAP_BEGIN_ALLOW_THREADS(self); - res = ldap_get_option(ld, option, &strval); - if (self) LDAP_END_ALLOW_THREADS(self); - if (res != LDAP_OPT_SUCCESS) - return option_error(res, "ldap_get_option"); - if (strval == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - v = PyUnicode_FromString(strval); - ldap_memfree(strval); - return v; + /* String-valued options */ + if (self) + LDAP_BEGIN_ALLOW_THREADS(self); + res = ldap_get_option(ld, option, &strval); + if (self) + LDAP_END_ALLOW_THREADS(self); + if (res != LDAP_OPT_SUCCESS) + return option_error(res, "ldap_get_option"); + if (strval == NULL) { + Py_INCREF(Py_None); + return Py_None; + } + v = PyUnicode_FromString(strval); + ldap_memfree(strval); + return v; case LDAP_OPT_TIMEOUT: case LDAP_OPT_NETWORK_TIMEOUT: - /* Double-valued timeval options */ - if (self) LDAP_BEGIN_ALLOW_THREADS(self); - res = ldap_get_option(ld, option, &tv); - if (self) LDAP_END_ALLOW_THREADS(self); - if (res != LDAP_OPT_SUCCESS) - return option_error(res, "ldap_get_option"); - if (tv == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - v = PyFloat_FromDouble( - (double) tv->tv_sec + ( (double) tv->tv_usec / 1000000.0 ) + /* Double-valued timeval options */ + if (self) + LDAP_BEGIN_ALLOW_THREADS(self); + res = ldap_get_option(ld, option, &tv); + if (self) + LDAP_END_ALLOW_THREADS(self); + if (res != LDAP_OPT_SUCCESS) + return option_error(res, "ldap_get_option"); + if (tv == NULL) { + Py_INCREF(Py_None); + return Py_None; + } + v = PyFloat_FromDouble((double)tv->tv_sec + + ((double)tv->tv_usec / 1000000.0) ); - ldap_memfree(tv); - return v; + ldap_memfree(tv); + return v; case LDAP_OPT_SERVER_CONTROLS: case LDAP_OPT_CLIENT_CONTROLS: - if (self) LDAP_BEGIN_ALLOW_THREADS(self); - res = ldap_get_option(ld, option, &lcs); - if (self) LDAP_END_ALLOW_THREADS(self); + if (self) + LDAP_BEGIN_ALLOW_THREADS(self); + res = ldap_get_option(ld, option, &lcs); + if (self) + LDAP_END_ALLOW_THREADS(self); + + if (res != LDAP_OPT_SUCCESS) + return option_error(res, "ldap_get_option"); - if (res != LDAP_OPT_SUCCESS) - return option_error(res, "ldap_get_option"); + v = LDAPControls_to_List(lcs); + ldap_controls_free(lcs); + return v; - v = LDAPControls_to_List(lcs); - ldap_controls_free(lcs); - return v; - default: - PyErr_Format(PyExc_ValueError, "unknown option %d", option); - return NULL; + PyErr_Format(PyExc_ValueError, "unknown option %d", option); + return NULL; } } diff --git a/Modules/options.h b/Modules/options.h index 570fdc15..fd6a5ce2 100644 --- a/Modules/options.h +++ b/Modules/options.h @@ -4,4 +4,4 @@ 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 ); +void set_timeval_from_double(struct timeval *tv, double d);