lists.arthurdejong.org
RSS feed

nss-pam-ldapd commit: r1766 - nss-pam-ldapd/nss

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

nss-pam-ldapd commit: r1766 - nss-pam-ldapd/nss



Author: arthur
Date: Fri Sep 14 23:08:48 2012
New Revision: 1766
URL: http://arthurdejong.org/viewvc/nss-pam-ldapd?revision=1766&view=revision

Log:
fix host name lookups for Solaris (tested with Solaris 10)

Modified:
   nss-pam-ldapd/nss/hosts.c

Modified: nss-pam-ldapd/nss/hosts.c
==============================================================================
--- nss-pam-ldapd/nss/hosts.c   Fri Sep 14 20:57:04 2012        (r1765)
+++ nss-pam-ldapd/nss/hosts.c   Fri Sep 14 23:08:48 2012        (r1766)
@@ -242,110 +242,87 @@
 
 #ifdef NSS_FLAVOUR_SOLARIS
 
-struct nss_ldap_hosts_backend
-{
-  nss_backend_op_t *ops;
-  int n_ops;
-  TFILE *fp;
-};
-
 #ifdef HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN
-
-static nss_status_t read_hoststring(TFILE *fp,nss_XbyY_args_t *args,int 
erronempty)
+static nss_status_t read_hoststring(TFILE *fp,int af,nss_XbyY_args_t *args,int 
erronempty)
 {
   struct hostent result;
-  nss_status_t retv;
-  char *buffer;
-  size_t buflen;
-  int i;
-  /* read the hostent */
+  char buffer[NSS_BUFLEN_HOSTS];
+  int retv;
+  int i,j;
+  /* read the result entry */
   if (erronempty)
-    
retv=read_hostent_erronempty(fp,NSS_ARGS(args)->key.hostaddr.type,&result,NSS_ARGS(args)->buf.buffer,args->buf.buflen,&NSS_ARGS(args)->erange,&(NSS_ARGS(args)->h_errno));
+    
retv=read_hostent_erronempty(fp,af,&result,buffer,sizeof(buffer),&args->erange,&args->h_errno);
   else
-    
retv=read_hostent_nextonempty(fp,NSS_ARGS(args)->key.hostaddr.type,&result,NSS_ARGS(args)->buf.buffer,args->buf.buflen,&NSS_ARGS(args)->erange,&(NSS_ARGS(args)->h_errno));
+    
retv=read_hostent_nextonempty(fp,af,&result,buffer,sizeof(buffer),&args->erange,&args->h_errno);
   if (retv!=NSS_STATUS_SUCCESS)
     return retv;
-  /* allocate a temporary buffer */
-  buflen=args->buf.buflen;
-  buffer=(char *)malloc(buflen);
-  /* build the formatted string */
-  /* FIXME: implement proper buffer size checking */
-  if (result.h_addr_list)
+  /* build the formatted string, one line per address */
+  args->buf.buffer[0]='\0';
+  if (result.h_addr_list!=NULL)
   {
-    struct in_addr in;
-    (void)memcpy(&in.s_addr,result.h_addr_list[0],sizeof(in.s_addr));
-    sprintf(buffer,"%s %s",inet_ntoa(in),result.h_name);
-    if (result.h_aliases)
+    for (i=0;result.h_addr_list[i];i++)
     {
-      int j;
-      for (j=0;result.h_aliases[j];j++)
+      if (i>0)
+        strlcat(args->buf.buffer,"\n",args->buf.buflen);
+      snprintf(args->buf.buffer,args->buf.buflen-strlen(args->buf.buffer)-1,
+               "%s %s",inet_ntoa(*((struct in_addr 
*)result.h_addr_list[i])),result.h_name);
+      /* add aliases for first line only */
+      if ((i==0)&&(result.h_aliases))
       {
-        strcat(buffer,"  ");
-        strcat(buffer,result.h_aliases[j]);
+        for (j=0;result.h_aliases[j];j++)
+        {
+          strlcat(args->buf.buffer," ",args->buf.buflen);
+          strlcat(args->buf.buffer,result.h_aliases[j],args->buf.buflen);
+        }
       }
     }
-    for (i=1;result.h_addr_list[i];i++)
-    {
-      (void)memcpy(&in.s_addr,result.h_addr_list[i],sizeof(in.s_addr));
-      strcat(buffer,"\n");
-      strcat(buffer,inet_ntoa(in));
-      strcat(buffer," ");
-      strcat(buffer,result.h_name);
-      /* TODO: aliases only supplied to the first address */
-      /* need review */
-    }
   }
-  /* copy the result back to the result buffer and free the temporary one */
-  strcpy(NSS_ARGS(args)->buf.buffer,buffer);
-  free(buffer);
-  NSS_ARGS(args)->returnval=NSS_ARGS(args)->buf.buffer;
-  NSS_ARGS(args)->returnlen=strlen(NSS_ARGS(args)->buf.buffer);
+  if (strlen(args->buf.buffer)>=args->buf.buflen-1)
+    return NSS_STR_PARSE_PARSE;
+  args->returnval=args->buf.buffer;
+  args->returnlen=strlen(args->returnval);
   return NSS_STATUS_SUCCESS;
 }
+#endif /* not HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN */
 
