diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2010-11-04 21:36:13 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2010-11-04 21:36:13 +0100 |
commit | 1e7270d346bd391e40456e2be42085bc324aa494 (patch) | |
tree | 0bb52a4fb8ff171146c055ce0a4d9c7fa1e7fb64 /nslcd | |
parent | 04e2a2ccbe2c7201928ac5f89d7dc493d25ad691 (diff) |
avoid unneeded strdup()s by using a passed buffer to lookup_dn2uid() and using strcmp() in dn2uid() to see if the existing cached value is ok
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1297 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nslcd')
-rw-r--r-- | nslcd/common.h | 2 | ||||
-rw-r--r-- | nslcd/pam.c | 15 | ||||
-rw-r--r-- | nslcd/passwd.c | 36 |
3 files changed, 27 insertions, 26 deletions
diff --git a/nslcd/common.h b/nslcd/common.h index c2bab4d..a7cc18a 100644 --- a/nslcd/common.h +++ b/nslcd/common.h @@ -83,7 +83,7 @@ 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); +MUST_USE char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn,int *rcp,char *buf,size_t buflen); /* 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); diff --git a/nslcd/pam.c b/nslcd/pam.c index 35bf8d8..0467280 100644 --- a/nslcd/pam.c +++ b/nslcd/pam.c @@ -47,7 +47,7 @@ static int try_bind(const char *userdn,const char *password) { MYLDAP_SESSION *session; - char *username; + char buffer[256]; int rc; /* set up a new connection */ session=myldap_create_session(); @@ -56,9 +56,8 @@ static int try_bind(const char *userdn,const char *password) /* set up credentials for the session */ myldap_set_credentials(session,userdn,password); /* perform search for own object (just to do any kind of search) */ - username=lookup_dn2uid(session,userdn,&rc); - if (username!=NULL) - free(username); + if ((lookup_dn2uid(session,userdn,&rc,buffer,sizeof(buffer))==NULL)&&(rc==LDAP_SUCCESS)) + rc=LDAP_LOCAL_ERROR; /* close the session */ myldap_session_close(session); /* handle the results */ @@ -404,7 +403,7 @@ static int try_pwmod(const char *binddn,const char *userdn, const char *oldpassword,const char *newpassword) { MYLDAP_SESSION *session; - char *username; + char buffer[256]; int rc; /* set up a new connection */ session=myldap_create_session(); @@ -413,11 +412,7 @@ static int try_pwmod(const char *binddn,const char *userdn, /* set up credentials for the session */ myldap_set_credentials(session,binddn,oldpassword); /* perform search for own object (just to do any kind of search) */ - username=lookup_dn2uid(session,userdn,&rc); - if (username!=NULL) - free(username); - /* perform actual password modification */ - if (rc==LDAP_SUCCESS) + if ((lookup_dn2uid(session,userdn,&rc,buffer,sizeof(buffer))!=NULL)&&(rc==LDAP_SUCCESS)) { /* if doing password modification as admin, don't pass old password along */ if ((nslcd_cfg->ldc_rootpwmoddn!=NULL)&&(strcmp(binddn,nslcd_cfg->ldc_rootpwmoddn)==0)) diff --git a/nslcd/passwd.c b/nslcd/passwd.c index 8ef2a5c..a0b61d7 100644 --- a/nslcd/passwd.c +++ b/nslcd/passwd.c @@ -140,14 +140,14 @@ 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. */ -char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn,int *rcp) +char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn,int *rcp,char *buf,size_t buflen) { MYLDAP_SEARCH *search; MYLDAP_ENTRY *entry; static const char *attrs[2]; int rc=LDAP_SUCCESS; const char **values; - char *uid; + char *uid=NULL; if (rcp==NULL) rcp=&rc; /* we have to look up the entry */ @@ -169,10 +169,12 @@ char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn,int *rcp) /* get uid (just use first one) */ values=myldap_get_values(entry,attmap_passwd_uid); /* check the result for presence and validity */ - if ((values!=NULL)&&(values[0]!=NULL)&&isvalidname(values[0])) - uid=strdup(values[0]); - else - uid=NULL; + if ((values!=NULL)&&(values[0]!=NULL)&&isvalidname(values[0])&&(strlen(values[0])<buflen)) + { + strcpy(buf,values[0]); + uid=buf; + } + /* clean up and return */ myldap_search_close(search); return uid; } @@ -216,7 +218,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,NULL); + uid=lookup_dn2uid(session,dn,NULL,buf,buflen); /* store the result in the cache */ pthread_mutex_lock(&dn2uid_cache_mutex); /* try to get the entry from the cache here again because it could have @@ -227,23 +229,27 @@ char *dn2uid(MYLDAP_SESSION *session,const char *dn,char *buf,size_t buflen) /* allocate a new entry in the cache */ cacheentry=(struct dn2uid_cache_entry *)malloc(sizeof(struct dn2uid_cache_entry)); if (cacheentry!=NULL) + { + cacheentry->uid=NULL; dict_put(dn2uid_cache,dn,cacheentry); + } } - else if (cacheentry->uid!=NULL) - free(cacheentry->uid); /* update the cache entry */ if (cacheentry!=NULL) { cacheentry->timestamp=time(NULL); - cacheentry->uid=uid; + /* copy the uid if needed */ + if (cacheentry->uid==NULL) + cacheentry->uid=uid!=NULL?strdup(uid):NULL; + else if (strcmp(cacheentry->uid,uid)!=0) + { + free(cacheentry->uid); + cacheentry->uid=uid!=NULL?strdup(uid):NULL; + } } pthread_mutex_unlock(&dn2uid_cache_mutex); /* copy the result into the buffer */ - if ((uid!=NULL)&&(strlen(uid)<buflen)) - strcpy(buf,uid); - else - buf=NULL; - return buf; + return uid; } MYLDAP_ENTRY *uid2entry(MYLDAP_SESSION *session,const char *uid) |