lists.arthurdejong.org
RSS feed

nss-pam-ldapd commit: r1297 - nss-pam-ldapd/nslcd

[Date Prev][Date Next] [Thread Prev][Thread Next]

nss-pam-ldapd commit: r1297 - nss-pam-ldapd/nslcd



Author: arthur
Date: Thu Nov  4 21:36:13 2010
New Revision: 1297
URL: http://arthurdejong.org/viewvc/nss-pam-ldapd?view=rev&revision=1297

Log:
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

Modified:
   nss-pam-ldapd/nslcd/common.h
   nss-pam-ldapd/nslcd/pam.c
   nss-pam-ldapd/nslcd/passwd.c

Modified: nss-pam-ldapd/nslcd/common.h
==============================================================================
--- nss-pam-ldapd/nslcd/common.h        Thu Nov  4 21:35:54 2010        (r1296)
+++ nss-pam-ldapd/nslcd/common.h        Thu Nov  4 21:36:13 2010        (r1297)
@@ -83,7 +83,7 @@
 
 /* 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);

Modified: nss-pam-ldapd/nslcd/pam.c
==============================================================================
--- nss-pam-ldapd/nslcd/pam.c   Thu Nov  4 21:35:54 2010        (r1296)
+++ nss-pam-ldapd/nslcd/pam.c   Thu Nov  4 21:36:13 2010        (r1297)
@@ -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 @@
   /* 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 @@
                      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 @@
   /* 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))

Modified: nss-pam-ldapd/nslcd/passwd.c
==============================================================================
--- nss-pam-ldapd/nslcd/passwd.c        Thu Nov  4 21:35:54 2010        (r1296)
+++ nss-pam-ldapd/nslcd/passwd.c        Thu Nov  4 21:36:13 2010        (r1297)
@@ -140,14 +140,14 @@
 
 /* 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 @@
   /* 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 @@
   }
   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 @@
     /* 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)
--
To unsubscribe send an email to
nss-pam-ldapd-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/nss-pam-ldapd-commits