-#define READ_RESULT_ERRONEMPTY(fp) \
-  NSS_ARGS(args)->buf.result? \
-    read_hostent_erronempty(fp,NSS_ARGS(args)->key.hostaddr.type,(struct 
hostent 
*)NSS_ARGS(args)->buf.result,NSS_ARGS(args)->buf.buffer,NSS_ARGS(args)->buf.buflen,&NSS_ARGS(args)->erange,&(NSS_ARGS(args)->h_errno)):
 \
-    read_hoststring(fp,args,1); \
-  if ((NSS_ARGS(args)->buf.result)&&(retv==NSS_STATUS_SUCCESS)) \
-    NSS_ARGS(args)->returnval=NSS_ARGS(args)->buf.result;
-
-#define READ_RESULT_NEXTONEMPTY(fp) \
-  NSS_ARGS(args)->buf.result? \
-    read_hostent_nextonempty(fp,NSS_ARGS(args)->key.hostaddr.type,(struct 
hostent 
*)NSS_ARGS(args)->buf.result,NSS_ARGS(args)->buf.buffer,NSS_ARGS(args)->buf.buflen,&NSS_ARGS(args)->erange,&(NSS_ARGS(args)->h_errno)):
 \
-    read_hoststring(fp,args,0); \
-  if ((NSS_ARGS(args)->buf.result)&&(retv==NSS_STATUS_SUCCESS)) \
-    NSS_ARGS(args)->returnval=NSS_ARGS(args)->buf.result;
-
-#else /* not HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN */
-
-#define READ_RESULT_ERRONEMPTY(fp) \
-  read_hostent_erronempty(fp,NSS_ARGS(args)->key.hostaddr.type,(struct hostent 
*)NSS_ARGS(args)->buf.result,NSS_ARGS(args)->buf.buffer,NSS_ARGS(args)->buf.buflen,&NSS_ARGS(args)->erange,&(NSS_ARGS(args)->h_errno));
 \
-  if (retv==NSS_STATUS_SUCCESS) \
-    NSS_ARGS(args)->returnval=NSS_ARGS(args)->buf.result;
-
-#define READ_RESULT_NEXTONEMPTY(fp) \
-  read_hostent_nextonempty(fp,NSS_ARGS(args)->key.hostaddr.type,(struct 
hostent 
*)NSS_ARGS(args)->buf.result,NSS_ARGS(args)->buf.buffer,NSS_ARGS(args)->buf.buflen,&NSS_ARGS(args)->erange,&(NSS_ARGS(args)->h_errno));
 \
-  if (retv==NSS_STATUS_SUCCESS) \
-    NSS_ARGS(args)->returnval=NSS_ARGS(args)->buf.result;
-
+static nss_status_t read_result(TFILE *fp,int af,nss_XbyY_args_t *args,int 
erronempty)
+{
+  nss_status_t retv;
+#ifdef HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN
+  /* try to return in string format if requested */
+  if (args->buf.result==NULL)
+    return read_hoststring(fp,af,args,erronempty);
 #endif /* not HAVE_STRUCT_NSS_XBYY_ARGS_RETURNLEN */
+  /* read the hostent */
+  if (erronempty)
+    retv=read_hostent_erronempty(fp,af,
+          args->buf.result,args->buf.buffer,args->buf.buflen,
+          &args->erange,&args->h_errno);
+  else
+    retv=read_hostent_nextonempty(fp,af,
+          args->buf.result,args->buf.buffer,args->buf.buflen,
+          &args->erange,&args->h_errno);
+  if (retv!=NSS_STATUS_SUCCESS)
+    return retv;
+  args->returnval=args->buf.result;
+  return NSS_STATUS_SUCCESS;
+}
 
-/* hack to set the correct errno and h_errno */
+/* hack to set the correct h_errno */
 #define h_errnop &(NSS_ARGS(args)->h_errno)
 
 static nss_status_t hosts_gethostbyname(nss_backend_t UNUSED(*be),void *args)
 {
   NSS_BYNAME(NSLCD_ACTION_HOST_BYNAME,
              NSS_ARGS(args)->key.name,
-             READ_RESULT_ERRONEMPTY(fp));
+             read_result(fp,AF_INET,args,1));
 }
 
 static nss_status_t hosts_gethostbyaddr(nss_backend_t UNUSED(*be),void *args)
 {
   NSS_BYGEN(NSLCD_ACTION_HOST_BYADDR,
             
WRITE_ADDRESS(fp,NSS_ARGS(args)->key.hostaddr.type,NSS_ARGS(args)->key.hostaddr.len,NSS_ARGS(args)->key.hostaddr.addr),
-            READ_RESULT_ERRONEMPTY(fp));
+            read_result(fp,NSS_ARGS(args)->key.hostaddr.type,args,1));
 }
 
 static nss_status_t hosts_sethostent(nss_backend_t *be,void UNUSED(*args))
@@ -356,7 +333,7 @@
 static nss_status_t hosts_gethostent(nss_backend_t *be,void *args)
 {
   NSS_GETENT(LDAP_BE(be)->fp,NSLCD_ACTION_HOST_ALL,
-             READ_RESULT_NEXTONEMPTY(LDAP_BE(be)->fp));
+             read_result(LDAP_BE(be)->fp,AF_INET,args,0));
 }
 
 static nss_status_t hosts_endhostent(nss_backend_t *be,void UNUSED(*args))
-- 
To unsubscribe send an email to
nss-pam-ldapd-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/nss-pam-ldapd-commits/