diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2007-12-25 17:10:40 +0000 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2007-12-25 17:10:40 +0000 |
commit | 364d83c5b955c254287b448d414481f9f5d921a7 (patch) | |
tree | 5b01db426c45550ef0c4a078eadc324a284f37e5 | |
parent | 35c147a6fc15b787d88be6f0fb54bc9650d52731 (diff) |
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
-rw-r--r-- | nslcd/common.h | 10 | ||||
-rw-r--r-- | nslcd/myldap.c | 27 | ||||
-rw-r--r-- | nslcd/myldap.h | 7 | ||||
-rw-r--r-- | tests/test_myldap.c | 23 |
4 files changed, 48 insertions, 19 deletions
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). */ diff --git a/tests/test_myldap.c b/tests/test_myldap.c index d84cdbd..d3856e6 100644 --- a/tests/test_myldap.c +++ b/tests/test_myldap.c @@ -56,6 +56,7 @@ static void test_search(void) MYLDAP_ENTRY *entry; const char *attrs[] = { "uid", "cn", "gid", NULL }; int i; + int rc; /* initialize session */ printf("test_search(): getting session...\n"); session=myldap_create_session(); @@ -69,14 +70,14 @@ static void test_search(void) assert(search!=NULL); /* go over results */ printf("test_search(): get results...\n"); - for (i=0;(entry=myldap_get_entry(search))!=NULL;i++) + for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++) { if (i<MAXRESULTS) printf("test_search(): [%d] DN %s\n",i,myldap_get_dn(entry)); else if (i==MAXRESULTS) printf("test_search(): ...\n"); } - printf("test_search(): %d entries returned\n",i); + printf("test_search(): %d entries returned: %s\n",i,ldap_err2string(rc)); /* perform another search */ printf("test_search(): doing search...\n"); search=myldap_search(session,nslcd_cfg->ldc_base, @@ -86,14 +87,14 @@ static void test_search(void) assert(search!=NULL); /* go over results */ printf("test_search(): get results...\n"); - for (i=0;(entry=myldap_get_entry(search))!=NULL;i++) + for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++) { if (i<MAXRESULTS) printf("test_search(): [%d] DN %s\n",i,myldap_get_dn(entry)); else if (i==MAXRESULTS) printf("test_search(): ...\n"); } - printf("test_search(): %d entries returned\n",i); + printf("test_search(): %d entries returned: %s\n",i,ldap_err2string(rc)); /* clean up */ myldap_session_close(session); } @@ -119,7 +120,7 @@ static void test_get_values(void) attrs); assert(search!=NULL); /* go over results */ - for (i=0;(entry=myldap_get_entry(search))!=NULL;i++) + for (i=0;(entry=myldap_get_entry(search,NULL))!=NULL;i++) { if (i<MAXRESULTS) printf("test_get_values(): [%d] DN %s\n",i,myldap_get_dn(entry)); @@ -174,7 +175,7 @@ static void test_two_searches(void) attrs); assert(search1!=NULL); /* get a result from search1 */ - entry=myldap_get_entry(search1); + entry=myldap_get_entry(search1,NULL); assert(entry!=NULL); printf("test_two_searches(): [search1] DN %s\n",myldap_get_dn(entry)); vals=myldap_get_values(entry,"cn"); @@ -187,14 +188,14 @@ static void test_two_searches(void) attrs); assert(search2!=NULL); /* get a result from search2 */ - entry=myldap_get_entry(search2); + entry=myldap_get_entry(search2,NULL); assert(entry!=NULL); printf("test_two_searches(): [search2] DN %s\n",myldap_get_dn(entry)); vals=myldap_get_values(entry,"cn"); assert((vals!=NULL)&&(vals[0]!=NULL)); printf("test_two_searches(): [search2] cn=%s\n",vals[0]); /* get another result from search1 */ - entry=myldap_get_entry(search1); + entry=myldap_get_entry(search1,NULL); assert(entry!=NULL); printf("test_two_searches(): [search1] DN %s\n",myldap_get_dn(entry)); vals=myldap_get_values(entry,"cn"); @@ -213,6 +214,7 @@ static void *worker(void *arg) const char *attrs[] = { "uid", "cn", "gid", NULL }; struct worker_args *args=(struct worker_args *)arg; int i; + int rc; /* initialize session */ session=myldap_create_session(); assert(session!=NULL); @@ -223,14 +225,14 @@ static void *worker(void *arg) attrs); assert(search!=NULL); /* go over results */ - for (i=0;(entry=myldap_get_entry(search))!=NULL;i++) + for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++) { if (i<MAXRESULTS) printf("test_threads(): [worker %d] [%d] DN %s\n",args->id,i,myldap_get_dn(entry)); else if (i==MAXRESULTS) printf("test_threads(): [worker %d] ...\n",args->id); } - printf("test_threads(): [worker %d] DONE\n",args->id); + printf("test_threads(): [worker %d] DONE: %s\n",args->id,ldap_err2string(rc)); /* clean up */ myldap_session_close(session); return 0; @@ -261,7 +263,6 @@ static void test_connections(void) { MYLDAP_SESSION *session; MYLDAP_SEARCH *search; - MYLDAP_ENTRY *entry; const char *attrs[] = { "uid", "cn", "gid", NULL }; char *old_uris[NSS_LDAP_CONFIG_URI_MAX+1]; int i; |