From 1eacf48835bcf7c28670aea30d981f170a0a2e73 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Wed, 3 Jun 2009 10:27:47 +0000 Subject: make lookup_dn2uid() available to other modules and split uid2dn() into uid2entry() and uid2dn() (from nss-pam-ldapd branch) git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@922 ef36b2f9-881f-0410-afb5-c4e39611909c --- nslcd/common.h | 7 +++++++ nslcd/passwd.c | 44 +++++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 21 deletions(-) (limited to 'nslcd') diff --git a/nslcd/common.h b/nslcd/common.h index 9fc56fa..48a7536 100644 --- a/nslcd/common.h +++ b/nslcd/common.h @@ -79,9 +79,16 @@ int read_address(TFILE *fp,char *addr,int *addrlen,int *af); /* checks to see if the specified string is a valid user or group name */ MUST_USE int isvalidname(const char *name); +/* Perform an LDAP lookup to translate the DN into a uid. + This function either returns NULL or a strdup()ed string. */ +MUST_USE char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn,int *rcp); + /* transforms the DN info a uid doing an LDAP lookup if needed */ MUST_USE char *dn2uid(MYLDAP_SESSION *session,const char *dn,char *buf,size_t buflen); +/* use the user id to lookup an LDAP entry */ +MYLDAP_ENTRY *uid2entry(MYLDAP_SESSION *session,const char *uid); + /* transforms the uid into a DN by doing an LDAP lookup */ MUST_USE char *uid2dn(MYLDAP_SESSION *session,const char *uid,char *buf,size_t buflen); diff --git a/nslcd/passwd.c b/nslcd/passwd.c index d140de3..58bc8f3 100644 --- a/nslcd/passwd.c +++ b/nslcd/passwd.c @@ -141,7 +141,7 @@ struct dn2uid_cache_entry /* Perform an LDAP lookup to translate the DN into a uid. This function either returns NULL or a strdup()ed string. */ -static char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn) +char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn,int *rcp) { MYLDAP_SEARCH *search; MYLDAP_ENTRY *entry; @@ -149,6 +149,8 @@ static char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn) int rc; const char **values; char *uid; + if (rcp!=NULL) + *rcp=LDAP_SUCCESS; /* we have to look up the entry */ attrs[0]=attmap_passwd_uid; attrs[1]=NULL; @@ -162,7 +164,11 @@ static char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn) if (entry==NULL) { if (rc!=LDAP_SUCCESS) + { log_log(LOG_WARNING,"lookup of user %s failed: %s",dn,ldap_err2string(rc)); + if (rcp!=NULL) + *rcp=rc; + } return NULL; } /* get uid (just use first one) */ @@ -215,7 +221,7 @@ char *dn2uid(MYLDAP_SESSION *session,const char *dn,char *buf,size_t buflen) } pthread_mutex_unlock(&dn2uid_cache_mutex); /* look up the uid using an LDAP query */ - uid=lookup_dn2uid(session,dn); + uid=lookup_dn2uid(session,dn,NULL); /* store the result in the cache */ pthread_mutex_lock(&dn2uid_cache_mutex); if (cacheentry==NULL) @@ -242,21 +248,21 @@ char *dn2uid(MYLDAP_SESSION *session,const char *dn,char *buf,size_t buflen) return buf; } -char *uid2dn(MYLDAP_SESSION *session,const char *uid,char *buf,size_t buflen) +MYLDAP_ENTRY *uid2entry(MYLDAP_SESSION *session,const char *uid) { MYLDAP_SEARCH *search=NULL; MYLDAP_ENTRY *entry=NULL; const char *base; int i; - static const char *attrs[1]; + static const char *attrs[2]; int rc; - const char *dn; char filter[1024]; /* if it isn't a valid username, just bail out now */ if (!isvalidname(uid)) return NULL; - /* set up attributes (we don't care, we just want the DN) */ - attrs[0]=NULL; + /* set up attributes (we don't need much) */ + attrs[0]=attmap_passwd_uid; + attrs[1]=NULL; /* we have to look up the entry */ mkfilter_passwd_byname(uid,filter,sizeof(filter)); for (i=0;(i