From 364d83c5b955c254287b448d414481f9f5d921a7 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Tue, 25 Dec 2007 17:10:40 +0000 Subject: have myldap_get_entry() return an LDAP status code that can signal errors in the lookup git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@531 ef36b2f9-881f-0410-afb5-c4e39611909c --- nslcd/common.h | 10 +++++++--- nslcd/myldap.c | 27 +++++++++++++++++++++++++-- nslcd/myldap.h | 7 ++++--- 3 files changed, 36 insertions(+), 8 deletions(-) (limited to 'nslcd') diff --git a/nslcd/common.h b/nslcd/common.h index 4a8aa8b..52d2ebd 100644 --- a/nslcd/common.h +++ b/nslcd/common.h @@ -113,10 +113,11 @@ int nslcd_shadow_all(TFILE *fp,MYLDAP_SESSION *session); #define NSLCD_HANDLE(db,fn,readfn,logcall,action,mkfilter,writefn) \ int nslcd_##db##_##fn(TFILE *fp,MYLDAP_SESSION *session) \ { \ - /* define commong variables */ \ + /* define common variables */ \ int32_t tmpint32; \ MYLDAP_SEARCH *search; \ MYLDAP_ENTRY *entry; \ + int rc; \ /* read request parameters */ \ readfn; \ /* log call */ \ @@ -136,12 +137,15 @@ int nslcd_shadow_all(TFILE *fp,MYLDAP_SESSION *session); if ((search=myldap_search(session,db##_base,db##_scope,filter,db##_attrs))==NULL) \ return -1; \ /* go over results */ \ - while ((entry=myldap_get_entry(search))!=NULL) \ + while ((entry=myldap_get_entry(search,&rc))!=NULL) \ { \ writefn; \ } \ /* write the final result code */ \ - WRITE_INT32(fp,NSLCD_RESULT_END); \ + if (rc==LDAP_SUCCESS) \ + { \ + WRITE_INT32(fp,NSLCD_RESULT_END); \ + } \ return 0; \ } diff --git a/nslcd/myldap.c b/nslcd/myldap.c index 942f22c..e35b015 100644 --- a/nslcd/myldap.c +++ b/nslcd/myldap.c @@ -744,7 +744,7 @@ void myldap_search_close(MYLDAP_SEARCH *search) myldap_search_free(search); } -MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) +MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search,int *rcp) { int rc; int parserc; @@ -757,6 +757,8 @@ MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) { log_log(LOG_ERR,"myldap_get_entry(): invalid search passed"); errno=EINVAL; + if (rcp!=NULL) + *rcp=LDAP_OPERATIONS_ERROR; return NULL; } /* set up a timelimit value for operations */ @@ -794,16 +796,22 @@ MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) rc=LDAP_UNAVAILABLE; log_log(LOG_ERR,"ldap_result() failed: %s",ldap_err2string(rc)); myldap_search_close(search); + if (rcp!=NULL) + *rcp=rc; return NULL; case 0: /* the timeout expired */ log_log(LOG_ERR,"ldap_result() timed out"); myldap_search_close(search); + if (rcp!=NULL) + *rcp=LDAP_TIMELIMIT_EXCEEDED; return NULL; case LDAP_RES_SEARCH_ENTRY: /* we have a normal search entry, update timestamp and return result */ time(&(search->session->lastactivity)); search->entry=myldap_entry_new(search); + if (rcp!=NULL) + *rcp=LDAP_SUCCESS; return search->entry; case LDAP_RES_SEARCH_RESULT: /* we have a search result, parse it */ @@ -824,6 +832,8 @@ MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) ldap_controls_free(resultcontrols); log_log(LOG_ERR,"ldap_parse_result() failed: %s",ldap_err2string(parserc)); myldap_search_close(search); + if (rcp!=NULL) + *rcp=parserc; return NULL; } /* check for errors in message */ @@ -833,6 +843,8 @@ MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) ldap_controls_free(resultcontrols); log_log(LOG_ERR,"ldap_result() failed: %s",ldap_err2string(rc)); myldap_search_close(search); + if (rcp!=NULL) + *rcp=rc; return NULL; } /* handle result controls */ @@ -848,7 +860,13 @@ MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) search->msgid=-1; /* check if there are more pages to come */ if ((search->cookie==NULL)||(search->cookie->bv_len==0)) + { + /* we are at the end of the search, no more results */ + myldap_search_close(search); + if (rcp!=NULL) + *rcp=LDAP_SUCCESS; return NULL; + } /* try the next page */ serverctrls[0]=NULL; serverctrls[1]=NULL; @@ -862,6 +880,8 @@ MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) log_log(LOG_WARNING,"ldap_create_page_control() failed: %s", ldap_err2string(rc)); myldap_search_close(search); + if (rcp!=NULL) + *rcp=rc; return NULL; } /* set up a new search for the next page */ @@ -875,10 +895,11 @@ MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) log_log(LOG_WARNING,"ldap_search_ext() failed: %s", ldap_err2string(rc)); myldap_search_close(search); + if (rcp!=NULL) + *rcp=rc; return NULL; } search->msgid=msgid; - /* we continue with another pass */ break; case LDAP_RES_SEARCH_REFERENCE: @@ -886,6 +907,8 @@ MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search) default: log_log(LOG_WARNING,"ldap_result() returned unexpected result type"); myldap_search_close(search); + if (rcp!=NULL) + *rcp=LDAP_PROTOCOL_ERROR; return NULL; } } diff --git a/nslcd/myldap.h b/nslcd/myldap.h index 10f54c4..5efb287 100644 --- a/nslcd/myldap.h +++ b/nslcd/myldap.h @@ -20,7 +20,7 @@ 02110-1301 USA */ -/* +/* This file describes the API of the myldap module which takes the complexity out of using the OpenLDAP library. Memory management, paging, reconnect logic, idle timeout of connections, etc is taken care of by the module. @@ -86,8 +86,9 @@ void myldap_search_close(MYLDAP_SEARCH *search); no more entries are available). Note that any memory allocated to return information about the previous entry (e.g. with myldap_get_values()) is freed with this call. The search is autoamtically closed when no more - results are available. */ -MUST_USE MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search); + results are available. The function returns an LDAP status code in the + location pointed to by rcp if it is non-NULL. */ +MUST_USE MYLDAP_ENTRY *myldap_get_entry(MYLDAP_SEARCH *search,int *rcp); /* Get the DN from the entry. This function does not return NULL (on error "unknown" is returned). */ -- cgit v1.2.3-54-g00ecf